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:Main.java

public static LinkedHashMap<String, Uri> GetMatchedContactsList(Context context, String searchTerm) {
    LinkedHashMap<String, Uri> contactList = new LinkedHashMap<String, Uri>();
    ContentResolver cr = context.getContentResolver();
    String columns[] = { ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI };
    Cursor cur;
    if (searchTerm == null) {
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, columns, null, null, null);
    } else {// ww  w .j  a va  2 s . c o  m
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, columns,
                ContactsContract.Contacts.DISPLAY_NAME + " LIKE "
                        + DatabaseUtils.sqlEscapeString("%" + searchTerm + "%"),
                null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
    }
    if (cur != null) {
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String photoURI = cur
                        .getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
                if (photoURI != null) {
                    Uri thumbUri = Uri.parse(photoURI);
                    contactList.put(name, thumbUri);
                }
            }
        }
        cur.close();
    }
    return contactList;
}

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

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

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

    Cursor c = dbread.rawQuery("SELECT _id FROM headers WHERE read=0 AND subscribed_group_id=" + groupid, null);
    int result = c.getCount();

    c.close();
    dbread.close();/*  w  ww . j  av  a  2 s.c o m*/
    dbhelper.close();
    return result;
}

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

/**
 * In support of deleting a message, find all attachments and delete associated cached
 * attachment files.// w  w  w. ja v a 2  s . c o m
 * @param context
 * @param accountId the account for the message
 * @param messageId the message
 */
public static void deleteAllCachedAttachmentFiles(Context context, long accountId, long messageId) {
    final Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, messageId);
    final Cursor c = context.getContentResolver().query(uri, ATTACHMENT_CACHED_FILE_PROJECTION, null, null,
            null);
    try {
        while (c.moveToNext()) {
            final String fileName = c.getString(0);
            if (!TextUtils.isEmpty(fileName)) {
                final File cachedFile = new File(fileName);
                // 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.
                cachedFile.delete();
            }
        }
    } finally {
        c.close();
    }
}

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

static List<Task> loadNewTasksFromDB(final Context context, final long listdbid, final String account) {
    final Cursor c = context.getContentResolver().query(Task.URI, Task.Columns.FIELDS,
            GoogleTask.getTaskWithoutRemoteClause(),
            GoogleTask.getTaskWithoutRemoteArgs(listdbid, account, GoogleTaskList.SERVICENAME), null);
    final ArrayList<Task> tasks = new ArrayList<Task>();
    try {//from   www.j  a va 2 s  .  c o m
        while (c.moveToNext()) {
            tasks.add(new Task(c));
        }
    } finally {
        if (c != null)
            c.close();
    }

    return tasks;
}

From source file:edu.mit.mobile.android.locast.data.JsonSyncableItem.java

/**
 * Given a public Uri fragment, finds the local item representing it. If there isn't any such item, null is returned.
 *
 * @param context//w  w  w .j a v a2  s .  c om
 * @param dirUri the base local URI to search.
 * @param pubUri A public URI fragment that represents the given item. This must match the result from the API.
 * @return a local URI matching the item or null if none were found.
 */
public static Uri getItemByPubIUri(Context context, Uri dirUri, String pubUri) {
    Uri uri = null;
    final ContentResolver cr = context.getContentResolver();

    final String[] selectionArgs = { pubUri };
    final Cursor c = cr.query(dirUri, PUB_URI_PROJECTION, _PUBLIC_URI + "=?", selectionArgs, null);
    if (c.moveToFirst()) {
        uri = ContentUris.withAppendedId(dirUri, c.getLong(c.getColumnIndex(_ID)));
    }

    c.close();
    return uri;
}

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

public static long ensureGroupExists(Context context, Account account) {
    final ContentResolver resolver = context.getContentResolver();

    // Lookup the group
    long groupId = 0;
    final Cursor cursor = resolver.query(Groups.CONTENT_URI, new String[] { Groups._ID },
            Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=? AND " + Groups.TITLE + "=?",
            new String[] { account.name, account.type, GROUP_NAME }, null);
    if (cursor != null) {
        try {//from  w  w  w .j  a v  a 2  s .co  m
            if (cursor.moveToFirst()) {
                groupId = cursor.getLong(0);
            }
        } finally {
            cursor.close();
        }
    }

    if (groupId == 0) {
        // Group doesn't exist yet, so create it
        final ContentValues contentValues = new ContentValues();
        contentValues.put(Groups.ACCOUNT_NAME, account.name);
        contentValues.put(Groups.ACCOUNT_TYPE, account.type);
        contentValues.put(Groups.TITLE, GROUP_NAME);
        //   contentValues.put(Groups.GROUP_IS_READ_ONLY, true);
        contentValues.put(Groups.GROUP_VISIBLE, true);

        final Uri newGroupUri = resolver.insert(Groups.CONTENT_URI, contentValues);
        groupId = ContentUris.parseId(newGroupUri);
    }
    return groupId;
}

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

public static Set<Integer> getRootNodeIds() {
    Set<Integer> ids = new TreeSet<Integer>();
    final String[] projection = new String[] { Nodes._ID };
    final String selection = Nodes.NODE_PARENT_PATH + " IS NULL";
    final Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, null, null);
    try {//from  w ww  . java  2  s .com
        if (c.moveToFirst()) {
            int id;
            do {
                id = c.getInt(c.getColumnIndex(Nodes._ID));
                ids.add(id);
            } while (c.moveToNext());
        }
    } finally {
        c.close();
    }
    return ids;
}

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

public static long getGroupLastFetchedNumber(String group, Context context) {

    long lastFetched = -1;

    DBHelper dbhelper = new DBHelper(context);
    SQLiteDatabase readdb = dbhelper.getReadableDatabase();

    String fQuery = "SELECT lastFetched FROM subscribed_groups WHERE name=" + esc(group);
    Cursor cur = readdb.rawQuery(fQuery, null);

    if (cur.getCount() > 0) {
        cur.moveToFirst();//  www .j a v a  2s.  c o m
        lastFetched = cur.getInt(0);
    }

    cur.close();
    readdb.close();
    dbhelper.close();

    return lastFetched;
}

From source file:com.eastedge.readnovel.weibo.net.Utility.java

/**
 * Get a HttpClient object which is setting correctly .
 * /*  w  w w. ja va 2s  .  c o  m*/
 * @param context
 *            : context of activity
 * @return HttpClient: HttpClient object
 */
public static HttpClient getHttpClient(Context context) {
    BasicHttpParams httpParameters = new BasicHttpParams();
    // Set the default socket timeout (SO_TIMEOUT) // in
    // milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setConnectionTimeout(httpParameters, Utility.SET_CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParameters, Utility.SET_SOCKET_TIMEOUT);
    HttpClient client = new DefaultHttpClient(httpParameters);
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (!wifiManager.isWifiEnabled()) {
        Uri uri = Uri.parse("content://telephony/carriers/preferapn");
        Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
        if (mCursor != null && mCursor.moveToFirst()) {
            String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
            if (proxyStr != null && proxyStr.trim().length() > 0) {
                HttpHost proxy = new HttpHost(proxyStr, 80);
                client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
            }
            mCursor.close();
        }
    }
    return client;
}

From source file:org.zywx.wbpalmstar.engine.EUtil.java

public static java.net.Proxy checkJavaProxy(Context context) {
    java.net.Proxy proxy = null;/*from w ww .jav a2s.c om*/
    if (!wifiEnable(context)) {// ??APN
        Uri uri = Uri.parse("content://telephony/carriers/preferapn");
        Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
        if (mCursor != null && mCursor.moveToFirst()) {
            String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
            int proxyPort = mCursor.getInt(mCursor.getColumnIndex("port"));
            if (proxyStr != null && proxyStr.trim().length() > 0) {
                if (0 == proxyPort) {
                    proxyPort = 80;
                }
                proxy = new java.net.Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyStr, proxyPort));
            }
            mCursor.close();
        }
    }
    return proxy;
}