blob: d2328eeef055356fa57d3ed09ec8520e155f37f6 [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;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070023import com.android.contacts.model.EntityModifier;
Evan Millar11d628c2009-09-02 08:55:01 -070024import com.android.contacts.model.Sources;
25import com.android.contacts.model.ContactsSource.DataKind;
Jeff Sharkey802b2052009-08-04 14:21:06 -070026import com.android.contacts.ui.FastTrackWindow;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070027import com.android.contacts.util.Constants;
Evan Millar11d628c2009-09-02 08:55:01 -070028import com.android.contacts.util.NotifyingAsyncQueryHandler;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080029import com.android.internal.telephony.ITelephony;
Evan Millar11d628c2009-09-02 08:55:01 -070030import com.android.internal.widget.ContactHeaderWidget;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080031
Evan Millar11d628c2009-09-02 08:55:01 -070032import android.app.Activity;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080033import android.app.AlertDialog;
34import android.app.Dialog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080035import android.content.ActivityNotFoundException;
36import android.content.ContentResolver;
37import android.content.ContentUris;
38import android.content.ContentValues;
39import android.content.Context;
40import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070041import android.content.Entity;
Evan Millar11d628c2009-09-02 08:55:01 -070042import android.content.EntityIterator;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080043import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070044import android.content.DialogInterface.OnClickListener;
Evan Millar11d628c2009-09-02 08:55:01 -070045import android.content.Entity.NamedContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080046import android.content.res.Resources;
47import android.database.ContentObserver;
48import android.database.Cursor;
49import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080050import android.net.Uri;
51import android.os.Bundle;
52import android.os.Handler;
53import android.os.RemoteException;
54import android.os.ServiceManager;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070055import android.provider.ContactsContract.AggregationExceptions;
56import android.provider.ContactsContract.CommonDataKinds;
Evan Millar11d628c2009-09-02 08:55:01 -070057import android.provider.ContactsContract.Contacts;
Evan Millar66388be2009-05-28 15:41:07 -070058import android.provider.ContactsContract.Data;
Evan Millar54a5c9f2009-06-23 17:41:09 -070059import android.provider.ContactsContract.Presence;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070060import android.provider.ContactsContract.RawContacts;
Evan Millar54a5c9f2009-06-23 17:41:09 -070061import android.provider.ContactsContract.CommonDataKinds.Phone;
62import android.telephony.PhoneNumberUtils;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070063import android.telephony.TelephonyManager;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080064import android.text.TextUtils;
65import android.util.Log;
Evan Millar11d628c2009-09-02 08:55:01 -070066import android.util.SparseArray;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080067import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070068import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080069import android.view.KeyEvent;
Evan Millar11d628c2009-09-02 08:55:01 -070070import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080071import android.view.Menu;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070072import android.view.MenuInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080073import android.view.MenuItem;
74import android.view.View;
75import android.view.ViewGroup;
Evan Millar11d628c2009-09-02 08:55:01 -070076import android.view.Window;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080077import android.view.ContextMenu.ContextMenuInfo;
78import android.widget.AdapterView;
Evan Millar7911ff52009-07-21 15:55:18 -070079import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080080import android.widget.ImageView;
81import android.widget.ListView;
82import android.widget.TextView;
83import android.widget.Toast;
84
85import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080086
87/**
88 * Displays the details of a specific contact.
89 */
Evan Millar11d628c2009-09-02 08:55:01 -070090public class ViewContactActivity extends Activity
Evan Millar7911ff52009-07-21 15:55:18 -070091 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
Evan Millar11d628c2009-09-02 08:55:01 -070092 AdapterView.OnItemClickListener, NotifyingAsyncQueryHandler.AsyncQueryListener,
93 OnTabSelectionChangedListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080094 private static final String TAG = "ViewContact";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080095
Evan Millar8a79cee2009-08-19 17:20:49 -070096 public static final String RAW_CONTACT_ID_EXTRA = "rawContactIdExtra";
97
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080098 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080099
100 private static final int DIALOG_CONFIRM_DELETE = 1;
101
Evan Millar8a79cee2009-08-19 17:20:49 -0700102 private static final int REQUEST_JOIN_CONTACT = 1;
103 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700104
Evan Millar8a79cee2009-08-19 17:20:49 -0700105 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800106
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700107 protected Uri mLookupUri;
Evan Millar11d628c2009-09-02 08:55:01 -0700108 private Uri mUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800109 private ContentResolver mResolver;
110 private ViewAdapter mAdapter;
111 private int mNumPhoneNumbers = 0;
112
Evan Millar8a79cee2009-08-19 17:20:49 -0700113 private static final long ALL_CONTACTS_ID = -100;
Evan Millar7911ff52009-07-21 15:55:18 -0700114
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700115 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700116 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700117 */
Evan Millar7911ff52009-07-21 15:55:18 -0700118 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700119
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800120 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
121 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
122 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
123 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
124 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
125 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700126 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800127 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
128 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
129
130 private Cursor mCursor;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700131
Evan Millar11d628c2009-09-02 08:55:01 -0700132 private SparseArray<Long> mTabRawContactIdMap;
133 protected ScrollingTabWidget mTabWidget;
134 protected ContactHeaderWidget mContactHeaderWidget;
135 private NotifyingAsyncQueryHandler mHandler;
136
137 protected LayoutInflater mInflater;
138
139 //Projection used for the query that determines which tabs to add.
140 protected static final String[] TAB_PROJECTION = new String[] {
141 RawContacts._ID,
142 RawContacts.ACCOUNT_NAME,
143 RawContacts.ACCOUNT_TYPE
144 };
145 protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0;
146 protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1;
147 protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2;
148
149 protected static final String SELECTED_RAW_CONTACT_ID_KEY = "selectedRawContact";
150 protected Long mSelectedRawContactId = null;
151
152 private static final int TOKEN_QUERY = 0;
153
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800154 private ContentObserver mObserver = new ContentObserver(new Handler()) {
155 @Override
156 public boolean deliverSelfNotifications() {
157 return true;
158 }
159
160 @Override
161 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700162 if (mCursor != null && !mCursor.isClosed()) {
163 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800164 }
165 }
166 };
167
168 public void onClick(DialogInterface dialog, int which) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700169 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800170 getContentResolver().delete(mUri, null, null);
171 finish();
172 }
173
Evan Millar7911ff52009-07-21 15:55:18 -0700174 private FrameLayout mTabContentLayout;
175 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800176 private boolean mShowSmsLinksForAllPhones;
Evan Millar11d628c2009-09-02 08:55:01 -0700177 private ArrayList<Entity> mEntities = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800178
179 @Override
180 protected void onCreate(Bundle icicle) {
181 super.onCreate(icicle);
182
Evan Millar11d628c2009-09-02 08:55:01 -0700183 final Intent intent = getIntent();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700184 mLookupUri = intent.getData();
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700185
186 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700187
188 requestWindowFeature(Window.FEATURE_NO_TITLE);
189 setContentView(R.layout.contact_card_layout);
190
191 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
192 mContactHeaderWidget.showStar(true);
Evan Millar11d628c2009-09-02 08:55:01 -0700193 mContactHeaderWidget.setExcludeMimes(new String[] {
194 Contacts.CONTENT_ITEM_TYPE
195 });
196
197 mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget);
198 mTabWidget.setTabSelectionListener(this);
199 mTabWidget.setVisibility(View.INVISIBLE);
200
201 mTabRawContactIdMap = new SparseArray<Long>();
202
203 mHandler = new NotifyingAsyncQueryHandler(this, this);
204
Evan Millar7911ff52009-07-21 15:55:18 -0700205 mListView = new ListView(this);
206 mListView.setOnCreateContextMenuListener(this);
207 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
208 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800209
Evan Millar7911ff52009-07-21 15:55:18 -0700210 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
211 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800212
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800213 mResolver = getContentResolver();
214
215 // Build the list of sections. The order they're added to mSections dictates the
216 // order they are displayed in the list.
217 mSections.add(mPhoneEntries);
218 mSections.add(mSmsEntries);
219 mSections.add(mEmailEntries);
220 mSections.add(mImEntries);
221 mSections.add(mPostalEntries);
222 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700223 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800224 mSections.add(mOtherEntries);
225
226 //TODO Read this value from a preference
227 mShowSmsLinksForAllPhones = true;
228
Evan Millar11d628c2009-09-02 08:55:01 -0700229 startEntityQuery();
230 }
231
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800232 @Override
233 protected void onResume() {
234 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700235 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800236 }
237
238 @Override
239 protected void onPause() {
240 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700241 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800242 }
243
244 @Override
245 protected void onDestroy() {
246 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700247 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800248 }
249
250 @Override
Evan Millar11d628c2009-09-02 08:55:01 -0700251 protected void onRestoreInstanceState(Bundle savedInstanceState) {
252 super.onRestoreInstanceState(savedInstanceState);
253 mSelectedRawContactId = savedInstanceState.getLong(SELECTED_RAW_CONTACT_ID_KEY);
254 }
255
256 @Override
257 protected void onSaveInstanceState(Bundle outState) {
258 super.onSaveInstanceState(outState);
259 outState.putLong(SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
260 }
261
262 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800263 protected Dialog onCreateDialog(int id) {
264 switch (id) {
265 case DIALOG_CONFIRM_DELETE:
266 return new AlertDialog.Builder(this)
267 .setTitle(R.string.deleteConfirmation_title)
268 .setIcon(android.R.drawable.ic_dialog_alert)
269 .setMessage(R.string.deleteConfirmation)
270 .setNegativeButton(android.R.string.cancel, null)
271 .setPositiveButton(android.R.string.ok, this)
272 .setCancelable(false)
273 .create();
274 }
275 return null;
276 }
277
Evan Millar11d628c2009-09-02 08:55:01 -0700278
279 // TAB CODE //
280 /**
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700281 * Adds a tab for each {@link RawContacts} associated with this contact.
Evan Millar11d628c2009-09-02 08:55:01 -0700282 * Override this method if you want to additional tabs and/or different
283 * tabs for your activity.
Evan Millar11d628c2009-09-02 08:55:01 -0700284 */
285 protected void bindTabs() {
286 if (mEntities.size() > 1) {
Evan Millar7911ff52009-07-21 15:55:18 -0700287 addAllTab();
288 }
Evan Millar11d628c2009-09-02 08:55:01 -0700289
290 final Sources sources = Sources.getInstance(this);
291
292 for (Entity entity : mEntities) {
293 final String accountType = entity.getEntityValues().
294 getAsString(RawContacts.ACCOUNT_TYPE);
295 final Long rawContactId = entity.getEntityValues().
296 getAsLong(RawContacts._ID);
297
298 // TODO: ensure inflation on background task so we don't block UI thread here
299 final ContactsSource source = sources.getInflatedSource(accountType,
300 ContactsSource.LEVEL_SUMMARY);
301 addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(), source));
302 }
303
304 selectInitialTab();
305 mTabWidget.setVisibility(View.VISIBLE);
306 mTabWidget.postInvalidate();
307 }
308
309 /**
310 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
311 *
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700312 * @param rawContactId The contact id associated with the tab.
Evan Millar11d628c2009-09-02 08:55:01 -0700313 * @param view A view to use as the tab indicator.
314 */
315 protected void addTab(long rawContactId, View view) {
316 mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId);
317 mTabWidget.addTab(view);
318 }
319
320
321 protected void clearCurrentTabs() {
322 mTabRawContactIdMap.clear();
323 mTabWidget.removeAllTabs();
324 }
325
326 protected void selectInitialTab() {
327 int selectedTabIndex = 0;
328
329 if (mSelectedRawContactId != null) {
330 selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
331 if (selectedTabIndex == -1) {
332 // If there was no matching tab, just select the first;
333 selectedTabIndex = 0;
334 }
335 }
336
337 mTabWidget.setCurrentTab(selectedTabIndex);
338 onTabSelectionChanged(selectedTabIndex, false);
339 }
340
Evan Millar7911ff52009-07-21 15:55:18 -0700341 private void addAllTab() {
Evan Millar56d2caa2009-08-20 20:30:12 -0700342 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator,
343 mTabWidget.getTabParent(), false);
Evan Millar74660912009-08-19 17:36:33 -0700344 allTabIndicator.getBackground().setDither(true);
Evan Millar7911ff52009-07-21 15:55:18 -0700345 addTab(ALL_CONTACTS_ID, allTabIndicator);
346 }
347
348 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
349 long rawContactId = getTabRawContactId(tabIndex);
Evan Millar8a79cee2009-08-19 17:20:49 -0700350 mSelectedRawContactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700351 bindData();
Evan Millar7911ff52009-07-21 15:55:18 -0700352 }
353
Evan Millar11d628c2009-09-02 08:55:01 -0700354 /**
355 * Return the RawContact id associated with the tab at an index.
356 *
357 * @param index The index of the tab in question.
358 * @return The contactId associated with the tab at the specified index.
359 */
360 protected long getTabRawContactId(int index) {
361 return mTabRawContactIdMap.get(index);
362 }
Evan Millar2c1cc832009-07-13 11:08:06 -0700363
Evan Millar11d628c2009-09-02 08:55:01 -0700364 /**
365 * Return the tab index associated with the RawContact id.
366 *
367 * @param index The index of the tab in question.
368 * @return The contactId associated with the tab at the specified index.
369 */
370 protected int getTabIndexForRawContactId(long rawContactId) {
371 int numTabs = mTabRawContactIdMap.size();
372 for (int i=0; i < numTabs; i++) {
373 if (mTabRawContactIdMap.get(i) == rawContactId) {
374 return i;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800375 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800376 }
Evan Millar11d628c2009-09-02 08:55:01 -0700377 return -1;
378 }
379
380
381 // QUERY CODE //
382 /** {@inheritDoc} */
383 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
384 try{
385 if (token == TOKEN_QUERY) {
386 clearCurrentTabs();
387 mEntities = readEntities(iterator);
388 bindTabs();
389 bindData();
390 }
391 } finally {
392 if (iterator != null) {
393 iterator.close();
394 }
395 }
396 }
397
398 /** {@inheritDoc} */
399 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700400 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700401 }
402
403 private ArrayList<Entity> readEntities(EntityIterator iterator) {
404 ArrayList<Entity> entities = new ArrayList<Entity>();
405 try {
406 while (iterator.hasNext()) {
407 entities.add(iterator.next());
408 }
409 } catch (RemoteException e) {
410 }
411
412 return entities;
413 }
414
415 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700416 closeCursor();
417
Dmitri Plotnikov0306bbd2009-09-03 14:37:15 -0700418 mUri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700419 if (mLookupUri != null) {
420 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
421 if (mLookupUri != null) {
422 mUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
423 }
424 }
425
426 if (mUri == null) {
427
428 // TODO either figure out a way to prevent a flash of black background or
429 // use some other UI than a toast
430 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
431 Log.e(TAG, "invalid contact uri: " + mLookupUri);
432 finish();
433 return;
434 }
435
436 mCursor = mResolver.query(Uri.withAppendedPath(mUri, Contacts.Data.CONTENT_DIRECTORY),
437 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
438 mCursor.registerContentObserver(mObserver);
439
Evan Millar11d628c2009-09-02 08:55:01 -0700440 long contactId = ContentUris.parseId(mUri);
441 mHandler.startQueryEntities(TOKEN_QUERY, null,
442 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700443
444 mContactHeaderWidget.bindFromContactId(ContentUris.parseId(mUri));
445 }
446
447 private void closeCursor() {
448 if (mCursor != null) {
449 mCursor.unregisterContentObserver(mObserver);
450 mCursor.close();
451 mCursor = null;
452 }
Evan Millar11d628c2009-09-02 08:55:01 -0700453 }
454
455 private void bindData() {
456
457 // Build up the contact entries
458 buildEntries();
459
460 // Collapse similar data items in select sections.
461 Collapser.collapseList(mPhoneEntries);
462 Collapser.collapseList(mSmsEntries);
463 Collapser.collapseList(mEmailEntries);
464 Collapser.collapseList(mPostalEntries);
465
466 if (mAdapter == null) {
467 mAdapter = new ViewAdapter(this, mSections);
468 mListView.setAdapter(mAdapter);
469 } else {
470 mAdapter.setSections(mSections, SHOW_SEPARATORS);
471 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800472 }
473
474 @Override
475 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700476 super.onCreateOptionsMenu(menu);
477
478 final MenuInflater inflater = getMenuInflater();
479 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800480 return true;
481 }
482
483 @Override
484 public boolean onPrepareOptionsMenu(Menu menu) {
485 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700486
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700487 // Only allow edit if we have a selected tab
488 final boolean contactSelected = (mSelectedRawContactId != null);
489 menu.findItem(R.id.menu_edit).setEnabled(contactSelected);
Evan Millardb5d88c2009-08-28 09:31:57 -0700490
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700491 // Only allow split when more than one contact
492 final boolean isAggregate = (mRawContactIds.size() > 1);
493 menu.findItem(R.id.menu_split).setEnabled(isAggregate);
494
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800495 return true;
496 }
497
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800498 @Override
499 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
500 AdapterView.AdapterContextMenuInfo info;
501 try {
502 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
503 } catch (ClassCastException e) {
504 Log.e(TAG, "bad menuInfo", e);
505 return;
506 }
507
508 // This can be null sometimes, don't crash...
509 if (info == null) {
510 Log.e(TAG, "bad menuInfo");
511 return;
512 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700513
Evan Millar45e0ed32009-06-01 16:44:38 -0700514 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
515 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
516 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700517 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
518 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700519 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800520 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700521 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
522 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700523 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700524 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800525 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700526 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700527 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
528 }
529 // TODO(emillar): add back with group support.
530 /* else if (entry.mimetype.equals()) {
531 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
532 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800533 }
534
535 @Override
536 public boolean onOptionsItemSelected(MenuItem item) {
537 switch (item.getItemId()) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700538 case R.id.menu_edit: {
Evan Millardb5d88c2009-08-28 09:31:57 -0700539 Long rawContactIdToEdit = mSelectedRawContactId;
540 if (rawContactIdToEdit == null) {
541 // This shouldn't be possible. We only show the edit option if
542 // this value is non-null.
543 break;
544 }
Evan Millar8a79cee2009-08-19 17:20:49 -0700545 if (rawContactIdToEdit == ALL_CONTACTS_ID) {
546 // If the "all" tab is selected, edit the next tab.
Evan Millardb5d88c2009-08-28 09:31:57 -0700547 rawContactIdToEdit = getTabRawContactId(mTabWidget.getCurrentTab() + 1);
Evan Millar8a79cee2009-08-19 17:20:49 -0700548 }
549 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
550 rawContactIdToEdit);
551 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
552 REQUEST_EDIT_CONTACT);
553 break;
554 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700555 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800556 // Get confirmation
557 showDialog(DIALOG_CONFIRM_DELETE);
558 return true;
559 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700560 case R.id.menu_split: {
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700561 if (mRawContactIds.size() == 2) {
562 splitContact(mRawContactIds.get(1));
563 } else {
564 showSplitAggregateDialog();
565 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700566 return true;
567 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700568 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700569 showJoinAggregateActivity();
570 return true;
571 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700572 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700573 showOptionsActivity();
574 return true;
575 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700576 case R.id.menu_share: {
577 final Intent intent = new Intent(Intent.ACTION_SEND);
578 intent.setType(Contacts.CONTENT_ITEM_TYPE);
579 intent.putExtra(Intent.EXTRA_STREAM, mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700580
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700581 // Launch chooser to share contact via
582 final CharSequence chooseTitle = getText(R.string.share_via);
583 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700584
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700585 try {
586 startActivity(chooseIntent);
587 } catch (ActivityNotFoundException ex) {
588 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800589 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700590 return true;
591 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800592 }
593 return super.onOptionsItemSelected(item);
594 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700595
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800596 @Override
597 public boolean onContextItemSelected(MenuItem item) {
598 switch (item.getItemId()) {
599 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700600 if (makeItemDefault(item)) {
601 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800602 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700603 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800604 }
605 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700606
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800607 return super.onContextItemSelected(item);
608 }
609
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700610 private boolean makeItemDefault(MenuItem item) {
611 ViewEntry entry = getViewEntryForMenuItem(item);
612 if (entry == null) {
613 return false;
614 }
615
616 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700617 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700618 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700619 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
620 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700621 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700622 return true;
623 }
624
625 /**
626 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
627 * The user picks a contact to be split into its own aggregate or clicks Cancel.
628 */
629 private void showSplitAggregateDialog() {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700630 // Wrap this dialog in a specific theme so that list items have correct text color.
631 final ContextThemeWrapper dialogContext =
632 new ContextThemeWrapper(this, android.R.style.Theme_Light);
633 AlertDialog.Builder builder =
634 new AlertDialog.Builder(dialogContext);
635 builder.setTitle(getString(R.string.splitAggregate_title));
636
637 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
638 builder.setView(view);
639
640 builder.setInverseBackgroundForced(true);
641 builder.setCancelable(true);
642 builder.setNegativeButton(android.R.string.cancel,
643 new OnClickListener() {
644 public void onClick(DialogInterface dialog, int which) {
645 dialog.dismiss();
646 }
647 });
648 final AlertDialog dialog = builder.create();
649
650 view.setOnContactSelectedListener(new OnContactSelectedListener() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700651 public void onContactSelected(long rawContactId) {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700652 dialog.dismiss();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700653 splitContact(rawContactId);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700654 }
655 });
656
657 dialog.show();
658 }
659
660 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700661 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700662 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700663 public void showJoinAggregateActivity() {
664 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
665 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700666 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700667 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700668
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700669 @Override
670 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700671 switch (requestCode) {
672 case REQUEST_JOIN_CONTACT: {
673 if (resultCode == RESULT_OK && intent != null) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700674 final long contactId = ContentUris.parseId(intent.getData());
675 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700676 }
677 break;
678 }
679 case REQUEST_EDIT_CONTACT: {
680 if (resultCode == RESULT_OK && intent != null) {
681 long newInitialSelectedRawContactId = intent.getLongExtra(
682 RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
683 if (newInitialSelectedRawContactId != mSelectedRawContactId) {
684 mSelectedRawContactId = newInitialSelectedRawContactId;
685 selectInitialTab();
686 }
687 }
688 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700689 }
690 }
691
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700692 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700693 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700694
695 // The split operation may have removed the original aggregate contact, so we need
696 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700697 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700698 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700699 }
700
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700701 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700702 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700703 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700704
705 try {
706 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700707 long rawContactId = c.getLong(0);
708 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700709 }
710 } finally {
711 c.close();
712 }
713
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700714 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
715 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700716 }
717
718 /**
719 * Given a contact ID sets an aggregation exception to either join the contact with the
720 * current aggregate or split off.
721 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700722 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700723 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700724 for (long aRawContactId : mRawContactIds) {
725 if (aRawContactId != rawContactId) {
726 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
727 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
728 values.put(AggregationExceptions.TYPE, exceptionType);
729 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
730 }
731 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700732 }
733
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700734 private void showOptionsActivity() {
735 final Intent intent = new Intent(this, ContactOptionsActivity.class);
736 intent.setData(mUri);
737 startActivity(intent);
738 }
739
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700740 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
741 AdapterView.AdapterContextMenuInfo info;
742 try {
743 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
744 } catch (ClassCastException e) {
745 Log.e(TAG, "bad menuInfo", e);
746 return null;
747 }
748
749 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
750 }
751
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800752 @Override
753 public boolean onKeyDown(int keyCode, KeyEvent event) {
754 switch (keyCode) {
755 case KeyEvent.KEYCODE_CALL: {
756 try {
757 ITelephony phone = ITelephony.Stub.asInterface(
758 ServiceManager.checkService("phone"));
759 if (phone != null && !phone.isIdle()) {
760 // Skip out and let the key be handled at a higher level
761 break;
762 }
763 } catch (RemoteException re) {
764 // Fall through and try to call the contact
765 }
766
Evan Millar7911ff52009-07-21 15:55:18 -0700767 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800768 if (index != -1) {
769 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700770 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
771 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800772 }
773 } else if (mNumPhoneNumbers != 0) {
774 // There isn't anything selected, call the default number
775 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
776 startActivity(intent);
777 }
778 return true;
779 }
780
781 case KeyEvent.KEYCODE_DEL: {
782 showDialog(DIALOG_CONFIRM_DELETE);
783 return true;
784 }
785 }
786
787 return super.onKeyDown(keyCode, event);
788 }
789
Evan Millar7911ff52009-07-21 15:55:18 -0700790 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800791 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
792 if (entry != null) {
793 Intent intent = entry.intent;
794 if (intent != null) {
795 try {
796 startActivity(intent);
797 } catch (ActivityNotFoundException e) {
798 Log.e(TAG, "No activity found for intent: " + intent);
799 signalError();
800 }
801 } else {
802 signalError();
803 }
804 } else {
805 signalError();
806 }
807 }
808
809 /**
810 * Signal an error to the user via a beep, or some other method.
811 */
812 private void signalError() {
813 //TODO: implement this when we have the sonification APIs
814 }
815
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800816 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700817 // 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 -0800818 StringBuilder buf = new StringBuilder("imto://");
819 buf.append(host);
820 buf.append('/');
821 buf.append(data);
822 return Uri.parse(buf.toString());
823 }
824
825 /**
826 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700827 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800828 * @param personCursor the URI for the contact being displayed
829 */
Evan Millar11d628c2009-09-02 08:55:01 -0700830 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800831 // Clear out the old entries
832 final int numSections = mSections.size();
833 for (int i = 0; i < numSections; i++) {
834 mSections.get(i).clear();
835 }
836
Evan Millar7911ff52009-07-21 15:55:18 -0700837 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700838
Evan Millar11d628c2009-09-02 08:55:01 -0700839 Sources sources = Sources.getInstance(this);
840
Evan Millar66388be2009-05-28 15:41:07 -0700841 // Build up method entries
842 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700843 for (Entity entity: mEntities) {
844 final ContentValues entValues = entity.getEntityValues();
845 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Evan Millar7911ff52009-07-21 15:55:18 -0700846 // TODO: entry.contactId should be renamed to entry.rawContactId
Evan Millar11d628c2009-09-02 08:55:01 -0700847 long contactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700848
Evan Millar11d628c2009-09-02 08:55:01 -0700849 for (NamedContentValues subValue : entity.getSubValues()) {
850 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800851
Evan Millar11d628c2009-09-02 08:55:01 -0700852 ContentValues entryValues = subValue.values;
853 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
854 if (mimetype == null || accountType == null) {
Evan Millar66388be2009-05-28 15:41:07 -0700855 continue;
856 }
857
Evan Millar11d628c2009-09-02 08:55:01 -0700858 ContactsSource contactsSource = sources.getInflatedSource(accountType,
859 ContactsSource.LEVEL_MIMETYPES);
860 if (contactsSource == null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800861 continue;
862 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700863
Evan Millar11d628c2009-09-02 08:55:01 -0700864 DataKind kind = contactsSource.getKindForMimetype(mimetype);
865 if (kind == null) {
866 continue;
867 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800868
Evan Millar11d628c2009-09-02 08:55:01 -0700869 final long id = entryValues.getAsLong(Data._ID);
870 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
871 entry.contactId = contactId;
872 entry.id = id;
873 entry.uri = uri;
874 entry.mimetype = mimetype;
875 entry.label = buildActionString(kind, entryValues, true);
876 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700877 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700878 entry.type = entryValues.getAsInteger(kind.typeColumn);
879 }
880 if (kind.iconRes > 0) {
881 entry.actionIcon = kind.iconRes;
882 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800883
Evan Millar11d628c2009-09-02 08:55:01 -0700884 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700885 if (TextUtils.isEmpty(entry.data)) {
886 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700887 continue;
888 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700889
Evan Millar11d628c2009-09-02 08:55:01 -0700890 if (!mRawContactIds.contains(entry.contactId)) {
891 mRawContactIds.add(entry.contactId);
892 }
893
894 // This performs the tab filtering
895 if (mSelectedRawContactId != null
896 && mSelectedRawContactId != entry.contactId
897 && mSelectedRawContactId != ALL_CONTACTS_ID) {
898 continue;
899 }
900
901 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)
902 || CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)
903 || CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)
904 || CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
905 final boolean isSuperPrimary = entryValues.getAsInteger(
906 Data.IS_SUPER_PRIMARY) != 0;
907
908 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
909 // Build phone entries
910 mNumPhoneNumbers++;
911
912 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
913 Uri.fromParts("tel", entry.data, null));
914 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
915 Uri.fromParts("sms", entry.data, null));
916 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700917
918 // If data is empty, don't show it.
919 if (TextUtils.isEmpty(entry.data)) {
920 Log.w(TAG, "empty data for contact method " + id);
921 continue;
922 }
923
Evan Millar11d628c2009-09-02 08:55:01 -0700924 entry.isPrimary = isSuperPrimary;
925 mPhoneEntries.add(entry);
926
927 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
928 || mShowSmsLinksForAllPhones) {
929 // Add an SMS entry
930 if (kind.iconAltRes > 0) {
931 entry.secondaryActionIcon = kind.iconAltRes;
932 }
933 }
934 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
935 // Build email entries
936 entry.intent = new Intent(Intent.ACTION_SENDTO,
937 Uri.fromParts("mailto", entry.data, null));
Evan Millar49714ee2009-09-02 16:42:47 -0700938 // Temporary hack until we get real label resources for exchange.
939 if (TextUtils.isEmpty(entry.label)) {
940 entry.label = getString(R.string.email).toLowerCase();
941 }
Evan Millar11d628c2009-09-02 08:55:01 -0700942 entry.isPrimary = isSuperPrimary;
943 mEmailEntries.add(entry);
944 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
945 equals(mimetype)) {
946 // Build postal entries
947 entry.maxLines = 4;
948 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
949 mPostalEntries.add(entry);
950 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
951 // Build im entries
952 Object protocolObj = entryValues.getAsInteger(Data.DATA5);
953 String host = null;
954
Evan Millar49714ee2009-09-02 16:42:47 -0700955 if (TextUtils.isEmpty(entry.label)) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700956 entry.label = getString(R.string.chat_other).toLowerCase();
Evan Millar49714ee2009-09-02 16:42:47 -0700957 }
958
Evan Millar11d628c2009-09-02 08:55:01 -0700959 if (protocolObj instanceof Number) {
960 int protocol = ((Number) protocolObj).intValue();
961 host = ContactsUtils.lookupProviderNameFromId(
962 protocol).toLowerCase();
963 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
964 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
965 entry.maxLabelLines = 2;
966 }
967 } else if (protocolObj != null) {
968 String providerName = (String) protocolObj;
969 host = providerName.toLowerCase();
970 }
971
972 // Only add the intent if there is a valid host
973 if (!TextUtils.isEmpty(host)) {
974 entry.intent = new Intent(Intent.ACTION_SENDTO,
975 constructImToUrl(host, entry.data));
976 }
977 //TODO(emillar) Add in presence info
978 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
979 entry.presenceIcon = Presence.getPresenceIconResourceId(
980 aggCursor.getInt(METHODS_STATUS_COLUMN));
981 entry.status = ...
982 }*/
983 mImEntries.add(entry);
984 }
985 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
986 // Build organization entries
987 mOrganizationEntries.add(entry);
988 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
989 // Build note entries
990 entry.id = 0;
991 entry.uri = null;
992 entry.intent = null;
993 entry.maxLines = 10;
994 mOtherEntries.add(entry);
995 }
996
997
998 // TODO(emillar) Add group entries
999 // // Build the group entries
1000 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1001 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1002 // null, null, Groups.DEFAULT_SORT_ORDER);
1003 // if (groupCursor != null) {
1004 // try {
1005 // StringBuilder sb = new StringBuilder();
1006 //
1007 // while (groupCursor.moveToNext()) {
1008 // String systemId = groupCursor.getString(
1009 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1010 //
1011 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1012 // continue;
1013 // }
1014 //
1015 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1016 // if (!TextUtils.isEmpty(name)) {
1017 // if (sb.length() == 0) {
1018 // sb.append(name);
1019 // } else {
1020 // sb.append(getString(R.string.group_list, name));
1021 // }
1022 // }
1023 // }
1024 //
1025 // if (sb.length() > 0) {
1026 // ViewEntry entry = new ViewEntry();
1027 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1028 // entry.label = getString(R.string.label_groups);
1029 // entry.data = sb.toString();
1030 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1031 //
1032 // // TODO: Add an icon for the groups item.
1033 //
1034 // mGroupEntries.add(entry);
1035 // }
1036 // } finally {
1037 // groupCursor.close();
1038 // }
1039 // }
Evan Millar66388be2009-05-28 15:41:07 -07001040 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001041
Evan Millar66388be2009-05-28 15:41:07 -07001042 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001043 }
1044 }
1045
Evan Millar11d628c2009-09-02 08:55:01 -07001046 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1047 if (kind.actionHeader == null) {
1048 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001049 }
Evan Millar11d628c2009-09-02 08:55:01 -07001050 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1051 if (actionHeader == null) {
1052 return null;
1053 }
1054 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1055 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001056
Evan Millar11d628c2009-09-02 08:55:01 -07001057 String buildDataString(DataKind kind, ContentValues values) {
1058 if (kind.actionBody == null) {
1059 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001060 }
Evan Millar11d628c2009-09-02 08:55:01 -07001061 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1062 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001063 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001064
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001065 /**
1066 * A basic structure with the data for a contact entry in the list.
1067 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001068 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001069 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001070 public boolean isPrimary = false;
1071 public int presenceIcon = -1;
1072 public int secondaryActionIcon = -1;
1073 public Intent intent;
1074 public Intent secondaryIntent = null;
1075 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001076 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001077 public ArrayList<Long> ids = new ArrayList<Long>();
1078 public int collapseCount = 0;
1079
1080 public boolean collapseWith(ViewEntry entry) {
1081 // assert equal collapse keys
1082 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1083 return false;
1084 }
1085
1086 // Choose the label associated with the highest type precedence.
1087 if (TypePrecedence.getTypePrecedence(mimetype, type)
1088 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1089 type = entry.type;
1090 label = entry.label;
1091 }
1092
1093 // Choose the max of the maxLines and maxLabelLines values.
1094 maxLines = Math.max(maxLines, entry.maxLines);
1095 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1096
1097 // Choose the presence with the highest precedence.
1098 if (Presence.getPresencePrecedence(status)
1099 < Presence.getPresencePrecedence(entry.status)) {
1100 status = entry.status;
1101 }
1102
1103 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001104 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001105
1106 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1107
1108 // Keep track of all the ids that have been collapsed with this one.
1109 ids.add(entry.id);
1110 collapseCount++;
1111 return true;
1112 }
1113
1114 public String getCollapseKey() {
1115 StringBuilder hashSb = new StringBuilder();
1116 hashSb.append(data);
1117 hashSb.append(mimetype);
1118 hashSb.append((intent != null && intent.getAction() != null)
1119 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001120 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1121 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001122 hashSb.append(actionIcon);
1123 return hashSb.toString();
1124 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001125 }
1126
Evan Millar15e514d2009-08-04 10:14:57 -07001127 /** Cache of the children views of a row */
1128 static class ViewCache {
1129 public TextView label;
1130 public TextView data;
1131 public ImageView actionIcon;
1132 public ImageView presenceIcon;
1133 public ImageView primaryIcon;
1134 public ImageView secondaryActionButton;
1135 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001136
Evan Millar15e514d2009-08-04 10:14:57 -07001137 // Need to keep track of this too
1138 ViewEntry entry;
1139 }
1140
1141 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1142 implements View.OnClickListener {
1143
Evan Millar5c22c3b2009-05-29 11:37:54 -07001144
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001145 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1146 super(context, sections, SHOW_SEPARATORS);
1147 }
1148
Evan Millar15e514d2009-08-04 10:14:57 -07001149 public void onClick(View v) {
1150 Intent intent = (Intent) v.getTag();
1151 startActivity(intent);
1152 }
1153
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001154 @Override
1155 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001156 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001157 View v;
1158
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001159 ViewCache views;
1160
1161 // Check to see if we can reuse convertView
1162 if (convertView != null) {
1163 v = convertView;
1164 views = (ViewCache) v.getTag();
1165 } else {
1166 // Create a new view if needed
1167 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1168
1169 // Cache the children
1170 views = new ViewCache();
1171 views.label = (TextView) v.findViewById(android.R.id.text1);
1172 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001173 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1174 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1175 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1176 views.secondaryActionButton = (ImageView) v.findViewById(
1177 R.id.secondary_action_button);
1178 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001179 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001180 v.setTag(views);
1181 }
1182
1183 // Update the entry in the view cache
1184 views.entry = entry;
1185
1186 // Bind the data to the view
1187 bindView(v, entry);
1188 return v;
1189 }
1190
1191 @Override
1192 protected View newView(int position, ViewGroup parent) {
1193 // getView() handles this
1194 throw new UnsupportedOperationException();
1195 }
1196
1197 @Override
1198 protected void bindView(View view, ViewEntry entry) {
1199 final Resources resources = mContext.getResources();
1200 ViewCache views = (ViewCache) view.getTag();
1201
1202 // Set the label
1203 TextView label = views.label;
1204 setMaxLines(label, entry.maxLabelLines);
1205 label.setText(entry.label);
1206
1207 // Set the data
1208 TextView data = views.data;
1209 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001210 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001211 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001212 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1213 } else {
1214 data.setText(entry.data);
1215 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001216 setMaxLines(data, entry.maxLines);
1217 }
1218
Evan Millar15e514d2009-08-04 10:14:57 -07001219 // Set the primary icon
1220 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1221
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001222 // Set the action icon
1223 ImageView action = views.actionIcon;
1224 if (entry.actionIcon != -1) {
1225 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1226 action.setVisibility(View.VISIBLE);
1227 } else {
1228 // Things should still line up as if there was an icon, so make it invisible
1229 action.setVisibility(View.INVISIBLE);
1230 }
1231
1232 // Set the presence icon
1233 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001234 if (entry.presenceIcon != -1) {
1235 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001236 } else if (entry.status != -1) {
1237 presenceIcon = resources.getDrawable(
1238 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001239 }
Evan Millar15e514d2009-08-04 10:14:57 -07001240 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001241 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001242 presenceIconView.setImageDrawable(presenceIcon);
1243 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001244 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001245 presenceIconView.setVisibility(View.GONE);
1246 }
1247
1248 // Set the secondary action button
1249 ImageView secondaryActionView = views.secondaryActionButton;
1250 Drawable secondaryActionIcon = null;
1251 if (entry.secondaryActionIcon != -1) {
1252 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1253 }
1254 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1255 secondaryActionView.setImageDrawable(secondaryActionIcon);
1256 secondaryActionView.setTag(entry.secondaryIntent);
1257 secondaryActionView.setVisibility(View.VISIBLE);
1258 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1259 } else {
1260 secondaryActionView.setVisibility(View.GONE);
1261 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001262 }
1263 }
1264
1265 private void setMaxLines(TextView textView, int maxLines) {
1266 if (maxLines == 1) {
1267 textView.setSingleLine(true);
1268 textView.setEllipsize(TextUtils.TruncateAt.END);
1269 } else {
1270 textView.setSingleLine(false);
1271 textView.setMaxLines(maxLines);
1272 textView.setEllipsize(null);
1273 }
1274 }
1275 }
1276}