List of usage examples for android.database Cursor isAfterLast
boolean isAfterLast();
From source file:Main.java
/** * Returns the ID for an album./* w ww . j av a 2 s . c o m*/ * * @param context The {@link Context} to use. * @param albumName The name of the album. * @param artistName The name of the artist * @return The ID for an album. */ public static final long getIdForAlbum(final Context context, final String albumName, final String artistName) { Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, new String[] { BaseColumns._ID }, AlbumColumns.ALBUM + "=? AND " + AlbumColumns.ARTIST + "=?", new String[] { albumName, artistName }, AlbumColumns.ALBUM); int id = -1; if (cursor != null) { cursor.moveToFirst(); if (!cursor.isAfterLast()) { id = cursor.getInt(0); } cursor.close(); cursor = null; } return id; }
From source file:Main.java
static public void dumpCursor(Cursor cursor) { final String LOG_TAG = "dumpCursor"; if (cursor != null) { Log.d(LOG_TAG, "cursor " + cursor.getCount()); for (int i = 0; i < cursor.getColumnCount(); i++) Log.d(LOG_TAG, "col " + i + " " + cursor.getColumnName(i)); cursor.moveToFirst();/*from www .j av a2 s .co m*/ String[] a = new String[cursor.getColumnCount()]; while (!cursor.isAfterLast()) { for (int i = 0; i < a.length; i++) a[i] = cursor.getString(i); Log.d(LOG_TAG, Arrays.toString(a)); cursor.moveToNext(); } cursor.moveToFirst(); } }
From source file:com.dahl.brendan.wordsearch.view.IOService.java
private static void writeFile(Context context, File file, boolean overwrite) { if (!file.exists() || overwrite) { Cursor cursor = context.getContentResolver().query(Word.CONTENT_URI, new String[] { Word.WORD }, null, null, null);//from w w w .j a v a 2s .c om JSONArray array = new JSONArray(); if (cursor.moveToFirst()) { while (!cursor.isAfterLast()) { array.put(cursor.getString(0)); cursor.moveToNext(); } } try { file.getParentFile().mkdirs(); file.createNewFile(); BufferedWriter out = new BufferedWriter(new FileWriter(file)); out.write(array.toString()); out.close(); } catch (IOException e) { Log.e(LOGTAG, "write failed", e); } } }
From source file:org.fdroid.enigtext.notifications.MessageNotifier.java
private static void updateNotification(Context context, MasterSecret masterSecret, boolean signal) { Cursor cursor = null; try {//from w ww . j av a 2s .c om cursor = DatabaseFactory.getMmsSmsDatabase(context).getUnread(); if (cursor == null || cursor.isAfterLast()) { ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)) .cancel(NOTIFICATION_ID); return; } NotificationState notificationState = constructNotificationState(context, masterSecret, cursor); if (notificationState.hasMultipleThreads()) { sendMultipleThreadNotification(context, masterSecret, notificationState, signal); } else { sendSingleThreadNotification(context, masterSecret, notificationState, signal); } } finally { if (cursor != null) cursor.close(); } }
From source file:com.cyanogenmod.filemanager.util.MediaHelper.java
/** * Method that returns an array with all the unique albums paths and ids. * * @param cr The ContentResolver/*w ww . ja va 2 s .c om*/ * @return The albums map */ public static Map<String, Long> getAllAlbums(ContentResolver cr) { final Map<String, Long> albums = new HashMap<>(); final String[] projection = { "distinct " + MediaStore.Audio.Media.ALBUM_ID, "substr(" + MediaStore.Audio.Media.DATA + ", 0, length(" + MediaStore.Audio.Media.DATA + ") - length(" + MediaStore.Audio.Media.DISPLAY_NAME + "))" }; final String where = MediaStore.Audio.Media.IS_MUSIC + " = ?"; Cursor c = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, where, new String[] { "1" }, null); if (c != null) { try { for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { long albumId = c.getLong(0); String albumPath = c.getString(1); albums.put(albumPath, albumId); } } finally { IOUtils.closeQuietly(c); } } return albums; }
From source file:nu.firetech.android.pactrack.backend.ParcelUpdater.java
public static void updateAll(boolean autoOnly, final RefreshContext ctx, ParcelDbAdapter dbAdapter) { final Cursor parcel = dbAdapter.fetchAllParcels(autoOnly); if (parcel == null) { UICommon.dbErrorDialog((Context) ctx); return;//from ww w .ja v a 2 s .com } ArrayList<Bundle> workParcels = new ArrayList<Bundle>(); parcel.moveToFirst(); while (!parcel.isAfterLast()) { workParcels.add(cursorToBundle(parcel)); parcel.moveToNext(); } parcel.close(); if (workParcels.isEmpty()) { ctx.refreshDone(); return; } new Thread(new ParcelUpdater(workParcels, ctx)).start(); }
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 w w w .j a v a 2 s . co 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.securecomcode.text.notifications.MessageNotifier.java
private static void updateNotification(Context context, MasterSecret masterSecret, boolean signal) { Cursor telcoCursor = null; Cursor pushCursor = null;//from w ww. j av a 2s . c o m try { telcoCursor = DatabaseFactory.getMmsSmsDatabase(context).getUnread(); pushCursor = DatabaseFactory.getPushDatabase(context).getPending(); if ((telcoCursor == null || telcoCursor.isAfterLast()) && (pushCursor == null || pushCursor.isAfterLast())) { ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)) .cancel(NOTIFICATION_ID); return; } NotificationState notificationState = constructNotificationState(context, masterSecret, telcoCursor); appendPushNotificationState(context, masterSecret, notificationState, pushCursor); if (notificationState.hasMultipleThreads()) { sendMultipleThreadNotification(context, masterSecret, notificationState, signal); } else { sendSingleThreadNotification(context, masterSecret, notificationState, signal); } } finally { if (telcoCursor != null) telcoCursor.close(); if (pushCursor != null) pushCursor.close(); } }
From source file:org.thinschema.dataaccess.JSONAdapter.java
/** * Get all data from table and convert them to JSON. Each row is converted * to its own JSONObject containing the names of the columns as keys, and * the contents as values. For example, if a table whose name is "People" * consists of two columns "FirstName" and "LastName", the generated JSON * would be:/* w ww .jav a2 s .c om*/ * </p> * <code> * { "name": "People", "rows": [ { "FirstName": "John", "LastName": "Doe" }, * { "FirstName": "Susan", "LastName": "Appleseed" } ] } * </code> * * @param database SQLiteDatabase instance. * @param tableName The name of the table. * @return JSONObject instance containing all records. */ public static JSONObject get(SQLiteDatabase database, String tableName) { JSONObject retval = new JSONObject(); Cursor cursor = null; try { retval.put("name", tableName); cursor = database.query(tableName, null, null, null, null, null, null); // we get the list of all column names to make it easier when inserting key-value pairs String[] columnNames = cursor.getColumnNames(); // iterate through each row if (cursor.getCount() > 0) { cursor.moveToFirst(); JSONArray data = new JSONArray(); while (!cursor.isAfterLast()) { JSONObject row = new JSONObject(); // for each column, store the key-value pair, using column name as key for (int i = 0, size = columnNames.length; i < size; ++i) { // convert everything to a string row.put(columnNames[i], cursor.getString(i)); } data.put(row); } // add all of that to the result retval.put("rows", data); } } catch (JSONException e) { e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); } } return retval; }
From source file:org.smssecure.smssecure.notifications.MessageNotifier.java
private static void updateNotification(Context context, MasterSecret masterSecret, int flags, int reminderCount) { Cursor telcoCursor = null; Cursor pushCursor = null;/*from ww w . j a v a 2 s . c o m*/ try { telcoCursor = DatabaseFactory.getMmsSmsDatabase(context).getUnread(); if ((telcoCursor == null || telcoCursor.isAfterLast()) && (pushCursor == null || pushCursor.isAfterLast())) { cancelActiveNotifications(context); updateBadge(context, 0); clearReminder(context); return; } NotificationState notificationState = constructNotificationState(context, masterSecret, telcoCursor); if (notificationState.hasMultipleThreads()) { if (Build.VERSION.SDK_INT >= 23) { for (long threadId : notificationState.getThreads()) { sendSingleThreadNotification(context, masterSecret, new NotificationState(notificationState.getNotificationsForThread(threadId)), 0, true); } } sendMultipleThreadNotification(context, notificationState, flags); } else { sendSingleThreadNotification(context, masterSecret, notificationState, flags, false); } cancelOrphanedNotifications(context, notificationState); updateBadge(context, notificationState.getMessageCount()); if (newNotificationRequested(flags)) { scheduleReminder(context, reminderCount); } } finally { if (telcoCursor != null) telcoCursor.close(); if (pushCursor != null) pushCursor.close(); } }