blob: 0eafd7ca0c2c2b195925b4de6700e5f5ce9d5647 [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts;
18
Evan Millar54a5c9f2009-06-23 17:41:09 -070019import com.android.contacts.Collapser.Collapsible;
Evan Millar11d628c2009-09-02 08:55:01 -070020import com.android.contacts.ScrollingTabWidget.OnTabSelectionChangedListener;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070021import com.android.contacts.SplitAggregateView.OnContactSelectedListener;
Evan Millar11d628c2009-09-02 08:55:01 -070022import com.android.contacts.model.ContactsSource;
23import com.android.contacts.model.Sources;
24import com.android.contacts.model.ContactsSource.DataKind;
Evan Millar49714ee2009-09-02 16:42:47 -070025import com.android.contacts.model.HardCodedSources.SimpleInflater;
Jeff Sharkey802b2052009-08-04 14:21:06 -070026import com.android.contacts.ui.FastTrackWindow;
Evan Millar11d628c2009-09-02 08:55:01 -070027import com.android.contacts.util.NotifyingAsyncQueryHandler;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080028import com.android.internal.telephony.ITelephony;
Evan Millar11d628c2009-09-02 08:55:01 -070029import com.android.internal.widget.ContactHeaderWidget;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080030
Evan Millar11d628c2009-09-02 08:55:01 -070031import android.app.Activity;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080032import android.app.AlertDialog;
33import android.app.Dialog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080034import android.content.ActivityNotFoundException;
35import android.content.ContentResolver;
36import android.content.ContentUris;
37import android.content.ContentValues;
38import android.content.Context;
39import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070040import android.content.Entity;
Evan Millar11d628c2009-09-02 08:55:01 -070041import android.content.EntityIterator;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080042import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070043import android.content.DialogInterface.OnClickListener;
Evan Millar11d628c2009-09-02 08:55:01 -070044import android.content.Entity.NamedContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080045import android.content.pm.PackageManager;
46import android.content.pm.ResolveInfo;
47import android.content.res.Resources;
48import android.database.ContentObserver;
49import android.database.Cursor;
Dmitri Plotnikov3d53ce22009-09-02 08:44:32 -070050import android.database.DatabaseUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080051import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080052import android.net.Uri;
53import android.os.Bundle;
54import android.os.Handler;
55import android.os.RemoteException;
56import android.os.ServiceManager;
Dmitri Plotnikov3d53ce22009-09-02 08:44:32 -070057import android.provider.ContactsContract;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070058import android.provider.ContactsContract.AggregationExceptions;
59import android.provider.ContactsContract.CommonDataKinds;
Evan Millar11d628c2009-09-02 08:55:01 -070060import android.provider.ContactsContract.Contacts;
Evan Millar66388be2009-05-28 15:41:07 -070061import android.provider.ContactsContract.Data;
Evan Millar54a5c9f2009-06-23 17:41:09 -070062import android.provider.ContactsContract.Presence;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070063import android.provider.ContactsContract.RawContacts;
Evan Millar54a5c9f2009-06-23 17:41:09 -070064import android.provider.ContactsContract.CommonDataKinds.Phone;
65import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080066import android.text.TextUtils;
67import android.util.Log;
Evan Millar11d628c2009-09-02 08:55:01 -070068import android.util.SparseArray;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080069import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070070import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080071import android.view.KeyEvent;
Evan Millar11d628c2009-09-02 08:55:01 -070072import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080073import android.view.Menu;
74import android.view.MenuItem;
75import android.view.View;
76import android.view.ViewGroup;
Evan Millar11d628c2009-09-02 08:55:01 -070077import android.view.Window;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080078import android.view.ContextMenu.ContextMenuInfo;
79import android.widget.AdapterView;
Evan Millar7911ff52009-07-21 15:55:18 -070080import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080081import android.widget.ImageView;
82import android.widget.ListView;
83import android.widget.TextView;
84import android.widget.Toast;
85
86import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087
88/**
89 * Displays the details of a specific contact.
90 */
Evan Millar11d628c2009-09-02 08:55:01 -070091public class ViewContactActivity extends Activity
Evan Millar7911ff52009-07-21 15:55:18 -070092 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
Evan Millar11d628c2009-09-02 08:55:01 -070093 AdapterView.OnItemClickListener, NotifyingAsyncQueryHandler.AsyncQueryListener,
94 OnTabSelectionChangedListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080095 private static final String TAG = "ViewContact";
96 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
97
Evan Millar8a79cee2009-08-19 17:20:49 -070098 public static final String RAW_CONTACT_ID_EXTRA = "rawContactIdExtra";
99
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800100 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800101
102 private static final int DIALOG_CONFIRM_DELETE = 1;
103
Evan Millar8a79cee2009-08-19 17:20:49 -0700104 private static final int REQUEST_JOIN_CONTACT = 1;
105 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700106
Evan Millar8a79cee2009-08-19 17:20:49 -0700107 public static final int MENU_ITEM_EDIT = 1;
108 public static final int MENU_ITEM_DELETE = 2;
109 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
110 public static final int MENU_ITEM_SHOW_BARCODE = 4;
111 public static final int MENU_ITEM_SPLIT_AGGREGATE = 5;
112 public static final int MENU_ITEM_JOIN_AGGREGATE = 6;
113 public static final int MENU_ITEM_OPTIONS = 7;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800114
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700115 protected Uri mLookupUri;
Evan Millar11d628c2009-09-02 08:55:01 -0700116 private Uri mUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800117 private ContentResolver mResolver;
118 private ViewAdapter mAdapter;
119 private int mNumPhoneNumbers = 0;
120
Evan Millar8a79cee2009-08-19 17:20:49 -0700121 private static final long ALL_CONTACTS_ID = -100;
Evan Millar7911ff52009-07-21 15:55:18 -0700122
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700123 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700124 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700125 */
Evan Millar7911ff52009-07-21 15:55:18 -0700126 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700127
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800128 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
129 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
130 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
131 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
132 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
133 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700134 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800135 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
136 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
137
138 private Cursor mCursor;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700139
Evan Millar11d628c2009-09-02 08:55:01 -0700140 private SparseArray<Long> mTabRawContactIdMap;
141 protected ScrollingTabWidget mTabWidget;
142 protected ContactHeaderWidget mContactHeaderWidget;
143 private NotifyingAsyncQueryHandler mHandler;
144
145 protected LayoutInflater mInflater;
146
147 //Projection used for the query that determines which tabs to add.
148 protected static final String[] TAB_PROJECTION = new String[] {
149 RawContacts._ID,
150 RawContacts.ACCOUNT_NAME,
151 RawContacts.ACCOUNT_TYPE
152 };
153 protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0;
154 protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1;
155 protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2;
156
157 protected static final String SELECTED_RAW_CONTACT_ID_KEY = "selectedRawContact";
158 protected Long mSelectedRawContactId = null;
159
160 private static final int TOKEN_QUERY = 0;
161
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800162 private ContentObserver mObserver = new ContentObserver(new Handler()) {
163 @Override
164 public boolean deliverSelfNotifications() {
165 return true;
166 }
167
168 @Override
169 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700170 if (mCursor != null && !mCursor.isClosed()) {
171 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800172 }
173 }
174 };
175
176 public void onClick(DialogInterface dialog, int which) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700177 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800178 getContentResolver().delete(mUri, null, null);
179 finish();
180 }
181
Evan Millar7911ff52009-07-21 15:55:18 -0700182 private FrameLayout mTabContentLayout;
183 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800184 private boolean mShowSmsLinksForAllPhones;
Evan Millar11d628c2009-09-02 08:55:01 -0700185 private ArrayList<Entity> mEntities = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800186
187 @Override
188 protected void onCreate(Bundle icicle) {
189 super.onCreate(icicle);
190
Evan Millar11d628c2009-09-02 08:55:01 -0700191 final Intent intent = getIntent();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700192 mLookupUri = intent.getData();
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700193
194 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700195
196 requestWindowFeature(Window.FEATURE_NO_TITLE);
197 setContentView(R.layout.contact_card_layout);
198
199 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
200 mContactHeaderWidget.showStar(true);
Evan Millar11d628c2009-09-02 08:55:01 -0700201 mContactHeaderWidget.setExcludeMimes(new String[] {
202 Contacts.CONTENT_ITEM_TYPE
203 });
204
205 mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget);
206 mTabWidget.setTabSelectionListener(this);
207 mTabWidget.setVisibility(View.INVISIBLE);
208
209 mTabRawContactIdMap = new SparseArray<Long>();
210
211 mHandler = new NotifyingAsyncQueryHandler(this, this);
212
Evan Millar7911ff52009-07-21 15:55:18 -0700213 mListView = new ListView(this);
214 mListView.setOnCreateContextMenuListener(this);
215 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
216 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800217
Evan Millar7911ff52009-07-21 15:55:18 -0700218 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
219 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800220
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800221 mResolver = getContentResolver();
222
223 // Build the list of sections. The order they're added to mSections dictates the
224 // order they are displayed in the list.
225 mSections.add(mPhoneEntries);
226 mSections.add(mSmsEntries);
227 mSections.add(mEmailEntries);
228 mSections.add(mImEntries);
229 mSections.add(mPostalEntries);
230 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700231 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800232 mSections.add(mOtherEntries);
233
234 //TODO Read this value from a preference
235 mShowSmsLinksForAllPhones = true;
236
Evan Millar11d628c2009-09-02 08:55:01 -0700237 startEntityQuery();
238 }
239
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800240 @Override
241 protected void onResume() {
242 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700243 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800244 }
245
246 @Override
247 protected void onPause() {
248 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700249 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800250 }
251
252 @Override
253 protected void onDestroy() {
254 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700255 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800256 }
257
258 @Override
Evan Millar11d628c2009-09-02 08:55:01 -0700259 protected void onRestoreInstanceState(Bundle savedInstanceState) {
260 super.onRestoreInstanceState(savedInstanceState);
261 mSelectedRawContactId = savedInstanceState.getLong(SELECTED_RAW_CONTACT_ID_KEY);
262 }
263
264 @Override
265 protected void onSaveInstanceState(Bundle outState) {
266 super.onSaveInstanceState(outState);
267 outState.putLong(SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
268 }
269
270 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800271 protected Dialog onCreateDialog(int id) {
272 switch (id) {
273 case DIALOG_CONFIRM_DELETE:
274 return new AlertDialog.Builder(this)
275 .setTitle(R.string.deleteConfirmation_title)
276 .setIcon(android.R.drawable.ic_dialog_alert)
277 .setMessage(R.string.deleteConfirmation)
278 .setNegativeButton(android.R.string.cancel, null)
279 .setPositiveButton(android.R.string.ok, this)
280 .setCancelable(false)
281 .create();
282 }
283 return null;
284 }
285
Evan Millar11d628c2009-09-02 08:55:01 -0700286
287 // TAB CODE //
288 /**
289 * Adds a tab for each {@link RawContact} associated with this contact.
290 * Override this method if you want to additional tabs and/or different
291 * tabs for your activity.
292 *
293 * @param entities An {@link ArrayList} of {@link Entity}s of all the RawContacts
294 * associated with the contact being displayed.
295 */
296 protected void bindTabs() {
297 if (mEntities.size() > 1) {
Evan Millar7911ff52009-07-21 15:55:18 -0700298 addAllTab();
299 }
Evan Millar11d628c2009-09-02 08:55:01 -0700300
301 final Sources sources = Sources.getInstance(this);
302
303 for (Entity entity : mEntities) {
304 final String accountType = entity.getEntityValues().
305 getAsString(RawContacts.ACCOUNT_TYPE);
306 final Long rawContactId = entity.getEntityValues().
307 getAsLong(RawContacts._ID);
308
309 // TODO: ensure inflation on background task so we don't block UI thread here
310 final ContactsSource source = sources.getInflatedSource(accountType,
311 ContactsSource.LEVEL_SUMMARY);
312 addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(), source));
313 }
314
315 selectInitialTab();
316 mTabWidget.setVisibility(View.VISIBLE);
317 mTabWidget.postInvalidate();
318 }
319
320 /**
321 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
322 *
323 * @param contactId The contact id associated with the tab.
324 * @param view A view to use as the tab indicator.
325 */
326 protected void addTab(long rawContactId, View view) {
327 mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId);
328 mTabWidget.addTab(view);
329 }
330
331
332 protected void clearCurrentTabs() {
333 mTabRawContactIdMap.clear();
334 mTabWidget.removeAllTabs();
335 }
336
337 protected void selectInitialTab() {
338 int selectedTabIndex = 0;
339
340 if (mSelectedRawContactId != null) {
341 selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
342 if (selectedTabIndex == -1) {
343 // If there was no matching tab, just select the first;
344 selectedTabIndex = 0;
345 }
346 }
347
348 mTabWidget.setCurrentTab(selectedTabIndex);
349 onTabSelectionChanged(selectedTabIndex, false);
350 }
351
Evan Millar7911ff52009-07-21 15:55:18 -0700352 private void addAllTab() {
Evan Millar56d2caa2009-08-20 20:30:12 -0700353 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator,
354 mTabWidget.getTabParent(), false);
Evan Millar74660912009-08-19 17:36:33 -0700355 allTabIndicator.getBackground().setDither(true);
Evan Millar7911ff52009-07-21 15:55:18 -0700356 addTab(ALL_CONTACTS_ID, allTabIndicator);
357 }
358
359 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
360 long rawContactId = getTabRawContactId(tabIndex);
Evan Millar8a79cee2009-08-19 17:20:49 -0700361 mSelectedRawContactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700362 bindData();
Evan Millar7911ff52009-07-21 15:55:18 -0700363 }
364
Evan Millar11d628c2009-09-02 08:55:01 -0700365 /**
366 * Return the RawContact id associated with the tab at an index.
367 *
368 * @param index The index of the tab in question.
369 * @return The contactId associated with the tab at the specified index.
370 */
371 protected long getTabRawContactId(int index) {
372 return mTabRawContactIdMap.get(index);
373 }
Evan Millar2c1cc832009-07-13 11:08:06 -0700374
Evan Millar11d628c2009-09-02 08:55:01 -0700375 /**
376 * Return the tab index associated with the RawContact id.
377 *
378 * @param index The index of the tab in question.
379 * @return The contactId associated with the tab at the specified index.
380 */
381 protected int getTabIndexForRawContactId(long rawContactId) {
382 int numTabs = mTabRawContactIdMap.size();
383 for (int i=0; i < numTabs; i++) {
384 if (mTabRawContactIdMap.get(i) == rawContactId) {
385 return i;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800386 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800387 }
Evan Millar11d628c2009-09-02 08:55:01 -0700388 return -1;
389 }
390
391
392 // QUERY CODE //
393 /** {@inheritDoc} */
394 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
395 try{
396 if (token == TOKEN_QUERY) {
397 clearCurrentTabs();
398 mEntities = readEntities(iterator);
399 bindTabs();
400 bindData();
401 }
402 } finally {
403 if (iterator != null) {
404 iterator.close();
405 }
406 }
407 }
408
409 /** {@inheritDoc} */
410 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700411 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700412 }
413
414 private ArrayList<Entity> readEntities(EntityIterator iterator) {
415 ArrayList<Entity> entities = new ArrayList<Entity>();
416 try {
417 while (iterator.hasNext()) {
418 entities.add(iterator.next());
419 }
420 } catch (RemoteException e) {
421 }
422
423 return entities;
424 }
425
426 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700427 closeCursor();
428
Dmitri Plotnikov0306bbd2009-09-03 14:37:15 -0700429 mUri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700430 if (mLookupUri != null) {
431 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
432 if (mLookupUri != null) {
433 mUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
434 }
435 }
436
437 if (mUri == null) {
438
439 // TODO either figure out a way to prevent a flash of black background or
440 // use some other UI than a toast
441 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
442 Log.e(TAG, "invalid contact uri: " + mLookupUri);
443 finish();
444 return;
445 }
446
447 mCursor = mResolver.query(Uri.withAppendedPath(mUri, Contacts.Data.CONTENT_DIRECTORY),
448 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
449 mCursor.registerContentObserver(mObserver);
450
Evan Millar11d628c2009-09-02 08:55:01 -0700451 long contactId = ContentUris.parseId(mUri);
452 mHandler.startQueryEntities(TOKEN_QUERY, null,
453 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700454
455 mContactHeaderWidget.bindFromContactId(ContentUris.parseId(mUri));
456 }
457
458 private void closeCursor() {
459 if (mCursor != null) {
460 mCursor.unregisterContentObserver(mObserver);
461 mCursor.close();
462 mCursor = null;
463 }
Evan Millar11d628c2009-09-02 08:55:01 -0700464 }
465
466 private void bindData() {
467
468 // Build up the contact entries
469 buildEntries();
470
471 // Collapse similar data items in select sections.
472 Collapser.collapseList(mPhoneEntries);
473 Collapser.collapseList(mSmsEntries);
474 Collapser.collapseList(mEmailEntries);
475 Collapser.collapseList(mPostalEntries);
476
477 if (mAdapter == null) {
478 mAdapter = new ViewAdapter(this, mSections);
479 mListView.setAdapter(mAdapter);
480 } else {
481 mAdapter.setSections(mSections, SHOW_SEPARATORS);
482 }
483
484// else {
485// Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
486// Log.e(TAG, "invalid contact uri: " + mOriginalUri);
487// finish();
488// }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800489 }
490
491 @Override
492 public boolean onCreateOptionsMenu(Menu menu) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800493 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
494 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700495 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
496 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700497 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
498 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700499 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
500 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800501 return true;
502 }
503
504 @Override
505 public boolean onPrepareOptionsMenu(Menu menu) {
506 super.onPrepareOptionsMenu(menu);
507 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
508 // could be installed or uninstalled at any time.
509 if (isBarcodeScannerInstalled()) {
510 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
511 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
512 .setIcon(R.drawable.ic_menu_show_barcode);
513 }
514 } else {
515 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
516 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700517
Evan Millardb5d88c2009-08-28 09:31:57 -0700518 // Only show the edit option if we have a selected tab.
519 if (mSelectedRawContactId != null) {
520 if (menu.findItem(MENU_ITEM_EDIT) == null) {
521 menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_editContact)
522 .setIcon(android.R.drawable.ic_menu_edit)
523 .setAlphabeticShortcut('e');
524 }
525 } else {
526 menu.removeItem(MENU_ITEM_EDIT);
527 }
528
Evan Millar7911ff52009-07-21 15:55:18 -0700529 boolean isAggregate = mRawContactIds.size() > 1;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700530 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800531 return true;
532 }
533
534 private boolean isBarcodeScannerInstalled() {
535 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
536 ResolveInfo ri = getPackageManager().resolveActivity(intent,
537 PackageManager.MATCH_DEFAULT_ONLY);
538 return ri != null;
539 }
540
541 @Override
542 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
543 AdapterView.AdapterContextMenuInfo info;
544 try {
545 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
546 } catch (ClassCastException e) {
547 Log.e(TAG, "bad menuInfo", e);
548 return;
549 }
550
551 // This can be null sometimes, don't crash...
552 if (info == null) {
553 Log.e(TAG, "bad menuInfo");
554 return;
555 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700556
Evan Millar45e0ed32009-06-01 16:44:38 -0700557 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
558 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
559 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700560 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
561 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700562 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800563 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700564 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
565 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700566 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700567 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800568 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700569 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700570 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
571 }
572 // TODO(emillar): add back with group support.
573 /* else if (entry.mimetype.equals()) {
574 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
575 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800576 }
577
578 @Override
579 public boolean onOptionsItemSelected(MenuItem item) {
580 switch (item.getItemId()) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700581 case MENU_ITEM_EDIT: {
Evan Millardb5d88c2009-08-28 09:31:57 -0700582 Long rawContactIdToEdit = mSelectedRawContactId;
583 if (rawContactIdToEdit == null) {
584 // This shouldn't be possible. We only show the edit option if
585 // this value is non-null.
586 break;
587 }
Evan Millar8a79cee2009-08-19 17:20:49 -0700588 if (rawContactIdToEdit == ALL_CONTACTS_ID) {
589 // If the "all" tab is selected, edit the next tab.
Evan Millardb5d88c2009-08-28 09:31:57 -0700590 rawContactIdToEdit = getTabRawContactId(mTabWidget.getCurrentTab() + 1);
Evan Millar8a79cee2009-08-19 17:20:49 -0700591 }
592 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
593 rawContactIdToEdit);
594 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
595 REQUEST_EDIT_CONTACT);
596 break;
597 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800598 case MENU_ITEM_DELETE: {
599 // Get confirmation
600 showDialog(DIALOG_CONFIRM_DELETE);
601 return true;
602 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700603
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700604 case MENU_ITEM_SPLIT_AGGREGATE: {
605 showSplitAggregateDialog();
606 return true;
607 }
608
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700609 case MENU_ITEM_JOIN_AGGREGATE: {
610 showJoinAggregateActivity();
611 return true;
612 }
613
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700614 case MENU_ITEM_OPTIONS: {
615 showOptionsActivity();
616 return true;
617 }
618
Evan Millar66388be2009-05-28 15:41:07 -0700619 // TODO(emillar) Bring this back.
620 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800621 if (mCursor.moveToFirst()) {
622 Intent intent = new Intent(SHOW_BARCODE_INTENT);
623 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
624 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700625 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800626 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700627 // Correctly handle when section headers are hidden
628 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700629
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800630 bundle.putString(Contacts.Intents.Insert.NAME, name);
631 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700632 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800633 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700634 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800635 bundle.putString(PHONE_KEYS[x], entry.data);
636 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700637 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800638 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700639 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800640 bundle.putString(EMAIL_KEYS[x], entry.data);
641 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700642 if (mPostalEntries.size() >= 1 + sepAdjust) {
643 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800644 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
645 }
646 intent.putExtra("ENCODE_DATA", bundle);
647 try {
648 startActivity(intent);
649 } catch (ActivityNotFoundException e) {
650 // The check in onPrepareOptionsMenu() should make this impossible, but
651 // for safety I'm catching the exception rather than crashing. Ideally
652 // I'd call Menu.removeItem() here too, but I don't see a way to get
653 // the options menu.
654 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
655 "was not installed.");
656 }
657 return true;
658 }
659 }
Evan Millar66388be2009-05-28 15:41:07 -0700660 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800661 }
662 return super.onOptionsItemSelected(item);
663 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700664
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800665 @Override
666 public boolean onContextItemSelected(MenuItem item) {
667 switch (item.getItemId()) {
668 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700669 if (makeItemDefault(item)) {
670 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800671 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700672 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800673 }
674 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700675
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800676 return super.onContextItemSelected(item);
677 }
678
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700679 private boolean makeItemDefault(MenuItem item) {
680 ViewEntry entry = getViewEntryForMenuItem(item);
681 if (entry == null) {
682 return false;
683 }
684
685 // Update the primary values in the data record.
686 ContentValues values = new ContentValues(2);
687 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700688
Evan Millar54a5c9f2009-06-23 17:41:09 -0700689 if (entry.ids.size() > 0) {
690 for (int i = 0; i < entry.ids.size(); i++) {
691 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
692 entry.ids.get(i)),
693 values, null, null);
694 }
695 }
696
697 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700698 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
699 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700700 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700701 return true;
702 }
703
704 /**
705 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
706 * The user picks a contact to be split into its own aggregate or clicks Cancel.
707 */
708 private void showSplitAggregateDialog() {
709
710 // Wrap this dialog in a specific theme so that list items have correct text color.
711 final ContextThemeWrapper dialogContext =
712 new ContextThemeWrapper(this, android.R.style.Theme_Light);
713 AlertDialog.Builder builder =
714 new AlertDialog.Builder(dialogContext);
715 builder.setTitle(getString(R.string.splitAggregate_title));
716
717 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
718 builder.setView(view);
719
720 builder.setInverseBackgroundForced(true);
721 builder.setCancelable(true);
722 builder.setNegativeButton(android.R.string.cancel,
723 new OnClickListener() {
724 public void onClick(DialogInterface dialog, int which) {
725 dialog.dismiss();
726 }
727 });
728 final AlertDialog dialog = builder.create();
729
730 view.setOnContactSelectedListener(new OnContactSelectedListener() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700731 public void onContactSelected(long rawContactId) {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700732 dialog.dismiss();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700733 splitContact(rawContactId);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700734 }
735 });
736
737 dialog.show();
738 }
739
740 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700741 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700742 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700743 public void showJoinAggregateActivity() {
744 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
745 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700746 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700747 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700748
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700749 @Override
750 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700751 switch (requestCode) {
752 case REQUEST_JOIN_CONTACT: {
753 if (resultCode == RESULT_OK && intent != null) {
754 final long aggregateId = ContentUris.parseId(intent.getData());
755 joinAggregate(aggregateId);
756 }
757 break;
758 }
759 case REQUEST_EDIT_CONTACT: {
760 if (resultCode == RESULT_OK && intent != null) {
761 long newInitialSelectedRawContactId = intent.getLongExtra(
762 RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
763 if (newInitialSelectedRawContactId != mSelectedRawContactId) {
764 mSelectedRawContactId = newInitialSelectedRawContactId;
765 selectInitialTab();
766 }
767 }
768 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700769 }
770 }
771
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700772 private void splitContact(long rawContactId) {
773 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_OUT);
774
775 // The split operation may have removed the original aggregate contact, so we need
776 // to requery everything
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700777 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700778 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700779 }
780
781 private void joinAggregate(final long aggregateId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700782 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700783 RawContacts.CONTACT_ID + "=" + aggregateId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700784
785 try {
786 while(c.moveToNext()) {
787 long contactId = c.getLong(0);
788 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
789 }
790 } finally {
791 c.close();
792 }
793
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700794 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700795 mAdapter.notifyDataSetChanged();
796 }
797
798 /**
799 * Given a contact ID sets an aggregation exception to either join the contact with the
800 * current aggregate or split off.
801 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700802 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700803 ContentValues values = new ContentValues(3);
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700804 values.put(AggregationExceptions.CONTACT_ID, ContentUris.parseId(mUri));
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700805 values.put(AggregationExceptions.RAW_CONTACT_ID, rawContactId);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700806 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700807 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700808 }
809
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700810 private void showOptionsActivity() {
811 final Intent intent = new Intent(this, ContactOptionsActivity.class);
812 intent.setData(mUri);
813 startActivity(intent);
814 }
815
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700816 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
817 AdapterView.AdapterContextMenuInfo info;
818 try {
819 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
820 } catch (ClassCastException e) {
821 Log.e(TAG, "bad menuInfo", e);
822 return null;
823 }
824
825 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
826 }
827
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800828 @Override
829 public boolean onKeyDown(int keyCode, KeyEvent event) {
830 switch (keyCode) {
831 case KeyEvent.KEYCODE_CALL: {
832 try {
833 ITelephony phone = ITelephony.Stub.asInterface(
834 ServiceManager.checkService("phone"));
835 if (phone != null && !phone.isIdle()) {
836 // Skip out and let the key be handled at a higher level
837 break;
838 }
839 } catch (RemoteException re) {
840 // Fall through and try to call the contact
841 }
842
Evan Millar7911ff52009-07-21 15:55:18 -0700843 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800844 if (index != -1) {
845 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700846 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
847 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800848 }
849 } else if (mNumPhoneNumbers != 0) {
850 // There isn't anything selected, call the default number
851 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
852 startActivity(intent);
853 }
854 return true;
855 }
856
857 case KeyEvent.KEYCODE_DEL: {
858 showDialog(DIALOG_CONFIRM_DELETE);
859 return true;
860 }
861 }
862
863 return super.onKeyDown(keyCode, event);
864 }
865
Evan Millar7911ff52009-07-21 15:55:18 -0700866 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800867 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
868 if (entry != null) {
869 Intent intent = entry.intent;
870 if (intent != null) {
871 try {
872 startActivity(intent);
873 } catch (ActivityNotFoundException e) {
874 Log.e(TAG, "No activity found for intent: " + intent);
875 signalError();
876 }
877 } else {
878 signalError();
879 }
880 } else {
881 signalError();
882 }
883 }
884
885 /**
886 * Signal an error to the user via a beep, or some other method.
887 */
888 private void signalError() {
889 //TODO: implement this when we have the sonification APIs
890 }
891
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800892 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700893 // 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 -0800894 StringBuilder buf = new StringBuilder("imto://");
895 buf.append(host);
896 buf.append('/');
897 buf.append(data);
898 return Uri.parse(buf.toString());
899 }
900
901 /**
902 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700903 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800904 * @param personCursor the URI for the contact being displayed
905 */
Evan Millar11d628c2009-09-02 08:55:01 -0700906 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800907 // Clear out the old entries
908 final int numSections = mSections.size();
909 for (int i = 0; i < numSections; i++) {
910 mSections.get(i).clear();
911 }
912
Evan Millar7911ff52009-07-21 15:55:18 -0700913 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700914
Evan Millar11d628c2009-09-02 08:55:01 -0700915 Sources sources = Sources.getInstance(this);
916
Evan Millar66388be2009-05-28 15:41:07 -0700917 // Build up method entries
918 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700919 for (Entity entity: mEntities) {
920 final ContentValues entValues = entity.getEntityValues();
921 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Evan Millar7911ff52009-07-21 15:55:18 -0700922 // TODO: entry.contactId should be renamed to entry.rawContactId
Evan Millar11d628c2009-09-02 08:55:01 -0700923 long contactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700924
Evan Millar11d628c2009-09-02 08:55:01 -0700925 for (NamedContentValues subValue : entity.getSubValues()) {
926 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800927
Evan Millar11d628c2009-09-02 08:55:01 -0700928 ContentValues entryValues = subValue.values;
929 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
930 if (mimetype == null || accountType == null) {
Evan Millar66388be2009-05-28 15:41:07 -0700931 continue;
932 }
933
Evan Millar11d628c2009-09-02 08:55:01 -0700934 ContactsSource contactsSource = sources.getInflatedSource(accountType,
935 ContactsSource.LEVEL_MIMETYPES);
936 if (contactsSource == null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800937 continue;
938 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700939
Evan Millar11d628c2009-09-02 08:55:01 -0700940 DataKind kind = contactsSource.getKindForMimetype(mimetype);
941 if (kind == null) {
942 continue;
943 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800944
Evan Millar11d628c2009-09-02 08:55:01 -0700945 final long id = entryValues.getAsLong(Data._ID);
946 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
947 entry.contactId = contactId;
948 entry.id = id;
949 entry.uri = uri;
950 entry.mimetype = mimetype;
951 entry.label = buildActionString(kind, entryValues, true);
952 entry.data = buildDataString(kind, entryValues);
953 if (kind.typeColumn != null) {
954 entry.type = entryValues.getAsInteger(kind.typeColumn);
955 }
956 if (kind.iconRes > 0) {
957 entry.actionIcon = kind.iconRes;
958 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800959
Evan Millar11d628c2009-09-02 08:55:01 -0700960 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700961 if (TextUtils.isEmpty(entry.data)) {
962 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700963 continue;
964 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700965
Evan Millar11d628c2009-09-02 08:55:01 -0700966 if (!mRawContactIds.contains(entry.contactId)) {
967 mRawContactIds.add(entry.contactId);
968 }
969
970 // This performs the tab filtering
971 if (mSelectedRawContactId != null
972 && mSelectedRawContactId != entry.contactId
973 && mSelectedRawContactId != ALL_CONTACTS_ID) {
974 continue;
975 }
976
977 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)
978 || CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)
979 || CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)
980 || CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
981 final boolean isSuperPrimary = entryValues.getAsInteger(
982 Data.IS_SUPER_PRIMARY) != 0;
983
984 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
985 // Build phone entries
986 mNumPhoneNumbers++;
987
988 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
989 Uri.fromParts("tel", entry.data, null));
990 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
991 Uri.fromParts("sms", entry.data, null));
992 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700993
994 // If data is empty, don't show it.
995 if (TextUtils.isEmpty(entry.data)) {
996 Log.w(TAG, "empty data for contact method " + id);
997 continue;
998 }
999
Evan Millar11d628c2009-09-02 08:55:01 -07001000 entry.isPrimary = isSuperPrimary;
1001 mPhoneEntries.add(entry);
1002
1003 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
1004 || mShowSmsLinksForAllPhones) {
1005 // Add an SMS entry
1006 if (kind.iconAltRes > 0) {
1007 entry.secondaryActionIcon = kind.iconAltRes;
1008 }
1009 }
1010 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
1011 // Build email entries
1012 entry.intent = new Intent(Intent.ACTION_SENDTO,
1013 Uri.fromParts("mailto", entry.data, null));
Evan Millar49714ee2009-09-02 16:42:47 -07001014 // Temporary hack until we get real label resources for exchange.
1015 if (TextUtils.isEmpty(entry.label)) {
1016 entry.label = getString(R.string.email).toLowerCase();
1017 }
Evan Millar11d628c2009-09-02 08:55:01 -07001018 entry.isPrimary = isSuperPrimary;
1019 mEmailEntries.add(entry);
1020 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
1021 equals(mimetype)) {
1022 // Build postal entries
1023 entry.maxLines = 4;
1024 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
1025 mPostalEntries.add(entry);
1026 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
1027 // Build im entries
1028 Object protocolObj = entryValues.getAsInteger(Data.DATA5);
1029 String host = null;
1030
Evan Millar49714ee2009-09-02 16:42:47 -07001031 if (TextUtils.isEmpty(entry.label)) {
1032 entry.label = getString(R.string.im).toLowerCase();
1033 }
1034
Evan Millar11d628c2009-09-02 08:55:01 -07001035 if (protocolObj instanceof Number) {
1036 int protocol = ((Number) protocolObj).intValue();
1037 host = ContactsUtils.lookupProviderNameFromId(
1038 protocol).toLowerCase();
1039 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
1040 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
1041 entry.maxLabelLines = 2;
1042 }
1043 } else if (protocolObj != null) {
1044 String providerName = (String) protocolObj;
1045 host = providerName.toLowerCase();
1046 }
1047
1048 // Only add the intent if there is a valid host
1049 if (!TextUtils.isEmpty(host)) {
1050 entry.intent = new Intent(Intent.ACTION_SENDTO,
1051 constructImToUrl(host, entry.data));
1052 }
1053 //TODO(emillar) Add in presence info
1054 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
1055 entry.presenceIcon = Presence.getPresenceIconResourceId(
1056 aggCursor.getInt(METHODS_STATUS_COLUMN));
1057 entry.status = ...
1058 }*/
1059 mImEntries.add(entry);
1060 }
1061 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
1062 // Build organization entries
1063 mOrganizationEntries.add(entry);
1064 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
1065 // Build note entries
1066 entry.id = 0;
1067 entry.uri = null;
1068 entry.intent = null;
1069 entry.maxLines = 10;
1070 mOtherEntries.add(entry);
1071 }
1072
1073
1074 // TODO(emillar) Add group entries
1075 // // Build the group entries
1076 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1077 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1078 // null, null, Groups.DEFAULT_SORT_ORDER);
1079 // if (groupCursor != null) {
1080 // try {
1081 // StringBuilder sb = new StringBuilder();
1082 //
1083 // while (groupCursor.moveToNext()) {
1084 // String systemId = groupCursor.getString(
1085 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1086 //
1087 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1088 // continue;
1089 // }
1090 //
1091 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1092 // if (!TextUtils.isEmpty(name)) {
1093 // if (sb.length() == 0) {
1094 // sb.append(name);
1095 // } else {
1096 // sb.append(getString(R.string.group_list, name));
1097 // }
1098 // }
1099 // }
1100 //
1101 // if (sb.length() > 0) {
1102 // ViewEntry entry = new ViewEntry();
1103 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1104 // entry.label = getString(R.string.label_groups);
1105 // entry.data = sb.toString();
1106 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1107 //
1108 // // TODO: Add an icon for the groups item.
1109 //
1110 // mGroupEntries.add(entry);
1111 // }
1112 // } finally {
1113 // groupCursor.close();
1114 // }
1115 // }
Evan Millar66388be2009-05-28 15:41:07 -07001116 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001117
Evan Millar66388be2009-05-28 15:41:07 -07001118 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001119 }
1120 }
1121
Evan Millar11d628c2009-09-02 08:55:01 -07001122 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1123 if (kind.actionHeader == null) {
1124 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001125 }
Evan Millar11d628c2009-09-02 08:55:01 -07001126 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1127 if (actionHeader == null) {
1128 return null;
1129 }
1130 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1131 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001132
Evan Millar11d628c2009-09-02 08:55:01 -07001133 String buildDataString(DataKind kind, ContentValues values) {
1134 if (kind.actionBody == null) {
1135 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001136 }
Evan Millar11d628c2009-09-02 08:55:01 -07001137 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1138 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001139 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001140
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001141 /**
1142 * A basic structure with the data for a contact entry in the list.
1143 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001144 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001145 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001146 public boolean isPrimary = false;
1147 public int presenceIcon = -1;
1148 public int secondaryActionIcon = -1;
1149 public Intent intent;
1150 public Intent secondaryIntent = null;
1151 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001152 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001153 public ArrayList<Long> ids = new ArrayList<Long>();
1154 public int collapseCount = 0;
1155
1156 public boolean collapseWith(ViewEntry entry) {
1157 // assert equal collapse keys
1158 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1159 return false;
1160 }
1161
1162 // Choose the label associated with the highest type precedence.
1163 if (TypePrecedence.getTypePrecedence(mimetype, type)
1164 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1165 type = entry.type;
1166 label = entry.label;
1167 }
1168
1169 // Choose the max of the maxLines and maxLabelLines values.
1170 maxLines = Math.max(maxLines, entry.maxLines);
1171 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1172
1173 // Choose the presence with the highest precedence.
1174 if (Presence.getPresencePrecedence(status)
1175 < Presence.getPresencePrecedence(entry.status)) {
1176 status = entry.status;
1177 }
1178
1179 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001180 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001181
1182 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1183
1184 // Keep track of all the ids that have been collapsed with this one.
1185 ids.add(entry.id);
1186 collapseCount++;
1187 return true;
1188 }
1189
1190 public String getCollapseKey() {
1191 StringBuilder hashSb = new StringBuilder();
1192 hashSb.append(data);
1193 hashSb.append(mimetype);
1194 hashSb.append((intent != null && intent.getAction() != null)
1195 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001196 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1197 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001198 hashSb.append(actionIcon);
1199 return hashSb.toString();
1200 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001201 }
1202
Evan Millar15e514d2009-08-04 10:14:57 -07001203 /** Cache of the children views of a row */
1204 static class ViewCache {
1205 public TextView label;
1206 public TextView data;
1207 public ImageView actionIcon;
1208 public ImageView presenceIcon;
1209 public ImageView primaryIcon;
1210 public ImageView secondaryActionButton;
1211 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001212
Evan Millar15e514d2009-08-04 10:14:57 -07001213 // Need to keep track of this too
1214 ViewEntry entry;
1215 }
1216
1217 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1218 implements View.OnClickListener {
1219
Evan Millar5c22c3b2009-05-29 11:37:54 -07001220
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001221 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1222 super(context, sections, SHOW_SEPARATORS);
1223 }
1224
Evan Millar15e514d2009-08-04 10:14:57 -07001225 public void onClick(View v) {
1226 Intent intent = (Intent) v.getTag();
1227 startActivity(intent);
1228 }
1229
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001230 @Override
1231 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001232 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001233 View v;
1234
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001235 ViewCache views;
1236
1237 // Check to see if we can reuse convertView
1238 if (convertView != null) {
1239 v = convertView;
1240 views = (ViewCache) v.getTag();
1241 } else {
1242 // Create a new view if needed
1243 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1244
1245 // Cache the children
1246 views = new ViewCache();
1247 views.label = (TextView) v.findViewById(android.R.id.text1);
1248 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001249 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1250 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1251 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1252 views.secondaryActionButton = (ImageView) v.findViewById(
1253 R.id.secondary_action_button);
1254 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001255 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001256 v.setTag(views);
1257 }
1258
1259 // Update the entry in the view cache
1260 views.entry = entry;
1261
1262 // Bind the data to the view
1263 bindView(v, entry);
1264 return v;
1265 }
1266
1267 @Override
1268 protected View newView(int position, ViewGroup parent) {
1269 // getView() handles this
1270 throw new UnsupportedOperationException();
1271 }
1272
1273 @Override
1274 protected void bindView(View view, ViewEntry entry) {
1275 final Resources resources = mContext.getResources();
1276 ViewCache views = (ViewCache) view.getTag();
1277
1278 // Set the label
1279 TextView label = views.label;
1280 setMaxLines(label, entry.maxLabelLines);
1281 label.setText(entry.label);
1282
1283 // Set the data
1284 TextView data = views.data;
1285 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001286 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1287 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1288 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1289 } else {
1290 data.setText(entry.data);
1291 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001292 setMaxLines(data, entry.maxLines);
1293 }
1294
Evan Millar15e514d2009-08-04 10:14:57 -07001295 // Set the primary icon
1296 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1297
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001298 // Set the action icon
1299 ImageView action = views.actionIcon;
1300 if (entry.actionIcon != -1) {
1301 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1302 action.setVisibility(View.VISIBLE);
1303 } else {
1304 // Things should still line up as if there was an icon, so make it invisible
1305 action.setVisibility(View.INVISIBLE);
1306 }
1307
1308 // Set the presence icon
1309 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001310 if (entry.presenceIcon != -1) {
1311 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001312 } else if (entry.status != -1) {
1313 presenceIcon = resources.getDrawable(
1314 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001315 }
Evan Millar15e514d2009-08-04 10:14:57 -07001316 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001317 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001318 presenceIconView.setImageDrawable(presenceIcon);
1319 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001320 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001321 presenceIconView.setVisibility(View.GONE);
1322 }
1323
1324 // Set the secondary action button
1325 ImageView secondaryActionView = views.secondaryActionButton;
1326 Drawable secondaryActionIcon = null;
1327 if (entry.secondaryActionIcon != -1) {
1328 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1329 }
1330 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1331 secondaryActionView.setImageDrawable(secondaryActionIcon);
1332 secondaryActionView.setTag(entry.secondaryIntent);
1333 secondaryActionView.setVisibility(View.VISIBLE);
1334 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1335 } else {
1336 secondaryActionView.setVisibility(View.GONE);
1337 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001338 }
1339 }
1340
1341 private void setMaxLines(TextView textView, int maxLines) {
1342 if (maxLines == 1) {
1343 textView.setSingleLine(true);
1344 textView.setEllipsize(TextUtils.TruncateAt.END);
1345 } else {
1346 textView.setSingleLine(false);
1347 textView.setMaxLines(maxLines);
1348 textView.setEllipsize(null);
1349 }
1350 }
1351 }
1352}