1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
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;
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";
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Uri uri = getIntent().getData();
if (uri == null) {
finish();
return;
}
final String nickName = getParameter(uri, IContacts.PARAM_NAME);
if (StringUtils.isEmpty(nickName)) {
finish();
return;
}
// 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, exact
if (contacts.isEmpty()) {
contacts = getContacts(nickName, ContactsContract.Contacts.CONTENT_URI, BaseColumns._ID, ContactsContract.Contacts.DISPLAY_NAME, false);
}
// 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;
}
if (contacts.size() > 1) {
selectContact(contacts);
}
else {
final int contactId = contacts.get(0).first;
openContactAndFinish(contactId);
}
}
private void selectContact(@NonNull final List<Pair<Integer, String>> contacts) {
final List<String> list = new ArrayList<>();
for (final 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() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
final int contactId = contacts.get(which).first;
dialog.dismiss();
openContactAndFinish(contactId);
}
})
.create().show();
}
private void openContactAndFinish(final int id) {
final Intent intent = new Intent(Intent.ACTION_VIEW);
final Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(id));
intent.setData(uri);
startActivity(intent);
finish();
}
@NonNull
private List<Pair<Integer, String>> getContacts(final @NonNull String searchName, final Uri uri, final @NonNull String idColumnName, final @NonNull String selectionColumnName, final 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;
final List<Pair<Integer, String>> result = new ArrayList<>();
try {
cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
while (cursor != null && cursor.moveToNext()) {
final int foundId = cursor.getInt(0);
final String foundName = cursor.getString(1);
result.add(new Pair<>(foundId, foundName));
}
} catch (final Exception e) {
Log.e(LOG_TAG, "ContactsActivity.getContactId", e);
} finally {
if (cursor != null) {
cursor.close();
}
}
return result;
}
public final void showToast(final String text) {
final Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 100);
toast.show();
}
@NonNull
private static String getParameter(@NonNull final Uri uri, @NonNull final String paramKey) {
try {
final String param = uri.getQueryParameter(paramKey);
if (param == null) {
return StringUtils.EMPTY;
}
return URLDecoder.decode(param, CharEncoding.UTF_8).trim();
} catch (final UnsupportedEncodingException e) {
Log.e(LOG_TAG, "ContactsActivity.getParameter", e);
}
return StringUtils.EMPTY;
}
}
|