aboutsummaryrefslogtreecommitdiffstats
path: root/cgeo-contacts/src
diff options
context:
space:
mode:
authorMichael Keppler <michael.keppler@gmx.de>2014-04-22 09:06:17 +0200
committerMichael Keppler <michael.keppler@gmx.de>2014-04-22 09:06:17 +0200
commitbd9851163db66bf4fce843ab133a30aa07dfe3ec (patch)
tree05b734a2f689e403f8b5cdc3e923e0a094a21056 /cgeo-contacts/src
parent601db7a7680d5689ff162c6a8b28d0b70bba48aa (diff)
downloadcgeo-bd9851163db66bf4fce843ab133a30aa07dfe3ec.zip
cgeo-bd9851163db66bf4fce843ab133a30aa07dfe3ec.tar.gz
cgeo-bd9851163db66bf4fce843ab133a30aa07dfe3ec.tar.bz2
fix #3728: improve contact matching
Diffstat (limited to 'cgeo-contacts/src')
-rw-r--r--cgeo-contacts/src/cgeo/contacts/ContactsActivity.java77
1 files changed, 58 insertions, 19 deletions
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) {