blob: 6414748da58edfefbd326fb1ff8da537b047b87e [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts;
18
Evan Millar66388be2009-05-28 15:41:07 -070019import static com.android.contacts.ContactEntryAdapter.AGGREGATE_PROJECTION;
20import static com.android.contacts.ContactEntryAdapter.AGGREGATE_STARRED_COLUMN;
Evan Millar66388be2009-05-28 15:41:07 -070021import static com.android.contacts.ContactEntryAdapter.DATA_1_COLUMN;
22import static com.android.contacts.ContactEntryAdapter.DATA_2_COLUMN;
23import static com.android.contacts.ContactEntryAdapter.DATA_3_COLUMN;
24import static com.android.contacts.ContactEntryAdapter.DATA_4_COLUMN;
25import static com.android.contacts.ContactEntryAdapter.DATA_5_COLUMN;
Evan Millar66388be2009-05-28 15:41:07 -070026import static com.android.contacts.ContactEntryAdapter.DATA_9_COLUMN;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070027import static com.android.contacts.ContactEntryAdapter.DATA_CONTACT_ID_COLUMN;
28import static com.android.contacts.ContactEntryAdapter.DATA_ID_COLUMN;
29import static com.android.contacts.ContactEntryAdapter.DATA_IS_SUPER_PRIMARY_COLUMN;
30import static com.android.contacts.ContactEntryAdapter.DATA_MIMETYPE_COLUMN;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080031
Evan Millar54a5c9f2009-06-23 17:41:09 -070032import com.android.contacts.Collapser.Collapsible;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070033import com.android.contacts.SplitAggregateView.OnContactSelectedListener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080034import com.android.internal.telephony.ITelephony;
35
36import android.app.AlertDialog;
37import android.app.Dialog;
38import android.app.ListActivity;
39import android.content.ActivityNotFoundException;
40import android.content.ContentResolver;
41import android.content.ContentUris;
42import android.content.ContentValues;
43import android.content.Context;
44import android.content.DialogInterface;
45import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070046import android.content.DialogInterface.OnClickListener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080047import android.content.pm.PackageManager;
48import android.content.pm.ResolveInfo;
49import android.content.res.Resources;
50import android.database.ContentObserver;
51import android.database.Cursor;
Evan Millar45e0ed32009-06-01 16:44:38 -070052import android.graphics.Bitmap;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080053import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080054import android.net.Uri;
55import android.os.Bundle;
56import android.os.Handler;
57import android.os.RemoteException;
58import android.os.ServiceManager;
59import android.os.SystemClock;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -070060import android.provider.ContactsContract.Contacts;
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;
66import android.provider.ContactsContract.CommonDataKinds.Phone;
67import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080068import android.text.TextUtils;
69import android.util.Log;
70import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070071import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080072import android.view.KeyEvent;
73import android.view.Menu;
74import android.view.MenuItem;
75import android.view.View;
76import android.view.ViewGroup;
77import android.view.ContextMenu.ContextMenuInfo;
78import android.widget.AdapterView;
79import android.widget.CheckBox;
80import android.widget.ImageView;
81import android.widget.ListView;
82import android.widget.TextView;
83import android.widget.Toast;
84
85import java.util.ArrayList;
Evan Millar54a5c9f2009-06-23 17:41:09 -070086import java.util.Collection;
87import java.util.HashMap;
88import java.util.Iterator;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080089
90/**
91 * Displays the details of a specific contact.
92 */
Evan Millar5c22c3b2009-05-29 11:37:54 -070093public class ViewContactActivity extends ListActivity
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080094 implements View.OnCreateContextMenuListener, View.OnClickListener,
95 DialogInterface.OnClickListener {
96 private static final String TAG = "ViewContact";
97 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
98
99 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800100
101 private static final int DIALOG_CONFIRM_DELETE = 1;
102
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700103 private static final int REQUEST_JOIN_AGGREGATE = 1;
104
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800105 public static final int MENU_ITEM_DELETE = 1;
106 public static final int MENU_ITEM_MAKE_DEFAULT = 2;
107 public static final int MENU_ITEM_SHOW_BARCODE = 3;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700108 public static final int MENU_ITEM_SPLIT_AGGREGATE = 4;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700109 public static final int MENU_ITEM_JOIN_AGGREGATE = 5;
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700110 public static final int MENU_ITEM_OPTIONS = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800111
112 private Uri mUri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700113 private Uri mAggDataUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800114 private ContentResolver mResolver;
115 private ViewAdapter mAdapter;
116 private int mNumPhoneNumbers = 0;
117
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700118 /**
119 * A list of distinct contact IDs included in the current aggregate.
120 */
121 private ArrayList<Long> mContactIds = new ArrayList<Long>();
122
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800123 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
124 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
125 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
126 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
127 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
128 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700129 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800130 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
131 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
132
133 private Cursor mCursor;
134 private boolean mObserverRegistered;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700135
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800136 private ContentObserver mObserver = new ContentObserver(new Handler()) {
137 @Override
138 public boolean deliverSelfNotifications() {
139 return true;
140 }
141
142 @Override
143 public void onChange(boolean selfChange) {
144 if (mCursor != null && !mCursor.isClosed()){
145 dataChanged();
146 }
147 }
148 };
149
150 public void onClick(DialogInterface dialog, int which) {
151 if (mCursor != null) {
152 if (mObserverRegistered) {
153 mCursor.unregisterContentObserver(mObserver);
154 mObserverRegistered = false;
155 }
156 mCursor.close();
157 mCursor = null;
158 }
159 getContentResolver().delete(mUri, null, null);
160 finish();
161 }
162
163 public void onClick(View view) {
164 if (!mObserverRegistered) {
165 return;
166 }
167 switch (view.getId()) {
168 case R.id.star: {
Evan Millar7e4accf2009-06-08 10:43:26 -0700169 mCursor.moveToFirst();
Evan Millar66388be2009-05-28 15:41:07 -0700170 int oldStarredState = mCursor.getInt(AGGREGATE_STARRED_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800171 ContentValues values = new ContentValues(1);
Evan Millar66388be2009-05-28 15:41:07 -0700172 values.put(Aggregates.STARRED, oldStarredState == 1 ? 0 : 1);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800173 getContentResolver().update(mUri, values, null, null);
174 break;
175 }
176 }
177 }
178
179 private TextView mNameView;
180 private TextView mPhoneticNameView; // may be null in some locales
181 private ImageView mPhotoView;
182 private int mNoPhotoResource;
183 private CheckBox mStarView;
184 private boolean mShowSmsLinksForAllPhones;
185
186 @Override
187 protected void onCreate(Bundle icicle) {
188 super.onCreate(icicle);
189
190 setContentView(R.layout.view_contact);
191 getListView().setOnCreateContextMenuListener(this);
192
193 mNameView = (TextView) findViewById(R.id.name);
194 mPhoneticNameView = (TextView) findViewById(R.id.phonetic_name);
195 mPhotoView = (ImageView) findViewById(R.id.photo);
196 mStarView = (CheckBox) findViewById(R.id.star);
197 mStarView.setOnClickListener(this);
198
199 // Set the photo with a random "no contact" image
200 long now = SystemClock.elapsedRealtime();
201 int num = (int) now & 0xf;
202 if (num < 9) {
203 // Leaning in from right, common
204 mNoPhotoResource = R.drawable.ic_contact_picture;
205 } else if (num < 14) {
206 // Leaning in from left uncommon
207 mNoPhotoResource = R.drawable.ic_contact_picture_2;
208 } else {
209 // Coming in from the top, rare
210 mNoPhotoResource = R.drawable.ic_contact_picture_3;
211 }
212
Evan Millar45e0ed32009-06-01 16:44:38 -0700213 mUri = getIntent().getData();
214 mAggDataUri = Uri.withAppendedPath(mUri, "data");
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800215 mResolver = getContentResolver();
216
217 // Build the list of sections. The order they're added to mSections dictates the
218 // order they are displayed in the list.
219 mSections.add(mPhoneEntries);
220 mSections.add(mSmsEntries);
221 mSections.add(mEmailEntries);
222 mSections.add(mImEntries);
223 mSections.add(mPostalEntries);
224 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700225 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800226 mSections.add(mOtherEntries);
227
228 //TODO Read this value from a preference
229 mShowSmsLinksForAllPhones = true;
230
Evan Millar45e0ed32009-06-01 16:44:38 -0700231 mCursor = mResolver.query(mAggDataUri,
232 AGGREGATE_PROJECTION, null, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800233 }
234
235 @Override
236 protected void onResume() {
237 super.onResume();
238 mObserverRegistered = true;
239 mCursor.registerContentObserver(mObserver);
240 dataChanged();
241 }
242
243 @Override
244 protected void onPause() {
245 super.onPause();
246 if (mCursor != null) {
247 if (mObserverRegistered) {
248 mObserverRegistered = false;
249 mCursor.unregisterContentObserver(mObserver);
250 }
251 mCursor.deactivate();
252 }
253 }
254
255 @Override
256 protected void onDestroy() {
257 super.onDestroy();
258
259 if (mCursor != null) {
260 if (mObserverRegistered) {
261 mCursor.unregisterContentObserver(mObserver);
262 mObserverRegistered = false;
263 }
264 mCursor.close();
265 }
266 }
267
268 @Override
269 protected Dialog onCreateDialog(int id) {
270 switch (id) {
271 case DIALOG_CONFIRM_DELETE:
272 return new AlertDialog.Builder(this)
273 .setTitle(R.string.deleteConfirmation_title)
274 .setIcon(android.R.drawable.ic_dialog_alert)
275 .setMessage(R.string.deleteConfirmation)
276 .setNegativeButton(android.R.string.cancel, null)
277 .setPositiveButton(android.R.string.ok, this)
278 .setCancelable(false)
279 .create();
280 }
281 return null;
282 }
283
284 private void dataChanged() {
285 mCursor.requery();
286 if (mCursor.moveToFirst()) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800287 // Set the star
Evan Millar66388be2009-05-28 15:41:07 -0700288 mStarView.setChecked(mCursor.getInt(AGGREGATE_STARRED_COLUMN) == 1 ? true : false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800289
290 // Build up the contact entries
291 buildEntries(mCursor);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700292
293 // Collapse similar data items in select sections.
294 Collapser.collapseList(mPhoneEntries);
295 Collapser.collapseList(mSmsEntries);
296 Collapser.collapseList(mEmailEntries);
297 Collapser.collapseList(mPostalEntries);
298
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800299 if (mAdapter == null) {
300 mAdapter = new ViewAdapter(this, mSections);
301 setListAdapter(mAdapter);
302 } else {
303 mAdapter.setSections(mSections, SHOW_SEPARATORS);
304 }
305 } else {
306 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
307 Log.e(TAG, "invalid contact uri: " + mUri);
308 finish();
309 }
310 }
311
312 @Override
313 public boolean onCreateOptionsMenu(Menu menu) {
314 menu.add(0, 0, 0, R.string.menu_editContact)
315 .setIcon(android.R.drawable.ic_menu_edit)
316 .setIntent(new Intent(Intent.ACTION_EDIT, mUri))
317 .setAlphabeticShortcut('e');
318 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
319 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700320 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
321 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700322 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
323 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700324 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
325 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800326 return true;
327 }
328
329 @Override
330 public boolean onPrepareOptionsMenu(Menu menu) {
331 super.onPrepareOptionsMenu(menu);
332 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
333 // could be installed or uninstalled at any time.
334 if (isBarcodeScannerInstalled()) {
335 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
336 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
337 .setIcon(R.drawable.ic_menu_show_barcode);
338 }
339 } else {
340 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
341 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700342
343 boolean isAggregate = mContactIds.size() > 1;
344 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800345 return true;
346 }
347
348 private boolean isBarcodeScannerInstalled() {
349 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
350 ResolveInfo ri = getPackageManager().resolveActivity(intent,
351 PackageManager.MATCH_DEFAULT_ONLY);
352 return ri != null;
353 }
354
355 @Override
356 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
357 AdapterView.AdapterContextMenuInfo info;
358 try {
359 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
360 } catch (ClassCastException e) {
361 Log.e(TAG, "bad menuInfo", e);
362 return;
363 }
364
365 // This can be null sometimes, don't crash...
366 if (info == null) {
367 Log.e(TAG, "bad menuInfo");
368 return;
369 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700370
Evan Millar45e0ed32009-06-01 16:44:38 -0700371 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
372 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
373 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
374 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.auxIntent);
375 if (entry.primaryIcon == -1) {
376 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800377 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700378 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
379 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
380 if (entry.primaryIcon == -1) {
381 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800382 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700383 } else if (entry.mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)) {
384 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
385 }
386 // TODO(emillar): add back with group support.
387 /* else if (entry.mimetype.equals()) {
388 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
389 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800390 }
391
392 @Override
393 public boolean onOptionsItemSelected(MenuItem item) {
394 switch (item.getItemId()) {
395 case MENU_ITEM_DELETE: {
396 // Get confirmation
397 showDialog(DIALOG_CONFIRM_DELETE);
398 return true;
399 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700400
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700401 case MENU_ITEM_SPLIT_AGGREGATE: {
402 showSplitAggregateDialog();
403 return true;
404 }
405
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700406 case MENU_ITEM_JOIN_AGGREGATE: {
407 showJoinAggregateActivity();
408 return true;
409 }
410
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700411 case MENU_ITEM_OPTIONS: {
412 showOptionsActivity();
413 return true;
414 }
415
Evan Millar66388be2009-05-28 15:41:07 -0700416 // TODO(emillar) Bring this back.
417 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800418 if (mCursor.moveToFirst()) {
419 Intent intent = new Intent(SHOW_BARCODE_INTENT);
420 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
421 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700422 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800423 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700424 // Correctly handle when section headers are hidden
425 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700426
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800427 bundle.putString(Contacts.Intents.Insert.NAME, name);
428 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700429 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800430 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700431 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800432 bundle.putString(PHONE_KEYS[x], entry.data);
433 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700434 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800435 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700436 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800437 bundle.putString(EMAIL_KEYS[x], entry.data);
438 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700439 if (mPostalEntries.size() >= 1 + sepAdjust) {
440 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800441 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
442 }
443 intent.putExtra("ENCODE_DATA", bundle);
444 try {
445 startActivity(intent);
446 } catch (ActivityNotFoundException e) {
447 // The check in onPrepareOptionsMenu() should make this impossible, but
448 // for safety I'm catching the exception rather than crashing. Ideally
449 // I'd call Menu.removeItem() here too, but I don't see a way to get
450 // the options menu.
451 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
452 "was not installed.");
453 }
454 return true;
455 }
456 }
Evan Millar66388be2009-05-28 15:41:07 -0700457 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800458 }
459 return super.onOptionsItemSelected(item);
460 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700461
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800462 @Override
463 public boolean onContextItemSelected(MenuItem item) {
464 switch (item.getItemId()) {
465 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700466 if (makeItemDefault(item)) {
467 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800468 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700469 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800470 }
471 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700472
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800473 return super.onContextItemSelected(item);
474 }
475
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700476 private boolean makeItemDefault(MenuItem item) {
477 ViewEntry entry = getViewEntryForMenuItem(item);
478 if (entry == null) {
479 return false;
480 }
481
482 // Update the primary values in the data record.
483 ContentValues values = new ContentValues(2);
484 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700485
Evan Millar54a5c9f2009-06-23 17:41:09 -0700486 if (entry.ids.size() > 0) {
487 for (int i = 0; i < entry.ids.size(); i++) {
488 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
489 entry.ids.get(i)),
490 values, null, null);
491 }
492 }
493
494 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700495 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
496 values, null, null);
497 dataChanged();
498 return true;
499 }
500
501 /**
502 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
503 * The user picks a contact to be split into its own aggregate or clicks Cancel.
504 */
505 private void showSplitAggregateDialog() {
506
507 // Wrap this dialog in a specific theme so that list items have correct text color.
508 final ContextThemeWrapper dialogContext =
509 new ContextThemeWrapper(this, android.R.style.Theme_Light);
510 AlertDialog.Builder builder =
511 new AlertDialog.Builder(dialogContext);
512 builder.setTitle(getString(R.string.splitAggregate_title));
513
514 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
515 builder.setView(view);
516
517 builder.setInverseBackgroundForced(true);
518 builder.setCancelable(true);
519 builder.setNegativeButton(android.R.string.cancel,
520 new OnClickListener() {
521 public void onClick(DialogInterface dialog, int which) {
522 dialog.dismiss();
523 }
524 });
525 final AlertDialog dialog = builder.create();
526
527 view.setOnContactSelectedListener(new OnContactSelectedListener() {
528 public void onContactSelected(long contactId) {
529 dialog.dismiss();
530 splitContact(contactId);
531 }
532 });
533
534 dialog.show();
535 }
536
537 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700538 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700539 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700540 public void showJoinAggregateActivity() {
541 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
542 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
543 startActivityForResult(intent, REQUEST_JOIN_AGGREGATE);
544 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700545
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700546 @Override
547 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
548 if (requestCode == REQUEST_JOIN_AGGREGATE && resultCode == RESULT_OK && intent != null) {
549 final long aggregateId = ContentUris.parseId(intent.getData());
550 joinAggregate(aggregateId);
551 }
552 }
553
554 private void splitContact(long contactId) {
555 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_OUT);
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700556 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700557 mAdapter.notifyDataSetChanged();
558 }
559
560 private void joinAggregate(final long aggregateId) {
561 Cursor c = mResolver.query(Contacts.CONTENT_URI, new String[] {Contacts._ID},
562 Contacts.AGGREGATE_ID + "=" + aggregateId, null, null);
563
564 try {
565 while(c.moveToNext()) {
566 long contactId = c.getLong(0);
567 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
568 }
569 } finally {
570 c.close();
571 }
572
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700573 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700574 mAdapter.notifyDataSetChanged();
575 }
576
577 /**
578 * Given a contact ID sets an aggregation exception to either join the contact with the
579 * current aggregate or split off.
580 */
581 protected void setAggregationException(long contactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700582 ContentValues values = new ContentValues(3);
583 values.put(AggregationExceptions.AGGREGATE_ID, ContentUris.parseId(mUri));
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700584 values.put(AggregationExceptions.CONTACT_ID, contactId);
585 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700586 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700587 }
588
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700589 private void showOptionsActivity() {
590 final Intent intent = new Intent(this, ContactOptionsActivity.class);
591 intent.setData(mUri);
592 startActivity(intent);
593 }
594
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700595 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
596 AdapterView.AdapterContextMenuInfo info;
597 try {
598 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
599 } catch (ClassCastException e) {
600 Log.e(TAG, "bad menuInfo", e);
601 return null;
602 }
603
604 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
605 }
606
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800607 @Override
608 public boolean onKeyDown(int keyCode, KeyEvent event) {
609 switch (keyCode) {
610 case KeyEvent.KEYCODE_CALL: {
611 try {
612 ITelephony phone = ITelephony.Stub.asInterface(
613 ServiceManager.checkService("phone"));
614 if (phone != null && !phone.isIdle()) {
615 // Skip out and let the key be handled at a higher level
616 break;
617 }
618 } catch (RemoteException re) {
619 // Fall through and try to call the contact
620 }
621
622 int index = getListView().getSelectedItemPosition();
623 if (index != -1) {
624 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700625 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
626 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800627 }
628 } else if (mNumPhoneNumbers != 0) {
629 // There isn't anything selected, call the default number
630 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
631 startActivity(intent);
632 }
633 return true;
634 }
635
636 case KeyEvent.KEYCODE_DEL: {
637 showDialog(DIALOG_CONFIRM_DELETE);
638 return true;
639 }
640 }
641
642 return super.onKeyDown(keyCode, event);
643 }
644
645 @Override
646 protected void onListItemClick(ListView l, View v, int position, long id) {
647 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
648 if (entry != null) {
649 Intent intent = entry.intent;
650 if (intent != null) {
651 try {
652 startActivity(intent);
653 } catch (ActivityNotFoundException e) {
654 Log.e(TAG, "No activity found for intent: " + intent);
655 signalError();
656 }
657 } else {
658 signalError();
659 }
660 } else {
661 signalError();
662 }
663 }
664
665 /**
666 * Signal an error to the user via a beep, or some other method.
667 */
668 private void signalError() {
669 //TODO: implement this when we have the sonification APIs
670 }
671
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800672 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700673 // 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 -0800674 StringBuilder buf = new StringBuilder("imto://");
675 buf.append(host);
676 buf.append('/');
677 buf.append(data);
678 return Uri.parse(buf.toString());
679 }
680
681 /**
682 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700683 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800684 * @param personCursor the URI for the contact being displayed
685 */
Evan Millar66388be2009-05-28 15:41:07 -0700686 private final void buildEntries(Cursor aggCursor) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800687 // Clear out the old entries
688 final int numSections = mSections.size();
689 for (int i = 0; i < numSections; i++) {
690 mSections.get(i).clear();
691 }
692
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700693 mContactIds.clear();
694
Evan Millar66388be2009-05-28 15:41:07 -0700695 // Build up method entries
696 if (mUri != null) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700697 Bitmap photoBitmap = null;
Evan Millar66388be2009-05-28 15:41:07 -0700698 while (aggCursor.moveToNext()) {
699 final String mimetype = aggCursor.getString(DATA_MIMETYPE_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700700
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800701 ViewEntry entry = new ViewEntry();
Evan Millar66388be2009-05-28 15:41:07 -0700702
703 final long id = aggCursor.getLong(DATA_ID_COLUMN);
704 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800705 entry.id = id;
706 entry.uri = uri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700707 entry.mimetype = mimetype;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700708 entry.contactId = aggCursor.getLong(DATA_CONTACT_ID_COLUMN);
709 if (!mContactIds.contains(entry.contactId)) {
710 mContactIds.add(entry.contactId);
711 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800712
Evan Millar66388be2009-05-28 15:41:07 -0700713 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
Evan Millar5c22c3b2009-05-29 11:37:54 -0700714 || mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)
715 || mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)
Evan Millar66388be2009-05-28 15:41:07 -0700716 || mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
717 final int type = aggCursor.getInt(DATA_1_COLUMN);
Evan Millar45e0ed32009-06-01 16:44:38 -0700718 final String label = aggCursor.getString(DATA_3_COLUMN);
719 final String data = aggCursor.getString(DATA_2_COLUMN);
720 final boolean isSuperPrimary = "1".equals(
721 aggCursor.getString(DATA_IS_SUPER_PRIMARY_COLUMN));
Evan Millar66388be2009-05-28 15:41:07 -0700722
Evan Millar54a5c9f2009-06-23 17:41:09 -0700723 entry.type = type;
724
Evan Millar66388be2009-05-28 15:41:07 -0700725 // Don't crash if the data is bogus
726 if (TextUtils.isEmpty(data)) {
727 Log.w(TAG, "empty data for contact method " + id);
728 continue;
729 }
730
731 // Build phone entries
732 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
733 mNumPhoneNumbers++;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700734
Evan Millar66388be2009-05-28 15:41:07 -0700735 final CharSequence displayLabel = ContactsUtils.getDisplayLabel(
736 this, mimetype, type, label);
737 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700738 entry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar66388be2009-05-28 15:41:07 -0700739 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, entry.uri);
740 entry.auxIntent = new Intent(Intent.ACTION_SENDTO,
741 Uri.fromParts("sms", data, null));
Evan Millar45e0ed32009-06-01 16:44:38 -0700742 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700743 entry.primaryIcon = R.drawable.ic_default_number;
744 }
745 entry.actionIcon = android.R.drawable.sym_action_call;
746 mPhoneEntries.add(entry);
747
Evan Millar5c22c3b2009-05-29 11:37:54 -0700748 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700749 || mShowSmsLinksForAllPhones) {
750 // Add an SMS entry
751 ViewEntry smsEntry = new ViewEntry();
752 smsEntry.label = buildActionString(
753 R.string.actionText, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700754 smsEntry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar66388be2009-05-28 15:41:07 -0700755 smsEntry.id = id;
756 smsEntry.uri = uri;
757 smsEntry.intent = entry.auxIntent;
Evan Millar45e0ed32009-06-01 16:44:38 -0700758 smsEntry.mimetype = FastTrackWindow.MIME_SMS_ADDRESS;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700759 smsEntry.type = type;
Evan Millar66388be2009-05-28 15:41:07 -0700760 smsEntry.actionIcon = R.drawable.sym_action_sms;
761 mSmsEntries.add(smsEntry);
762 }
763 // Build email entries
764 } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800765 entry.label = buildActionString(R.string.actionEmail,
Evan Millar66388be2009-05-28 15:41:07 -0700766 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800767 entry.data = data;
768 entry.intent = new Intent(Intent.ACTION_SENDTO,
769 Uri.fromParts("mailto", data, null));
770 entry.actionIcon = android.R.drawable.sym_action_email;
Evan Millar45e0ed32009-06-01 16:44:38 -0700771 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700772 entry.primaryIcon = R.drawable.ic_default_number;
773 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800774 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700775 // Build postal entries
776 } else if (mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800777 entry.label = buildActionString(R.string.actionMap,
Evan Millar66388be2009-05-28 15:41:07 -0700778 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800779 entry.data = data;
780 entry.maxLines = 4;
781 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
782 entry.actionIcon = R.drawable.sym_action_map;
783 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700784 // Build im entries
785 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
786 String[] protocolStrings = getResources().getStringArray(
787 android.R.array.imProtocols);
788 Object protocolObj = ContactsUtils.decodeImProtocol(
789 aggCursor.getString(DATA_5_COLUMN));
Alex Kennberg87fc3172009-03-28 06:43:06 -0700790 String host = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800791 if (protocolObj instanceof Number) {
792 int protocol = ((Number) protocolObj).intValue();
793 entry.label = buildActionString(R.string.actionChat,
794 protocolStrings[protocol], false);
Evan Millar66388be2009-05-28 15:41:07 -0700795 host = ContactsUtils.lookupProviderNameFromId(
796 protocol).toLowerCase();
797 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
798 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800799 entry.maxLabelLines = 2;
800 }
Alex Kennberg87fc3172009-03-28 06:43:06 -0700801 } else if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800802 String providerName = (String) protocolObj;
803 entry.label = buildActionString(R.string.actionChat,
804 providerName, false);
805 host = providerName.toLowerCase();
806 }
807
808 // Only add the intent if there is a valid host
809 if (!TextUtils.isEmpty(host)) {
810 entry.intent = new Intent(Intent.ACTION_SENDTO,
811 constructImToUrl(host, data));
812 }
813 entry.data = data;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700814 //TODO(emillar) Add in presence info
Evan Millar66388be2009-05-28 15:41:07 -0700815 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800816 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700817 aggCursor.getInt(METHODS_STATUS_COLUMN));
Evan Millar54a5c9f2009-06-23 17:41:09 -0700818 entry.status = ...
Evan Millar66388be2009-05-28 15:41:07 -0700819 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800820 entry.actionIcon = android.R.drawable.sym_action_chat;
821 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700822 }
Evan Millar7e4accf2009-06-08 10:43:26 -0700823 // Set the name
824 } else if (mimetype.equals(CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
825 String name = mCursor.getString(DATA_9_COLUMN);
826 if (TextUtils.isEmpty(name)) {
827 mNameView.setText(getText(android.R.string.unknownName));
828 } else {
829 mNameView.setText(name);
830 }
831 /*
832 if (mPhoneticNameView != null) {
833 String phoneticName = mCursor.getString(CONTACT_PHONETIC_NAME_COLUMN);
834 mPhoneticNameView.setText(phoneticName);
835 }
836 */
Evan Millar66388be2009-05-28 15:41:07 -0700837 // Build organization entries
838 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
Evan Millar66388be2009-05-28 15:41:07 -0700839 final String company = aggCursor.getString(DATA_3_COLUMN);
840 final String title = aggCursor.getString(DATA_4_COLUMN);
841 final boolean isPrimary = "1".equals(aggCursor.getString(DATA_5_COLUMN));
842
843 if (isPrimary) {
844 entry.primaryIcon = R.drawable.ic_default_number;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800845 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700846
Evan Millar66388be2009-05-28 15:41:07 -0700847 // Don't crash if the data is bogus
848 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
849 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800850 continue;
851 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700852
Evan Millar66388be2009-05-28 15:41:07 -0700853 entry.data = title;
854 entry.actionIcon = R.drawable.sym_action_organization;
855 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800856
Evan Millar66388be2009-05-28 15:41:07 -0700857 mOrganizationEntries.add(entry);
858 // Build note entries
859 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
860 entry.label = getString(R.string.label_notes);
861 entry.data = aggCursor.getString(DATA_1_COLUMN);
862 entry.id = 0;
863 entry.uri = null;
864 entry.intent = null;
865 entry.maxLines = 10;
866 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800867
Evan Millar66388be2009-05-28 15:41:07 -0700868 if (TextUtils.isEmpty(entry.data)) {
869 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700870 continue;
871 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700872
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800873 mOtherEntries.add(entry);
Evan Millar45e0ed32009-06-01 16:44:38 -0700874 // Load the photo
875 } else if (mimetype.equals(CommonDataKinds.Photo.CONTENT_ITEM_TYPE)) {
876 photoBitmap = ContactsUtils.loadContactPhoto(aggCursor, DATA_1_COLUMN, null);
Evan Millar66388be2009-05-28 15:41:07 -0700877 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700878
Evan Millar45e0ed32009-06-01 16:44:38 -0700879
Evan Millar66388be2009-05-28 15:41:07 -0700880 // TODO(emillar) Add group entries
881// // Build the group entries
882// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
883// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
884// null, null, Groups.DEFAULT_SORT_ORDER);
885// if (groupCursor != null) {
886// try {
887// StringBuilder sb = new StringBuilder();
888//
889// while (groupCursor.moveToNext()) {
890// String systemId = groupCursor.getString(
891// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
892//
893// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
894// continue;
895// }
896//
897// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
898// if (!TextUtils.isEmpty(name)) {
899// if (sb.length() == 0) {
900// sb.append(name);
901// } else {
902// sb.append(getString(R.string.group_list, name));
903// }
904// }
905// }
906//
907// if (sb.length() > 0) {
908// ViewEntry entry = new ViewEntry();
909// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
910// entry.label = getString(R.string.label_groups);
911// entry.data = sb.toString();
912// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
913//
914// // TODO: Add an icon for the groups item.
915//
916// mGroupEntries.add(entry);
917// }
918// } finally {
919// groupCursor.close();
920// }
921// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700922
Evan Millar66388be2009-05-28 15:41:07 -0700923 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700924
925 if (photoBitmap == null) {
926 photoBitmap = ContactsUtils.loadPlaceholderPhoto(mNoPhotoResource, this, null);
927 }
928 mPhotoView.setImageBitmap(photoBitmap);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800929 }
930 }
931
932 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700933 // If there is no type just display an empty string
934 if (type == null) {
935 type = "";
936 }
937
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800938 if (lowerCase) {
939 return getString(actionResId, type.toString().toLowerCase());
940 } else {
941 return getString(actionResId, type.toString());
942 }
943 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700944
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800945 /**
946 * A basic structure with the data for a contact entry in the list.
947 */
Evan Millar54a5c9f2009-06-23 17:41:09 -0700948 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800949 public int primaryIcon = -1;
950 public Intent intent;
951 public Intent auxIntent = null;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700952 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800953 public int actionIcon = -1;
954 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700955 public ArrayList<Long> ids = new ArrayList<Long>();
956 public int collapseCount = 0;
957
958 public boolean collapseWith(ViewEntry entry) {
959 // assert equal collapse keys
960 if (!getCollapseKey().equals(entry.getCollapseKey())) {
961 return false;
962 }
963
964 // Choose the label associated with the highest type precedence.
965 if (TypePrecedence.getTypePrecedence(mimetype, type)
966 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
967 type = entry.type;
968 label = entry.label;
969 }
970
971 // Choose the max of the maxLines and maxLabelLines values.
972 maxLines = Math.max(maxLines, entry.maxLines);
973 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
974
975 // Choose the presence with the highest precedence.
976 if (Presence.getPresencePrecedence(status)
977 < Presence.getPresencePrecedence(entry.status)) {
978 status = entry.status;
979 }
980
981 // If any of the collapsed entries are primary make the whole thing primary.
982 if (primaryIcon != entry.primaryIcon && primaryIcon == -1) {
983 primaryIcon = entry.primaryIcon;
984 }
985
986 // uri, and contactdId, shouldn't make a difference. Just keep the original.
987
988 // Keep track of all the ids that have been collapsed with this one.
989 ids.add(entry.id);
990 collapseCount++;
991 return true;
992 }
993
994 public String getCollapseKey() {
995 StringBuilder hashSb = new StringBuilder();
996 hashSb.append(data);
997 hashSb.append(mimetype);
998 hashSb.append((intent != null && intent.getAction() != null)
999 ? intent.getAction() : "");
1000 hashSb.append((auxIntent != null && auxIntent.getAction() != null)
1001 ? auxIntent.getAction() : "");
1002 hashSb.append(actionIcon);
1003 return hashSb.toString();
1004 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001005 }
1006
1007 private static final class ViewAdapter extends ContactEntryAdapter<ViewEntry> {
1008 /** Cache of the children views of a row */
1009 static class ViewCache {
1010 public TextView label;
1011 public TextView data;
1012 public ImageView actionIcon;
1013 public ImageView presenceIcon;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001014
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001015 // Need to keep track of this too
1016 ViewEntry entry;
1017 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001018
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001019 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1020 super(context, sections, SHOW_SEPARATORS);
1021 }
1022
1023 @Override
1024 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001025 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001026 View v;
1027
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001028 ViewCache views;
1029
1030 // Check to see if we can reuse convertView
1031 if (convertView != null) {
1032 v = convertView;
1033 views = (ViewCache) v.getTag();
1034 } else {
1035 // Create a new view if needed
1036 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1037
1038 // Cache the children
1039 views = new ViewCache();
1040 views.label = (TextView) v.findViewById(android.R.id.text1);
1041 views.data = (TextView) v.findViewById(android.R.id.text2);
1042 views.actionIcon = (ImageView) v.findViewById(R.id.icon1);
1043 views.presenceIcon = (ImageView) v.findViewById(R.id.icon2);
1044 v.setTag(views);
1045 }
1046
1047 // Update the entry in the view cache
1048 views.entry = entry;
1049
1050 // Bind the data to the view
1051 bindView(v, entry);
1052 return v;
1053 }
1054
1055 @Override
1056 protected View newView(int position, ViewGroup parent) {
1057 // getView() handles this
1058 throw new UnsupportedOperationException();
1059 }
1060
1061 @Override
1062 protected void bindView(View view, ViewEntry entry) {
1063 final Resources resources = mContext.getResources();
1064 ViewCache views = (ViewCache) view.getTag();
1065
1066 // Set the label
1067 TextView label = views.label;
1068 setMaxLines(label, entry.maxLabelLines);
1069 label.setText(entry.label);
1070
1071 // Set the data
1072 TextView data = views.data;
1073 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001074 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1075 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1076 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1077 } else {
1078 data.setText(entry.data);
1079 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001080 setMaxLines(data, entry.maxLines);
1081 }
1082
1083 // Set the action icon
1084 ImageView action = views.actionIcon;
1085 if (entry.actionIcon != -1) {
1086 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1087 action.setVisibility(View.VISIBLE);
1088 } else {
1089 // Things should still line up as if there was an icon, so make it invisible
1090 action.setVisibility(View.INVISIBLE);
1091 }
1092
1093 // Set the presence icon
1094 Drawable presenceIcon = null;
1095 if (entry.primaryIcon != -1) {
1096 presenceIcon = resources.getDrawable(entry.primaryIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001097 } else if (entry.status != -1) {
1098 presenceIcon = resources.getDrawable(
1099 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001100 }
1101
1102 ImageView presence = views.presenceIcon;
1103 if (presenceIcon != null) {
1104 presence.setImageDrawable(presenceIcon);
1105 presence.setVisibility(View.VISIBLE);
1106 } else {
1107 presence.setVisibility(View.GONE);
1108 }
1109 }
1110
1111 private void setMaxLines(TextView textView, int maxLines) {
1112 if (maxLines == 1) {
1113 textView.setSingleLine(true);
1114 textView.setEllipsize(TextUtils.TruncateAt.END);
1115 } else {
1116 textView.setSingleLine(false);
1117 textView.setMaxLines(maxLines);
1118 textView.setEllipsize(null);
1119 }
1120 }
1121 }
1122}