blob: d3c51f3f499cb754c53cd34c91ca3e813abd617c [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)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700424 // Correctly handle when section headers are hidden
425 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
426
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800427 bundle.putString(Contacts.Intents.Insert.NAME, name);
428 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700429 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800430 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700431 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800432 bundle.putString(PHONE_KEYS[x], entry.data);
433 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700434 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800435 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700436 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800437 bundle.putString(EMAIL_KEYS[x], entry.data);
438 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700439 if (mPostalEntries.size() >= 1 + sepAdjust) {
440 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800441 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
442 }
443 intent.putExtra("ENCODE_DATA", bundle);
444 try {
445 startActivity(intent);
446 } catch (ActivityNotFoundException e) {
447 // The check in onPrepareOptionsMenu() should make this impossible, but
448 // for safety I'm catching the exception rather than crashing. Ideally
449 // I'd call Menu.removeItem() here too, but I don't see a way to get
450 // the options menu.
451 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
452 "was not installed.");
453 }
454 return true;
455 }
456 }
457 break;
458 }
459 return super.onOptionsItemSelected(item);
460 }
461
462 @Override
463 public boolean onContextItemSelected(MenuItem item) {
464 switch (item.getItemId()) {
465 case MENU_ITEM_MAKE_DEFAULT: {
466 AdapterView.AdapterContextMenuInfo info;
467 try {
468 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
469 } catch (ClassCastException e) {
470 Log.e(TAG, "bad menuInfo", e);
471 break;
472 }
473
474 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position,
475 SHOW_SEPARATORS);
476 ContentValues values = new ContentValues(1);
477 values.put(People.PRIMARY_PHONE_ID, entry.id);
478 getContentResolver().update(mUri, values, null, null);
479 dataChanged();
480 return true;
481 }
482 }
483 return super.onContextItemSelected(item);
484 }
485
486 @Override
487 public boolean onKeyDown(int keyCode, KeyEvent event) {
488 switch (keyCode) {
489 case KeyEvent.KEYCODE_CALL: {
490 try {
491 ITelephony phone = ITelephony.Stub.asInterface(
492 ServiceManager.checkService("phone"));
493 if (phone != null && !phone.isIdle()) {
494 // Skip out and let the key be handled at a higher level
495 break;
496 }
497 } catch (RemoteException re) {
498 // Fall through and try to call the contact
499 }
500
501 int index = getListView().getSelectedItemPosition();
502 if (index != -1) {
503 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
504 if (entry.kind == Contacts.KIND_PHONE) {
505 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, entry.uri);
506 startActivity(intent);
507 }
508 } else if (mNumPhoneNumbers != 0) {
509 // There isn't anything selected, call the default number
510 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
511 startActivity(intent);
512 }
513 return true;
514 }
515
516 case KeyEvent.KEYCODE_DEL: {
517 showDialog(DIALOG_CONFIRM_DELETE);
518 return true;
519 }
520 }
521
522 return super.onKeyDown(keyCode, event);
523 }
524
525 @Override
526 protected void onListItemClick(ListView l, View v, int position, long id) {
527 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
528 if (entry != null) {
529 Intent intent = entry.intent;
530 if (intent != null) {
531 try {
532 startActivity(intent);
533 } catch (ActivityNotFoundException e) {
534 Log.e(TAG, "No activity found for intent: " + intent);
535 signalError();
536 }
537 } else {
538 signalError();
539 }
540 } else {
541 signalError();
542 }
543 }
544
545 /**
546 * Signal an error to the user via a beep, or some other method.
547 */
548 private void signalError() {
549 //TODO: implement this when we have the sonification APIs
550 }
551
552 /**
553 * Build separator entries for all of the sections.
554 */
555 private void buildSeparators() {
556 ViewEntry separator;
557
558 separator = new ViewEntry();
559 separator.kind = ViewEntry.KIND_SEPARATOR;
560 separator.data = getString(R.string.listSeparatorCallNumber);
561 mPhoneEntries.add(separator);
562
563 separator = new ViewEntry();
564 separator.kind = ViewEntry.KIND_SEPARATOR;
565 separator.data = getString(R.string.listSeparatorSendSmsMms);
566 mSmsEntries.add(separator);
567
568 separator = new ViewEntry();
569 separator.kind = ViewEntry.KIND_SEPARATOR;
570 separator.data = getString(R.string.listSeparatorSendEmail);
571 mEmailEntries.add(separator);
572
573 separator = new ViewEntry();
574 separator.kind = ViewEntry.KIND_SEPARATOR;
575 separator.data = getString(R.string.listSeparatorSendIm);
576 mImEntries.add(separator);
577
578 separator = new ViewEntry();
579 separator.kind = ViewEntry.KIND_SEPARATOR;
580 separator.data = getString(R.string.listSeparatorMapAddress);
581 mPostalEntries.add(separator);
582
583 separator = new ViewEntry();
584 separator.kind = ViewEntry.KIND_SEPARATOR;
585 separator.data = getString(R.string.listSeparatorOrganizations);
586 mOrganizationEntries.add(separator);
587
588 separator = new ViewEntry();
589 separator.kind = ViewEntry.KIND_SEPARATOR;
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700590 separator.data = getString(R.string.listSeparatorGroups);
591 mGroupEntries.add(separator);
592
593 separator = new ViewEntry();
594 separator.kind = ViewEntry.KIND_SEPARATOR;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800595 separator.data = getString(R.string.listSeparatorOtherInformation);
596 mOtherEntries.add(separator);
597 }
598
599 private Uri constructImToUrl(String host, String data) {
600 // don't encode the url, because the Activity Manager can't find using the encoded url
601 StringBuilder buf = new StringBuilder("imto://");
602 buf.append(host);
603 buf.append('/');
604 buf.append(data);
605 return Uri.parse(buf.toString());
606 }
607
608 /**
609 * Build up the entries to display on the screen.
610 *
611 * @param personCursor the URI for the contact being displayed
612 */
613 private final void buildEntries(Cursor personCursor) {
614 // Clear out the old entries
615 final int numSections = mSections.size();
616 for (int i = 0; i < numSections; i++) {
617 mSections.get(i).clear();
618 }
619
620 if (SHOW_SEPARATORS) {
621 buildSeparators();
622 }
623
624 // Build up the phone entries
625 final Uri phonesUri = Uri.withAppendedPath(mUri, People.Phones.CONTENT_DIRECTORY);
626 final Cursor phonesCursor = mResolver.query(phonesUri, PHONES_PROJECTION, null, null,
627 Phones.ISPRIMARY + " DESC");
628
629 if (phonesCursor != null) {
630 while (phonesCursor.moveToNext()) {
631 final int type = phonesCursor.getInt(PHONES_TYPE_COLUMN);
632 final String number = phonesCursor.getString(PHONES_NUMBER_COLUMN);
633 final String label = phonesCursor.getString(PHONES_LABEL_COLUMN);
634 final boolean isPrimary = phonesCursor.getInt(PHONES_ISPRIMARY_COLUMN) == 1;
635 final long id = phonesCursor.getLong(PHONES_ID_COLUMN);
636 final Uri uri = ContentUris.withAppendedId(phonesUri, id);
637
638 // Don't crash if the number is bogus
639 if (TextUtils.isEmpty(number)) {
640 Log.w(TAG, "empty number for phone " + id);
641 continue;
642 }
643
644 mNumPhoneNumbers++;
645
646 // Add a phone number entry
647 final ViewEntry entry = new ViewEntry();
648 final CharSequence displayLabel = Phones.getDisplayLabel(this, type, label);
649 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
650 entry.data = number;
651 entry.id = id;
652 entry.uri = uri;
653 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, entry.uri);
654 entry.auxIntent = new Intent(Intent.ACTION_SENDTO,
655 Uri.fromParts("sms", number, null));
656 entry.kind = Contacts.KIND_PHONE;
657 if (isPrimary) {
658 entry.primaryIcon = R.drawable.ic_default_number;
659 }
660 entry.actionIcon = android.R.drawable.sym_action_call;
661 mPhoneEntries.add(entry);
662
663 if (type == Phones.TYPE_MOBILE || mShowSmsLinksForAllPhones) {
664 // Add an SMS entry
665 ViewEntry smsEntry = new ViewEntry();
666 smsEntry.label = buildActionString(R.string.actionText, displayLabel, true);
667 smsEntry.data = number;
668 smsEntry.id = id;
669 smsEntry.uri = uri;
670 smsEntry.intent = entry.auxIntent;
671 smsEntry.kind = ViewEntry.KIND_SMS;
672 smsEntry.actionIcon = R.drawable.sym_action_sms;
673 mSmsEntries.add(smsEntry);
674 }
675 }
676
677 phonesCursor.close();
678 }
679
680 // Build the contact method entries
681 final Uri methodsUri = Uri.withAppendedPath(mUri, People.ContactMethods.CONTENT_DIRECTORY);
682 Cursor methodsCursor = mResolver.query(
683 Uri.withAppendedPath(mUri, "contact_methods_with_presence"),
684 METHODS_WITH_PRESENCE_PROJECTION, null, null, null);
685
686 if (methodsCursor != null) {
687 String[] protocolStrings = getResources().getStringArray(android.R.array.imProtocols);
688
689 while (methodsCursor.moveToNext()) {
690 final int kind = methodsCursor.getInt(METHODS_KIND_COLUMN);
691 final String label = methodsCursor.getString(METHODS_LABEL_COLUMN);
692 final String data = methodsCursor.getString(METHODS_DATA_COLUMN);
693 final int type = methodsCursor.getInt(METHODS_TYPE_COLUMN);
694 final long id = methodsCursor.getLong(METHODS_ID_COLUMN);
695 final Uri uri = ContentUris.withAppendedId(methodsUri, id);
696
697 // Don't crash if the data is bogus
698 if (TextUtils.isEmpty(data)) {
699 Log.w(TAG, "empty data for contact method " + id);
700 continue;
701 }
702
703 ViewEntry entry = new ViewEntry();
704 entry.id = id;
705 entry.uri = uri;
706 entry.kind = kind;
707
708 switch (kind) {
709 case Contacts.KIND_EMAIL:
710 entry.label = buildActionString(R.string.actionEmail,
711 ContactMethods.getDisplayLabel(this, kind, type, label), true);
712 entry.data = data;
713 entry.intent = new Intent(Intent.ACTION_SENDTO,
714 Uri.fromParts("mailto", data, null));
715 entry.actionIcon = android.R.drawable.sym_action_email;
716 mEmailEntries.add(entry);
717 break;
718
719 case Contacts.KIND_POSTAL:
720 entry.label = buildActionString(R.string.actionMap,
721 ContactMethods.getDisplayLabel(this, kind, type, label), true);
722 entry.data = data;
723 entry.maxLines = 4;
724 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
725 entry.actionIcon = R.drawable.sym_action_map;
726 mPostalEntries.add(entry);
727 break;
728
729 case Contacts.KIND_IM: {
730 Object protocolObj = ContactMethods.decodeImProtocol(
731 methodsCursor.getString(METHODS_AUX_DATA_COLUMN));
732 String host;
733 if (protocolObj instanceof Number) {
734 int protocol = ((Number) protocolObj).intValue();
735 entry.label = buildActionString(R.string.actionChat,
736 protocolStrings[protocol], false);
737 host = ContactMethods.lookupProviderNameFromId(protocol).toLowerCase();
738 if (protocol == ContactMethods.PROTOCOL_GOOGLE_TALK
739 || protocol == ContactMethods.PROTOCOL_MSN) {
740 entry.maxLabelLines = 2;
741 }
742 } else {
743 String providerName = (String) protocolObj;
744 entry.label = buildActionString(R.string.actionChat,
745 providerName, false);
746 host = providerName.toLowerCase();
747 }
748
749 // Only add the intent if there is a valid host
750 if (!TextUtils.isEmpty(host)) {
751 entry.intent = new Intent(Intent.ACTION_SENDTO,
752 constructImToUrl(host, data));
753 }
754 entry.data = data;
755 if (!methodsCursor.isNull(METHODS_STATUS_COLUMN)) {
756 entry.presenceIcon = Presence.getPresenceIconResourceId(
757 methodsCursor.getInt(METHODS_STATUS_COLUMN));
758 }
759 entry.actionIcon = android.R.drawable.sym_action_chat;
760 mImEntries.add(entry);
761 break;
762 }
763 }
764 }
765
766 methodsCursor.close();
767 }
768
769 // Build IM entries for things we have presence info about but not explicit IM entries for
770 long personId = ContentUris.parseId(mUri);
771 String[] projection = new String[] {
772 Presence.IM_HANDLE, // 0
773 Presence.IM_PROTOCOL, // 1
774 Presence.PRESENCE_STATUS, // 2
775 };
776 Cursor presenceCursor = mResolver.query(Presence.CONTENT_URI, projection,
777 Presence.PERSON_ID + "=" + personId, null, null);
778 if (presenceCursor != null) {
779 try {
780 while (presenceCursor.moveToNext()) {
781 // Find the display info for the provider
782 String data = presenceCursor.getString(0);
783 String label;
784 Object protocolObj = ContactMethods.decodeImProtocol(
785 presenceCursor.getString(1));
786 String host;
787 if (protocolObj instanceof Number) {
788 int protocol = ((Number) protocolObj).intValue();
789 label = getResources().getStringArray(
790 android.R.array.imProtocols)[protocol];
791 host = ContactMethods.lookupProviderNameFromId(protocol).toLowerCase();
792 } else {
793 String providerName = (String) protocolObj;
794 label = providerName;
795 host = providerName.toLowerCase();
796 }
797
798 if (TextUtils.isEmpty(host)) {
799 // A valid provider name is required
800 continue;
801 }
802
803
804 Intent intent = new Intent(Intent.ACTION_SENDTO, constructImToUrl(host, data));
805
806 // Check to see if there is already an entry for this IM account
807 boolean addEntry = true;
808 int numImEntries = mImEntries.size();
809 for (int i = 0; i < numImEntries; i++) {
810 // Check to see if the intent point to the same thing, if so we won't
811 // add this entry to the list since there is already an explict entry
812 // for the IM account
813 Intent existingIntent = mImEntries.get(i).intent;
814 if (intent.filterEquals(existingIntent)) {
815 addEntry = false;
816 break;
817 }
818 }
819
820 // Add the entry if an existing one wasn't found
821 if (addEntry) {
822 ViewEntry entry = new ViewEntry();
823 entry.kind = Contacts.KIND_IM;
824 entry.data = data;
825 entry.label = label;
826 entry.intent = intent;
827 entry.actionIcon = android.R.drawable.sym_action_chat;
828 entry.presenceIcon = Presence.getPresenceIconResourceId(
829 presenceCursor.getInt(2));
830 entry.maxLabelLines = 2;
831 mImEntries.add(entry);
832 }
833 }
834 } finally {
835 presenceCursor.close();
836 }
837 }
838
839 // Build the organization entries
840 final Uri organizationsUri = Uri.withAppendedPath(mUri, Organizations.CONTENT_DIRECTORY);
841 Cursor organizationsCursor = mResolver.query(organizationsUri, ORGANIZATIONS_PROJECTION,
842 null, null, null);
843
844 if (organizationsCursor != null) {
845 while (organizationsCursor.moveToNext()) {
846 ViewEntry entry = new ViewEntry();
847 entry.id = organizationsCursor.getLong(ORGANIZATIONS_ID_COLUMN);
848 entry.uri = ContentUris.withAppendedId(organizationsUri, entry.id);
849 entry.kind = Contacts.KIND_ORGANIZATION;
850 entry.label = organizationsCursor.getString(ORGANIZATIONS_COMPANY_COLUMN);
851 entry.data = organizationsCursor.getString(ORGANIZATIONS_TITLE_COLUMN);
852 entry.actionIcon = R.drawable.sym_action_organization;
853/*
854 entry.label = Organizations.getDisplayLabel(this,
855 organizationsCursor.getInt(ORGANIZATIONS_TYPE_COLUMN),
856 organizationsCursor.getString(ORGANIZATIONS_LABEL_COLUMN)).toString();
857*/
858 mOrganizationEntries.add(entry);
859 }
860
861 organizationsCursor.close();
862 }
863
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700864 // Build the group entries
865 final Uri groupsUri = Uri.withAppendedPath(mUri,
866 ContactEntryAdapter.GROUP_CONTENT_DIRECTORY);
867 Cursor cursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
868 null, null, Groups.DEFAULT_SORT_ORDER);
869 try {
870 ArrayList<CharSequence> groups = new ArrayList<CharSequence>();
871 ArrayList<CharSequence> prefStrings = new ArrayList<CharSequence>();
872 StringBuilder sb = new StringBuilder();
873
874 while (cursor.moveToNext()) {
875 String systemId = cursor.getString(
876 ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
877
878 if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
879 continue;
880 }
881
882 String name = cursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
883 if (!TextUtils.isEmpty(name)) {
884 if (sb.length() == 0) {
885 sb.append(name);
886 } else {
887 sb.append(getString(R.string.group_list, name));
888 }
889 }
890 }
891
892 if (sb.length() > 0) {
893 ViewEntry entry = new ViewEntry();
894 entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
895 entry.label = getString(R.string.label_groups);
896 entry.data = sb.toString();
Alex Kennbergbd7e1232009-03-27 10:28:23 -0700897 entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700898
899 // TODO: Add an icon for the groups item.
900
901 mGroupEntries.add(entry);
902 }
903 } finally {
904 cursor.close();
905 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800906
907 // Build the other entries
908 String note = personCursor.getString(CONTACT_NOTES_COLUMN);
909 if (!TextUtils.isEmpty(note)) {
910 ViewEntry entry = new ViewEntry();
911 entry.label = getString(R.string.label_notes);
912 entry.data = note;
913 entry.id = 0;
914 entry.kind = ViewEntry.KIND_CONTACT;
915 entry.uri = null;
916 entry.intent = null;
917 entry.maxLines = 10;
918 entry.actionIcon = R.drawable.sym_note;
919 mOtherEntries.add(entry);
920 }
921
922 // 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}