Example usage for android.database Cursor close

List of usage examples for android.database Cursor close

Introduction

In this page you can find the example usage for android.database Cursor close.

Prototype

void close();

Source Link

Document

Closes the Cursor, releasing all of its resources and making it completely invalid.

Usage

From source file:com.nononsenseapps.notepad.sync.googleapi.GoogleTaskSync.java

static Task loadRemoteTaskFromDB(final Context context, final GoogleTask remoteTask) {
    final Cursor c = context.getContentResolver().query(Task.URI, Task.Columns.FIELDS,
            remoteTask.getTaskWithRemoteClause(), remoteTask.getTaskWithRemoteArgs(), null);
    Task t = null;/*from  w w w  .ja v  a 2 s  .c o  m*/
    try {
        if (c.moveToFirst()) {
            t = new Task(c);
        }
    } finally {
        if (c != null)
            c.close();
    }

    return t;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static String getStringField(String resourcePath, String columnName) {
    String result = null;/*from  w w w  .  j a v a 2  s .c  om*/
    final String[] projection = new String[] { columnName };
    final String[] selectionArgs = new String[] { resourcePath };
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, sSelection, selectionArgs, null);
    try {
        if (c.moveToFirst()) {
            result = c.getString(c.getColumnIndex(columnName));
        }
    } finally {
        c.close();
    }
    return result;
}

From source file:com.hichinaschool.flashcards.libanki.Stats.java

public static double[][] getSmallDueStats(Collection col) {
    ArrayList<int[]> dues = new ArrayList<int[]>();
    Cursor cur = null;
    try {//from www  .  ja  v  a 2 s.  co m
        cur = col.getDb().getDatabase().rawQuery("SELECT (due - " + col.getSched().getToday() + ") AS day, " // day
                + "count(), " // all cards
                + "sum(CASE WHEN ivl >= 21 THEN 1 ELSE 0 END) " // mature cards
                + "FROM cards WHERE did IN " + col.getSched()._deckLimit()
                + " AND queue IN (2,3) AND day <= 7 GROUP BY day ORDER BY day", null);
        while (cur.moveToNext()) {
            dues.add(new int[] { cur.getInt(0), cur.getInt(1), cur.getInt(2) });
        }
    } finally {
        if (cur != null && !cur.isClosed()) {
            cur.close();
        }
    }
    // small adjustment for a proper chartbuilding with achartengine
    if (dues.size() == 0 || dues.get(0)[0] > 0) {
        dues.add(0, new int[] { 0, 0, 0 });
    }
    if (dues.get(dues.size() - 1)[0] < 7) {
        dues.add(new int[] { 7, 0, 0 });
    }
    double[][] serieslist = new double[3][dues.size()];
    for (int i = 0; i < dues.size(); i++) {
        int[] data = dues.get(i);
        serieslist[0][i] = data[0];
        serieslist[1][i] = data[1];
        serieslist[2][i] = data[2];
    }
    return serieslist;
}

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

/**
 * In support of deleting a message, find all attachments and delete associated attachment
 * files.//from   w  w w. j  ava  2s .  c o m
 * @param context
 * @param accountId the account for the message
 * @param messageId the message
 */
public static void deleteAllAttachmentFiles(Context context, long accountId, long messageId) {
    Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, messageId);
    Cursor c = context.getContentResolver().query(uri, Attachment.ID_PROJECTION, null, null, null);
    try {
        while (c.moveToNext()) {
            long attachmentId = c.getLong(Attachment.ID_PROJECTION_COLUMN);
            File attachmentFile = getAttachmentFilename(context, accountId, attachmentId);
            // Note, delete() throws no exceptions for basic FS errors (e.g. file not found)
            // it just returns false, which we ignore, and proceed to the next file.
            // This entire loop is best-effort only.
            attachmentFile.delete();
        }
    } finally {
        c.close();
    }
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static long getSize(final String resourcePath) {
    final String[] projection = new String[] { Nodes.NODE_SIZE };
    final String selection = Nodes.NODE_RESOURCE_PATH + "=?";
    final String[] selectionArgs = new String[] { resourcePath };
    Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    long size = 0L;
    try {//  ww w.j  av a 2  s  . c  om
        if (c.moveToFirst()) {
            size = c.getLong(c.getColumnIndex(Nodes.NODE_SIZE));
        }
    } finally {
        c.close();
    }
    return size;
}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static boolean groupHasUncatchedMessages(String group, Context context) {
    int groupid = getGroupIdFromName(group, context);

    DBHelper db = new DBHelper(context);
    SQLiteDatabase dbread = db.getReadableDatabase();

    String q = "SELECT _id FROM headers WHERE subscribed_group_id=" + groupid + " AND read=0 AND catched=0";
    Cursor c = dbread.rawQuery(q, null);

    int count = c.getCount();

    c.close();
    dbread.close();/*  w  w w. j  av  a2 s. c  o m*/
    db.close();

    return (count > 0);
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static String getPublicUrl(final String resourcePath) {
    final String[] projection = new String[] { Nodes.NODE_PUBLIC_URL };
    final String selection = Nodes.NODE_RESOURCE_PATH + "=?";
    final String[] selectionArgs = new String[] { resourcePath };
    Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    String url = null;// w  w  w. ja  va 2  s .com
    try {
        if (c.moveToFirst()) {
            url = c.getString(c.getColumnIndex(Nodes.NODE_PUBLIC_URL));
        }
    } finally {
        c.close();
    }
    return TextUtils.isEmpty(url) ? null : url;
}

From source file:ro.weednet.contactssync.platform.ContactManager.java

private static long lookupRawContact(ContentResolver resolver, Account account, String serverContactId) {
    long rawContactId = 0;
    final Cursor c = resolver.query(UserIdQuery.CONTENT_URI, UserIdQuery.PROJECTION, UserIdQuery.SELECTION,
            new String[] { account.name, account.type, serverContactId }, null);
    try {/*ww w .  jav a 2s .  c  o m*/
        if ((c != null) && c.moveToFirst()) {
            rawContactId = c.getLong(UserIdQuery.COLUMN_RAW_CONTACT_ID);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return rawContactId;
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static boolean isDirectory(long id) {
    String[] projection = new String[] { Nodes.NODE_KIND };
    String selection = Nodes._ID + "=?";
    String[] selectionArgs = new String[] { String.valueOf(id) };
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);
    String type = null;/* ww  w .ja v a2 s .  com*/
    try {
        if (c.moveToFirst()) {
            type = c.getString(c.getColumnIndex(Nodes.NODE_KIND));
        }
    } finally {
        c.close();
    }
    return (type != null) ? U1NodeKind.DIRECTORY == U1NodeKind.valueOf(type.toUpperCase(Locale.US)) : false;
}

From source file:Main.java

public static File getFromMediaUri(ContentResolver resolver, Uri uri) {
    if (uri == null)
        return null;

    if (SCHEME_FILE.equals(uri.getScheme())) {
        return new File(uri.getPath());
    } else if (SCHEME_CONTENT.equals(uri.getScheme())) {
        final String[] filePathColumn = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME };
        Cursor cursor = null;
        try {/*from  w  ww  . ja va  2 s .c o  m*/
            cursor = resolver.query(uri, filePathColumn, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int columnIndex = (uri.toString().startsWith("content://com.google.android.gallery3d"))
                        ? cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)
                        : cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
                // Picasa image on newer devices with Honeycomb and up
                if (columnIndex != -1) {
                    String filePath = cursor.getString(columnIndex);
                    if (!TextUtils.isEmpty(filePath)) {
                        return new File(filePath);
                    }
                }
            }
        } catch (SecurityException ignored) {
            // Nothing we can do
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
    return null;
}