blob: b29a44cc69ad2a946ee84f00838a8993197662b3 [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;
Jeff Sharkey802b2052009-08-04 14:21:06 -070025import com.android.contacts.ui.FastTrackWindow;
Evan Millar11d628c2009-09-02 08:55:01 -070026import com.android.contacts.util.NotifyingAsyncQueryHandler;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080027import com.android.internal.telephony.ITelephony;
Evan Millar11d628c2009-09-02 08:55:01 -070028import com.android.internal.widget.ContactHeaderWidget;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080029
Evan Millar11d628c2009-09-02 08:55:01 -070030import android.app.Activity;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080031import android.app.AlertDialog;
32import android.app.Dialog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080033import android.content.ActivityNotFoundException;
34import android.content.ContentResolver;
35import android.content.ContentUris;
36import android.content.ContentValues;
37import android.content.Context;
38import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070039import android.content.Entity;
Evan Millar11d628c2009-09-02 08:55:01 -070040import android.content.EntityIterator;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080041import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070042import android.content.DialogInterface.OnClickListener;
Evan Millar11d628c2009-09-02 08:55:01 -070043import android.content.Entity.NamedContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080044import android.content.pm.PackageManager;
45import android.content.pm.ResolveInfo;
46import android.content.res.Resources;
47import android.database.ContentObserver;
48import android.database.Cursor;
Dmitri Plotnikov3d53ce22009-09-02 08:44:32 -070049import android.database.DatabaseUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080050import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080051import android.net.Uri;
52import android.os.Bundle;
53import android.os.Handler;
54import android.os.RemoteException;
55import android.os.ServiceManager;
Dmitri Plotnikov3d53ce22009-09-02 08:44:32 -070056import android.provider.ContactsContract;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070057import android.provider.ContactsContract.AggregationExceptions;
58import android.provider.ContactsContract.CommonDataKinds;
Evan Millar11d628c2009-09-02 08:55:01 -070059import android.provider.ContactsContract.Contacts;
Evan Millar66388be2009-05-28 15:41:07 -070060import android.provider.ContactsContract.Data;
Evan Millar54a5c9f2009-06-23 17:41:09 -070061import android.provider.ContactsContract.Presence;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070062import android.provider.ContactsContract.RawContacts;
Evan Millar54a5c9f2009-06-23 17:41:09 -070063import android.provider.ContactsContract.CommonDataKinds.Phone;
64import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080065import android.text.TextUtils;
66import android.util.Log;
Evan Millar11d628c2009-09-02 08:55:01 -070067import android.util.SparseArray;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080068import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070069import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080070import android.view.KeyEvent;
Evan Millar11d628c2009-09-02 08:55:01 -070071import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080072import android.view.Menu;
73import android.view.MenuItem;
74import android.view.View;
75import android.view.ViewGroup;
Evan Millar11d628c2009-09-02 08:55:01 -070076import android.view.Window;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080077import android.view.ContextMenu.ContextMenuInfo;
78import android.widget.AdapterView;
Evan Millar7911ff52009-07-21 15:55:18 -070079import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080080import android.widget.ImageView;
81import android.widget.ListView;
82import android.widget.TextView;
83import android.widget.Toast;
84
85import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080086
87/**
88 * Displays the details of a specific contact.
89 */
Evan Millar11d628c2009-09-02 08:55:01 -070090public class ViewContactActivity extends Activity
Evan Millar7911ff52009-07-21 15:55:18 -070091 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
Evan Millar11d628c2009-09-02 08:55:01 -070092 AdapterView.OnItemClickListener, NotifyingAsyncQueryHandler.AsyncQueryListener,
93 OnTabSelectionChangedListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080094 private static final String TAG = "ViewContact";
95 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
96
Evan Millar8a79cee2009-08-19 17:20:49 -070097 public static final String RAW_CONTACT_ID_EXTRA = "rawContactIdExtra";
98
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080099 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
Evan Millar8a79cee2009-08-19 17:20:49 -0700103 private static final int REQUEST_JOIN_CONTACT = 1;
104 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700105
Evan Millar8a79cee2009-08-19 17:20:49 -0700106 public static final int MENU_ITEM_EDIT = 1;
107 public static final int MENU_ITEM_DELETE = 2;
108 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
109 public static final int MENU_ITEM_SHOW_BARCODE = 4;
110 public static final int MENU_ITEM_SPLIT_AGGREGATE = 5;
111 public static final int MENU_ITEM_JOIN_AGGREGATE = 6;
112 public static final int MENU_ITEM_OPTIONS = 7;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800113
Evan Millar11d628c2009-09-02 08:55:01 -0700114 protected Uri mOriginalUri;
115 private Uri mUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800116 private ContentResolver mResolver;
117 private ViewAdapter mAdapter;
118 private int mNumPhoneNumbers = 0;
119
Evan Millar8a79cee2009-08-19 17:20:49 -0700120 private static final long ALL_CONTACTS_ID = -100;
Evan Millar7911ff52009-07-21 15:55:18 -0700121
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700122 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700123 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700124 */
Evan Millar7911ff52009-07-21 15:55:18 -0700125 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700126
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800127 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
128 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
129 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
130 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
131 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
132 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700133 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800134 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
135 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
136
137 private Cursor mCursor;
138 private boolean mObserverRegistered;
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) {
177 if (mCursor != null) {
178 if (mObserverRegistered) {
179 mCursor.unregisterContentObserver(mObserver);
180 mObserverRegistered = false;
181 }
182 mCursor.close();
183 mCursor = null;
184 }
185 getContentResolver().delete(mUri, null, null);
186 finish();
187 }
188
Evan Millar7911ff52009-07-21 15:55:18 -0700189 private FrameLayout mTabContentLayout;
190 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800191 private boolean mShowSmsLinksForAllPhones;
Evan Millar11d628c2009-09-02 08:55:01 -0700192 private ArrayList<Entity> mEntities = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800193
194 @Override
195 protected void onCreate(Bundle icicle) {
196 super.onCreate(icicle);
197
Evan Millar11d628c2009-09-02 08:55:01 -0700198 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
199
200 final Intent intent = getIntent();
201 mUri = intent.getData();
202 resolveContactUriFromIntent(intent);
203
204 requestWindowFeature(Window.FEATURE_NO_TITLE);
205 setContentView(R.layout.contact_card_layout);
206
207 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
208 mContactHeaderWidget.showStar(true);
209 mContactHeaderWidget.bindFromContactId(ContentUris.parseId(mUri));
210 mContactHeaderWidget.setExcludeMimes(new String[] {
211 Contacts.CONTENT_ITEM_TYPE
212 });
213
214 mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget);
215 mTabWidget.setTabSelectionListener(this);
216 mTabWidget.setVisibility(View.INVISIBLE);
217
218 mTabRawContactIdMap = new SparseArray<Long>();
219
220 mHandler = new NotifyingAsyncQueryHandler(this, this);
221
Evan Millar7911ff52009-07-21 15:55:18 -0700222 mListView = new ListView(this);
223 mListView.setOnCreateContextMenuListener(this);
224 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
225 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800226
Evan Millar7911ff52009-07-21 15:55:18 -0700227 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
228 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800229
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800230 mResolver = getContentResolver();
231
232 // Build the list of sections. The order they're added to mSections dictates the
233 // order they are displayed in the list.
234 mSections.add(mPhoneEntries);
235 mSections.add(mSmsEntries);
236 mSections.add(mEmailEntries);
237 mSections.add(mImEntries);
238 mSections.add(mPostalEntries);
239 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700240 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800241 mSections.add(mOtherEntries);
242
243 //TODO Read this value from a preference
244 mShowSmsLinksForAllPhones = true;
245
Evan Millar11d628c2009-09-02 08:55:01 -0700246 //Stub query so we can get notifications.
Evan Millar8a79cee2009-08-19 17:20:49 -0700247 mCursor = mResolver.query(Uri.withAppendedPath(mUri, "data"),
Evan Millar11d628c2009-09-02 08:55:01 -0700248 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
249
250 startEntityQuery();
251 }
252
253 private void resolveContactUriFromIntent(final Intent intent) {
254 mOriginalUri = intent.getData();
255 mUri = ContactsContract.Contacts.lookupContact(getContentResolver(), mOriginalUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800256 }
257
258 @Override
259 protected void onResume() {
260 super.onResume();
261 mObserverRegistered = true;
262 mCursor.registerContentObserver(mObserver);
Evan Millar11d628c2009-09-02 08:55:01 -0700263 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800264 }
265
266 @Override
267 protected void onPause() {
268 super.onPause();
269 if (mCursor != null) {
270 if (mObserverRegistered) {
271 mObserverRegistered = false;
272 mCursor.unregisterContentObserver(mObserver);
273 }
274 mCursor.deactivate();
275 }
276 }
277
278 @Override
279 protected void onDestroy() {
280 super.onDestroy();
281
282 if (mCursor != null) {
283 if (mObserverRegistered) {
284 mCursor.unregisterContentObserver(mObserver);
285 mObserverRegistered = false;
286 }
287 mCursor.close();
288 }
289 }
290
291 @Override
Evan Millar11d628c2009-09-02 08:55:01 -0700292 protected void onRestoreInstanceState(Bundle savedInstanceState) {
293 super.onRestoreInstanceState(savedInstanceState);
294 mSelectedRawContactId = savedInstanceState.getLong(SELECTED_RAW_CONTACT_ID_KEY);
295 }
296
297 @Override
298 protected void onSaveInstanceState(Bundle outState) {
299 super.onSaveInstanceState(outState);
300 outState.putLong(SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
301 }
302
303 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800304 protected Dialog onCreateDialog(int id) {
305 switch (id) {
306 case DIALOG_CONFIRM_DELETE:
307 return new AlertDialog.Builder(this)
308 .setTitle(R.string.deleteConfirmation_title)
309 .setIcon(android.R.drawable.ic_dialog_alert)
310 .setMessage(R.string.deleteConfirmation)
311 .setNegativeButton(android.R.string.cancel, null)
312 .setPositiveButton(android.R.string.ok, this)
313 .setCancelable(false)
314 .create();
315 }
316 return null;
317 }
318
Evan Millar11d628c2009-09-02 08:55:01 -0700319
320 // TAB CODE //
321 /**
322 * Adds a tab for each {@link RawContact} associated with this contact.
323 * Override this method if you want to additional tabs and/or different
324 * tabs for your activity.
325 *
326 * @param entities An {@link ArrayList} of {@link Entity}s of all the RawContacts
327 * associated with the contact being displayed.
328 */
329 protected void bindTabs() {
330 if (mEntities.size() > 1) {
Evan Millar7911ff52009-07-21 15:55:18 -0700331 addAllTab();
332 }
Evan Millar11d628c2009-09-02 08:55:01 -0700333
334 final Sources sources = Sources.getInstance(this);
335
336 for (Entity entity : mEntities) {
337 final String accountType = entity.getEntityValues().
338 getAsString(RawContacts.ACCOUNT_TYPE);
339 final Long rawContactId = entity.getEntityValues().
340 getAsLong(RawContacts._ID);
341
342 // TODO: ensure inflation on background task so we don't block UI thread here
343 final ContactsSource source = sources.getInflatedSource(accountType,
344 ContactsSource.LEVEL_SUMMARY);
345 addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(), source));
346 }
347
348 selectInitialTab();
349 mTabWidget.setVisibility(View.VISIBLE);
350 mTabWidget.postInvalidate();
351 }
352
353 /**
354 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
355 *
356 * @param contactId The contact id associated with the tab.
357 * @param view A view to use as the tab indicator.
358 */
359 protected void addTab(long rawContactId, View view) {
360 mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId);
361 mTabWidget.addTab(view);
362 }
363
364
365 protected void clearCurrentTabs() {
366 mTabRawContactIdMap.clear();
367 mTabWidget.removeAllTabs();
368 }
369
370 protected void selectInitialTab() {
371 int selectedTabIndex = 0;
372
373 if (mSelectedRawContactId != null) {
374 selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
375 if (selectedTabIndex == -1) {
376 // If there was no matching tab, just select the first;
377 selectedTabIndex = 0;
378 }
379 }
380
381 mTabWidget.setCurrentTab(selectedTabIndex);
382 onTabSelectionChanged(selectedTabIndex, false);
383 }
384
385 @Override
386 public void onNewIntent(Intent newIntent) {
387 setIntent(newIntent);
388 resolveContactUriFromIntent(newIntent);
389 selectInitialTab();
Evan Millar7911ff52009-07-21 15:55:18 -0700390 }
391
392 private void addAllTab() {
Evan Millar56d2caa2009-08-20 20:30:12 -0700393 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator,
394 mTabWidget.getTabParent(), false);
Evan Millar74660912009-08-19 17:36:33 -0700395 allTabIndicator.getBackground().setDither(true);
Evan Millar7911ff52009-07-21 15:55:18 -0700396 addTab(ALL_CONTACTS_ID, allTabIndicator);
397 }
398
399 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
400 long rawContactId = getTabRawContactId(tabIndex);
Evan Millar8a79cee2009-08-19 17:20:49 -0700401 mSelectedRawContactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700402 bindData();
Evan Millar7911ff52009-07-21 15:55:18 -0700403 }
404
Evan Millar11d628c2009-09-02 08:55:01 -0700405 /**
406 * Return the RawContact id associated with the tab at an index.
407 *
408 * @param index The index of the tab in question.
409 * @return The contactId associated with the tab at the specified index.
410 */
411 protected long getTabRawContactId(int index) {
412 return mTabRawContactIdMap.get(index);
413 }
Evan Millar2c1cc832009-07-13 11:08:06 -0700414
Evan Millar11d628c2009-09-02 08:55:01 -0700415 /**
416 * Return the tab index associated with the RawContact id.
417 *
418 * @param index The index of the tab in question.
419 * @return The contactId associated with the tab at the specified index.
420 */
421 protected int getTabIndexForRawContactId(long rawContactId) {
422 int numTabs = mTabRawContactIdMap.size();
423 for (int i=0; i < numTabs; i++) {
424 if (mTabRawContactIdMap.get(i) == rawContactId) {
425 return i;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800426 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800427 }
Evan Millar11d628c2009-09-02 08:55:01 -0700428 return -1;
429 }
430
431
432 // QUERY CODE //
433 /** {@inheritDoc} */
434 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
435 try{
436 if (token == TOKEN_QUERY) {
437 clearCurrentTabs();
438 mEntities = readEntities(iterator);
439 bindTabs();
440 bindData();
441 }
442 } finally {
443 if (iterator != null) {
444 iterator.close();
445 }
446 }
447 }
448
449 /** {@inheritDoc} */
450 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
451 // Emtpy
452 }
453
454 private ArrayList<Entity> readEntities(EntityIterator iterator) {
455 ArrayList<Entity> entities = new ArrayList<Entity>();
456 try {
457 while (iterator.hasNext()) {
458 entities.add(iterator.next());
459 }
460 } catch (RemoteException e) {
461 }
462
463 return entities;
464 }
465
466 private void startEntityQuery() {
467 long contactId = ContentUris.parseId(mUri);
468 mHandler.startQueryEntities(TOKEN_QUERY, null,
469 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
470 }
471
472 private void bindData() {
473
474 // Build up the contact entries
475 buildEntries();
476
477 // Collapse similar data items in select sections.
478 Collapser.collapseList(mPhoneEntries);
479 Collapser.collapseList(mSmsEntries);
480 Collapser.collapseList(mEmailEntries);
481 Collapser.collapseList(mPostalEntries);
482
483 if (mAdapter == null) {
484 mAdapter = new ViewAdapter(this, mSections);
485 mListView.setAdapter(mAdapter);
486 } else {
487 mAdapter.setSections(mSections, SHOW_SEPARATORS);
488 }
489
490// else {
491// Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
492// Log.e(TAG, "invalid contact uri: " + mOriginalUri);
493// finish();
494// }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800495 }
496
497 @Override
498 public boolean onCreateOptionsMenu(Menu menu) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800499 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
500 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700501 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
502 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700503 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
504 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700505 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
506 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800507 return true;
508 }
509
510 @Override
511 public boolean onPrepareOptionsMenu(Menu menu) {
512 super.onPrepareOptionsMenu(menu);
513 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
514 // could be installed or uninstalled at any time.
515 if (isBarcodeScannerInstalled()) {
516 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
517 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
518 .setIcon(R.drawable.ic_menu_show_barcode);
519 }
520 } else {
521 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
522 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700523
Evan Millardb5d88c2009-08-28 09:31:57 -0700524 // Only show the edit option if we have a selected tab.
525 if (mSelectedRawContactId != null) {
526 if (menu.findItem(MENU_ITEM_EDIT) == null) {
527 menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_editContact)
528 .setIcon(android.R.drawable.ic_menu_edit)
529 .setAlphabeticShortcut('e');
530 }
531 } else {
532 menu.removeItem(MENU_ITEM_EDIT);
533 }
534
Evan Millar7911ff52009-07-21 15:55:18 -0700535 boolean isAggregate = mRawContactIds.size() > 1;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700536 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800537 return true;
538 }
539
540 private boolean isBarcodeScannerInstalled() {
541 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
542 ResolveInfo ri = getPackageManager().resolveActivity(intent,
543 PackageManager.MATCH_DEFAULT_ONLY);
544 return ri != null;
545 }
546
547 @Override
548 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
549 AdapterView.AdapterContextMenuInfo info;
550 try {
551 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
552 } catch (ClassCastException e) {
553 Log.e(TAG, "bad menuInfo", e);
554 return;
555 }
556
557 // This can be null sometimes, don't crash...
558 if (info == null) {
559 Log.e(TAG, "bad menuInfo");
560 return;
561 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700562
Evan Millar45e0ed32009-06-01 16:44:38 -0700563 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
564 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
565 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700566 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
567 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700568 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800569 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700570 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
571 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700572 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700573 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800574 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700575 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700576 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
577 }
578 // TODO(emillar): add back with group support.
579 /* else if (entry.mimetype.equals()) {
580 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
581 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800582 }
583
584 @Override
585 public boolean onOptionsItemSelected(MenuItem item) {
586 switch (item.getItemId()) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700587 case MENU_ITEM_EDIT: {
Evan Millardb5d88c2009-08-28 09:31:57 -0700588 Long rawContactIdToEdit = mSelectedRawContactId;
589 if (rawContactIdToEdit == null) {
590 // This shouldn't be possible. We only show the edit option if
591 // this value is non-null.
592 break;
593 }
Evan Millar8a79cee2009-08-19 17:20:49 -0700594 if (rawContactIdToEdit == ALL_CONTACTS_ID) {
595 // If the "all" tab is selected, edit the next tab.
Evan Millardb5d88c2009-08-28 09:31:57 -0700596 rawContactIdToEdit = getTabRawContactId(mTabWidget.getCurrentTab() + 1);
Evan Millar8a79cee2009-08-19 17:20:49 -0700597 }
598 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
599 rawContactIdToEdit);
600 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
601 REQUEST_EDIT_CONTACT);
602 break;
603 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800604 case MENU_ITEM_DELETE: {
605 // Get confirmation
606 showDialog(DIALOG_CONFIRM_DELETE);
607 return true;
608 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700609
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700610 case MENU_ITEM_SPLIT_AGGREGATE: {
611 showSplitAggregateDialog();
612 return true;
613 }
614
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700615 case MENU_ITEM_JOIN_AGGREGATE: {
616 showJoinAggregateActivity();
617 return true;
618 }
619
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700620 case MENU_ITEM_OPTIONS: {
621 showOptionsActivity();
622 return true;
623 }
624
Evan Millar66388be2009-05-28 15:41:07 -0700625 // TODO(emillar) Bring this back.
626 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800627 if (mCursor.moveToFirst()) {
628 Intent intent = new Intent(SHOW_BARCODE_INTENT);
629 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
630 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700631 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800632 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700633 // Correctly handle when section headers are hidden
634 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700635
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800636 bundle.putString(Contacts.Intents.Insert.NAME, name);
637 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700638 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800639 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700640 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800641 bundle.putString(PHONE_KEYS[x], entry.data);
642 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700643 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800644 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700645 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800646 bundle.putString(EMAIL_KEYS[x], entry.data);
647 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700648 if (mPostalEntries.size() >= 1 + sepAdjust) {
649 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800650 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
651 }
652 intent.putExtra("ENCODE_DATA", bundle);
653 try {
654 startActivity(intent);
655 } catch (ActivityNotFoundException e) {
656 // The check in onPrepareOptionsMenu() should make this impossible, but
657 // for safety I'm catching the exception rather than crashing. Ideally
658 // I'd call Menu.removeItem() here too, but I don't see a way to get
659 // the options menu.
660 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
661 "was not installed.");
662 }
663 return true;
664 }
665 }
Evan Millar66388be2009-05-28 15:41:07 -0700666 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800667 }
668 return super.onOptionsItemSelected(item);
669 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700670
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800671 @Override
672 public boolean onContextItemSelected(MenuItem item) {
673 switch (item.getItemId()) {
674 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700675 if (makeItemDefault(item)) {
676 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800677 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700678 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800679 }
680 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700681
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800682 return super.onContextItemSelected(item);
683 }
684
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700685 private boolean makeItemDefault(MenuItem item) {
686 ViewEntry entry = getViewEntryForMenuItem(item);
687 if (entry == null) {
688 return false;
689 }
690
691 // Update the primary values in the data record.
692 ContentValues values = new ContentValues(2);
693 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700694
Evan Millar54a5c9f2009-06-23 17:41:09 -0700695 if (entry.ids.size() > 0) {
696 for (int i = 0; i < entry.ids.size(); i++) {
697 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
698 entry.ids.get(i)),
699 values, null, null);
700 }
701 }
702
703 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700704 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
705 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700706 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700707 return true;
708 }
709
710 /**
711 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
712 * The user picks a contact to be split into its own aggregate or clicks Cancel.
713 */
714 private void showSplitAggregateDialog() {
715
716 // Wrap this dialog in a specific theme so that list items have correct text color.
717 final ContextThemeWrapper dialogContext =
718 new ContextThemeWrapper(this, android.R.style.Theme_Light);
719 AlertDialog.Builder builder =
720 new AlertDialog.Builder(dialogContext);
721 builder.setTitle(getString(R.string.splitAggregate_title));
722
723 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
724 builder.setView(view);
725
726 builder.setInverseBackgroundForced(true);
727 builder.setCancelable(true);
728 builder.setNegativeButton(android.R.string.cancel,
729 new OnClickListener() {
730 public void onClick(DialogInterface dialog, int which) {
731 dialog.dismiss();
732 }
733 });
734 final AlertDialog dialog = builder.create();
735
736 view.setOnContactSelectedListener(new OnContactSelectedListener() {
737 public void onContactSelected(long contactId) {
738 dialog.dismiss();
739 splitContact(contactId);
740 }
741 });
742
743 dialog.show();
744 }
745
746 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700747 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700748 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700749 public void showJoinAggregateActivity() {
750 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
751 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700752 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700753 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700754
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700755 @Override
756 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700757 switch (requestCode) {
758 case REQUEST_JOIN_CONTACT: {
759 if (resultCode == RESULT_OK && intent != null) {
760 final long aggregateId = ContentUris.parseId(intent.getData());
761 joinAggregate(aggregateId);
762 }
763 break;
764 }
765 case REQUEST_EDIT_CONTACT: {
766 if (resultCode == RESULT_OK && intent != null) {
767 long newInitialSelectedRawContactId = intent.getLongExtra(
768 RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
769 if (newInitialSelectedRawContactId != mSelectedRawContactId) {
770 mSelectedRawContactId = newInitialSelectedRawContactId;
771 selectInitialTab();
772 }
773 }
774 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700775 }
776 }
777
778 private void splitContact(long contactId) {
779 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_OUT);
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700780 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700781 mAdapter.notifyDataSetChanged();
782 }
783
784 private void joinAggregate(final long aggregateId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700785 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700786 RawContacts.CONTACT_ID + "=" + aggregateId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700787
788 try {
789 while(c.moveToNext()) {
790 long contactId = c.getLong(0);
791 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
792 }
793 } finally {
794 c.close();
795 }
796
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700797 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700798 mAdapter.notifyDataSetChanged();
799 }
800
801 /**
802 * Given a contact ID sets an aggregation exception to either join the contact with the
803 * current aggregate or split off.
804 */
805 protected void setAggregationException(long contactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700806 ContentValues values = new ContentValues(3);
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700807 values.put(AggregationExceptions.CONTACT_ID, ContentUris.parseId(mUri));
Jeff Sharkey14f61ab2009-08-05 21:02:37 -0700808 values.put(AggregationExceptions.RAW_CONTACT_ID, contactId);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700809 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700810 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700811 }
812
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700813 private void showOptionsActivity() {
814 final Intent intent = new Intent(this, ContactOptionsActivity.class);
815 intent.setData(mUri);
816 startActivity(intent);
817 }
818
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700819 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
820 AdapterView.AdapterContextMenuInfo info;
821 try {
822 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
823 } catch (ClassCastException e) {
824 Log.e(TAG, "bad menuInfo", e);
825 return null;
826 }
827
828 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
829 }
830
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800831 @Override
832 public boolean onKeyDown(int keyCode, KeyEvent event) {
833 switch (keyCode) {
834 case KeyEvent.KEYCODE_CALL: {
835 try {
836 ITelephony phone = ITelephony.Stub.asInterface(
837 ServiceManager.checkService("phone"));
838 if (phone != null && !phone.isIdle()) {
839 // Skip out and let the key be handled at a higher level
840 break;
841 }
842 } catch (RemoteException re) {
843 // Fall through and try to call the contact
844 }
845
Evan Millar7911ff52009-07-21 15:55:18 -0700846 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800847 if (index != -1) {
848 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700849 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
850 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800851 }
852 } else if (mNumPhoneNumbers != 0) {
853 // There isn't anything selected, call the default number
854 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
855 startActivity(intent);
856 }
857 return true;
858 }
859
860 case KeyEvent.KEYCODE_DEL: {
861 showDialog(DIALOG_CONFIRM_DELETE);
862 return true;
863 }
864 }
865
866 return super.onKeyDown(keyCode, event);
867 }
868
Evan Millar7911ff52009-07-21 15:55:18 -0700869 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800870 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
871 if (entry != null) {
872 Intent intent = entry.intent;
873 if (intent != null) {
874 try {
875 startActivity(intent);
876 } catch (ActivityNotFoundException e) {
877 Log.e(TAG, "No activity found for intent: " + intent);
878 signalError();
879 }
880 } else {
881 signalError();
882 }
883 } else {
884 signalError();
885 }
886 }
887
888 /**
889 * Signal an error to the user via a beep, or some other method.
890 */
891 private void signalError() {
892 //TODO: implement this when we have the sonification APIs
893 }
894
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800895 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700896 // 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 -0800897 StringBuilder buf = new StringBuilder("imto://");
898 buf.append(host);
899 buf.append('/');
900 buf.append(data);
901 return Uri.parse(buf.toString());
902 }
903
904 /**
905 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700906 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800907 * @param personCursor the URI for the contact being displayed
908 */
Evan Millar11d628c2009-09-02 08:55:01 -0700909 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800910 // Clear out the old entries
911 final int numSections = mSections.size();
912 for (int i = 0; i < numSections; i++) {
913 mSections.get(i).clear();
914 }
915
Evan Millar7911ff52009-07-21 15:55:18 -0700916 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700917
Evan Millar11d628c2009-09-02 08:55:01 -0700918 Sources sources = Sources.getInstance(this);
919
Evan Millar66388be2009-05-28 15:41:07 -0700920 // Build up method entries
921 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700922 for (Entity entity: mEntities) {
923 final ContentValues entValues = entity.getEntityValues();
924 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Evan Millar7911ff52009-07-21 15:55:18 -0700925 // TODO: entry.contactId should be renamed to entry.rawContactId
Evan Millar11d628c2009-09-02 08:55:01 -0700926 long contactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700927
Evan Millar11d628c2009-09-02 08:55:01 -0700928 for (NamedContentValues subValue : entity.getSubValues()) {
929 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800930
Evan Millar11d628c2009-09-02 08:55:01 -0700931 ContentValues entryValues = subValue.values;
932 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
933 if (mimetype == null || accountType == null) {
Evan Millar66388be2009-05-28 15:41:07 -0700934 continue;
935 }
936
Evan Millar11d628c2009-09-02 08:55:01 -0700937 ContactsSource contactsSource = sources.getInflatedSource(accountType,
938 ContactsSource.LEVEL_MIMETYPES);
939 if (contactsSource == null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800940 continue;
941 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700942
Evan Millar11d628c2009-09-02 08:55:01 -0700943 DataKind kind = contactsSource.getKindForMimetype(mimetype);
944 if (kind == null) {
945 continue;
946 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800947
Evan Millar11d628c2009-09-02 08:55:01 -0700948 final long id = entryValues.getAsLong(Data._ID);
949 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
950 entry.contactId = contactId;
951 entry.id = id;
952 entry.uri = uri;
953 entry.mimetype = mimetype;
954 entry.label = buildActionString(kind, entryValues, true);
955 entry.data = buildDataString(kind, entryValues);
956 if (kind.typeColumn != null) {
957 entry.type = entryValues.getAsInteger(kind.typeColumn);
958 }
959 if (kind.iconRes > 0) {
960 entry.actionIcon = kind.iconRes;
961 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800962
Evan Millar11d628c2009-09-02 08:55:01 -0700963 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700964 if (TextUtils.isEmpty(entry.data)) {
965 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700966 continue;
967 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700968
Evan Millar11d628c2009-09-02 08:55:01 -0700969 if (!mRawContactIds.contains(entry.contactId)) {
970 mRawContactIds.add(entry.contactId);
971 }
972
973 // This performs the tab filtering
974 if (mSelectedRawContactId != null
975 && mSelectedRawContactId != entry.contactId
976 && mSelectedRawContactId != ALL_CONTACTS_ID) {
977 continue;
978 }
979
980 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)
981 || CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)
982 || CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)
983 || CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
984 final boolean isSuperPrimary = entryValues.getAsInteger(
985 Data.IS_SUPER_PRIMARY) != 0;
986
987 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
988 // Build phone entries
989 mNumPhoneNumbers++;
990
991 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
992 Uri.fromParts("tel", entry.data, null));
993 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
994 Uri.fromParts("sms", entry.data, null));
995 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
996 entry.isPrimary = isSuperPrimary;
997 mPhoneEntries.add(entry);
998
999 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
1000 || mShowSmsLinksForAllPhones) {
1001 // Add an SMS entry
1002 if (kind.iconAltRes > 0) {
1003 entry.secondaryActionIcon = kind.iconAltRes;
1004 }
1005 }
1006 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
1007 // Build email entries
1008 entry.intent = new Intent(Intent.ACTION_SENDTO,
1009 Uri.fromParts("mailto", entry.data, null));
1010 entry.isPrimary = isSuperPrimary;
1011 mEmailEntries.add(entry);
1012 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
1013 equals(mimetype)) {
1014 // Build postal entries
1015 entry.maxLines = 4;
1016 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
1017 mPostalEntries.add(entry);
1018 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
1019 // Build im entries
1020 Object protocolObj = entryValues.getAsInteger(Data.DATA5);
1021 String host = null;
1022
1023 if (protocolObj instanceof Number) {
1024 int protocol = ((Number) protocolObj).intValue();
1025 host = ContactsUtils.lookupProviderNameFromId(
1026 protocol).toLowerCase();
1027 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
1028 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
1029 entry.maxLabelLines = 2;
1030 }
1031 } else if (protocolObj != null) {
1032 String providerName = (String) protocolObj;
1033 host = providerName.toLowerCase();
1034 }
1035
1036 // Only add the intent if there is a valid host
1037 if (!TextUtils.isEmpty(host)) {
1038 entry.intent = new Intent(Intent.ACTION_SENDTO,
1039 constructImToUrl(host, entry.data));
1040 }
1041 //TODO(emillar) Add in presence info
1042 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
1043 entry.presenceIcon = Presence.getPresenceIconResourceId(
1044 aggCursor.getInt(METHODS_STATUS_COLUMN));
1045 entry.status = ...
1046 }*/
1047 mImEntries.add(entry);
1048 }
1049 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
1050 // Build organization entries
1051 mOrganizationEntries.add(entry);
1052 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
1053 // Build note entries
1054 entry.id = 0;
1055 entry.uri = null;
1056 entry.intent = null;
1057 entry.maxLines = 10;
1058 mOtherEntries.add(entry);
1059 }
1060
1061
1062 // TODO(emillar) Add group entries
1063 // // Build the group entries
1064 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1065 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1066 // null, null, Groups.DEFAULT_SORT_ORDER);
1067 // if (groupCursor != null) {
1068 // try {
1069 // StringBuilder sb = new StringBuilder();
1070 //
1071 // while (groupCursor.moveToNext()) {
1072 // String systemId = groupCursor.getString(
1073 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1074 //
1075 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1076 // continue;
1077 // }
1078 //
1079 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1080 // if (!TextUtils.isEmpty(name)) {
1081 // if (sb.length() == 0) {
1082 // sb.append(name);
1083 // } else {
1084 // sb.append(getString(R.string.group_list, name));
1085 // }
1086 // }
1087 // }
1088 //
1089 // if (sb.length() > 0) {
1090 // ViewEntry entry = new ViewEntry();
1091 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1092 // entry.label = getString(R.string.label_groups);
1093 // entry.data = sb.toString();
1094 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1095 //
1096 // // TODO: Add an icon for the groups item.
1097 //
1098 // mGroupEntries.add(entry);
1099 // }
1100 // } finally {
1101 // groupCursor.close();
1102 // }
1103 // }
Evan Millar66388be2009-05-28 15:41:07 -07001104 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001105
Evan Millar66388be2009-05-28 15:41:07 -07001106 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001107 }
1108 }
1109
Evan Millar11d628c2009-09-02 08:55:01 -07001110 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1111 if (kind.actionHeader == null) {
1112 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001113 }
Evan Millar11d628c2009-09-02 08:55:01 -07001114 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1115 if (actionHeader == null) {
1116 return null;
1117 }
1118 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1119 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001120
Evan Millar11d628c2009-09-02 08:55:01 -07001121 String buildDataString(DataKind kind, ContentValues values) {
1122 if (kind.actionBody == null) {
1123 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001124 }
Evan Millar11d628c2009-09-02 08:55:01 -07001125 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1126 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001127 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001128
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001129 /**
1130 * A basic structure with the data for a contact entry in the list.
1131 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001132 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001133 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001134 public boolean isPrimary = false;
1135 public int presenceIcon = -1;
1136 public int secondaryActionIcon = -1;
1137 public Intent intent;
1138 public Intent secondaryIntent = null;
1139 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001140 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001141 public ArrayList<Long> ids = new ArrayList<Long>();
1142 public int collapseCount = 0;
1143
1144 public boolean collapseWith(ViewEntry entry) {
1145 // assert equal collapse keys
1146 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1147 return false;
1148 }
1149
1150 // Choose the label associated with the highest type precedence.
1151 if (TypePrecedence.getTypePrecedence(mimetype, type)
1152 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1153 type = entry.type;
1154 label = entry.label;
1155 }
1156
1157 // Choose the max of the maxLines and maxLabelLines values.
1158 maxLines = Math.max(maxLines, entry.maxLines);
1159 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1160
1161 // Choose the presence with the highest precedence.
1162 if (Presence.getPresencePrecedence(status)
1163 < Presence.getPresencePrecedence(entry.status)) {
1164 status = entry.status;
1165 }
1166
1167 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001168 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001169
1170 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1171
1172 // Keep track of all the ids that have been collapsed with this one.
1173 ids.add(entry.id);
1174 collapseCount++;
1175 return true;
1176 }
1177
1178 public String getCollapseKey() {
1179 StringBuilder hashSb = new StringBuilder();
1180 hashSb.append(data);
1181 hashSb.append(mimetype);
1182 hashSb.append((intent != null && intent.getAction() != null)
1183 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001184 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1185 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001186 hashSb.append(actionIcon);
1187 return hashSb.toString();
1188 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001189 }
1190
Evan Millar15e514d2009-08-04 10:14:57 -07001191 /** Cache of the children views of a row */
1192 static class ViewCache {
1193 public TextView label;
1194 public TextView data;
1195 public ImageView actionIcon;
1196 public ImageView presenceIcon;
1197 public ImageView primaryIcon;
1198 public ImageView secondaryActionButton;
1199 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001200
Evan Millar15e514d2009-08-04 10:14:57 -07001201 // Need to keep track of this too
1202 ViewEntry entry;
1203 }
1204
1205 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1206 implements View.OnClickListener {
1207
Evan Millar5c22c3b2009-05-29 11:37:54 -07001208
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001209 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1210 super(context, sections, SHOW_SEPARATORS);
1211 }
1212
Evan Millar15e514d2009-08-04 10:14:57 -07001213 public void onClick(View v) {
1214 Intent intent = (Intent) v.getTag();
1215 startActivity(intent);
1216 }
1217
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001218 @Override
1219 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001220 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001221 View v;
1222
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001223 ViewCache views;
1224
1225 // Check to see if we can reuse convertView
1226 if (convertView != null) {
1227 v = convertView;
1228 views = (ViewCache) v.getTag();
1229 } else {
1230 // Create a new view if needed
1231 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1232
1233 // Cache the children
1234 views = new ViewCache();
1235 views.label = (TextView) v.findViewById(android.R.id.text1);
1236 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001237 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1238 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1239 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1240 views.secondaryActionButton = (ImageView) v.findViewById(
1241 R.id.secondary_action_button);
1242 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001243 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001244 v.setTag(views);
1245 }
1246
1247 // Update the entry in the view cache
1248 views.entry = entry;
1249
1250 // Bind the data to the view
1251 bindView(v, entry);
1252 return v;
1253 }
1254
1255 @Override
1256 protected View newView(int position, ViewGroup parent) {
1257 // getView() handles this
1258 throw new UnsupportedOperationException();
1259 }
1260
1261 @Override
1262 protected void bindView(View view, ViewEntry entry) {
1263 final Resources resources = mContext.getResources();
1264 ViewCache views = (ViewCache) view.getTag();
1265
1266 // Set the label
1267 TextView label = views.label;
1268 setMaxLines(label, entry.maxLabelLines);
1269 label.setText(entry.label);
1270
1271 // Set the data
1272 TextView data = views.data;
1273 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001274 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1275 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1276 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1277 } else {
1278 data.setText(entry.data);
1279 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001280 setMaxLines(data, entry.maxLines);
1281 }
1282
Evan Millar15e514d2009-08-04 10:14:57 -07001283 // Set the primary icon
1284 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1285
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001286 // Set the action icon
1287 ImageView action = views.actionIcon;
1288 if (entry.actionIcon != -1) {
1289 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1290 action.setVisibility(View.VISIBLE);
1291 } else {
1292 // Things should still line up as if there was an icon, so make it invisible
1293 action.setVisibility(View.INVISIBLE);
1294 }
1295
1296 // Set the presence icon
1297 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001298 if (entry.presenceIcon != -1) {
1299 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001300 } else if (entry.status != -1) {
1301 presenceIcon = resources.getDrawable(
1302 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001303 }
Evan Millar15e514d2009-08-04 10:14:57 -07001304 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001305 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001306 presenceIconView.setImageDrawable(presenceIcon);
1307 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001308 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001309 presenceIconView.setVisibility(View.GONE);
1310 }
1311
1312 // Set the secondary action button
1313 ImageView secondaryActionView = views.secondaryActionButton;
1314 Drawable secondaryActionIcon = null;
1315 if (entry.secondaryActionIcon != -1) {
1316 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1317 }
1318 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1319 secondaryActionView.setImageDrawable(secondaryActionIcon);
1320 secondaryActionView.setTag(entry.secondaryIntent);
1321 secondaryActionView.setVisibility(View.VISIBLE);
1322 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1323 } else {
1324 secondaryActionView.setVisibility(View.GONE);
1325 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001326 }
1327 }
1328
1329 private void setMaxLines(TextView textView, int maxLines) {
1330 if (maxLines == 1) {
1331 textView.setSingleLine(true);
1332 textView.setEllipsize(TextUtils.TruncateAt.END);
1333 } else {
1334 textView.setSingleLine(false);
1335 textView.setMaxLines(maxLines);
1336 textView.setEllipsize(null);
1337 }
1338 }
1339 }
1340}