blob: 36d9ee691a84b0489d925a5d1b9fcca7704b7f7d [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 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
200
201 final Intent intent = getIntent();
202 mUri = intent.getData();
203 resolveContactUriFromIntent(intent);
204
205 requestWindowFeature(Window.FEATURE_NO_TITLE);
206 setContentView(R.layout.contact_card_layout);
207
208 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
209 mContactHeaderWidget.showStar(true);
210 mContactHeaderWidget.bindFromContactId(ContentUris.parseId(mUri));
211 mContactHeaderWidget.setExcludeMimes(new String[] {
212 Contacts.CONTENT_ITEM_TYPE
213 });
214
215 mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget);
216 mTabWidget.setTabSelectionListener(this);
217 mTabWidget.setVisibility(View.INVISIBLE);
218
219 mTabRawContactIdMap = new SparseArray<Long>();
220
221 mHandler = new NotifyingAsyncQueryHandler(this, this);
222
Evan Millar7911ff52009-07-21 15:55:18 -0700223 mListView = new ListView(this);
224 mListView.setOnCreateContextMenuListener(this);
225 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
226 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800227
Evan Millar7911ff52009-07-21 15:55:18 -0700228 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
229 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800230
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800231 mResolver = getContentResolver();
232
233 // Build the list of sections. The order they're added to mSections dictates the
234 // order they are displayed in the list.
235 mSections.add(mPhoneEntries);
236 mSections.add(mSmsEntries);
237 mSections.add(mEmailEntries);
238 mSections.add(mImEntries);
239 mSections.add(mPostalEntries);
240 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700241 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800242 mSections.add(mOtherEntries);
243
244 //TODO Read this value from a preference
245 mShowSmsLinksForAllPhones = true;
246
Evan Millar11d628c2009-09-02 08:55:01 -0700247 //Stub query so we can get notifications.
Evan Millar8a79cee2009-08-19 17:20:49 -0700248 mCursor = mResolver.query(Uri.withAppendedPath(mUri, "data"),
Evan Millar11d628c2009-09-02 08:55:01 -0700249 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
250
251 startEntityQuery();
252 }
253
254 private void resolveContactUriFromIntent(final Intent intent) {
255 mOriginalUri = intent.getData();
256 mUri = ContactsContract.Contacts.lookupContact(getContentResolver(), mOriginalUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800257 }
258
259 @Override
260 protected void onResume() {
261 super.onResume();
262 mObserverRegistered = true;
263 mCursor.registerContentObserver(mObserver);
Evan Millar11d628c2009-09-02 08:55:01 -0700264 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800265 }
266
267 @Override
268 protected void onPause() {
269 super.onPause();
270 if (mCursor != null) {
271 if (mObserverRegistered) {
272 mObserverRegistered = false;
273 mCursor.unregisterContentObserver(mObserver);
274 }
275 mCursor.deactivate();
276 }
277 }
278
279 @Override
280 protected void onDestroy() {
281 super.onDestroy();
282
283 if (mCursor != null) {
284 if (mObserverRegistered) {
285 mCursor.unregisterContentObserver(mObserver);
286 mObserverRegistered = false;
287 }
288 mCursor.close();
289 }
290 }
291
292 @Override
Evan Millar11d628c2009-09-02 08:55:01 -0700293 protected void onRestoreInstanceState(Bundle savedInstanceState) {
294 super.onRestoreInstanceState(savedInstanceState);
295 mSelectedRawContactId = savedInstanceState.getLong(SELECTED_RAW_CONTACT_ID_KEY);
296 }
297
298 @Override
299 protected void onSaveInstanceState(Bundle outState) {
300 super.onSaveInstanceState(outState);
301 outState.putLong(SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
302 }
303
304 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800305 protected Dialog onCreateDialog(int id) {
306 switch (id) {
307 case DIALOG_CONFIRM_DELETE:
308 return new AlertDialog.Builder(this)
309 .setTitle(R.string.deleteConfirmation_title)
310 .setIcon(android.R.drawable.ic_dialog_alert)
311 .setMessage(R.string.deleteConfirmation)
312 .setNegativeButton(android.R.string.cancel, null)
313 .setPositiveButton(android.R.string.ok, this)
314 .setCancelable(false)
315 .create();
316 }
317 return null;
318 }
319
Evan Millar11d628c2009-09-02 08:55:01 -0700320
321 // TAB CODE //
322 /**
323 * Adds a tab for each {@link RawContact} associated with this contact.
324 * Override this method if you want to additional tabs and/or different
325 * tabs for your activity.
326 *
327 * @param entities An {@link ArrayList} of {@link Entity}s of all the RawContacts
328 * associated with the contact being displayed.
329 */
330 protected void bindTabs() {
331 if (mEntities.size() > 1) {
Evan Millar7911ff52009-07-21 15:55:18 -0700332 addAllTab();
333 }
Evan Millar11d628c2009-09-02 08:55:01 -0700334
335 final Sources sources = Sources.getInstance(this);
336
337 for (Entity entity : mEntities) {
338 final String accountType = entity.getEntityValues().
339 getAsString(RawContacts.ACCOUNT_TYPE);
340 final Long rawContactId = entity.getEntityValues().
341 getAsLong(RawContacts._ID);
342
343 // TODO: ensure inflation on background task so we don't block UI thread here
344 final ContactsSource source = sources.getInflatedSource(accountType,
345 ContactsSource.LEVEL_SUMMARY);
346 addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(), source));
347 }
348
349 selectInitialTab();
350 mTabWidget.setVisibility(View.VISIBLE);
351 mTabWidget.postInvalidate();
352 }
353
354 /**
355 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
356 *
357 * @param contactId The contact id associated with the tab.
358 * @param view A view to use as the tab indicator.
359 */
360 protected void addTab(long rawContactId, View view) {
361 mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId);
362 mTabWidget.addTab(view);
363 }
364
365
366 protected void clearCurrentTabs() {
367 mTabRawContactIdMap.clear();
368 mTabWidget.removeAllTabs();
369 }
370
371 protected void selectInitialTab() {
372 int selectedTabIndex = 0;
373
374 if (mSelectedRawContactId != null) {
375 selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
376 if (selectedTabIndex == -1) {
377 // If there was no matching tab, just select the first;
378 selectedTabIndex = 0;
379 }
380 }
381
382 mTabWidget.setCurrentTab(selectedTabIndex);
383 onTabSelectionChanged(selectedTabIndex, false);
384 }
385
386 @Override
387 public void onNewIntent(Intent newIntent) {
388 setIntent(newIntent);
389 resolveContactUriFromIntent(newIntent);
390 selectInitialTab();
Evan Millar7911ff52009-07-21 15:55:18 -0700391 }
392
393 private void addAllTab() {
Evan Millar56d2caa2009-08-20 20:30:12 -0700394 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator,
395 mTabWidget.getTabParent(), false);
Evan Millar74660912009-08-19 17:36:33 -0700396 allTabIndicator.getBackground().setDither(true);
Evan Millar7911ff52009-07-21 15:55:18 -0700397 addTab(ALL_CONTACTS_ID, allTabIndicator);
398 }
399
400 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
401 long rawContactId = getTabRawContactId(tabIndex);
Evan Millar8a79cee2009-08-19 17:20:49 -0700402 mSelectedRawContactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700403 bindData();
Evan Millar7911ff52009-07-21 15:55:18 -0700404 }
405
Evan Millar11d628c2009-09-02 08:55:01 -0700406 /**
407 * Return the RawContact id associated with the tab at an index.
408 *
409 * @param index The index of the tab in question.
410 * @return The contactId associated with the tab at the specified index.
411 */
412 protected long getTabRawContactId(int index) {
413 return mTabRawContactIdMap.get(index);
414 }
Evan Millar2c1cc832009-07-13 11:08:06 -0700415
Evan Millar11d628c2009-09-02 08:55:01 -0700416 /**
417 * Return the tab index associated with the RawContact id.
418 *
419 * @param index The index of the tab in question.
420 * @return The contactId associated with the tab at the specified index.
421 */
422 protected int getTabIndexForRawContactId(long rawContactId) {
423 int numTabs = mTabRawContactIdMap.size();
424 for (int i=0; i < numTabs; i++) {
425 if (mTabRawContactIdMap.get(i) == rawContactId) {
426 return i;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800427 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800428 }
Evan Millar11d628c2009-09-02 08:55:01 -0700429 return -1;
430 }
431
432
433 // QUERY CODE //
434 /** {@inheritDoc} */
435 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
436 try{
437 if (token == TOKEN_QUERY) {
438 clearCurrentTabs();
439 mEntities = readEntities(iterator);
440 bindTabs();
441 bindData();
442 }
443 } finally {
444 if (iterator != null) {
445 iterator.close();
446 }
447 }
448 }
449
450 /** {@inheritDoc} */
451 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
452 // Emtpy
453 }
454
455 private ArrayList<Entity> readEntities(EntityIterator iterator) {
456 ArrayList<Entity> entities = new ArrayList<Entity>();
457 try {
458 while (iterator.hasNext()) {
459 entities.add(iterator.next());
460 }
461 } catch (RemoteException e) {
462 }
463
464 return entities;
465 }
466
467 private void startEntityQuery() {
468 long contactId = ContentUris.parseId(mUri);
469 mHandler.startQueryEntities(TOKEN_QUERY, null,
470 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
471 }
472
473 private void bindData() {
474
475 // Build up the contact entries
476 buildEntries();
477
478 // Collapse similar data items in select sections.
479 Collapser.collapseList(mPhoneEntries);
480 Collapser.collapseList(mSmsEntries);
481 Collapser.collapseList(mEmailEntries);
482 Collapser.collapseList(mPostalEntries);
483
484 if (mAdapter == null) {
485 mAdapter = new ViewAdapter(this, mSections);
486 mListView.setAdapter(mAdapter);
487 } else {
488 mAdapter.setSections(mSections, SHOW_SEPARATORS);
489 }
490
491// else {
492// Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
493// Log.e(TAG, "invalid contact uri: " + mOriginalUri);
494// finish();
495// }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800496 }
497
498 @Override
499 public boolean onCreateOptionsMenu(Menu menu) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800500 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
501 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700502 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
503 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700504 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
505 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700506 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
507 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800508 return true;
509 }
510
511 @Override
512 public boolean onPrepareOptionsMenu(Menu menu) {
513 super.onPrepareOptionsMenu(menu);
514 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
515 // could be installed or uninstalled at any time.
516 if (isBarcodeScannerInstalled()) {
517 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
518 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
519 .setIcon(R.drawable.ic_menu_show_barcode);
520 }
521 } else {
522 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
523 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700524
Evan Millardb5d88c2009-08-28 09:31:57 -0700525 // Only show the edit option if we have a selected tab.
526 if (mSelectedRawContactId != null) {
527 if (menu.findItem(MENU_ITEM_EDIT) == null) {
528 menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_editContact)
529 .setIcon(android.R.drawable.ic_menu_edit)
530 .setAlphabeticShortcut('e');
531 }
532 } else {
533 menu.removeItem(MENU_ITEM_EDIT);
534 }
535
Evan Millar7911ff52009-07-21 15:55:18 -0700536 boolean isAggregate = mRawContactIds.size() > 1;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700537 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800538 return true;
539 }
540
541 private boolean isBarcodeScannerInstalled() {
542 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
543 ResolveInfo ri = getPackageManager().resolveActivity(intent,
544 PackageManager.MATCH_DEFAULT_ONLY);
545 return ri != null;
546 }
547
548 @Override
549 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
550 AdapterView.AdapterContextMenuInfo info;
551 try {
552 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
553 } catch (ClassCastException e) {
554 Log.e(TAG, "bad menuInfo", e);
555 return;
556 }
557
558 // This can be null sometimes, don't crash...
559 if (info == null) {
560 Log.e(TAG, "bad menuInfo");
561 return;
562 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700563
Evan Millar45e0ed32009-06-01 16:44:38 -0700564 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
565 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
566 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700567 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
568 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700569 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800570 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700571 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
572 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700573 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700574 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800575 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700576 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700577 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
578 }
579 // TODO(emillar): add back with group support.
580 /* else if (entry.mimetype.equals()) {
581 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
582 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800583 }
584
585 @Override
586 public boolean onOptionsItemSelected(MenuItem item) {
587 switch (item.getItemId()) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700588 case MENU_ITEM_EDIT: {
Evan Millardb5d88c2009-08-28 09:31:57 -0700589 Long rawContactIdToEdit = mSelectedRawContactId;
590 if (rawContactIdToEdit == null) {
591 // This shouldn't be possible. We only show the edit option if
592 // this value is non-null.
593 break;
594 }
Evan Millar8a79cee2009-08-19 17:20:49 -0700595 if (rawContactIdToEdit == ALL_CONTACTS_ID) {
596 // If the "all" tab is selected, edit the next tab.
Evan Millardb5d88c2009-08-28 09:31:57 -0700597 rawContactIdToEdit = getTabRawContactId(mTabWidget.getCurrentTab() + 1);
Evan Millar8a79cee2009-08-19 17:20:49 -0700598 }
599 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
600 rawContactIdToEdit);
601 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
602 REQUEST_EDIT_CONTACT);
603 break;
604 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800605 case MENU_ITEM_DELETE: {
606 // Get confirmation
607 showDialog(DIALOG_CONFIRM_DELETE);
608 return true;
609 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700610
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700611 case MENU_ITEM_SPLIT_AGGREGATE: {
612 showSplitAggregateDialog();
613 return true;
614 }
615
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700616 case MENU_ITEM_JOIN_AGGREGATE: {
617 showJoinAggregateActivity();
618 return true;
619 }
620
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700621 case MENU_ITEM_OPTIONS: {
622 showOptionsActivity();
623 return true;
624 }
625
Evan Millar66388be2009-05-28 15:41:07 -0700626 // TODO(emillar) Bring this back.
627 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800628 if (mCursor.moveToFirst()) {
629 Intent intent = new Intent(SHOW_BARCODE_INTENT);
630 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
631 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700632 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800633 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700634 // Correctly handle when section headers are hidden
635 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700636
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800637 bundle.putString(Contacts.Intents.Insert.NAME, name);
638 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700639 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800640 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700641 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800642 bundle.putString(PHONE_KEYS[x], entry.data);
643 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700644 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800645 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700646 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800647 bundle.putString(EMAIL_KEYS[x], entry.data);
648 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700649 if (mPostalEntries.size() >= 1 + sepAdjust) {
650 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800651 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
652 }
653 intent.putExtra("ENCODE_DATA", bundle);
654 try {
655 startActivity(intent);
656 } catch (ActivityNotFoundException e) {
657 // The check in onPrepareOptionsMenu() should make this impossible, but
658 // for safety I'm catching the exception rather than crashing. Ideally
659 // I'd call Menu.removeItem() here too, but I don't see a way to get
660 // the options menu.
661 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
662 "was not installed.");
663 }
664 return true;
665 }
666 }
Evan Millar66388be2009-05-28 15:41:07 -0700667 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800668 }
669 return super.onOptionsItemSelected(item);
670 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700671
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800672 @Override
673 public boolean onContextItemSelected(MenuItem item) {
674 switch (item.getItemId()) {
675 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700676 if (makeItemDefault(item)) {
677 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800678 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700679 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800680 }
681 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700682
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800683 return super.onContextItemSelected(item);
684 }
685
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700686 private boolean makeItemDefault(MenuItem item) {
687 ViewEntry entry = getViewEntryForMenuItem(item);
688 if (entry == null) {
689 return false;
690 }
691
692 // Update the primary values in the data record.
693 ContentValues values = new ContentValues(2);
694 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700695
Evan Millar54a5c9f2009-06-23 17:41:09 -0700696 if (entry.ids.size() > 0) {
697 for (int i = 0; i < entry.ids.size(); i++) {
698 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
699 entry.ids.get(i)),
700 values, null, null);
701 }
702 }
703
704 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700705 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
706 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700707 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700708 return true;
709 }
710
711 /**
712 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
713 * The user picks a contact to be split into its own aggregate or clicks Cancel.
714 */
715 private void showSplitAggregateDialog() {
716
717 // Wrap this dialog in a specific theme so that list items have correct text color.
718 final ContextThemeWrapper dialogContext =
719 new ContextThemeWrapper(this, android.R.style.Theme_Light);
720 AlertDialog.Builder builder =
721 new AlertDialog.Builder(dialogContext);
722 builder.setTitle(getString(R.string.splitAggregate_title));
723
724 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
725 builder.setView(view);
726
727 builder.setInverseBackgroundForced(true);
728 builder.setCancelable(true);
729 builder.setNegativeButton(android.R.string.cancel,
730 new OnClickListener() {
731 public void onClick(DialogInterface dialog, int which) {
732 dialog.dismiss();
733 }
734 });
735 final AlertDialog dialog = builder.create();
736
737 view.setOnContactSelectedListener(new OnContactSelectedListener() {
738 public void onContactSelected(long contactId) {
739 dialog.dismiss();
740 splitContact(contactId);
741 }
742 });
743
744 dialog.show();
745 }
746
747 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700748 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700749 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700750 public void showJoinAggregateActivity() {
751 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
752 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700753 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700754 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700755
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700756 @Override
757 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700758 switch (requestCode) {
759 case REQUEST_JOIN_CONTACT: {
760 if (resultCode == RESULT_OK && intent != null) {
761 final long aggregateId = ContentUris.parseId(intent.getData());
762 joinAggregate(aggregateId);
763 }
764 break;
765 }
766 case REQUEST_EDIT_CONTACT: {
767 if (resultCode == RESULT_OK && intent != null) {
768 long newInitialSelectedRawContactId = intent.getLongExtra(
769 RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
770 if (newInitialSelectedRawContactId != mSelectedRawContactId) {
771 mSelectedRawContactId = newInitialSelectedRawContactId;
772 selectInitialTab();
773 }
774 }
775 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700776 }
777 }
778
779 private void splitContact(long contactId) {
780 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_OUT);
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700781 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700782 mAdapter.notifyDataSetChanged();
783 }
784
785 private void joinAggregate(final long aggregateId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700786 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700787 RawContacts.CONTACT_ID + "=" + aggregateId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700788
789 try {
790 while(c.moveToNext()) {
791 long contactId = c.getLong(0);
792 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
793 }
794 } finally {
795 c.close();
796 }
797
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700798 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700799 mAdapter.notifyDataSetChanged();
800 }
801
802 /**
803 * Given a contact ID sets an aggregation exception to either join the contact with the
804 * current aggregate or split off.
805 */
806 protected void setAggregationException(long contactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700807 ContentValues values = new ContentValues(3);
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700808 values.put(AggregationExceptions.CONTACT_ID, ContentUris.parseId(mUri));
Jeff Sharkey14f61ab2009-08-05 21:02:37 -0700809 values.put(AggregationExceptions.RAW_CONTACT_ID, contactId);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700810 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700811 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700812 }
813
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700814 private void showOptionsActivity() {
815 final Intent intent = new Intent(this, ContactOptionsActivity.class);
816 intent.setData(mUri);
817 startActivity(intent);
818 }
819
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700820 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
821 AdapterView.AdapterContextMenuInfo info;
822 try {
823 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
824 } catch (ClassCastException e) {
825 Log.e(TAG, "bad menuInfo", e);
826 return null;
827 }
828
829 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
830 }
831
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800832 @Override
833 public boolean onKeyDown(int keyCode, KeyEvent event) {
834 switch (keyCode) {
835 case KeyEvent.KEYCODE_CALL: {
836 try {
837 ITelephony phone = ITelephony.Stub.asInterface(
838 ServiceManager.checkService("phone"));
839 if (phone != null && !phone.isIdle()) {
840 // Skip out and let the key be handled at a higher level
841 break;
842 }
843 } catch (RemoteException re) {
844 // Fall through and try to call the contact
845 }
846
Evan Millar7911ff52009-07-21 15:55:18 -0700847 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800848 if (index != -1) {
849 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700850 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
851 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800852 }
853 } else if (mNumPhoneNumbers != 0) {
854 // There isn't anything selected, call the default number
855 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
856 startActivity(intent);
857 }
858 return true;
859 }
860
861 case KeyEvent.KEYCODE_DEL: {
862 showDialog(DIALOG_CONFIRM_DELETE);
863 return true;
864 }
865 }
866
867 return super.onKeyDown(keyCode, event);
868 }
869
Evan Millar7911ff52009-07-21 15:55:18 -0700870 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800871 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
872 if (entry != null) {
873 Intent intent = entry.intent;
874 if (intent != null) {
875 try {
876 startActivity(intent);
877 } catch (ActivityNotFoundException e) {
878 Log.e(TAG, "No activity found for intent: " + intent);
879 signalError();
880 }
881 } else {
882 signalError();
883 }
884 } else {
885 signalError();
886 }
887 }
888
889 /**
890 * Signal an error to the user via a beep, or some other method.
891 */
892 private void signalError() {
893 //TODO: implement this when we have the sonification APIs
894 }
895
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800896 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700897 // 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 -0800898 StringBuilder buf = new StringBuilder("imto://");
899 buf.append(host);
900 buf.append('/');
901 buf.append(data);
902 return Uri.parse(buf.toString());
903 }
904
905 /**
906 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700907 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800908 * @param personCursor the URI for the contact being displayed
909 */
Evan Millar11d628c2009-09-02 08:55:01 -0700910 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800911 // Clear out the old entries
912 final int numSections = mSections.size();
913 for (int i = 0; i < numSections; i++) {
914 mSections.get(i).clear();
915 }
916
Evan Millar7911ff52009-07-21 15:55:18 -0700917 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700918
Evan Millar11d628c2009-09-02 08:55:01 -0700919 Sources sources = Sources.getInstance(this);
920
Evan Millar66388be2009-05-28 15:41:07 -0700921 // Build up method entries
922 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700923 for (Entity entity: mEntities) {
924 final ContentValues entValues = entity.getEntityValues();
925 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Evan Millar7911ff52009-07-21 15:55:18 -0700926 // TODO: entry.contactId should be renamed to entry.rawContactId
Evan Millar11d628c2009-09-02 08:55:01 -0700927 long contactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700928
Evan Millar11d628c2009-09-02 08:55:01 -0700929 for (NamedContentValues subValue : entity.getSubValues()) {
930 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800931
Evan Millar11d628c2009-09-02 08:55:01 -0700932 ContentValues entryValues = subValue.values;
933 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
934 if (mimetype == null || accountType == null) {
Evan Millar66388be2009-05-28 15:41:07 -0700935 continue;
936 }
937
Evan Millar11d628c2009-09-02 08:55:01 -0700938 ContactsSource contactsSource = sources.getInflatedSource(accountType,
939 ContactsSource.LEVEL_MIMETYPES);
940 if (contactsSource == null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800941 continue;
942 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700943
Evan Millar11d628c2009-09-02 08:55:01 -0700944 DataKind kind = contactsSource.getKindForMimetype(mimetype);
945 if (kind == null) {
946 continue;
947 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800948
Evan Millar11d628c2009-09-02 08:55:01 -0700949 final long id = entryValues.getAsLong(Data._ID);
950 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
951 entry.contactId = contactId;
952 entry.id = id;
953 entry.uri = uri;
954 entry.mimetype = mimetype;
955 entry.label = buildActionString(kind, entryValues, true);
956 entry.data = buildDataString(kind, entryValues);
957 if (kind.typeColumn != null) {
958 entry.type = entryValues.getAsInteger(kind.typeColumn);
959 }
960 if (kind.iconRes > 0) {
961 entry.actionIcon = kind.iconRes;
962 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800963
Evan Millar11d628c2009-09-02 08:55:01 -0700964 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700965 if (TextUtils.isEmpty(entry.data)) {
966 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700967 continue;
968 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700969
Evan Millar11d628c2009-09-02 08:55:01 -0700970 if (!mRawContactIds.contains(entry.contactId)) {
971 mRawContactIds.add(entry.contactId);
972 }
973
974 // This performs the tab filtering
975 if (mSelectedRawContactId != null
976 && mSelectedRawContactId != entry.contactId
977 && mSelectedRawContactId != ALL_CONTACTS_ID) {
978 continue;
979 }
980
981 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)
982 || CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)
983 || CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)
984 || CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
985 final boolean isSuperPrimary = entryValues.getAsInteger(
986 Data.IS_SUPER_PRIMARY) != 0;
987
988 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
989 // Build phone entries
990 mNumPhoneNumbers++;
991
992 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
993 Uri.fromParts("tel", entry.data, null));
994 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
995 Uri.fromParts("sms", entry.data, null));
996 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700997
998 // If data is empty, don't show it.
999 if (TextUtils.isEmpty(entry.data)) {
1000 Log.w(TAG, "empty data for contact method " + id);
1001 continue;
1002 }
1003
Evan Millar11d628c2009-09-02 08:55:01 -07001004 entry.isPrimary = isSuperPrimary;
1005 mPhoneEntries.add(entry);
1006
1007 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
1008 || mShowSmsLinksForAllPhones) {
1009 // Add an SMS entry
1010 if (kind.iconAltRes > 0) {
1011 entry.secondaryActionIcon = kind.iconAltRes;
1012 }
1013 }
1014 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
1015 // Build email entries
1016 entry.intent = new Intent(Intent.ACTION_SENDTO,
1017 Uri.fromParts("mailto", entry.data, null));
Evan Millar49714ee2009-09-02 16:42:47 -07001018 // Temporary hack until we get real label resources for exchange.
1019 if (TextUtils.isEmpty(entry.label)) {
1020 entry.label = getString(R.string.email).toLowerCase();
1021 }
Evan Millar11d628c2009-09-02 08:55:01 -07001022 entry.isPrimary = isSuperPrimary;
1023 mEmailEntries.add(entry);
1024 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
1025 equals(mimetype)) {
1026 // Build postal entries
1027 entry.maxLines = 4;
1028 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
1029 mPostalEntries.add(entry);
1030 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
1031 // Build im entries
1032 Object protocolObj = entryValues.getAsInteger(Data.DATA5);
1033 String host = null;
1034
Evan Millar49714ee2009-09-02 16:42:47 -07001035 if (TextUtils.isEmpty(entry.label)) {
1036 entry.label = getString(R.string.im).toLowerCase();
1037 }
1038
Evan Millar11d628c2009-09-02 08:55:01 -07001039 if (protocolObj instanceof Number) {
1040 int protocol = ((Number) protocolObj).intValue();
1041 host = ContactsUtils.lookupProviderNameFromId(
1042 protocol).toLowerCase();
1043 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
1044 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
1045 entry.maxLabelLines = 2;
1046 }
1047 } else if (protocolObj != null) {
1048 String providerName = (String) protocolObj;
1049 host = providerName.toLowerCase();
1050 }
1051
1052 // Only add the intent if there is a valid host
1053 if (!TextUtils.isEmpty(host)) {
1054 entry.intent = new Intent(Intent.ACTION_SENDTO,
1055 constructImToUrl(host, entry.data));
1056 }
1057 //TODO(emillar) Add in presence info
1058 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
1059 entry.presenceIcon = Presence.getPresenceIconResourceId(
1060 aggCursor.getInt(METHODS_STATUS_COLUMN));
1061 entry.status = ...
1062 }*/
1063 mImEntries.add(entry);
1064 }
1065 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
1066 // Build organization entries
1067 mOrganizationEntries.add(entry);
1068 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
1069 // Build note entries
1070 entry.id = 0;
1071 entry.uri = null;
1072 entry.intent = null;
1073 entry.maxLines = 10;
1074 mOtherEntries.add(entry);
1075 }
1076
1077
1078 // TODO(emillar) Add group entries
1079 // // Build the group entries
1080 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1081 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1082 // null, null, Groups.DEFAULT_SORT_ORDER);
1083 // if (groupCursor != null) {
1084 // try {
1085 // StringBuilder sb = new StringBuilder();
1086 //
1087 // while (groupCursor.moveToNext()) {
1088 // String systemId = groupCursor.getString(
1089 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1090 //
1091 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1092 // continue;
1093 // }
1094 //
1095 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1096 // if (!TextUtils.isEmpty(name)) {
1097 // if (sb.length() == 0) {
1098 // sb.append(name);
1099 // } else {
1100 // sb.append(getString(R.string.group_list, name));
1101 // }
1102 // }
1103 // }
1104 //
1105 // if (sb.length() > 0) {
1106 // ViewEntry entry = new ViewEntry();
1107 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1108 // entry.label = getString(R.string.label_groups);
1109 // entry.data = sb.toString();
1110 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1111 //
1112 // // TODO: Add an icon for the groups item.
1113 //
1114 // mGroupEntries.add(entry);
1115 // }
1116 // } finally {
1117 // groupCursor.close();
1118 // }
1119 // }
Evan Millar66388be2009-05-28 15:41:07 -07001120 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001121
Evan Millar66388be2009-05-28 15:41:07 -07001122 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001123 }
1124 }
1125
Evan Millar11d628c2009-09-02 08:55:01 -07001126 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1127 if (kind.actionHeader == null) {
1128 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001129 }
Evan Millar11d628c2009-09-02 08:55:01 -07001130 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1131 if (actionHeader == null) {
1132 return null;
1133 }
1134 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1135 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001136
Evan Millar11d628c2009-09-02 08:55:01 -07001137 String buildDataString(DataKind kind, ContentValues values) {
1138 if (kind.actionBody == null) {
1139 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001140 }
Evan Millar11d628c2009-09-02 08:55:01 -07001141 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1142 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001143 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001144
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001145 /**
1146 * A basic structure with the data for a contact entry in the list.
1147 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001148 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001149 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001150 public boolean isPrimary = false;
1151 public int presenceIcon = -1;
1152 public int secondaryActionIcon = -1;
1153 public Intent intent;
1154 public Intent secondaryIntent = null;
1155 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001156 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001157 public ArrayList<Long> ids = new ArrayList<Long>();
1158 public int collapseCount = 0;
1159
1160 public boolean collapseWith(ViewEntry entry) {
1161 // assert equal collapse keys
1162 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1163 return false;
1164 }
1165
1166 // Choose the label associated with the highest type precedence.
1167 if (TypePrecedence.getTypePrecedence(mimetype, type)
1168 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1169 type = entry.type;
1170 label = entry.label;
1171 }
1172
1173 // Choose the max of the maxLines and maxLabelLines values.
1174 maxLines = Math.max(maxLines, entry.maxLines);
1175 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1176
1177 // Choose the presence with the highest precedence.
1178 if (Presence.getPresencePrecedence(status)
1179 < Presence.getPresencePrecedence(entry.status)) {
1180 status = entry.status;
1181 }
1182
1183 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001184 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001185
1186 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1187
1188 // Keep track of all the ids that have been collapsed with this one.
1189 ids.add(entry.id);
1190 collapseCount++;
1191 return true;
1192 }
1193
1194 public String getCollapseKey() {
1195 StringBuilder hashSb = new StringBuilder();
1196 hashSb.append(data);
1197 hashSb.append(mimetype);
1198 hashSb.append((intent != null && intent.getAction() != null)
1199 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001200 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1201 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001202 hashSb.append(actionIcon);
1203 return hashSb.toString();
1204 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001205 }
1206
Evan Millar15e514d2009-08-04 10:14:57 -07001207 /** Cache of the children views of a row */
1208 static class ViewCache {
1209 public TextView label;
1210 public TextView data;
1211 public ImageView actionIcon;
1212 public ImageView presenceIcon;
1213 public ImageView primaryIcon;
1214 public ImageView secondaryActionButton;
1215 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001216
Evan Millar15e514d2009-08-04 10:14:57 -07001217 // Need to keep track of this too
1218 ViewEntry entry;
1219 }
1220
1221 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1222 implements View.OnClickListener {
1223
Evan Millar5c22c3b2009-05-29 11:37:54 -07001224
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001225 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1226 super(context, sections, SHOW_SEPARATORS);
1227 }
1228
Evan Millar15e514d2009-08-04 10:14:57 -07001229 public void onClick(View v) {
1230 Intent intent = (Intent) v.getTag();
1231 startActivity(intent);
1232 }
1233
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001234 @Override
1235 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001236 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001237 View v;
1238
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001239 ViewCache views;
1240
1241 // Check to see if we can reuse convertView
1242 if (convertView != null) {
1243 v = convertView;
1244 views = (ViewCache) v.getTag();
1245 } else {
1246 // Create a new view if needed
1247 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1248
1249 // Cache the children
1250 views = new ViewCache();
1251 views.label = (TextView) v.findViewById(android.R.id.text1);
1252 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001253 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1254 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1255 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1256 views.secondaryActionButton = (ImageView) v.findViewById(
1257 R.id.secondary_action_button);
1258 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001259 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001260 v.setTag(views);
1261 }
1262
1263 // Update the entry in the view cache
1264 views.entry = entry;
1265
1266 // Bind the data to the view
1267 bindView(v, entry);
1268 return v;
1269 }
1270
1271 @Override
1272 protected View newView(int position, ViewGroup parent) {
1273 // getView() handles this
1274 throw new UnsupportedOperationException();
1275 }
1276
1277 @Override
1278 protected void bindView(View view, ViewEntry entry) {
1279 final Resources resources = mContext.getResources();
1280 ViewCache views = (ViewCache) view.getTag();
1281
1282 // Set the label
1283 TextView label = views.label;
1284 setMaxLines(label, entry.maxLabelLines);
1285 label.setText(entry.label);
1286
1287 // Set the data
1288 TextView data = views.data;
1289 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001290 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1291 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1292 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1293 } else {
1294 data.setText(entry.data);
1295 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001296 setMaxLines(data, entry.maxLines);
1297 }
1298
Evan Millar15e514d2009-08-04 10:14:57 -07001299 // Set the primary icon
1300 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1301
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001302 // Set the action icon
1303 ImageView action = views.actionIcon;
1304 if (entry.actionIcon != -1) {
1305 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1306 action.setVisibility(View.VISIBLE);
1307 } else {
1308 // Things should still line up as if there was an icon, so make it invisible
1309 action.setVisibility(View.INVISIBLE);
1310 }
1311
1312 // Set the presence icon
1313 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001314 if (entry.presenceIcon != -1) {
1315 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001316 } else if (entry.status != -1) {
1317 presenceIcon = resources.getDrawable(
1318 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001319 }
Evan Millar15e514d2009-08-04 10:14:57 -07001320 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001321 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001322 presenceIconView.setImageDrawable(presenceIcon);
1323 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001324 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001325 presenceIconView.setVisibility(View.GONE);
1326 }
1327
1328 // Set the secondary action button
1329 ImageView secondaryActionView = views.secondaryActionButton;
1330 Drawable secondaryActionIcon = null;
1331 if (entry.secondaryActionIcon != -1) {
1332 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1333 }
1334 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1335 secondaryActionView.setImageDrawable(secondaryActionIcon);
1336 secondaryActionView.setTag(entry.secondaryIntent);
1337 secondaryActionView.setVisibility(View.VISIBLE);
1338 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1339 } else {
1340 secondaryActionView.setVisibility(View.GONE);
1341 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001342 }
1343 }
1344
1345 private void setMaxLines(TextView textView, int maxLines) {
1346 if (maxLines == 1) {
1347 textView.setSingleLine(true);
1348 textView.setEllipsize(TextUtils.TruncateAt.END);
1349 } else {
1350 textView.setSingleLine(false);
1351 textView.setMaxLines(maxLines);
1352 textView.setEllipsize(null);
1353 }
1354 }
1355 }
1356}