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