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:ee.ria.DigiDoc.util.FileUtils.java

public static String resolveFileName(Uri uri, ContentResolver contentResolver) {
    String uriString = uri.toString();
    if (isContentUri(uriString)) {
        try (Cursor cursor = contentResolver.query(uri, null, null, null, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }//from  ww w.  j  a  v a  2 s. c o m
        }
    } else if (isFileUri(uriString)) {
        return new File(uriString).getName();
    }
    return null;
}

From source file:com.kku.apps.pricesearch.util.Utils.java

public static Cursor readFavorites(Context context, ListItem item) {

    final ContentResolver cr = context.getContentResolver();
    final Cursor c = cr.query(SearchContract.URI_FAVORITES, null, FavoritesColumns.ITEMURL + " = ?",
            new String[] { item.getItemUrl() }, null);
    return c;/* www  .  j  a  v  a2  s .co m*/
}

From source file:Main.java

@Nullable
public static Cursor query(Context context, Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder, int limit) {
    try {//from   w  w w. j a  v a  2  s . com
        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 (UnsupportedOperationException ex) {
        return null;
    }
}

From source file:totalcross.android.ConnectionManager4A.java

public static void setDefaultConfiguration(int type, String cfg) {
    if (cfg == null)
        cfg = "";

    switch (type) {
    case GPRS: {//  w  ww  .  j  av a 2s  .c  o  m
        int id = -1;
        ContentResolver contentResolver = Launcher4A.loader.getContentResolver();
        Cursor cursor = contentResolver.query(CONTENT_URI, new String[] { "_id" },
                "apn = ? and user = ? and password = ?", new String[] { "tim.br", "tim", "tim" }, null);
        if (cursor == null || cursor.getCount() <= 0) {
            TelephonyManager tel = (TelephonyManager) Launcher4A.loader
                    .getSystemService(Context.TELEPHONY_SERVICE);
            String networkOperator = tel.getNetworkOperator();

            if (networkOperator != null && networkOperator.length() > 0) {
                int mcc = Integer.parseInt(networkOperator.substring(0, 3));
                int mnc = Integer.parseInt(networkOperator.substring(3));
                ContentValues values = new ContentValues();
                values.put("apn", "tim.br");
                values.put("user", "tim");
                values.put("password", "tim");
                values.put("mcc", mcc);
                values.put("mnc", mnc);
                values.put("numeric", mcc + "" + mnc);
                contentResolver.insert(CONTENT_URI, values);
                cursor = contentResolver.query(CONTENT_URI, new String[] { "_id" },
                        "apn = ? and user = ? and password = ?", new String[] { "tim.br", "tim", "tim" }, null);
            }
        }
        if (cursor == null)
            return;
        if (cursor.moveToFirst())
            id = cursor.getInt(0);
        cursor.close();

        if (id > -1) {
            ContentValues values = new ContentValues();
            //See /etc/apns-conf.xml. The TelephonyProvider uses this file to provide
            //content://telephony/carriers/preferapn URI mapping
            values.put("apn_id", id);
            contentResolver.update(PREFERRED_APN_URI, values, null, null);
            cursor = contentResolver.query(PREFERRED_APN_URI, new String[] { "name", "apn" }, "_id=" + id, null,
                    null);
            if (cursor != null)
                cursor.close();
        }
    }
        break;
    case WIFI:
        connWIFI = connWIFIPrefix + cfg;
        break;
    //default:
    //   throw new IllegalArgumentException("Invalid value for argument 'type'");
    }
}

From source file:Main.java

private static Set<Integer> getAllAvailableProtocolVersions(Context context) {
    ContentResolver contentResolver = context.getContentResolver();
    Set<Integer> allAvailableVersions = new HashSet<Integer>();
    Uri uri = Uri.parse("content://com.facebook.orca.provider.MessengerPlatformProvider/versions");
    String[] projection = new String[] { "version" };
    Cursor c = contentResolver.query(uri, projection, null, null, null);
    if (c != null) {
        try {//from  w w w.j av  a 2  s  . c  o m
            int versionColumnIndex = c.getColumnIndex("version");
            while (c.moveToNext()) {
                int version = c.getInt(versionColumnIndex);
                allAvailableVersions.add(version);
            }
        } finally {
            c.close();
        }
    }
    return allAvailableVersions;
}

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

private static boolean isSessionUpdated(Uri uri, JSONObject session, ContentResolver resolver)
        throws JSONException {
    final Cursor cursor = resolver.query(uri, SessionsQuery.PROJECTION, null, null, null);
    try {/*from  w  w  w  . ja va2  s  .co m*/
        if (!cursor.moveToFirst())
            return false;

        final String curTitle = cursor.getString(SessionsQuery.TITLE).toLowerCase().trim();
        final String curSummary = cursor.getString(SessionsQuery.SUMMARY).toLowerCase().trim();
        final String curTrackId = cursor.getString(SessionsQuery.TRACK_ID) != null
                ? cursor.getString(SessionsQuery.TRACK_ID).toLowerCase().trim()
                : "";
        final String newTitle = session.getString("name").toLowerCase().trim();
        final String newSummary = session.getString("description").toLowerCase().trim();
        final String newTrackId = session.has("track") ? session.getString("track").toLowerCase().trim() : "";

        return (!curTitle.equals(newTitle) || !curSummary.equals(newSummary) || !curTrackId.equals(newTrackId));
    } finally {
        cursor.close();
    }
}

From source file:Main.java

private static Set<Integer> getAllAvailableProtocolVersions(Context context) {
    ContentResolver contentResolver = context.getContentResolver();
    Set<Integer> allAvailableVersions = new HashSet<Integer>();
    Uri uri = Uri.parse("content://appsneva.facebook.orca.provider.MessengerPlatformProvider/versions");
    String[] projection = new String[] { "version" };
    Cursor c = contentResolver.query(uri, projection, null, null, null);
    if (c != null) {
        try {//from   ww  w  .j  a va 2s. c o  m
            int versionColumnIndex = c.getColumnIndex("version");
            while (c.moveToNext()) {
                int version = c.getInt(versionColumnIndex);
                allAvailableVersions.add(version);
            }
        } finally {
            c.close();
        }
    }
    return allAvailableVersions;
}

From source file:Main.java

public static Cursor query(Context context, Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder, int limit) {
    try {// w ww .  j a  va2s  .  c om
        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 (UnsupportedOperationException ex) {
        return null;
    }

}

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

/**
 * Get mode by id.//from   w  w  w.  j av a2 s  .  c o  m
 * 
 * @param contentResolver
 * @param modeId
 * @return mode if found, null otherwise
 */
public static OptimalMode getMode(ContentResolver contentResolver, long modeId) {
    Cursor cursor = contentResolver.query(getContentUriForId(modeId), QUERY_COLUMNS, null, null, null);
    OptimalMode result = null;
    if (cursor == null) {
        return result;
    }

    try {
        if (cursor.moveToFirst()) {
            result = new OptimalMode(cursor);
        }
    } finally {
        cursor.close();
    }

    return result;
}

From source file:Main.java

public static long getLastProgramEndTimeMillis(ContentResolver resolver, Uri channelUri) {
    Uri uri = TvContract.buildProgramsUriForChannel(channelUri);
    String[] projection = { Programs.COLUMN_END_TIME_UTC_MILLIS };
    Cursor cursor = null;/*from   www . ja  v a  2s .  c  om*/
    try {
        // TvProvider returns programs chronological order by default.
        cursor = resolver.query(uri, projection, null, null, null);
        if (cursor == null || cursor.getCount() == 0) {
            return 0;
        }
        cursor.moveToLast();
        return cursor.getLong(0);
    } catch (Exception e) {
        Log.w(TAG, "Unable to get last program end time for " + channelUri, e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return 0;
}