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