Example usage for android.content ContentResolver query

List of usage examples for android.content ContentResolver query

Introduction

In this page you can find the example usage for android.content ContentResolver query.

Prototype

public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri, @Nullable String[] projection,
        @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) 

Source Link

Document

Query the given URI, returning a Cursor over the result set.

Usage

From source file:Main.java

public static LinkedHashMap<String, Uri> GetMatchedContactsList(Context context, String searchTerm) {
    LinkedHashMap<String, Uri> contactList = new LinkedHashMap<String, Uri>();
    ContentResolver cr = context.getContentResolver();
    String columns[] = { ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI };
    Cursor cur;/*from  w  w  w.j  av a  2  s.  c  o  m*/
    if (searchTerm == null) {
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, columns, null, null, null);
    } else {
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, columns,
                ContactsContract.Contacts.DISPLAY_NAME + " LIKE "
                        + DatabaseUtils.sqlEscapeString("%" + searchTerm + "%"),
                null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
    }
    if (cur != null) {
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String photoURI = cur
                        .getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
                if (photoURI != null) {
                    Uri thumbUri = Uri.parse(photoURI);
                    contactList.put(name, thumbUri);
                }
            }
        }
        cur.close();
    }
    return contactList;
}

From source file:com.anthonymandra.support.v4.provider.DocumentsContractApi19.java

private static String queryForString(Context context, Uri self, String column, String defaultValue) {
    final ContentResolver resolver = context.getContentResolver();

    Cursor c = null;//w ww.  j  a v a 2  s.c  o m
    try {
        c = resolver.query(self, new String[] { column }, null, null, null);
        if (c.moveToFirst() && !c.isNull(0)) {
            return c.getString(0);
        } else {
            return defaultValue;
        }
    } catch (Exception e) {
        Log.w(TAG, "Failed query: " + e);
        return defaultValue;
    } finally {
        closeQuietly(c);
    }
}

From source file:com.anthonymandra.support.v4.provider.DocumentsContractApi19.java

private static long queryForLong(Context context, Uri self, String column, long defaultValue) {
    final ContentResolver resolver = context.getContentResolver();

    Cursor c = null;//from ww w  .j  a v  a 2 s.  c  om
    try {
        c = resolver.query(self, new String[] { column }, null, null, null);
        if (c.moveToFirst() && !c.isNull(0)) {
            return c.getLong(0);
        } else {
            return defaultValue;
        }
    } catch (Exception e) {
        Log.w(TAG, "Failed query: " + e);
        return defaultValue;
    } finally {
        closeQuietly(c);
    }
}

From source file:Main.java

public static Cursor query(final Context context, Uri uri, final String[] projection, final String selection,
        final String[] selectionArgs, final String sortOrder, final int limit) {
    try {//from  ww  w . j  ava  2s .com
        final ContentResolver resolver = context.getContentResolver();
        if (resolver == null) {
            return null;
        }
        if (limit > 0) {
            uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build();
        }
        return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
    } catch (final UnsupportedOperationException ex) {
        return null;
    }

}

From source file:ca.mudar.parkcatcher.utils.ParserUtils.java

/**
 * Query and return the {@link SyncColumns#UPDATED} time for the requested
 * {@link Uri}. Expects the {@link Uri} to reference a single item.
 *//* w ww  .j ava  2 s.com*/
public static long queryItemUpdated(Uri uri, ContentResolver resolver) {
    final String[] projection = { SyncColumns.UPDATED };
    final Cursor cursor = resolver.query(uri, projection, null, null, null);
    try {
        if (cursor.moveToFirst()) {
            return cursor.getLong(0);
        } else {
            return ParkingContract.UPDATED_NEVER;
        }
    } finally {
        cursor.close();
    }
}

From source file:Main.java

public static Bundle getContactFromNumber(String number, Context context) {
    ContentResolver cr = context.getContentResolver();
    String[] projection = new String[] { Contacts._ID, Contacts.DISPLAY_NAME };
    Bundle bundle = new Bundle();
    Uri contactUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(number));
    Cursor cursor = cr.query(contactUri, projection, null, null, null);
    try {//  ww  w  .ja v  a 2s  .  co m
        if (cursor.moveToFirst()) {
            for (String key : projection) {
                bundle.putString(key, cursor.getString(cursor.getColumnIndex(key)));
            }
        }
        return bundle;
    } catch (Exception e) {
        return null;
    } finally {
        cursor.close();
    }

}

From source file:ca.mudar.parkcatcher.utils.ParserUtils.java

/**
 * Query and return the newest {@link SyncColumns#UPDATED} time for all
 * entries under the requested {@link Uri}. Expects the {@link Uri} to
 * reference a directory of several items.
 *///from   w ww  .  java  2 s  . c  o m
public static long queryDirUpdated(Uri uri, ContentResolver resolver) {
    final String[] projection = { "MAX(" + SyncColumns.UPDATED + ")" };
    final Cursor cursor = resolver.query(uri, projection, null, null, null);
    try {
        cursor.moveToFirst();
        return cursor.getLong(0);
    } finally {
        cursor.close();
    }
}

From source file:com.ksk.droidbatterybooster.provider.PowerSchedule.java

public static List<PowerSchedule> getSchedules(ContentResolver contentResolver, String selection,
        String... selectionArgs) {
    Cursor cursor = contentResolver.query(CONTENT_URI, QUERY_COLUMNS, selection, selectionArgs, null);
    List<PowerSchedule> result = new LinkedList<PowerSchedule>();
    if (cursor == null) {
        return result;
    }/*from w w w.  j a va2s .c om*/

    try {
        if (cursor.moveToFirst()) {
            do {
                result.add(new PowerSchedule(cursor));
            } while (cursor.moveToNext());
        }
    } finally {
        cursor.close();
    }

    return result;
}

From source file:fr.mixit.android.io.RemoteSessionsHandler.java

private static boolean isSessionSpeakersUpdated(Uri uri, JSONArray speakers, ContentResolver resolver)
        throws JSONException {
    final Cursor cursor = resolver.query(uri, SpeakersQuery.PROJECTION, null, null, null);
    try {/*  w w w.j a v  a2  s. com*/
        if (!cursor.moveToFirst())
            return false;
        return cursor.getCount() != speakers.length();
    } finally {
        cursor.close();
    }
}

From source file:fr.mixit.android.io.RemoteSessionsHandler.java

private static boolean isSessionTagsUpdated(Uri uri, JSONArray tags, ContentResolver resolver)
        throws JSONException {
    final Cursor cursor = resolver.query(uri, TagsQuery.PROJECTION, null, null, null);
    try {//from   w  w  w . ja  v  a2s. c  o  m
        if (!cursor.moveToFirst())
            return false;
        return cursor.getCount() != tags.length();
    } finally {
        cursor.close();
    }
}