blob: 65489bbba509cf7c00525a3425d5dc5e255ed1c1 [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
Evan Millar11d628c2009-09-02 08:55:01 -0700115 protected Uri mOriginalUri;
116 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;
139 private boolean mObserverRegistered;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700140
Evan Millar11d628c2009-09-02 08:55:01 -0700141 private SparseArray<Long> mTabRawContactIdMap;
142 protected ScrollingTabWidget mTabWidget;
143 protected ContactHeaderWidget mContactHeaderWidget;
144 private NotifyingAsyncQueryHandler mHandler;
145
146 protected LayoutInflater mInflater;
147
148 //Projection used for the query that determines which tabs to add.
149 protected static final String[] TAB_PROJECTION = new String[] {
150 RawContacts._ID,
151 RawContacts.ACCOUNT_NAME,
152 RawContacts.ACCOUNT_TYPE
153 };
154 protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0;
155 protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1;
156 protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2;
157
158 protected static final String SELECTED_RAW_CONTACT_ID_KEY = "selectedRawContact";
159 protected Long mSelectedRawContactId = null;
160
161 private static final int TOKEN_QUERY = 0;
162
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800163 private ContentObserver mObserver = new ContentObserver(new Handler()) {
164 @Override
165 public boolean deliverSelfNotifications() {
166 return true;
167 }
168
169 @Override
170 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700171 if (mCursor != null && !mCursor.isClosed()) {
172 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800173 }
174 }
175 };
176
177 public void onClick(DialogInterface dialog, int which) {
178 if (mCursor != null) {
179 if (mObserverRegistered) {
180 mCursor.unregisterContentObserver(mObserver);
181 mObserverRegistered = false;
182 }
183 mCursor.close();
184 mCursor = null;
185 }
186 getContentResolver().delete(mUri, null, null);
187 finish();
188 }
189
Evan Millar7911ff52009-07-21 15:55:18 -0700190 private FrameLayout mTabContentLayout;
191 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800192 private boolean mShowSmsLinksForAllPhones;
Evan Millar11d628c2009-09-02 08:55:01 -0700193 private ArrayList<Entity> mEntities = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800194
195 @Override
196 protected void onCreate(Bundle icicle) {
197 super.onCreate(icicle);
198
Evan Millar11d628c2009-09-02 08:55:01 -0700199 final Intent intent = getIntent();
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700200 if (!resolveContactUriFromIntent(intent)) {
201
202 // TODO either figure out a way to prevent a flash of black background or
203 // use some other UI than a toast
204 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
205 Log.e(TAG, "invalid contact uri: " + mOriginalUri);
206 finish();
207 return;
208 }
209
210 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700211
212 requestWindowFeature(Window.FEATURE_NO_TITLE);
213 setContentView(R.layout.contact_card_layout);
214
215 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
216 mContactHeaderWidget.showStar(true);
217 mContactHeaderWidget.bindFromContactId(ContentUris.parseId(mUri));
218 mContactHeaderWidget.setExcludeMimes(new String[] {
219 Contacts.CONTENT_ITEM_TYPE
220 });
221
222 mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget);
223 mTabWidget.setTabSelectionListener(this);
224 mTabWidget.setVisibility(View.INVISIBLE);
225
226 mTabRawContactIdMap = new SparseArray<Long>();
227
228 mHandler = new NotifyingAsyncQueryHandler(this, this);
229
Evan Millar7911ff52009-07-21 15:55:18 -0700230 mListView = new ListView(this);
231 mListView.setOnCreateContextMenuListener(this);
232 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
233 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800234
Evan Millar7911ff52009-07-21 15:55:18 -0700235 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
236 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800237
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800238 mResolver = getContentResolver();
239
240 // Build the list of sections. The order they're added to mSections dictates the
241 // order they are displayed in the list.
242 mSections.add(mPhoneEntries);
243 mSections.add(mSmsEntries);
244 mSections.add(mEmailEntries);
245 mSections.add(mImEntries);
246 mSections.add(mPostalEntries);
247 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700248 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800249 mSections.add(mOtherEntries);
250
251 //TODO Read this value from a preference
252 mShowSmsLinksForAllPhones = true;
253
Evan Millar11d628c2009-09-02 08:55:01 -0700254 //Stub query so we can get notifications.
Evan Millar8a79cee2009-08-19 17:20:49 -0700255 mCursor = mResolver.query(Uri.withAppendedPath(mUri, "data"),
Evan Millar11d628c2009-09-02 08:55:01 -0700256 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
257
258 startEntityQuery();
259 }
260
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700261 private boolean resolveContactUriFromIntent(final Intent intent) {
Evan Millar11d628c2009-09-02 08:55:01 -0700262 mOriginalUri = intent.getData();
263 mUri = ContactsContract.Contacts.lookupContact(getContentResolver(), mOriginalUri);
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700264 return mUri != null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800265 }
266
267 @Override
268 protected void onResume() {
269 super.onResume();
270 mObserverRegistered = true;
271 mCursor.registerContentObserver(mObserver);
Evan Millar11d628c2009-09-02 08:55:01 -0700272 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800273 }
274
275 @Override
276 protected void onPause() {
277 super.onPause();
278 if (mCursor != null) {
279 if (mObserverRegistered) {
280 mObserverRegistered = false;
281 mCursor.unregisterContentObserver(mObserver);
282 }
283 mCursor.deactivate();
284 }
285 }
286
287 @Override
288 protected void onDestroy() {
289 super.onDestroy();
290
291 if (mCursor != null) {
292 if (mObserverRegistered) {
293 mCursor.unregisterContentObserver(mObserver);
294 mObserverRegistered = false;
295 }
296 mCursor.close();
297 }
298 }
299
300 @Override
Evan Millar11d628c2009-09-02 08:55:01 -0700301 protected void onRestoreInstanceState(Bundle savedInstanceState) {
302 super.onRestoreInstanceState(savedInstanceState);
303 mSelectedRawContactId = savedInstanceState.getLong(SELECTED_RAW_CONTACT_ID_KEY);
304 }
305
306 @Override
307 protected void onSaveInstanceState(Bundle outState) {
308 super.onSaveInstanceState(outState);
309 outState.putLong(SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
310 }
311
312 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800313 protected Dialog onCreateDialog(int id) {
314 switch (id) {
315 case DIALOG_CONFIRM_DELETE:
316 return new AlertDialog.Builder(this)
317 .setTitle(R.string.deleteConfirmation_title)
318 .setIcon(android.R.drawable.ic_dialog_alert)
319 .setMessage(R.string.deleteConfirmation)
320 .setNegativeButton(android.R.string.cancel, null)
321 .setPositiveButton(android.R.string.ok, this)
322 .setCancelable(false)
323 .create();
324 }
325 return null;
326 }
327
Evan Millar11d628c2009-09-02 08:55:01 -0700328
329 // TAB CODE //
330 /**
331 * Adds a tab for each {@link RawContact} associated with this contact.
332 * Override this method if you want to additional tabs and/or different
333 * tabs for your activity.
334 *
335 * @param entities An {@link ArrayList} of {@link Entity}s of all the RawContacts
336 * associated with the contact being displayed.
337 */
338 protected void bindTabs() {
339 if (mEntities.size() > 1) {
Evan Millar7911ff52009-07-21 15:55:18 -0700340 addAllTab();
341 }
Evan Millar11d628c2009-09-02 08:55:01 -0700342
343 final Sources sources = Sources.getInstance(this);
344
345 for (Entity entity : mEntities) {
346 final String accountType = entity.getEntityValues().
347 getAsString(RawContacts.ACCOUNT_TYPE);
348 final Long rawContactId = entity.getEntityValues().
349 getAsLong(RawContacts._ID);
350
351 // TODO: ensure inflation on background task so we don't block UI thread here
352 final ContactsSource source = sources.getInflatedSource(accountType,
353 ContactsSource.LEVEL_SUMMARY);
354 addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(), source));
355 }
356
357 selectInitialTab();
358 mTabWidget.setVisibility(View.VISIBLE);
359 mTabWidget.postInvalidate();
360 }
361
362 /**
363 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
364 *
365 * @param contactId The contact id associated with the tab.
366 * @param view A view to use as the tab indicator.
367 */
368 protected void addTab(long rawContactId, View view) {
369 mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId);
370 mTabWidget.addTab(view);
371 }
372
373
374 protected void clearCurrentTabs() {
375 mTabRawContactIdMap.clear();
376 mTabWidget.removeAllTabs();
377 }
378
379 protected void selectInitialTab() {
380 int selectedTabIndex = 0;
381
382 if (mSelectedRawContactId != null) {
383 selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
384 if (selectedTabIndex == -1) {
385 // If there was no matching tab, just select the first;
386 selectedTabIndex = 0;
387 }
388 }
389
390 mTabWidget.setCurrentTab(selectedTabIndex);
391 onTabSelectionChanged(selectedTabIndex, false);
392 }
393
394 @Override
395 public void onNewIntent(Intent newIntent) {
396 setIntent(newIntent);
397 resolveContactUriFromIntent(newIntent);
398 selectInitialTab();
Evan Millar7911ff52009-07-21 15:55:18 -0700399 }
400
401 private void addAllTab() {
Evan Millar56d2caa2009-08-20 20:30:12 -0700402 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator,
403 mTabWidget.getTabParent(), false);
Evan Millar74660912009-08-19 17:36:33 -0700404 allTabIndicator.getBackground().setDither(true);
Evan Millar7911ff52009-07-21 15:55:18 -0700405 addTab(ALL_CONTACTS_ID, allTabIndicator);
406 }
407
408 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
409 long rawContactId = getTabRawContactId(tabIndex);
Evan Millar8a79cee2009-08-19 17:20:49 -0700410 mSelectedRawContactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700411 bindData();
Evan Millar7911ff52009-07-21 15:55:18 -0700412 }
413
Evan Millar11d628c2009-09-02 08:55:01 -0700414 /**
415 * Return the RawContact id associated with the tab at an index.
416 *
417 * @param index The index of the tab in question.
418 * @return The contactId associated with the tab at the specified index.
419 */
420 protected long getTabRawContactId(int index) {
421 return mTabRawContactIdMap.get(index);
422 }
Evan Millar2c1cc832009-07-13 11:08:06 -0700423
Evan Millar11d628c2009-09-02 08:55:01 -0700424 /**
425 * Return the tab index associated with the RawContact id.
426 *
427 * @param index The index of the tab in question.
428 * @return The contactId associated with the tab at the specified index.
429 */
430 protected int getTabIndexForRawContactId(long rawContactId) {
431 int numTabs = mTabRawContactIdMap.size();
432 for (int i=0; i < numTabs; i++) {
433 if (mTabRawContactIdMap.get(i) == rawContactId) {
434 return i;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800435 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800436 }
Evan Millar11d628c2009-09-02 08:55:01 -0700437 return -1;
438 }
439
440
441 // QUERY CODE //
442 /** {@inheritDoc} */
443 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
444 try{
445 if (token == TOKEN_QUERY) {
446 clearCurrentTabs();
447 mEntities = readEntities(iterator);
448 bindTabs();
449 bindData();
450 }
451 } finally {
452 if (iterator != null) {
453 iterator.close();
454 }
455 }
456 }
457
458 /** {@inheritDoc} */
459 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
460 // Emtpy
461 }
462
463 private ArrayList<Entity> readEntities(EntityIterator iterator) {
464 ArrayList<Entity> entities = new ArrayList<Entity>();
465 try {
466 while (iterator.hasNext()) {
467 entities.add(iterator.next());
468 }
469 } catch (RemoteException e) {
470 }
471
472 return entities;
473 }
474
475 private void startEntityQuery() {
476 long contactId = ContentUris.parseId(mUri);
477 mHandler.startQueryEntities(TOKEN_QUERY, null,
478 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
479 }
480
481 private void bindData() {
482
483 // Build up the contact entries
484 buildEntries();
485
486 // Collapse similar data items in select sections.
487 Collapser.collapseList(mPhoneEntries);
488 Collapser.collapseList(mSmsEntries);
489 Collapser.collapseList(mEmailEntries);
490 Collapser.collapseList(mPostalEntries);
491
492 if (mAdapter == null) {
493 mAdapter = new ViewAdapter(this, mSections);
494 mListView.setAdapter(mAdapter);
495 } else {
496 mAdapter.setSections(mSections, SHOW_SEPARATORS);
497 }
498
499// else {
500// Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
501// Log.e(TAG, "invalid contact uri: " + mOriginalUri);
502// finish();
503// }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800504 }
505
506 @Override
507 public boolean onCreateOptionsMenu(Menu menu) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800508 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
509 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700510 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
511 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700512 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
513 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700514 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
515 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800516 return true;
517 }
518
519 @Override
520 public boolean onPrepareOptionsMenu(Menu menu) {
521 super.onPrepareOptionsMenu(menu);
522 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
523 // could be installed or uninstalled at any time.
524 if (isBarcodeScannerInstalled()) {
525 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
526 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
527 .setIcon(R.drawable.ic_menu_show_barcode);
528 }
529 } else {
530 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
531 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700532
Evan Millardb5d88c2009-08-28 09:31:57 -0700533 // Only show the edit option if we have a selected tab.
534 if (mSelectedRawContactId != null) {
535 if (menu.findItem(MENU_ITEM_EDIT) == null) {
536 menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_editContact)
537 .setIcon(android.R.drawable.ic_menu_edit)
538 .setAlphabeticShortcut('e');
539 }
540 } else {
541 menu.removeItem(MENU_ITEM_EDIT);
542 }
543
Evan Millar7911ff52009-07-21 15:55:18 -0700544 boolean isAggregate = mRawContactIds.size() > 1;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700545 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800546 return true;
547 }
548
549 private boolean isBarcodeScannerInstalled() {
550 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
551 ResolveInfo ri = getPackageManager().resolveActivity(intent,
552 PackageManager.MATCH_DEFAULT_ONLY);
553 return ri != null;
554 }
555
556 @Override
557 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
558 AdapterView.AdapterContextMenuInfo info;
559 try {
560 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
561 } catch (ClassCastException e) {
562 Log.e(TAG, "bad menuInfo", e);
563 return;
564 }
565
566 // This can be null sometimes, don't crash...
567 if (info == null) {
568 Log.e(TAG, "bad menuInfo");
569 return;
570 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700571
Evan Millar45e0ed32009-06-01 16:44:38 -0700572 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
573 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
574 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700575 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
576 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700577 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800578 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700579 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
580 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700581 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700582 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800583 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700584 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700585 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
586 }
587 // TODO(emillar): add back with group support.
588 /* else if (entry.mimetype.equals()) {
589 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
590 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800591 }
592
593 @Override
594 public boolean onOptionsItemSelected(MenuItem item) {
595 switch (item.getItemId()) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700596 case MENU_ITEM_EDIT: {
Evan Millardb5d88c2009-08-28 09:31:57 -0700597 Long rawContactIdToEdit = mSelectedRawContactId;
598 if (rawContactIdToEdit == null) {
599 // This shouldn't be possible. We only show the edit option if
600 // this value is non-null.
601 break;
602 }
Evan Millar8a79cee2009-08-19 17:20:49 -0700603 if (rawContactIdToEdit == ALL_CONTACTS_ID) {
604 // If the "all" tab is selected, edit the next tab.
Evan Millardb5d88c2009-08-28 09:31:57 -0700605 rawContactIdToEdit = getTabRawContactId(mTabWidget.getCurrentTab() + 1);
Evan Millar8a79cee2009-08-19 17:20:49 -0700606 }
607 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
608 rawContactIdToEdit);
609 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
610 REQUEST_EDIT_CONTACT);
611 break;
612 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800613 case MENU_ITEM_DELETE: {
614 // Get confirmation
615 showDialog(DIALOG_CONFIRM_DELETE);
616 return true;
617 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700618
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700619 case MENU_ITEM_SPLIT_AGGREGATE: {
620 showSplitAggregateDialog();
621 return true;
622 }
623
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700624 case MENU_ITEM_JOIN_AGGREGATE: {
625 showJoinAggregateActivity();
626 return true;
627 }
628
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700629 case MENU_ITEM_OPTIONS: {
630 showOptionsActivity();
631 return true;
632 }
633
Evan Millar66388be2009-05-28 15:41:07 -0700634 // TODO(emillar) Bring this back.
635 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800636 if (mCursor.moveToFirst()) {
637 Intent intent = new Intent(SHOW_BARCODE_INTENT);
638 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
639 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700640 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800641 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700642 // Correctly handle when section headers are hidden
643 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700644
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800645 bundle.putString(Contacts.Intents.Insert.NAME, name);
646 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700647 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800648 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700649 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800650 bundle.putString(PHONE_KEYS[x], entry.data);
651 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700652 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800653 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700654 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800655 bundle.putString(EMAIL_KEYS[x], entry.data);
656 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700657 if (mPostalEntries.size() >= 1 + sepAdjust) {
658 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800659 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
660 }
661 intent.putExtra("ENCODE_DATA", bundle);
662 try {
663 startActivity(intent);
664 } catch (ActivityNotFoundException e) {
665 // The check in onPrepareOptionsMenu() should make this impossible, but
666 // for safety I'm catching the exception rather than crashing. Ideally
667 // I'd call Menu.removeItem() here too, but I don't see a way to get
668 // the options menu.
669 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
670 "was not installed.");
671 }
672 return true;
673 }
674 }
Evan Millar66388be2009-05-28 15:41:07 -0700675 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800676 }
677 return super.onOptionsItemSelected(item);
678 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700679
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800680 @Override
681 public boolean onContextItemSelected(MenuItem item) {
682 switch (item.getItemId()) {
683 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700684 if (makeItemDefault(item)) {
685 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800686 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700687 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800688 }
689 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700690
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800691 return super.onContextItemSelected(item);
692 }
693
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700694 private boolean makeItemDefault(MenuItem item) {
695 ViewEntry entry = getViewEntryForMenuItem(item);
696 if (entry == null) {
697 return false;
698 }
699
700 // Update the primary values in the data record.
701 ContentValues values = new ContentValues(2);
702 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700703
Evan Millar54a5c9f2009-06-23 17:41:09 -0700704 if (entry.ids.size() > 0) {
705 for (int i = 0; i < entry.ids.size(); i++) {
706 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
707 entry.ids.get(i)),
708 values, null, null);
709 }
710 }
711
712 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700713 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
714 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700715 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700716 return true;
717 }
718
719 /**
720 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
721 * The user picks a contact to be split into its own aggregate or clicks Cancel.
722 */
723 private void showSplitAggregateDialog() {
724
725 // Wrap this dialog in a specific theme so that list items have correct text color.
726 final ContextThemeWrapper dialogContext =
727 new ContextThemeWrapper(this, android.R.style.Theme_Light);
728 AlertDialog.Builder builder =
729 new AlertDialog.Builder(dialogContext);
730 builder.setTitle(getString(R.string.splitAggregate_title));
731
732 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
733 builder.setView(view);
734
735 builder.setInverseBackgroundForced(true);
736 builder.setCancelable(true);
737 builder.setNegativeButton(android.R.string.cancel,
738 new OnClickListener() {
739 public void onClick(DialogInterface dialog, int which) {
740 dialog.dismiss();
741 }
742 });
743 final AlertDialog dialog = builder.create();
744
745 view.setOnContactSelectedListener(new OnContactSelectedListener() {
746 public void onContactSelected(long contactId) {
747 dialog.dismiss();
748 splitContact(contactId);
749 }
750 });
751
752 dialog.show();
753 }
754
755 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700756 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700757 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700758 public void showJoinAggregateActivity() {
759 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
760 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700761 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700762 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700763
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700764 @Override
765 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700766 switch (requestCode) {
767 case REQUEST_JOIN_CONTACT: {
768 if (resultCode == RESULT_OK && intent != null) {
769 final long aggregateId = ContentUris.parseId(intent.getData());
770 joinAggregate(aggregateId);
771 }
772 break;
773 }
774 case REQUEST_EDIT_CONTACT: {
775 if (resultCode == RESULT_OK && intent != null) {
776 long newInitialSelectedRawContactId = intent.getLongExtra(
777 RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
778 if (newInitialSelectedRawContactId != mSelectedRawContactId) {
779 mSelectedRawContactId = newInitialSelectedRawContactId;
780 selectInitialTab();
781 }
782 }
783 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700784 }
785 }
786
787 private void splitContact(long contactId) {
788 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_OUT);
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700789 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700790 mAdapter.notifyDataSetChanged();
791 }
792
793 private void joinAggregate(final long aggregateId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700794 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700795 RawContacts.CONTACT_ID + "=" + aggregateId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700796
797 try {
798 while(c.moveToNext()) {
799 long contactId = c.getLong(0);
800 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
801 }
802 } finally {
803 c.close();
804 }
805
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700806 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700807 mAdapter.notifyDataSetChanged();
808 }
809
810 /**
811 * Given a contact ID sets an aggregation exception to either join the contact with the
812 * current aggregate or split off.
813 */
814 protected void setAggregationException(long contactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700815 ContentValues values = new ContentValues(3);
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700816 values.put(AggregationExceptions.CONTACT_ID, ContentUris.parseId(mUri));
Jeff Sharkey14f61ab2009-08-05 21:02:37 -0700817 values.put(AggregationExceptions.RAW_CONTACT_ID, contactId);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700818 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700819 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700820 }
821
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700822 private void showOptionsActivity() {
823 final Intent intent = new Intent(this, ContactOptionsActivity.class);
824 intent.setData(mUri);
825 startActivity(intent);
826 }
827
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700828 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
829 AdapterView.AdapterContextMenuInfo info;
830 try {
831 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
832 } catch (ClassCastException e) {
833 Log.e(TAG, "bad menuInfo", e);
834 return null;
835 }
836
837 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
838 }
839
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800840 @Override
841 public boolean onKeyDown(int keyCode, KeyEvent event) {
842 switch (keyCode) {
843 case KeyEvent.KEYCODE_CALL: {
844 try {
845 ITelephony phone = ITelephony.Stub.asInterface(
846 ServiceManager.checkService("phone"));
847 if (phone != null && !phone.isIdle()) {
848 // Skip out and let the key be handled at a higher level
849 break;
850 }
851 } catch (RemoteException re) {
852 // Fall through and try to call the contact
853 }
854
Evan Millar7911ff52009-07-21 15:55:18 -0700855 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800856 if (index != -1) {
857 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700858 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
859 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800860 }
861 } else if (mNumPhoneNumbers != 0) {
862 // There isn't anything selected, call the default number
863 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
864 startActivity(intent);
865 }
866 return true;
867 }
868
869 case KeyEvent.KEYCODE_DEL: {
870 showDialog(DIALOG_CONFIRM_DELETE);
871 return true;
872 }
873 }
874
875 return super.onKeyDown(keyCode, event);
876 }
877
Evan Millar7911ff52009-07-21 15:55:18 -0700878 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800879 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
880 if (entry != null) {
881 Intent intent = entry.intent;
882 if (intent != null) {
883 try {
884 startActivity(intent);
885 } catch (ActivityNotFoundException e) {
886 Log.e(TAG, "No activity found for intent: " + intent);
887 signalError();
888 }
889 } else {
890 signalError();
891 }
892 } else {
893 signalError();
894 }
895 }
896
897 /**
898 * Signal an error to the user via a beep, or some other method.
899 */
900 private void signalError() {
901 //TODO: implement this when we have the sonification APIs
902 }
903
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800904 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700905 // 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 -0800906 StringBuilder buf = new StringBuilder("imto://");
907 buf.append(host);
908 buf.append('/');
909 buf.append(data);
910 return Uri.parse(buf.toString());
911 }
912
913 /**
914 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700915 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800916 * @param personCursor the URI for the contact being displayed
917 */
Evan Millar11d628c2009-09-02 08:55:01 -0700918 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800919 // Clear out the old entries
920 final int numSections = mSections.size();
921 for (int i = 0; i < numSections; i++) {
922 mSections.get(i).clear();
923 }
924
Evan Millar7911ff52009-07-21 15:55:18 -0700925 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700926
Evan Millar11d628c2009-09-02 08:55:01 -0700927 Sources sources = Sources.getInstance(this);
928
Evan Millar66388be2009-05-28 15:41:07 -0700929 // Build up method entries
930 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700931 for (Entity entity: mEntities) {
932 final ContentValues entValues = entity.getEntityValues();
933 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Evan Millar7911ff52009-07-21 15:55:18 -0700934 // TODO: entry.contactId should be renamed to entry.rawContactId
Evan Millar11d628c2009-09-02 08:55:01 -0700935 long contactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700936
Evan Millar11d628c2009-09-02 08:55:01 -0700937 for (NamedContentValues subValue : entity.getSubValues()) {
938 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800939
Evan Millar11d628c2009-09-02 08:55:01 -0700940 ContentValues entryValues = subValue.values;
941 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
942 if (mimetype == null || accountType == null) {
Evan Millar66388be2009-05-28 15:41:07 -0700943 continue;
944 }
945
Evan Millar11d628c2009-09-02 08:55:01 -0700946 ContactsSource contactsSource = sources.getInflatedSource(accountType,
947 ContactsSource.LEVEL_MIMETYPES);
948 if (contactsSource == null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800949 continue;
950 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700951
Evan Millar11d628c2009-09-02 08:55:01 -0700952 DataKind kind = contactsSource.getKindForMimetype(mimetype);
953 if (kind == null) {
954 continue;
955 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800956
Evan Millar11d628c2009-09-02 08:55:01 -0700957 final long id = entryValues.getAsLong(Data._ID);
958 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
959 entry.contactId = contactId;
960 entry.id = id;
961 entry.uri = uri;
962 entry.mimetype = mimetype;
963 entry.label = buildActionString(kind, entryValues, true);
964 entry.data = buildDataString(kind, entryValues);
965 if (kind.typeColumn != null) {
966 entry.type = entryValues.getAsInteger(kind.typeColumn);
967 }
968 if (kind.iconRes > 0) {
969 entry.actionIcon = kind.iconRes;
970 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800971
Evan Millar11d628c2009-09-02 08:55:01 -0700972 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700973 if (TextUtils.isEmpty(entry.data)) {
974 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700975 continue;
976 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700977
Evan Millar11d628c2009-09-02 08:55:01 -0700978 if (!mRawContactIds.contains(entry.contactId)) {
979 mRawContactIds.add(entry.contactId);
980 }
981
982 // This performs the tab filtering
983 if (mSelectedRawContactId != null
984 && mSelectedRawContactId != entry.contactId
985 && mSelectedRawContactId != ALL_CONTACTS_ID) {
986 continue;
987 }
988
989 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)
990 || CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)
991 || CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)
992 || CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
993 final boolean isSuperPrimary = entryValues.getAsInteger(
994 Data.IS_SUPER_PRIMARY) != 0;
995
996 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
997 // Build phone entries
998 mNumPhoneNumbers++;
999
1000 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
1001 Uri.fromParts("tel", entry.data, null));
1002 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
1003 Uri.fromParts("sms", entry.data, null));
1004 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -07001005
1006 // If data is empty, don't show it.
1007 if (TextUtils.isEmpty(entry.data)) {
1008 Log.w(TAG, "empty data for contact method " + id);
1009 continue;
1010 }
1011
Evan Millar11d628c2009-09-02 08:55:01 -07001012 entry.isPrimary = isSuperPrimary;
1013 mPhoneEntries.add(entry);
1014
1015 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
1016 || mShowSmsLinksForAllPhones) {
1017 // Add an SMS entry
1018 if (kind.iconAltRes > 0) {
1019 entry.secondaryActionIcon = kind.iconAltRes;
1020 }
1021 }
1022 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
1023 // Build email entries
1024 entry.intent = new Intent(Intent.ACTION_SENDTO,
1025 Uri.fromParts("mailto", entry.data, null));
Evan Millar49714ee2009-09-02 16:42:47 -07001026 // Temporary hack until we get real label resources for exchange.
1027 if (TextUtils.isEmpty(entry.label)) {
1028 entry.label = getString(R.string.email).toLowerCase();
1029 }
Evan Millar11d628c2009-09-02 08:55:01 -07001030 entry.isPrimary = isSuperPrimary;
1031 mEmailEntries.add(entry);
1032 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
1033 equals(mimetype)) {
1034 // Build postal entries
1035 entry.maxLines = 4;
1036 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
1037 mPostalEntries.add(entry);
1038 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
1039 // Build im entries
1040 Object protocolObj = entryValues.getAsInteger(Data.DATA5);
1041 String host = null;
1042
Evan Millar49714ee2009-09-02 16:42:47 -07001043 if (TextUtils.isEmpty(entry.label)) {
1044 entry.label = getString(R.string.im).toLowerCase();
1045 }
1046
Evan Millar11d628c2009-09-02 08:55:01 -07001047 if (protocolObj instanceof Number) {
1048 int protocol = ((Number) protocolObj).intValue();
1049 host = ContactsUtils.lookupProviderNameFromId(
1050 protocol).toLowerCase();
1051 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
1052 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
1053 entry.maxLabelLines = 2;
1054 }
1055 } else if (protocolObj != null) {
1056 String providerName = (String) protocolObj;
1057 host = providerName.toLowerCase();
1058 }
1059
1060 // Only add the intent if there is a valid host
1061 if (!TextUtils.isEmpty(host)) {
1062 entry.intent = new Intent(Intent.ACTION_SENDTO,
1063 constructImToUrl(host, entry.data));
1064 }
1065 //TODO(emillar) Add in presence info
1066 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
1067 entry.presenceIcon = Presence.getPresenceIconResourceId(
1068 aggCursor.getInt(METHODS_STATUS_COLUMN));
1069 entry.status = ...
1070 }*/
1071 mImEntries.add(entry);
1072 }
1073 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
1074 // Build organization entries
1075 mOrganizationEntries.add(entry);
1076 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
1077 // Build note entries
1078 entry.id = 0;
1079 entry.uri = null;
1080 entry.intent = null;
1081 entry.maxLines = 10;
1082 mOtherEntries.add(entry);
1083 }
1084
1085
1086 // TODO(emillar) Add group entries
1087 // // Build the group entries
1088 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1089 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1090 // null, null, Groups.DEFAULT_SORT_ORDER);
1091 // if (groupCursor != null) {
1092 // try {
1093 // StringBuilder sb = new StringBuilder();
1094 //
1095 // while (groupCursor.moveToNext()) {
1096 // String systemId = groupCursor.getString(
1097 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1098 //
1099 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1100 // continue;
1101 // }
1102 //
1103 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1104 // if (!TextUtils.isEmpty(name)) {
1105 // if (sb.length() == 0) {
1106 // sb.append(name);
1107 // } else {
1108 // sb.append(getString(R.string.group_list, name));
1109 // }
1110 // }
1111 // }
1112 //
1113 // if (sb.length() > 0) {
1114 // ViewEntry entry = new ViewEntry();
1115 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1116 // entry.label = getString(R.string.label_groups);
1117 // entry.data = sb.toString();
1118 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1119 //
1120 // // TODO: Add an icon for the groups item.
1121 //
1122 // mGroupEntries.add(entry);
1123 // }
1124 // } finally {
1125 // groupCursor.close();
1126 // }
1127 // }
Evan Millar66388be2009-05-28 15:41:07 -07001128 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001129
Evan Millar66388be2009-05-28 15:41:07 -07001130 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001131 }
1132 }
1133
Evan Millar11d628c2009-09-02 08:55:01 -07001134 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1135 if (kind.actionHeader == null) {
1136 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001137 }
Evan Millar11d628c2009-09-02 08:55:01 -07001138 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1139 if (actionHeader == null) {
1140 return null;
1141 }
1142 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1143 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001144
Evan Millar11d628c2009-09-02 08:55:01 -07001145 String buildDataString(DataKind kind, ContentValues values) {
1146 if (kind.actionBody == null) {
1147 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001148 }
Evan Millar11d628c2009-09-02 08:55:01 -07001149 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1150 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001151 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001152
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001153 /**
1154 * A basic structure with the data for a contact entry in the list.
1155 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001156 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001157 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001158 public boolean isPrimary = false;
1159 public int presenceIcon = -1;
1160 public int secondaryActionIcon = -1;
1161 public Intent intent;
1162 public Intent secondaryIntent = null;
1163 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001164 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001165 public ArrayList<Long> ids = new ArrayList<Long>();
1166 public int collapseCount = 0;
1167
1168 public boolean collapseWith(ViewEntry entry) {
1169 // assert equal collapse keys
1170 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1171 return false;
1172 }
1173
1174 // Choose the label associated with the highest type precedence.
1175 if (TypePrecedence.getTypePrecedence(mimetype, type)
1176 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1177 type = entry.type;
1178 label = entry.label;
1179 }
1180
1181 // Choose the max of the maxLines and maxLabelLines values.
1182 maxLines = Math.max(maxLines, entry.maxLines);
1183 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1184
1185 // Choose the presence with the highest precedence.
1186 if (Presence.getPresencePrecedence(status)
1187 < Presence.getPresencePrecedence(entry.status)) {
1188 status = entry.status;
1189 }
1190
1191 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001192 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001193
1194 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1195
1196 // Keep track of all the ids that have been collapsed with this one.
1197 ids.add(entry.id);
1198 collapseCount++;
1199 return true;
1200 }
1201
1202 public String getCollapseKey() {
1203 StringBuilder hashSb = new StringBuilder();
1204 hashSb.append(data);
1205 hashSb.append(mimetype);
1206 hashSb.append((intent != null && intent.getAction() != null)
1207 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001208 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1209 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001210 hashSb.append(actionIcon);
1211 return hashSb.toString();
1212 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001213 }
1214
Evan Millar15e514d2009-08-04 10:14:57 -07001215 /** Cache of the children views of a row */
1216 static class ViewCache {
1217 public TextView label;
1218 public TextView data;
1219 public ImageView actionIcon;
1220 public ImageView presenceIcon;
1221 public ImageView primaryIcon;
1222 public ImageView secondaryActionButton;
1223 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001224
Evan Millar15e514d2009-08-04 10:14:57 -07001225 // Need to keep track of this too
1226 ViewEntry entry;
1227 }
1228
1229 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1230 implements View.OnClickListener {
1231
Evan Millar5c22c3b2009-05-29 11:37:54 -07001232
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001233 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1234 super(context, sections, SHOW_SEPARATORS);
1235 }
1236
Evan Millar15e514d2009-08-04 10:14:57 -07001237 public void onClick(View v) {
1238 Intent intent = (Intent) v.getTag();
1239 startActivity(intent);
1240 }
1241
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001242 @Override
1243 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001244 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001245 View v;
1246
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001247 ViewCache views;
1248
1249 // Check to see if we can reuse convertView
1250 if (convertView != null) {
1251 v = convertView;
1252 views = (ViewCache) v.getTag();
1253 } else {
1254 // Create a new view if needed
1255 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1256
1257 // Cache the children
1258 views = new ViewCache();
1259 views.label = (TextView) v.findViewById(android.R.id.text1);
1260 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001261 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1262 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1263 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1264 views.secondaryActionButton = (ImageView) v.findViewById(
1265 R.id.secondary_action_button);
1266 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001267 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001268 v.setTag(views);
1269 }
1270
1271 // Update the entry in the view cache
1272 views.entry = entry;
1273
1274 // Bind the data to the view
1275 bindView(v, entry);
1276 return v;
1277 }
1278
1279 @Override
1280 protected View newView(int position, ViewGroup parent) {
1281 // getView() handles this
1282 throw new UnsupportedOperationException();
1283 }
1284
1285 @Override
1286 protected void bindView(View view, ViewEntry entry) {
1287 final Resources resources = mContext.getResources();
1288 ViewCache views = (ViewCache) view.getTag();
1289
1290 // Set the label
1291 TextView label = views.label;
1292 setMaxLines(label, entry.maxLabelLines);
1293 label.setText(entry.label);
1294
1295 // Set the data
1296 TextView data = views.data;
1297 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001298 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1299 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1300 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1301 } else {
1302 data.setText(entry.data);
1303 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001304 setMaxLines(data, entry.maxLines);
1305 }
1306
Evan Millar15e514d2009-08-04 10:14:57 -07001307 // Set the primary icon
1308 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1309
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001310 // Set the action icon
1311 ImageView action = views.actionIcon;
1312 if (entry.actionIcon != -1) {
1313 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1314 action.setVisibility(View.VISIBLE);
1315 } else {
1316 // Things should still line up as if there was an icon, so make it invisible
1317 action.setVisibility(View.INVISIBLE);
1318 }
1319
1320 // Set the presence icon
1321 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001322 if (entry.presenceIcon != -1) {
1323 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001324 } else if (entry.status != -1) {
1325 presenceIcon = resources.getDrawable(
1326 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001327 }
Evan Millar15e514d2009-08-04 10:14:57 -07001328 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001329 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001330 presenceIconView.setImageDrawable(presenceIcon);
1331 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001332 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001333 presenceIconView.setVisibility(View.GONE);
1334 }
1335
1336 // Set the secondary action button
1337 ImageView secondaryActionView = views.secondaryActionButton;
1338 Drawable secondaryActionIcon = null;
1339 if (entry.secondaryActionIcon != -1) {
1340 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1341 }
1342 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1343 secondaryActionView.setImageDrawable(secondaryActionIcon);
1344 secondaryActionView.setTag(entry.secondaryIntent);
1345 secondaryActionView.setVisibility(View.VISIBLE);
1346 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1347 } else {
1348 secondaryActionView.setVisibility(View.GONE);
1349 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001350 }
1351 }
1352
1353 private void setMaxLines(TextView textView, int maxLines) {
1354 if (maxLines == 1) {
1355 textView.setSingleLine(true);
1356 textView.setEllipsize(TextUtils.TruncateAt.END);
1357 } else {
1358 textView.setSingleLine(false);
1359 textView.setMaxLines(maxLines);
1360 textView.setEllipsize(null);
1361 }
1362 }
1363 }
1364}