blob: 46cd26b052b0d6646e79a395de20a369fa49a477 [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 Millar2c1cc832009-07-13 11:08:06 -070021import static com.android.contacts.ContactEntryAdapter.AGGREGATE_PHOTO_ID;
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;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -070061import android.provider.ContactsContract.Contacts;
Evan Millar66388be2009-05-28 15:41:07 -070062import android.provider.ContactsContract.Aggregates;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070063import android.provider.ContactsContract.AggregationExceptions;
64import android.provider.ContactsContract.CommonDataKinds;
Evan Millar66388be2009-05-28 15:41:07 -070065import android.provider.ContactsContract.Data;
Evan Millar54a5c9f2009-06-23 17:41:09 -070066import android.provider.ContactsContract.Presence;
67import 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;
Evan Millar54a5c9f2009-06-23 17:41:09 -070087import java.util.Collection;
88import java.util.HashMap;
89import java.util.Iterator;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080090
91/**
92 * Displays the details of a specific contact.
93 */
Evan Millar5c22c3b2009-05-29 11:37:54 -070094public class ViewContactActivity extends ListActivity
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080095 implements View.OnCreateContextMenuListener, View.OnClickListener,
96 DialogInterface.OnClickListener {
97 private static final String TAG = "ViewContact";
98 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
99
100 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800101
102 private static final int DIALOG_CONFIRM_DELETE = 1;
103
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700104 private static final int REQUEST_JOIN_AGGREGATE = 1;
105
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800106 public static final int MENU_ITEM_DELETE = 1;
107 public static final int MENU_ITEM_MAKE_DEFAULT = 2;
108 public static final int MENU_ITEM_SHOW_BARCODE = 3;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700109 public static final int MENU_ITEM_SPLIT_AGGREGATE = 4;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700110 public static final int MENU_ITEM_JOIN_AGGREGATE = 5;
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700111 public static final int MENU_ITEM_OPTIONS = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800112
113 private Uri mUri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700114 private Uri mAggDataUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800115 private ContentResolver mResolver;
116 private ViewAdapter mAdapter;
117 private int mNumPhoneNumbers = 0;
118
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700119 /**
120 * A list of distinct contact IDs included in the current aggregate.
121 */
122 private ArrayList<Long> mContactIds = new ArrayList<Long>();
123
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800124 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
125 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
126 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
127 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
128 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
129 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700130 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800131 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
132 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
133
134 private Cursor mCursor;
135 private boolean mObserverRegistered;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700136
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800137 private ContentObserver mObserver = new ContentObserver(new Handler()) {
138 @Override
139 public boolean deliverSelfNotifications() {
140 return true;
141 }
142
143 @Override
144 public void onChange(boolean selfChange) {
145 if (mCursor != null && !mCursor.isClosed()){
146 dataChanged();
147 }
148 }
149 };
150
151 public void onClick(DialogInterface dialog, int which) {
152 if (mCursor != null) {
153 if (mObserverRegistered) {
154 mCursor.unregisterContentObserver(mObserver);
155 mObserverRegistered = false;
156 }
157 mCursor.close();
158 mCursor = null;
159 }
160 getContentResolver().delete(mUri, null, null);
161 finish();
162 }
163
164 public void onClick(View view) {
165 if (!mObserverRegistered) {
166 return;
167 }
168 switch (view.getId()) {
169 case R.id.star: {
Evan Millar7e4accf2009-06-08 10:43:26 -0700170 mCursor.moveToFirst();
Evan Millar66388be2009-05-28 15:41:07 -0700171 int oldStarredState = mCursor.getInt(AGGREGATE_STARRED_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800172 ContentValues values = new ContentValues(1);
Evan Millar66388be2009-05-28 15:41:07 -0700173 values.put(Aggregates.STARRED, oldStarredState == 1 ? 0 : 1);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800174 getContentResolver().update(mUri, values, null, null);
175 break;
176 }
177 }
178 }
179
180 private TextView mNameView;
181 private TextView mPhoneticNameView; // may be null in some locales
182 private ImageView mPhotoView;
183 private int mNoPhotoResource;
184 private CheckBox mStarView;
185 private boolean mShowSmsLinksForAllPhones;
186
187 @Override
188 protected void onCreate(Bundle icicle) {
189 super.onCreate(icicle);
190
191 setContentView(R.layout.view_contact);
192 getListView().setOnCreateContextMenuListener(this);
193
194 mNameView = (TextView) findViewById(R.id.name);
195 mPhoneticNameView = (TextView) findViewById(R.id.phonetic_name);
196 mPhotoView = (ImageView) findViewById(R.id.photo);
197 mStarView = (CheckBox) findViewById(R.id.star);
198 mStarView.setOnClickListener(this);
199
200 // Set the photo with a random "no contact" image
201 long now = SystemClock.elapsedRealtime();
202 int num = (int) now & 0xf;
203 if (num < 9) {
204 // Leaning in from right, common
205 mNoPhotoResource = R.drawable.ic_contact_picture;
206 } else if (num < 14) {
207 // Leaning in from left uncommon
208 mNoPhotoResource = R.drawable.ic_contact_picture_2;
209 } else {
210 // Coming in from the top, rare
211 mNoPhotoResource = R.drawable.ic_contact_picture_3;
212 }
213
Evan Millar45e0ed32009-06-01 16:44:38 -0700214 mUri = getIntent().getData();
215 mAggDataUri = Uri.withAppendedPath(mUri, "data");
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800216 mResolver = getContentResolver();
217
218 // Build the list of sections. The order they're added to mSections dictates the
219 // order they are displayed in the list.
220 mSections.add(mPhoneEntries);
221 mSections.add(mSmsEntries);
222 mSections.add(mEmailEntries);
223 mSections.add(mImEntries);
224 mSections.add(mPostalEntries);
225 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700226 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800227 mSections.add(mOtherEntries);
228
229 //TODO Read this value from a preference
230 mShowSmsLinksForAllPhones = true;
231
Evan Millar45e0ed32009-06-01 16:44:38 -0700232 mCursor = mResolver.query(mAggDataUri,
233 AGGREGATE_PROJECTION, null, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800234 }
235
236 @Override
237 protected void onResume() {
238 super.onResume();
239 mObserverRegistered = true;
240 mCursor.registerContentObserver(mObserver);
241 dataChanged();
242 }
243
244 @Override
245 protected void onPause() {
246 super.onPause();
247 if (mCursor != null) {
248 if (mObserverRegistered) {
249 mObserverRegistered = false;
250 mCursor.unregisterContentObserver(mObserver);
251 }
252 mCursor.deactivate();
253 }
254 }
255
256 @Override
257 protected void onDestroy() {
258 super.onDestroy();
259
260 if (mCursor != null) {
261 if (mObserverRegistered) {
262 mCursor.unregisterContentObserver(mObserver);
263 mObserverRegistered = false;
264 }
265 mCursor.close();
266 }
267 }
268
269 @Override
270 protected Dialog onCreateDialog(int id) {
271 switch (id) {
272 case DIALOG_CONFIRM_DELETE:
273 return new AlertDialog.Builder(this)
274 .setTitle(R.string.deleteConfirmation_title)
275 .setIcon(android.R.drawable.ic_dialog_alert)
276 .setMessage(R.string.deleteConfirmation)
277 .setNegativeButton(android.R.string.cancel, null)
278 .setPositiveButton(android.R.string.ok, this)
279 .setCancelable(false)
280 .create();
281 }
282 return null;
283 }
284
285 private void dataChanged() {
286 mCursor.requery();
287 if (mCursor.moveToFirst()) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800288 // Set the star
Evan Millar66388be2009-05-28 15:41:07 -0700289 mStarView.setChecked(mCursor.getInt(AGGREGATE_STARRED_COLUMN) == 1 ? true : false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800290
Evan Millar2c1cc832009-07-13 11:08:06 -0700291 //Set the photo
292 int photoId = mCursor.getInt(AGGREGATE_PHOTO_ID);
293 Bitmap photoBitmap = ContactsUtils.loadContactPhoto(
294 this, photoId, null);
295 if (photoBitmap == null) {
296 photoBitmap = ContactsUtils.loadPlaceholderPhoto(mNoPhotoResource, this, null);
297 }
298 mPhotoView.setImageBitmap(photoBitmap);
299
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800300 // Build up the contact entries
301 buildEntries(mCursor);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700302
303 // Collapse similar data items in select sections.
304 Collapser.collapseList(mPhoneEntries);
305 Collapser.collapseList(mSmsEntries);
306 Collapser.collapseList(mEmailEntries);
307 Collapser.collapseList(mPostalEntries);
308
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800309 if (mAdapter == null) {
310 mAdapter = new ViewAdapter(this, mSections);
311 setListAdapter(mAdapter);
312 } else {
313 mAdapter.setSections(mSections, SHOW_SEPARATORS);
314 }
315 } else {
316 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
317 Log.e(TAG, "invalid contact uri: " + mUri);
318 finish();
319 }
320 }
321
322 @Override
323 public boolean onCreateOptionsMenu(Menu menu) {
324 menu.add(0, 0, 0, R.string.menu_editContact)
325 .setIcon(android.R.drawable.ic_menu_edit)
326 .setIntent(new Intent(Intent.ACTION_EDIT, mUri))
327 .setAlphabeticShortcut('e');
328 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
329 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700330 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
331 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700332 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
333 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700334 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
335 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800336 return true;
337 }
338
339 @Override
340 public boolean onPrepareOptionsMenu(Menu menu) {
341 super.onPrepareOptionsMenu(menu);
342 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
343 // could be installed or uninstalled at any time.
344 if (isBarcodeScannerInstalled()) {
345 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
346 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
347 .setIcon(R.drawable.ic_menu_show_barcode);
348 }
349 } else {
350 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
351 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700352
353 boolean isAggregate = mContactIds.size() > 1;
354 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800355 return true;
356 }
357
358 private boolean isBarcodeScannerInstalled() {
359 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
360 ResolveInfo ri = getPackageManager().resolveActivity(intent,
361 PackageManager.MATCH_DEFAULT_ONLY);
362 return ri != null;
363 }
364
365 @Override
366 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
367 AdapterView.AdapterContextMenuInfo info;
368 try {
369 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
370 } catch (ClassCastException e) {
371 Log.e(TAG, "bad menuInfo", e);
372 return;
373 }
374
375 // This can be null sometimes, don't crash...
376 if (info == null) {
377 Log.e(TAG, "bad menuInfo");
378 return;
379 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700380
Evan Millar45e0ed32009-06-01 16:44:38 -0700381 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
382 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
383 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
384 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.auxIntent);
385 if (entry.primaryIcon == -1) {
386 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800387 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700388 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
389 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
390 if (entry.primaryIcon == -1) {
391 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800392 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700393 } else if (entry.mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)) {
394 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
395 }
396 // TODO(emillar): add back with group support.
397 /* else if (entry.mimetype.equals()) {
398 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
399 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800400 }
401
402 @Override
403 public boolean onOptionsItemSelected(MenuItem item) {
404 switch (item.getItemId()) {
405 case MENU_ITEM_DELETE: {
406 // Get confirmation
407 showDialog(DIALOG_CONFIRM_DELETE);
408 return true;
409 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700410
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700411 case MENU_ITEM_SPLIT_AGGREGATE: {
412 showSplitAggregateDialog();
413 return true;
414 }
415
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700416 case MENU_ITEM_JOIN_AGGREGATE: {
417 showJoinAggregateActivity();
418 return true;
419 }
420
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700421 case MENU_ITEM_OPTIONS: {
422 showOptionsActivity();
423 return true;
424 }
425
Evan Millar66388be2009-05-28 15:41:07 -0700426 // TODO(emillar) Bring this back.
427 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800428 if (mCursor.moveToFirst()) {
429 Intent intent = new Intent(SHOW_BARCODE_INTENT);
430 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
431 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700432 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800433 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700434 // Correctly handle when section headers are hidden
435 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700436
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800437 bundle.putString(Contacts.Intents.Insert.NAME, name);
438 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700439 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800440 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700441 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800442 bundle.putString(PHONE_KEYS[x], entry.data);
443 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700444 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800445 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700446 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800447 bundle.putString(EMAIL_KEYS[x], entry.data);
448 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700449 if (mPostalEntries.size() >= 1 + sepAdjust) {
450 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800451 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
452 }
453 intent.putExtra("ENCODE_DATA", bundle);
454 try {
455 startActivity(intent);
456 } catch (ActivityNotFoundException e) {
457 // The check in onPrepareOptionsMenu() should make this impossible, but
458 // for safety I'm catching the exception rather than crashing. Ideally
459 // I'd call Menu.removeItem() here too, but I don't see a way to get
460 // the options menu.
461 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
462 "was not installed.");
463 }
464 return true;
465 }
466 }
Evan Millar66388be2009-05-28 15:41:07 -0700467 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800468 }
469 return super.onOptionsItemSelected(item);
470 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700471
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800472 @Override
473 public boolean onContextItemSelected(MenuItem item) {
474 switch (item.getItemId()) {
475 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700476 if (makeItemDefault(item)) {
477 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800478 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700479 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800480 }
481 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700482
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800483 return super.onContextItemSelected(item);
484 }
485
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700486 private boolean makeItemDefault(MenuItem item) {
487 ViewEntry entry = getViewEntryForMenuItem(item);
488 if (entry == null) {
489 return false;
490 }
491
492 // Update the primary values in the data record.
493 ContentValues values = new ContentValues(2);
494 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700495
Evan Millar54a5c9f2009-06-23 17:41:09 -0700496 if (entry.ids.size() > 0) {
497 for (int i = 0; i < entry.ids.size(); i++) {
498 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
499 entry.ids.get(i)),
500 values, null, null);
501 }
502 }
503
504 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700505 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
506 values, null, null);
507 dataChanged();
508 return true;
509 }
510
511 /**
512 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
513 * The user picks a contact to be split into its own aggregate or clicks Cancel.
514 */
515 private void showSplitAggregateDialog() {
516
517 // Wrap this dialog in a specific theme so that list items have correct text color.
518 final ContextThemeWrapper dialogContext =
519 new ContextThemeWrapper(this, android.R.style.Theme_Light);
520 AlertDialog.Builder builder =
521 new AlertDialog.Builder(dialogContext);
522 builder.setTitle(getString(R.string.splitAggregate_title));
523
524 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
525 builder.setView(view);
526
527 builder.setInverseBackgroundForced(true);
528 builder.setCancelable(true);
529 builder.setNegativeButton(android.R.string.cancel,
530 new OnClickListener() {
531 public void onClick(DialogInterface dialog, int which) {
532 dialog.dismiss();
533 }
534 });
535 final AlertDialog dialog = builder.create();
536
537 view.setOnContactSelectedListener(new OnContactSelectedListener() {
538 public void onContactSelected(long contactId) {
539 dialog.dismiss();
540 splitContact(contactId);
541 }
542 });
543
544 dialog.show();
545 }
546
547 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700548 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700549 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700550 public void showJoinAggregateActivity() {
551 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
552 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
553 startActivityForResult(intent, REQUEST_JOIN_AGGREGATE);
554 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700555
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700556 @Override
557 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
558 if (requestCode == REQUEST_JOIN_AGGREGATE && resultCode == RESULT_OK && intent != null) {
559 final long aggregateId = ContentUris.parseId(intent.getData());
560 joinAggregate(aggregateId);
561 }
562 }
563
564 private void splitContact(long contactId) {
565 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_OUT);
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700566 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700567 mAdapter.notifyDataSetChanged();
568 }
569
570 private void joinAggregate(final long aggregateId) {
571 Cursor c = mResolver.query(Contacts.CONTENT_URI, new String[] {Contacts._ID},
572 Contacts.AGGREGATE_ID + "=" + aggregateId, null, null);
573
574 try {
575 while(c.moveToNext()) {
576 long contactId = c.getLong(0);
577 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
578 }
579 } finally {
580 c.close();
581 }
582
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700583 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700584 mAdapter.notifyDataSetChanged();
585 }
586
587 /**
588 * Given a contact ID sets an aggregation exception to either join the contact with the
589 * current aggregate or split off.
590 */
591 protected void setAggregationException(long contactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700592 ContentValues values = new ContentValues(3);
593 values.put(AggregationExceptions.AGGREGATE_ID, ContentUris.parseId(mUri));
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700594 values.put(AggregationExceptions.CONTACT_ID, contactId);
595 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700596 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700597 }
598
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700599 private void showOptionsActivity() {
600 final Intent intent = new Intent(this, ContactOptionsActivity.class);
601 intent.setData(mUri);
602 startActivity(intent);
603 }
604
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700605 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
606 AdapterView.AdapterContextMenuInfo info;
607 try {
608 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
609 } catch (ClassCastException e) {
610 Log.e(TAG, "bad menuInfo", e);
611 return null;
612 }
613
614 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
615 }
616
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800617 @Override
618 public boolean onKeyDown(int keyCode, KeyEvent event) {
619 switch (keyCode) {
620 case KeyEvent.KEYCODE_CALL: {
621 try {
622 ITelephony phone = ITelephony.Stub.asInterface(
623 ServiceManager.checkService("phone"));
624 if (phone != null && !phone.isIdle()) {
625 // Skip out and let the key be handled at a higher level
626 break;
627 }
628 } catch (RemoteException re) {
629 // Fall through and try to call the contact
630 }
631
632 int index = getListView().getSelectedItemPosition();
633 if (index != -1) {
634 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700635 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
636 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800637 }
638 } else if (mNumPhoneNumbers != 0) {
639 // There isn't anything selected, call the default number
640 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
641 startActivity(intent);
642 }
643 return true;
644 }
645
646 case KeyEvent.KEYCODE_DEL: {
647 showDialog(DIALOG_CONFIRM_DELETE);
648 return true;
649 }
650 }
651
652 return super.onKeyDown(keyCode, event);
653 }
654
655 @Override
656 protected void onListItemClick(ListView l, View v, int position, long id) {
657 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
658 if (entry != null) {
659 Intent intent = entry.intent;
660 if (intent != null) {
661 try {
662 startActivity(intent);
663 } catch (ActivityNotFoundException e) {
664 Log.e(TAG, "No activity found for intent: " + intent);
665 signalError();
666 }
667 } else {
668 signalError();
669 }
670 } else {
671 signalError();
672 }
673 }
674
675 /**
676 * Signal an error to the user via a beep, or some other method.
677 */
678 private void signalError() {
679 //TODO: implement this when we have the sonification APIs
680 }
681
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800682 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700683 // 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 -0800684 StringBuilder buf = new StringBuilder("imto://");
685 buf.append(host);
686 buf.append('/');
687 buf.append(data);
688 return Uri.parse(buf.toString());
689 }
690
691 /**
692 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700693 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800694 * @param personCursor the URI for the contact being displayed
695 */
Evan Millar66388be2009-05-28 15:41:07 -0700696 private final void buildEntries(Cursor aggCursor) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800697 // Clear out the old entries
698 final int numSections = mSections.size();
699 for (int i = 0; i < numSections; i++) {
700 mSections.get(i).clear();
701 }
702
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700703 mContactIds.clear();
704
Evan Millar66388be2009-05-28 15:41:07 -0700705 // Build up method entries
706 if (mUri != null) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700707 Bitmap photoBitmap = null;
Evan Millar66388be2009-05-28 15:41:07 -0700708 while (aggCursor.moveToNext()) {
709 final String mimetype = aggCursor.getString(DATA_MIMETYPE_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700710
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800711 ViewEntry entry = new ViewEntry();
Evan Millar66388be2009-05-28 15:41:07 -0700712
713 final long id = aggCursor.getLong(DATA_ID_COLUMN);
714 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800715 entry.id = id;
716 entry.uri = uri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700717 entry.mimetype = mimetype;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700718 entry.contactId = aggCursor.getLong(DATA_CONTACT_ID_COLUMN);
719 if (!mContactIds.contains(entry.contactId)) {
720 mContactIds.add(entry.contactId);
721 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800722
Evan Millar66388be2009-05-28 15:41:07 -0700723 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
Evan Millar5c22c3b2009-05-29 11:37:54 -0700724 || mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)
725 || mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)
Evan Millar66388be2009-05-28 15:41:07 -0700726 || mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
727 final int type = aggCursor.getInt(DATA_1_COLUMN);
Evan Millar45e0ed32009-06-01 16:44:38 -0700728 final String label = aggCursor.getString(DATA_3_COLUMN);
729 final String data = aggCursor.getString(DATA_2_COLUMN);
730 final boolean isSuperPrimary = "1".equals(
731 aggCursor.getString(DATA_IS_SUPER_PRIMARY_COLUMN));
Evan Millar66388be2009-05-28 15:41:07 -0700732
Evan Millar54a5c9f2009-06-23 17:41:09 -0700733 entry.type = type;
734
Evan Millar66388be2009-05-28 15:41:07 -0700735 // Don't crash if the data is bogus
736 if (TextUtils.isEmpty(data)) {
737 Log.w(TAG, "empty data for contact method " + id);
738 continue;
739 }
740
741 // Build phone entries
742 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
743 mNumPhoneNumbers++;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700744
Evan Millar66388be2009-05-28 15:41:07 -0700745 final CharSequence displayLabel = ContactsUtils.getDisplayLabel(
746 this, mimetype, type, label);
747 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700748 entry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar66388be2009-05-28 15:41:07 -0700749 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, entry.uri);
750 entry.auxIntent = new Intent(Intent.ACTION_SENDTO,
751 Uri.fromParts("sms", data, null));
Evan Millar45e0ed32009-06-01 16:44:38 -0700752 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700753 entry.primaryIcon = R.drawable.ic_default_number;
754 }
755 entry.actionIcon = android.R.drawable.sym_action_call;
756 mPhoneEntries.add(entry);
757
Evan Millar5c22c3b2009-05-29 11:37:54 -0700758 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700759 || mShowSmsLinksForAllPhones) {
760 // Add an SMS entry
761 ViewEntry smsEntry = new ViewEntry();
762 smsEntry.label = buildActionString(
763 R.string.actionText, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700764 smsEntry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar66388be2009-05-28 15:41:07 -0700765 smsEntry.id = id;
766 smsEntry.uri = uri;
767 smsEntry.intent = entry.auxIntent;
Evan Millar45e0ed32009-06-01 16:44:38 -0700768 smsEntry.mimetype = FastTrackWindow.MIME_SMS_ADDRESS;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700769 smsEntry.type = type;
Evan Millar66388be2009-05-28 15:41:07 -0700770 smsEntry.actionIcon = R.drawable.sym_action_sms;
771 mSmsEntries.add(smsEntry);
772 }
773 // Build email entries
774 } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800775 entry.label = buildActionString(R.string.actionEmail,
Evan Millar66388be2009-05-28 15:41:07 -0700776 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800777 entry.data = data;
778 entry.intent = new Intent(Intent.ACTION_SENDTO,
779 Uri.fromParts("mailto", data, null));
780 entry.actionIcon = android.R.drawable.sym_action_email;
Evan Millar45e0ed32009-06-01 16:44:38 -0700781 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700782 entry.primaryIcon = R.drawable.ic_default_number;
783 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800784 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700785 // Build postal entries
786 } else if (mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800787 entry.label = buildActionString(R.string.actionMap,
Evan Millar66388be2009-05-28 15:41:07 -0700788 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800789 entry.data = data;
790 entry.maxLines = 4;
791 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
792 entry.actionIcon = R.drawable.sym_action_map;
793 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700794 // Build im entries
795 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
796 String[] protocolStrings = getResources().getStringArray(
797 android.R.array.imProtocols);
798 Object protocolObj = ContactsUtils.decodeImProtocol(
799 aggCursor.getString(DATA_5_COLUMN));
Alex Kennberg87fc3172009-03-28 06:43:06 -0700800 String host = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800801 if (protocolObj instanceof Number) {
802 int protocol = ((Number) protocolObj).intValue();
803 entry.label = buildActionString(R.string.actionChat,
804 protocolStrings[protocol], false);
Evan Millar66388be2009-05-28 15:41:07 -0700805 host = ContactsUtils.lookupProviderNameFromId(
806 protocol).toLowerCase();
807 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
808 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800809 entry.maxLabelLines = 2;
810 }
Alex Kennberg87fc3172009-03-28 06:43:06 -0700811 } else if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800812 String providerName = (String) protocolObj;
813 entry.label = buildActionString(R.string.actionChat,
814 providerName, false);
815 host = providerName.toLowerCase();
816 }
817
818 // Only add the intent if there is a valid host
819 if (!TextUtils.isEmpty(host)) {
820 entry.intent = new Intent(Intent.ACTION_SENDTO,
821 constructImToUrl(host, data));
822 }
823 entry.data = data;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700824 //TODO(emillar) Add in presence info
Evan Millar66388be2009-05-28 15:41:07 -0700825 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800826 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700827 aggCursor.getInt(METHODS_STATUS_COLUMN));
Evan Millar54a5c9f2009-06-23 17:41:09 -0700828 entry.status = ...
Evan Millar66388be2009-05-28 15:41:07 -0700829 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800830 entry.actionIcon = android.R.drawable.sym_action_chat;
831 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700832 }
Evan Millar7e4accf2009-06-08 10:43:26 -0700833 // Set the name
834 } else if (mimetype.equals(CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
835 String name = mCursor.getString(DATA_9_COLUMN);
836 if (TextUtils.isEmpty(name)) {
837 mNameView.setText(getText(android.R.string.unknownName));
838 } else {
839 mNameView.setText(name);
840 }
841 /*
842 if (mPhoneticNameView != null) {
843 String phoneticName = mCursor.getString(CONTACT_PHONETIC_NAME_COLUMN);
844 mPhoneticNameView.setText(phoneticName);
845 }
846 */
Evan Millar66388be2009-05-28 15:41:07 -0700847 // Build organization entries
848 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
Evan Millar66388be2009-05-28 15:41:07 -0700849 final String company = aggCursor.getString(DATA_3_COLUMN);
850 final String title = aggCursor.getString(DATA_4_COLUMN);
851 final boolean isPrimary = "1".equals(aggCursor.getString(DATA_5_COLUMN));
852
853 if (isPrimary) {
854 entry.primaryIcon = R.drawable.ic_default_number;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800855 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700856
Evan Millar66388be2009-05-28 15:41:07 -0700857 // Don't crash if the data is bogus
858 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
859 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800860 continue;
861 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700862
Evan Millar66388be2009-05-28 15:41:07 -0700863 entry.data = title;
864 entry.actionIcon = R.drawable.sym_action_organization;
865 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800866
Evan Millar66388be2009-05-28 15:41:07 -0700867 mOrganizationEntries.add(entry);
868 // Build note entries
869 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
870 entry.label = getString(R.string.label_notes);
871 entry.data = aggCursor.getString(DATA_1_COLUMN);
872 entry.id = 0;
873 entry.uri = null;
874 entry.intent = null;
875 entry.maxLines = 10;
876 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800877
Evan Millar66388be2009-05-28 15:41:07 -0700878 if (TextUtils.isEmpty(entry.data)) {
879 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700880 continue;
881 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700882
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800883 mOtherEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700884 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700885
Evan Millar45e0ed32009-06-01 16:44:38 -0700886
Evan Millar66388be2009-05-28 15:41:07 -0700887 // TODO(emillar) Add group entries
888// // Build the group entries
889// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
890// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
891// null, null, Groups.DEFAULT_SORT_ORDER);
892// if (groupCursor != null) {
893// try {
894// StringBuilder sb = new StringBuilder();
895//
896// while (groupCursor.moveToNext()) {
897// String systemId = groupCursor.getString(
898// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
899//
900// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
901// continue;
902// }
903//
904// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
905// if (!TextUtils.isEmpty(name)) {
906// if (sb.length() == 0) {
907// sb.append(name);
908// } else {
909// sb.append(getString(R.string.group_list, name));
910// }
911// }
912// }
913//
914// if (sb.length() > 0) {
915// ViewEntry entry = new ViewEntry();
916// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
917// entry.label = getString(R.string.label_groups);
918// entry.data = sb.toString();
919// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
920//
921// // TODO: Add an icon for the groups item.
922//
923// mGroupEntries.add(entry);
924// }
925// } finally {
926// groupCursor.close();
927// }
928// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700929
Evan Millar66388be2009-05-28 15:41:07 -0700930 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800931 }
932 }
933
934 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700935 // If there is no type just display an empty string
936 if (type == null) {
937 type = "";
938 }
939
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800940 if (lowerCase) {
941 return getString(actionResId, type.toString().toLowerCase());
942 } else {
943 return getString(actionResId, type.toString());
944 }
945 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700946
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800947 /**
948 * A basic structure with the data for a contact entry in the list.
949 */
Evan Millar54a5c9f2009-06-23 17:41:09 -0700950 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800951 public int primaryIcon = -1;
952 public Intent intent;
953 public Intent auxIntent = null;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700954 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800955 public int actionIcon = -1;
956 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700957 public ArrayList<Long> ids = new ArrayList<Long>();
958 public int collapseCount = 0;
959
960 public boolean collapseWith(ViewEntry entry) {
961 // assert equal collapse keys
962 if (!getCollapseKey().equals(entry.getCollapseKey())) {
963 return false;
964 }
965
966 // Choose the label associated with the highest type precedence.
967 if (TypePrecedence.getTypePrecedence(mimetype, type)
968 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
969 type = entry.type;
970 label = entry.label;
971 }
972
973 // Choose the max of the maxLines and maxLabelLines values.
974 maxLines = Math.max(maxLines, entry.maxLines);
975 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
976
977 // Choose the presence with the highest precedence.
978 if (Presence.getPresencePrecedence(status)
979 < Presence.getPresencePrecedence(entry.status)) {
980 status = entry.status;
981 }
982
983 // If any of the collapsed entries are primary make the whole thing primary.
984 if (primaryIcon != entry.primaryIcon && primaryIcon == -1) {
985 primaryIcon = entry.primaryIcon;
986 }
987
988 // uri, and contactdId, shouldn't make a difference. Just keep the original.
989
990 // Keep track of all the ids that have been collapsed with this one.
991 ids.add(entry.id);
992 collapseCount++;
993 return true;
994 }
995
996 public String getCollapseKey() {
997 StringBuilder hashSb = new StringBuilder();
998 hashSb.append(data);
999 hashSb.append(mimetype);
1000 hashSb.append((intent != null && intent.getAction() != null)
1001 ? intent.getAction() : "");
1002 hashSb.append((auxIntent != null && auxIntent.getAction() != null)
1003 ? auxIntent.getAction() : "");
1004 hashSb.append(actionIcon);
1005 return hashSb.toString();
1006 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001007 }
1008
1009 private static final class ViewAdapter extends ContactEntryAdapter<ViewEntry> {
1010 /** Cache of the children views of a row */
1011 static class ViewCache {
1012 public TextView label;
1013 public TextView data;
1014 public ImageView actionIcon;
1015 public ImageView presenceIcon;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001016
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001017 // Need to keep track of this too
1018 ViewEntry entry;
1019 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001020
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001021 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1022 super(context, sections, SHOW_SEPARATORS);
1023 }
1024
1025 @Override
1026 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001027 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001028 View v;
1029
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001030 ViewCache views;
1031
1032 // Check to see if we can reuse convertView
1033 if (convertView != null) {
1034 v = convertView;
1035 views = (ViewCache) v.getTag();
1036 } else {
1037 // Create a new view if needed
1038 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1039
1040 // Cache the children
1041 views = new ViewCache();
1042 views.label = (TextView) v.findViewById(android.R.id.text1);
1043 views.data = (TextView) v.findViewById(android.R.id.text2);
1044 views.actionIcon = (ImageView) v.findViewById(R.id.icon1);
1045 views.presenceIcon = (ImageView) v.findViewById(R.id.icon2);
1046 v.setTag(views);
1047 }
1048
1049 // Update the entry in the view cache
1050 views.entry = entry;
1051
1052 // Bind the data to the view
1053 bindView(v, entry);
1054 return v;
1055 }
1056
1057 @Override
1058 protected View newView(int position, ViewGroup parent) {
1059 // getView() handles this
1060 throw new UnsupportedOperationException();
1061 }
1062
1063 @Override
1064 protected void bindView(View view, ViewEntry entry) {
1065 final Resources resources = mContext.getResources();
1066 ViewCache views = (ViewCache) view.getTag();
1067
1068 // Set the label
1069 TextView label = views.label;
1070 setMaxLines(label, entry.maxLabelLines);
1071 label.setText(entry.label);
1072
1073 // Set the data
1074 TextView data = views.data;
1075 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001076 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1077 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1078 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1079 } else {
1080 data.setText(entry.data);
1081 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001082 setMaxLines(data, entry.maxLines);
1083 }
1084
1085 // Set the action icon
1086 ImageView action = views.actionIcon;
1087 if (entry.actionIcon != -1) {
1088 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1089 action.setVisibility(View.VISIBLE);
1090 } else {
1091 // Things should still line up as if there was an icon, so make it invisible
1092 action.setVisibility(View.INVISIBLE);
1093 }
1094
1095 // Set the presence icon
1096 Drawable presenceIcon = null;
1097 if (entry.primaryIcon != -1) {
1098 presenceIcon = resources.getDrawable(entry.primaryIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001099 } else if (entry.status != -1) {
1100 presenceIcon = resources.getDrawable(
1101 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001102 }
1103
1104 ImageView presence = views.presenceIcon;
1105 if (presenceIcon != null) {
1106 presence.setImageDrawable(presenceIcon);
1107 presence.setVisibility(View.VISIBLE);
1108 } else {
1109 presence.setVisibility(View.GONE);
1110 }
1111 }
1112
1113 private void setMaxLines(TextView textView, int maxLines) {
1114 if (maxLines == 1) {
1115 textView.setSingleLine(true);
1116 textView.setEllipsize(TextUtils.TruncateAt.END);
1117 } else {
1118 textView.setSingleLine(false);
1119 textView.setMaxLines(maxLines);
1120 textView.setEllipsize(null);
1121 }
1122 }
1123 }
1124}