List of usage examples for android.content ContentResolver query
public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder)
From source file:com.murrayc.galaxyzoo.app.syncadapter.SyncAdapter.java
/** * Upload any outstanding classifications. * * @return Return true if we know for sure that no further uploading is currently necessary. *//* w ww . ja va 2 s.c o m*/ private boolean uploadOutstandingClassifications() { //To keep things simple, don't do this while it is already happening. //This only ever happens on this thread so there should be no need for a lock here. if (mUploadsInProgress > 0) return false; // TODO: Request re-authentication when the server says we have used the wrong name + api_key. // What does the server reply in that case? // See https://github.com/zooniverse/Galaxy-Zoo/issues/184 final LoginUtils.LoginDetails loginDetails = LoginUtils.getAccountLoginDetails(getContext()); // query the database for any item whose classification is not yet uploaded. final ContentResolver resolver = getContentResolver(); final String[] projection = { Item.Columns._ID, Item.Columns.SUBJECT_ID }; final String whereClause = "(" + Item.Columns.DONE + " == 1) AND " + "(" + Item.Columns.UPLOADED + " != 1)"; final Cursor c = resolver.query(Item.ITEMS_URI, projection, whereClause, new String[] {}, null); //TODO: Order by? if (c.getCount() == 0) { c.close(); return true; //Tell the caller that no action was necessary. } while (c.moveToNext()) { final String itemId = c.getString(0); final String subjectId = c.getString(1); mUploadsInProgress++; final UploadTask task = new UploadTask(itemId, subjectId, loginDetails.name, loginDetails.authApiKey); final Thread thread = new Thread(task); thread.start(); } c.close(); return false; }
From source file:com.visva.voicerecorder.utils.Utils.java
public static Uri getPhotoUriFromPhoneNumber(ContentResolver resolver, String phoneNo) { Uri result = null;/*from w w w. j a va2 s . c om*/ if (phoneNo == "" || "null".equals(phoneNo)) { phoneNo = "111111111"; } String[] projection = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.NUMBER, ContactsContract.Contacts.PHOTO_THUMBNAIL_URI, ContactsContract.PhoneLookup.PHOTO_URI, ContactsContract.PhoneLookup.LOOKUP_KEY }; } else { projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.NUMBER, ContactsContract.PhoneLookup.PHOTO_URI, ContactsContract.PhoneLookup.LOOKUP_KEY }; } Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNo)); Cursor cursor = resolver.query(lookupUri, projection, null, null, null); if (cursor == null) return null; if (cursor.moveToFirst()) { if (cursor.getString(3) != null) result = Uri.parse(cursor.getString(3)); else if (cursor.getString(4) != null) result = Uri.parse(cursor.getString(4)); } if (cursor != null) { cursor.close(); cursor = null; } return result; }
From source file:com.frostwire.android.gui.Librarian.java
/** * Returns a list of Files.//w w w .j a v a 2 s .c o m * * @param offset - from where (starting at 0) * @param pageSize - how many results * @param fetcher - An implementation of TableFetcher * @return List<FileDescriptor> */ private List<FileDescriptor> getFiles(final Context context, int offset, int pageSize, TableFetcher fetcher, String where, String[] whereArgs) { List<FileDescriptor> result = new ArrayList<>(0); if (context == null || fetcher == null) { return result; } Cursor c = null; try { ContentResolver cr = context.getContentResolver(); String[] columns = fetcher.getColumns(); String sort = fetcher.getSortByExpression(); if (where == null) { where = fetcher.where(); whereArgs = fetcher.whereArgs(); } c = cr.query(fetcher.getContentUri(), columns, where, whereArgs, sort); if (c == null || !c.moveToPosition(offset)) { return result; } fetcher.prepare(c); int count = 1; do { FileDescriptor fd = fetcher.fetch(c); result.add(fd); } while (c.moveToNext() && count++ < pageSize); } catch (Throwable e) { Log.e(TAG, "General failure getting files", e); } finally { if (c != null) { c.close(); } } return result; }
From source file:com.phonegap.ContactAccessorSdk3_4.java
/** * Create a ContactField JSONArray//w ww. j av a2 s . com * @param cr database access object * @param contactId the ID to search the database for * @return a JSONArray representing a set of ContactFields */ private JSONArray phoneQuery(ContentResolver cr, String contactId) { Cursor cursor = cr.query(Phones.CONTENT_URI, null, Phones.PERSON_ID + " = ?", new String[] { contactId }, null); JSONArray phones = new JSONArray(); JSONObject phone; while (cursor.moveToNext()) { phone = new JSONObject(); try { phone.put("id", cursor.getString(cursor.getColumnIndex(Phones._ID))); phone.put("perf", false); phone.put("value", cursor.getString(cursor.getColumnIndex(Phones.NUMBER))); phone.put("type", getPhoneType(cursor.getInt(cursor.getColumnIndex(Phones.TYPE)))); phones.put(phone); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); } } return phones; }
From source file:com.phonegap.ContactAccessorSdk3_4.java
/** * A convenience method so we don't duplicate code in doQuery * @param searchTerm/*from w ww .jav a 2s. com*/ * @param contactIds * @param uri * @param projection * @param selection * @param selectionArgs */ private void doQuery(String searchTerm, Set<String> contactIds, Uri uri, String projection, String selection, String[] selectionArgs) { ContentResolver cr = mApp.getContentResolver(); Cursor cursor = cr.query(uri, null, selection, selectionArgs, null); while (cursor.moveToNext()) { contactIds.add(cursor.getString(cursor.getColumnIndex(projection))); } cursor.close(); }
From source file:com.phonegap.ContactAccessorSdk3_4.java
/** * Create a ContactField JSONArray/* w w w . java2 s. com*/ * @param cr database access object * @param contactId the ID to search the database for * @return a JSONArray representing a set of ContactFields */ private JSONArray emailQuery(ContentResolver cr, String contactId) { Cursor cursor = cr.query(ContactMethods.CONTENT_EMAIL_URI, null, ContactMethods.PERSON_ID + " = ?", new String[] { contactId }, null); JSONArray emails = new JSONArray(); JSONObject email; while (cursor.moveToNext()) { email = new JSONObject(); try { email.put("id", cursor.getString(cursor.getColumnIndex(ContactMethods._ID))); email.put("perf", false); email.put("value", cursor.getString(cursor.getColumnIndex(ContactMethods.DATA))); // TODO Find out why adding an email type throws and exception //email.put("type", cursor.getString(cursor.getColumnIndex(ContactMethods.TYPE))); emails.put(email); } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); } } return emails; }
From source file:com.ichi2.anki.tests.ContentProviderTest.java
/** * Query .../models URI/*from w ww.java2 s . c o m*/ */ public void testQueryAllModels() { final ContentResolver cr = getContext().getContentResolver(); // Query all available models final Cursor allModels = cr.query(FlashCardsContract.Model.CONTENT_URI, null, null, null, null); assertNotNull(allModels); try { assertTrue("Check that there is at least one result", allModels.getCount() > 0); while (allModels.moveToNext()) { long modelId = allModels.getLong(allModels.getColumnIndex(FlashCardsContract.Model._ID)); Uri modelUri = Uri.withAppendedPath(FlashCardsContract.Model.CONTENT_URI, Long.toString(modelId)); final Cursor singleModel = cr.query(modelUri, null, null, null, null); assertNotNull(singleModel); try { assertEquals("Check that there is exactly one result", 1, singleModel.getCount()); assertTrue("Move to beginning of cursor", singleModel.moveToFirst()); String nameFromModels = allModels .getString(allModels.getColumnIndex(FlashCardsContract.Model.NAME)); String nameFromModel = singleModel .getString(allModels.getColumnIndex(FlashCardsContract.Model.NAME)); assertEquals("Check that model names are the same", nameFromModel, nameFromModels); String flds = allModels .getString(allModels.getColumnIndex(FlashCardsContract.Model.FIELD_NAMES)); assertTrue("Check that valid number of fields", Utils.splitFields(flds).length >= 1); Integer numCards = allModels .getInt(allModels.getColumnIndex(FlashCardsContract.Model.NUM_CARDS)); assertTrue("Check that valid number of cards", numCards >= 1); } finally { singleModel.close(); } } } finally { allModels.close(); } }
From source file:cn.edu.nju.dapenti.activity.EditFeedActivity.java
public void onClickOk(View view) { // only in insert mode String url = mUrlEditText.getText().toString(); ContentResolver cr = getContentResolver(); if (!url.startsWith(Constants.HTTP) && !url.startsWith(Constants.HTTPS)) { url = Constants.HTTP + url;/*from w ww .j av a 2s . c o m*/ } Cursor cursor = cr.query(FeedColumns.CONTENT_URI, null, FeedColumns.URL + Constants.DB_ARG, new String[] { url }, null); if (cursor.moveToFirst()) { cursor.close(); Toast.makeText(EditFeedActivity.this, R.string.error_feed_url_exists, Toast.LENGTH_LONG).show(); } else { cursor.close(); ContentValues values = new ContentValues(); values.put(FeedColumns.URL, url); values.putNull(FeedColumns.ERROR); String name = mNameEditText.getText().toString(); if (name.trim().length() > 0) { values.put(FeedColumns.NAME, name); } values.put(FeedColumns.RETRIEVE_FULLTEXT, mRetrieveFulltextCb.isChecked() ? 1 : null); cr.insert(FeedColumns.CONTENT_URI, values); cr.notifyChange(FeedColumns.GROUPS_CONTENT_URI, null); cr.notifyChange(FeedColumns.GROUPED_FEEDS_CONTENT_URI, null); } setResult(RESULT_OK); finish(); }
From source file:co.nerdart.ourss.activity.EditFeedActivity.java
public void onClickOk(View view) { // only in insert mode String url = mUrlEditText.getText().toString(); ContentResolver cr = getContentResolver(); if (!url.startsWith(Constants.HTTP) && !url.startsWith(Constants.HTTPS)) { url = Constants.HTTP + url;/*from w ww .ja v a2s . com*/ } Cursor cursor = cr.query(FeedColumns.CONTENT_URI, null, FeedColumns.URL + Constants.DB_ARG, new String[] { url }, null); if (cursor.moveToFirst()) { cursor.close(); Crouton.makeText(EditFeedActivity.this, R.string.error_feed_url_exists, Style.INFO); } else { cursor.close(); ContentValues values = new ContentValues(); values.put(FeedColumns.URL, url); values.putNull(FeedColumns.ERROR); String name = mNameEditText.getText().toString(); if (name.trim().length() > 0) { values.put(FeedColumns.NAME, name); } values.put(FeedColumns.RETRIEVE_FULLTEXT, mRetrieveFulltextCb.isChecked() ? 1 : null); cr.insert(FeedColumns.CONTENT_URI, values); cr.notifyChange(FeedColumns.GROUPS_CONTENT_URI, null); } setResult(RESULT_OK); finish(); }
From source file:com.ichi2.anki.tests.ContentProviderTest.java
/** * Test that a query for all the notes added in setup() looks correct *//*from w w w . ja va2s. c o m*/ public void testQueryNoteIds() { final ContentResolver cr = getContext().getContentResolver(); // Query all available notes final Cursor allNotesCursor = cr.query(FlashCardsContract.Note.CONTENT_URI, null, "tag:" + TEST_TAG, null, null); assertNotNull(allNotesCursor); try { assertEquals("Check number of results", mCreatedNotes.size(), allNotesCursor.getCount()); while (allNotesCursor.moveToNext()) { // Check that it's possible to leave out columns from the projection for (int i = 0; i < FlashCardsContract.Note.DEFAULT_PROJECTION.length; i++) { String[] projection = removeFromProjection(FlashCardsContract.Note.DEFAULT_PROJECTION, i); String noteId = allNotesCursor .getString(allNotesCursor.getColumnIndex(FlashCardsContract.Note._ID)); Uri noteUri = Uri.withAppendedPath(FlashCardsContract.Note.CONTENT_URI, noteId); final Cursor singleNoteCursor = cr.query(noteUri, projection, null, null, null); assertNotNull("Check that there is a valid cursor for detail data", singleNoteCursor); try { assertEquals("Check that there is exactly one result", 1, singleNoteCursor.getCount()); assertTrue("Move to beginning of cursor after querying for detail data", singleNoteCursor.moveToFirst()); // Check columns assertEquals("Check column count", projection.length, singleNoteCursor.getColumnCount()); for (int j = 0; j < projection.length; j++) { assertEquals("Check column name " + j, projection[j], singleNoteCursor.getColumnName(j)); } } finally { singleNoteCursor.close(); } } } } finally { allNotesCursor.close(); } }