blob: 4b90199c05656df34da26031fcce9931230f5e11 [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
Evan Millar66388be2009-05-28 15:41:07 -070019import static com.android.contacts.ContactEntryAdapter.AGGREGATE_DISPLAY_NAME_COLUMN;
20import static com.android.contacts.ContactEntryAdapter.AGGREGATE_PROJECTION;
21import static com.android.contacts.ContactEntryAdapter.AGGREGATE_STARRED_COLUMN;
22import static com.android.contacts.ContactEntryAdapter.DATA_ID_COLUMN;
23import static com.android.contacts.ContactEntryAdapter.DATA_PACKAGE_COLUMN;
24import static com.android.contacts.ContactEntryAdapter.DATA_MIMETYPE_COLUMN;
Evan Millar45e0ed32009-06-01 16:44:38 -070025import static com.android.contacts.ContactEntryAdapter.DATA_IS_PRIMARY_COLUMN;
26import static com.android.contacts.ContactEntryAdapter.DATA_IS_SUPER_PRIMARY_COLUMN;
Evan Millar66388be2009-05-28 15:41:07 -070027import static com.android.contacts.ContactEntryAdapter.DATA_1_COLUMN;
28import static com.android.contacts.ContactEntryAdapter.DATA_2_COLUMN;
29import static com.android.contacts.ContactEntryAdapter.DATA_3_COLUMN;
30import static com.android.contacts.ContactEntryAdapter.DATA_4_COLUMN;
31import static com.android.contacts.ContactEntryAdapter.DATA_5_COLUMN;
32import static com.android.contacts.ContactEntryAdapter.DATA_6_COLUMN;
33import static com.android.contacts.ContactEntryAdapter.DATA_7_COLUMN;
34import static com.android.contacts.ContactEntryAdapter.DATA_8_COLUMN;
35import static com.android.contacts.ContactEntryAdapter.DATA_9_COLUMN;
36import static com.android.contacts.ContactEntryAdapter.DATA_10_COLUMN;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080037
38import com.android.internal.telephony.ITelephony;
39
40import android.app.AlertDialog;
41import android.app.Dialog;
42import android.app.ListActivity;
43import android.content.ActivityNotFoundException;
44import android.content.ContentResolver;
45import android.content.ContentUris;
46import android.content.ContentValues;
47import android.content.Context;
48import android.content.DialogInterface;
49import android.content.Intent;
50import android.content.pm.PackageManager;
51import android.content.pm.ResolveInfo;
52import android.content.res.Resources;
53import android.database.ContentObserver;
54import android.database.Cursor;
Evan Millar45e0ed32009-06-01 16:44:38 -070055import android.graphics.Bitmap;
Evan Millar66388be2009-05-28 15:41:07 -070056import android.graphics.BitmapFactory;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080057import android.graphics.drawable.Drawable;
58import android.media.Ringtone;
59import android.media.RingtoneManager;
60import android.net.Uri;
61import android.os.Bundle;
62import android.os.Handler;
63import android.os.RemoteException;
64import android.os.ServiceManager;
65import android.os.SystemClock;
Evan Millar66388be2009-05-28 15:41:07 -070066import android.provider.ContactsContract.CommonDataKinds;
67import android.provider.ContactsContract.Aggregates;
Evan Millar45e0ed32009-06-01 16:44:38 -070068import android.provider.ContactsContract.Contacts;
Evan Millar66388be2009-05-28 15:41:07 -070069import android.provider.ContactsContract.Data;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080070import android.provider.Im;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080071import android.text.TextUtils;
72import android.util.Log;
73import android.view.ContextMenu;
74import android.view.KeyEvent;
75import android.view.Menu;
76import android.view.MenuItem;
77import android.view.View;
78import android.view.ViewGroup;
79import android.view.ContextMenu.ContextMenuInfo;
80import android.widget.AdapterView;
81import android.widget.CheckBox;
82import android.widget.ImageView;
83import android.widget.ListView;
84import android.widget.TextView;
85import android.widget.Toast;
86
87import java.util.ArrayList;
88import java.util.List;
89
90/**
91 * Displays the details of a specific contact.
92 */
Evan Millar5c22c3b2009-05-29 11:37:54 -070093public class ViewContactActivity extends ListActivity
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080094 implements View.OnCreateContextMenuListener, View.OnClickListener,
95 DialogInterface.OnClickListener {
96 private static final String TAG = "ViewContact";
97 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
98
99 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800100
101 private static final int DIALOG_CONFIRM_DELETE = 1;
102
103 public static final int MENU_ITEM_DELETE = 1;
104 public static final int MENU_ITEM_MAKE_DEFAULT = 2;
105 public static final int MENU_ITEM_SHOW_BARCODE = 3;
106
107 private Uri mUri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700108 private Uri mAggDataUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800109 private ContentResolver mResolver;
110 private ViewAdapter mAdapter;
111 private int mNumPhoneNumbers = 0;
112
113 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
114 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
115 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
116 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
117 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
118 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700119 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800120 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
121 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
122
123 private Cursor mCursor;
124 private boolean mObserverRegistered;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700125
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800126 private ContentObserver mObserver = new ContentObserver(new Handler()) {
127 @Override
128 public boolean deliverSelfNotifications() {
129 return true;
130 }
131
132 @Override
133 public void onChange(boolean selfChange) {
134 if (mCursor != null && !mCursor.isClosed()){
135 dataChanged();
136 }
137 }
138 };
139
140 public void onClick(DialogInterface dialog, int which) {
141 if (mCursor != null) {
142 if (mObserverRegistered) {
143 mCursor.unregisterContentObserver(mObserver);
144 mObserverRegistered = false;
145 }
146 mCursor.close();
147 mCursor = null;
148 }
149 getContentResolver().delete(mUri, null, null);
150 finish();
151 }
152
153 public void onClick(View view) {
154 if (!mObserverRegistered) {
155 return;
156 }
157 switch (view.getId()) {
158 case R.id.star: {
Evan Millar66388be2009-05-28 15:41:07 -0700159 int oldStarredState = mCursor.getInt(AGGREGATE_STARRED_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800160 ContentValues values = new ContentValues(1);
Evan Millar66388be2009-05-28 15:41:07 -0700161 values.put(Aggregates.STARRED, oldStarredState == 1 ? 0 : 1);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800162 getContentResolver().update(mUri, values, null, null);
163 break;
164 }
165 }
166 }
167
168 private TextView mNameView;
169 private TextView mPhoneticNameView; // may be null in some locales
170 private ImageView mPhotoView;
171 private int mNoPhotoResource;
172 private CheckBox mStarView;
173 private boolean mShowSmsLinksForAllPhones;
174
175 @Override
176 protected void onCreate(Bundle icicle) {
177 super.onCreate(icicle);
178
179 setContentView(R.layout.view_contact);
180 getListView().setOnCreateContextMenuListener(this);
181
182 mNameView = (TextView) findViewById(R.id.name);
183 mPhoneticNameView = (TextView) findViewById(R.id.phonetic_name);
184 mPhotoView = (ImageView) findViewById(R.id.photo);
185 mStarView = (CheckBox) findViewById(R.id.star);
186 mStarView.setOnClickListener(this);
187
188 // Set the photo with a random "no contact" image
189 long now = SystemClock.elapsedRealtime();
190 int num = (int) now & 0xf;
191 if (num < 9) {
192 // Leaning in from right, common
193 mNoPhotoResource = R.drawable.ic_contact_picture;
194 } else if (num < 14) {
195 // Leaning in from left uncommon
196 mNoPhotoResource = R.drawable.ic_contact_picture_2;
197 } else {
198 // Coming in from the top, rare
199 mNoPhotoResource = R.drawable.ic_contact_picture_3;
200 }
201
Evan Millar45e0ed32009-06-01 16:44:38 -0700202 mUri = getIntent().getData();
203 mAggDataUri = Uri.withAppendedPath(mUri, "data");
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800204 mResolver = getContentResolver();
205
206 // Build the list of sections. The order they're added to mSections dictates the
207 // order they are displayed in the list.
208 mSections.add(mPhoneEntries);
209 mSections.add(mSmsEntries);
210 mSections.add(mEmailEntries);
211 mSections.add(mImEntries);
212 mSections.add(mPostalEntries);
213 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700214 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800215 mSections.add(mOtherEntries);
216
217 //TODO Read this value from a preference
218 mShowSmsLinksForAllPhones = true;
219
Evan Millar45e0ed32009-06-01 16:44:38 -0700220 mCursor = mResolver.query(mAggDataUri,
221 AGGREGATE_PROJECTION, null, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800222 }
223
224 @Override
225 protected void onResume() {
226 super.onResume();
227 mObserverRegistered = true;
228 mCursor.registerContentObserver(mObserver);
229 dataChanged();
230 }
231
232 @Override
233 protected void onPause() {
234 super.onPause();
235 if (mCursor != null) {
236 if (mObserverRegistered) {
237 mObserverRegistered = false;
238 mCursor.unregisterContentObserver(mObserver);
239 }
240 mCursor.deactivate();
241 }
242 }
243
244 @Override
245 protected void onDestroy() {
246 super.onDestroy();
247
248 if (mCursor != null) {
249 if (mObserverRegistered) {
250 mCursor.unregisterContentObserver(mObserver);
251 mObserverRegistered = false;
252 }
253 mCursor.close();
254 }
255 }
256
257 @Override
258 protected Dialog onCreateDialog(int id) {
259 switch (id) {
260 case DIALOG_CONFIRM_DELETE:
261 return new AlertDialog.Builder(this)
262 .setTitle(R.string.deleteConfirmation_title)
263 .setIcon(android.R.drawable.ic_dialog_alert)
264 .setMessage(R.string.deleteConfirmation)
265 .setNegativeButton(android.R.string.cancel, null)
266 .setPositiveButton(android.R.string.ok, this)
267 .setCancelable(false)
268 .create();
269 }
270 return null;
271 }
272
273 private void dataChanged() {
274 mCursor.requery();
275 if (mCursor.moveToFirst()) {
276 // Set the name
Evan Millar66388be2009-05-28 15:41:07 -0700277 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800278 if (TextUtils.isEmpty(name)) {
279 mNameView.setText(getText(android.R.string.unknownName));
280 } else {
281 mNameView.setText(name);
282 }
283
Evan Millar45e0ed32009-06-01 16:44:38 -0700284 // TODO(emillar) Bring this back.
Evan Millar66388be2009-05-28 15:41:07 -0700285 /*if (mPhoneticNameView != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800286 String phoneticName = mCursor.getString(CONTACT_PHONETIC_NAME_COLUMN);
287 mPhoneticNameView.setText(phoneticName);
Evan Millar45e0ed32009-06-01 16:44:38 -0700288 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800289
290 // Set the star
Evan Millar66388be2009-05-28 15:41:07 -0700291 mStarView.setChecked(mCursor.getInt(AGGREGATE_STARRED_COLUMN) == 1 ? true : false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800292
293 // Build up the contact entries
294 buildEntries(mCursor);
295 if (mAdapter == null) {
296 mAdapter = new ViewAdapter(this, mSections);
297 setListAdapter(mAdapter);
298 } else {
299 mAdapter.setSections(mSections, SHOW_SEPARATORS);
300 }
301 } else {
302 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
303 Log.e(TAG, "invalid contact uri: " + mUri);
304 finish();
305 }
306 }
307
308 @Override
309 public boolean onCreateOptionsMenu(Menu menu) {
310 menu.add(0, 0, 0, R.string.menu_editContact)
311 .setIcon(android.R.drawable.ic_menu_edit)
312 .setIntent(new Intent(Intent.ACTION_EDIT, mUri))
313 .setAlphabeticShortcut('e');
314 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
315 .setIcon(android.R.drawable.ic_menu_delete);
316
317 return true;
318 }
319
320 @Override
321 public boolean onPrepareOptionsMenu(Menu menu) {
322 super.onPrepareOptionsMenu(menu);
323 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
324 // could be installed or uninstalled at any time.
325 if (isBarcodeScannerInstalled()) {
326 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
327 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
328 .setIcon(R.drawable.ic_menu_show_barcode);
329 }
330 } else {
331 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
332 }
333 return true;
334 }
335
336 private boolean isBarcodeScannerInstalled() {
337 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
338 ResolveInfo ri = getPackageManager().resolveActivity(intent,
339 PackageManager.MATCH_DEFAULT_ONLY);
340 return ri != null;
341 }
342
343 @Override
344 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
345 AdapterView.AdapterContextMenuInfo info;
346 try {
347 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
348 } catch (ClassCastException e) {
349 Log.e(TAG, "bad menuInfo", e);
350 return;
351 }
352
353 // This can be null sometimes, don't crash...
354 if (info == null) {
355 Log.e(TAG, "bad menuInfo");
356 return;
357 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700358
Evan Millar45e0ed32009-06-01 16:44:38 -0700359 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
360 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
361 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
362 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.auxIntent);
363 if (entry.primaryIcon == -1) {
364 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800365 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700366 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
367 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
368 if (entry.primaryIcon == -1) {
369 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800370 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700371 } else if (entry.mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)) {
372 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
373 }
374 // TODO(emillar): add back with group support.
375 /* else if (entry.mimetype.equals()) {
376 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
377 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800378 }
379
380 @Override
381 public boolean onOptionsItemSelected(MenuItem item) {
382 switch (item.getItemId()) {
383 case MENU_ITEM_DELETE: {
384 // Get confirmation
385 showDialog(DIALOG_CONFIRM_DELETE);
386 return true;
387 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700388
Evan Millar66388be2009-05-28 15:41:07 -0700389 // TODO(emillar) Bring this back.
390 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800391 if (mCursor.moveToFirst()) {
392 Intent intent = new Intent(SHOW_BARCODE_INTENT);
393 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
394 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700395 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800396 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700397 // Correctly handle when section headers are hidden
398 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700399
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800400 bundle.putString(Contacts.Intents.Insert.NAME, name);
401 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700402 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800403 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700404 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800405 bundle.putString(PHONE_KEYS[x], entry.data);
406 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700407 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800408 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700409 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800410 bundle.putString(EMAIL_KEYS[x], entry.data);
411 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700412 if (mPostalEntries.size() >= 1 + sepAdjust) {
413 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800414 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
415 }
416 intent.putExtra("ENCODE_DATA", bundle);
417 try {
418 startActivity(intent);
419 } catch (ActivityNotFoundException e) {
420 // The check in onPrepareOptionsMenu() should make this impossible, but
421 // for safety I'm catching the exception rather than crashing. Ideally
422 // I'd call Menu.removeItem() here too, but I don't see a way to get
423 // the options menu.
424 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
425 "was not installed.");
426 }
427 return true;
428 }
429 }
Evan Millar66388be2009-05-28 15:41:07 -0700430 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800431 }
432 return super.onOptionsItemSelected(item);
433 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700434
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800435 @Override
436 public boolean onContextItemSelected(MenuItem item) {
437 switch (item.getItemId()) {
438 case MENU_ITEM_MAKE_DEFAULT: {
Evan Millar45e0ed32009-06-01 16:44:38 -0700439 AdapterView.AdapterContextMenuInfo info;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800440 try {
441 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
442 } catch (ClassCastException e) {
443 Log.e(TAG, "bad menuInfo", e);
444 break;
445 }
446
447 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position,
448 SHOW_SEPARATORS);
Evan Millar45e0ed32009-06-01 16:44:38 -0700449
450 // Update the primary values in the data record.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800451 ContentValues values = new ContentValues(1);
Evan Millar45e0ed32009-06-01 16:44:38 -0700452 values.put(Data.IS_PRIMARY, 1);
453 values.put(Data.IS_SUPER_PRIMARY, 1);
454
455 Log.i(TAG, mUri.toString());
456 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
457 values, null, null);
458 dataChanged();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800459 return true;
460 }
461 }
462 return super.onContextItemSelected(item);
463 }
464
465 @Override
466 public boolean onKeyDown(int keyCode, KeyEvent event) {
467 switch (keyCode) {
468 case KeyEvent.KEYCODE_CALL: {
469 try {
470 ITelephony phone = ITelephony.Stub.asInterface(
471 ServiceManager.checkService("phone"));
472 if (phone != null && !phone.isIdle()) {
473 // Skip out and let the key be handled at a higher level
474 break;
475 }
476 } catch (RemoteException re) {
477 // Fall through and try to call the contact
478 }
479
480 int index = getListView().getSelectedItemPosition();
481 if (index != -1) {
482 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700483 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
484 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800485 }
486 } else if (mNumPhoneNumbers != 0) {
487 // There isn't anything selected, call the default number
488 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
489 startActivity(intent);
490 }
491 return true;
492 }
493
494 case KeyEvent.KEYCODE_DEL: {
495 showDialog(DIALOG_CONFIRM_DELETE);
496 return true;
497 }
498 }
499
500 return super.onKeyDown(keyCode, event);
501 }
502
503 @Override
504 protected void onListItemClick(ListView l, View v, int position, long id) {
505 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
506 if (entry != null) {
507 Intent intent = entry.intent;
508 if (intent != null) {
509 try {
510 startActivity(intent);
511 } catch (ActivityNotFoundException e) {
512 Log.e(TAG, "No activity found for intent: " + intent);
513 signalError();
514 }
515 } else {
516 signalError();
517 }
518 } else {
519 signalError();
520 }
521 }
522
523 /**
524 * Signal an error to the user via a beep, or some other method.
525 */
526 private void signalError() {
527 //TODO: implement this when we have the sonification APIs
528 }
529
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800530 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700531 // don't encode the url, because the Activity Manager can't find using the encoded url
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800532 StringBuilder buf = new StringBuilder("imto://");
533 buf.append(host);
534 buf.append('/');
535 buf.append(data);
536 return Uri.parse(buf.toString());
537 }
538
539 /**
540 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700541 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800542 * @param personCursor the URI for the contact being displayed
543 */
Evan Millar66388be2009-05-28 15:41:07 -0700544 private final void buildEntries(Cursor aggCursor) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800545 // Clear out the old entries
546 final int numSections = mSections.size();
547 for (int i = 0; i < numSections; i++) {
548 mSections.get(i).clear();
549 }
550
Evan Millar66388be2009-05-28 15:41:07 -0700551 // Build up method entries
552 if (mUri != null) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700553 Bitmap photoBitmap = null;
Evan Millar66388be2009-05-28 15:41:07 -0700554 while (aggCursor.moveToNext()) {
555 final String mimetype = aggCursor.getString(DATA_MIMETYPE_COLUMN);
556 final String pkg = aggCursor.getString(DATA_PACKAGE_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700557
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800558 ViewEntry entry = new ViewEntry();
Evan Millar66388be2009-05-28 15:41:07 -0700559
560 final long id = aggCursor.getLong(DATA_ID_COLUMN);
561 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800562 entry.id = id;
563 entry.uri = uri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700564 entry.mimetype = mimetype;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800565
Evan Millar66388be2009-05-28 15:41:07 -0700566 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
Evan Millar5c22c3b2009-05-29 11:37:54 -0700567 || mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)
568 || mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)
Evan Millar66388be2009-05-28 15:41:07 -0700569 || mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
570 final int type = aggCursor.getInt(DATA_1_COLUMN);
Evan Millar45e0ed32009-06-01 16:44:38 -0700571 final String label = aggCursor.getString(DATA_3_COLUMN);
572 final String data = aggCursor.getString(DATA_2_COLUMN);
573 final boolean isSuperPrimary = "1".equals(
574 aggCursor.getString(DATA_IS_SUPER_PRIMARY_COLUMN));
Evan Millar66388be2009-05-28 15:41:07 -0700575
576 // Don't crash if the data is bogus
577 if (TextUtils.isEmpty(data)) {
578 Log.w(TAG, "empty data for contact method " + id);
579 continue;
580 }
581
582 // Build phone entries
583 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
584 mNumPhoneNumbers++;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700585
Evan Millar66388be2009-05-28 15:41:07 -0700586 final CharSequence displayLabel = ContactsUtils.getDisplayLabel(
587 this, mimetype, type, label);
588 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
589 entry.data = data;
590 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, entry.uri);
591 entry.auxIntent = new Intent(Intent.ACTION_SENDTO,
592 Uri.fromParts("sms", data, null));
Evan Millar45e0ed32009-06-01 16:44:38 -0700593 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700594 entry.primaryIcon = R.drawable.ic_default_number;
595 }
596 entry.actionIcon = android.R.drawable.sym_action_call;
597 mPhoneEntries.add(entry);
598
Evan Millar5c22c3b2009-05-29 11:37:54 -0700599 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700600 || mShowSmsLinksForAllPhones) {
601 // Add an SMS entry
602 ViewEntry smsEntry = new ViewEntry();
603 smsEntry.label = buildActionString(
604 R.string.actionText, displayLabel, true);
605 smsEntry.data = data;
606 smsEntry.id = id;
607 smsEntry.uri = uri;
608 smsEntry.intent = entry.auxIntent;
Evan Millar45e0ed32009-06-01 16:44:38 -0700609 smsEntry.mimetype = FastTrackWindow.MIME_SMS_ADDRESS;
Evan Millar66388be2009-05-28 15:41:07 -0700610 smsEntry.actionIcon = R.drawable.sym_action_sms;
611 mSmsEntries.add(smsEntry);
612 }
613 // Build email entries
614 } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800615 entry.label = buildActionString(R.string.actionEmail,
Evan Millar66388be2009-05-28 15:41:07 -0700616 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800617 entry.data = data;
618 entry.intent = new Intent(Intent.ACTION_SENDTO,
619 Uri.fromParts("mailto", data, null));
620 entry.actionIcon = android.R.drawable.sym_action_email;
Evan Millar45e0ed32009-06-01 16:44:38 -0700621 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700622 entry.primaryIcon = R.drawable.ic_default_number;
623 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800624 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700625 // Build postal entries
626 } else if (mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800627 entry.label = buildActionString(R.string.actionMap,
Evan Millar66388be2009-05-28 15:41:07 -0700628 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800629 entry.data = data;
630 entry.maxLines = 4;
631 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
632 entry.actionIcon = R.drawable.sym_action_map;
633 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700634 // Build im entries
635 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
636 String[] protocolStrings = getResources().getStringArray(
637 android.R.array.imProtocols);
638 Object protocolObj = ContactsUtils.decodeImProtocol(
639 aggCursor.getString(DATA_5_COLUMN));
Alex Kennberg87fc3172009-03-28 06:43:06 -0700640 String host = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800641 if (protocolObj instanceof Number) {
642 int protocol = ((Number) protocolObj).intValue();
643 entry.label = buildActionString(R.string.actionChat,
644 protocolStrings[protocol], false);
Evan Millar66388be2009-05-28 15:41:07 -0700645 host = ContactsUtils.lookupProviderNameFromId(
646 protocol).toLowerCase();
647 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
648 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800649 entry.maxLabelLines = 2;
650 }
Alex Kennberg87fc3172009-03-28 06:43:06 -0700651 } else if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800652 String providerName = (String) protocolObj;
653 entry.label = buildActionString(R.string.actionChat,
654 providerName, false);
655 host = providerName.toLowerCase();
656 }
657
658 // Only add the intent if there is a valid host
659 if (!TextUtils.isEmpty(host)) {
660 entry.intent = new Intent(Intent.ACTION_SENDTO,
661 constructImToUrl(host, data));
662 }
663 entry.data = data;
Evan Millar66388be2009-05-28 15:41:07 -0700664 //TODO(emillar) Do we want presence info?
665 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800666 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700667 aggCursor.getInt(METHODS_STATUS_COLUMN));
668 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800669 entry.actionIcon = android.R.drawable.sym_action_chat;
670 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700671 }
Evan Millar66388be2009-05-28 15:41:07 -0700672 // Build organization entries
673 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
674 final int type = aggCursor.getInt(DATA_1_COLUMN);
675 final String label = aggCursor.getString(DATA_2_COLUMN);
676 final String company = aggCursor.getString(DATA_3_COLUMN);
677 final String title = aggCursor.getString(DATA_4_COLUMN);
678 final boolean isPrimary = "1".equals(aggCursor.getString(DATA_5_COLUMN));
679
680 if (isPrimary) {
681 entry.primaryIcon = R.drawable.ic_default_number;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800682 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700683
Evan Millar66388be2009-05-28 15:41:07 -0700684 // Don't crash if the data is bogus
685 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
686 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800687 continue;
688 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700689
Evan Millar66388be2009-05-28 15:41:07 -0700690 entry.data = title;
691 entry.actionIcon = R.drawable.sym_action_organization;
692 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800693
Evan Millar66388be2009-05-28 15:41:07 -0700694 mOrganizationEntries.add(entry);
695 // Build note entries
696 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
697 entry.label = getString(R.string.label_notes);
698 entry.data = aggCursor.getString(DATA_1_COLUMN);
699 entry.id = 0;
700 entry.uri = null;
701 entry.intent = null;
702 entry.maxLines = 10;
703 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800704
Evan Millar66388be2009-05-28 15:41:07 -0700705 if (TextUtils.isEmpty(entry.data)) {
706 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700707 continue;
708 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700709
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800710 mOtherEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700711 // Build the ringtone and send to voicemail entries
712 } else if (mimetype.equals(CommonDataKinds.CustomRingtone.CONTENT_ITEM_TYPE)) {
713 final Boolean sendToVoicemail = "1".equals(aggCursor.getString(DATA_1_COLUMN));
714 final String ringtoneStr = aggCursor.getString(DATA_2_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800715
Evan Millar5c22c3b2009-05-29 11:37:54 -0700716 // TODO(emillar) we need to enforce uniqueness of custom ringtone entries on a
Evan Millar66388be2009-05-28 15:41:07 -0700717 // single aggregate. This could be done by checking against the reference ID stored
718 // in the aggregate.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700719
Evan Millar66388be2009-05-28 15:41:07 -0700720 // Build the send directly to voice mail entry
721 if (sendToVoicemail) {
722 entry.label = getString(R.string.actionIncomingCall);
723 entry.data = getString(R.string.detailIncomingCallsGoToVoicemail);
724 entry.actionIcon = R.drawable.sym_send_to_voicemail;
725 mOtherEntries.add(entry);
726 } else if (!TextUtils.isEmpty(ringtoneStr)) {
727 // Get the URI
728 Uri ringtoneUri = Uri.parse(ringtoneStr);
729 if (ringtoneUri != null) {
730 Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
731 if (ringtone != null) {
732 entry.label = getString(R.string.label_ringtone);
733 entry.data = ringtone.getTitle(this);
734 entry.uri = ringtoneUri;
735 entry.actionIcon = R.drawable.sym_ringtone;
736 mOtherEntries.add(entry);
737 }
738 }
739 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700740 // Load the photo
741 } else if (mimetype.equals(CommonDataKinds.Photo.CONTENT_ITEM_TYPE)) {
742 photoBitmap = ContactsUtils.loadContactPhoto(aggCursor, DATA_1_COLUMN, null);
Evan Millar66388be2009-05-28 15:41:07 -0700743 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700744
Evan Millar45e0ed32009-06-01 16:44:38 -0700745
Evan Millar66388be2009-05-28 15:41:07 -0700746 // TODO(emillar) Add group entries
747// // Build the group entries
748// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
749// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
750// null, null, Groups.DEFAULT_SORT_ORDER);
751// if (groupCursor != null) {
752// try {
753// StringBuilder sb = new StringBuilder();
754//
755// while (groupCursor.moveToNext()) {
756// String systemId = groupCursor.getString(
757// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
758//
759// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
760// continue;
761// }
762//
763// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
764// if (!TextUtils.isEmpty(name)) {
765// if (sb.length() == 0) {
766// sb.append(name);
767// } else {
768// sb.append(getString(R.string.group_list, name));
769// }
770// }
771// }
772//
773// if (sb.length() > 0) {
774// ViewEntry entry = new ViewEntry();
775// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
776// entry.label = getString(R.string.label_groups);
777// entry.data = sb.toString();
778// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
779//
780// // TODO: Add an icon for the groups item.
781//
782// mGroupEntries.add(entry);
783// }
784// } finally {
785// groupCursor.close();
786// }
787// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700788
Evan Millar66388be2009-05-28 15:41:07 -0700789 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700790
791 if (photoBitmap == null) {
792 photoBitmap = ContactsUtils.loadPlaceholderPhoto(mNoPhotoResource, this, null);
793 }
794 mPhotoView.setImageBitmap(photoBitmap);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800795 }
796 }
797
Evan Millar66388be2009-05-28 15:41:07 -0700798
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800799 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700800 // If there is no type just display an empty string
801 if (type == null) {
802 type = "";
803 }
804
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800805 if (lowerCase) {
806 return getString(actionResId, type.toString().toLowerCase());
807 } else {
808 return getString(actionResId, type.toString());
809 }
810 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700811
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800812 /**
813 * A basic structure with the data for a contact entry in the list.
814 */
815 final static class ViewEntry extends ContactEntryAdapter.Entry {
816 public int primaryIcon = -1;
817 public Intent intent;
818 public Intent auxIntent = null;
819 public int presenceIcon = -1;
820 public int actionIcon = -1;
821 public int maxLabelLines = 1;
822 }
823
824 private static final class ViewAdapter extends ContactEntryAdapter<ViewEntry> {
825 /** Cache of the children views of a row */
826 static class ViewCache {
827 public TextView label;
828 public TextView data;
829 public ImageView actionIcon;
830 public ImageView presenceIcon;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700831
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800832 // Need to keep track of this too
833 ViewEntry entry;
834 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700835
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800836 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
837 super(context, sections, SHOW_SEPARATORS);
838 }
839
840 @Override
841 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -0700842 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800843 View v;
844
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800845 ViewCache views;
846
847 // Check to see if we can reuse convertView
848 if (convertView != null) {
849 v = convertView;
850 views = (ViewCache) v.getTag();
851 } else {
852 // Create a new view if needed
853 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
854
855 // Cache the children
856 views = new ViewCache();
857 views.label = (TextView) v.findViewById(android.R.id.text1);
858 views.data = (TextView) v.findViewById(android.R.id.text2);
859 views.actionIcon = (ImageView) v.findViewById(R.id.icon1);
860 views.presenceIcon = (ImageView) v.findViewById(R.id.icon2);
861 v.setTag(views);
862 }
863
864 // Update the entry in the view cache
865 views.entry = entry;
866
867 // Bind the data to the view
868 bindView(v, entry);
869 return v;
870 }
871
872 @Override
873 protected View newView(int position, ViewGroup parent) {
874 // getView() handles this
875 throw new UnsupportedOperationException();
876 }
877
878 @Override
879 protected void bindView(View view, ViewEntry entry) {
880 final Resources resources = mContext.getResources();
881 ViewCache views = (ViewCache) view.getTag();
882
883 // Set the label
884 TextView label = views.label;
885 setMaxLines(label, entry.maxLabelLines);
886 label.setText(entry.label);
887
888 // Set the data
889 TextView data = views.data;
890 if (data != null) {
891 data.setText(entry.data);
892 setMaxLines(data, entry.maxLines);
893 }
894
895 // Set the action icon
896 ImageView action = views.actionIcon;
897 if (entry.actionIcon != -1) {
898 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
899 action.setVisibility(View.VISIBLE);
900 } else {
901 // Things should still line up as if there was an icon, so make it invisible
902 action.setVisibility(View.INVISIBLE);
903 }
904
905 // Set the presence icon
906 Drawable presenceIcon = null;
907 if (entry.primaryIcon != -1) {
908 presenceIcon = resources.getDrawable(entry.primaryIcon);
909 } else if (entry.presenceIcon != -1) {
910 presenceIcon = resources.getDrawable(entry.presenceIcon);
911 }
912
913 ImageView presence = views.presenceIcon;
914 if (presenceIcon != null) {
915 presence.setImageDrawable(presenceIcon);
916 presence.setVisibility(View.VISIBLE);
917 } else {
918 presence.setVisibility(View.GONE);
919 }
920 }
921
922 private void setMaxLines(TextView textView, int maxLines) {
923 if (maxLines == 1) {
924 textView.setSingleLine(true);
925 textView.setEllipsize(TextUtils.TruncateAt.END);
926 } else {
927 textView.setSingleLine(false);
928 textView.setMaxLines(maxLines);
929 textView.setEllipsize(null);
930 }
931 }
932 }
933}