diff options
author | Michael Keppler <michael.keppler@gmx.de> | 2014-04-22 09:06:17 +0200 |
---|---|---|
committer | Michael Keppler <michael.keppler@gmx.de> | 2014-04-22 09:06:17 +0200 |
commit | bd9851163db66bf4fce843ab133a30aa07dfe3ec (patch) | |
tree | 05b734a2f689e403f8b5cdc3e923e0a094a21056 /cgeo-contacts | |
parent | 601db7a7680d5689ff162c6a8b28d0b70bba48aa (diff) | |
download | cgeo-bd9851163db66bf4fce843ab133a30aa07dfe3ec.zip cgeo-bd9851163db66bf4fce843ab133a30aa07dfe3ec.tar.gz cgeo-bd9851163db66bf4fce843ab133a30aa07dfe3ec.tar.bz2 |
fix #3728: improve contact matching
Diffstat (limited to 'cgeo-contacts')
-rw-r--r-- | cgeo-contacts/.classpath | 1 | ||||
-rw-r--r-- | cgeo-contacts/org.eclipse.jdt.annotation_1.1.0.v20130513-1648.jar | bin | 0 -> 14613 bytes | |||
-rw-r--r-- | cgeo-contacts/res/values/strings.xml | 1 | ||||
-rw-r--r-- | cgeo-contacts/src/cgeo/contacts/ContactsActivity.java | 77 |
4 files changed, 60 insertions, 19 deletions
diff --git a/cgeo-contacts/.classpath b/cgeo-contacts/.classpath index 2f1c26a..448b12e 100644 --- a/cgeo-contacts/.classpath +++ b/cgeo-contacts/.classpath @@ -10,5 +10,6 @@ <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/> <classpathentry combineaccessrules="false" kind="src" path="/cgeo"/> <classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/> + <classpathentry kind="lib" path="org.eclipse.jdt.annotation_1.1.0.v20130513-1648.jar"/> <classpathentry kind="output" path="bin/classes"/> </classpath> diff --git a/cgeo-contacts/org.eclipse.jdt.annotation_1.1.0.v20130513-1648.jar b/cgeo-contacts/org.eclipse.jdt.annotation_1.1.0.v20130513-1648.jar Binary files differnew file mode 100644 index 0000000..daca164 --- /dev/null +++ b/cgeo-contacts/org.eclipse.jdt.annotation_1.1.0.v20130513-1648.jar diff --git a/cgeo-contacts/res/values/strings.xml b/cgeo-contacts/res/values/strings.xml index d2ca7fb..f9e555d 100644 --- a/cgeo-contacts/res/values/strings.xml +++ b/cgeo-contacts/res/values/strings.xml @@ -3,5 +3,6 @@ <string name="app_name">c:geo - contacts (add-on)</string> <string name="contact_not_found">Contact with alias/nickname %s not found. Add it in your contacts app first.</string> + <string name="multiple_matches">Multiple matches</string> </resources>
\ No newline at end of file diff --git a/cgeo-contacts/src/cgeo/contacts/ContactsActivity.java b/cgeo-contacts/src/cgeo/contacts/ContactsActivity.java index a9e8e2f..fa24a59 100644 --- a/cgeo-contacts/src/cgeo/contacts/ContactsActivity.java +++ b/cgeo-contacts/src/cgeo/contacts/ContactsActivity.java @@ -2,8 +2,11 @@ package cgeo.contacts; import org.apache.commons.lang3.CharEncoding; import org.apache.commons.lang3.StringUtils; - +import org.eclipse.jdt.annotation.NonNull; import android.app.Activity; +import android.app.AlertDialog; +import android.content.DialogInterface; +import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.database.Cursor; import android.net.Uri; @@ -11,11 +14,13 @@ import android.os.Bundle; import android.provider.BaseColumns; import android.provider.ContactsContract; import android.util.Log; +import android.util.Pair; import android.view.Gravity; import android.widget.Toast; - import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import java.util.ArrayList; +import java.util.List; public final class ContactsActivity extends Activity { static final String LOG_TAG = "cgeo.contacts"; @@ -36,41 +41,75 @@ public final class ContactsActivity extends Activity { return; } - // search by nickname - int contactId = getContactId(nickName, ContactsContract.Data.CONTENT_URI, ContactsContract.Data.CONTACT_ID, ContactsContract.CommonDataKinds.Nickname.NAME); + // search by nickname, exact + List<Pair<Integer, String>> contacts = getContactId(nickName, ContactsContract.Data.CONTENT_URI, ContactsContract.Data.CONTACT_ID, ContactsContract.CommonDataKinds.Nickname.NAME, false); + + // search by display name, exact + if (contacts.isEmpty()) { + contacts = getContactId(nickName, ContactsContract.Contacts.CONTENT_URI, BaseColumns._ID, ContactsContract.Contacts.DISPLAY_NAME, false); + } - // search by display name - if (contactId == 0) { - contactId = getContactId(nickName, ContactsContract.Contacts.CONTENT_URI, BaseColumns._ID, ContactsContract.Contacts.DISPLAY_NAME); + // search by contained name parts + if (contacts.isEmpty()) { + contacts.addAll(getContactId(nickName, ContactsContract.Data.CONTENT_URI, ContactsContract.Data.CONTACT_ID, ContactsContract.CommonDataKinds.Nickname.NAME, true)); + contacts.addAll(getContactId(nickName, ContactsContract.Contacts.CONTENT_URI, BaseColumns._ID, ContactsContract.Contacts.DISPLAY_NAME, true)); } - if (contactId == 0) { + if (contacts.isEmpty()) { showToast(getString(R.string.contact_not_found, nickName)); finish(); return; } - openContact(contactId); - finish(); + if (contacts.size() > 1) { + selectContact(contacts); + } + else { + int contactId = contacts.get(0).first.intValue(); + openContactAndFinish(contactId); + } + } + + private void selectContact(final List<Pair<Integer, String>> contacts) { + List<String> list = new ArrayList<String>(); + for (Pair<Integer, String> p : contacts) { + list.add(p.second); + } + final CharSequence[] items = list.toArray(new CharSequence[list.size()]); + new AlertDialog.Builder(this) + .setTitle(R.string.multiple_matches) + .setItems(items, new OnClickListener() { + + public void onClick(DialogInterface dialog, int which) { + int contactId = contacts.get(which).first.intValue(); + dialog.dismiss(); + openContactAndFinish(contactId); + } + }) + .create().show(); } - private void openContact(int id) { + private void openContactAndFinish(int id) { final Intent intent = new Intent(Intent.ACTION_VIEW); Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(id)); intent.setData(uri); startActivity(intent); + finish(); } - private int getContactId(final String searchName, Uri uri, final String idColumnName, final String selectionColumnName) { - int foundId = 0; - final String[] projection = new String[] { idColumnName }; - final String selection = selectionColumnName + " = ? COLLATE NOCASE"; - final String[] selectionArgs = new String[] { searchName }; + private List<Pair<Integer, String>> getContactId(final @NonNull String searchName, Uri uri, final @NonNull String idColumnName, final @NonNull String selectionColumnName, boolean like) { + final String[] projection = new String[] { idColumnName, selectionColumnName }; + final String selection = selectionColumnName + (like ? " LIKE" : " =") + " ? COLLATE NOCASE"; + final String[] selectionArgs = new String[] { like ? "%" + searchName + "%" : searchName }; Cursor cursor = null; + + List<Pair<Integer, String>> result = new ArrayList<Pair<Integer, String>>(); try { cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null); - if (cursor != null && cursor.moveToNext()) { - foundId = cursor.getInt(0); + while (cursor != null && cursor.moveToNext()) { + int foundId = cursor.getInt(0); + String foundName = cursor.getString(1); + result.add(new Pair<Integer, String>(Integer.valueOf(foundId), foundName)); } } catch (Exception e) { Log.e(LOG_TAG, "ContactsActivity.getContactId", e); @@ -79,7 +118,7 @@ public final class ContactsActivity extends Activity { cursor.close(); } } - return foundId; + return result; } public final void showToast(final String text) { |