List of usage examples for android.database DatabaseUtils sqlEscapeString
public static String sqlEscapeString(String value)
From source file:at.bitfire.davdroid.resource.LocalCollection.java
/** * Deletes all resources except the give ones from the local collection. * @param remoteResources resources with these remote file names will be kept * @return number of deleted resources/*w ww.j av a 2 s . c o m*/ */ public int deleteAllExceptRemoteNames(Resource[] remoteResources) throws LocalStorageException { final String where; if (remoteResources.length != 0) { // delete all except certain entries final List<String> sqlFileNames = new LinkedList<>(); for (final Resource res : remoteResources) sqlFileNames.add(DatabaseUtils.sqlEscapeString(res.getName())); where = entryColumnRemoteName() + " NOT IN (" + StringUtils.join(sqlFileNames, ",") + ')'; } else // delete all entries where = entryColumnRemoteName() + " IS NOT NULL"; try { if (entryColumnParentID() != null) // entries have a parent collection (for instance, events which have a calendar) return providerClient.delete(entriesURI(), entryColumnParentID() + "=? AND (" + where + ')', // restrict deletion to parent collection new String[] { String.valueOf(getId()) }); else // entries don't have a parent collection (contacts are stored directly and not within an address book) return providerClient.delete(entriesURI(), where, null); } catch (RemoteException e) { throw new LocalStorageException("Couldn't delete local resources", e); } }
From source file:com.android.messaging.datamodel.ParticipantRefresh.java
/** * Ensure that there is a self participant corresponding to every active SIM. Also, ensure * that any other older SIM self participants are marked as inactive. *//*from w ww. ja va 2 s .c o m*/ private static void refreshSelfParticipantList() { if (!OsUtil.isAtLeastL_MR1()) { return; } final DatabaseWrapper db = DataModel.get().getDatabase(); final List<SubscriptionInfo> subInfoRecords = PhoneUtils.getDefault().toLMr1() .getActiveSubscriptionInfoList(); final ArrayMap<Integer, SubscriptionInfo> activeSubscriptionIdToRecordMap = new ArrayMap<Integer, SubscriptionInfo>(); db.beginTransaction(); final Set<Integer> existingSubIds = getExistingSubIds(); try { if (subInfoRecords != null) { for (final SubscriptionInfo subInfoRecord : subInfoRecords) { final int subId = subInfoRecord.getSubscriptionId(); // If its a new subscription, add it to the database. if (!existingSubIds.contains(subId)) { db.execSQL(DatabaseHelper.getCreateSelfParticipantSql(subId)); // Add it to the local set to guard against duplicated entries returned // by subscription manager. existingSubIds.add(subId); } activeSubscriptionIdToRecordMap.put(subId, subInfoRecord); if (subId == PhoneUtils.getDefault().getDefaultSmsSubscriptionId()) { // This is the system default subscription, so update the default self. activeSubscriptionIdToRecordMap.put(ParticipantData.DEFAULT_SELF_SUB_ID, subInfoRecord); } } } // For subscriptions already in the database, refresh ParticipantColumns.SIM_SLOT_ID. for (final Integer subId : activeSubscriptionIdToRecordMap.keySet()) { final SubscriptionInfo record = activeSubscriptionIdToRecordMap.get(subId); final String displayName = DatabaseUtils.sqlEscapeString(record.getDisplayName().toString()); db.execSQL(getUpdateSelfParticipantSubscriptionInfoSql(record.getSimSlotIndex(), record.getIconTint(), displayName, ParticipantColumns.SUB_ID + " = " + subId)); } db.execSQL(getUpdateSelfParticipantSubscriptionInfoSql(ParticipantData.INVALID_SLOT_ID, Color.TRANSPARENT, "''", ParticipantColumns.SUB_ID + " NOT IN (" + Joiner.on(", ").join(activeSubscriptionIdToRecordMap.keySet()) + ")")); db.setTransactionSuccessful(); } finally { db.endTransaction(); } // Fix up conversation self ids by reverting to default self for conversations whose self // ids are no longer active. refreshConversationSelfIds(); }
From source file:com.gelakinetic.mtgfam.helpers.CardDbAdapter.java
public Cursor fetchCardByName(String name, String[] fields) throws FamiliarDbException { // replace lowercase ae with Ae name = name.replace(Character.toChars(0xE6)[0], Character.toChars(0xC6)[0]); String sql = "SELECT "; boolean first = true; for (String field : fields) { if (first) { first = false;/*from w w w . j a va 2 s . c o m*/ } else { sql += ", "; } sql += DATABASE_TABLE_CARDS + "." + field; } sql += " FROM " + DATABASE_TABLE_CARDS + " JOIN " + DATABASE_TABLE_SETS + " ON " + DATABASE_TABLE_SETS + "." + KEY_CODE + " = " + DATABASE_TABLE_CARDS + "." + KEY_SET + " WHERE " + DATABASE_TABLE_CARDS + "." + KEY_NAME + " = " + DatabaseUtils.sqlEscapeString(name) + " ORDER BY " + DATABASE_TABLE_SETS + "." + KEY_DATE + " DESC"; Cursor mCursor = null; try { mCursor = mDb.rawQuery(sql, null); } catch (SQLiteException e) { throw new FamiliarDbException(e); } catch (IllegalStateException e) { throw new FamiliarDbException(e); } if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; }
From source file:com.gelakinetic.mtgfam.helpers.CardDbAdapter.java
public Cursor fetchLatestCardByName(String name, String[] fields) throws FamiliarDbException { // replace lowercase ae with Ae name = name.replace(Character.toChars(0xE6)[0], Character.toChars(0xC6)[0]); String sql = "SELECT "; boolean first = true; for (String field : fields) { if (first) { first = false;// ww w . j av a2 s. c o m } else { sql += ", "; } sql += DATABASE_TABLE_CARDS + "." + field; } sql += " FROM " + DATABASE_TABLE_CARDS + " JOIN " + DATABASE_TABLE_SETS + " ON " + DATABASE_TABLE_SETS + "." + KEY_CODE + " = " + DATABASE_TABLE_CARDS + "." + KEY_SET + " WHERE " + DATABASE_TABLE_CARDS + "." + KEY_NAME + " = " + DatabaseUtils.sqlEscapeString(name) + " ORDER BY " + DATABASE_TABLE_SETS + "." + KEY_DATE + " DESC LIMIT 1"; Cursor mCursor = null; try { mCursor = mDb.rawQuery(sql, null); } catch (SQLiteException e) { throw new FamiliarDbException(e); } catch (IllegalStateException e) { throw new FamiliarDbException(e); } if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; }
From source file:com.gelakinetic.mtgfam.helpers.CardDbAdapter.java
public Cursor fetchCardByNameAndSet(String name, String setCode) throws FamiliarDbException { // replace lowercase ae with Ae name = name.replace(Character.toChars(0xE6)[0], Character.toChars(0xC6)[0]); String sql = "SELECT " + DATABASE_TABLE_CARDS + "." + KEY_ID + ", " + DATABASE_TABLE_CARDS + "." + KEY_NAME + ", " + DATABASE_TABLE_CARDS + "." + KEY_SET + ", " + DATABASE_TABLE_CARDS + "." + KEY_NUMBER + ", " + DATABASE_TABLE_CARDS + "." + KEY_TYPE + ", " + DATABASE_TABLE_CARDS + "." + KEY_MANACOST + ", " + DATABASE_TABLE_CARDS + "." + KEY_ABILITY + ", " + DATABASE_TABLE_CARDS + "." + KEY_POWER + ", " + DATABASE_TABLE_CARDS + "." + KEY_TOUGHNESS + ", " + DATABASE_TABLE_CARDS + "." + KEY_LOYALTY + ", " + DATABASE_TABLE_CARDS + "." + KEY_RARITY + " FROM " + DATABASE_TABLE_CARDS + " JOIN " + DATABASE_TABLE_SETS + " ON " + DATABASE_TABLE_SETS + "." + KEY_CODE + " = " + DATABASE_TABLE_CARDS + "." + KEY_SET + " WHERE " + DATABASE_TABLE_CARDS + "." + KEY_NAME + " = " + DatabaseUtils.sqlEscapeString(name) + " AND " + DATABASE_TABLE_CARDS + "." + KEY_SET + " = '" + setCode + "' ORDER BY " + DATABASE_TABLE_SETS + "." + KEY_DATE + " DESC"; Cursor mCursor = null;//from w w w .j a v a2 s . c om try { mCursor = mDb.rawQuery(sql, null); } catch (SQLiteException e) { throw new FamiliarDbException(e); } catch (IllegalStateException e) { throw new FamiliarDbException(e); } if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; }
From source file:com.gelakinetic.mtgfam.helpers.CardDbAdapter.java
public long fetchIdByName(String name) throws FamiliarDbException { // replace lowercase ae with Ae name = name.replace(Character.toChars(0xE6)[0], Character.toChars(0xC6)[0]); String sql = "SELECT " + DATABASE_TABLE_CARDS + "." + KEY_ID + ", " + DATABASE_TABLE_CARDS + "." + KEY_SET + ", " + DATABASE_TABLE_SETS + "." + KEY_DATE + " FROM (" + DATABASE_TABLE_CARDS + " JOIN " + DATABASE_TABLE_SETS + " ON " + DATABASE_TABLE_CARDS + "." + KEY_SET + "=" + DATABASE_TABLE_SETS + "." + KEY_CODE + ")" + " WHERE " + DATABASE_TABLE_CARDS + "." + KEY_NAME + " = " + DatabaseUtils.sqlEscapeString(name) + " ORDER BY " + DATABASE_TABLE_SETS + "." + KEY_DATE + " DESC"; Cursor mCursor = null;/* w w w. j a v a2 s .c o m*/ try { mCursor = mDb.rawQuery(sql, null); } catch (SQLiteException e) { throw new FamiliarDbException(e); } catch (IllegalStateException e) { throw new FamiliarDbException(e); } if (mCursor != null) { mCursor.moveToFirst(); long id = mCursor.getLong(mCursor.getColumnIndex(CardDbAdapter.KEY_ID)); mCursor.close(); return id; } return -1; }
From source file:com.gelakinetic.mtgfam.helpers.CardDbAdapter.java
public Cursor autoComplete(String cardname) throws FamiliarDbException { Cursor mCursor = null;/*w w w . j a v a2 s .com*/ String convertName = null; if (cardname != null) { cardname = cardname.replace(Character.toChars(0xE6)[0], Character.toChars(0xC6)[0]).trim(); convertName = cardname.toLowerCase().replace("ae", String.valueOf(Character.toChars(0xC6)[0])); } String sql = "SELECT MIN(" + KEY_ID + ") AS " + KEY_ID + ", " + KEY_NAME + " FROM " + DATABASE_TABLE_CARDS + " WHERE " + KEY_NAME + " LIKE " + DatabaseUtils.sqlEscapeString(cardname + "%") + " OR " + KEY_NAME + " LIKE " + DatabaseUtils.sqlEscapeString(convertName + "%") + "GROUP BY " + KEY_NAME + " ORDER BY " + KEY_NAME + " COLLATE UNICODE"; try { mCursor = mDb.rawQuery(sql, null); } catch (SQLiteException e) { throw new FamiliarDbException(e); } catch (IllegalStateException e) { throw new FamiliarDbException(e); } if (mCursor != null) { mCursor.moveToFirst(); } return mCursor; }
From source file:group.pals.android.lib.ui.filechooser.utils.ui.bookmark.BookmarkFragment.java
/** * Shows a dialog to let user enter new name or change current name of a * bookmark.//from w ww .j a va2 s . co m * * @param context * {@link Context} * @param providerId * the provider ID. * @param id * the bookmark ID. * @param uri * the URI to the bookmark. * @param name * the name. To enter new name, this is the suggested name you * provide. To rename, this is the old name. */ public static void doEnterNewNameOrRenameBookmark(final Context context, final String providerId, final int id, final Uri uri, final String name) { final AlertDialog dialog = Dlg.newDlg(context); View view = LayoutInflater.from(context).inflate(R.layout.afc_simple_text_input_view, null); final EditText textName = (EditText) view.findViewById(R.id.afc_text1); textName.setText(name); textName.selectAll(); textName.setHint(R.string.afc_hint_new_name); textName.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { Ui.showSoftKeyboard(textName, false); Button btn = dialog.getButton(DialogInterface.BUTTON_POSITIVE); if (btn.isEnabled()) btn.performClick(); return true; } return false; }// onEditorAction() }); dialog.setView(view); dialog.setIcon(R.drawable.afc_bookmarks_dark); dialog.setTitle(id < 0 ? R.string.afc_title_new_bookmark : R.string.afc_title_rename); dialog.setButton(DialogInterface.BUTTON_POSITIVE, context.getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String newName = textName.getText().toString().trim(); if (android.text.TextUtils.isEmpty(newName)) { Dlg.toast(context, R.string.afc_msg_bookmark_name_is_invalid, Dlg._LengthShort); return; } Ui.showSoftKeyboard(textName, false); ContentValues values = new ContentValues(); values.put(BookmarkContract.Bookmark._ColumnName, newName); if (id >= 0) { values.put(BookmarkContract.Bookmark._ColumnModificationTime, DbUtils.formatNumber(new Date().getTime())); context.getContentResolver() .update(Uri.withAppendedPath(BookmarkContract.Bookmark._ContentIdUriBase, Uri.encode(Integer.toString(id))), values, null, null); } else { /* * Check if the URI exists or doesn't. If it exists, * update it instead of inserting the new one. */ Cursor cursor = context.getContentResolver().query( BookmarkContract.Bookmark._ContentUri, null, String.format("%s = %s AND %s LIKE %s", BookmarkContract.Bookmark._ColumnProviderId, DatabaseUtils.sqlEscapeString(providerId), BookmarkContract.Bookmark._ColumnUri, DatabaseUtils.sqlEscapeString(uri.toString())), null, null); try { if (cursor != null && cursor.moveToFirst()) { values.put(BookmarkContract.Bookmark._ColumnModificationTime, DbUtils.formatNumber(new Date().getTime())); context.getContentResolver().update( Uri.withAppendedPath(BookmarkContract.Bookmark._ContentIdUriBase, Uri.encode(cursor.getString( cursor.getColumnIndex(BookmarkContract.Bookmark._ID)))), values, null, null); } else { values.put(BookmarkContract.Bookmark._ColumnProviderId, providerId); values.put(BookmarkContract.Bookmark._ColumnUri, uri.toString()); context.getContentResolver().insert(BookmarkContract.Bookmark._ContentUri, values); } } finally { if (cursor != null) cursor.close(); } } Dlg.toast(context, context.getString(R.string.afc_msg_done), Dlg._LengthShort); }// onClick() }); dialog.show(); Ui.showSoftKeyboard(textName, true); final Button buttonOk = dialog.getButton(DialogInterface.BUTTON_POSITIVE); buttonOk.setEnabled(id < 0); textName.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { String newName = s.toString().trim(); boolean enabled = !android.text.TextUtils.isEmpty(newName); buttonOk.setEnabled(enabled); /* * If renaming, only enable button OK if new name is not equal * to the old one. */ if (enabled && id >= 0) buttonOk.setEnabled(!newName.equals(name)); } }); }
From source file:com.haibison.android.anhuu.utils.ui.bookmark.BookmarkFragment.java
/** * Shows a dialog to let the user enter new name or change current name of a * bookmark.//from w w w . ja v a2s . c o m * * @param context * {@link Context} * @param providerId * the provider ID. * @param id * the bookmark ID. * @param uri * the URI to the bookmark. * @param name * the name. To enter new name, this is the suggested name you * provide. To rename, this is the old name. */ public static void doEnterNewNameOrRenameBookmark(final Context context, final String providerId, final int id, final Uri uri, final String name) { final AlertDialog dialog = Dlg.newAlertDlg(context); View view = LayoutInflater.from(context).inflate(R.layout.anhuu_f5be488d_simple_text_input_view, null); final EditText textName = (EditText) view.findViewById(R.id.anhuu_f5be488d_text1); textName.setText(name); textName.selectAll(); textName.setHint(R.string.anhuu_f5be488d_hint_new_name); textName.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { UI.showSoftKeyboard(textName, false); Button btn = dialog.getButton(DialogInterface.BUTTON_POSITIVE); if (btn.isEnabled()) btn.performClick(); return true; } return false; }// onEditorAction() }); dialog.setView(view); dialog.setIcon(R.drawable.anhuu_f5be488d_bookmarks_dark); dialog.setTitle(id < 0 ? R.string.anhuu_f5be488d_title_new_bookmark : R.string.anhuu_f5be488d_title_rename); dialog.setButton(DialogInterface.BUTTON_POSITIVE, context.getString(android.R.string.ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String newName = textName.getText().toString().trim(); if (android.text.TextUtils.isEmpty(newName)) { Dlg.toast(context, R.string.anhuu_f5be488d_msg_bookmark_name_is_invalid, Dlg.LENGTH_SHORT); return; } UI.showSoftKeyboard(textName, false); ContentValues values = new ContentValues(); values.put(BookmarkContract.COLUMN_NAME, newName); if (id >= 0) { values.put(BookmarkContract.COLUMN_MODIFICATION_TIME, DbUtils.formatNumber(new Date().getTime())); context.getContentResolver().update( ContentUris.withAppendedId(BookmarkContract.genContentIdUriBase(context), id), values, null, null); } else { /* * Check if the URI exists or doesn't. If it exists, * update it instead of inserting the new one. */ Cursor cursor = context.getContentResolver().query( BookmarkContract.genContentUri(context), null, String.format("%s = %s AND %s LIKE %s", BookmarkContract.COLUMN_PROVIDER_ID, DatabaseUtils.sqlEscapeString(providerId), BookmarkContract.COLUMN_URI, DatabaseUtils.sqlEscapeString(uri.toString())), null, null); try { if (cursor != null && cursor.moveToFirst()) { values.put(BookmarkContract.COLUMN_MODIFICATION_TIME, DbUtils.formatNumber(new Date().getTime())); context.getContentResolver().update( Uri.withAppendedPath(BookmarkContract.genContentIdUriBase(context), Uri.encode(cursor.getString( cursor.getColumnIndex(BookmarkContract._ID)))), values, null, null); } else { values.put(BookmarkContract.COLUMN_PROVIDER_ID, providerId); values.put(BookmarkContract.COLUMN_URI, uri.toString()); context.getContentResolver().insert(BookmarkContract.genContentUri(context), values); } } finally { if (cursor != null) cursor.close(); } } Dlg.toast(context, context.getString(R.string.anhuu_f5be488d_msg_done), Dlg.LENGTH_SHORT); }// onClick() }); dialog.show(); UI.showSoftKeyboard(textName, true); final Button buttonOk = dialog.getButton(DialogInterface.BUTTON_POSITIVE); buttonOk.setEnabled(id < 0); textName.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { String newName = s.toString().trim(); boolean enabled = !android.text.TextUtils.isEmpty(newName); buttonOk.setEnabled(enabled); /* * If renaming, only enable button OK if new name is not equal * to the old one. */ if (enabled && id >= 0) buttonOk.setEnabled(!newName.equals(name)); } }); }
From source file:org.thialfihar.android.apg.provider.ProviderHelper.java
/** * TODO: currently not used, but will be needed to upload many keys at once! * * @param masterKeyIds/*w w w. j ava 2 s. c om*/ * @return * @throws IOException */ public ArrayList<String> getKeyRingsAsArmoredString(long[] masterKeyIds) throws IOException { ArrayList<String> output = new ArrayList<String>(); if (masterKeyIds == null || masterKeyIds.length == 0) { Log.e(Constants.TAG, "No master keys given!"); return output; } // Build a cursor for the selected masterKeyIds Cursor cursor; { String inMasterKeyList = KeyRingData.MASTER_KEY_ID + " IN ("; for (int i = 0; i < masterKeyIds.length; ++i) { if (i != 0) { inMasterKeyList += ", "; } inMasterKeyList += DatabaseUtils.sqlEscapeString("" + masterKeyIds[i]); } inMasterKeyList += ")"; cursor = mContentResolver.query(KeyRingData.buildPublicKeyRingUri(), new String[] { KeyRingData._ID, KeyRingData.MASTER_KEY_ID, KeyRingData.KEY_RING_DATA }, inMasterKeyList, null, null); } try { if (cursor != null) { int masterIdCol = cursor.getColumnIndex(KeyRingData.MASTER_KEY_ID); int dataCol = cursor.getColumnIndex(KeyRingData.KEY_RING_DATA); if (cursor.moveToFirst()) { do { Log.d(Constants.TAG, "masterKeyId: " + cursor.getLong(masterIdCol)); byte[] data = cursor.getBlob(dataCol); // get actual keyring data blob and write it to ByteArrayOutputStream try { output.add(getKeyRingAsArmoredString(data)); } catch (IOException e) { Log.e(Constants.TAG, "IOException", e); } } while (cursor.moveToNext()); } } } finally { if (cursor != null) { cursor.close(); } } if (output.size() > 0) { return output; } else { return null; } }