diff options
author | Bananeweizen <Bananeweizen@gmx.de> | 2013-12-30 10:08:37 +0100 |
---|---|---|
committer | Bananeweizen <Bananeweizen@gmx.de> | 2013-12-30 10:08:37 +0100 |
commit | 8dcb0e32fc92d0fc81c3f7e708ff6e175458ba8c (patch) | |
tree | a31457d6ecdaa24888b01f4c776883d95b838cc1 /cgeo-contacts/src | |
parent | ab59db6fe636da3143c60407205058d456bd1b79 (diff) | |
download | cgeo-8dcb0e32fc92d0fc81c3f7e708ff6e175458ba8c.zip cgeo-8dcb0e32fc92d0fc81c3f7e708ff6e175458ba8c.tar.gz cgeo-8dcb0e32fc92d0fc81c3f7e708ff6e175458ba8c.tar.bz2 |
refactor: find contact by direct query instead of looping
Diffstat (limited to 'cgeo-contacts/src')
-rw-r--r-- | cgeo-contacts/src/cgeo/contacts/ContactsActivity.java | 43 |
1 files changed, 10 insertions, 33 deletions
diff --git a/cgeo-contacts/src/cgeo/contacts/ContactsActivity.java b/cgeo-contacts/src/cgeo/contacts/ContactsActivity.java index f484340..00a0fde 100644 --- a/cgeo-contacts/src/cgeo/contacts/ContactsActivity.java +++ b/cgeo-contacts/src/cgeo/contacts/ContactsActivity.java @@ -8,7 +8,6 @@ import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; -import android.provider.BaseColumns; import android.provider.ContactsContract; import android.util.Log; import android.view.Gravity; @@ -55,43 +54,21 @@ public final class ContactsActivity extends Activity { } private int getContactId(final String searchName) { - Cursor contactCursor = null; int foundId = 0; + final String[] projection = new String[] { ContactsContract.Data.CONTACT_ID }; + final String selection = ContactsContract.CommonDataKinds.Nickname.NAME + " = ? COLLATE NOCASE"; + final String[] selectionArgs = new String[] { searchName }; + Cursor nicknameCursor = null; try { - contactCursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); - int idColumn = contactCursor.getColumnIndex(BaseColumns._ID); - while (contactCursor.moveToNext()) { - int contactId = contactCursor.getInt(idColumn); - String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; - String[] params = new String[] { String.valueOf(contactId), ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE }; - Cursor nicknameCursor = null; - try { - nicknameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, where, params, null); - while (nicknameCursor.moveToNext()) { - String nicknameName = nicknameCursor.getString(nicknameCursor.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME)); - if (StringUtils.equalsIgnoreCase(nicknameName, searchName)) { - foundId = contactId; - break; - } - } - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } finally { - if (nicknameCursor != null) { - nicknameCursor.close(); - } - } - if (foundId != 0) { - break; - } + nicknameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, selection, selectionArgs, null); + if (nicknameCursor != null && nicknameCursor.moveToNext()) { + foundId = nicknameCursor.getInt(0); } } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + Log.e(LOG_TAG, "ContactsActivity.getContactId", e); } finally { - if (contactCursor != null) { - contactCursor.close(); + if (nicknameCursor != null) { + nicknameCursor.close(); } } return foundId; |