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.rks.musicx.misc.utils.PlaylistHelper.java

/**
 * Return Playlist Id/*  w ww.  j a v a2s  .  c  o  m*/
 *
 * @param context
 * @param playlist
 * @return
 */
private static String getPlayListId(Context context, String playlist) {
    int recordcount;
    Uri newuri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
    final String playlistid = MediaStore.Audio.Playlists._ID;
    final String playlistname = MediaStore.Audio.Playlists.NAME;
    String where = MediaStore.Audio.Playlists.NAME + "=?";
    String[] whereVal = { playlist };
    String[] projection = { playlistid, playlistname };
    ContentResolver resolver = context.getContentResolver();
    Cursor cursor = resolver.query(newuri, projection, where, whereVal, null);
    if (cursor != null) {
        recordcount = cursor.getCount();
        String foundplaylistid = "";
        if (recordcount > 0) {
            cursor.moveToFirst();
            int idColumn = cursor.getColumnIndex(playlistid);
            foundplaylistid = cursor.getString(idColumn);
            cursor.close();
        }
        cursor.close();
        return foundplaylistid;
    } else {
        return "";
    }

}

From source file:Main.java

public static boolean isExistShortcut(Activity context, String authorities) {
    boolean isInstallShortcut = false;
    final ContentResolver cr = context.getContentResolver();

    /*/*  w w  w . j av a2s . co m*/
     * if (android.os.Build.VERSION.SDK_INT < 8) { AUTHORITIES =
     * "com.android.launcher.settings"; } else { AUTHORITIES =
     * "com.android.launcher2.settings"; }
     */
    final Uri CONTENT_URI = Uri.parse("content://" + authorities + "/favorites?notify=true");
    Cursor c = cr.query(CONTENT_URI, new String[] { "iconPackage" }, "iconPackage=?",
            new String[] { context.getApplication().getPackageName() }, null);
    if (c != null) {
        if (c.getCount() > 0) {
            isInstallShortcut = true;
        }
        c.close();
    }
    return isInstallShortcut;
}

From source file:com.marvin.rocklock.PlaylistUtils.java

/**
 * Adds a given song to an existing playlist
 *
 * @param context the managing activity/*from  w  w  w .j a v a 2 s.  c  o  m*/
 * @param playlistId the playlist id to append
 * @param id the song id to add
 */
private static void addToPlaylist(RockLockActivity context, int playlistId, String id) {
    ContentResolver resolver = context.getContentResolver();
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
    // Get most recent play order ID from playlist, so we can append
    Cursor orderCursor = resolver.query(uri, new String[] { MediaStore.Audio.Playlists.Members.PLAY_ORDER },
            null, null, MediaStore.Audio.Playlists.Members.PLAY_ORDER + " DESC ");

    int playOrder = 0;
    if (orderCursor != null) {
        if (orderCursor.moveToFirst()) {
            playOrder = orderCursor.getInt(0) + 1;
        }
        orderCursor.close();
    }

    ContentValues value = new ContentValues();
    value.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, id);
    value.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, playOrder++);
    resolver.insert(uri, value);
}

From source file:com.rks.musicx.misc.utils.PlaylistHelper.java

/**
 * Return song Count/*from   w w  w  .j  a  v  a  2 s  .c  om*/
 *
 * @param resolver
 * @param uri
 * @return
 */
public static int getSongCount(ContentResolver resolver, Uri uri) {
    String[] cols = new String[] { "count(*)" };
    Cursor cur = resolver.query(uri, cols, null, null, null);
    if (cur != null) {
        cur.moveToFirst();
        final int count = cur.getInt(0);
        cur.close();
        return count;
    } else {
        return 0;
    }
}

From source file:com.android.emailcommon.utility.AttachmentUtilities.java

/**
 * Resolve attachment id to content URI.  Returns the resolved content URI (from the attachment
 * DB) or, if not found, simply returns the incoming value.
 *
 * @param attachmentUri/*from   w  w w.j  a  v a2 s  .c om*/
 * @return resolved content URI
 *
 * TODO:  Throws an SQLite exception on a missing DB file (e.g. unknown URI) instead of just
 * returning the incoming uri, as it should.
 */
public static Uri resolveAttachmentIdToContentUri(ContentResolver resolver, Uri attachmentUri) {
    Cursor c = resolver.query(attachmentUri, new String[] { Columns.DATA }, null, null, null);
    if (c != null) {
        try {
            if (c.moveToFirst()) {
                final String strUri = c.getString(0);
                if (strUri != null) {
                    return Uri.parse(strUri);
                }
            }
        } finally {
            c.close();
        }
    }
    return attachmentUri;
}

From source file:com.cyanogenmod.filemanager.util.MediaHelper.java

/**
 * Method that returns the album thumbnail path by its identifier.
 *
 * @param cr The ContentResolver/* ww w  . j a  v  a  2  s  .  c  o m*/
 * @param albumId The album identifier to search
 * @return String The album thumbnail path
 */
public static String getAlbumThumbnailPath(@NonNull final ContentResolver cr, final long albumId) {
    final String[] projection = { MediaStore.Audio.Albums.ALBUM_ART };
    final String where = BaseColumns._ID + "=?";
    Cursor c = cr.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, projection, where,
            new String[] { String.valueOf(albumId) }, null);
    try {
        if (c != null && c.moveToFirst()) {
            return c.getString(0);
        }
    } finally {
        IOUtils.closeQuietly(c);
    }
    return null;
}

From source file:net.peterkuterna.android.apps.devoxxsched.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 ww . j a v a2  s  .  c om
        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 curExperience = cursor.getString(SessionsQuery.EXPERIENCE).toLowerCase().trim();
        final String curType = cursor.getString(SessionsQuery.TYPE).toLowerCase().trim();
        final String newTitle = session.getString("title").toLowerCase().trim();
        final String newSummary = session.getString("summary").toLowerCase().trim();
        final String newExperience = session.getString("experience").toLowerCase().trim();
        final String newType = session.getString("type").toLowerCase().trim();

        return (!curTitle.equals(newTitle) || !curSummary.equals(newSummary)
                || !curExperience.equals(newExperience) || !curType.equals(newType));
    } finally {
        cursor.close();
    }
}

From source file:at.flack.receiver.SmsReceiver.java

public static String getContactName(Context context, String 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;
    }//from   w ww.  j a va  2 s  . c  o  m
    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.nbos.phonebook.sync.platform.ContactManager.java

/**
 * Returns the Data id for a sample SyncAdapter contact's profile row, or 0
 * if the sample SyncAdapter user isn't found.
 * //from w w  w.  j  av  a2  s .  c o  m
 * @param resolver a content resolver
 * @param userId the sample SyncAdapter user ID to lookup
 * @return the profile Data row id, or 0 if not found
 */
private static long lookupProfile(ContentResolver resolver, long userId) {
    long profileId = 0;
    final Cursor c = resolver.query(Data.CONTENT_URI, ProfileQuery.PROJECTION, ProfileQuery.SELECTION,
            new String[] { String.valueOf(userId) }, null);
    try {
        if (c != null && c.moveToFirst()) {
            profileId = c.getLong(ProfileQuery.COLUMN_ID);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return profileId;
}

From source file:Main.java

public static void addToCalendar(Activity context, Long beginTime, String title) {
    ContentResolver cr = context.getContentResolver();

    Uri.Builder builder = Uri.parse("content://com.android.calendar/instances/when").buildUpon();
    Long time = new Date(beginTime).getTime();
    ContentUris.appendId(builder, time - 10 * 60 * 1000);
    ContentUris.appendId(builder, time + 10 * 60 * 1000);

    String[] projection = new String[] { "title", "begin" };
    Cursor cursor = cr.query(builder.build(), projection, null, null, null);

    boolean exists = false;
    if (cursor != null) {
        while (cursor.moveToNext()) {
            if ((time == cursor.getLong(1)) && title.equals(cursor.getString(0))) {
                exists = true;// w w w  . ja v a2  s .  c o m
            }
        }
    }

    if (!exists) {
        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("beginTime", time);
        intent.putExtra("allDay", false);
        intent.putExtra("endTime", time + 60 * 60 * 1000);
        intent.putExtra("title", title);
        context.startActivity(intent);
    } else {
        Toast.makeText(context, "Event already exist!", Toast.LENGTH_LONG).show();
    }
}