blob: aa8f9449d7787391dbc97a0a0537a0d55528f4c3 [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
Evan Millar9b1a1242009-09-16 10:26:10 -0700444 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700445 }
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;
Evan Millar8a79cee2009-08-19 17:20:49 -0700685 }
686 }
687 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700688 }
689 }
690
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700691 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700692 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700693
694 // The split operation may have removed the original aggregate contact, so we need
695 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700696 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700697 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700698 }
699
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700700 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700701 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700702 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700703
704 try {
705 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700706 long rawContactId = c.getLong(0);
707 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700708 }
709 } finally {
710 c.close();
711 }
712
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700713 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
714 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700715 }
716
717 /**
718 * Given a contact ID sets an aggregation exception to either join the contact with the
719 * current aggregate or split off.
720 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700721 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700722 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700723 for (long aRawContactId : mRawContactIds) {
724 if (aRawContactId != rawContactId) {
725 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
726 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
727 values.put(AggregationExceptions.TYPE, exceptionType);
728 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
729 }
730 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700731 }
732
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700733 private void showOptionsActivity() {
734 final Intent intent = new Intent(this, ContactOptionsActivity.class);
735 intent.setData(mUri);
736 startActivity(intent);
737 }
738
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700739 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
740 AdapterView.AdapterContextMenuInfo info;
741 try {
742 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
743 } catch (ClassCastException e) {
744 Log.e(TAG, "bad menuInfo", e);
745 return null;
746 }
747
748 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
749 }
750
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800751 @Override
752 public boolean onKeyDown(int keyCode, KeyEvent event) {
753 switch (keyCode) {
754 case KeyEvent.KEYCODE_CALL: {
755 try {
756 ITelephony phone = ITelephony.Stub.asInterface(
757 ServiceManager.checkService("phone"));
758 if (phone != null && !phone.isIdle()) {
759 // Skip out and let the key be handled at a higher level
760 break;
761 }
762 } catch (RemoteException re) {
763 // Fall through and try to call the contact
764 }
765
Evan Millar7911ff52009-07-21 15:55:18 -0700766 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800767 if (index != -1) {
768 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700769 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
770 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800771 }
772 } else if (mNumPhoneNumbers != 0) {
773 // There isn't anything selected, call the default number
774 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
775 startActivity(intent);
776 }
777 return true;
778 }
779
780 case KeyEvent.KEYCODE_DEL: {
781 showDialog(DIALOG_CONFIRM_DELETE);
782 return true;
783 }
784 }
785
786 return super.onKeyDown(keyCode, event);
787 }
788
Evan Millar7911ff52009-07-21 15:55:18 -0700789 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800790 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
791 if (entry != null) {
792 Intent intent = entry.intent;
793 if (intent != null) {
794 try {
795 startActivity(intent);
796 } catch (ActivityNotFoundException e) {
797 Log.e(TAG, "No activity found for intent: " + intent);
798 signalError();
799 }
800 } else {
801 signalError();
802 }
803 } else {
804 signalError();
805 }
806 }
807
808 /**
809 * Signal an error to the user via a beep, or some other method.
810 */
811 private void signalError() {
812 //TODO: implement this when we have the sonification APIs
813 }
814
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800815 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700816 // 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 -0800817 StringBuilder buf = new StringBuilder("imto://");
818 buf.append(host);
819 buf.append('/');
820 buf.append(data);
821 return Uri.parse(buf.toString());
822 }
823
824 /**
825 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700826 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800827 * @param personCursor the URI for the contact being displayed
828 */
Evan Millar11d628c2009-09-02 08:55:01 -0700829 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800830 // Clear out the old entries
831 final int numSections = mSections.size();
832 for (int i = 0; i < numSections; i++) {
833 mSections.get(i).clear();
834 }
835
Evan Millar7911ff52009-07-21 15:55:18 -0700836 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700837
Evan Millar11d628c2009-09-02 08:55:01 -0700838 Sources sources = Sources.getInstance(this);
839
Evan Millar66388be2009-05-28 15:41:07 -0700840 // Build up method entries
841 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700842 for (Entity entity: mEntities) {
843 final ContentValues entValues = entity.getEntityValues();
844 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Evan Millar7911ff52009-07-21 15:55:18 -0700845 // TODO: entry.contactId should be renamed to entry.rawContactId
Evan Millar11d628c2009-09-02 08:55:01 -0700846 long contactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700847
Evan Millar11d628c2009-09-02 08:55:01 -0700848 for (NamedContentValues subValue : entity.getSubValues()) {
849 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800850
Evan Millar11d628c2009-09-02 08:55:01 -0700851 ContentValues entryValues = subValue.values;
852 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
853 if (mimetype == null || accountType == null) {
Evan Millar66388be2009-05-28 15:41:07 -0700854 continue;
855 }
856
Evan Millar11d628c2009-09-02 08:55:01 -0700857 ContactsSource contactsSource = sources.getInflatedSource(accountType,
858 ContactsSource.LEVEL_MIMETYPES);
859 if (contactsSource == null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800860 continue;
861 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700862
Evan Millar11d628c2009-09-02 08:55:01 -0700863 DataKind kind = contactsSource.getKindForMimetype(mimetype);
864 if (kind == null) {
865 continue;
866 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800867
Evan Millar11d628c2009-09-02 08:55:01 -0700868 final long id = entryValues.getAsLong(Data._ID);
869 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
870 entry.contactId = contactId;
871 entry.id = id;
872 entry.uri = uri;
873 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -0700874 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -0700875 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700876 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700877 entry.type = entryValues.getAsInteger(kind.typeColumn);
878 }
879 if (kind.iconRes > 0) {
880 entry.actionIcon = kind.iconRes;
881 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800882
Evan Millar11d628c2009-09-02 08:55:01 -0700883 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700884 if (TextUtils.isEmpty(entry.data)) {
885 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700886 continue;
887 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700888
Evan Millar11d628c2009-09-02 08:55:01 -0700889 if (!mRawContactIds.contains(entry.contactId)) {
890 mRawContactIds.add(entry.contactId);
891 }
892
893 // This performs the tab filtering
894 if (mSelectedRawContactId != null
895 && mSelectedRawContactId != entry.contactId
896 && mSelectedRawContactId != ALL_CONTACTS_ID) {
897 continue;
898 }
899
900 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)
901 || CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)
902 || CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)
903 || CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
904 final boolean isSuperPrimary = entryValues.getAsInteger(
905 Data.IS_SUPER_PRIMARY) != 0;
906
907 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
908 // Build phone entries
909 mNumPhoneNumbers++;
910
911 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
912 Uri.fromParts("tel", entry.data, null));
913 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
914 Uri.fromParts("sms", entry.data, null));
915 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700916
917 // If data is empty, don't show it.
918 if (TextUtils.isEmpty(entry.data)) {
919 Log.w(TAG, "empty data for contact method " + id);
920 continue;
921 }
922
Evan Millar11d628c2009-09-02 08:55:01 -0700923 entry.isPrimary = isSuperPrimary;
924 mPhoneEntries.add(entry);
925
926 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
927 || mShowSmsLinksForAllPhones) {
928 // Add an SMS entry
929 if (kind.iconAltRes > 0) {
930 entry.secondaryActionIcon = kind.iconAltRes;
931 }
932 }
933 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
934 // Build email entries
935 entry.intent = new Intent(Intent.ACTION_SENDTO,
936 Uri.fromParts("mailto", entry.data, null));
Evan Millar49714ee2009-09-02 16:42:47 -0700937 // Temporary hack until we get real label resources for exchange.
938 if (TextUtils.isEmpty(entry.label)) {
939 entry.label = getString(R.string.email).toLowerCase();
940 }
Evan Millar11d628c2009-09-02 08:55:01 -0700941 entry.isPrimary = isSuperPrimary;
942 mEmailEntries.add(entry);
943 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
944 equals(mimetype)) {
945 // Build postal entries
946 entry.maxLines = 4;
947 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
948 mPostalEntries.add(entry);
949 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
950 // Build im entries
951 Object protocolObj = entryValues.getAsInteger(Data.DATA5);
952 String host = null;
953
Evan Millar49714ee2009-09-02 16:42:47 -0700954 if (TextUtils.isEmpty(entry.label)) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700955 entry.label = getString(R.string.chat_other).toLowerCase();
Evan Millar49714ee2009-09-02 16:42:47 -0700956 }
957
Evan Millar11d628c2009-09-02 08:55:01 -0700958 if (protocolObj instanceof Number) {
959 int protocol = ((Number) protocolObj).intValue();
960 host = ContactsUtils.lookupProviderNameFromId(
961 protocol).toLowerCase();
962 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
963 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
964 entry.maxLabelLines = 2;
965 }
966 } else if (protocolObj != null) {
967 String providerName = (String) protocolObj;
968 host = providerName.toLowerCase();
969 }
970
971 // Only add the intent if there is a valid host
972 if (!TextUtils.isEmpty(host)) {
973 entry.intent = new Intent(Intent.ACTION_SENDTO,
974 constructImToUrl(host, entry.data));
975 }
976 //TODO(emillar) Add in presence info
977 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
978 entry.presenceIcon = Presence.getPresenceIconResourceId(
979 aggCursor.getInt(METHODS_STATUS_COLUMN));
980 entry.status = ...
981 }*/
982 mImEntries.add(entry);
983 }
984 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
985 // Build organization entries
986 mOrganizationEntries.add(entry);
987 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
988 // Build note entries
989 entry.id = 0;
990 entry.uri = null;
991 entry.intent = null;
992 entry.maxLines = 10;
993 mOtherEntries.add(entry);
994 }
995
996
997 // TODO(emillar) Add group entries
998 // // Build the group entries
999 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1000 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1001 // null, null, Groups.DEFAULT_SORT_ORDER);
1002 // if (groupCursor != null) {
1003 // try {
1004 // StringBuilder sb = new StringBuilder();
1005 //
1006 // while (groupCursor.moveToNext()) {
1007 // String systemId = groupCursor.getString(
1008 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1009 //
1010 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1011 // continue;
1012 // }
1013 //
1014 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1015 // if (!TextUtils.isEmpty(name)) {
1016 // if (sb.length() == 0) {
1017 // sb.append(name);
1018 // } else {
1019 // sb.append(getString(R.string.group_list, name));
1020 // }
1021 // }
1022 // }
1023 //
1024 // if (sb.length() > 0) {
1025 // ViewEntry entry = new ViewEntry();
1026 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1027 // entry.label = getString(R.string.label_groups);
1028 // entry.data = sb.toString();
1029 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1030 //
1031 // // TODO: Add an icon for the groups item.
1032 //
1033 // mGroupEntries.add(entry);
1034 // }
1035 // } finally {
1036 // groupCursor.close();
1037 // }
1038 // }
Evan Millar66388be2009-05-28 15:41:07 -07001039 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001040
Evan Millar66388be2009-05-28 15:41:07 -07001041 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001042 }
1043 }
1044
Evan Millar11d628c2009-09-02 08:55:01 -07001045 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1046 if (kind.actionHeader == null) {
1047 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001048 }
Evan Millar11d628c2009-09-02 08:55:01 -07001049 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1050 if (actionHeader == null) {
1051 return null;
1052 }
1053 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1054 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001055
Evan Millar11d628c2009-09-02 08:55:01 -07001056 String buildDataString(DataKind kind, ContentValues values) {
1057 if (kind.actionBody == null) {
1058 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001059 }
Evan Millar11d628c2009-09-02 08:55:01 -07001060 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1061 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001062 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001063
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001064 /**
1065 * A basic structure with the data for a contact entry in the list.
1066 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001067 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001068 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001069 public boolean isPrimary = false;
1070 public int presenceIcon = -1;
1071 public int secondaryActionIcon = -1;
1072 public Intent intent;
1073 public Intent secondaryIntent = null;
1074 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001075 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001076 public ArrayList<Long> ids = new ArrayList<Long>();
1077 public int collapseCount = 0;
1078
1079 public boolean collapseWith(ViewEntry entry) {
1080 // assert equal collapse keys
1081 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1082 return false;
1083 }
1084
1085 // Choose the label associated with the highest type precedence.
1086 if (TypePrecedence.getTypePrecedence(mimetype, type)
1087 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1088 type = entry.type;
1089 label = entry.label;
1090 }
1091
1092 // Choose the max of the maxLines and maxLabelLines values.
1093 maxLines = Math.max(maxLines, entry.maxLines);
1094 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1095
1096 // Choose the presence with the highest precedence.
1097 if (Presence.getPresencePrecedence(status)
1098 < Presence.getPresencePrecedence(entry.status)) {
1099 status = entry.status;
1100 }
1101
1102 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001103 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001104
1105 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1106
1107 // Keep track of all the ids that have been collapsed with this one.
1108 ids.add(entry.id);
1109 collapseCount++;
1110 return true;
1111 }
1112
1113 public String getCollapseKey() {
1114 StringBuilder hashSb = new StringBuilder();
1115 hashSb.append(data);
1116 hashSb.append(mimetype);
1117 hashSb.append((intent != null && intent.getAction() != null)
1118 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001119 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1120 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001121 hashSb.append(actionIcon);
1122 return hashSb.toString();
1123 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001124 }
1125
Evan Millar15e514d2009-08-04 10:14:57 -07001126 /** Cache of the children views of a row */
1127 static class ViewCache {
1128 public TextView label;
1129 public TextView data;
1130 public ImageView actionIcon;
1131 public ImageView presenceIcon;
1132 public ImageView primaryIcon;
1133 public ImageView secondaryActionButton;
1134 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001135
Evan Millar15e514d2009-08-04 10:14:57 -07001136 // Need to keep track of this too
1137 ViewEntry entry;
1138 }
1139
1140 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1141 implements View.OnClickListener {
1142
Evan Millar5c22c3b2009-05-29 11:37:54 -07001143
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001144 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1145 super(context, sections, SHOW_SEPARATORS);
1146 }
1147
Evan Millar15e514d2009-08-04 10:14:57 -07001148 public void onClick(View v) {
1149 Intent intent = (Intent) v.getTag();
1150 startActivity(intent);
1151 }
1152
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001153 @Override
1154 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001155 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001156 View v;
1157
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001158 ViewCache views;
1159
1160 // Check to see if we can reuse convertView
1161 if (convertView != null) {
1162 v = convertView;
1163 views = (ViewCache) v.getTag();
1164 } else {
1165 // Create a new view if needed
1166 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1167
1168 // Cache the children
1169 views = new ViewCache();
1170 views.label = (TextView) v.findViewById(android.R.id.text1);
1171 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001172 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1173 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1174 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1175 views.secondaryActionButton = (ImageView) v.findViewById(
1176 R.id.secondary_action_button);
1177 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001178 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001179 v.setTag(views);
1180 }
1181
1182 // Update the entry in the view cache
1183 views.entry = entry;
1184
1185 // Bind the data to the view
1186 bindView(v, entry);
1187 return v;
1188 }
1189
1190 @Override
1191 protected View newView(int position, ViewGroup parent) {
1192 // getView() handles this
1193 throw new UnsupportedOperationException();
1194 }
1195
1196 @Override
1197 protected void bindView(View view, ViewEntry entry) {
1198 final Resources resources = mContext.getResources();
1199 ViewCache views = (ViewCache) view.getTag();
1200
1201 // Set the label
1202 TextView label = views.label;
1203 setMaxLines(label, entry.maxLabelLines);
1204 label.setText(entry.label);
1205
1206 // Set the data
1207 TextView data = views.data;
1208 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001209 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001210 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001211 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1212 } else {
1213 data.setText(entry.data);
1214 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001215 setMaxLines(data, entry.maxLines);
1216 }
1217
Evan Millar15e514d2009-08-04 10:14:57 -07001218 // Set the primary icon
1219 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1220
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001221 // Set the action icon
1222 ImageView action = views.actionIcon;
1223 if (entry.actionIcon != -1) {
1224 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1225 action.setVisibility(View.VISIBLE);
1226 } else {
1227 // Things should still line up as if there was an icon, so make it invisible
1228 action.setVisibility(View.INVISIBLE);
1229 }
1230
1231 // Set the presence icon
1232 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001233 if (entry.presenceIcon != -1) {
1234 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001235 } else if (entry.status != -1) {
1236 presenceIcon = resources.getDrawable(
1237 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001238 }
Evan Millar15e514d2009-08-04 10:14:57 -07001239 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001240 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001241 presenceIconView.setImageDrawable(presenceIcon);
1242 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001243 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001244 presenceIconView.setVisibility(View.GONE);
1245 }
1246
1247 // Set the secondary action button
1248 ImageView secondaryActionView = views.secondaryActionButton;
1249 Drawable secondaryActionIcon = null;
1250 if (entry.secondaryActionIcon != -1) {
1251 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1252 }
1253 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1254 secondaryActionView.setImageDrawable(secondaryActionIcon);
1255 secondaryActionView.setTag(entry.secondaryIntent);
1256 secondaryActionView.setVisibility(View.VISIBLE);
1257 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1258 } else {
1259 secondaryActionView.setVisibility(View.GONE);
1260 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001261 }
1262 }
1263
1264 private void setMaxLines(TextView textView, int maxLines) {
1265 if (maxLines == 1) {
1266 textView.setSingleLine(true);
1267 textView.setEllipsize(TextUtils.TruncateAt.END);
1268 } else {
1269 textView.setSingleLine(false);
1270 textView.setMaxLines(maxLines);
1271 textView.setEllipsize(null);
1272 }
1273 }
1274 }
1275}