blob: 8c29d49534605da3d4e2abb3053f1c300fba2220 [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
Dmitri Plotnikov39466592009-07-27 11:23:51 -070019import static com.android.contacts.ContactEntryAdapter.AGGREGATE_PHOTO_ID;
Evan Millar66388be2009-05-28 15:41:07 -070020import static com.android.contacts.ContactEntryAdapter.AGGREGATE_PROJECTION;
21import static com.android.contacts.ContactEntryAdapter.AGGREGATE_STARRED_COLUMN;
Evan Millar66388be2009-05-28 15:41:07 -070022import static com.android.contacts.ContactEntryAdapter.DATA_1_COLUMN;
23import static com.android.contacts.ContactEntryAdapter.DATA_2_COLUMN;
24import static com.android.contacts.ContactEntryAdapter.DATA_3_COLUMN;
25import static com.android.contacts.ContactEntryAdapter.DATA_4_COLUMN;
26import static com.android.contacts.ContactEntryAdapter.DATA_5_COLUMN;
Evan Millar66388be2009-05-28 15:41:07 -070027import static com.android.contacts.ContactEntryAdapter.DATA_9_COLUMN;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070028import static com.android.contacts.ContactEntryAdapter.DATA_CONTACT_ID_COLUMN;
29import static com.android.contacts.ContactEntryAdapter.DATA_ID_COLUMN;
30import static com.android.contacts.ContactEntryAdapter.DATA_IS_SUPER_PRIMARY_COLUMN;
31import static com.android.contacts.ContactEntryAdapter.DATA_MIMETYPE_COLUMN;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080032
Evan Millar54a5c9f2009-06-23 17:41:09 -070033import com.android.contacts.Collapser.Collapsible;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070034import com.android.contacts.SplitAggregateView.OnContactSelectedListener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080035import com.android.internal.telephony.ITelephony;
36
37import android.app.AlertDialog;
38import android.app.Dialog;
39import android.app.ListActivity;
40import android.content.ActivityNotFoundException;
41import android.content.ContentResolver;
42import android.content.ContentUris;
43import android.content.ContentValues;
44import android.content.Context;
45import android.content.DialogInterface;
46import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070047import android.content.DialogInterface.OnClickListener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080048import android.content.pm.PackageManager;
49import android.content.pm.ResolveInfo;
50import android.content.res.Resources;
51import android.database.ContentObserver;
52import android.database.Cursor;
Evan Millar45e0ed32009-06-01 16:44:38 -070053import android.graphics.Bitmap;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080054import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080055import 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;
Evan Millar54a5c9f2009-06-23 17:41:09 -070065import android.provider.ContactsContract.Presence;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070066import android.provider.ContactsContract.RawContacts;
Evan Millar54a5c9f2009-06-23 17:41:09 -070067import android.provider.ContactsContract.CommonDataKinds.Phone;
68import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080069import android.text.TextUtils;
70import android.util.Log;
71import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070072import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080073import android.view.KeyEvent;
74import android.view.Menu;
75import android.view.MenuItem;
76import android.view.View;
77import android.view.ViewGroup;
78import android.view.ContextMenu.ContextMenuInfo;
79import android.widget.AdapterView;
80import android.widget.CheckBox;
81import android.widget.ImageView;
82import android.widget.ListView;
83import android.widget.TextView;
84import android.widget.Toast;
85
86import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087
88/**
89 * Displays the details of a specific contact.
90 */
Evan Millar5c22c3b2009-05-29 11:37:54 -070091public class ViewContactActivity extends ListActivity
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080092 implements View.OnCreateContextMenuListener, View.OnClickListener,
93 DialogInterface.OnClickListener {
94 private static final String TAG = "ViewContact";
95 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
96
97 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080098
99 private static final int DIALOG_CONFIRM_DELETE = 1;
100
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700101 private static final int REQUEST_JOIN_AGGREGATE = 1;
102
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800103 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;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700106 public static final int MENU_ITEM_SPLIT_AGGREGATE = 4;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700107 public static final int MENU_ITEM_JOIN_AGGREGATE = 5;
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700108 public static final int MENU_ITEM_OPTIONS = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800109
110 private Uri mUri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700111 private Uri mAggDataUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800112 private ContentResolver mResolver;
113 private ViewAdapter mAdapter;
114 private int mNumPhoneNumbers = 0;
115
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700116 /**
117 * A list of distinct contact IDs included in the current aggregate.
118 */
119 private ArrayList<Long> mContactIds = new ArrayList<Long>();
120
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800121 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
122 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
123 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
124 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
125 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
126 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700127 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800128 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
129 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
130
131 private Cursor mCursor;
132 private boolean mObserverRegistered;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700133
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800134 private ContentObserver mObserver = new ContentObserver(new Handler()) {
135 @Override
136 public boolean deliverSelfNotifications() {
137 return true;
138 }
139
140 @Override
141 public void onChange(boolean selfChange) {
142 if (mCursor != null && !mCursor.isClosed()){
143 dataChanged();
144 }
145 }
146 };
147
148 public void onClick(DialogInterface dialog, int which) {
149 if (mCursor != null) {
150 if (mObserverRegistered) {
151 mCursor.unregisterContentObserver(mObserver);
152 mObserverRegistered = false;
153 }
154 mCursor.close();
155 mCursor = null;
156 }
157 getContentResolver().delete(mUri, null, null);
158 finish();
159 }
160
161 public void onClick(View view) {
162 if (!mObserverRegistered) {
163 return;
164 }
165 switch (view.getId()) {
166 case R.id.star: {
Evan Millar7e4accf2009-06-08 10:43:26 -0700167 mCursor.moveToFirst();
Evan Millar66388be2009-05-28 15:41:07 -0700168 int oldStarredState = mCursor.getInt(AGGREGATE_STARRED_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800169 ContentValues values = new ContentValues(1);
Evan Millar66388be2009-05-28 15:41:07 -0700170 values.put(Aggregates.STARRED, oldStarredState == 1 ? 0 : 1);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800171 getContentResolver().update(mUri, values, null, null);
172 break;
173 }
174 }
175 }
176
177 private TextView mNameView;
178 private TextView mPhoneticNameView; // may be null in some locales
179 private ImageView mPhotoView;
180 private int mNoPhotoResource;
181 private CheckBox mStarView;
182 private boolean mShowSmsLinksForAllPhones;
183
184 @Override
185 protected void onCreate(Bundle icicle) {
186 super.onCreate(icicle);
187
188 setContentView(R.layout.view_contact);
189 getListView().setOnCreateContextMenuListener(this);
190
191 mNameView = (TextView) findViewById(R.id.name);
192 mPhoneticNameView = (TextView) findViewById(R.id.phonetic_name);
193 mPhotoView = (ImageView) findViewById(R.id.photo);
194 mStarView = (CheckBox) findViewById(R.id.star);
195 mStarView.setOnClickListener(this);
196
197 // Set the photo with a random "no contact" image
198 long now = SystemClock.elapsedRealtime();
199 int num = (int) now & 0xf;
200 if (num < 9) {
201 // Leaning in from right, common
202 mNoPhotoResource = R.drawable.ic_contact_picture;
203 } else if (num < 14) {
204 // Leaning in from left uncommon
205 mNoPhotoResource = R.drawable.ic_contact_picture_2;
206 } else {
207 // Coming in from the top, rare
208 mNoPhotoResource = R.drawable.ic_contact_picture_3;
209 }
210
Evan Millar45e0ed32009-06-01 16:44:38 -0700211 mUri = getIntent().getData();
212 mAggDataUri = Uri.withAppendedPath(mUri, "data");
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800213 mResolver = getContentResolver();
214
215 // Build the list of sections. The order they're added to mSections dictates the
216 // order they are displayed in the list.
217 mSections.add(mPhoneEntries);
218 mSections.add(mSmsEntries);
219 mSections.add(mEmailEntries);
220 mSections.add(mImEntries);
221 mSections.add(mPostalEntries);
222 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700223 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800224 mSections.add(mOtherEntries);
225
226 //TODO Read this value from a preference
227 mShowSmsLinksForAllPhones = true;
228
Evan Millar45e0ed32009-06-01 16:44:38 -0700229 mCursor = mResolver.query(mAggDataUri,
230 AGGREGATE_PROJECTION, null, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800231 }
232
233 @Override
234 protected void onResume() {
235 super.onResume();
236 mObserverRegistered = true;
237 mCursor.registerContentObserver(mObserver);
238 dataChanged();
239 }
240
241 @Override
242 protected void onPause() {
243 super.onPause();
244 if (mCursor != null) {
245 if (mObserverRegistered) {
246 mObserverRegistered = false;
247 mCursor.unregisterContentObserver(mObserver);
248 }
249 mCursor.deactivate();
250 }
251 }
252
253 @Override
254 protected void onDestroy() {
255 super.onDestroy();
256
257 if (mCursor != null) {
258 if (mObserverRegistered) {
259 mCursor.unregisterContentObserver(mObserver);
260 mObserverRegistered = false;
261 }
262 mCursor.close();
263 }
264 }
265
266 @Override
267 protected Dialog onCreateDialog(int id) {
268 switch (id) {
269 case DIALOG_CONFIRM_DELETE:
270 return new AlertDialog.Builder(this)
271 .setTitle(R.string.deleteConfirmation_title)
272 .setIcon(android.R.drawable.ic_dialog_alert)
273 .setMessage(R.string.deleteConfirmation)
274 .setNegativeButton(android.R.string.cancel, null)
275 .setPositiveButton(android.R.string.ok, this)
276 .setCancelable(false)
277 .create();
278 }
279 return null;
280 }
281
282 private void dataChanged() {
283 mCursor.requery();
284 if (mCursor.moveToFirst()) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800285 // Set the star
Evan Millar66388be2009-05-28 15:41:07 -0700286 mStarView.setChecked(mCursor.getInt(AGGREGATE_STARRED_COLUMN) == 1 ? true : false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800287
Evan Millar2c1cc832009-07-13 11:08:06 -0700288 //Set the photo
289 int photoId = mCursor.getInt(AGGREGATE_PHOTO_ID);
290 Bitmap photoBitmap = ContactsUtils.loadContactPhoto(
291 this, photoId, null);
292 if (photoBitmap == null) {
293 photoBitmap = ContactsUtils.loadPlaceholderPhoto(mNoPhotoResource, this, null);
294 }
295 mPhotoView.setImageBitmap(photoBitmap);
296
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800297 // Build up the contact entries
298 buildEntries(mCursor);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700299
300 // Collapse similar data items in select sections.
301 Collapser.collapseList(mPhoneEntries);
302 Collapser.collapseList(mSmsEntries);
303 Collapser.collapseList(mEmailEntries);
304 Collapser.collapseList(mPostalEntries);
305
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800306 if (mAdapter == null) {
307 mAdapter = new ViewAdapter(this, mSections);
308 setListAdapter(mAdapter);
309 } else {
310 mAdapter.setSections(mSections, SHOW_SEPARATORS);
311 }
312 } else {
313 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
314 Log.e(TAG, "invalid contact uri: " + mUri);
315 finish();
316 }
317 }
318
319 @Override
320 public boolean onCreateOptionsMenu(Menu menu) {
321 menu.add(0, 0, 0, R.string.menu_editContact)
322 .setIcon(android.R.drawable.ic_menu_edit)
323 .setIntent(new Intent(Intent.ACTION_EDIT, mUri))
324 .setAlphabeticShortcut('e');
325 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
326 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700327 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
328 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700329 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
330 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700331 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
332 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800333 return true;
334 }
335
336 @Override
337 public boolean onPrepareOptionsMenu(Menu menu) {
338 super.onPrepareOptionsMenu(menu);
339 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
340 // could be installed or uninstalled at any time.
341 if (isBarcodeScannerInstalled()) {
342 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
343 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
344 .setIcon(R.drawable.ic_menu_show_barcode);
345 }
346 } else {
347 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
348 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700349
350 boolean isAggregate = mContactIds.size() > 1;
351 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800352 return true;
353 }
354
355 private boolean isBarcodeScannerInstalled() {
356 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
357 ResolveInfo ri = getPackageManager().resolveActivity(intent,
358 PackageManager.MATCH_DEFAULT_ONLY);
359 return ri != null;
360 }
361
362 @Override
363 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
364 AdapterView.AdapterContextMenuInfo info;
365 try {
366 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
367 } catch (ClassCastException e) {
368 Log.e(TAG, "bad menuInfo", e);
369 return;
370 }
371
372 // This can be null sometimes, don't crash...
373 if (info == null) {
374 Log.e(TAG, "bad menuInfo");
375 return;
376 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700377
Evan Millar45e0ed32009-06-01 16:44:38 -0700378 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
379 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
380 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
381 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.auxIntent);
382 if (entry.primaryIcon == -1) {
383 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800384 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700385 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
386 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
387 if (entry.primaryIcon == -1) {
388 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800389 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700390 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700391 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
392 }
393 // TODO(emillar): add back with group support.
394 /* else if (entry.mimetype.equals()) {
395 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
396 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800397 }
398
399 @Override
400 public boolean onOptionsItemSelected(MenuItem item) {
401 switch (item.getItemId()) {
402 case MENU_ITEM_DELETE: {
403 // Get confirmation
404 showDialog(DIALOG_CONFIRM_DELETE);
405 return true;
406 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700407
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700408 case MENU_ITEM_SPLIT_AGGREGATE: {
409 showSplitAggregateDialog();
410 return true;
411 }
412
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700413 case MENU_ITEM_JOIN_AGGREGATE: {
414 showJoinAggregateActivity();
415 return true;
416 }
417
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700418 case MENU_ITEM_OPTIONS: {
419 showOptionsActivity();
420 return true;
421 }
422
Evan Millar66388be2009-05-28 15:41:07 -0700423 // TODO(emillar) Bring this back.
424 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800425 if (mCursor.moveToFirst()) {
426 Intent intent = new Intent(SHOW_BARCODE_INTENT);
427 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
428 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700429 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800430 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700431 // Correctly handle when section headers are hidden
432 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700433
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800434 bundle.putString(Contacts.Intents.Insert.NAME, name);
435 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700436 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800437 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700438 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800439 bundle.putString(PHONE_KEYS[x], entry.data);
440 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700441 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800442 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700443 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800444 bundle.putString(EMAIL_KEYS[x], entry.data);
445 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700446 if (mPostalEntries.size() >= 1 + sepAdjust) {
447 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800448 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
449 }
450 intent.putExtra("ENCODE_DATA", bundle);
451 try {
452 startActivity(intent);
453 } catch (ActivityNotFoundException e) {
454 // The check in onPrepareOptionsMenu() should make this impossible, but
455 // for safety I'm catching the exception rather than crashing. Ideally
456 // I'd call Menu.removeItem() here too, but I don't see a way to get
457 // the options menu.
458 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
459 "was not installed.");
460 }
461 return true;
462 }
463 }
Evan Millar66388be2009-05-28 15:41:07 -0700464 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800465 }
466 return super.onOptionsItemSelected(item);
467 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700468
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800469 @Override
470 public boolean onContextItemSelected(MenuItem item) {
471 switch (item.getItemId()) {
472 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700473 if (makeItemDefault(item)) {
474 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800475 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700476 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800477 }
478 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700479
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800480 return super.onContextItemSelected(item);
481 }
482
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700483 private boolean makeItemDefault(MenuItem item) {
484 ViewEntry entry = getViewEntryForMenuItem(item);
485 if (entry == null) {
486 return false;
487 }
488
489 // Update the primary values in the data record.
490 ContentValues values = new ContentValues(2);
491 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700492
Evan Millar54a5c9f2009-06-23 17:41:09 -0700493 if (entry.ids.size() > 0) {
494 for (int i = 0; i < entry.ids.size(); i++) {
495 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
496 entry.ids.get(i)),
497 values, null, null);
498 }
499 }
500
501 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700502 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
503 values, null, null);
504 dataChanged();
505 return true;
506 }
507
508 /**
509 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
510 * The user picks a contact to be split into its own aggregate or clicks Cancel.
511 */
512 private void showSplitAggregateDialog() {
513
514 // Wrap this dialog in a specific theme so that list items have correct text color.
515 final ContextThemeWrapper dialogContext =
516 new ContextThemeWrapper(this, android.R.style.Theme_Light);
517 AlertDialog.Builder builder =
518 new AlertDialog.Builder(dialogContext);
519 builder.setTitle(getString(R.string.splitAggregate_title));
520
521 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
522 builder.setView(view);
523
524 builder.setInverseBackgroundForced(true);
525 builder.setCancelable(true);
526 builder.setNegativeButton(android.R.string.cancel,
527 new OnClickListener() {
528 public void onClick(DialogInterface dialog, int which) {
529 dialog.dismiss();
530 }
531 });
532 final AlertDialog dialog = builder.create();
533
534 view.setOnContactSelectedListener(new OnContactSelectedListener() {
535 public void onContactSelected(long contactId) {
536 dialog.dismiss();
537 splitContact(contactId);
538 }
539 });
540
541 dialog.show();
542 }
543
544 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700545 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700546 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700547 public void showJoinAggregateActivity() {
548 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
549 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
550 startActivityForResult(intent, REQUEST_JOIN_AGGREGATE);
551 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700552
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700553 @Override
554 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
555 if (requestCode == REQUEST_JOIN_AGGREGATE && resultCode == RESULT_OK && intent != null) {
556 final long aggregateId = ContentUris.parseId(intent.getData());
557 joinAggregate(aggregateId);
558 }
559 }
560
561 private void splitContact(long contactId) {
562 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_OUT);
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700563 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700564 mAdapter.notifyDataSetChanged();
565 }
566
567 private void joinAggregate(final long aggregateId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700568 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
569 RawContacts.AGGREGATE_ID + "=" + aggregateId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700570
571 try {
572 while(c.moveToNext()) {
573 long contactId = c.getLong(0);
574 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
575 }
576 } finally {
577 c.close();
578 }
579
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700580 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700581 mAdapter.notifyDataSetChanged();
582 }
583
584 /**
585 * Given a contact ID sets an aggregation exception to either join the contact with the
586 * current aggregate or split off.
587 */
588 protected void setAggregationException(long contactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700589 ContentValues values = new ContentValues(3);
590 values.put(AggregationExceptions.AGGREGATE_ID, ContentUris.parseId(mUri));
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700591 values.put(AggregationExceptions.CONTACT_ID, contactId);
592 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700593 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700594 }
595
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700596 private void showOptionsActivity() {
597 final Intent intent = new Intent(this, ContactOptionsActivity.class);
598 intent.setData(mUri);
599 startActivity(intent);
600 }
601
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700602 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
603 AdapterView.AdapterContextMenuInfo info;
604 try {
605 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
606 } catch (ClassCastException e) {
607 Log.e(TAG, "bad menuInfo", e);
608 return null;
609 }
610
611 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
612 }
613
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800614 @Override
615 public boolean onKeyDown(int keyCode, KeyEvent event) {
616 switch (keyCode) {
617 case KeyEvent.KEYCODE_CALL: {
618 try {
619 ITelephony phone = ITelephony.Stub.asInterface(
620 ServiceManager.checkService("phone"));
621 if (phone != null && !phone.isIdle()) {
622 // Skip out and let the key be handled at a higher level
623 break;
624 }
625 } catch (RemoteException re) {
626 // Fall through and try to call the contact
627 }
628
629 int index = getListView().getSelectedItemPosition();
630 if (index != -1) {
631 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700632 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
633 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800634 }
635 } else if (mNumPhoneNumbers != 0) {
636 // There isn't anything selected, call the default number
637 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
638 startActivity(intent);
639 }
640 return true;
641 }
642
643 case KeyEvent.KEYCODE_DEL: {
644 showDialog(DIALOG_CONFIRM_DELETE);
645 return true;
646 }
647 }
648
649 return super.onKeyDown(keyCode, event);
650 }
651
652 @Override
653 protected void onListItemClick(ListView l, View v, int position, long id) {
654 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
655 if (entry != null) {
656 Intent intent = entry.intent;
657 if (intent != null) {
658 try {
659 startActivity(intent);
660 } catch (ActivityNotFoundException e) {
661 Log.e(TAG, "No activity found for intent: " + intent);
662 signalError();
663 }
664 } else {
665 signalError();
666 }
667 } else {
668 signalError();
669 }
670 }
671
672 /**
673 * Signal an error to the user via a beep, or some other method.
674 */
675 private void signalError() {
676 //TODO: implement this when we have the sonification APIs
677 }
678
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800679 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700680 // 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 -0800681 StringBuilder buf = new StringBuilder("imto://");
682 buf.append(host);
683 buf.append('/');
684 buf.append(data);
685 return Uri.parse(buf.toString());
686 }
687
688 /**
689 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700690 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800691 * @param personCursor the URI for the contact being displayed
692 */
Evan Millar66388be2009-05-28 15:41:07 -0700693 private final void buildEntries(Cursor aggCursor) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800694 // Clear out the old entries
695 final int numSections = mSections.size();
696 for (int i = 0; i < numSections; i++) {
697 mSections.get(i).clear();
698 }
699
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700700 mContactIds.clear();
701
Evan Millar66388be2009-05-28 15:41:07 -0700702 // Build up method entries
703 if (mUri != null) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700704 Bitmap photoBitmap = null;
Evan Millar66388be2009-05-28 15:41:07 -0700705 while (aggCursor.moveToNext()) {
706 final String mimetype = aggCursor.getString(DATA_MIMETYPE_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700707
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800708 ViewEntry entry = new ViewEntry();
Evan Millar66388be2009-05-28 15:41:07 -0700709
710 final long id = aggCursor.getLong(DATA_ID_COLUMN);
711 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800712 entry.id = id;
713 entry.uri = uri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700714 entry.mimetype = mimetype;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700715 entry.contactId = aggCursor.getLong(DATA_CONTACT_ID_COLUMN);
716 if (!mContactIds.contains(entry.contactId)) {
717 mContactIds.add(entry.contactId);
718 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800719
Evan Millar66388be2009-05-28 15:41:07 -0700720 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
Evan Millar5c22c3b2009-05-29 11:37:54 -0700721 || mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700722 || mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
Evan Millar66388be2009-05-28 15:41:07 -0700723 || mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
724 final int type = aggCursor.getInt(DATA_1_COLUMN);
Evan Millar45e0ed32009-06-01 16:44:38 -0700725 final String label = aggCursor.getString(DATA_3_COLUMN);
726 final String data = aggCursor.getString(DATA_2_COLUMN);
727 final boolean isSuperPrimary = "1".equals(
728 aggCursor.getString(DATA_IS_SUPER_PRIMARY_COLUMN));
Evan Millar66388be2009-05-28 15:41:07 -0700729
Evan Millar54a5c9f2009-06-23 17:41:09 -0700730 entry.type = type;
731
Evan Millar66388be2009-05-28 15:41:07 -0700732 // Don't crash if the data is bogus
733 if (TextUtils.isEmpty(data)) {
734 Log.w(TAG, "empty data for contact method " + id);
735 continue;
736 }
737
738 // Build phone entries
739 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
740 mNumPhoneNumbers++;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700741
Evan Millar66388be2009-05-28 15:41:07 -0700742 final CharSequence displayLabel = ContactsUtils.getDisplayLabel(
743 this, mimetype, type, label);
744 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700745 entry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar66388be2009-05-28 15:41:07 -0700746 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, entry.uri);
747 entry.auxIntent = new Intent(Intent.ACTION_SENDTO,
748 Uri.fromParts("sms", data, null));
Evan Millar45e0ed32009-06-01 16:44:38 -0700749 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700750 entry.primaryIcon = R.drawable.ic_default_number;
751 }
752 entry.actionIcon = android.R.drawable.sym_action_call;
753 mPhoneEntries.add(entry);
754
Evan Millar5c22c3b2009-05-29 11:37:54 -0700755 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700756 || mShowSmsLinksForAllPhones) {
757 // Add an SMS entry
758 ViewEntry smsEntry = new ViewEntry();
759 smsEntry.label = buildActionString(
760 R.string.actionText, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700761 smsEntry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar66388be2009-05-28 15:41:07 -0700762 smsEntry.id = id;
763 smsEntry.uri = uri;
764 smsEntry.intent = entry.auxIntent;
Evan Millar45e0ed32009-06-01 16:44:38 -0700765 smsEntry.mimetype = FastTrackWindow.MIME_SMS_ADDRESS;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700766 smsEntry.type = type;
Evan Millar66388be2009-05-28 15:41:07 -0700767 smsEntry.actionIcon = R.drawable.sym_action_sms;
768 mSmsEntries.add(smsEntry);
769 }
770 // Build email entries
771 } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800772 entry.label = buildActionString(R.string.actionEmail,
Evan Millar66388be2009-05-28 15:41:07 -0700773 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800774 entry.data = data;
775 entry.intent = new Intent(Intent.ACTION_SENDTO,
776 Uri.fromParts("mailto", data, null));
777 entry.actionIcon = android.R.drawable.sym_action_email;
Evan Millar45e0ed32009-06-01 16:44:38 -0700778 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700779 entry.primaryIcon = R.drawable.ic_default_number;
780 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800781 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700782 // Build postal entries
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700783 } else if (mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800784 entry.label = buildActionString(R.string.actionMap,
Evan Millar66388be2009-05-28 15:41:07 -0700785 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800786 entry.data = data;
787 entry.maxLines = 4;
788 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
789 entry.actionIcon = R.drawable.sym_action_map;
790 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700791 // Build im entries
792 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
793 String[] protocolStrings = getResources().getStringArray(
794 android.R.array.imProtocols);
795 Object protocolObj = ContactsUtils.decodeImProtocol(
796 aggCursor.getString(DATA_5_COLUMN));
Alex Kennberg87fc3172009-03-28 06:43:06 -0700797 String host = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800798 if (protocolObj instanceof Number) {
799 int protocol = ((Number) protocolObj).intValue();
800 entry.label = buildActionString(R.string.actionChat,
801 protocolStrings[protocol], false);
Evan Millar66388be2009-05-28 15:41:07 -0700802 host = ContactsUtils.lookupProviderNameFromId(
803 protocol).toLowerCase();
804 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
805 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800806 entry.maxLabelLines = 2;
807 }
Alex Kennberg87fc3172009-03-28 06:43:06 -0700808 } else if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800809 String providerName = (String) protocolObj;
810 entry.label = buildActionString(R.string.actionChat,
811 providerName, false);
812 host = providerName.toLowerCase();
813 }
814
815 // Only add the intent if there is a valid host
816 if (!TextUtils.isEmpty(host)) {
817 entry.intent = new Intent(Intent.ACTION_SENDTO,
818 constructImToUrl(host, data));
819 }
820 entry.data = data;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700821 //TODO(emillar) Add in presence info
Evan Millar66388be2009-05-28 15:41:07 -0700822 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800823 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700824 aggCursor.getInt(METHODS_STATUS_COLUMN));
Evan Millar54a5c9f2009-06-23 17:41:09 -0700825 entry.status = ...
Evan Millar66388be2009-05-28 15:41:07 -0700826 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800827 entry.actionIcon = android.R.drawable.sym_action_chat;
828 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700829 }
Evan Millar7e4accf2009-06-08 10:43:26 -0700830 // Set the name
831 } else if (mimetype.equals(CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
832 String name = mCursor.getString(DATA_9_COLUMN);
833 if (TextUtils.isEmpty(name)) {
834 mNameView.setText(getText(android.R.string.unknownName));
835 } else {
836 mNameView.setText(name);
837 }
838 /*
839 if (mPhoneticNameView != null) {
840 String phoneticName = mCursor.getString(CONTACT_PHONETIC_NAME_COLUMN);
841 mPhoneticNameView.setText(phoneticName);
842 }
843 */
Evan Millar66388be2009-05-28 15:41:07 -0700844 // Build organization entries
845 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
Evan Millar66388be2009-05-28 15:41:07 -0700846 final String company = aggCursor.getString(DATA_3_COLUMN);
847 final String title = aggCursor.getString(DATA_4_COLUMN);
848 final boolean isPrimary = "1".equals(aggCursor.getString(DATA_5_COLUMN));
849
850 if (isPrimary) {
851 entry.primaryIcon = R.drawable.ic_default_number;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800852 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700853
Evan Millar66388be2009-05-28 15:41:07 -0700854 // Don't crash if the data is bogus
855 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
856 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800857 continue;
858 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700859
Evan Millar66388be2009-05-28 15:41:07 -0700860 entry.data = title;
861 entry.actionIcon = R.drawable.sym_action_organization;
862 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800863
Evan Millar66388be2009-05-28 15:41:07 -0700864 mOrganizationEntries.add(entry);
865 // Build note entries
866 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
867 entry.label = getString(R.string.label_notes);
868 entry.data = aggCursor.getString(DATA_1_COLUMN);
869 entry.id = 0;
870 entry.uri = null;
871 entry.intent = null;
872 entry.maxLines = 10;
873 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800874
Evan Millar66388be2009-05-28 15:41:07 -0700875 if (TextUtils.isEmpty(entry.data)) {
876 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700877 continue;
878 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700879
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800880 mOtherEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700881 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700882
Evan Millar45e0ed32009-06-01 16:44:38 -0700883
Evan Millar66388be2009-05-28 15:41:07 -0700884 // TODO(emillar) Add group entries
885// // Build the group entries
886// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
887// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
888// null, null, Groups.DEFAULT_SORT_ORDER);
889// if (groupCursor != null) {
890// try {
891// StringBuilder sb = new StringBuilder();
892//
893// while (groupCursor.moveToNext()) {
894// String systemId = groupCursor.getString(
895// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
896//
897// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
898// continue;
899// }
900//
901// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
902// if (!TextUtils.isEmpty(name)) {
903// if (sb.length() == 0) {
904// sb.append(name);
905// } else {
906// sb.append(getString(R.string.group_list, name));
907// }
908// }
909// }
910//
911// if (sb.length() > 0) {
912// ViewEntry entry = new ViewEntry();
913// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
914// entry.label = getString(R.string.label_groups);
915// entry.data = sb.toString();
916// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
917//
918// // TODO: Add an icon for the groups item.
919//
920// mGroupEntries.add(entry);
921// }
922// } finally {
923// groupCursor.close();
924// }
925// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700926
Evan Millar66388be2009-05-28 15:41:07 -0700927 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800928 }
929 }
930
931 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700932 // If there is no type just display an empty string
933 if (type == null) {
934 type = "";
935 }
936
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800937 if (lowerCase) {
938 return getString(actionResId, type.toString().toLowerCase());
939 } else {
940 return getString(actionResId, type.toString());
941 }
942 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700943
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800944 /**
945 * A basic structure with the data for a contact entry in the list.
946 */
Evan Millar54a5c9f2009-06-23 17:41:09 -0700947 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800948 public int primaryIcon = -1;
949 public Intent intent;
950 public Intent auxIntent = null;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700951 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800952 public int actionIcon = -1;
953 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700954 public ArrayList<Long> ids = new ArrayList<Long>();
955 public int collapseCount = 0;
956
957 public boolean collapseWith(ViewEntry entry) {
958 // assert equal collapse keys
959 if (!getCollapseKey().equals(entry.getCollapseKey())) {
960 return false;
961 }
962
963 // Choose the label associated with the highest type precedence.
964 if (TypePrecedence.getTypePrecedence(mimetype, type)
965 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
966 type = entry.type;
967 label = entry.label;
968 }
969
970 // Choose the max of the maxLines and maxLabelLines values.
971 maxLines = Math.max(maxLines, entry.maxLines);
972 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
973
974 // Choose the presence with the highest precedence.
975 if (Presence.getPresencePrecedence(status)
976 < Presence.getPresencePrecedence(entry.status)) {
977 status = entry.status;
978 }
979
980 // If any of the collapsed entries are primary make the whole thing primary.
981 if (primaryIcon != entry.primaryIcon && primaryIcon == -1) {
982 primaryIcon = entry.primaryIcon;
983 }
984
985 // uri, and contactdId, shouldn't make a difference. Just keep the original.
986
987 // Keep track of all the ids that have been collapsed with this one.
988 ids.add(entry.id);
989 collapseCount++;
990 return true;
991 }
992
993 public String getCollapseKey() {
994 StringBuilder hashSb = new StringBuilder();
995 hashSb.append(data);
996 hashSb.append(mimetype);
997 hashSb.append((intent != null && intent.getAction() != null)
998 ? intent.getAction() : "");
999 hashSb.append((auxIntent != null && auxIntent.getAction() != null)
1000 ? auxIntent.getAction() : "");
1001 hashSb.append(actionIcon);
1002 return hashSb.toString();
1003 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001004 }
1005
1006 private static final class ViewAdapter extends ContactEntryAdapter<ViewEntry> {
1007 /** Cache of the children views of a row */
1008 static class ViewCache {
1009 public TextView label;
1010 public TextView data;
1011 public ImageView actionIcon;
1012 public ImageView presenceIcon;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001013
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001014 // Need to keep track of this too
1015 ViewEntry entry;
1016 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001017
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001018 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1019 super(context, sections, SHOW_SEPARATORS);
1020 }
1021
1022 @Override
1023 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001024 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001025 View v;
1026
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001027 ViewCache views;
1028
1029 // Check to see if we can reuse convertView
1030 if (convertView != null) {
1031 v = convertView;
1032 views = (ViewCache) v.getTag();
1033 } else {
1034 // Create a new view if needed
1035 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1036
1037 // Cache the children
1038 views = new ViewCache();
1039 views.label = (TextView) v.findViewById(android.R.id.text1);
1040 views.data = (TextView) v.findViewById(android.R.id.text2);
1041 views.actionIcon = (ImageView) v.findViewById(R.id.icon1);
1042 views.presenceIcon = (ImageView) v.findViewById(R.id.icon2);
1043 v.setTag(views);
1044 }
1045
1046 // Update the entry in the view cache
1047 views.entry = entry;
1048
1049 // Bind the data to the view
1050 bindView(v, entry);
1051 return v;
1052 }
1053
1054 @Override
1055 protected View newView(int position, ViewGroup parent) {
1056 // getView() handles this
1057 throw new UnsupportedOperationException();
1058 }
1059
1060 @Override
1061 protected void bindView(View view, ViewEntry entry) {
1062 final Resources resources = mContext.getResources();
1063 ViewCache views = (ViewCache) view.getTag();
1064
1065 // Set the label
1066 TextView label = views.label;
1067 setMaxLines(label, entry.maxLabelLines);
1068 label.setText(entry.label);
1069
1070 // Set the data
1071 TextView data = views.data;
1072 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001073 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1074 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1075 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1076 } else {
1077 data.setText(entry.data);
1078 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001079 setMaxLines(data, entry.maxLines);
1080 }
1081
1082 // Set the action icon
1083 ImageView action = views.actionIcon;
1084 if (entry.actionIcon != -1) {
1085 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1086 action.setVisibility(View.VISIBLE);
1087 } else {
1088 // Things should still line up as if there was an icon, so make it invisible
1089 action.setVisibility(View.INVISIBLE);
1090 }
1091
1092 // Set the presence icon
1093 Drawable presenceIcon = null;
1094 if (entry.primaryIcon != -1) {
1095 presenceIcon = resources.getDrawable(entry.primaryIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001096 } else if (entry.status != -1) {
1097 presenceIcon = resources.getDrawable(
1098 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001099 }
1100
1101 ImageView presence = views.presenceIcon;
1102 if (presenceIcon != null) {
1103 presence.setImageDrawable(presenceIcon);
1104 presence.setVisibility(View.VISIBLE);
1105 } else {
1106 presence.setVisibility(View.GONE);
1107 }
1108 }
1109
1110 private void setMaxLines(TextView textView, int maxLines) {
1111 if (maxLines == 1) {
1112 textView.setSingleLine(true);
1113 textView.setEllipsize(TextUtils.TruncateAt.END);
1114 } else {
1115 textView.setSingleLine(false);
1116 textView.setMaxLines(maxLines);
1117 textView.setEllipsize(null);
1118 }
1119 }
1120 }
1121}