blob: 0939f6ca3ae1a76443ce759c0cb2ba12cf0069c6 [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 Millar54a5c9f2009-06-23 17:41:09 -070019import com.android.contacts.Collapser.Collapsible;
Evan Millar11d628c2009-09-02 08:55:01 -070020import com.android.contacts.ScrollingTabWidget.OnTabSelectionChangedListener;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070021import com.android.contacts.SplitAggregateView.OnContactSelectedListener;
Evan Millar11d628c2009-09-02 08:55:01 -070022import com.android.contacts.model.ContactsSource;
23import com.android.contacts.model.Sources;
24import com.android.contacts.model.ContactsSource.DataKind;
Evan Millar49714ee2009-09-02 16:42:47 -070025import com.android.contacts.model.HardCodedSources.SimpleInflater;
Jeff Sharkey802b2052009-08-04 14:21:06 -070026import com.android.contacts.ui.FastTrackWindow;
Evan Millar11d628c2009-09-02 08:55:01 -070027import com.android.contacts.util.NotifyingAsyncQueryHandler;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080028import com.android.internal.telephony.ITelephony;
Evan Millar11d628c2009-09-02 08:55:01 -070029import com.android.internal.widget.ContactHeaderWidget;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080030
Evan Millar11d628c2009-09-02 08:55:01 -070031import android.app.Activity;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080032import android.app.AlertDialog;
33import android.app.Dialog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080034import android.content.ActivityNotFoundException;
35import android.content.ContentResolver;
36import android.content.ContentUris;
37import android.content.ContentValues;
38import android.content.Context;
39import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070040import android.content.Entity;
Evan Millar11d628c2009-09-02 08:55:01 -070041import android.content.EntityIterator;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080042import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070043import android.content.DialogInterface.OnClickListener;
Evan Millar11d628c2009-09-02 08:55:01 -070044import android.content.Entity.NamedContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080045import android.content.pm.PackageManager;
46import android.content.pm.ResolveInfo;
47import android.content.res.Resources;
48import android.database.ContentObserver;
49import android.database.Cursor;
Dmitri Plotnikov3d53ce22009-09-02 08:44:32 -070050import android.database.DatabaseUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080051import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080052import android.net.Uri;
53import android.os.Bundle;
54import android.os.Handler;
55import android.os.RemoteException;
56import android.os.ServiceManager;
Dmitri Plotnikov3d53ce22009-09-02 08:44:32 -070057import android.provider.ContactsContract;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070058import android.provider.ContactsContract.AggregationExceptions;
59import android.provider.ContactsContract.CommonDataKinds;
Evan Millar11d628c2009-09-02 08:55:01 -070060import android.provider.ContactsContract.Contacts;
Evan Millar66388be2009-05-28 15:41:07 -070061import android.provider.ContactsContract.Data;
Evan Millar54a5c9f2009-06-23 17:41:09 -070062import android.provider.ContactsContract.Presence;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070063import android.provider.ContactsContract.RawContacts;
Evan Millar54a5c9f2009-06-23 17:41:09 -070064import android.provider.ContactsContract.CommonDataKinds.Phone;
65import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080066import android.text.TextUtils;
67import android.util.Log;
Evan Millar11d628c2009-09-02 08:55:01 -070068import android.util.SparseArray;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080069import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070070import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080071import android.view.KeyEvent;
Evan Millar11d628c2009-09-02 08:55:01 -070072import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080073import android.view.Menu;
74import android.view.MenuItem;
75import android.view.View;
76import android.view.ViewGroup;
Evan Millar11d628c2009-09-02 08:55:01 -070077import android.view.Window;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080078import android.view.ContextMenu.ContextMenuInfo;
79import android.widget.AdapterView;
Evan Millar7911ff52009-07-21 15:55:18 -070080import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080081import android.widget.ImageView;
82import android.widget.ListView;
83import android.widget.TextView;
84import android.widget.Toast;
85
86import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087
88/**
89 * Displays the details of a specific contact.
90 */
Evan Millar11d628c2009-09-02 08:55:01 -070091public class ViewContactActivity extends Activity
Evan Millar7911ff52009-07-21 15:55:18 -070092 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
Evan Millar11d628c2009-09-02 08:55:01 -070093 AdapterView.OnItemClickListener, NotifyingAsyncQueryHandler.AsyncQueryListener,
94 OnTabSelectionChangedListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080095 private static final String TAG = "ViewContact";
96 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
97
Evan Millar8a79cee2009-08-19 17:20:49 -070098 public static final String RAW_CONTACT_ID_EXTRA = "rawContactIdExtra";
99
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800100 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
Evan Millar8a79cee2009-08-19 17:20:49 -0700104 private static final int REQUEST_JOIN_CONTACT = 1;
105 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700106
Evan Millar8a79cee2009-08-19 17:20:49 -0700107 public static final int MENU_ITEM_EDIT = 1;
108 public static final int MENU_ITEM_DELETE = 2;
109 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
110 public static final int MENU_ITEM_SHOW_BARCODE = 4;
111 public static final int MENU_ITEM_SPLIT_AGGREGATE = 5;
112 public static final int MENU_ITEM_JOIN_AGGREGATE = 6;
113 public static final int MENU_ITEM_OPTIONS = 7;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800114
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700115 protected Uri mLookupUri;
Evan Millar11d628c2009-09-02 08:55:01 -0700116 private Uri mUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800117 private ContentResolver mResolver;
118 private ViewAdapter mAdapter;
119 private int mNumPhoneNumbers = 0;
120
Evan Millar8a79cee2009-08-19 17:20:49 -0700121 private static final long ALL_CONTACTS_ID = -100;
Evan Millar7911ff52009-07-21 15:55:18 -0700122
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700123 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700124 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700125 */
Evan Millar7911ff52009-07-21 15:55:18 -0700126 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700127
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800128 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
129 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
130 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
131 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
132 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
133 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700134 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800135 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
136 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
137
138 private Cursor mCursor;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700139
Evan Millar11d628c2009-09-02 08:55:01 -0700140 private SparseArray<Long> mTabRawContactIdMap;
141 protected ScrollingTabWidget mTabWidget;
142 protected ContactHeaderWidget mContactHeaderWidget;
143 private NotifyingAsyncQueryHandler mHandler;
144
145 protected LayoutInflater mInflater;
146
147 //Projection used for the query that determines which tabs to add.
148 protected static final String[] TAB_PROJECTION = new String[] {
149 RawContacts._ID,
150 RawContacts.ACCOUNT_NAME,
151 RawContacts.ACCOUNT_TYPE
152 };
153 protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0;
154 protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1;
155 protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2;
156
157 protected static final String SELECTED_RAW_CONTACT_ID_KEY = "selectedRawContact";
158 protected Long mSelectedRawContactId = null;
159
160 private static final int TOKEN_QUERY = 0;
161
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800162 private ContentObserver mObserver = new ContentObserver(new Handler()) {
163 @Override
164 public boolean deliverSelfNotifications() {
165 return true;
166 }
167
168 @Override
169 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700170 if (mCursor != null && !mCursor.isClosed()) {
171 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800172 }
173 }
174 };
175
176 public void onClick(DialogInterface dialog, int which) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700177 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800178 getContentResolver().delete(mUri, null, null);
179 finish();
180 }
181
Evan Millar7911ff52009-07-21 15:55:18 -0700182 private FrameLayout mTabContentLayout;
183 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800184 private boolean mShowSmsLinksForAllPhones;
Evan Millar11d628c2009-09-02 08:55:01 -0700185 private ArrayList<Entity> mEntities = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800186
187 @Override
188 protected void onCreate(Bundle icicle) {
189 super.onCreate(icicle);
190
Evan Millar11d628c2009-09-02 08:55:01 -0700191 final Intent intent = getIntent();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700192 mLookupUri = intent.getData();
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700193
194 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700195
196 requestWindowFeature(Window.FEATURE_NO_TITLE);
197 setContentView(R.layout.contact_card_layout);
198
199 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
200 mContactHeaderWidget.showStar(true);
Evan Millar11d628c2009-09-02 08:55:01 -0700201 mContactHeaderWidget.setExcludeMimes(new String[] {
202 Contacts.CONTENT_ITEM_TYPE
203 });
204
205 mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget);
206 mTabWidget.setTabSelectionListener(this);
207 mTabWidget.setVisibility(View.INVISIBLE);
208
209 mTabRawContactIdMap = new SparseArray<Long>();
210
211 mHandler = new NotifyingAsyncQueryHandler(this, this);
212
Evan Millar7911ff52009-07-21 15:55:18 -0700213 mListView = new ListView(this);
214 mListView.setOnCreateContextMenuListener(this);
215 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
216 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800217
Evan Millar7911ff52009-07-21 15:55:18 -0700218 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
219 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800220
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800221 mResolver = getContentResolver();
222
223 // Build the list of sections. The order they're added to mSections dictates the
224 // order they are displayed in the list.
225 mSections.add(mPhoneEntries);
226 mSections.add(mSmsEntries);
227 mSections.add(mEmailEntries);
228 mSections.add(mImEntries);
229 mSections.add(mPostalEntries);
230 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700231 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800232 mSections.add(mOtherEntries);
233
234 //TODO Read this value from a preference
235 mShowSmsLinksForAllPhones = true;
236
Evan Millar11d628c2009-09-02 08:55:01 -0700237 startEntityQuery();
238 }
239
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800240 @Override
241 protected void onResume() {
242 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700243 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800244 }
245
246 @Override
247 protected void onPause() {
248 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700249 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800250 }
251
252 @Override
253 protected void onDestroy() {
254 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700255 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800256 }
257
258 @Override
Evan Millar11d628c2009-09-02 08:55:01 -0700259 protected void onRestoreInstanceState(Bundle savedInstanceState) {
260 super.onRestoreInstanceState(savedInstanceState);
261 mSelectedRawContactId = savedInstanceState.getLong(SELECTED_RAW_CONTACT_ID_KEY);
262 }
263
264 @Override
265 protected void onSaveInstanceState(Bundle outState) {
266 super.onSaveInstanceState(outState);
267 outState.putLong(SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
268 }
269
270 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800271 protected Dialog onCreateDialog(int id) {
272 switch (id) {
273 case DIALOG_CONFIRM_DELETE:
274 return new AlertDialog.Builder(this)
275 .setTitle(R.string.deleteConfirmation_title)
276 .setIcon(android.R.drawable.ic_dialog_alert)
277 .setMessage(R.string.deleteConfirmation)
278 .setNegativeButton(android.R.string.cancel, null)
279 .setPositiveButton(android.R.string.ok, this)
280 .setCancelable(false)
281 .create();
282 }
283 return null;
284 }
285
Evan Millar11d628c2009-09-02 08:55:01 -0700286
287 // TAB CODE //
288 /**
289 * Adds a tab for each {@link RawContact} associated with this contact.
290 * Override this method if you want to additional tabs and/or different
291 * tabs for your activity.
292 *
293 * @param entities An {@link ArrayList} of {@link Entity}s of all the RawContacts
294 * associated with the contact being displayed.
295 */
296 protected void bindTabs() {
297 if (mEntities.size() > 1) {
Evan Millar7911ff52009-07-21 15:55:18 -0700298 addAllTab();
299 }
Evan Millar11d628c2009-09-02 08:55:01 -0700300
301 final Sources sources = Sources.getInstance(this);
302
303 for (Entity entity : mEntities) {
304 final String accountType = entity.getEntityValues().
305 getAsString(RawContacts.ACCOUNT_TYPE);
306 final Long rawContactId = entity.getEntityValues().
307 getAsLong(RawContacts._ID);
308
309 // TODO: ensure inflation on background task so we don't block UI thread here
310 final ContactsSource source = sources.getInflatedSource(accountType,
311 ContactsSource.LEVEL_SUMMARY);
312 addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(), source));
313 }
314
315 selectInitialTab();
316 mTabWidget.setVisibility(View.VISIBLE);
317 mTabWidget.postInvalidate();
318 }
319
320 /**
321 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
322 *
323 * @param contactId The contact id associated with the tab.
324 * @param view A view to use as the tab indicator.
325 */
326 protected void addTab(long rawContactId, View view) {
327 mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId);
328 mTabWidget.addTab(view);
329 }
330
331
332 protected void clearCurrentTabs() {
333 mTabRawContactIdMap.clear();
334 mTabWidget.removeAllTabs();
335 }
336
337 protected void selectInitialTab() {
338 int selectedTabIndex = 0;
339
340 if (mSelectedRawContactId != null) {
341 selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
342 if (selectedTabIndex == -1) {
343 // If there was no matching tab, just select the first;
344 selectedTabIndex = 0;
345 }
346 }
347
348 mTabWidget.setCurrentTab(selectedTabIndex);
349 onTabSelectionChanged(selectedTabIndex, false);
350 }
351
Evan Millar7911ff52009-07-21 15:55:18 -0700352 private void addAllTab() {
Evan Millar56d2caa2009-08-20 20:30:12 -0700353 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator,
354 mTabWidget.getTabParent(), false);
Evan Millar74660912009-08-19 17:36:33 -0700355 allTabIndicator.getBackground().setDither(true);
Evan Millar7911ff52009-07-21 15:55:18 -0700356 addTab(ALL_CONTACTS_ID, allTabIndicator);
357 }
358
359 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
360 long rawContactId = getTabRawContactId(tabIndex);
Evan Millar8a79cee2009-08-19 17:20:49 -0700361 mSelectedRawContactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700362 bindData();
Evan Millar7911ff52009-07-21 15:55:18 -0700363 }
364
Evan Millar11d628c2009-09-02 08:55:01 -0700365 /**
366 * Return the RawContact id associated with the tab at an index.
367 *
368 * @param index The index of the tab in question.
369 * @return The contactId associated with the tab at the specified index.
370 */
371 protected long getTabRawContactId(int index) {
372 return mTabRawContactIdMap.get(index);
373 }
Evan Millar2c1cc832009-07-13 11:08:06 -0700374
Evan Millar11d628c2009-09-02 08:55:01 -0700375 /**
376 * Return the tab index associated with the RawContact id.
377 *
378 * @param index The index of the tab in question.
379 * @return The contactId associated with the tab at the specified index.
380 */
381 protected int getTabIndexForRawContactId(long rawContactId) {
382 int numTabs = mTabRawContactIdMap.size();
383 for (int i=0; i < numTabs; i++) {
384 if (mTabRawContactIdMap.get(i) == rawContactId) {
385 return i;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800386 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800387 }
Evan Millar11d628c2009-09-02 08:55:01 -0700388 return -1;
389 }
390
391
392 // QUERY CODE //
393 /** {@inheritDoc} */
394 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
395 try{
396 if (token == TOKEN_QUERY) {
397 clearCurrentTabs();
398 mEntities = readEntities(iterator);
399 bindTabs();
400 bindData();
401 }
402 } finally {
403 if (iterator != null) {
404 iterator.close();
405 }
406 }
407 }
408
409 /** {@inheritDoc} */
410 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700411 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700412 }
413
414 private ArrayList<Entity> readEntities(EntityIterator iterator) {
415 ArrayList<Entity> entities = new ArrayList<Entity>();
416 try {
417 while (iterator.hasNext()) {
418 entities.add(iterator.next());
419 }
420 } catch (RemoteException e) {
421 }
422
423 return entities;
424 }
425
426 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700427 closeCursor();
428
429 if (mLookupUri != null) {
430 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
431 if (mLookupUri != null) {
432 mUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
433 }
434 }
435
436 if (mUri == null) {
437
438 // TODO either figure out a way to prevent a flash of black background or
439 // use some other UI than a toast
440 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
441 Log.e(TAG, "invalid contact uri: " + mLookupUri);
442 finish();
443 return;
444 }
445
446 mCursor = mResolver.query(Uri.withAppendedPath(mUri, Contacts.Data.CONTENT_DIRECTORY),
447 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
448 mCursor.registerContentObserver(mObserver);
449
Evan Millar11d628c2009-09-02 08:55:01 -0700450 long contactId = ContentUris.parseId(mUri);
451 mHandler.startQueryEntities(TOKEN_QUERY, null,
452 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700453
454 mContactHeaderWidget.bindFromContactId(ContentUris.parseId(mUri));
455 }
456
457 private void closeCursor() {
458 if (mCursor != null) {
459 mCursor.unregisterContentObserver(mObserver);
460 mCursor.close();
461 mCursor = null;
462 }
Evan Millar11d628c2009-09-02 08:55:01 -0700463 }
464
465 private void bindData() {
466
467 // Build up the contact entries
468 buildEntries();
469
470 // Collapse similar data items in select sections.
471 Collapser.collapseList(mPhoneEntries);
472 Collapser.collapseList(mSmsEntries);
473 Collapser.collapseList(mEmailEntries);
474 Collapser.collapseList(mPostalEntries);
475
476 if (mAdapter == null) {
477 mAdapter = new ViewAdapter(this, mSections);
478 mListView.setAdapter(mAdapter);
479 } else {
480 mAdapter.setSections(mSections, SHOW_SEPARATORS);
481 }
482
483// else {
484// Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
485// Log.e(TAG, "invalid contact uri: " + mOriginalUri);
486// finish();
487// }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800488 }
489
490 @Override
491 public boolean onCreateOptionsMenu(Menu menu) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800492 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
493 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700494 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
495 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700496 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
497 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700498 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
499 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800500 return true;
501 }
502
503 @Override
504 public boolean onPrepareOptionsMenu(Menu menu) {
505 super.onPrepareOptionsMenu(menu);
506 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
507 // could be installed or uninstalled at any time.
508 if (isBarcodeScannerInstalled()) {
509 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
510 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
511 .setIcon(R.drawable.ic_menu_show_barcode);
512 }
513 } else {
514 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
515 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700516
Evan Millardb5d88c2009-08-28 09:31:57 -0700517 // Only show the edit option if we have a selected tab.
518 if (mSelectedRawContactId != null) {
519 if (menu.findItem(MENU_ITEM_EDIT) == null) {
520 menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_editContact)
521 .setIcon(android.R.drawable.ic_menu_edit)
522 .setAlphabeticShortcut('e');
523 }
524 } else {
525 menu.removeItem(MENU_ITEM_EDIT);
526 }
527
Evan Millar7911ff52009-07-21 15:55:18 -0700528 boolean isAggregate = mRawContactIds.size() > 1;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700529 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800530 return true;
531 }
532
533 private boolean isBarcodeScannerInstalled() {
534 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
535 ResolveInfo ri = getPackageManager().resolveActivity(intent,
536 PackageManager.MATCH_DEFAULT_ONLY);
537 return ri != null;
538 }
539
540 @Override
541 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
542 AdapterView.AdapterContextMenuInfo info;
543 try {
544 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
545 } catch (ClassCastException e) {
546 Log.e(TAG, "bad menuInfo", e);
547 return;
548 }
549
550 // This can be null sometimes, don't crash...
551 if (info == null) {
552 Log.e(TAG, "bad menuInfo");
553 return;
554 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700555
Evan Millar45e0ed32009-06-01 16:44:38 -0700556 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
557 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
558 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700559 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
560 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700561 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800562 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700563 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
564 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700565 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700566 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800567 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700568 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700569 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
570 }
571 // TODO(emillar): add back with group support.
572 /* else if (entry.mimetype.equals()) {
573 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
574 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800575 }
576
577 @Override
578 public boolean onOptionsItemSelected(MenuItem item) {
579 switch (item.getItemId()) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700580 case MENU_ITEM_EDIT: {
Evan Millardb5d88c2009-08-28 09:31:57 -0700581 Long rawContactIdToEdit = mSelectedRawContactId;
582 if (rawContactIdToEdit == null) {
583 // This shouldn't be possible. We only show the edit option if
584 // this value is non-null.
585 break;
586 }
Evan Millar8a79cee2009-08-19 17:20:49 -0700587 if (rawContactIdToEdit == ALL_CONTACTS_ID) {
588 // If the "all" tab is selected, edit the next tab.
Evan Millardb5d88c2009-08-28 09:31:57 -0700589 rawContactIdToEdit = getTabRawContactId(mTabWidget.getCurrentTab() + 1);
Evan Millar8a79cee2009-08-19 17:20:49 -0700590 }
591 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
592 rawContactIdToEdit);
593 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
594 REQUEST_EDIT_CONTACT);
595 break;
596 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800597 case MENU_ITEM_DELETE: {
598 // Get confirmation
599 showDialog(DIALOG_CONFIRM_DELETE);
600 return true;
601 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700602
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700603 case MENU_ITEM_SPLIT_AGGREGATE: {
604 showSplitAggregateDialog();
605 return true;
606 }
607
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700608 case MENU_ITEM_JOIN_AGGREGATE: {
609 showJoinAggregateActivity();
610 return true;
611 }
612
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700613 case MENU_ITEM_OPTIONS: {
614 showOptionsActivity();
615 return true;
616 }
617
Evan Millar66388be2009-05-28 15:41:07 -0700618 // TODO(emillar) Bring this back.
619 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800620 if (mCursor.moveToFirst()) {
621 Intent intent = new Intent(SHOW_BARCODE_INTENT);
622 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
623 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700624 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800625 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700626 // Correctly handle when section headers are hidden
627 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700628
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800629 bundle.putString(Contacts.Intents.Insert.NAME, name);
630 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700631 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800632 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700633 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800634 bundle.putString(PHONE_KEYS[x], entry.data);
635 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700636 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800637 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700638 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800639 bundle.putString(EMAIL_KEYS[x], entry.data);
640 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700641 if (mPostalEntries.size() >= 1 + sepAdjust) {
642 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800643 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
644 }
645 intent.putExtra("ENCODE_DATA", bundle);
646 try {
647 startActivity(intent);
648 } catch (ActivityNotFoundException e) {
649 // The check in onPrepareOptionsMenu() should make this impossible, but
650 // for safety I'm catching the exception rather than crashing. Ideally
651 // I'd call Menu.removeItem() here too, but I don't see a way to get
652 // the options menu.
653 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
654 "was not installed.");
655 }
656 return true;
657 }
658 }
Evan Millar66388be2009-05-28 15:41:07 -0700659 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800660 }
661 return super.onOptionsItemSelected(item);
662 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700663
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800664 @Override
665 public boolean onContextItemSelected(MenuItem item) {
666 switch (item.getItemId()) {
667 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700668 if (makeItemDefault(item)) {
669 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800670 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700671 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800672 }
673 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700674
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800675 return super.onContextItemSelected(item);
676 }
677
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700678 private boolean makeItemDefault(MenuItem item) {
679 ViewEntry entry = getViewEntryForMenuItem(item);
680 if (entry == null) {
681 return false;
682 }
683
684 // Update the primary values in the data record.
685 ContentValues values = new ContentValues(2);
686 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700687
Evan Millar54a5c9f2009-06-23 17:41:09 -0700688 if (entry.ids.size() > 0) {
689 for (int i = 0; i < entry.ids.size(); i++) {
690 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
691 entry.ids.get(i)),
692 values, null, null);
693 }
694 }
695
696 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700697 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
698 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700699 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700700 return true;
701 }
702
703 /**
704 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
705 * The user picks a contact to be split into its own aggregate or clicks Cancel.
706 */
707 private void showSplitAggregateDialog() {
708
709 // Wrap this dialog in a specific theme so that list items have correct text color.
710 final ContextThemeWrapper dialogContext =
711 new ContextThemeWrapper(this, android.R.style.Theme_Light);
712 AlertDialog.Builder builder =
713 new AlertDialog.Builder(dialogContext);
714 builder.setTitle(getString(R.string.splitAggregate_title));
715
716 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
717 builder.setView(view);
718
719 builder.setInverseBackgroundForced(true);
720 builder.setCancelable(true);
721 builder.setNegativeButton(android.R.string.cancel,
722 new OnClickListener() {
723 public void onClick(DialogInterface dialog, int which) {
724 dialog.dismiss();
725 }
726 });
727 final AlertDialog dialog = builder.create();
728
729 view.setOnContactSelectedListener(new OnContactSelectedListener() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700730 public void onContactSelected(long rawContactId) {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700731 dialog.dismiss();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700732 splitContact(rawContactId);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700733 }
734 });
735
736 dialog.show();
737 }
738
739 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700740 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700741 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700742 public void showJoinAggregateActivity() {
743 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
744 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700745 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700746 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700747
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700748 @Override
749 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700750 switch (requestCode) {
751 case REQUEST_JOIN_CONTACT: {
752 if (resultCode == RESULT_OK && intent != null) {
753 final long aggregateId = ContentUris.parseId(intent.getData());
754 joinAggregate(aggregateId);
755 }
756 break;
757 }
758 case REQUEST_EDIT_CONTACT: {
759 if (resultCode == RESULT_OK && intent != null) {
760 long newInitialSelectedRawContactId = intent.getLongExtra(
761 RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
762 if (newInitialSelectedRawContactId != mSelectedRawContactId) {
763 mSelectedRawContactId = newInitialSelectedRawContactId;
764 selectInitialTab();
765 }
766 }
767 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700768 }
769 }
770
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700771 private void splitContact(long rawContactId) {
772 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_OUT);
773
774 // The split operation may have removed the original aggregate contact, so we need
775 // to requery everything
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700776 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700777 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700778 }
779
780 private void joinAggregate(final long aggregateId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700781 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700782 RawContacts.CONTACT_ID + "=" + aggregateId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700783
784 try {
785 while(c.moveToNext()) {
786 long contactId = c.getLong(0);
787 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
788 }
789 } finally {
790 c.close();
791 }
792
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700793 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700794 mAdapter.notifyDataSetChanged();
795 }
796
797 /**
798 * Given a contact ID sets an aggregation exception to either join the contact with the
799 * current aggregate or split off.
800 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700801 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700802 ContentValues values = new ContentValues(3);
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700803 values.put(AggregationExceptions.CONTACT_ID, ContentUris.parseId(mUri));
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700804 values.put(AggregationExceptions.RAW_CONTACT_ID, rawContactId);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700805 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700806 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700807 }
808
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700809 private void showOptionsActivity() {
810 final Intent intent = new Intent(this, ContactOptionsActivity.class);
811 intent.setData(mUri);
812 startActivity(intent);
813 }
814
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700815 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
816 AdapterView.AdapterContextMenuInfo info;
817 try {
818 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
819 } catch (ClassCastException e) {
820 Log.e(TAG, "bad menuInfo", e);
821 return null;
822 }
823
824 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
825 }
826
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800827 @Override
828 public boolean onKeyDown(int keyCode, KeyEvent event) {
829 switch (keyCode) {
830 case KeyEvent.KEYCODE_CALL: {
831 try {
832 ITelephony phone = ITelephony.Stub.asInterface(
833 ServiceManager.checkService("phone"));
834 if (phone != null && !phone.isIdle()) {
835 // Skip out and let the key be handled at a higher level
836 break;
837 }
838 } catch (RemoteException re) {
839 // Fall through and try to call the contact
840 }
841
Evan Millar7911ff52009-07-21 15:55:18 -0700842 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800843 if (index != -1) {
844 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700845 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
846 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800847 }
848 } else if (mNumPhoneNumbers != 0) {
849 // There isn't anything selected, call the default number
850 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
851 startActivity(intent);
852 }
853 return true;
854 }
855
856 case KeyEvent.KEYCODE_DEL: {
857 showDialog(DIALOG_CONFIRM_DELETE);
858 return true;
859 }
860 }
861
862 return super.onKeyDown(keyCode, event);
863 }
864
Evan Millar7911ff52009-07-21 15:55:18 -0700865 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800866 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
867 if (entry != null) {
868 Intent intent = entry.intent;
869 if (intent != null) {
870 try {
871 startActivity(intent);
872 } catch (ActivityNotFoundException e) {
873 Log.e(TAG, "No activity found for intent: " + intent);
874 signalError();
875 }
876 } else {
877 signalError();
878 }
879 } else {
880 signalError();
881 }
882 }
883
884 /**
885 * Signal an error to the user via a beep, or some other method.
886 */
887 private void signalError() {
888 //TODO: implement this when we have the sonification APIs
889 }
890
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800891 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700892 // 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 -0800893 StringBuilder buf = new StringBuilder("imto://");
894 buf.append(host);
895 buf.append('/');
896 buf.append(data);
897 return Uri.parse(buf.toString());
898 }
899
900 /**
901 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700902 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800903 * @param personCursor the URI for the contact being displayed
904 */
Evan Millar11d628c2009-09-02 08:55:01 -0700905 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800906 // Clear out the old entries
907 final int numSections = mSections.size();
908 for (int i = 0; i < numSections; i++) {
909 mSections.get(i).clear();
910 }
911
Evan Millar7911ff52009-07-21 15:55:18 -0700912 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700913
Evan Millar11d628c2009-09-02 08:55:01 -0700914 Sources sources = Sources.getInstance(this);
915
Evan Millar66388be2009-05-28 15:41:07 -0700916 // Build up method entries
917 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700918 for (Entity entity: mEntities) {
919 final ContentValues entValues = entity.getEntityValues();
920 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Evan Millar7911ff52009-07-21 15:55:18 -0700921 // TODO: entry.contactId should be renamed to entry.rawContactId
Evan Millar11d628c2009-09-02 08:55:01 -0700922 long contactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700923
Evan Millar11d628c2009-09-02 08:55:01 -0700924 for (NamedContentValues subValue : entity.getSubValues()) {
925 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800926
Evan Millar11d628c2009-09-02 08:55:01 -0700927 ContentValues entryValues = subValue.values;
928 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
929 if (mimetype == null || accountType == null) {
Evan Millar66388be2009-05-28 15:41:07 -0700930 continue;
931 }
932
Evan Millar11d628c2009-09-02 08:55:01 -0700933 ContactsSource contactsSource = sources.getInflatedSource(accountType,
934 ContactsSource.LEVEL_MIMETYPES);
935 if (contactsSource == null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800936 continue;
937 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700938
Evan Millar11d628c2009-09-02 08:55:01 -0700939 DataKind kind = contactsSource.getKindForMimetype(mimetype);
940 if (kind == null) {
941 continue;
942 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800943
Evan Millar11d628c2009-09-02 08:55:01 -0700944 final long id = entryValues.getAsLong(Data._ID);
945 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
946 entry.contactId = contactId;
947 entry.id = id;
948 entry.uri = uri;
949 entry.mimetype = mimetype;
950 entry.label = buildActionString(kind, entryValues, true);
951 entry.data = buildDataString(kind, entryValues);
952 if (kind.typeColumn != null) {
953 entry.type = entryValues.getAsInteger(kind.typeColumn);
954 }
955 if (kind.iconRes > 0) {
956 entry.actionIcon = kind.iconRes;
957 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800958
Evan Millar11d628c2009-09-02 08:55:01 -0700959 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700960 if (TextUtils.isEmpty(entry.data)) {
961 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700962 continue;
963 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700964
Evan Millar11d628c2009-09-02 08:55:01 -0700965 if (!mRawContactIds.contains(entry.contactId)) {
966 mRawContactIds.add(entry.contactId);
967 }
968
969 // This performs the tab filtering
970 if (mSelectedRawContactId != null
971 && mSelectedRawContactId != entry.contactId
972 && mSelectedRawContactId != ALL_CONTACTS_ID) {
973 continue;
974 }
975
976 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)
977 || CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)
978 || CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)
979 || CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
980 final boolean isSuperPrimary = entryValues.getAsInteger(
981 Data.IS_SUPER_PRIMARY) != 0;
982
983 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
984 // Build phone entries
985 mNumPhoneNumbers++;
986
987 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
988 Uri.fromParts("tel", entry.data, null));
989 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
990 Uri.fromParts("sms", entry.data, null));
991 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700992
993 // If data is empty, don't show it.
994 if (TextUtils.isEmpty(entry.data)) {
995 Log.w(TAG, "empty data for contact method " + id);
996 continue;
997 }
998
Evan Millar11d628c2009-09-02 08:55:01 -0700999 entry.isPrimary = isSuperPrimary;
1000 mPhoneEntries.add(entry);
1001
1002 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
1003 || mShowSmsLinksForAllPhones) {
1004 // Add an SMS entry
1005 if (kind.iconAltRes > 0) {
1006 entry.secondaryActionIcon = kind.iconAltRes;
1007 }
1008 }
1009 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
1010 // Build email entries
1011 entry.intent = new Intent(Intent.ACTION_SENDTO,
1012 Uri.fromParts("mailto", entry.data, null));
Evan Millar49714ee2009-09-02 16:42:47 -07001013 // Temporary hack until we get real label resources for exchange.
1014 if (TextUtils.isEmpty(entry.label)) {
1015 entry.label = getString(R.string.email).toLowerCase();
1016 }
Evan Millar11d628c2009-09-02 08:55:01 -07001017 entry.isPrimary = isSuperPrimary;
1018 mEmailEntries.add(entry);
1019 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
1020 equals(mimetype)) {
1021 // Build postal entries
1022 entry.maxLines = 4;
1023 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
1024 mPostalEntries.add(entry);
1025 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
1026 // Build im entries
1027 Object protocolObj = entryValues.getAsInteger(Data.DATA5);
1028 String host = null;
1029
Evan Millar49714ee2009-09-02 16:42:47 -07001030 if (TextUtils.isEmpty(entry.label)) {
1031 entry.label = getString(R.string.im).toLowerCase();
1032 }
1033
Evan Millar11d628c2009-09-02 08:55:01 -07001034 if (protocolObj instanceof Number) {
1035 int protocol = ((Number) protocolObj).intValue();
1036 host = ContactsUtils.lookupProviderNameFromId(
1037 protocol).toLowerCase();
1038 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
1039 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
1040 entry.maxLabelLines = 2;
1041 }
1042 } else if (protocolObj != null) {
1043 String providerName = (String) protocolObj;
1044 host = providerName.toLowerCase();
1045 }
1046
1047 // Only add the intent if there is a valid host
1048 if (!TextUtils.isEmpty(host)) {
1049 entry.intent = new Intent(Intent.ACTION_SENDTO,
1050 constructImToUrl(host, entry.data));
1051 }
1052 //TODO(emillar) Add in presence info
1053 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
1054 entry.presenceIcon = Presence.getPresenceIconResourceId(
1055 aggCursor.getInt(METHODS_STATUS_COLUMN));
1056 entry.status = ...
1057 }*/
1058 mImEntries.add(entry);
1059 }
1060 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
1061 // Build organization entries
1062 mOrganizationEntries.add(entry);
1063 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
1064 // Build note entries
1065 entry.id = 0;
1066 entry.uri = null;
1067 entry.intent = null;
1068 entry.maxLines = 10;
1069 mOtherEntries.add(entry);
1070 }
1071
1072
1073 // TODO(emillar) Add group entries
1074 // // Build the group entries
1075 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1076 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1077 // null, null, Groups.DEFAULT_SORT_ORDER);
1078 // if (groupCursor != null) {
1079 // try {
1080 // StringBuilder sb = new StringBuilder();
1081 //
1082 // while (groupCursor.moveToNext()) {
1083 // String systemId = groupCursor.getString(
1084 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1085 //
1086 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1087 // continue;
1088 // }
1089 //
1090 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1091 // if (!TextUtils.isEmpty(name)) {
1092 // if (sb.length() == 0) {
1093 // sb.append(name);
1094 // } else {
1095 // sb.append(getString(R.string.group_list, name));
1096 // }
1097 // }
1098 // }
1099 //
1100 // if (sb.length() > 0) {
1101 // ViewEntry entry = new ViewEntry();
1102 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1103 // entry.label = getString(R.string.label_groups);
1104 // entry.data = sb.toString();
1105 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1106 //
1107 // // TODO: Add an icon for the groups item.
1108 //
1109 // mGroupEntries.add(entry);
1110 // }
1111 // } finally {
1112 // groupCursor.close();
1113 // }
1114 // }
Evan Millar66388be2009-05-28 15:41:07 -07001115 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001116
Evan Millar66388be2009-05-28 15:41:07 -07001117 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001118 }
1119 }
1120
Evan Millar11d628c2009-09-02 08:55:01 -07001121 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1122 if (kind.actionHeader == null) {
1123 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001124 }
Evan Millar11d628c2009-09-02 08:55:01 -07001125 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1126 if (actionHeader == null) {
1127 return null;
1128 }
1129 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1130 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001131
Evan Millar11d628c2009-09-02 08:55:01 -07001132 String buildDataString(DataKind kind, ContentValues values) {
1133 if (kind.actionBody == null) {
1134 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001135 }
Evan Millar11d628c2009-09-02 08:55:01 -07001136 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1137 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001138 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001139
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001140 /**
1141 * A basic structure with the data for a contact entry in the list.
1142 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001143 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001144 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001145 public boolean isPrimary = false;
1146 public int presenceIcon = -1;
1147 public int secondaryActionIcon = -1;
1148 public Intent intent;
1149 public Intent secondaryIntent = null;
1150 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001151 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001152 public ArrayList<Long> ids = new ArrayList<Long>();
1153 public int collapseCount = 0;
1154
1155 public boolean collapseWith(ViewEntry entry) {
1156 // assert equal collapse keys
1157 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1158 return false;
1159 }
1160
1161 // Choose the label associated with the highest type precedence.
1162 if (TypePrecedence.getTypePrecedence(mimetype, type)
1163 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1164 type = entry.type;
1165 label = entry.label;
1166 }
1167
1168 // Choose the max of the maxLines and maxLabelLines values.
1169 maxLines = Math.max(maxLines, entry.maxLines);
1170 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1171
1172 // Choose the presence with the highest precedence.
1173 if (Presence.getPresencePrecedence(status)
1174 < Presence.getPresencePrecedence(entry.status)) {
1175 status = entry.status;
1176 }
1177
1178 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001179 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001180
1181 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1182
1183 // Keep track of all the ids that have been collapsed with this one.
1184 ids.add(entry.id);
1185 collapseCount++;
1186 return true;
1187 }
1188
1189 public String getCollapseKey() {
1190 StringBuilder hashSb = new StringBuilder();
1191 hashSb.append(data);
1192 hashSb.append(mimetype);
1193 hashSb.append((intent != null && intent.getAction() != null)
1194 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001195 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1196 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001197 hashSb.append(actionIcon);
1198 return hashSb.toString();
1199 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001200 }
1201
Evan Millar15e514d2009-08-04 10:14:57 -07001202 /** Cache of the children views of a row */
1203 static class ViewCache {
1204 public TextView label;
1205 public TextView data;
1206 public ImageView actionIcon;
1207 public ImageView presenceIcon;
1208 public ImageView primaryIcon;
1209 public ImageView secondaryActionButton;
1210 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001211
Evan Millar15e514d2009-08-04 10:14:57 -07001212 // Need to keep track of this too
1213 ViewEntry entry;
1214 }
1215
1216 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1217 implements View.OnClickListener {
1218
Evan Millar5c22c3b2009-05-29 11:37:54 -07001219
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001220 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1221 super(context, sections, SHOW_SEPARATORS);
1222 }
1223
Evan Millar15e514d2009-08-04 10:14:57 -07001224 public void onClick(View v) {
1225 Intent intent = (Intent) v.getTag();
1226 startActivity(intent);
1227 }
1228
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001229 @Override
1230 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001231 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001232 View v;
1233
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001234 ViewCache views;
1235
1236 // Check to see if we can reuse convertView
1237 if (convertView != null) {
1238 v = convertView;
1239 views = (ViewCache) v.getTag();
1240 } else {
1241 // Create a new view if needed
1242 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1243
1244 // Cache the children
1245 views = new ViewCache();
1246 views.label = (TextView) v.findViewById(android.R.id.text1);
1247 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001248 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1249 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1250 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1251 views.secondaryActionButton = (ImageView) v.findViewById(
1252 R.id.secondary_action_button);
1253 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001254 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001255 v.setTag(views);
1256 }
1257
1258 // Update the entry in the view cache
1259 views.entry = entry;
1260
1261 // Bind the data to the view
1262 bindView(v, entry);
1263 return v;
1264 }
1265
1266 @Override
1267 protected View newView(int position, ViewGroup parent) {
1268 // getView() handles this
1269 throw new UnsupportedOperationException();
1270 }
1271
1272 @Override
1273 protected void bindView(View view, ViewEntry entry) {
1274 final Resources resources = mContext.getResources();
1275 ViewCache views = (ViewCache) view.getTag();
1276
1277 // Set the label
1278 TextView label = views.label;
1279 setMaxLines(label, entry.maxLabelLines);
1280 label.setText(entry.label);
1281
1282 // Set the data
1283 TextView data = views.data;
1284 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001285 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1286 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1287 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1288 } else {
1289 data.setText(entry.data);
1290 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001291 setMaxLines(data, entry.maxLines);
1292 }
1293
Evan Millar15e514d2009-08-04 10:14:57 -07001294 // Set the primary icon
1295 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1296
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001297 // Set the action icon
1298 ImageView action = views.actionIcon;
1299 if (entry.actionIcon != -1) {
1300 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1301 action.setVisibility(View.VISIBLE);
1302 } else {
1303 // Things should still line up as if there was an icon, so make it invisible
1304 action.setVisibility(View.INVISIBLE);
1305 }
1306
1307 // Set the presence icon
1308 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001309 if (entry.presenceIcon != -1) {
1310 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001311 } else if (entry.status != -1) {
1312 presenceIcon = resources.getDrawable(
1313 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001314 }
Evan Millar15e514d2009-08-04 10:14:57 -07001315 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001316 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001317 presenceIconView.setImageDrawable(presenceIcon);
1318 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001319 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001320 presenceIconView.setVisibility(View.GONE);
1321 }
1322
1323 // Set the secondary action button
1324 ImageView secondaryActionView = views.secondaryActionButton;
1325 Drawable secondaryActionIcon = null;
1326 if (entry.secondaryActionIcon != -1) {
1327 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1328 }
1329 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1330 secondaryActionView.setImageDrawable(secondaryActionIcon);
1331 secondaryActionView.setTag(entry.secondaryIntent);
1332 secondaryActionView.setVisibility(View.VISIBLE);
1333 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1334 } else {
1335 secondaryActionView.setVisibility(View.GONE);
1336 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001337 }
1338 }
1339
1340 private void setMaxLines(TextView textView, int maxLines) {
1341 if (maxLines == 1) {
1342 textView.setSingleLine(true);
1343 textView.setEllipsize(TextUtils.TruncateAt.END);
1344 } else {
1345 textView.setSingleLine(false);
1346 textView.setMaxLines(maxLines);
1347 textView.setEllipsize(null);
1348 }
1349 }
1350 }
1351}