List of usage examples for android.content ContentResolver query
public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder)
From source file:com.android.calendar.event.EventLocationAdapter.java
/** * Matches the input string against contacts names and addresses. * * @param resolver The content resolver. * @param input The user-typed input string. * @param addressesRetVal The addresses in the returned result are also returned here * for faster lookup. Pass in an empty set. * @return Ordered list of all the matched results. If there are multiple address matches * for the same contact, they will be listed together in individual items, with only * the first item containing a name/icon. *///from w w w . j ava2 s .c o m private static List<Result> queryContacts(ContentResolver resolver, String input, HashSet<String> addressesRetVal) { String where = null; String[] whereArgs = null; // Match any word in contact name or address. if (!TextUtils.isEmpty(input)) { where = CONTACTS_WHERE; String param1 = input + "%"; String param2 = "% " + input + "%"; whereArgs = new String[] { param1, param2, param1, param2 }; } // Perform the query. Cursor c = resolver.query(CommonDataKinds.StructuredPostal.CONTENT_URI, CONTACTS_PROJECTION, where, whereArgs, Contacts.DISPLAY_NAME + " ASC"); // Process results. Group together addresses for the same contact. try { Map<String, List<Result>> nameToAddresses = new HashMap<String, List<Result>>(); c.moveToPosition(-1); while (c.moveToNext()) { String name = c.getString(CONTACTS_INDEX_DISPLAY_NAME); String address = c.getString(CONTACTS_INDEX_ADDRESS); if (name != null) { List<Result> addressesForName = nameToAddresses.get(name); Result result; if (addressesForName == null) { // Determine if there is a photo for the icon. Uri contactPhotoUri = null; if (c.getLong(CONTACTS_INDEX_PHOTO_ID) > 0) { contactPhotoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, c.getLong(CONTACTS_INDEX_CONTACT_ID)); } // First listing for a distinct contact should have the name/icon. addressesForName = new ArrayList<Result>(); nameToAddresses.put(name, addressesForName); result = new Result(name, address, R.drawable.ic_contact_picture, contactPhotoUri); } else { // Do not include name/icon in subsequent listings for the same contact. result = new Result(null, address, null, null); } addressesForName.add(result); addressesRetVal.add(address); } } // Return the list of results. List<Result> allResults = new ArrayList<Result>(); for (List<Result> result : nameToAddresses.values()) { allResults.addAll(result); } return allResults; } finally { if (c != null) { c.close(); } } }
From source file:com.android.emailcommon.provider.Account.java
/** * Clear all account hold flags that are set. * * (This will trigger watchers, and in particular will cause EAS to try and resync the * account(s).)//from w w w . ja v a2 s . co m */ public static void clearSecurityHoldOnAllAccounts(Context context) { ContentResolver resolver = context.getContentResolver(); Cursor c = resolver.query(Account.CONTENT_URI, ACCOUNT_FLAGS_PROJECTION, SECURITY_NONZERO_SELECTION, null, null); try { while (c.moveToNext()) { int flags = c.getInt(ACCOUNT_FLAGS_COLUMN_FLAGS); if (0 != (flags & FLAGS_SECURITY_HOLD)) { ContentValues cv = new ContentValues(); cv.put(AccountColumns.FLAGS, flags & ~FLAGS_SECURITY_HOLD); long accountId = c.getLong(ACCOUNT_FLAGS_COLUMN_ID); Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, accountId); resolver.update(uri, cv, null, null); } } } finally { c.close(); } }
From source file:com.applozic.mobicommons.file.FileUtils.java
/** * Attempt to retrieve the thumbnail of given Uri from the MediaStore. This * should not be called on the UI thread. * * @param context//from w ww. j av a 2 s.c om * @param uri * @param mimeType * @return * @author paulburke */ public static Bitmap getThumbnail(Context context, Uri uri, String mimeType) { if (DEBUG) Log.d(TAG, "Attempting to get thumbnail"); if (!isMediaUri(uri)) { Log.e(TAG, "You can only retrieve thumbnails for images and videos."); return null; } Bitmap bm = null; if (uri != null) { final ContentResolver resolver = context.getContentResolver(); Cursor cursor = null; try { cursor = resolver.query(uri, null, null, null, null); if (cursor.moveToFirst()) { final int id = cursor.getInt(0); if (DEBUG) Log.d(TAG, "Got thumb ID: " + id); if (mimeType.contains("video")) { bm = MediaStore.Video.Thumbnails.getThumbnail(resolver, id, MediaStore.Video.Thumbnails.MINI_KIND, null); } else if (mimeType.contains(FileUtils.MIME_TYPE_IMAGE)) { bm = MediaStore.Images.Thumbnails.getThumbnail(resolver, id, MediaStore.Images.Thumbnails.MINI_KIND, null); } } } catch (Exception e) { if (DEBUG) Log.e(TAG, "getThumbnail", e); } finally { if (cursor != null) cursor.close(); } } return bm; }
From source file:edu.mit.mobile.android.locast.data.JsonSyncableItem.java
/** * Given a public Uri fragment, finds the local item representing it. If there isn't any such item, null is returned. * * @param context/*from w w w. j a va2s.c om*/ * @param dirUri the base local URI to search. * @param pubUri A public URI fragment that represents the given item. This must match the result from the API. * @return a local URI matching the item or null if none were found. */ public static Uri getItemByPubIUri(Context context, Uri dirUri, String pubUri) { Uri uri = null; final ContentResolver cr = context.getContentResolver(); final String[] selectionArgs = { pubUri }; final Cursor c = cr.query(dirUri, PUB_URI_PROJECTION, _PUBLIC_URI + "=?", selectionArgs, null); if (c.moveToFirst()) { uri = ContentUris.withAppendedId(dirUri, c.getLong(c.getColumnIndex(_ID))); } c.close(); return uri; }
From source file:com.amaze.carbonfilemanager.utils.files.Futils.java
private static Uri fileToContentUri(Context context, String path, String volume) { String[] projection = null;// w w w .ja v a2 s .co m final String where = MediaStore.MediaColumns.DATA + " = ?"; Uri baseUri = MediaStore.Files.getContentUri(volume); boolean isMimeTypeImage = false, isMimeTypeVideo = false, isMimeTypeAudio = false; isMimeTypeImage = Icons.isPicture(path); if (!isMimeTypeImage) { isMimeTypeVideo = Icons.isVideo(path); if (!isMimeTypeVideo) { isMimeTypeAudio = Icons.isVideo(path); } } if (isMimeTypeImage || isMimeTypeVideo || isMimeTypeAudio) { projection = new String[] { BaseColumns._ID }; if (isMimeTypeImage) { baseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if (isMimeTypeVideo) { baseUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if (isMimeTypeAudio) { baseUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } } else { projection = new String[] { BaseColumns._ID, MediaStore.Files.FileColumns.MEDIA_TYPE }; } ContentResolver cr = context.getContentResolver(); Cursor c = cr.query(baseUri, projection, where, new String[] { path }, null); try { if (c != null && c.moveToNext()) { boolean isValid = false; if (isMimeTypeImage || isMimeTypeVideo || isMimeTypeAudio) { isValid = true; } else { int type = c.getInt(c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MEDIA_TYPE)); isValid = type != 0; } if (isValid) { // Do not force to use content uri for no media files long id = c.getLong(c.getColumnIndexOrThrow(BaseColumns._ID)); return Uri.withAppendedPath(baseUri, String.valueOf(id)); } } } finally { if (c != null) { c.close(); } } return null; }
From source file:at.flack.MainActivity.java
public static String getContactName(Context context, String phoneNumber) { if (phoneNumber == null || phoneNumber.length() == 0) return phoneNumber; ContentResolver cr = context.getContentResolver(); Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); Cursor cursor = cr.query(uri, new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null); if (cursor == null) { return null; }//w ww . j a v a 2 s .c om String contactName = null; if (cursor.moveToFirst()) { contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME)); } if (cursor != null && !cursor.isClosed()) { cursor.close(); } return contactName; }
From source file:com.amaze.filemanager.utils.files.FileUtils.java
private static Uri fileToContentUri(Context context, String path, boolean isDirectory, String volume) { final String where = MediaStore.MediaColumns.DATA + " = ?"; Uri baseUri;/*from w w w . j av a 2s. co m*/ String[] projection; int mimeType = Icons.getTypeOfFile(path, isDirectory); switch (mimeType) { case Icons.IMAGE: baseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; projection = new String[] { BaseColumns._ID }; break; case Icons.VIDEO: baseUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; projection = new String[] { BaseColumns._ID }; break; case Icons.AUDIO: baseUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; projection = new String[] { BaseColumns._ID }; break; default: baseUri = MediaStore.Files.getContentUri(volume); projection = new String[] { BaseColumns._ID, MediaStore.Files.FileColumns.MEDIA_TYPE }; } ContentResolver cr = context.getContentResolver(); Cursor c = cr.query(baseUri, projection, where, new String[] { path }, null); try { if (c != null && c.moveToNext()) { boolean isValid = false; if (mimeType == Icons.IMAGE || mimeType == Icons.VIDEO || mimeType == Icons.AUDIO) { isValid = true; } else { int type = c.getInt(c.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MEDIA_TYPE)); isValid = type != 0; } if (isValid) { // Do not force to use content uri for no media files long id = c.getLong(c.getColumnIndexOrThrow(BaseColumns._ID)); return Uri.withAppendedPath(baseUri, String.valueOf(id)); } } } finally { if (c != null) { c.close(); } } return null; }
From source file:Main.java
/** * Retourner la liste des calendriers//from w w w .j a v a 2s.c om * * @return Liste des calendriers */ @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) public static Map<Integer, String> getCalendars(ContentResolver contentResolver) { Map<Integer, String> calendars = new HashMap<Integer, String>(); String[] projection; Uri calendarUri; Cursor cursor; String accessLevelCol; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { calendarUri = CalendarContract.Calendars.CONTENT_URI; projection = new String[] { CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME }; accessLevelCol = CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL; } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { calendarUri = Uri.parse("content://com.android.calendar/calendars"); projection = new String[] { "_id", "displayname" }; accessLevelCol = "ACCESS_LEVEL"; } else { calendarUri = Uri.parse("content://calendar/calendars"); projection = new String[] { "_id", "displayname" }; accessLevelCol = "ACCESS_LEVEL"; } cursor = contentResolver.query(calendarUri, projection, accessLevelCol + "=700", null, null); if (cursor != null && cursor.moveToFirst()) { while (cursor.isAfterLast() == false) { calendars.put(cursor.getInt(0), cursor.getString(1)); cursor.moveToNext(); } cursor.close(); } return calendars; }
From source file:com.goliathonline.android.kegbot.io.RemoteUsersHandler.java
public RemoteUsersHandler(ContentResolver resolver) { final Uri usersUri = KegbotContract.Drinks.buildUsersDirUri(); mCursor = resolver.query(usersUri, UsersQuery.PROJECTION, null, null, null); while (mCursor.moveToNext()) { final String userId = mCursor.getString(UsersQuery.USER_ID); mUsers.add(userId);//from w w w. j a va 2 s .com } mCursor.close(); }
From source file:cm.confide.ex.chips.RecipientAlternatesAdapter.java
private static Cursor doQuery(CharSequence constraint, int limit, Long directoryId, Account account, ContentResolver resolver, Query query) { final Uri.Builder builder = query.getContentFilterUri().buildUpon().appendPath(constraint.toString()) .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY, String.valueOf(limit + BaseRecipientAdapter.ALLOWANCE_FOR_DUPLICATES)); if (directoryId != null) { builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(directoryId)); }//from ww w. ja va2 s. c om if (account != null) { builder.appendQueryParameter(BaseRecipientAdapter.PRIMARY_ACCOUNT_NAME, account.name); builder.appendQueryParameter(BaseRecipientAdapter.PRIMARY_ACCOUNT_TYPE, account.type); } final Cursor cursor = resolver.query(builder.build(), query.getProjection(), null, null, null); return cursor; }