diff options
Diffstat (limited to 'cgeo-contacts')
-rw-r--r-- | cgeo-contacts/.classpath | 1 | ||||
-rw-r--r-- | cgeo-contacts/cgeo-contacts.iml | 2 | ||||
-rw-r--r-- | cgeo-contacts/libs/commons-lang3-3.1.jar | bin | 315805 -> 0 bytes | |||
-rw-r--r-- | cgeo-contacts/libs/commons-lang3-3.3.2.jar | bin | 0 -> 412740 bytes | |||
-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-lt/strings.xml | 1 | ||||
-rw-r--r-- | cgeo-contacts/res/values/strings.xml | 1 | ||||
-rw-r--r-- | cgeo-contacts/src/cgeo/contacts/ContactsActivity.java | 79 |
8 files changed, 64 insertions, 20 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/cgeo-contacts.iml b/cgeo-contacts/cgeo-contacts.iml index fae2f1c..107cc3a 100644 --- a/cgeo-contacts/cgeo-contacts.iml +++ b/cgeo-contacts/cgeo-contacts.iml @@ -18,7 +18,7 @@ </configuration> </facet> </component> - <component name="NewModuleRootManager" inherit-compiler-output="false"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> <output url="file://$MODULE_DIR$/bin/classes" /> <exclude-output /> <content url="file://$MODULE_DIR$"> diff --git a/cgeo-contacts/libs/commons-lang3-3.1.jar b/cgeo-contacts/libs/commons-lang3-3.1.jar Binary files differdeleted file mode 100644 index a85e539..0000000 --- a/cgeo-contacts/libs/commons-lang3-3.1.jar +++ /dev/null diff --git a/cgeo-contacts/libs/commons-lang3-3.3.2.jar b/cgeo-contacts/libs/commons-lang3-3.3.2.jar Binary files differnew file mode 100644 index 0000000..bb06979 --- /dev/null +++ b/cgeo-contacts/libs/commons-lang3-3.3.2.jar 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-lt/strings.xml b/cgeo-contacts/res/values-lt/strings.xml index 78115eb..a2edbd6 100644 --- a/cgeo-contacts/res/values-lt/strings.xml +++ b/cgeo-contacts/res/values-lt/strings.xml @@ -3,4 +3,5 @@ <resources> <string name="app_name">c:geo - kontaktai (įskiepis)</string> <string name="contact_not_found">Kontaktas su %s vardu/pravarde nerastas. Pirmiausia įtraukite jį į savo kontaktų programėlę.</string> + <string name="multiple_matches">Daugiau nei vienas atitikmuo</string> </resources> 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..c9f7656 100644 --- a/cgeo-contacts/src/cgeo/contacts/ContactsActivity.java +++ b/cgeo-contacts/src/cgeo/contacts/ContactsActivity.java @@ -2,8 +2,12 @@ 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 +15,14 @@ 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 +43,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 = getContacts(nickName, ContactsContract.Data.CONTENT_URI, ContactsContract.Data.CONTACT_ID, ContactsContract.CommonDataKinds.Nickname.NAME, false); - // search by display name - if (contactId == 0) { - contactId = getContactId(nickName, ContactsContract.Contacts.CONTENT_URI, BaseColumns._ID, ContactsContract.Contacts.DISPLAY_NAME); + // search by display name, exact + if (contacts.isEmpty()) { + contacts = getContacts(nickName, ContactsContract.Contacts.CONTENT_URI, BaseColumns._ID, ContactsContract.Contacts.DISPLAY_NAME, false); } - if (contactId == 0) { + // search by contained name parts + if (contacts.isEmpty()) { + contacts.addAll(getContacts(nickName, ContactsContract.Data.CONTENT_URI, ContactsContract.Data.CONTACT_ID, ContactsContract.CommonDataKinds.Nickname.NAME, true)); + contacts.addAll(getContacts(nickName, ContactsContract.Contacts.CONTENT_URI, BaseColumns._ID, ContactsContract.Contacts.DISPLAY_NAME, true)); + } + + 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 openContact(int id) { + 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 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>> getContacts(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 +120,7 @@ public final class ContactsActivity extends Activity { cursor.close(); } } - return foundId; + return result; } public final void showToast(final String text) { @@ -93,13 +134,13 @@ public final class ContactsActivity extends Activity { try { final String param = uri.getQueryParameter(paramKey); if (param == null) { - return ""; + return StringUtils.EMPTY; } return URLDecoder.decode(param, CharEncoding.UTF_8).trim(); } catch (UnsupportedEncodingException e) { Log.e(LOG_TAG, "ContactsActivity.getParameter", e); } - return ""; + return StringUtils.EMPTY; } }
\ No newline at end of file |