blob: 99e72b0c7312e63935bd27170acd8f867efee361 [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts;
18
19import static com.android.contacts.ContactEntryAdapter.CONTACT_CUSTOM_RINGTONE_COLUMN;
20import static com.android.contacts.ContactEntryAdapter.CONTACT_NAME_COLUMN;
21import static com.android.contacts.ContactEntryAdapter.CONTACT_NOTES_COLUMN;
22import static com.android.contacts.ContactEntryAdapter.CONTACT_PHONETIC_NAME_COLUMN;
23import static com.android.contacts.ContactEntryAdapter.CONTACT_PROJECTION;
24import static com.android.contacts.ContactEntryAdapter.CONTACT_SEND_TO_VOICEMAIL_COLUMN;
25import static com.android.contacts.ContactEntryAdapter.CONTACT_STARRED_COLUMN;
26import static com.android.contacts.ContactEntryAdapter.METHODS_AUX_DATA_COLUMN;
27import static com.android.contacts.ContactEntryAdapter.METHODS_DATA_COLUMN;
28import static com.android.contacts.ContactEntryAdapter.METHODS_ID_COLUMN;
29import static com.android.contacts.ContactEntryAdapter.METHODS_KIND_COLUMN;
30import static com.android.contacts.ContactEntryAdapter.METHODS_LABEL_COLUMN;
31import static com.android.contacts.ContactEntryAdapter.METHODS_STATUS_COLUMN;
32import static com.android.contacts.ContactEntryAdapter.METHODS_TYPE_COLUMN;
33import static com.android.contacts.ContactEntryAdapter.METHODS_WITH_PRESENCE_PROJECTION;
34import static com.android.contacts.ContactEntryAdapter.ORGANIZATIONS_COMPANY_COLUMN;
35import static com.android.contacts.ContactEntryAdapter.ORGANIZATIONS_ID_COLUMN;
36import static com.android.contacts.ContactEntryAdapter.ORGANIZATIONS_LABEL_COLUMN;
37import static com.android.contacts.ContactEntryAdapter.ORGANIZATIONS_PROJECTION;
38import static com.android.contacts.ContactEntryAdapter.ORGANIZATIONS_TITLE_COLUMN;
39import static com.android.contacts.ContactEntryAdapter.ORGANIZATIONS_TYPE_COLUMN;
40import static com.android.contacts.ContactEntryAdapter.PHONES_ID_COLUMN;
41import static com.android.contacts.ContactEntryAdapter.PHONES_ISPRIMARY_COLUMN;
42import static com.android.contacts.ContactEntryAdapter.PHONES_LABEL_COLUMN;
43import static com.android.contacts.ContactEntryAdapter.PHONES_NUMBER_COLUMN;
44import static com.android.contacts.ContactEntryAdapter.PHONES_PROJECTION;
45import static com.android.contacts.ContactEntryAdapter.PHONES_TYPE_COLUMN;
46
47import com.android.internal.telephony.ITelephony;
48
49import android.app.AlertDialog;
50import android.app.Dialog;
51import android.app.ListActivity;
52import android.content.ActivityNotFoundException;
53import android.content.ContentResolver;
54import android.content.ContentUris;
55import android.content.ContentValues;
56import android.content.Context;
57import android.content.DialogInterface;
58import android.content.Intent;
59import android.content.pm.PackageManager;
60import android.content.pm.ResolveInfo;
61import android.content.res.Resources;
62import android.database.ContentObserver;
63import android.database.Cursor;
64import android.graphics.drawable.Drawable;
65import android.media.Ringtone;
66import android.media.RingtoneManager;
67import android.net.Uri;
68import android.os.Bundle;
69import android.os.Handler;
70import android.os.RemoteException;
71import android.os.ServiceManager;
72import android.os.SystemClock;
73import android.provider.Contacts;
74import android.provider.Im;
75import android.provider.Contacts.ContactMethods;
76import android.provider.Contacts.Organizations;
77import android.provider.Contacts.People;
78import android.provider.Contacts.Phones;
79import android.provider.Contacts.Presence;
80import android.text.TextUtils;
81import android.util.Log;
82import android.view.ContextMenu;
83import android.view.KeyEvent;
84import android.view.Menu;
85import android.view.MenuItem;
86import android.view.View;
87import android.view.ViewGroup;
88import android.view.ContextMenu.ContextMenuInfo;
89import android.widget.AdapterView;
90import android.widget.CheckBox;
91import android.widget.ImageView;
92import android.widget.ListView;
93import android.widget.TextView;
94import android.widget.Toast;
95
96import java.util.ArrayList;
97import java.util.List;
98
99/**
100 * Displays the details of a specific contact.
101 */
102public class ViewContactActivity extends ListActivity
103 implements View.OnCreateContextMenuListener, View.OnClickListener,
104 DialogInterface.OnClickListener {
105 private static final String TAG = "ViewContact";
106 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
107
108 private static final boolean SHOW_SEPARATORS = false;
109
110 private static final String[] PHONE_KEYS = {
111 Contacts.Intents.Insert.PHONE,
112 Contacts.Intents.Insert.SECONDARY_PHONE,
113 Contacts.Intents.Insert.TERTIARY_PHONE
114 };
115
116 private static final String[] EMAIL_KEYS = {
117 Contacts.Intents.Insert.EMAIL,
118 Contacts.Intents.Insert.SECONDARY_EMAIL,
119 Contacts.Intents.Insert.TERTIARY_EMAIL
120 };
121
122 private static final int DIALOG_CONFIRM_DELETE = 1;
123
124 public static final int MENU_ITEM_DELETE = 1;
125 public static final int MENU_ITEM_MAKE_DEFAULT = 2;
126 public static final int MENU_ITEM_SHOW_BARCODE = 3;
127
128 private Uri mUri;
129 private ContentResolver mResolver;
130 private ViewAdapter mAdapter;
131 private int mNumPhoneNumbers = 0;
132
133 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
134 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
135 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
136 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
137 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
138 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
139 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
140 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
141
142 private Cursor mCursor;
143 private boolean mObserverRegistered;
144
145 private ContentObserver mObserver = new ContentObserver(new Handler()) {
146 @Override
147 public boolean deliverSelfNotifications() {
148 return true;
149 }
150
151 @Override
152 public void onChange(boolean selfChange) {
153 if (mCursor != null && !mCursor.isClosed()){
154 dataChanged();
155 }
156 }
157 };
158
159 public void onClick(DialogInterface dialog, int which) {
160 if (mCursor != null) {
161 if (mObserverRegistered) {
162 mCursor.unregisterContentObserver(mObserver);
163 mObserverRegistered = false;
164 }
165 mCursor.close();
166 mCursor = null;
167 }
168 getContentResolver().delete(mUri, null, null);
169 finish();
170 }
171
172 public void onClick(View view) {
173 if (!mObserverRegistered) {
174 return;
175 }
176 switch (view.getId()) {
177 case R.id.star: {
178 int oldStarredState = mCursor.getInt(CONTACT_STARRED_COLUMN);
179 ContentValues values = new ContentValues(1);
180 values.put(People.STARRED, oldStarredState == 1 ? 0 : 1);
181 getContentResolver().update(mUri, values, null, null);
182 break;
183 }
184 }
185 }
186
187 private TextView mNameView;
188 private TextView mPhoneticNameView; // may be null in some locales
189 private ImageView mPhotoView;
190 private int mNoPhotoResource;
191 private CheckBox mStarView;
192 private boolean mShowSmsLinksForAllPhones;
193
194 @Override
195 protected void onCreate(Bundle icicle) {
196 super.onCreate(icicle);
197
198 setContentView(R.layout.view_contact);
199 getListView().setOnCreateContextMenuListener(this);
200
201 mNameView = (TextView) findViewById(R.id.name);
202 mPhoneticNameView = (TextView) findViewById(R.id.phonetic_name);
203 mPhotoView = (ImageView) findViewById(R.id.photo);
204 mStarView = (CheckBox) findViewById(R.id.star);
205 mStarView.setOnClickListener(this);
206
207 // Set the photo with a random "no contact" image
208 long now = SystemClock.elapsedRealtime();
209 int num = (int) now & 0xf;
210 if (num < 9) {
211 // Leaning in from right, common
212 mNoPhotoResource = R.drawable.ic_contact_picture;
213 } else if (num < 14) {
214 // Leaning in from left uncommon
215 mNoPhotoResource = R.drawable.ic_contact_picture_2;
216 } else {
217 // Coming in from the top, rare
218 mNoPhotoResource = R.drawable.ic_contact_picture_3;
219 }
220
221 mUri = getIntent().getData();
222 mResolver = getContentResolver();
223
224 // Build the list of sections. The order they're added to mSections dictates the
225 // order they are displayed in the list.
226 mSections.add(mPhoneEntries);
227 mSections.add(mSmsEntries);
228 mSections.add(mEmailEntries);
229 mSections.add(mImEntries);
230 mSections.add(mPostalEntries);
231 mSections.add(mOrganizationEntries);
232 mSections.add(mOtherEntries);
233
234 //TODO Read this value from a preference
235 mShowSmsLinksForAllPhones = true;
236
237 mCursor = mResolver.query(mUri, CONTACT_PROJECTION, null, null, null);
238 }
239
240 @Override
241 protected void onResume() {
242 super.onResume();
243 mObserverRegistered = true;
244 mCursor.registerContentObserver(mObserver);
245 dataChanged();
246 }
247
248 @Override
249 protected void onPause() {
250 super.onPause();
251 if (mCursor != null) {
252 if (mObserverRegistered) {
253 mObserverRegistered = false;
254 mCursor.unregisterContentObserver(mObserver);
255 }
256 mCursor.deactivate();
257 }
258 }
259
260 @Override
261 protected void onDestroy() {
262 super.onDestroy();
263
264 if (mCursor != null) {
265 if (mObserverRegistered) {
266 mCursor.unregisterContentObserver(mObserver);
267 mObserverRegistered = false;
268 }
269 mCursor.close();
270 }
271 }
272
273 @Override
274 protected Dialog onCreateDialog(int id) {
275 switch (id) {
276 case DIALOG_CONFIRM_DELETE:
277 return new AlertDialog.Builder(this)
278 .setTitle(R.string.deleteConfirmation_title)
279 .setIcon(android.R.drawable.ic_dialog_alert)
280 .setMessage(R.string.deleteConfirmation)
281 .setNegativeButton(android.R.string.cancel, null)
282 .setPositiveButton(android.R.string.ok, this)
283 .setCancelable(false)
284 .create();
285 }
286 return null;
287 }
288
289 private void dataChanged() {
290 mCursor.requery();
291 if (mCursor.moveToFirst()) {
292 // Set the name
293 String name = mCursor.getString(CONTACT_NAME_COLUMN);
294 if (TextUtils.isEmpty(name)) {
295 mNameView.setText(getText(android.R.string.unknownName));
296 } else {
297 mNameView.setText(name);
298 }
299
300 if (mPhoneticNameView != null) {
301 String phoneticName = mCursor.getString(CONTACT_PHONETIC_NAME_COLUMN);
302 mPhoneticNameView.setText(phoneticName);
303 }
304
305 // Load the photo
306 mPhotoView.setImageBitmap(People.loadContactPhoto(this, mUri, mNoPhotoResource,
307 null /* use the default options */));
308
309 // Set the star
310 mStarView.setChecked(mCursor.getInt(CONTACT_STARRED_COLUMN) == 1 ? true : false);
311
312 // Build up the contact entries
313 buildEntries(mCursor);
314 if (mAdapter == null) {
315 mAdapter = new ViewAdapter(this, mSections);
316 setListAdapter(mAdapter);
317 } else {
318 mAdapter.setSections(mSections, SHOW_SEPARATORS);
319 }
320 } else {
321 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
322 Log.e(TAG, "invalid contact uri: " + mUri);
323 finish();
324 }
325 }
326
327 @Override
328 public boolean onCreateOptionsMenu(Menu menu) {
329 menu.add(0, 0, 0, R.string.menu_editContact)
330 .setIcon(android.R.drawable.ic_menu_edit)
331 .setIntent(new Intent(Intent.ACTION_EDIT, mUri))
332 .setAlphabeticShortcut('e');
333 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
334 .setIcon(android.R.drawable.ic_menu_delete);
335
336 return true;
337 }
338
339 @Override
340 public boolean onPrepareOptionsMenu(Menu menu) {
341 super.onPrepareOptionsMenu(menu);
342 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
343 // could be installed or uninstalled at any time.
344 if (isBarcodeScannerInstalled()) {
345 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
346 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
347 .setIcon(R.drawable.ic_menu_show_barcode);
348 }
349 } else {
350 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
351 }
352 return true;
353 }
354
355 private boolean isBarcodeScannerInstalled() {
356 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
357 ResolveInfo ri = getPackageManager().resolveActivity(intent,
358 PackageManager.MATCH_DEFAULT_ONLY);
359 return ri != null;
360 }
361
362 @Override
363 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
364 AdapterView.AdapterContextMenuInfo info;
365 try {
366 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
367 } catch (ClassCastException e) {
368 Log.e(TAG, "bad menuInfo", e);
369 return;
370 }
371
372 // This can be null sometimes, don't crash...
373 if (info == null) {
374 Log.e(TAG, "bad menuInfo");
375 return;
376 }
377
378 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
379 switch (entry.kind) {
380 case Contacts.KIND_PHONE: {
381 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
382 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.auxIntent);
383 if (entry.primaryIcon == -1) {
384 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
385 }
386 break;
387 }
388
389 case Contacts.KIND_EMAIL: {
390 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
391 break;
392 }
393
394 case Contacts.KIND_POSTAL: {
395 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
396 break;
397 }
398 }
399 }
400
401 @Override
402 public boolean onOptionsItemSelected(MenuItem item) {
403 switch (item.getItemId()) {
404 case MENU_ITEM_DELETE: {
405 // Get confirmation
406 showDialog(DIALOG_CONFIRM_DELETE);
407 return true;
408 }
409 case MENU_ITEM_SHOW_BARCODE:
410 if (mCursor.moveToFirst()) {
411 Intent intent = new Intent(SHOW_BARCODE_INTENT);
412 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
413 Bundle bundle = new Bundle();
414 String name = mCursor.getString(CONTACT_NAME_COLUMN);
415 if (!TextUtils.isEmpty(name)) {
416 bundle.putString(Contacts.Intents.Insert.NAME, name);
417 // The 0th ViewEntry in each ArrayList below is a separator item
418 int entriesToAdd = Math.min(mPhoneEntries.size() - 1, PHONE_KEYS.length);
419 for (int x = 0; x < entriesToAdd; x++) {
420 ViewEntry entry = mPhoneEntries.get(x + 1);
421 bundle.putString(PHONE_KEYS[x], entry.data);
422 }
423 entriesToAdd = Math.min(mEmailEntries.size() - 1, EMAIL_KEYS.length);
424 for (int x = 0; x < entriesToAdd; x++) {
425 ViewEntry entry = mEmailEntries.get(x + 1);
426 bundle.putString(EMAIL_KEYS[x], entry.data);
427 }
428 if (mPostalEntries.size() >= 2) {
429 ViewEntry entry = mPostalEntries.get(1);
430 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
431 }
432 intent.putExtra("ENCODE_DATA", bundle);
433 try {
434 startActivity(intent);
435 } catch (ActivityNotFoundException e) {
436 // The check in onPrepareOptionsMenu() should make this impossible, but
437 // for safety I'm catching the exception rather than crashing. Ideally
438 // I'd call Menu.removeItem() here too, but I don't see a way to get
439 // the options menu.
440 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
441 "was not installed.");
442 }
443 return true;
444 }
445 }
446 break;
447 }
448 return super.onOptionsItemSelected(item);
449 }
450
451 @Override
452 public boolean onContextItemSelected(MenuItem item) {
453 switch (item.getItemId()) {
454 case MENU_ITEM_MAKE_DEFAULT: {
455 AdapterView.AdapterContextMenuInfo info;
456 try {
457 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
458 } catch (ClassCastException e) {
459 Log.e(TAG, "bad menuInfo", e);
460 break;
461 }
462
463 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position,
464 SHOW_SEPARATORS);
465 ContentValues values = new ContentValues(1);
466 values.put(People.PRIMARY_PHONE_ID, entry.id);
467 getContentResolver().update(mUri, values, null, null);
468 dataChanged();
469 return true;
470 }
471 }
472 return super.onContextItemSelected(item);
473 }
474
475 @Override
476 public boolean onKeyDown(int keyCode, KeyEvent event) {
477 switch (keyCode) {
478 case KeyEvent.KEYCODE_CALL: {
479 try {
480 ITelephony phone = ITelephony.Stub.asInterface(
481 ServiceManager.checkService("phone"));
482 if (phone != null && !phone.isIdle()) {
483 // Skip out and let the key be handled at a higher level
484 break;
485 }
486 } catch (RemoteException re) {
487 // Fall through and try to call the contact
488 }
489
490 int index = getListView().getSelectedItemPosition();
491 if (index != -1) {
492 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
493 if (entry.kind == Contacts.KIND_PHONE) {
494 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, entry.uri);
495 startActivity(intent);
496 }
497 } else if (mNumPhoneNumbers != 0) {
498 // There isn't anything selected, call the default number
499 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
500 startActivity(intent);
501 }
502 return true;
503 }
504
505 case KeyEvent.KEYCODE_DEL: {
506 showDialog(DIALOG_CONFIRM_DELETE);
507 return true;
508 }
509 }
510
511 return super.onKeyDown(keyCode, event);
512 }
513
514 @Override
515 protected void onListItemClick(ListView l, View v, int position, long id) {
516 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
517 if (entry != null) {
518 Intent intent = entry.intent;
519 if (intent != null) {
520 try {
521 startActivity(intent);
522 } catch (ActivityNotFoundException e) {
523 Log.e(TAG, "No activity found for intent: " + intent);
524 signalError();
525 }
526 } else {
527 signalError();
528 }
529 } else {
530 signalError();
531 }
532 }
533
534 /**
535 * Signal an error to the user via a beep, or some other method.
536 */
537 private void signalError() {
538 //TODO: implement this when we have the sonification APIs
539 }
540
541 /**
542 * Build separator entries for all of the sections.
543 */
544 private void buildSeparators() {
545 ViewEntry separator;
546
547 separator = new ViewEntry();
548 separator.kind = ViewEntry.KIND_SEPARATOR;
549 separator.data = getString(R.string.listSeparatorCallNumber);
550 mPhoneEntries.add(separator);
551
552 separator = new ViewEntry();
553 separator.kind = ViewEntry.KIND_SEPARATOR;
554 separator.data = getString(R.string.listSeparatorSendSmsMms);
555 mSmsEntries.add(separator);
556
557 separator = new ViewEntry();
558 separator.kind = ViewEntry.KIND_SEPARATOR;
559 separator.data = getString(R.string.listSeparatorSendEmail);
560 mEmailEntries.add(separator);
561
562 separator = new ViewEntry();
563 separator.kind = ViewEntry.KIND_SEPARATOR;
564 separator.data = getString(R.string.listSeparatorSendIm);
565 mImEntries.add(separator);
566
567 separator = new ViewEntry();
568 separator.kind = ViewEntry.KIND_SEPARATOR;
569 separator.data = getString(R.string.listSeparatorMapAddress);
570 mPostalEntries.add(separator);
571
572 separator = new ViewEntry();
573 separator.kind = ViewEntry.KIND_SEPARATOR;
574 separator.data = getString(R.string.listSeparatorOrganizations);
575 mOrganizationEntries.add(separator);
576
577 separator = new ViewEntry();
578 separator.kind = ViewEntry.KIND_SEPARATOR;
579 separator.data = getString(R.string.listSeparatorOtherInformation);
580 mOtherEntries.add(separator);
581 }
582
583 private Uri constructImToUrl(String host, String data) {
584 // don't encode the url, because the Activity Manager can't find using the encoded url
585 StringBuilder buf = new StringBuilder("imto://");
586 buf.append(host);
587 buf.append('/');
588 buf.append(data);
589 return Uri.parse(buf.toString());
590 }
591
592 /**
593 * Build up the entries to display on the screen.
594 *
595 * @param personCursor the URI for the contact being displayed
596 */
597 private final void buildEntries(Cursor personCursor) {
598 // Clear out the old entries
599 final int numSections = mSections.size();
600 for (int i = 0; i < numSections; i++) {
601 mSections.get(i).clear();
602 }
603
604 if (SHOW_SEPARATORS) {
605 buildSeparators();
606 }
607
608 // Build up the phone entries
609 final Uri phonesUri = Uri.withAppendedPath(mUri, People.Phones.CONTENT_DIRECTORY);
610 final Cursor phonesCursor = mResolver.query(phonesUri, PHONES_PROJECTION, null, null,
611 Phones.ISPRIMARY + " DESC");
612
613 if (phonesCursor != null) {
614 while (phonesCursor.moveToNext()) {
615 final int type = phonesCursor.getInt(PHONES_TYPE_COLUMN);
616 final String number = phonesCursor.getString(PHONES_NUMBER_COLUMN);
617 final String label = phonesCursor.getString(PHONES_LABEL_COLUMN);
618 final boolean isPrimary = phonesCursor.getInt(PHONES_ISPRIMARY_COLUMN) == 1;
619 final long id = phonesCursor.getLong(PHONES_ID_COLUMN);
620 final Uri uri = ContentUris.withAppendedId(phonesUri, id);
621
622 // Don't crash if the number is bogus
623 if (TextUtils.isEmpty(number)) {
624 Log.w(TAG, "empty number for phone " + id);
625 continue;
626 }
627
628 mNumPhoneNumbers++;
629
630 // Add a phone number entry
631 final ViewEntry entry = new ViewEntry();
632 final CharSequence displayLabel = Phones.getDisplayLabel(this, type, label);
633 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
634 entry.data = number;
635 entry.id = id;
636 entry.uri = uri;
637 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, entry.uri);
638 entry.auxIntent = new Intent(Intent.ACTION_SENDTO,
639 Uri.fromParts("sms", number, null));
640 entry.kind = Contacts.KIND_PHONE;
641 if (isPrimary) {
642 entry.primaryIcon = R.drawable.ic_default_number;
643 }
644 entry.actionIcon = android.R.drawable.sym_action_call;
645 mPhoneEntries.add(entry);
646
647 if (type == Phones.TYPE_MOBILE || mShowSmsLinksForAllPhones) {
648 // Add an SMS entry
649 ViewEntry smsEntry = new ViewEntry();
650 smsEntry.label = buildActionString(R.string.actionText, displayLabel, true);
651 smsEntry.data = number;
652 smsEntry.id = id;
653 smsEntry.uri = uri;
654 smsEntry.intent = entry.auxIntent;
655 smsEntry.kind = ViewEntry.KIND_SMS;
656 smsEntry.actionIcon = R.drawable.sym_action_sms;
657 mSmsEntries.add(smsEntry);
658 }
659 }
660
661 phonesCursor.close();
662 }
663
664 // Build the contact method entries
665 final Uri methodsUri = Uri.withAppendedPath(mUri, People.ContactMethods.CONTENT_DIRECTORY);
666 Cursor methodsCursor = mResolver.query(
667 Uri.withAppendedPath(mUri, "contact_methods_with_presence"),
668 METHODS_WITH_PRESENCE_PROJECTION, null, null, null);
669
670 if (methodsCursor != null) {
671 String[] protocolStrings = getResources().getStringArray(android.R.array.imProtocols);
672
673 while (methodsCursor.moveToNext()) {
674 final int kind = methodsCursor.getInt(METHODS_KIND_COLUMN);
675 final String label = methodsCursor.getString(METHODS_LABEL_COLUMN);
676 final String data = methodsCursor.getString(METHODS_DATA_COLUMN);
677 final int type = methodsCursor.getInt(METHODS_TYPE_COLUMN);
678 final long id = methodsCursor.getLong(METHODS_ID_COLUMN);
679 final Uri uri = ContentUris.withAppendedId(methodsUri, id);
680
681 // Don't crash if the data is bogus
682 if (TextUtils.isEmpty(data)) {
683 Log.w(TAG, "empty data for contact method " + id);
684 continue;
685 }
686
687 ViewEntry entry = new ViewEntry();
688 entry.id = id;
689 entry.uri = uri;
690 entry.kind = kind;
691
692 switch (kind) {
693 case Contacts.KIND_EMAIL:
694 entry.label = buildActionString(R.string.actionEmail,
695 ContactMethods.getDisplayLabel(this, kind, type, label), true);
696 entry.data = data;
697 entry.intent = new Intent(Intent.ACTION_SENDTO,
698 Uri.fromParts("mailto", data, null));
699 entry.actionIcon = android.R.drawable.sym_action_email;
700 mEmailEntries.add(entry);
701 break;
702
703 case Contacts.KIND_POSTAL:
704 entry.label = buildActionString(R.string.actionMap,
705 ContactMethods.getDisplayLabel(this, kind, type, label), true);
706 entry.data = data;
707 entry.maxLines = 4;
708 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
709 entry.actionIcon = R.drawable.sym_action_map;
710 mPostalEntries.add(entry);
711 break;
712
713 case Contacts.KIND_IM: {
714 Object protocolObj = ContactMethods.decodeImProtocol(
715 methodsCursor.getString(METHODS_AUX_DATA_COLUMN));
716 String host;
717 if (protocolObj instanceof Number) {
718 int protocol = ((Number) protocolObj).intValue();
719 entry.label = buildActionString(R.string.actionChat,
720 protocolStrings[protocol], false);
721 host = ContactMethods.lookupProviderNameFromId(protocol).toLowerCase();
722 if (protocol == ContactMethods.PROTOCOL_GOOGLE_TALK
723 || protocol == ContactMethods.PROTOCOL_MSN) {
724 entry.maxLabelLines = 2;
725 }
726 } else {
727 String providerName = (String) protocolObj;
728 entry.label = buildActionString(R.string.actionChat,
729 providerName, false);
730 host = providerName.toLowerCase();
731 }
732
733 // Only add the intent if there is a valid host
734 if (!TextUtils.isEmpty(host)) {
735 entry.intent = new Intent(Intent.ACTION_SENDTO,
736 constructImToUrl(host, data));
737 }
738 entry.data = data;
739 if (!methodsCursor.isNull(METHODS_STATUS_COLUMN)) {
740 entry.presenceIcon = Presence.getPresenceIconResourceId(
741 methodsCursor.getInt(METHODS_STATUS_COLUMN));
742 }
743 entry.actionIcon = android.R.drawable.sym_action_chat;
744 mImEntries.add(entry);
745 break;
746 }
747 }
748 }
749
750 methodsCursor.close();
751 }
752
753 // Build IM entries for things we have presence info about but not explicit IM entries for
754 long personId = ContentUris.parseId(mUri);
755 String[] projection = new String[] {
756 Presence.IM_HANDLE, // 0
757 Presence.IM_PROTOCOL, // 1
758 Presence.PRESENCE_STATUS, // 2
759 };
760 Cursor presenceCursor = mResolver.query(Presence.CONTENT_URI, projection,
761 Presence.PERSON_ID + "=" + personId, null, null);
762 if (presenceCursor != null) {
763 try {
764 while (presenceCursor.moveToNext()) {
765 // Find the display info for the provider
766 String data = presenceCursor.getString(0);
767 String label;
768 Object protocolObj = ContactMethods.decodeImProtocol(
769 presenceCursor.getString(1));
770 String host;
771 if (protocolObj instanceof Number) {
772 int protocol = ((Number) protocolObj).intValue();
773 label = getResources().getStringArray(
774 android.R.array.imProtocols)[protocol];
775 host = ContactMethods.lookupProviderNameFromId(protocol).toLowerCase();
776 } else {
777 String providerName = (String) protocolObj;
778 label = providerName;
779 host = providerName.toLowerCase();
780 }
781
782 if (TextUtils.isEmpty(host)) {
783 // A valid provider name is required
784 continue;
785 }
786
787
788 Intent intent = new Intent(Intent.ACTION_SENDTO, constructImToUrl(host, data));
789
790 // Check to see if there is already an entry for this IM account
791 boolean addEntry = true;
792 int numImEntries = mImEntries.size();
793 for (int i = 0; i < numImEntries; i++) {
794 // Check to see if the intent point to the same thing, if so we won't
795 // add this entry to the list since there is already an explict entry
796 // for the IM account
797 Intent existingIntent = mImEntries.get(i).intent;
798 if (intent.filterEquals(existingIntent)) {
799 addEntry = false;
800 break;
801 }
802 }
803
804 // Add the entry if an existing one wasn't found
805 if (addEntry) {
806 ViewEntry entry = new ViewEntry();
807 entry.kind = Contacts.KIND_IM;
808 entry.data = data;
809 entry.label = label;
810 entry.intent = intent;
811 entry.actionIcon = android.R.drawable.sym_action_chat;
812 entry.presenceIcon = Presence.getPresenceIconResourceId(
813 presenceCursor.getInt(2));
814 entry.maxLabelLines = 2;
815 mImEntries.add(entry);
816 }
817 }
818 } finally {
819 presenceCursor.close();
820 }
821 }
822
823 // Build the organization entries
824 final Uri organizationsUri = Uri.withAppendedPath(mUri, Organizations.CONTENT_DIRECTORY);
825 Cursor organizationsCursor = mResolver.query(organizationsUri, ORGANIZATIONS_PROJECTION,
826 null, null, null);
827
828 if (organizationsCursor != null) {
829 while (organizationsCursor.moveToNext()) {
830 ViewEntry entry = new ViewEntry();
831 entry.id = organizationsCursor.getLong(ORGANIZATIONS_ID_COLUMN);
832 entry.uri = ContentUris.withAppendedId(organizationsUri, entry.id);
833 entry.kind = Contacts.KIND_ORGANIZATION;
834 entry.label = organizationsCursor.getString(ORGANIZATIONS_COMPANY_COLUMN);
835 entry.data = organizationsCursor.getString(ORGANIZATIONS_TITLE_COLUMN);
836 entry.actionIcon = R.drawable.sym_action_organization;
837/*
838 entry.label = Organizations.getDisplayLabel(this,
839 organizationsCursor.getInt(ORGANIZATIONS_TYPE_COLUMN),
840 organizationsCursor.getString(ORGANIZATIONS_LABEL_COLUMN)).toString();
841*/
842 mOrganizationEntries.add(entry);
843 }
844
845 organizationsCursor.close();
846 }
847
848
849 // Build the other entries
850 String note = personCursor.getString(CONTACT_NOTES_COLUMN);
851 if (!TextUtils.isEmpty(note)) {
852 ViewEntry entry = new ViewEntry();
853 entry.label = getString(R.string.label_notes);
854 entry.data = note;
855 entry.id = 0;
856 entry.kind = ViewEntry.KIND_CONTACT;
857 entry.uri = null;
858 entry.intent = null;
859 entry.maxLines = 10;
860 entry.actionIcon = R.drawable.sym_note;
861 mOtherEntries.add(entry);
862 }
863
864 // Build the ringtone entry
865 String ringtoneStr = personCursor.getString(CONTACT_CUSTOM_RINGTONE_COLUMN);
866 if (!TextUtils.isEmpty(ringtoneStr)) {
867 // Get the URI
868 Uri ringtoneUri = Uri.parse(ringtoneStr);
869 if (ringtoneUri != null) {
870 Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
871 if (ringtone != null) {
872 ViewEntry entry = new ViewEntry();
873 entry.label = getString(R.string.label_ringtone);
874 entry.data = ringtone.getTitle(this);
875 entry.kind = ViewEntry.KIND_CONTACT;
876 entry.uri = ringtoneUri;
877 entry.actionIcon = R.drawable.sym_ringtone;
878 mOtherEntries.add(entry);
879 }
880 }
881 }
882
883 // Build the send directly to voice mail entry
884 boolean sendToVoicemail = personCursor.getInt(CONTACT_SEND_TO_VOICEMAIL_COLUMN) == 1;
885 if (sendToVoicemail) {
886 ViewEntry entry = new ViewEntry();
887 entry.label = getString(R.string.actionIncomingCall);
888 entry.data = getString(R.string.detailIncomingCallsGoToVoicemail);
889 entry.kind = ViewEntry.KIND_CONTACT;
890 entry.actionIcon = R.drawable.sym_send_to_voicemail;
891 mOtherEntries.add(entry);
892 }
893 }
894
895 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
896 if (lowerCase) {
897 return getString(actionResId, type.toString().toLowerCase());
898 } else {
899 return getString(actionResId, type.toString());
900 }
901 }
902
903 /**
904 * A basic structure with the data for a contact entry in the list.
905 */
906 final static class ViewEntry extends ContactEntryAdapter.Entry {
907 public int primaryIcon = -1;
908 public Intent intent;
909 public Intent auxIntent = null;
910 public int presenceIcon = -1;
911 public int actionIcon = -1;
912 public int maxLabelLines = 1;
913 }
914
915 private static final class ViewAdapter extends ContactEntryAdapter<ViewEntry> {
916 /** Cache of the children views of a row */
917 static class ViewCache {
918 public TextView label;
919 public TextView data;
920 public ImageView actionIcon;
921 public ImageView presenceIcon;
922
923 // Need to keep track of this too
924 ViewEntry entry;
925 }
926
927 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
928 super(context, sections, SHOW_SEPARATORS);
929 }
930
931 @Override
932 public View getView(int position, View convertView, ViewGroup parent) {
933 ViewEntry entry = getEntry(mSections, position, false);
934 View v;
935
936 // Handle separators specially
937 if (entry.kind == ViewEntry.KIND_SEPARATOR) {
938 TextView separator = (TextView) mInflater.inflate(
939 R.layout.list_separator, parent, SHOW_SEPARATORS);
940 separator.setText(entry.data);
941 return separator;
942 }
943
944 ViewCache views;
945
946 // Check to see if we can reuse convertView
947 if (convertView != null) {
948 v = convertView;
949 views = (ViewCache) v.getTag();
950 } else {
951 // Create a new view if needed
952 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
953
954 // Cache the children
955 views = new ViewCache();
956 views.label = (TextView) v.findViewById(android.R.id.text1);
957 views.data = (TextView) v.findViewById(android.R.id.text2);
958 views.actionIcon = (ImageView) v.findViewById(R.id.icon1);
959 views.presenceIcon = (ImageView) v.findViewById(R.id.icon2);
960 v.setTag(views);
961 }
962
963 // Update the entry in the view cache
964 views.entry = entry;
965
966 // Bind the data to the view
967 bindView(v, entry);
968 return v;
969 }
970
971 @Override
972 protected View newView(int position, ViewGroup parent) {
973 // getView() handles this
974 throw new UnsupportedOperationException();
975 }
976
977 @Override
978 protected void bindView(View view, ViewEntry entry) {
979 final Resources resources = mContext.getResources();
980 ViewCache views = (ViewCache) view.getTag();
981
982 // Set the label
983 TextView label = views.label;
984 setMaxLines(label, entry.maxLabelLines);
985 label.setText(entry.label);
986
987 // Set the data
988 TextView data = views.data;
989 if (data != null) {
990 data.setText(entry.data);
991 setMaxLines(data, entry.maxLines);
992 }
993
994 // Set the action icon
995 ImageView action = views.actionIcon;
996 if (entry.actionIcon != -1) {
997 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
998 action.setVisibility(View.VISIBLE);
999 } else {
1000 // Things should still line up as if there was an icon, so make it invisible
1001 action.setVisibility(View.INVISIBLE);
1002 }
1003
1004 // Set the presence icon
1005 Drawable presenceIcon = null;
1006 if (entry.primaryIcon != -1) {
1007 presenceIcon = resources.getDrawable(entry.primaryIcon);
1008 } else if (entry.presenceIcon != -1) {
1009 presenceIcon = resources.getDrawable(entry.presenceIcon);
1010 }
1011
1012 ImageView presence = views.presenceIcon;
1013 if (presenceIcon != null) {
1014 presence.setImageDrawable(presenceIcon);
1015 presence.setVisibility(View.VISIBLE);
1016 } else {
1017 presence.setVisibility(View.GONE);
1018 }
1019 }
1020
1021 private void setMaxLines(TextView textView, int maxLines) {
1022 if (maxLines == 1) {
1023 textView.setSingleLine(true);
1024 textView.setEllipsize(TextUtils.TruncateAt.END);
1025 } else {
1026 textView.setSingleLine(false);
1027 textView.setMaxLines(maxLines);
1028 textView.setEllipsize(null);
1029 }
1030 }
1031 }
1032}