List of usage examples for android.database Cursor moveToFirst
boolean moveToFirst();
From source file:com.adjust.sdk.Util.java
protected static String getAttributionId(final Context context) { try {//from ww w .j a va 2 s .c om final ContentResolver contentResolver = context.getContentResolver(); final Uri uri = Uri.parse("content://com.facebook.katana.provider.AttributionIdProvider"); final String columnName = "aid"; final String[] projection = { columnName }; final Cursor cursor = contentResolver.query(uri, projection, null, null, null); if (null == cursor) { return null; } if (!cursor.moveToFirst()) { cursor.close(); return null; } final String attributionId = cursor.getString(cursor.getColumnIndex(columnName)); cursor.close(); return attributionId; } catch (Exception e) { return null; } }
From source file:com.coinomi.wallet.ExchangeRatesProvider.java
public static List<ExchangeRate> getRates(final Context context, @Nonnull String localSymbol) { ImmutableList.Builder<ExchangeRate> builder = ImmutableList.builder(); if (context != null) { final Uri uri = contentUriToCrypto(context.getPackageName(), localSymbol, true); final Cursor cursor = context.getContentResolver().query(uri, null, null, new String[] { null }, null); if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); do {/*from w w w. j a va 2 s. c o m*/ builder.add(getExchangeRate(cursor)); } while (cursor.moveToNext()); cursor.close(); } } return builder.build(); }
From source file:net.ccghe.emocha.model.DBAdapter.java
public static FileInfo getFile(String path) throws SQLException { FileInfo info;//w ww . j av a 2 s . co m Cursor c = sDB.query(true, TABLE_DOWNLOADS, new String[] { "path", "ts", "size", "md5", "to_delete", "to_download" }, "path='" + path + "'", null, null, null, null, null); if (c != null) { c.moveToFirst(); } info = new FileInfo(c); c.close(); return info; }
From source file:Main.java
/** * Print the content of the cursor/*from w w w . j a v a 2 s . co m*/ * * @param cursor, The cursor, which content needs to be printed * @param logTag, The log tag */ public static void printCursorContent(Cursor cursor, String logTag) { if (cursor == null) { Log.d(logTag, "Cursor is NULL!"); return; } final int columnSpace = 2; ArrayList<Integer> columnWidth = new ArrayList<Integer>(); for (int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) { String value = cursor.getColumnName(columnIndex); int maxWidth = value.length(); if (cursor.moveToFirst()) { do { try { value = cursor.getString(columnIndex); } catch (Exception e) { value = "BLOB"; Log.d(logTag, "Get value from " + cursor.getColumnName(columnIndex) + " failed. Caused by " + e.getLocalizedMessage()); } if (!TextUtils.isEmpty(value) && value.length() > maxWidth) { maxWidth = value.length(); } } while (cursor.moveToNext()); } columnWidth.add(maxWidth + columnSpace); } ArrayList<ArrayList<String>> tableContent = new ArrayList<ArrayList<String>>(); for (int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) { ArrayList<String> columnContent = new ArrayList<String>(); String value = cursor.getColumnName(columnIndex); columnContent.add(appendColumnSpaces(value, columnWidth.get(columnIndex))); if (cursor.moveToFirst()) { do { try { value = cursor.getString(columnIndex); } catch (Exception e) { value = "BLOB"; } columnContent.add(appendColumnSpaces(value, columnWidth.get(columnIndex))); } while (cursor.moveToNext()); } tableContent.add(columnContent); } // Including the header int maxRowIndex = cursor.getCount() + 1; for (int rowIndex = 0; rowIndex < maxRowIndex; rowIndex++) { StringBuilder rowValues = new StringBuilder(); for (int columnIndex = 0; columnIndex < cursor.getColumnCount(); columnIndex++) { ArrayList<String> columnValues = tableContent.get(columnIndex); rowValues.append(columnValues.get(rowIndex)); } Log.d(logTag, rowValues.toString()); } // set the cursor back the first item cursor.moveToFirst(); }
From source file:net.kourlas.voipms_sms.Utils.java
/** * Gets the name of a contact from the Android contacts provider, given a phone number. * * @param applicationContext The application context. * @param phoneNumber The phone number of the contact. * @return the name of the contact./* w ww . j a v a 2s. c om*/ */ public static String getContactName(Context applicationContext, String phoneNumber) { Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); Cursor cursor = applicationContext.getContentResolver().query(uri, new String[] { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null); if (cursor.moveToFirst()) { String name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); cursor.close(); return name; } else { cursor.close(); return null; } }
From source file:com.deliciousdroid.platform.ContactManager.java
/** * Returns the RawContact id for a sample SyncAdapter contact, or 0 if the * sample SyncAdapter user isn't found.//ww w . ja va2 s .co m * * @param context the Authenticator Activity context * @param userId the sample SyncAdapter user ID to lookup * @return the RawContact id, or 0 if not found */ private static long lookupRawContact(ContentResolver resolver, String userName) { long authorId = 0; final Cursor c = resolver.query(RawContacts.CONTENT_URI, UserIdQuery.PROJECTION, UserIdQuery.SELECTION, new String[] { userName }, null); try { if (c.moveToFirst()) { authorId = c.getLong(UserIdQuery.COLUMN_ID); } } finally { if (c != null) { c.close(); } } return authorId; }
From source file:org.jsharkey.sky.webservice.WebserviceHelper.java
/** * Perform a webservice query to retrieve and store the forecast for the * given widget. This call blocks until request is finished and * {@link Forecasts#CONTENT_URI} has been updated. *//*from w w w. ja v a 2s .c om*/ public static void updateForecasts(Context context, Uri appWidgetUri, int days) throws ParseException { if (sUserAgent == null) { prepareUserAgent(context); } Uri appWidgetForecasts = Uri.withAppendedPath(appWidgetUri, AppWidgets.TWIG_FORECASTS); ContentResolver resolver = context.getContentResolver(); Cursor cursor = null; double lat = Double.NaN; double lon = Double.NaN; String countryCode = null; // Pull exact forecast location from database try { cursor = resolver.query(appWidgetUri, PROJECTION_APPWIDGET, null, null, null); if (cursor != null && cursor.moveToFirst()) { lat = cursor.getDouble(COL_LAT); lon = cursor.getDouble(COL_LON); countryCode = cursor.getString(COL_COUNTRY_CODE); } } finally { if (cursor != null) { cursor.close(); } } Log.d(TAG, "using country code=" + countryCode); // Query webservice for this location List<Forecast> forecasts = null; if (COUNTRY_US.equals(countryCode)) { forecasts = new NoaaSource().getForecasts(lat, lon, days); } else { forecasts = new MetarSource().getForecasts(lat, lon, days); } if (forecasts == null || forecasts.size() == 0) { throw new ParseException("No forecasts found from webservice query"); } // Purge existing forecasts covered by incoming data, and anything // before today long lastMidnight = ForecastUtils.getLastMidnight(); long earliest = Long.MAX_VALUE; for (Forecast forecast : forecasts) { earliest = Math.min(earliest, forecast.validStart); } resolver.delete(appWidgetForecasts, ForecastsColumns.VALID_START + " >= " + earliest + " OR " + ForecastsColumns.VALID_START + " <= " + lastMidnight, null); // Insert any new forecasts found ContentValues values = new ContentValues(); for (Forecast forecast : forecasts) { Log.d(TAG, "inserting forecast with validStart=" + forecast.validStart); values.clear(); values.put(ForecastsColumns.VALID_START, forecast.validStart); values.put(ForecastsColumns.TEMP_HIGH, forecast.tempHigh); values.put(ForecastsColumns.TEMP_LOW, forecast.tempLow); values.put(ForecastsColumns.CONDITIONS, forecast.conditions); values.put(ForecastsColumns.URL, forecast.url); if (forecast.alert) { values.put(ForecastsColumns.ALERT, ForecastsColumns.ALERT_TRUE); } resolver.insert(appWidgetForecasts, values); } // Mark widget cache as being updated values.clear(); values.put(AppWidgetsColumns.LAST_UPDATED, System.currentTimeMillis()); resolver.update(appWidgetUri, values, null, null); }
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. * /*ww w . ja v a 2 s . c om*/ * @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:net.kourlas.voipms_sms.Utils.java
/** * Gets a URI pointing to a contact's photo, given the URI for that contact. * * @param applicationContext The application context. * @param uri The URI of the contact. * @return a URI pointing to the contact's photo. *//*from ww w.j a va 2 s .c o m*/ public static String getContactPhotoUri(Context applicationContext, Uri uri) { Cursor cursor = applicationContext.getContentResolver().query(uri, new String[] { ContactsContract.PhoneLookup._ID, ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI }, null, null, null); if (cursor.moveToFirst()) { String photoUri = cursor .getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI)); cursor.close(); return photoUri; } else { cursor.close(); return null; } }
From source file:com.github.chenxiaolong.dualbootpatcher.FileUtils.java
public static UriMetadata[] queryUriMetadata(ContentResolver cr, Uri... uris) { ThreadUtils.enforceExecutionOnNonMainThread(); UriMetadata[] metadatas = new UriMetadata[uris.length]; for (int i = 0; i < metadatas.length; i++) { UriMetadata metadata = new UriMetadata(); metadatas[i] = metadata;/* w w w. j av a 2 s. c o m*/ metadata.uri = uris[i]; metadata.mimeType = cr.getType(metadata.uri); if (SAF_SCHEME.equals(metadata.uri.getScheme())) { Cursor cursor = cr.query(metadata.uri, null, null, null, null, null); try { if (cursor != null && cursor.moveToFirst()) { int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE); metadata.displayName = cursor.getString(nameIndex); if (cursor.isNull(sizeIndex)) { metadata.size = -1; } else { metadata.size = cursor.getLong(sizeIndex); } } } finally { IOUtils.closeQuietly(cursor); } } else if (FILE_SCHEME.equals(metadata.uri.getScheme())) { metadata.displayName = metadata.uri.getLastPathSegment(); metadata.size = new File(metadata.uri.getPath()).length(); } else { throw new IllegalArgumentException("Cannot handle URI: " + metadata.uri); } } return metadatas; }