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:com.enadein.carlogbook.db.CommonUtils.java

public static void validateDateNotifications(Context ctx) {
    ContentResolver cr = ctx.getContentResolver();
    long carId = DBUtils.getActiveCarId(cr);
    String selection = DBUtils.CAR_SELECTION_NOTIFY + " and " + ProviderDescriptor.Notify.Cols.TYPE + " = ?";

    Cursor c = cr.query(ProviderDescriptor.Notify.CONTENT_URI, null, selection,
            new String[] { String.valueOf(carId), String.valueOf(ProviderDescriptor.Notify.Type.DATE) }, null);

    if (c == null) {
        NotifyService.cancelAlarm(ctx);// ww  w .  ja va2 s .c  om
        return;
    }

    int count = c.getCount();
    c.close();

    if (count > 0) {
        NotifyService.cancelAlarm(ctx);
        NotifyService.createAlarm(ctx);
    } else {
        NotifyService.cancelAlarm(ctx);
    }

}

From source file:Main.java

public static boolean checkShortCut(Context context, String appName) {
    boolean hasShortCut = false;
    try {/* www  .java2 s .c o m*/
        ContentResolver cr = context.getContentResolver();
        final String AUTHORITY1 = "com.android.launcher.settings";
        final String AUTHORITY2 = "com.android.launcher2.settings";
        String contentUri = "";
        if (android.os.Build.VERSION.SDK_INT < 8) {
            contentUri = "content://" + AUTHORITY1 + "/favorites?notify=true";
        } else {
            contentUri = "content://" + AUTHORITY2 + "/favorites?notify=true";
        }
        final Uri CONTENT_URI = Uri.parse(contentUri);
        Cursor c = cr.query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?",
                new String[] { appName }, null);
        if (c != null && c.getCount() > 0) {
            hasShortCut = true;
        }
    } catch (Exception e) {
    }
    return hasShortCut;
}

From source file:edu.mit.mobile.android.locast.data.TaggableItem.java

/**
 * TODO make this pick the set of tags for a set of content.
 *
 * @param cr a content resolver/*from  ww  w.ja  v a2s  .  c o m*/
 * @return the top MAX_POPULAR_TAGS most popular tags in the set, with the most popular first.
 */
public static List<String> getPopularTags(ContentResolver cr) {

    final Map<String, Integer> tagPop = new HashMap<String, Integer>();
    final List<String> popTags;

    final Cursor c = cr.query(Tag.CONTENT_URI, Tag.DEFAULT_PROJECTION, null, null, null);
    final int tagColumn = c.getColumnIndex(Tag._NAME);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        final String tag = c.getString(tagColumn);

        final Integer count = tagPop.get(tag);
        if (count == null) {
            tagPop.put(tag, 1);
        } else {
            tagPop.put(tag, count + 1);
        }
    }
    c.close();

    popTags = new ArrayList<String>(tagPop.keySet());

    Collections.sort(popTags, new Comparator<String>() {
        public int compare(String object1, String object2) {
            return tagPop.get(object2).compareTo(tagPop.get(object1));
        }

    });
    int limit;
    if (popTags.size() < MAX_POPULAR_TAGS) {
        limit = popTags.size();
    } else {
        limit = MAX_POPULAR_TAGS;
    }
    return popTags.subList(0, limit);
}

From source file:com.smarthome.deskclock.Alarms.java

/**
 * Return an Alarm object representing the alarm id in the database.
 * Returns null if no alarm exists.//from www .j  a  v  a2 s  .c o  m
 */
public static Alarm getAlarm(ContentResolver contentResolver, int alarmId) {
    Cursor cursor = contentResolver.query(ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, alarmId),
            Alarm.Columns.ALARM_QUERY_COLUMNS, null, null, null);
    Alarm alarm = null;
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            alarm = new Alarm(cursor);
        }
        cursor.close();
    }
    return alarm;
}

From source file:info.guardianproject.otr.app.im.app.DatabaseUtils.java

public static boolean doesAvatarHashExist(ContentResolver resolver, Uri queryUri, String jid, String hash) {

    StringBuilder buf = new StringBuilder(Imps.Avatars.CONTACT);
    buf.append("=?");
    buf.append(" AND ");
    buf.append(Imps.Avatars.HASH);//from  w ww.  ja va2 s  .  c  om
    buf.append("=?");

    String[] selectionArgs = new String[] { jid, hash };

    Cursor cursor = resolver.query(queryUri, null, buf.toString(), selectionArgs, null);
    if (cursor == null)
        return false;
    try {
        return cursor.getCount() > 0;
    } finally {
        cursor.close();
    }
}

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

private static Cursor getFilteredTimeSchedulesCursor(ContentResolver contentResolver) {
    return contentResolver.query(CONTENT_URI, QUERY_COLUMNS, WHERE_ENABLED, null, null);
}

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

/**
 * Queries all time schedules//from  w  w w.j  av a  2 s  . co m
 * @return cursor over all schedules
 */
public static Cursor getTimeSchedulesCursor(ContentResolver contentResolver) {
    return contentResolver.query(CONTENT_URI, QUERY_COLUMNS, null, null, DEFAULT_SORT_ORDER);
}

From source file:info.staticfree.android.units.UnitUsageDBHelper.java

/**
 * Increments the usage counter for the given unit.
 * @param unit name of the unit//  w w w  .  j av a2 s.  c om
 * @param db the unit usage database
 */
public static void logUnitUsed(String unit, ContentResolver cr) {
    final String[] selectionArgs = { unit };
    final Cursor c = cr.query(UsageEntry.CONTENT_URI, INCREMENT_QUERY_PROJECTION, UsageEntry._UNIT + "=?",
            selectionArgs, null);
    if (c.getCount() > 0) {
        c.moveToFirst();
        final int useCount = c.getInt(c.getColumnIndex(UsageEntry._USE_COUNT));
        final int id = c.getInt(c.getColumnIndex(UsageEntry._ID));
        final ContentValues cv = new ContentValues();
        cv.put(UsageEntry._USE_COUNT, useCount + 1);

        cr.update(ContentUris.withAppendedId(UsageEntry.CONTENT_URI, id), cv, null, null);
    } else {
        final ContentValues cv = new ContentValues();
        cv.put(UsageEntry._UNIT, unit);
        cv.put(UsageEntry._USE_COUNT, 1);
        cv.put(UsageEntry._FACTOR_FPRINT, getFingerprint(unit));
        cr.insert(UsageEntry.CONTENT_URI, cv);
    }
    c.close();
}

From source file:com.enadein.carlogbook.db.CommonUtils.java

public static void validateOdometerNotifications(Context ctx, int odmeterValue) {
    ContentResolver cr = ctx.getContentResolver();
    long carId = DBUtils.getActiveCarId(cr);
    String selection = DBUtils.CAR_SELECTION_NOTIFY + " and " + ProviderDescriptor.Notify.Cols.TYPE
            + " = ? and " + ProviderDescriptor.Notify.Cols.TRIGGER_VALUE + " <= ?";
    Cursor c = cr.query(
            ProviderDescriptor.Notify.CONTENT_URI, null, selection, new String[] { String.valueOf(carId),
                    String.valueOf(ProviderDescriptor.Notify.Type.ODOMETER), String.valueOf(odmeterValue) },
            null);//from   w  w  w. j  av a  2 s. c o m

    if (c == null) {
        return;
    }

    while (c.moveToNext()) {
        long id = c.getLong(c.getColumnIndex(ProviderDescriptor.Notify.Cols._ID));
        createNotify(ctx, id, R.drawable.not_odom);
    }

    c.close();

    //DOUBLE CHECK
    NotifyService.checkDateNotifications(ctx);
    CommonUtils.validateDateNotifications(ctx);

}

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

private static boolean isSharedLinkUpdated(Uri uri, String itemId, JSONObject sharedLink,
        ContentResolver resolver) throws JSONException {
    final String[] selectionArgs = { itemId };
    final Cursor cursor = resolver.query(uri, MixItContract.SharedLinks.PROJ.PROJECTION,
            MixItContract.SharedLinks.MEMBER_ID + " = ?", selectionArgs, null);
    try {//from ww  w.ja v a  2 s  .  co  m
        if (!cursor.moveToFirst()) {
            return false;
        }

        final int curOrderNum = cursor.getInt(MixItContract.SharedLinks.PROJ.ORDER_NUM);
        final String curName = cursor.getString(MixItContract.SharedLinks.PROJ.NAME)
                .toLowerCase(Locale.getDefault()).trim();
        final String curUrl = cursor.getString(MixItContract.SharedLinks.PROJ.URL)
                .toLowerCase(Locale.getDefault()).trim();

        final int newOrderNum = sharedLink.has(TAG_ORDER_NUM) ? sharedLink.getInt(TAG_ORDER_NUM) : curOrderNum;
        final String newName = sharedLink.has(TAG_NAME)
                ? sharedLink.getString(TAG_NAME).toLowerCase(Locale.getDefault()).trim()
                : curName;
        final String newUrl = sharedLink.has(TAG_URL)
                ? sharedLink.getString(TAG_URL).toLowerCase(Locale.getDefault()).trim()
                : curUrl;

        return !curName.equals(newName) || //
                !curUrl.equals(newUrl) || //
                curOrderNum != newOrderNum;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}