blob: 3b80da5bdc5a56987fc14d46cf795165b6e31c9f [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) {
508 long contactToKeep;
509
510 // We are guaranteed to have at least two items in the mContactIds array, because
511 // otherwise the "Split" menu item would be disabled.
512 if (mContactIds.get(0) != contactToSplit) {
513 contactToKeep = mContactIds.get(0);
514 } else {
515 contactToKeep = mContactIds.get(1);
516 }
517
518 ContentValues values = new ContentValues(3);
519 values.put(AggregationExceptions.TYPE, AggregationExceptions.TYPE_NEVER_MATCH);
520
521 boolean updated = false;
522
523 // First see if there is an existing exception for this pair of contacts
524 Cursor c = mResolver.query(AggregationExceptions.CONTENT_URI,
525 AGGREGATION_EXCEPTIONS_PROJECTION,
526 "(" + AggregationExceptions.CONTACT_ID1 + "=" + contactToSplit
527 + " AND " + AggregationExceptions.CONTACT_ID2 + "=" + contactToKeep
528 + ") OR (" + AggregationExceptions.CONTACT_ID1 + "=" + contactToKeep
529 + " AND " + AggregationExceptions.CONTACT_ID2 + "=" + contactToSplit + ")",
530 null, null);
531
532 try {
533 if (c.moveToFirst()) {
534 long exceptionId = c.getLong(AGGREGATION_EXCEPTIONS_COL_ID);
535 mResolver.update(ContentUris.withAppendedId(AggregationExceptions.CONTENT_URI,
536 exceptionId), values, null, null);
537 updated = true;
538 }
539 } finally {
540 c.close();
541 }
542
543 // Otherwise, insert a new exception
544 if (!updated) {
545 values.put(AggregationExceptions.CONTACT_ID1, contactToSplit);
546 values.put(AggregationExceptions.CONTACT_ID2, contactToKeep);
547 mResolver.insert(AggregationExceptions.CONTENT_URI, values);
548 }
549
550 mAdapter.notifyDataSetChanged();
551 }
552
553 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
554 AdapterView.AdapterContextMenuInfo info;
555 try {
556 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
557 } catch (ClassCastException e) {
558 Log.e(TAG, "bad menuInfo", e);
559 return null;
560 }
561
562 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
563 }
564
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800565 @Override
566 public boolean onKeyDown(int keyCode, KeyEvent event) {
567 switch (keyCode) {
568 case KeyEvent.KEYCODE_CALL: {
569 try {
570 ITelephony phone = ITelephony.Stub.asInterface(
571 ServiceManager.checkService("phone"));
572 if (phone != null && !phone.isIdle()) {
573 // Skip out and let the key be handled at a higher level
574 break;
575 }
576 } catch (RemoteException re) {
577 // Fall through and try to call the contact
578 }
579
580 int index = getListView().getSelectedItemPosition();
581 if (index != -1) {
582 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700583 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
584 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800585 }
586 } else if (mNumPhoneNumbers != 0) {
587 // There isn't anything selected, call the default number
588 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
589 startActivity(intent);
590 }
591 return true;
592 }
593
594 case KeyEvent.KEYCODE_DEL: {
595 showDialog(DIALOG_CONFIRM_DELETE);
596 return true;
597 }
598 }
599
600 return super.onKeyDown(keyCode, event);
601 }
602
603 @Override
604 protected void onListItemClick(ListView l, View v, int position, long id) {
605 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
606 if (entry != null) {
607 Intent intent = entry.intent;
608 if (intent != null) {
609 try {
610 startActivity(intent);
611 } catch (ActivityNotFoundException e) {
612 Log.e(TAG, "No activity found for intent: " + intent);
613 signalError();
614 }
615 } else {
616 signalError();
617 }
618 } else {
619 signalError();
620 }
621 }
622
623 /**
624 * Signal an error to the user via a beep, or some other method.
625 */
626 private void signalError() {
627 //TODO: implement this when we have the sonification APIs
628 }
629
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800630 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700631 // 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 -0800632 StringBuilder buf = new StringBuilder("imto://");
633 buf.append(host);
634 buf.append('/');
635 buf.append(data);
636 return Uri.parse(buf.toString());
637 }
638
639 /**
640 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700641 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800642 * @param personCursor the URI for the contact being displayed
643 */
Evan Millar66388be2009-05-28 15:41:07 -0700644 private final void buildEntries(Cursor aggCursor) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800645 // Clear out the old entries
646 final int numSections = mSections.size();
647 for (int i = 0; i < numSections; i++) {
648 mSections.get(i).clear();
649 }
650
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700651 mContactIds.clear();
652
Evan Millar66388be2009-05-28 15:41:07 -0700653 // Build up method entries
654 if (mUri != null) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700655 Bitmap photoBitmap = null;
Evan Millar66388be2009-05-28 15:41:07 -0700656 while (aggCursor.moveToNext()) {
657 final String mimetype = aggCursor.getString(DATA_MIMETYPE_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700658
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800659 ViewEntry entry = new ViewEntry();
Evan Millar66388be2009-05-28 15:41:07 -0700660
661 final long id = aggCursor.getLong(DATA_ID_COLUMN);
662 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800663 entry.id = id;
664 entry.uri = uri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700665 entry.mimetype = mimetype;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700666 entry.contactId = aggCursor.getLong(DATA_CONTACT_ID_COLUMN);
667 if (!mContactIds.contains(entry.contactId)) {
668 mContactIds.add(entry.contactId);
669 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800670
Evan Millar66388be2009-05-28 15:41:07 -0700671 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
Evan Millar5c22c3b2009-05-29 11:37:54 -0700672 || mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)
673 || mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)
Evan Millar66388be2009-05-28 15:41:07 -0700674 || mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
675 final int type = aggCursor.getInt(DATA_1_COLUMN);
Evan Millar45e0ed32009-06-01 16:44:38 -0700676 final String label = aggCursor.getString(DATA_3_COLUMN);
677 final String data = aggCursor.getString(DATA_2_COLUMN);
678 final boolean isSuperPrimary = "1".equals(
679 aggCursor.getString(DATA_IS_SUPER_PRIMARY_COLUMN));
Evan Millar66388be2009-05-28 15:41:07 -0700680
681 // Don't crash if the data is bogus
682 if (TextUtils.isEmpty(data)) {
683 Log.w(TAG, "empty data for contact method " + id);
684 continue;
685 }
686
687 // Build phone entries
688 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
689 mNumPhoneNumbers++;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700690
Evan Millar66388be2009-05-28 15:41:07 -0700691 final CharSequence displayLabel = ContactsUtils.getDisplayLabel(
692 this, mimetype, type, label);
693 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
694 entry.data = data;
695 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, entry.uri);
696 entry.auxIntent = new Intent(Intent.ACTION_SENDTO,
697 Uri.fromParts("sms", data, null));
Evan Millar45e0ed32009-06-01 16:44:38 -0700698 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700699 entry.primaryIcon = R.drawable.ic_default_number;
700 }
701 entry.actionIcon = android.R.drawable.sym_action_call;
702 mPhoneEntries.add(entry);
703
Evan Millar5c22c3b2009-05-29 11:37:54 -0700704 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700705 || mShowSmsLinksForAllPhones) {
706 // Add an SMS entry
707 ViewEntry smsEntry = new ViewEntry();
708 smsEntry.label = buildActionString(
709 R.string.actionText, displayLabel, true);
710 smsEntry.data = data;
711 smsEntry.id = id;
712 smsEntry.uri = uri;
713 smsEntry.intent = entry.auxIntent;
Evan Millar45e0ed32009-06-01 16:44:38 -0700714 smsEntry.mimetype = FastTrackWindow.MIME_SMS_ADDRESS;
Evan Millar66388be2009-05-28 15:41:07 -0700715 smsEntry.actionIcon = R.drawable.sym_action_sms;
716 mSmsEntries.add(smsEntry);
717 }
718 // Build email entries
719 } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800720 entry.label = buildActionString(R.string.actionEmail,
Evan Millar66388be2009-05-28 15:41:07 -0700721 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800722 entry.data = data;
723 entry.intent = new Intent(Intent.ACTION_SENDTO,
724 Uri.fromParts("mailto", data, null));
725 entry.actionIcon = android.R.drawable.sym_action_email;
Evan Millar45e0ed32009-06-01 16:44:38 -0700726 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700727 entry.primaryIcon = R.drawable.ic_default_number;
728 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800729 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700730 // Build postal entries
731 } else if (mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800732 entry.label = buildActionString(R.string.actionMap,
Evan Millar66388be2009-05-28 15:41:07 -0700733 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800734 entry.data = data;
735 entry.maxLines = 4;
736 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
737 entry.actionIcon = R.drawable.sym_action_map;
738 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700739 // Build im entries
740 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
741 String[] protocolStrings = getResources().getStringArray(
742 android.R.array.imProtocols);
743 Object protocolObj = ContactsUtils.decodeImProtocol(
744 aggCursor.getString(DATA_5_COLUMN));
Alex Kennberg87fc3172009-03-28 06:43:06 -0700745 String host = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800746 if (protocolObj instanceof Number) {
747 int protocol = ((Number) protocolObj).intValue();
748 entry.label = buildActionString(R.string.actionChat,
749 protocolStrings[protocol], false);
Evan Millar66388be2009-05-28 15:41:07 -0700750 host = ContactsUtils.lookupProviderNameFromId(
751 protocol).toLowerCase();
752 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
753 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800754 entry.maxLabelLines = 2;
755 }
Alex Kennberg87fc3172009-03-28 06:43:06 -0700756 } else if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800757 String providerName = (String) protocolObj;
758 entry.label = buildActionString(R.string.actionChat,
759 providerName, false);
760 host = providerName.toLowerCase();
761 }
762
763 // Only add the intent if there is a valid host
764 if (!TextUtils.isEmpty(host)) {
765 entry.intent = new Intent(Intent.ACTION_SENDTO,
766 constructImToUrl(host, data));
767 }
768 entry.data = data;
Evan Millar66388be2009-05-28 15:41:07 -0700769 //TODO(emillar) Do we want presence info?
770 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800771 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700772 aggCursor.getInt(METHODS_STATUS_COLUMN));
773 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800774 entry.actionIcon = android.R.drawable.sym_action_chat;
775 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700776 }
Evan Millar7e4accf2009-06-08 10:43:26 -0700777 // Set the name
778 } else if (mimetype.equals(CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
779 String name = mCursor.getString(DATA_9_COLUMN);
780 if (TextUtils.isEmpty(name)) {
781 mNameView.setText(getText(android.R.string.unknownName));
782 } else {
783 mNameView.setText(name);
784 }
785 /*
786 if (mPhoneticNameView != null) {
787 String phoneticName = mCursor.getString(CONTACT_PHONETIC_NAME_COLUMN);
788 mPhoneticNameView.setText(phoneticName);
789 }
790 */
Evan Millar66388be2009-05-28 15:41:07 -0700791 // Build organization entries
792 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
Evan Millar66388be2009-05-28 15:41:07 -0700793 final String company = aggCursor.getString(DATA_3_COLUMN);
794 final String title = aggCursor.getString(DATA_4_COLUMN);
795 final boolean isPrimary = "1".equals(aggCursor.getString(DATA_5_COLUMN));
796
797 if (isPrimary) {
798 entry.primaryIcon = R.drawable.ic_default_number;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800799 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700800
Evan Millar66388be2009-05-28 15:41:07 -0700801 // Don't crash if the data is bogus
802 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
803 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800804 continue;
805 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700806
Evan Millar66388be2009-05-28 15:41:07 -0700807 entry.data = title;
808 entry.actionIcon = R.drawable.sym_action_organization;
809 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800810
Evan Millar66388be2009-05-28 15:41:07 -0700811 mOrganizationEntries.add(entry);
812 // Build note entries
813 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
814 entry.label = getString(R.string.label_notes);
815 entry.data = aggCursor.getString(DATA_1_COLUMN);
816 entry.id = 0;
817 entry.uri = null;
818 entry.intent = null;
819 entry.maxLines = 10;
820 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800821
Evan Millar66388be2009-05-28 15:41:07 -0700822 if (TextUtils.isEmpty(entry.data)) {
823 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700824 continue;
825 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700826
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800827 mOtherEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700828 // Build the ringtone and send to voicemail entries
829 } else if (mimetype.equals(CommonDataKinds.CustomRingtone.CONTENT_ITEM_TYPE)) {
830 final Boolean sendToVoicemail = "1".equals(aggCursor.getString(DATA_1_COLUMN));
831 final String ringtoneStr = aggCursor.getString(DATA_2_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800832
Evan Millar5c22c3b2009-05-29 11:37:54 -0700833 // TODO(emillar) we need to enforce uniqueness of custom ringtone entries on a
Evan Millar66388be2009-05-28 15:41:07 -0700834 // single aggregate. This could be done by checking against the reference ID stored
835 // in the aggregate.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700836
Evan Millar66388be2009-05-28 15:41:07 -0700837 // Build the send directly to voice mail entry
838 if (sendToVoicemail) {
839 entry.label = getString(R.string.actionIncomingCall);
840 entry.data = getString(R.string.detailIncomingCallsGoToVoicemail);
841 entry.actionIcon = R.drawable.sym_send_to_voicemail;
842 mOtherEntries.add(entry);
843 } else if (!TextUtils.isEmpty(ringtoneStr)) {
844 // Get the URI
845 Uri ringtoneUri = Uri.parse(ringtoneStr);
846 if (ringtoneUri != null) {
847 Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
848 if (ringtone != null) {
849 entry.label = getString(R.string.label_ringtone);
850 entry.data = ringtone.getTitle(this);
851 entry.uri = ringtoneUri;
852 entry.actionIcon = R.drawable.sym_ringtone;
853 mOtherEntries.add(entry);
854 }
855 }
856 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700857 // Load the photo
858 } else if (mimetype.equals(CommonDataKinds.Photo.CONTENT_ITEM_TYPE)) {
859 photoBitmap = ContactsUtils.loadContactPhoto(aggCursor, DATA_1_COLUMN, null);
Evan Millar66388be2009-05-28 15:41:07 -0700860 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700861
Evan Millar45e0ed32009-06-01 16:44:38 -0700862
Evan Millar66388be2009-05-28 15:41:07 -0700863 // TODO(emillar) Add group entries
864// // Build the group entries
865// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
866// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
867// null, null, Groups.DEFAULT_SORT_ORDER);
868// if (groupCursor != null) {
869// try {
870// StringBuilder sb = new StringBuilder();
871//
872// while (groupCursor.moveToNext()) {
873// String systemId = groupCursor.getString(
874// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
875//
876// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
877// continue;
878// }
879//
880// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
881// if (!TextUtils.isEmpty(name)) {
882// if (sb.length() == 0) {
883// sb.append(name);
884// } else {
885// sb.append(getString(R.string.group_list, name));
886// }
887// }
888// }
889//
890// if (sb.length() > 0) {
891// ViewEntry entry = new ViewEntry();
892// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
893// entry.label = getString(R.string.label_groups);
894// entry.data = sb.toString();
895// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
896//
897// // TODO: Add an icon for the groups item.
898//
899// mGroupEntries.add(entry);
900// }
901// } finally {
902// groupCursor.close();
903// }
904// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700905
Evan Millar66388be2009-05-28 15:41:07 -0700906 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700907
908 if (photoBitmap == null) {
909 photoBitmap = ContactsUtils.loadPlaceholderPhoto(mNoPhotoResource, this, null);
910 }
911 mPhotoView.setImageBitmap(photoBitmap);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800912 }
913 }
914
Evan Millar66388be2009-05-28 15:41:07 -0700915
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800916 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700917 // If there is no type just display an empty string
918 if (type == null) {
919 type = "";
920 }
921
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800922 if (lowerCase) {
923 return getString(actionResId, type.toString().toLowerCase());
924 } else {
925 return getString(actionResId, type.toString());
926 }
927 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700928
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800929 /**
930 * A basic structure with the data for a contact entry in the list.
931 */
932 final static class ViewEntry extends ContactEntryAdapter.Entry {
933 public int primaryIcon = -1;
934 public Intent intent;
935 public Intent auxIntent = null;
936 public int presenceIcon = -1;
937 public int actionIcon = -1;
938 public int maxLabelLines = 1;
939 }
940
941 private static final class ViewAdapter extends ContactEntryAdapter<ViewEntry> {
942 /** Cache of the children views of a row */
943 static class ViewCache {
944 public TextView label;
945 public TextView data;
946 public ImageView actionIcon;
947 public ImageView presenceIcon;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700948
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800949 // Need to keep track of this too
950 ViewEntry entry;
951 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700952
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800953 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
954 super(context, sections, SHOW_SEPARATORS);
955 }
956
957 @Override
958 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -0700959 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800960 View v;
961
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800962 ViewCache views;
963
964 // Check to see if we can reuse convertView
965 if (convertView != null) {
966 v = convertView;
967 views = (ViewCache) v.getTag();
968 } else {
969 // Create a new view if needed
970 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
971
972 // Cache the children
973 views = new ViewCache();
974 views.label = (TextView) v.findViewById(android.R.id.text1);
975 views.data = (TextView) v.findViewById(android.R.id.text2);
976 views.actionIcon = (ImageView) v.findViewById(R.id.icon1);
977 views.presenceIcon = (ImageView) v.findViewById(R.id.icon2);
978 v.setTag(views);
979 }
980
981 // Update the entry in the view cache
982 views.entry = entry;
983
984 // Bind the data to the view
985 bindView(v, entry);
986 return v;
987 }
988
989 @Override
990 protected View newView(int position, ViewGroup parent) {
991 // getView() handles this
992 throw new UnsupportedOperationException();
993 }
994
995 @Override
996 protected void bindView(View view, ViewEntry entry) {
997 final Resources resources = mContext.getResources();
998 ViewCache views = (ViewCache) view.getTag();
999
1000 // Set the label
1001 TextView label = views.label;
1002 setMaxLines(label, entry.maxLabelLines);
1003 label.setText(entry.label);
1004
1005 // Set the data
1006 TextView data = views.data;
1007 if (data != null) {
1008 data.setText(entry.data);
1009 setMaxLines(data, entry.maxLines);
1010 }
1011
1012 // Set the action icon
1013 ImageView action = views.actionIcon;
1014 if (entry.actionIcon != -1) {
1015 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1016 action.setVisibility(View.VISIBLE);
1017 } else {
1018 // Things should still line up as if there was an icon, so make it invisible
1019 action.setVisibility(View.INVISIBLE);
1020 }
1021
1022 // Set the presence icon
1023 Drawable presenceIcon = null;
1024 if (entry.primaryIcon != -1) {
1025 presenceIcon = resources.getDrawable(entry.primaryIcon);
1026 } else if (entry.presenceIcon != -1) {
1027 presenceIcon = resources.getDrawable(entry.presenceIcon);
1028 }
1029
1030 ImageView presence = views.presenceIcon;
1031 if (presenceIcon != null) {
1032 presence.setImageDrawable(presenceIcon);
1033 presence.setVisibility(View.VISIBLE);
1034 } else {
1035 presence.setVisibility(View.GONE);
1036 }
1037 }
1038
1039 private void setMaxLines(TextView textView, int maxLines) {
1040 if (maxLines == 1) {
1041 textView.setSingleLine(true);
1042 textView.setEllipsize(TextUtils.TruncateAt.END);
1043 } else {
1044 textView.setSingleLine(false);
1045 textView.setMaxLines(maxLines);
1046 textView.setEllipsize(null);
1047 }
1048 }
1049 }
1050}