blob: 451caa8e57fa02b83c44fb7bd718d5638e1cb0d6 [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_PROJECTION;
20import static com.android.contacts.ContactEntryAdapter.AGGREGATE_STARRED_COLUMN;
Evan Millar66388be2009-05-28 15:41:07 -070021import static com.android.contacts.ContactEntryAdapter.DATA_1_COLUMN;
22import static com.android.contacts.ContactEntryAdapter.DATA_2_COLUMN;
23import static com.android.contacts.ContactEntryAdapter.DATA_3_COLUMN;
24import static com.android.contacts.ContactEntryAdapter.DATA_4_COLUMN;
25import static com.android.contacts.ContactEntryAdapter.DATA_5_COLUMN;
Evan Millar66388be2009-05-28 15:41:07 -070026import static com.android.contacts.ContactEntryAdapter.DATA_9_COLUMN;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070027import static com.android.contacts.ContactEntryAdapter.DATA_CONTACT_ID_COLUMN;
28import static com.android.contacts.ContactEntryAdapter.DATA_ID_COLUMN;
29import static com.android.contacts.ContactEntryAdapter.DATA_IS_SUPER_PRIMARY_COLUMN;
30import static com.android.contacts.ContactEntryAdapter.DATA_MIMETYPE_COLUMN;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080031
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070032import com.android.contacts.SplitAggregateView.OnContactSelectedListener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080033import com.android.internal.telephony.ITelephony;
34
35import android.app.AlertDialog;
36import android.app.Dialog;
37import android.app.ListActivity;
38import android.content.ActivityNotFoundException;
39import android.content.ContentResolver;
40import android.content.ContentUris;
41import android.content.ContentValues;
42import android.content.Context;
43import android.content.DialogInterface;
44import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070045import android.content.DialogInterface.OnClickListener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080046import android.content.pm.PackageManager;
47import android.content.pm.ResolveInfo;
48import android.content.res.Resources;
49import android.database.ContentObserver;
50import android.database.Cursor;
Evan Millar45e0ed32009-06-01 16:44:38 -070051import android.graphics.Bitmap;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080052import android.graphics.drawable.Drawable;
53import android.media.Ringtone;
54import android.media.RingtoneManager;
55import android.net.Uri;
56import android.os.Bundle;
57import android.os.Handler;
58import android.os.RemoteException;
59import android.os.ServiceManager;
60import android.os.SystemClock;
Evan Millar66388be2009-05-28 15:41:07 -070061import android.provider.ContactsContract.Aggregates;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070062import android.provider.ContactsContract.AggregationExceptions;
63import android.provider.ContactsContract.CommonDataKinds;
Evan Millar66388be2009-05-28 15:41:07 -070064import android.provider.ContactsContract.Data;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080065import android.text.TextUtils;
66import android.util.Log;
67import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070068import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080069import android.view.KeyEvent;
70import android.view.Menu;
71import android.view.MenuItem;
72import android.view.View;
73import android.view.ViewGroup;
74import android.view.ContextMenu.ContextMenuInfo;
75import android.widget.AdapterView;
76import android.widget.CheckBox;
77import android.widget.ImageView;
78import android.widget.ListView;
79import android.widget.TextView;
80import android.widget.Toast;
81
82import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080083
84/**
85 * Displays the details of a specific contact.
86 */
Evan Millar5c22c3b2009-05-29 11:37:54 -070087public class ViewContactActivity extends ListActivity
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080088 implements View.OnCreateContextMenuListener, View.OnClickListener,
89 DialogInterface.OnClickListener {
90 private static final String TAG = "ViewContact";
91 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
92
93 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080094
95 private static final int DIALOG_CONFIRM_DELETE = 1;
96
97 public static final int MENU_ITEM_DELETE = 1;
98 public static final int MENU_ITEM_MAKE_DEFAULT = 2;
99 public static final int MENU_ITEM_SHOW_BARCODE = 3;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700100 public static final int MENU_ITEM_SPLIT_AGGREGATE = 4;
101
102 private static final String[] AGGREGATION_EXCEPTIONS_PROJECTION =
103 new String[] { AggregationExceptions._ID};
104
105 private static final int AGGREGATION_EXCEPTIONS_COL_ID = 0;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800106
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
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700113 /**
114 * A list of distinct contact IDs included in the current aggregate.
115 */
116 private ArrayList<Long> mContactIds = new ArrayList<Long>();
117
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800118 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
119 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
120 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
121 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
122 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
123 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700124 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800125 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
126 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
127
128 private Cursor mCursor;
129 private boolean mObserverRegistered;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700130
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800131 private ContentObserver mObserver = new ContentObserver(new Handler()) {
132 @Override
133 public boolean deliverSelfNotifications() {
134 return true;
135 }
136
137 @Override
138 public void onChange(boolean selfChange) {
139 if (mCursor != null && !mCursor.isClosed()){
140 dataChanged();
141 }
142 }
143 };
144
145 public void onClick(DialogInterface dialog, int which) {
146 if (mCursor != null) {
147 if (mObserverRegistered) {
148 mCursor.unregisterContentObserver(mObserver);
149 mObserverRegistered = false;
150 }
151 mCursor.close();
152 mCursor = null;
153 }
154 getContentResolver().delete(mUri, null, null);
155 finish();
156 }
157
158 public void onClick(View view) {
159 if (!mObserverRegistered) {
160 return;
161 }
162 switch (view.getId()) {
163 case R.id.star: {
Evan Millar7e4accf2009-06-08 10:43:26 -0700164 mCursor.moveToFirst();
Evan Millar66388be2009-05-28 15:41:07 -0700165 int oldStarredState = mCursor.getInt(AGGREGATE_STARRED_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800166 ContentValues values = new ContentValues(1);
Evan Millar66388be2009-05-28 15:41:07 -0700167 values.put(Aggregates.STARRED, oldStarredState == 1 ? 0 : 1);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800168 getContentResolver().update(mUri, values, null, null);
169 break;
170 }
171 }
172 }
173
174 private TextView mNameView;
175 private TextView mPhoneticNameView; // may be null in some locales
176 private ImageView mPhotoView;
177 private int mNoPhotoResource;
178 private CheckBox mStarView;
179 private boolean mShowSmsLinksForAllPhones;
180
181 @Override
182 protected void onCreate(Bundle icicle) {
183 super.onCreate(icicle);
184
185 setContentView(R.layout.view_contact);
186 getListView().setOnCreateContextMenuListener(this);
187
188 mNameView = (TextView) findViewById(R.id.name);
189 mPhoneticNameView = (TextView) findViewById(R.id.phonetic_name);
190 mPhotoView = (ImageView) findViewById(R.id.photo);
191 mStarView = (CheckBox) findViewById(R.id.star);
192 mStarView.setOnClickListener(this);
193
194 // Set the photo with a random "no contact" image
195 long now = SystemClock.elapsedRealtime();
196 int num = (int) now & 0xf;
197 if (num < 9) {
198 // Leaning in from right, common
199 mNoPhotoResource = R.drawable.ic_contact_picture;
200 } else if (num < 14) {
201 // Leaning in from left uncommon
202 mNoPhotoResource = R.drawable.ic_contact_picture_2;
203 } else {
204 // Coming in from the top, rare
205 mNoPhotoResource = R.drawable.ic_contact_picture_3;
206 }
207
Evan Millar45e0ed32009-06-01 16:44:38 -0700208 mUri = getIntent().getData();
209 mAggDataUri = Uri.withAppendedPath(mUri, "data");
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800210 mResolver = getContentResolver();
211
212 // Build the list of sections. The order they're added to mSections dictates the
213 // order they are displayed in the list.
214 mSections.add(mPhoneEntries);
215 mSections.add(mSmsEntries);
216 mSections.add(mEmailEntries);
217 mSections.add(mImEntries);
218 mSections.add(mPostalEntries);
219 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700220 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800221 mSections.add(mOtherEntries);
222
223 //TODO Read this value from a preference
224 mShowSmsLinksForAllPhones = true;
225
Evan Millar45e0ed32009-06-01 16:44:38 -0700226 mCursor = mResolver.query(mAggDataUri,
227 AGGREGATE_PROJECTION, null, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800228 }
229
230 @Override
231 protected void onResume() {
232 super.onResume();
233 mObserverRegistered = true;
234 mCursor.registerContentObserver(mObserver);
235 dataChanged();
236 }
237
238 @Override
239 protected void onPause() {
240 super.onPause();
241 if (mCursor != null) {
242 if (mObserverRegistered) {
243 mObserverRegistered = false;
244 mCursor.unregisterContentObserver(mObserver);
245 }
246 mCursor.deactivate();
247 }
248 }
249
250 @Override
251 protected void onDestroy() {
252 super.onDestroy();
253
254 if (mCursor != null) {
255 if (mObserverRegistered) {
256 mCursor.unregisterContentObserver(mObserver);
257 mObserverRegistered = false;
258 }
259 mCursor.close();
260 }
261 }
262
263 @Override
264 protected Dialog onCreateDialog(int id) {
265 switch (id) {
266 case DIALOG_CONFIRM_DELETE:
267 return new AlertDialog.Builder(this)
268 .setTitle(R.string.deleteConfirmation_title)
269 .setIcon(android.R.drawable.ic_dialog_alert)
270 .setMessage(R.string.deleteConfirmation)
271 .setNegativeButton(android.R.string.cancel, null)
272 .setPositiveButton(android.R.string.ok, this)
273 .setCancelable(false)
274 .create();
275 }
276 return null;
277 }
278
279 private void dataChanged() {
280 mCursor.requery();
281 if (mCursor.moveToFirst()) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800282 // Set the star
Evan Millar66388be2009-05-28 15:41:07 -0700283 mStarView.setChecked(mCursor.getInt(AGGREGATE_STARRED_COLUMN) == 1 ? true : false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800284
285 // Build up the contact entries
286 buildEntries(mCursor);
287 if (mAdapter == null) {
288 mAdapter = new ViewAdapter(this, mSections);
289 setListAdapter(mAdapter);
290 } else {
291 mAdapter.setSections(mSections, SHOW_SEPARATORS);
292 }
293 } else {
294 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
295 Log.e(TAG, "invalid contact uri: " + mUri);
296 finish();
297 }
298 }
299
300 @Override
301 public boolean onCreateOptionsMenu(Menu menu) {
302 menu.add(0, 0, 0, R.string.menu_editContact)
303 .setIcon(android.R.drawable.ic_menu_edit)
304 .setIntent(new Intent(Intent.ACTION_EDIT, mUri))
305 .setAlphabeticShortcut('e');
306 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
307 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700308 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
309 .setIcon(android.R.drawable.ic_menu_share);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800310 return true;
311 }
312
313 @Override
314 public boolean onPrepareOptionsMenu(Menu menu) {
315 super.onPrepareOptionsMenu(menu);
316 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
317 // could be installed or uninstalled at any time.
318 if (isBarcodeScannerInstalled()) {
319 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
320 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
321 .setIcon(R.drawable.ic_menu_show_barcode);
322 }
323 } else {
324 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
325 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700326
327 boolean isAggregate = mContactIds.size() > 1;
328 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800329 return true;
330 }
331
332 private boolean isBarcodeScannerInstalled() {
333 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
334 ResolveInfo ri = getPackageManager().resolveActivity(intent,
335 PackageManager.MATCH_DEFAULT_ONLY);
336 return ri != null;
337 }
338
339 @Override
340 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
341 AdapterView.AdapterContextMenuInfo info;
342 try {
343 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
344 } catch (ClassCastException e) {
345 Log.e(TAG, "bad menuInfo", e);
346 return;
347 }
348
349 // This can be null sometimes, don't crash...
350 if (info == null) {
351 Log.e(TAG, "bad menuInfo");
352 return;
353 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700354
Evan Millar45e0ed32009-06-01 16:44:38 -0700355 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
356 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
357 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
358 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.auxIntent);
359 if (entry.primaryIcon == -1) {
360 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800361 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700362 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
363 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
364 if (entry.primaryIcon == -1) {
365 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800366 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700367 } else if (entry.mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)) {
368 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
369 }
370 // TODO(emillar): add back with group support.
371 /* else if (entry.mimetype.equals()) {
372 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
373 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800374 }
375
376 @Override
377 public boolean onOptionsItemSelected(MenuItem item) {
378 switch (item.getItemId()) {
379 case MENU_ITEM_DELETE: {
380 // Get confirmation
381 showDialog(DIALOG_CONFIRM_DELETE);
382 return true;
383 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700384
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700385 case MENU_ITEM_SPLIT_AGGREGATE: {
386 showSplitAggregateDialog();
387 return true;
388 }
389
Evan Millar66388be2009-05-28 15:41:07 -0700390 // TODO(emillar) Bring this back.
391 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800392 if (mCursor.moveToFirst()) {
393 Intent intent = new Intent(SHOW_BARCODE_INTENT);
394 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
395 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700396 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800397 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700398 // Correctly handle when section headers are hidden
399 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700400
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800401 bundle.putString(Contacts.Intents.Insert.NAME, name);
402 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700403 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800404 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700405 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800406 bundle.putString(PHONE_KEYS[x], entry.data);
407 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700408 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800409 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700410 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800411 bundle.putString(EMAIL_KEYS[x], entry.data);
412 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700413 if (mPostalEntries.size() >= 1 + sepAdjust) {
414 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800415 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
416 }
417 intent.putExtra("ENCODE_DATA", bundle);
418 try {
419 startActivity(intent);
420 } catch (ActivityNotFoundException e) {
421 // The check in onPrepareOptionsMenu() should make this impossible, but
422 // for safety I'm catching the exception rather than crashing. Ideally
423 // I'd call Menu.removeItem() here too, but I don't see a way to get
424 // the options menu.
425 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
426 "was not installed.");
427 }
428 return true;
429 }
430 }
Evan Millar66388be2009-05-28 15:41:07 -0700431 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800432 }
433 return super.onOptionsItemSelected(item);
434 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700435
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800436 @Override
437 public boolean onContextItemSelected(MenuItem item) {
438 switch (item.getItemId()) {
439 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700440 if (makeItemDefault(item)) {
441 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800442 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700443 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800444 }
445 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700446
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800447 return super.onContextItemSelected(item);
448 }
449
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700450 private boolean makeItemDefault(MenuItem item) {
451 ViewEntry entry = getViewEntryForMenuItem(item);
452 if (entry == null) {
453 return false;
454 }
455
456 // Update the primary values in the data record.
457 ContentValues values = new ContentValues(2);
458 values.put(Data.IS_PRIMARY, 1);
459 values.put(Data.IS_SUPER_PRIMARY, 1);
460
461 Log.i(TAG, mUri.toString());
462 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
463 values, null, null);
464 dataChanged();
465 return true;
466 }
467
468 /**
469 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
470 * The user picks a contact to be split into its own aggregate or clicks Cancel.
471 */
472 private void showSplitAggregateDialog() {
473
474 // Wrap this dialog in a specific theme so that list items have correct text color.
475 final ContextThemeWrapper dialogContext =
476 new ContextThemeWrapper(this, android.R.style.Theme_Light);
477 AlertDialog.Builder builder =
478 new AlertDialog.Builder(dialogContext);
479 builder.setTitle(getString(R.string.splitAggregate_title));
480
481 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
482 builder.setView(view);
483
484 builder.setInverseBackgroundForced(true);
485 builder.setCancelable(true);
486 builder.setNegativeButton(android.R.string.cancel,
487 new OnClickListener() {
488 public void onClick(DialogInterface dialog, int which) {
489 dialog.dismiss();
490 }
491 });
492 final AlertDialog dialog = builder.create();
493
494 view.setOnContactSelectedListener(new OnContactSelectedListener() {
495 public void onContactSelected(long contactId) {
496 dialog.dismiss();
497 splitContact(contactId);
498 }
499 });
500
501 dialog.show();
502 }
503
504 /**
505 * Given an ID of a constituent contact, splits it off into a separate aggregate.
506 */
507 protected void splitContact(long contactToSplit) {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700508
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700509 ContentValues values = new ContentValues(3);
510 values.put(AggregationExceptions.AGGREGATE_ID, ContentUris.parseId(mUri));
511 values.put(AggregationExceptions.CONTACT_ID, contactToSplit);
512 values.put(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_OUT);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700513
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700514 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700515
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700516 mAdapter.notifyDataSetChanged();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700517 }
518
519 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
520 AdapterView.AdapterContextMenuInfo info;
521 try {
522 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
523 } catch (ClassCastException e) {
524 Log.e(TAG, "bad menuInfo", e);
525 return null;
526 }
527
528 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
529 }
530
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800531 @Override
532 public boolean onKeyDown(int keyCode, KeyEvent event) {
533 switch (keyCode) {
534 case KeyEvent.KEYCODE_CALL: {
535 try {
536 ITelephony phone = ITelephony.Stub.asInterface(
537 ServiceManager.checkService("phone"));
538 if (phone != null && !phone.isIdle()) {
539 // Skip out and let the key be handled at a higher level
540 break;
541 }
542 } catch (RemoteException re) {
543 // Fall through and try to call the contact
544 }
545
546 int index = getListView().getSelectedItemPosition();
547 if (index != -1) {
548 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700549 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
550 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800551 }
552 } else if (mNumPhoneNumbers != 0) {
553 // There isn't anything selected, call the default number
554 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
555 startActivity(intent);
556 }
557 return true;
558 }
559
560 case KeyEvent.KEYCODE_DEL: {
561 showDialog(DIALOG_CONFIRM_DELETE);
562 return true;
563 }
564 }
565
566 return super.onKeyDown(keyCode, event);
567 }
568
569 @Override
570 protected void onListItemClick(ListView l, View v, int position, long id) {
571 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
572 if (entry != null) {
573 Intent intent = entry.intent;
574 if (intent != null) {
575 try {
576 startActivity(intent);
577 } catch (ActivityNotFoundException e) {
578 Log.e(TAG, "No activity found for intent: " + intent);
579 signalError();
580 }
581 } else {
582 signalError();
583 }
584 } else {
585 signalError();
586 }
587 }
588
589 /**
590 * Signal an error to the user via a beep, or some other method.
591 */
592 private void signalError() {
593 //TODO: implement this when we have the sonification APIs
594 }
595
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800596 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700597 // 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 -0800598 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.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700607 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800608 * @param personCursor the URI for the contact being displayed
609 */
Evan Millar66388be2009-05-28 15:41:07 -0700610 private final void buildEntries(Cursor aggCursor) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800611 // 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
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700617 mContactIds.clear();
618
Evan Millar66388be2009-05-28 15:41:07 -0700619 // Build up method entries
620 if (mUri != null) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700621 Bitmap photoBitmap = null;
Evan Millar66388be2009-05-28 15:41:07 -0700622 while (aggCursor.moveToNext()) {
623 final String mimetype = aggCursor.getString(DATA_MIMETYPE_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700624
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800625 ViewEntry entry = new ViewEntry();
Evan Millar66388be2009-05-28 15:41:07 -0700626
627 final long id = aggCursor.getLong(DATA_ID_COLUMN);
628 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800629 entry.id = id;
630 entry.uri = uri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700631 entry.mimetype = mimetype;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700632 entry.contactId = aggCursor.getLong(DATA_CONTACT_ID_COLUMN);
633 if (!mContactIds.contains(entry.contactId)) {
634 mContactIds.add(entry.contactId);
635 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800636
Evan Millar66388be2009-05-28 15:41:07 -0700637 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
Evan Millar5c22c3b2009-05-29 11:37:54 -0700638 || mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)
639 || mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)
Evan Millar66388be2009-05-28 15:41:07 -0700640 || mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
641 final int type = aggCursor.getInt(DATA_1_COLUMN);
Evan Millar45e0ed32009-06-01 16:44:38 -0700642 final String label = aggCursor.getString(DATA_3_COLUMN);
643 final String data = aggCursor.getString(DATA_2_COLUMN);
644 final boolean isSuperPrimary = "1".equals(
645 aggCursor.getString(DATA_IS_SUPER_PRIMARY_COLUMN));
Evan Millar66388be2009-05-28 15:41:07 -0700646
647 // Don't crash if the data is bogus
648 if (TextUtils.isEmpty(data)) {
649 Log.w(TAG, "empty data for contact method " + id);
650 continue;
651 }
652
653 // Build phone entries
654 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
655 mNumPhoneNumbers++;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700656
Evan Millar66388be2009-05-28 15:41:07 -0700657 final CharSequence displayLabel = ContactsUtils.getDisplayLabel(
658 this, mimetype, type, label);
659 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
660 entry.data = data;
661 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, entry.uri);
662 entry.auxIntent = new Intent(Intent.ACTION_SENDTO,
663 Uri.fromParts("sms", data, null));
Evan Millar45e0ed32009-06-01 16:44:38 -0700664 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700665 entry.primaryIcon = R.drawable.ic_default_number;
666 }
667 entry.actionIcon = android.R.drawable.sym_action_call;
668 mPhoneEntries.add(entry);
669
Evan Millar5c22c3b2009-05-29 11:37:54 -0700670 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700671 || mShowSmsLinksForAllPhones) {
672 // Add an SMS entry
673 ViewEntry smsEntry = new ViewEntry();
674 smsEntry.label = buildActionString(
675 R.string.actionText, displayLabel, true);
676 smsEntry.data = data;
677 smsEntry.id = id;
678 smsEntry.uri = uri;
679 smsEntry.intent = entry.auxIntent;
Evan Millar45e0ed32009-06-01 16:44:38 -0700680 smsEntry.mimetype = FastTrackWindow.MIME_SMS_ADDRESS;
Evan Millar66388be2009-05-28 15:41:07 -0700681 smsEntry.actionIcon = R.drawable.sym_action_sms;
682 mSmsEntries.add(smsEntry);
683 }
684 // Build email entries
685 } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800686 entry.label = buildActionString(R.string.actionEmail,
Evan Millar66388be2009-05-28 15:41:07 -0700687 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800688 entry.data = data;
689 entry.intent = new Intent(Intent.ACTION_SENDTO,
690 Uri.fromParts("mailto", data, null));
691 entry.actionIcon = android.R.drawable.sym_action_email;
Evan Millar45e0ed32009-06-01 16:44:38 -0700692 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700693 entry.primaryIcon = R.drawable.ic_default_number;
694 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800695 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700696 // Build postal entries
697 } else if (mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800698 entry.label = buildActionString(R.string.actionMap,
Evan Millar66388be2009-05-28 15:41:07 -0700699 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800700 entry.data = data;
701 entry.maxLines = 4;
702 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
703 entry.actionIcon = R.drawable.sym_action_map;
704 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700705 // Build im entries
706 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
707 String[] protocolStrings = getResources().getStringArray(
708 android.R.array.imProtocols);
709 Object protocolObj = ContactsUtils.decodeImProtocol(
710 aggCursor.getString(DATA_5_COLUMN));
Alex Kennberg87fc3172009-03-28 06:43:06 -0700711 String host = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800712 if (protocolObj instanceof Number) {
713 int protocol = ((Number) protocolObj).intValue();
714 entry.label = buildActionString(R.string.actionChat,
715 protocolStrings[protocol], false);
Evan Millar66388be2009-05-28 15:41:07 -0700716 host = ContactsUtils.lookupProviderNameFromId(
717 protocol).toLowerCase();
718 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
719 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800720 entry.maxLabelLines = 2;
721 }
Alex Kennberg87fc3172009-03-28 06:43:06 -0700722 } else if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800723 String providerName = (String) protocolObj;
724 entry.label = buildActionString(R.string.actionChat,
725 providerName, false);
726 host = providerName.toLowerCase();
727 }
728
729 // Only add the intent if there is a valid host
730 if (!TextUtils.isEmpty(host)) {
731 entry.intent = new Intent(Intent.ACTION_SENDTO,
732 constructImToUrl(host, data));
733 }
734 entry.data = data;
Evan Millar66388be2009-05-28 15:41:07 -0700735 //TODO(emillar) Do we want presence info?
736 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800737 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700738 aggCursor.getInt(METHODS_STATUS_COLUMN));
739 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800740 entry.actionIcon = android.R.drawable.sym_action_chat;
741 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700742 }
Evan Millar7e4accf2009-06-08 10:43:26 -0700743 // Set the name
744 } else if (mimetype.equals(CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
745 String name = mCursor.getString(DATA_9_COLUMN);
746 if (TextUtils.isEmpty(name)) {
747 mNameView.setText(getText(android.R.string.unknownName));
748 } else {
749 mNameView.setText(name);
750 }
751 /*
752 if (mPhoneticNameView != null) {
753 String phoneticName = mCursor.getString(CONTACT_PHONETIC_NAME_COLUMN);
754 mPhoneticNameView.setText(phoneticName);
755 }
756 */
Evan Millar66388be2009-05-28 15:41:07 -0700757 // Build organization entries
758 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
Evan Millar66388be2009-05-28 15:41:07 -0700759 final String company = aggCursor.getString(DATA_3_COLUMN);
760 final String title = aggCursor.getString(DATA_4_COLUMN);
761 final boolean isPrimary = "1".equals(aggCursor.getString(DATA_5_COLUMN));
762
763 if (isPrimary) {
764 entry.primaryIcon = R.drawable.ic_default_number;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800765 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700766
Evan Millar66388be2009-05-28 15:41:07 -0700767 // Don't crash if the data is bogus
768 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
769 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800770 continue;
771 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700772
Evan Millar66388be2009-05-28 15:41:07 -0700773 entry.data = title;
774 entry.actionIcon = R.drawable.sym_action_organization;
775 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800776
Evan Millar66388be2009-05-28 15:41:07 -0700777 mOrganizationEntries.add(entry);
778 // Build note entries
779 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
780 entry.label = getString(R.string.label_notes);
781 entry.data = aggCursor.getString(DATA_1_COLUMN);
782 entry.id = 0;
783 entry.uri = null;
784 entry.intent = null;
785 entry.maxLines = 10;
786 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800787
Evan Millar66388be2009-05-28 15:41:07 -0700788 if (TextUtils.isEmpty(entry.data)) {
789 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700790 continue;
791 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700792
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800793 mOtherEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700794 // Build the ringtone and send to voicemail entries
795 } else if (mimetype.equals(CommonDataKinds.CustomRingtone.CONTENT_ITEM_TYPE)) {
796 final Boolean sendToVoicemail = "1".equals(aggCursor.getString(DATA_1_COLUMN));
797 final String ringtoneStr = aggCursor.getString(DATA_2_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800798
Evan Millar5c22c3b2009-05-29 11:37:54 -0700799 // TODO(emillar) we need to enforce uniqueness of custom ringtone entries on a
Evan Millar66388be2009-05-28 15:41:07 -0700800 // single aggregate. This could be done by checking against the reference ID stored
801 // in the aggregate.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700802
Evan Millar66388be2009-05-28 15:41:07 -0700803 // Build the send directly to voice mail entry
804 if (sendToVoicemail) {
805 entry.label = getString(R.string.actionIncomingCall);
806 entry.data = getString(R.string.detailIncomingCallsGoToVoicemail);
807 entry.actionIcon = R.drawable.sym_send_to_voicemail;
808 mOtherEntries.add(entry);
809 } else if (!TextUtils.isEmpty(ringtoneStr)) {
810 // Get the URI
811 Uri ringtoneUri = Uri.parse(ringtoneStr);
812 if (ringtoneUri != null) {
813 Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
814 if (ringtone != null) {
815 entry.label = getString(R.string.label_ringtone);
816 entry.data = ringtone.getTitle(this);
817 entry.uri = ringtoneUri;
818 entry.actionIcon = R.drawable.sym_ringtone;
819 mOtherEntries.add(entry);
820 }
821 }
822 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700823 // Load the photo
824 } else if (mimetype.equals(CommonDataKinds.Photo.CONTENT_ITEM_TYPE)) {
825 photoBitmap = ContactsUtils.loadContactPhoto(aggCursor, DATA_1_COLUMN, null);
Evan Millar66388be2009-05-28 15:41:07 -0700826 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700827
Evan Millar45e0ed32009-06-01 16:44:38 -0700828
Evan Millar66388be2009-05-28 15:41:07 -0700829 // TODO(emillar) Add group entries
830// // Build the group entries
831// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
832// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
833// null, null, Groups.DEFAULT_SORT_ORDER);
834// if (groupCursor != null) {
835// try {
836// StringBuilder sb = new StringBuilder();
837//
838// while (groupCursor.moveToNext()) {
839// String systemId = groupCursor.getString(
840// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
841//
842// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
843// continue;
844// }
845//
846// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
847// if (!TextUtils.isEmpty(name)) {
848// if (sb.length() == 0) {
849// sb.append(name);
850// } else {
851// sb.append(getString(R.string.group_list, name));
852// }
853// }
854// }
855//
856// if (sb.length() > 0) {
857// ViewEntry entry = new ViewEntry();
858// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
859// entry.label = getString(R.string.label_groups);
860// entry.data = sb.toString();
861// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
862//
863// // TODO: Add an icon for the groups item.
864//
865// mGroupEntries.add(entry);
866// }
867// } finally {
868// groupCursor.close();
869// }
870// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700871
Evan Millar66388be2009-05-28 15:41:07 -0700872 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700873
874 if (photoBitmap == null) {
875 photoBitmap = ContactsUtils.loadPlaceholderPhoto(mNoPhotoResource, this, null);
876 }
877 mPhotoView.setImageBitmap(photoBitmap);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800878 }
879 }
880
Evan Millar66388be2009-05-28 15:41:07 -0700881
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800882 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700883 // If there is no type just display an empty string
884 if (type == null) {
885 type = "";
886 }
887
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800888 if (lowerCase) {
889 return getString(actionResId, type.toString().toLowerCase());
890 } else {
891 return getString(actionResId, type.toString());
892 }
893 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700894
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800895 /**
896 * A basic structure with the data for a contact entry in the list.
897 */
898 final static class ViewEntry extends ContactEntryAdapter.Entry {
899 public int primaryIcon = -1;
900 public Intent intent;
901 public Intent auxIntent = null;
902 public int presenceIcon = -1;
903 public int actionIcon = -1;
904 public int maxLabelLines = 1;
905 }
906
907 private static final class ViewAdapter extends ContactEntryAdapter<ViewEntry> {
908 /** Cache of the children views of a row */
909 static class ViewCache {
910 public TextView label;
911 public TextView data;
912 public ImageView actionIcon;
913 public ImageView presenceIcon;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700914
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800915 // Need to keep track of this too
916 ViewEntry entry;
917 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700918
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800919 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
920 super(context, sections, SHOW_SEPARATORS);
921 }
922
923 @Override
924 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -0700925 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800926 View v;
927
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800928 ViewCache views;
929
930 // Check to see if we can reuse convertView
931 if (convertView != null) {
932 v = convertView;
933 views = (ViewCache) v.getTag();
934 } else {
935 // Create a new view if needed
936 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
937
938 // Cache the children
939 views = new ViewCache();
940 views.label = (TextView) v.findViewById(android.R.id.text1);
941 views.data = (TextView) v.findViewById(android.R.id.text2);
942 views.actionIcon = (ImageView) v.findViewById(R.id.icon1);
943 views.presenceIcon = (ImageView) v.findViewById(R.id.icon2);
944 v.setTag(views);
945 }
946
947 // Update the entry in the view cache
948 views.entry = entry;
949
950 // Bind the data to the view
951 bindView(v, entry);
952 return v;
953 }
954
955 @Override
956 protected View newView(int position, ViewGroup parent) {
957 // getView() handles this
958 throw new UnsupportedOperationException();
959 }
960
961 @Override
962 protected void bindView(View view, ViewEntry entry) {
963 final Resources resources = mContext.getResources();
964 ViewCache views = (ViewCache) view.getTag();
965
966 // Set the label
967 TextView label = views.label;
968 setMaxLines(label, entry.maxLabelLines);
969 label.setText(entry.label);
970
971 // Set the data
972 TextView data = views.data;
973 if (data != null) {
974 data.setText(entry.data);
975 setMaxLines(data, entry.maxLines);
976 }
977
978 // Set the action icon
979 ImageView action = views.actionIcon;
980 if (entry.actionIcon != -1) {
981 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
982 action.setVisibility(View.VISIBLE);
983 } else {
984 // Things should still line up as if there was an icon, so make it invisible
985 action.setVisibility(View.INVISIBLE);
986 }
987
988 // Set the presence icon
989 Drawable presenceIcon = null;
990 if (entry.primaryIcon != -1) {
991 presenceIcon = resources.getDrawable(entry.primaryIcon);
992 } else if (entry.presenceIcon != -1) {
993 presenceIcon = resources.getDrawable(entry.presenceIcon);
994 }
995
996 ImageView presence = views.presenceIcon;
997 if (presenceIcon != null) {
998 presence.setImageDrawable(presenceIcon);
999 presence.setVisibility(View.VISIBLE);
1000 } else {
1001 presence.setVisibility(View.GONE);
1002 }
1003 }
1004
1005 private void setMaxLines(TextView textView, int maxLines) {
1006 if (maxLines == 1) {
1007 textView.setSingleLine(true);
1008 textView.setEllipsize(TextUtils.TruncateAt.END);
1009 } else {
1010 textView.setSingleLine(false);
1011 textView.setMaxLines(maxLines);
1012 textView.setEllipsize(null);
1013 }
1014 }
1015 }
1016}