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.lgallardo.youtorrentcontroller.RefreshListener.java

public static String getFilePathFromUri(Context c, Uri uri) {
    String filePath = null;//w  ww.  j  a  v  a2  s .  c o  m
    if ("content".equals(uri.getScheme())) {
        String[] filePathColumn = { MediaStore.MediaColumns.DATA };
        ContentResolver contentResolver = c.getContentResolver();

        Cursor cursor = contentResolver.query(uri, filePathColumn, null, null, null);

        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        filePath = cursor.getString(columnIndex);
        cursor.close();
    } else if ("file".equals(uri.getScheme())) {
        filePath = new File(uri.getPath()).getAbsolutePath();
    }
    return filePath;
}

From source file:com.dwdesign.tweetings.util.Utils.java

public static boolean isFiltered(final Context context, final String screen_name, final String source,
        final String text) {
    if (context == null)
        return false;
    final ContentResolver resolver = context.getContentResolver();
    final String[] cols = new String[] { Filters.TEXT };
    Cursor cur;/* ww  w  .  j a v a 2 s . co  m*/
    if (screen_name != null) {
        cur = resolver.query(Filters.Users.CONTENT_URI, cols, null, null, null);
        cur.moveToFirst();
        while (!cur.isAfterLast()) {
            if (screen_name.equals(cur.getString(0)))
                return true;
            cur.moveToNext();
        }
        cur.close();
    }
    if (source != null) {
        cur = resolver.query(Filters.Sources.CONTENT_URI, cols, null, null, null);
        cur.moveToFirst();
        while (!cur.isAfterLast()) {
            if (HtmlEscapeHelper.unescape(source).equals(cur.getString(0)))
                return true;
            cur.moveToNext();
        }
        cur.close();
    }
    if (text != null) {
        cur = resolver.query(Filters.Users.CONTENT_URI, cols, null, null, null);
        cur.moveToFirst();
        while (!cur.isAfterLast()) {
            if (text.contains(cur.getString(0)))
                return true;
            cur.moveToNext();
        }
        cur.close();
    }
    return false;
}

From source file:com.android.contacts.ContactSaveService.java

private long[] getRawContactIdsForAggregation(long[] contactIds) {
    if (contactIds == null) {
        return null;
    }//from ww w.  ja va  2s  . c  o m

    final ContentResolver resolver = getContentResolver();

    final StringBuilder queryBuilder = new StringBuilder();
    final String stringContactIds[] = new String[contactIds.length];
    for (int i = 0; i < contactIds.length; i++) {
        queryBuilder.append(RawContacts.CONTACT_ID + "=?");
        stringContactIds[i] = String.valueOf(contactIds[i]);
        if (contactIds[i] == -1) {
            return null;
        }
        if (i == contactIds.length - 1) {
            break;
        }
        queryBuilder.append(" OR ");
    }

    final Cursor c = resolver.query(RawContacts.CONTENT_URI, JoinContactQuery.PROJECTION,
            queryBuilder.toString(), stringContactIds, null);
    if (c == null) {
        Log.e(TAG, "Unable to open Contacts DB cursor");
        showToast(R.string.contactSavedErrorToast);
        return null;
    }
    long rawContactIds[];
    try {
        if (c.getCount() < 2) {
            Log.e(TAG, "Not enough raw contacts to aggregate together.");
            return null;
        }
        rawContactIds = new long[c.getCount()];
        for (int i = 0; i < rawContactIds.length; i++) {
            c.moveToPosition(i);
            long rawContactId = c.getLong(JoinContactQuery._ID);
            rawContactIds[i] = rawContactId;
        }
    } finally {
        c.close();
    }
    return rawContactIds;
}

From source file:com.dwdesign.tweetings.util.Utils.java

public static ArrayList<Long> getStatusIdsInDatabase(final Context context, final Uri uri,
        final long account_id) {
    final ArrayList<Long> list = new ArrayList<Long>();
    if (context == null)
        return list;
    final ContentResolver resolver = context.getContentResolver();
    final String where = Statuses.ACCOUNT_ID + " = " + account_id;
    final String[] projection = new String[] { Statuses.STATUS_ID };
    final Cursor cur = resolver.query(uri, projection, where, null, null);
    if (cur != null) {
        final int idx = cur.getColumnIndexOrThrow(Statuses.STATUS_ID);
        cur.moveToFirst();//from w w w  .j a  va  2s . co m
        while (!cur.isAfterLast()) {
            list.add(cur.getLong(idx));
            cur.moveToNext();
        }
        cur.close();
    }
    return list;
}

From source file:com.akop.bach.parser.XboxLiveParser.java

private void parseAccountSummary(XboxLiveAccount account) throws ParserException, IOException {
    ContentValues cv = parseSummaryData(account);
    ContentResolver cr = mContext.getContentResolver();

    long accountId = account.getId();
    boolean newRecord = true;

    long started = System.currentTimeMillis();
    Cursor c = cr.query(Profiles.CONTENT_URI, new String[] { Profiles._ID },
            Profiles.ACCOUNT_ID + "=" + accountId, null, null);

    if (c != null) {
        if (c.moveToFirst())
            newRecord = false;/*from w w  w  .j  a  v a 2s . c  o  m*/
        c.close();
    }

    if (newRecord) {
        cv.put(Profiles.ACCOUNT_ID, account.getId());
        cv.put(Profiles.UUID, account.getUuid());

        cr.insert(Profiles.CONTENT_URI, cv);
    } else {
        cr.update(Profiles.CONTENT_URI, cv, Profiles.ACCOUNT_ID + "=" + accountId, null);
    }

    cr.notifyChange(Profiles.CONTENT_URI, null);

    if (App.getConfig().logToConsole())
        displayTimeTaken("Summary update", started);

    account.refresh(Preferences.get(mContext));
    account.setGamertag(cv.getAsString(Profiles.GAMERTAG));
    account.setIconUrl(cv.getAsString(Profiles.ICON_URL));
    account.setGoldStatus(cv.getAsBoolean(Profiles.IS_GOLD));
    account.setLastSummaryUpdate(System.currentTimeMillis());
    account.save(Preferences.get(mContext));
}

From source file:com.akop.bach.parser.XboxLiveParser.java

@Override
public void deleteAccount(BasicAccount account) {
    ContentResolver cr = mContext.getContentResolver();
    long accountId = account.getId();

    // Clear games & achievements
    Cursor c = cr.query(Games.CONTENT_URI, new String[] { Games._ID }, Games.ACCOUNT_ID + "=" + accountId, null,
            null);//from   www.  j a  va2 s . c o  m

    if (c != null) {
        StringBuffer buffer = new StringBuffer();

        try {
            while (c.moveToNext()) {
                if (buffer.length() > 0)
                    buffer.append(",");
                buffer.append(c.getLong(0));
            }

            if (buffer.length() > 0) {
                // Clear achievements
                cr.delete(Achievements.CONTENT_URI, Achievements.GAME_ID + " IN (" + buffer.toString() + ")",
                        null);
            }
        } catch (Exception e) {
            // Do nothing
        } finally {
            c.close();
        }
    }

    try {
        // Clear games
        cr.delete(Games.CONTENT_URI, Games.ACCOUNT_ID + "=" + accountId, null);
    } catch (Exception e) {
        // Do nothing
    }

    try {
        // Delete friends
        cr.delete(Friends.CONTENT_URI, Friends.ACCOUNT_ID + "=" + accountId, null);
    } catch (Exception e) {
        // Do nothing
    }

    try {
        // Delete messages
        cr.delete(Messages.CONTENT_URI, Messages.ACCOUNT_ID + "=" + accountId, null);
    } catch (Exception e) {
        // Do nothing
    }

    try {
        // Delete sent messages
        cr.delete(SentMessages.CONTENT_URI, SentMessages.ACCOUNT_ID + "=" + accountId, null);
    } catch (Exception e) {
        // Do nothing
    }

    try {
        // Delete beacons
        cr.delete(Beacons.CONTENT_URI, Beacons.ACCOUNT_ID + "=" + accountId, null);
    } catch (Exception e) {
        // Do nothing
    }

    try {
        // Delete profiles
        cr.delete(Profiles.CONTENT_URI, Profiles.ACCOUNT_ID + "=" + accountId, null);
    } catch (Exception e) {
        // Do nothing
    }

    try {
        // Delete notify states
        cr.delete(NotifyStates.CONTENT_URI, Profiles.ACCOUNT_ID + "=" + accountId, null);
    } catch (Exception e) {
        // Do nothing
    }

    try {
        // Delete authenticated session
        deleteSession(account);
    } catch (Exception e) {
        // Do nothing
    }

    // Send notifications
    cr.notifyChange(Achievements.CONTENT_URI, null);
    cr.notifyChange(Games.CONTENT_URI, null);
    cr.notifyChange(Friends.CONTENT_URI, null);
    cr.notifyChange(Messages.CONTENT_URI, null);
    cr.notifyChange(Profiles.CONTENT_URI, null);
    cr.notifyChange(Beacons.CONTENT_URI, null);
    cr.notifyChange(SentMessages.CONTENT_URI, null);
    cr.notifyChange(NotifyStates.CONTENT_URI, null);

}

From source file:com.dwdesign.tweetings.util.Utils.java

public static long[] getLastMessageIdsFromDatabase(final Context context, final Uri uri) {
    if (context == null || uri == null)
        return null;
    final long[] account_ids = getActivatedAccountIds(context);
    final String[] cols = new String[] { DirectMessages.MESSAGE_ID };
    final ContentResolver resolver = context.getContentResolver();
    final long[] status_ids = new long[account_ids.length];
    int idx = 0;//from  w  ww.  j  av a 2s  .  c  o m
    for (final long account_id : account_ids) {
        final String where = Statuses.ACCOUNT_ID + " = " + account_id;
        final Cursor cur = resolver.query(uri, cols, where, null, DirectMessages.MESSAGE_ID);
        if (cur == null) {
            continue;
        }

        if (cur.getCount() > 0) {
            cur.moveToFirst();
            status_ids[idx] = cur.getLong(cur.getColumnIndexOrThrow(DirectMessages.MESSAGE_ID));
        }
        cur.close();
        idx++;
    }
    return status_ids;
}

From source file:com.dwdesign.tweetings.util.Utils.java

public static long[] getNewestMessageIdsFromDatabase(final Context context, final Uri uri) {
    if (context == null || uri == null)
        return null;
    final long[] account_ids = getActivatedAccountIds(context);
    final String[] cols = new String[] { DirectMessages.MESSAGE_ID };
    final ContentResolver resolver = context.getContentResolver();
    final long[] status_ids = new long[account_ids.length];
    int idx = 0;//from w ww .j  a  va 2s . c o m
    for (final long account_id : account_ids) {
        final String where = Statuses.ACCOUNT_ID + " = " + account_id;
        final Cursor cur = resolver.query(uri, cols, where, null, DirectMessages.DEFAULT_SORT_ORDER);
        if (cur == null) {
            continue;
        }

        if (cur.getCount() > 0) {
            cur.moveToFirst();
            status_ids[idx] = cur.getLong(cur.getColumnIndexOrThrow(DirectMessages.MESSAGE_ID));
        }
        cur.close();
        idx++;
    }
    return status_ids;
}

From source file:com.dwdesign.tweetings.util.Utils.java

public static long[] getNewestStatusIdsFromDatabase(final Context context, final Uri uri) {
    if (context == null || uri == null)
        return null;
    final long[] account_ids = getActivatedAccountIds(context);
    final String[] cols = new String[] { Statuses.STATUS_ID };
    final ContentResolver resolver = context.getContentResolver();
    final long[] status_ids = new long[account_ids.length];
    int idx = 0;//  ww  w.  j  a  va 2  s . c  o  m
    for (final long account_id : account_ids) {
        final String where = Statuses.ACCOUNT_ID + " = " + account_id;
        final Cursor cur = resolver.query(uri, cols, where, null, Statuses.DEFAULT_SORT_ORDER);
        if (cur == null) {
            continue;
        }

        if (cur.getCount() > 0) {
            cur.moveToFirst();
            status_ids[idx] = cur.getLong(cur.getColumnIndexOrThrow(Statuses.STATUS_ID));
        }
        cur.close();
        idx++;
    }
    return status_ids;
}

From source file:com.dwdesign.tweetings.util.Utils.java

public static long[] getLastStatusIdsFromDatabase(final Context context, final Uri uri) {
    if (context == null || uri == null)
        return null;
    final long[] account_ids = getActivatedAccountIds(context);
    final String[] cols = new String[] { Statuses.STATUS_ID };
    final ContentResolver resolver = context.getContentResolver();
    final long[] status_ids = new long[account_ids.length];
    int idx = 0;//from   w  ww .  java  2 s  .com
    for (final long account_id : account_ids) {
        final String where = Statuses.ACCOUNT_ID + " = " + account_id;
        final Cursor cur = resolver.query(uri, cols, where, null, Statuses.STATUS_ID);
        if (cur == null) {
            continue;
        }

        if (cur.getCount() > 0) {
            cur.moveToFirst();
            status_ids[idx] = cur.getLong(cur.getColumnIndexOrThrow(Statuses.STATUS_ID));
        }
        cur.close();
        idx++;
    }
    return status_ids;
}