blob: 385fbc6707cf2ca8c31ce5c15be1bb1263e04f2a [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts;
18
Evan Millar54a5c9f2009-06-23 17:41:09 -070019import com.android.contacts.Collapser.Collapsible;
Evan Millar11d628c2009-09-02 08:55:01 -070020import com.android.contacts.ScrollingTabWidget.OnTabSelectionChangedListener;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070021import com.android.contacts.SplitAggregateView.OnContactSelectedListener;
Evan Millar11d628c2009-09-02 08:55:01 -070022import com.android.contacts.model.ContactsSource;
23import com.android.contacts.model.Sources;
24import com.android.contacts.model.ContactsSource.DataKind;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070025import com.android.contacts.util.Constants;
Evan Millar11d628c2009-09-02 08:55:01 -070026import com.android.contacts.util.NotifyingAsyncQueryHandler;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080027import com.android.internal.telephony.ITelephony;
Evan Millar11d628c2009-09-02 08:55:01 -070028import com.android.internal.widget.ContactHeaderWidget;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080029
Evan Millar11d628c2009-09-02 08:55:01 -070030import android.app.Activity;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080031import android.app.AlertDialog;
32import android.app.Dialog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080033import android.content.ActivityNotFoundException;
34import android.content.ContentResolver;
35import android.content.ContentUris;
36import android.content.ContentValues;
37import android.content.Context;
38import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070039import android.content.Entity;
Evan Millar11d628c2009-09-02 08:55:01 -070040import android.content.EntityIterator;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080041import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070042import android.content.DialogInterface.OnClickListener;
Evan Millar11d628c2009-09-02 08:55:01 -070043import android.content.Entity.NamedContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080044import android.content.res.Resources;
45import android.database.ContentObserver;
46import android.database.Cursor;
47import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080048import android.net.Uri;
49import android.os.Bundle;
50import android.os.Handler;
51import android.os.RemoteException;
52import android.os.ServiceManager;
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -070053import android.provider.ContactsContract;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070054import android.provider.ContactsContract.AggregationExceptions;
55import android.provider.ContactsContract.CommonDataKinds;
Evan Millar11d628c2009-09-02 08:55:01 -070056import android.provider.ContactsContract.Contacts;
Evan Millar66388be2009-05-28 15:41:07 -070057import android.provider.ContactsContract.Data;
Evan Millar54a5c9f2009-06-23 17:41:09 -070058import android.provider.ContactsContract.Presence;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070059import android.provider.ContactsContract.RawContacts;
Evan Millar54a5c9f2009-06-23 17:41:09 -070060import android.provider.ContactsContract.CommonDataKinds.Phone;
61import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080062import android.text.TextUtils;
63import android.util.Log;
Evan Millar11d628c2009-09-02 08:55:01 -070064import android.util.SparseArray;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080065import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070066import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080067import android.view.KeyEvent;
Evan Millar11d628c2009-09-02 08:55:01 -070068import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080069import android.view.Menu;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070070import android.view.MenuInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080071import android.view.MenuItem;
72import android.view.View;
73import android.view.ViewGroup;
Evan Millar11d628c2009-09-02 08:55:01 -070074import android.view.Window;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080075import android.view.ContextMenu.ContextMenuInfo;
76import android.widget.AdapterView;
Evan Millar7911ff52009-07-21 15:55:18 -070077import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080078import android.widget.ImageView;
79import android.widget.ListView;
80import android.widget.TextView;
81import android.widget.Toast;
82
83import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080084
85/**
86 * Displays the details of a specific contact.
87 */
Evan Millar11d628c2009-09-02 08:55:01 -070088public class ViewContactActivity extends Activity
Evan Millar7911ff52009-07-21 15:55:18 -070089 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
Evan Millar11d628c2009-09-02 08:55:01 -070090 AdapterView.OnItemClickListener, NotifyingAsyncQueryHandler.AsyncQueryListener,
91 OnTabSelectionChangedListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080092 private static final String TAG = "ViewContact";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080093
Evan Millar8a79cee2009-08-19 17:20:49 -070094 public static final String RAW_CONTACT_ID_EXTRA = "rawContactIdExtra";
95
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080096 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080097
98 private static final int DIALOG_CONFIRM_DELETE = 1;
99
Evan Millar8a79cee2009-08-19 17:20:49 -0700100 private static final int REQUEST_JOIN_CONTACT = 1;
101 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700102
Evan Millar8a79cee2009-08-19 17:20:49 -0700103 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800104
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700105 protected Uri mLookupUri;
Evan Millar11d628c2009-09-02 08:55:01 -0700106 private Uri mUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800107 private ContentResolver mResolver;
108 private ViewAdapter mAdapter;
109 private int mNumPhoneNumbers = 0;
110
Evan Millar8a79cee2009-08-19 17:20:49 -0700111 private static final long ALL_CONTACTS_ID = -100;
Evan Millar7911ff52009-07-21 15:55:18 -0700112
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700113 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700114 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700115 */
Evan Millar7911ff52009-07-21 15:55:18 -0700116 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700117
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800118 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
119 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
120 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
121 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
122 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
123 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700124 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800125 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
126 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
127
128 private Cursor mCursor;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700129
Evan Millar11d628c2009-09-02 08:55:01 -0700130 private SparseArray<Long> mTabRawContactIdMap;
131 protected ScrollingTabWidget mTabWidget;
132 protected ContactHeaderWidget mContactHeaderWidget;
133 private NotifyingAsyncQueryHandler mHandler;
134
135 protected LayoutInflater mInflater;
136
137 //Projection used for the query that determines which tabs to add.
138 protected static final String[] TAB_PROJECTION = new String[] {
139 RawContacts._ID,
140 RawContacts.ACCOUNT_NAME,
141 RawContacts.ACCOUNT_TYPE
142 };
143 protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0;
144 protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1;
145 protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2;
146
147 protected static final String SELECTED_RAW_CONTACT_ID_KEY = "selectedRawContact";
148 protected Long mSelectedRawContactId = null;
149
150 private static final int TOKEN_QUERY = 0;
151
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800152 private ContentObserver mObserver = new ContentObserver(new Handler()) {
153 @Override
154 public boolean deliverSelfNotifications() {
155 return true;
156 }
157
158 @Override
159 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700160 if (mCursor != null && !mCursor.isClosed()) {
161 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800162 }
163 }
164 };
165
166 public void onClick(DialogInterface dialog, int which) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700167 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800168 getContentResolver().delete(mUri, null, null);
169 finish();
170 }
171
Evan Millar7911ff52009-07-21 15:55:18 -0700172 private FrameLayout mTabContentLayout;
173 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800174 private boolean mShowSmsLinksForAllPhones;
Evan Millar11d628c2009-09-02 08:55:01 -0700175 private ArrayList<Entity> mEntities = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800176
177 @Override
178 protected void onCreate(Bundle icicle) {
179 super.onCreate(icicle);
180
Evan Millar11d628c2009-09-02 08:55:01 -0700181 final Intent intent = getIntent();
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700182 Uri data = intent.getData();
183 String authority = data.getAuthority();
184 if (ContactsContract.AUTHORITY.equals(authority)) {
185 mLookupUri = data;
186 } else if (android.provider.Contacts.AUTHORITY.equals(authority)) {
187 final long rawContactId = ContentUris.parseId(data);
188 mLookupUri = RawContacts.getContactLookupUri(getContentResolver(),
189 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700190
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700191 }
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700192 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700193
194 requestWindowFeature(Window.FEATURE_NO_TITLE);
195 setContentView(R.layout.contact_card_layout);
196
197 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
198 mContactHeaderWidget.showStar(true);
Evan Millar11d628c2009-09-02 08:55:01 -0700199 mContactHeaderWidget.setExcludeMimes(new String[] {
200 Contacts.CONTENT_ITEM_TYPE
201 });
202
203 mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget);
204 mTabWidget.setTabSelectionListener(this);
205 mTabWidget.setVisibility(View.INVISIBLE);
206
207 mTabRawContactIdMap = new SparseArray<Long>();
208
209 mHandler = new NotifyingAsyncQueryHandler(this, this);
210
Evan Millar7911ff52009-07-21 15:55:18 -0700211 mListView = new ListView(this);
212 mListView.setOnCreateContextMenuListener(this);
213 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
214 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800215
Evan Millar7911ff52009-07-21 15:55:18 -0700216 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
217 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800218
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800219 mResolver = getContentResolver();
220
221 // Build the list of sections. The order they're added to mSections dictates the
222 // order they are displayed in the list.
223 mSections.add(mPhoneEntries);
224 mSections.add(mSmsEntries);
225 mSections.add(mEmailEntries);
226 mSections.add(mImEntries);
227 mSections.add(mPostalEntries);
228 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700229 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800230 mSections.add(mOtherEntries);
231
232 //TODO Read this value from a preference
233 mShowSmsLinksForAllPhones = true;
234
Evan Millar11d628c2009-09-02 08:55:01 -0700235 startEntityQuery();
236 }
237
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800238 @Override
239 protected void onResume() {
240 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700241 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800242 }
243
244 @Override
245 protected void onPause() {
246 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700247 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800248 }
249
250 @Override
251 protected void onDestroy() {
252 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700253 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800254 }
255
256 @Override
Evan Millar11d628c2009-09-02 08:55:01 -0700257 protected void onRestoreInstanceState(Bundle savedInstanceState) {
258 super.onRestoreInstanceState(savedInstanceState);
259 mSelectedRawContactId = savedInstanceState.getLong(SELECTED_RAW_CONTACT_ID_KEY);
260 }
261
262 @Override
263 protected void onSaveInstanceState(Bundle outState) {
264 super.onSaveInstanceState(outState);
265 outState.putLong(SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
266 }
267
268 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800269 protected Dialog onCreateDialog(int id) {
270 switch (id) {
271 case DIALOG_CONFIRM_DELETE:
272 return new AlertDialog.Builder(this)
273 .setTitle(R.string.deleteConfirmation_title)
274 .setIcon(android.R.drawable.ic_dialog_alert)
275 .setMessage(R.string.deleteConfirmation)
276 .setNegativeButton(android.R.string.cancel, null)
277 .setPositiveButton(android.R.string.ok, this)
278 .setCancelable(false)
279 .create();
280 }
281 return null;
282 }
283
Evan Millar11d628c2009-09-02 08:55:01 -0700284
285 // TAB CODE //
286 /**
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700287 * Adds a tab for each {@link RawContacts} associated with this contact.
Evan Millar11d628c2009-09-02 08:55:01 -0700288 * Override this method if you want to additional tabs and/or different
289 * tabs for your activity.
Evan Millar11d628c2009-09-02 08:55:01 -0700290 */
291 protected void bindTabs() {
292 if (mEntities.size() > 1) {
Evan Millar7911ff52009-07-21 15:55:18 -0700293 addAllTab();
294 }
Evan Millar11d628c2009-09-02 08:55:01 -0700295
296 final Sources sources = Sources.getInstance(this);
297
298 for (Entity entity : mEntities) {
299 final String accountType = entity.getEntityValues().
300 getAsString(RawContacts.ACCOUNT_TYPE);
301 final Long rawContactId = entity.getEntityValues().
302 getAsLong(RawContacts._ID);
303
304 // TODO: ensure inflation on background task so we don't block UI thread here
305 final ContactsSource source = sources.getInflatedSource(accountType,
306 ContactsSource.LEVEL_SUMMARY);
307 addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(), source));
308 }
309
310 selectInitialTab();
311 mTabWidget.setVisibility(View.VISIBLE);
312 mTabWidget.postInvalidate();
313 }
314
315 /**
316 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
317 *
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700318 * @param rawContactId The contact id associated with the tab.
Evan Millar11d628c2009-09-02 08:55:01 -0700319 * @param view A view to use as the tab indicator.
320 */
321 protected void addTab(long rawContactId, View view) {
322 mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId);
323 mTabWidget.addTab(view);
324 }
325
326
327 protected void clearCurrentTabs() {
328 mTabRawContactIdMap.clear();
329 mTabWidget.removeAllTabs();
330 }
331
332 protected void selectInitialTab() {
333 int selectedTabIndex = 0;
334
335 if (mSelectedRawContactId != null) {
336 selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
337 if (selectedTabIndex == -1) {
338 // If there was no matching tab, just select the first;
339 selectedTabIndex = 0;
340 }
341 }
342
343 mTabWidget.setCurrentTab(selectedTabIndex);
344 onTabSelectionChanged(selectedTabIndex, false);
345 }
346
Evan Millar7911ff52009-07-21 15:55:18 -0700347 private void addAllTab() {
Evan Millar56d2caa2009-08-20 20:30:12 -0700348 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator,
349 mTabWidget.getTabParent(), false);
Evan Millar74660912009-08-19 17:36:33 -0700350 allTabIndicator.getBackground().setDither(true);
Evan Millar7911ff52009-07-21 15:55:18 -0700351 addTab(ALL_CONTACTS_ID, allTabIndicator);
352 }
353
354 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
355 long rawContactId = getTabRawContactId(tabIndex);
Evan Millar8a79cee2009-08-19 17:20:49 -0700356 mSelectedRawContactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700357 bindData();
Evan Millar7911ff52009-07-21 15:55:18 -0700358 }
359
Evan Millar11d628c2009-09-02 08:55:01 -0700360 /**
361 * Return the RawContact id associated with the tab at an index.
362 *
363 * @param index The index of the tab in question.
364 * @return The contactId associated with the tab at the specified index.
365 */
366 protected long getTabRawContactId(int index) {
367 return mTabRawContactIdMap.get(index);
368 }
Evan Millar2c1cc832009-07-13 11:08:06 -0700369
Evan Millar11d628c2009-09-02 08:55:01 -0700370 /**
371 * Return the tab index associated with the RawContact id.
372 *
373 * @param index The index of the tab in question.
374 * @return The contactId associated with the tab at the specified index.
375 */
376 protected int getTabIndexForRawContactId(long rawContactId) {
377 int numTabs = mTabRawContactIdMap.size();
378 for (int i=0; i < numTabs; i++) {
379 if (mTabRawContactIdMap.get(i) == rawContactId) {
380 return i;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800381 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800382 }
Evan Millar11d628c2009-09-02 08:55:01 -0700383 return -1;
384 }
385
386
387 // QUERY CODE //
388 /** {@inheritDoc} */
389 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
390 try{
391 if (token == TOKEN_QUERY) {
392 clearCurrentTabs();
393 mEntities = readEntities(iterator);
394 bindTabs();
395 bindData();
396 }
397 } finally {
398 if (iterator != null) {
399 iterator.close();
400 }
401 }
402 }
403
404 /** {@inheritDoc} */
405 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700406 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700407 }
408
409 private ArrayList<Entity> readEntities(EntityIterator iterator) {
410 ArrayList<Entity> entities = new ArrayList<Entity>();
411 try {
412 while (iterator.hasNext()) {
413 entities.add(iterator.next());
414 }
415 } catch (RemoteException e) {
416 }
417
418 return entities;
419 }
420
421 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700422 closeCursor();
423
Dmitri Plotnikov0306bbd2009-09-03 14:37:15 -0700424 mUri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700425 if (mLookupUri != null) {
426 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
427 if (mLookupUri != null) {
428 mUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
429 }
430 }
431
432 if (mUri == null) {
433
434 // TODO either figure out a way to prevent a flash of black background or
435 // use some other UI than a toast
436 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
437 Log.e(TAG, "invalid contact uri: " + mLookupUri);
438 finish();
439 return;
440 }
441
442 mCursor = mResolver.query(Uri.withAppendedPath(mUri, Contacts.Data.CONTENT_DIRECTORY),
443 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
444 mCursor.registerContentObserver(mObserver);
445
Evan Millar11d628c2009-09-02 08:55:01 -0700446 long contactId = ContentUris.parseId(mUri);
447 mHandler.startQueryEntities(TOKEN_QUERY, null,
448 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700449
Evan Millar9b1a1242009-09-16 10:26:10 -0700450 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700451 }
452
453 private void closeCursor() {
454 if (mCursor != null) {
455 mCursor.unregisterContentObserver(mObserver);
456 mCursor.close();
457 mCursor = null;
458 }
Evan Millar11d628c2009-09-02 08:55:01 -0700459 }
460
461 private void bindData() {
462
463 // Build up the contact entries
464 buildEntries();
465
466 // Collapse similar data items in select sections.
467 Collapser.collapseList(mPhoneEntries);
468 Collapser.collapseList(mSmsEntries);
469 Collapser.collapseList(mEmailEntries);
470 Collapser.collapseList(mPostalEntries);
471
472 if (mAdapter == null) {
473 mAdapter = new ViewAdapter(this, mSections);
474 mListView.setAdapter(mAdapter);
475 } else {
476 mAdapter.setSections(mSections, SHOW_SEPARATORS);
477 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800478 }
479
480 @Override
481 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700482 super.onCreateOptionsMenu(menu);
483
484 final MenuInflater inflater = getMenuInflater();
485 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800486 return true;
487 }
488
489 @Override
490 public boolean onPrepareOptionsMenu(Menu menu) {
491 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700492
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700493 // Only allow edit if we have a selected tab
494 final boolean contactSelected = (mSelectedRawContactId != null);
495 menu.findItem(R.id.menu_edit).setEnabled(contactSelected);
Evan Millardb5d88c2009-08-28 09:31:57 -0700496
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700497 // Only allow split when more than one contact
498 final boolean isAggregate = (mRawContactIds.size() > 1);
499 menu.findItem(R.id.menu_split).setEnabled(isAggregate);
500
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800501 return true;
502 }
503
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800504 @Override
505 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
506 AdapterView.AdapterContextMenuInfo info;
507 try {
508 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
509 } catch (ClassCastException e) {
510 Log.e(TAG, "bad menuInfo", e);
511 return;
512 }
513
514 // This can be null sometimes, don't crash...
515 if (info == null) {
516 Log.e(TAG, "bad menuInfo");
517 return;
518 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700519
Evan Millar45e0ed32009-06-01 16:44:38 -0700520 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
521 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
522 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700523 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
524 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700525 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800526 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700527 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
528 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700529 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700530 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800531 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700532 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700533 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
534 }
535 // TODO(emillar): add back with group support.
536 /* else if (entry.mimetype.equals()) {
537 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
538 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800539 }
540
541 @Override
542 public boolean onOptionsItemSelected(MenuItem item) {
543 switch (item.getItemId()) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700544 case R.id.menu_edit: {
Evan Millardb5d88c2009-08-28 09:31:57 -0700545 Long rawContactIdToEdit = mSelectedRawContactId;
546 if (rawContactIdToEdit == null) {
547 // This shouldn't be possible. We only show the edit option if
548 // this value is non-null.
549 break;
550 }
Evan Millar8a79cee2009-08-19 17:20:49 -0700551 if (rawContactIdToEdit == ALL_CONTACTS_ID) {
552 // If the "all" tab is selected, edit the next tab.
Evan Millardb5d88c2009-08-28 09:31:57 -0700553 rawContactIdToEdit = getTabRawContactId(mTabWidget.getCurrentTab() + 1);
Evan Millar8a79cee2009-08-19 17:20:49 -0700554 }
555 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
556 rawContactIdToEdit);
557 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
558 REQUEST_EDIT_CONTACT);
559 break;
560 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700561 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800562 // Get confirmation
563 showDialog(DIALOG_CONFIRM_DELETE);
564 return true;
565 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700566 case R.id.menu_split: {
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700567 if (mRawContactIds.size() == 2) {
568 splitContact(mRawContactIds.get(1));
569 } else {
570 showSplitAggregateDialog();
571 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700572 return true;
573 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700574 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700575 showJoinAggregateActivity();
576 return true;
577 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700578 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700579 showOptionsActivity();
580 return true;
581 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700582 case R.id.menu_share: {
583 final Intent intent = new Intent(Intent.ACTION_SEND);
584 intent.setType(Contacts.CONTENT_ITEM_TYPE);
585 intent.putExtra(Intent.EXTRA_STREAM, mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700586
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700587 // Launch chooser to share contact via
588 final CharSequence chooseTitle = getText(R.string.share_via);
589 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700590
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700591 try {
592 startActivity(chooseIntent);
593 } catch (ActivityNotFoundException ex) {
594 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800595 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700596 return true;
597 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800598 }
599 return super.onOptionsItemSelected(item);
600 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700601
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800602 @Override
603 public boolean onContextItemSelected(MenuItem item) {
604 switch (item.getItemId()) {
605 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700606 if (makeItemDefault(item)) {
607 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800608 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700609 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800610 }
611 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700612
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800613 return super.onContextItemSelected(item);
614 }
615
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700616 private boolean makeItemDefault(MenuItem item) {
617 ViewEntry entry = getViewEntryForMenuItem(item);
618 if (entry == null) {
619 return false;
620 }
621
622 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700623 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700624 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700625 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
626 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700627 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700628 return true;
629 }
630
631 /**
632 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
633 * The user picks a contact to be split into its own aggregate or clicks Cancel.
634 */
635 private void showSplitAggregateDialog() {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700636 // Wrap this dialog in a specific theme so that list items have correct text color.
637 final ContextThemeWrapper dialogContext =
638 new ContextThemeWrapper(this, android.R.style.Theme_Light);
639 AlertDialog.Builder builder =
640 new AlertDialog.Builder(dialogContext);
641 builder.setTitle(getString(R.string.splitAggregate_title));
642
643 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
644 builder.setView(view);
645
646 builder.setInverseBackgroundForced(true);
647 builder.setCancelable(true);
648 builder.setNegativeButton(android.R.string.cancel,
649 new OnClickListener() {
650 public void onClick(DialogInterface dialog, int which) {
651 dialog.dismiss();
652 }
653 });
654 final AlertDialog dialog = builder.create();
655
656 view.setOnContactSelectedListener(new OnContactSelectedListener() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700657 public void onContactSelected(long rawContactId) {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700658 dialog.dismiss();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700659 splitContact(rawContactId);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700660 }
661 });
662
663 dialog.show();
664 }
665
666 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700667 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700668 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700669 public void showJoinAggregateActivity() {
670 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
671 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700672 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700673 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700674
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700675 @Override
676 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700677 switch (requestCode) {
678 case REQUEST_JOIN_CONTACT: {
679 if (resultCode == RESULT_OK && intent != null) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700680 final long contactId = ContentUris.parseId(intent.getData());
681 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700682 }
683 break;
684 }
685 case REQUEST_EDIT_CONTACT: {
686 if (resultCode == RESULT_OK && intent != null) {
687 long newInitialSelectedRawContactId = intent.getLongExtra(
688 RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
689 if (newInitialSelectedRawContactId != mSelectedRawContactId) {
690 mSelectedRawContactId = newInitialSelectedRawContactId;
Evan Millar8a79cee2009-08-19 17:20:49 -0700691 }
692 }
693 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700694 }
695 }
696
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700697 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700698 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700699
700 // The split operation may have removed the original aggregate contact, so we need
701 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700702 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700703 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700704 }
705
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700706 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700707 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700708 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700709
710 try {
711 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700712 long rawContactId = c.getLong(0);
713 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700714 }
715 } finally {
716 c.close();
717 }
718
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700719 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
720 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700721 }
722
723 /**
724 * Given a contact ID sets an aggregation exception to either join the contact with the
725 * current aggregate or split off.
726 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700727 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700728 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700729 for (long aRawContactId : mRawContactIds) {
730 if (aRawContactId != rawContactId) {
731 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
732 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
733 values.put(AggregationExceptions.TYPE, exceptionType);
734 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
735 }
736 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700737 }
738
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700739 private void showOptionsActivity() {
740 final Intent intent = new Intent(this, ContactOptionsActivity.class);
741 intent.setData(mUri);
742 startActivity(intent);
743 }
744
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700745 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
746 AdapterView.AdapterContextMenuInfo info;
747 try {
748 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
749 } catch (ClassCastException e) {
750 Log.e(TAG, "bad menuInfo", e);
751 return null;
752 }
753
754 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
755 }
756
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800757 @Override
758 public boolean onKeyDown(int keyCode, KeyEvent event) {
759 switch (keyCode) {
760 case KeyEvent.KEYCODE_CALL: {
761 try {
762 ITelephony phone = ITelephony.Stub.asInterface(
763 ServiceManager.checkService("phone"));
764 if (phone != null && !phone.isIdle()) {
765 // Skip out and let the key be handled at a higher level
766 break;
767 }
768 } catch (RemoteException re) {
769 // Fall through and try to call the contact
770 }
771
Evan Millar7911ff52009-07-21 15:55:18 -0700772 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800773 if (index != -1) {
774 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700775 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
776 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800777 }
778 } else if (mNumPhoneNumbers != 0) {
779 // There isn't anything selected, call the default number
780 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
781 startActivity(intent);
782 }
783 return true;
784 }
785
786 case KeyEvent.KEYCODE_DEL: {
787 showDialog(DIALOG_CONFIRM_DELETE);
788 return true;
789 }
790 }
791
792 return super.onKeyDown(keyCode, event);
793 }
794
Evan Millar7911ff52009-07-21 15:55:18 -0700795 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800796 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
797 if (entry != null) {
798 Intent intent = entry.intent;
799 if (intent != null) {
800 try {
801 startActivity(intent);
802 } catch (ActivityNotFoundException e) {
803 Log.e(TAG, "No activity found for intent: " + intent);
804 signalError();
805 }
806 } else {
807 signalError();
808 }
809 } else {
810 signalError();
811 }
812 }
813
814 /**
815 * Signal an error to the user via a beep, or some other method.
816 */
817 private void signalError() {
818 //TODO: implement this when we have the sonification APIs
819 }
820
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800821 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700822 // 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 -0800823 StringBuilder buf = new StringBuilder("imto://");
824 buf.append(host);
825 buf.append('/');
826 buf.append(data);
827 return Uri.parse(buf.toString());
828 }
829
830 /**
831 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700832 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800833 * @param personCursor the URI for the contact being displayed
834 */
Evan Millar11d628c2009-09-02 08:55:01 -0700835 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800836 // Clear out the old entries
837 final int numSections = mSections.size();
838 for (int i = 0; i < numSections; i++) {
839 mSections.get(i).clear();
840 }
841
Evan Millar7911ff52009-07-21 15:55:18 -0700842 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700843
Evan Millar11d628c2009-09-02 08:55:01 -0700844 Sources sources = Sources.getInstance(this);
845
Evan Millar66388be2009-05-28 15:41:07 -0700846 // Build up method entries
847 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700848 for (Entity entity: mEntities) {
849 final ContentValues entValues = entity.getEntityValues();
850 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Evan Millar7911ff52009-07-21 15:55:18 -0700851 // TODO: entry.contactId should be renamed to entry.rawContactId
Evan Millar11d628c2009-09-02 08:55:01 -0700852 long contactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700853
Evan Millar11d628c2009-09-02 08:55:01 -0700854 for (NamedContentValues subValue : entity.getSubValues()) {
855 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800856
Evan Millar11d628c2009-09-02 08:55:01 -0700857 ContentValues entryValues = subValue.values;
858 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
859 if (mimetype == null || accountType == null) {
Evan Millar66388be2009-05-28 15:41:07 -0700860 continue;
861 }
862
Evan Millar11d628c2009-09-02 08:55:01 -0700863 ContactsSource contactsSource = sources.getInflatedSource(accountType,
864 ContactsSource.LEVEL_MIMETYPES);
865 if (contactsSource == null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800866 continue;
867 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700868
Evan Millar11d628c2009-09-02 08:55:01 -0700869 DataKind kind = contactsSource.getKindForMimetype(mimetype);
870 if (kind == null) {
871 continue;
872 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800873
Evan Millar11d628c2009-09-02 08:55:01 -0700874 final long id = entryValues.getAsLong(Data._ID);
875 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
876 entry.contactId = contactId;
877 entry.id = id;
878 entry.uri = uri;
879 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -0700880 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -0700881 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700882 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700883 entry.type = entryValues.getAsInteger(kind.typeColumn);
884 }
885 if (kind.iconRes > 0) {
886 entry.actionIcon = kind.iconRes;
887 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800888
Evan Millar11d628c2009-09-02 08:55:01 -0700889 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700890 if (TextUtils.isEmpty(entry.data)) {
891 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700892 continue;
893 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700894
Evan Millar11d628c2009-09-02 08:55:01 -0700895 if (!mRawContactIds.contains(entry.contactId)) {
896 mRawContactIds.add(entry.contactId);
897 }
898
899 // This performs the tab filtering
900 if (mSelectedRawContactId != null
901 && mSelectedRawContactId != entry.contactId
902 && mSelectedRawContactId != ALL_CONTACTS_ID) {
903 continue;
904 }
905
906 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)
907 || CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)
908 || CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)
909 || CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
910 final boolean isSuperPrimary = entryValues.getAsInteger(
911 Data.IS_SUPER_PRIMARY) != 0;
912
913 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
914 // Build phone entries
915 mNumPhoneNumbers++;
916
917 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
918 Uri.fromParts("tel", entry.data, null));
919 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
920 Uri.fromParts("sms", entry.data, null));
921 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700922
923 // If data is empty, don't show it.
924 if (TextUtils.isEmpty(entry.data)) {
925 Log.w(TAG, "empty data for contact method " + id);
926 continue;
927 }
928
Evan Millar11d628c2009-09-02 08:55:01 -0700929 entry.isPrimary = isSuperPrimary;
930 mPhoneEntries.add(entry);
931
932 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
933 || mShowSmsLinksForAllPhones) {
934 // Add an SMS entry
935 if (kind.iconAltRes > 0) {
936 entry.secondaryActionIcon = kind.iconAltRes;
937 }
938 }
939 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
940 // Build email entries
941 entry.intent = new Intent(Intent.ACTION_SENDTO,
942 Uri.fromParts("mailto", entry.data, null));
Evan Millar49714ee2009-09-02 16:42:47 -0700943 // Temporary hack until we get real label resources for exchange.
944 if (TextUtils.isEmpty(entry.label)) {
945 entry.label = getString(R.string.email).toLowerCase();
946 }
Evan Millar11d628c2009-09-02 08:55:01 -0700947 entry.isPrimary = isSuperPrimary;
948 mEmailEntries.add(entry);
949 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
950 equals(mimetype)) {
951 // Build postal entries
952 entry.maxLines = 4;
953 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
954 mPostalEntries.add(entry);
955 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
956 // Build im entries
957 Object protocolObj = entryValues.getAsInteger(Data.DATA5);
958 String host = null;
959
Evan Millar49714ee2009-09-02 16:42:47 -0700960 if (TextUtils.isEmpty(entry.label)) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700961 entry.label = getString(R.string.chat_other).toLowerCase();
Evan Millar49714ee2009-09-02 16:42:47 -0700962 }
963
Evan Millar11d628c2009-09-02 08:55:01 -0700964 if (protocolObj instanceof Number) {
965 int protocol = ((Number) protocolObj).intValue();
966 host = ContactsUtils.lookupProviderNameFromId(
967 protocol).toLowerCase();
968 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
969 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
970 entry.maxLabelLines = 2;
971 }
972 } else if (protocolObj != null) {
973 String providerName = (String) protocolObj;
974 host = providerName.toLowerCase();
975 }
976
977 // Only add the intent if there is a valid host
978 if (!TextUtils.isEmpty(host)) {
979 entry.intent = new Intent(Intent.ACTION_SENDTO,
980 constructImToUrl(host, entry.data));
981 }
982 //TODO(emillar) Add in presence info
983 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
984 entry.presenceIcon = Presence.getPresenceIconResourceId(
985 aggCursor.getInt(METHODS_STATUS_COLUMN));
986 entry.status = ...
987 }*/
988 mImEntries.add(entry);
989 }
990 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
991 // Build organization entries
992 mOrganizationEntries.add(entry);
993 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
994 // Build note entries
995 entry.id = 0;
996 entry.uri = null;
997 entry.intent = null;
998 entry.maxLines = 10;
999 mOtherEntries.add(entry);
1000 }
1001
1002
1003 // TODO(emillar) Add group entries
1004 // // Build the group entries
1005 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1006 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1007 // null, null, Groups.DEFAULT_SORT_ORDER);
1008 // if (groupCursor != null) {
1009 // try {
1010 // StringBuilder sb = new StringBuilder();
1011 //
1012 // while (groupCursor.moveToNext()) {
1013 // String systemId = groupCursor.getString(
1014 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1015 //
1016 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1017 // continue;
1018 // }
1019 //
1020 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1021 // if (!TextUtils.isEmpty(name)) {
1022 // if (sb.length() == 0) {
1023 // sb.append(name);
1024 // } else {
1025 // sb.append(getString(R.string.group_list, name));
1026 // }
1027 // }
1028 // }
1029 //
1030 // if (sb.length() > 0) {
1031 // ViewEntry entry = new ViewEntry();
1032 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1033 // entry.label = getString(R.string.label_groups);
1034 // entry.data = sb.toString();
1035 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1036 //
1037 // // TODO: Add an icon for the groups item.
1038 //
1039 // mGroupEntries.add(entry);
1040 // }
1041 // } finally {
1042 // groupCursor.close();
1043 // }
1044 // }
Evan Millar66388be2009-05-28 15:41:07 -07001045 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001046
Evan Millar66388be2009-05-28 15:41:07 -07001047 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001048 }
1049 }
1050
Evan Millar11d628c2009-09-02 08:55:01 -07001051 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1052 if (kind.actionHeader == null) {
1053 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001054 }
Evan Millar11d628c2009-09-02 08:55:01 -07001055 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1056 if (actionHeader == null) {
1057 return null;
1058 }
1059 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1060 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001061
Evan Millar11d628c2009-09-02 08:55:01 -07001062 String buildDataString(DataKind kind, ContentValues values) {
1063 if (kind.actionBody == null) {
1064 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001065 }
Evan Millar11d628c2009-09-02 08:55:01 -07001066 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1067 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001068 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001069
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001070 /**
1071 * A basic structure with the data for a contact entry in the list.
1072 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001073 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001074 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001075 public boolean isPrimary = false;
1076 public int presenceIcon = -1;
1077 public int secondaryActionIcon = -1;
1078 public Intent intent;
1079 public Intent secondaryIntent = null;
1080 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001081 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001082 public ArrayList<Long> ids = new ArrayList<Long>();
1083 public int collapseCount = 0;
1084
1085 public boolean collapseWith(ViewEntry entry) {
1086 // assert equal collapse keys
1087 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1088 return false;
1089 }
1090
1091 // Choose the label associated with the highest type precedence.
1092 if (TypePrecedence.getTypePrecedence(mimetype, type)
1093 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1094 type = entry.type;
1095 label = entry.label;
1096 }
1097
1098 // Choose the max of the maxLines and maxLabelLines values.
1099 maxLines = Math.max(maxLines, entry.maxLines);
1100 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1101
1102 // Choose the presence with the highest precedence.
1103 if (Presence.getPresencePrecedence(status)
1104 < Presence.getPresencePrecedence(entry.status)) {
1105 status = entry.status;
1106 }
1107
1108 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001109 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001110
1111 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1112
1113 // Keep track of all the ids that have been collapsed with this one.
1114 ids.add(entry.id);
1115 collapseCount++;
1116 return true;
1117 }
1118
1119 public String getCollapseKey() {
1120 StringBuilder hashSb = new StringBuilder();
1121 hashSb.append(data);
1122 hashSb.append(mimetype);
1123 hashSb.append((intent != null && intent.getAction() != null)
1124 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001125 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1126 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001127 hashSb.append(actionIcon);
1128 return hashSb.toString();
1129 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001130 }
1131
Evan Millar15e514d2009-08-04 10:14:57 -07001132 /** Cache of the children views of a row */
1133 static class ViewCache {
1134 public TextView label;
1135 public TextView data;
1136 public ImageView actionIcon;
1137 public ImageView presenceIcon;
1138 public ImageView primaryIcon;
1139 public ImageView secondaryActionButton;
1140 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001141
Evan Millar15e514d2009-08-04 10:14:57 -07001142 // Need to keep track of this too
1143 ViewEntry entry;
1144 }
1145
1146 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1147 implements View.OnClickListener {
1148
Evan Millar5c22c3b2009-05-29 11:37:54 -07001149
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001150 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1151 super(context, sections, SHOW_SEPARATORS);
1152 }
1153
Evan Millar15e514d2009-08-04 10:14:57 -07001154 public void onClick(View v) {
1155 Intent intent = (Intent) v.getTag();
1156 startActivity(intent);
1157 }
1158
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001159 @Override
1160 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001161 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001162 View v;
1163
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001164 ViewCache views;
1165
1166 // Check to see if we can reuse convertView
1167 if (convertView != null) {
1168 v = convertView;
1169 views = (ViewCache) v.getTag();
1170 } else {
1171 // Create a new view if needed
1172 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1173
1174 // Cache the children
1175 views = new ViewCache();
1176 views.label = (TextView) v.findViewById(android.R.id.text1);
1177 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001178 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1179 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1180 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1181 views.secondaryActionButton = (ImageView) v.findViewById(
1182 R.id.secondary_action_button);
1183 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001184 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001185 v.setTag(views);
1186 }
1187
1188 // Update the entry in the view cache
1189 views.entry = entry;
1190
1191 // Bind the data to the view
1192 bindView(v, entry);
1193 return v;
1194 }
1195
1196 @Override
1197 protected View newView(int position, ViewGroup parent) {
1198 // getView() handles this
1199 throw new UnsupportedOperationException();
1200 }
1201
1202 @Override
1203 protected void bindView(View view, ViewEntry entry) {
1204 final Resources resources = mContext.getResources();
1205 ViewCache views = (ViewCache) view.getTag();
1206
1207 // Set the label
1208 TextView label = views.label;
1209 setMaxLines(label, entry.maxLabelLines);
1210 label.setText(entry.label);
1211
1212 // Set the data
1213 TextView data = views.data;
1214 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001215 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001216 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001217 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1218 } else {
1219 data.setText(entry.data);
1220 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001221 setMaxLines(data, entry.maxLines);
1222 }
1223
Evan Millar15e514d2009-08-04 10:14:57 -07001224 // Set the primary icon
1225 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1226
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001227 // Set the action icon
1228 ImageView action = views.actionIcon;
1229 if (entry.actionIcon != -1) {
1230 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1231 action.setVisibility(View.VISIBLE);
1232 } else {
1233 // Things should still line up as if there was an icon, so make it invisible
1234 action.setVisibility(View.INVISIBLE);
1235 }
1236
1237 // Set the presence icon
1238 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001239 if (entry.presenceIcon != -1) {
1240 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001241 } else if (entry.status != -1) {
1242 presenceIcon = resources.getDrawable(
1243 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001244 }
Evan Millar15e514d2009-08-04 10:14:57 -07001245 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001246 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001247 presenceIconView.setImageDrawable(presenceIcon);
1248 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001249 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001250 presenceIconView.setVisibility(View.GONE);
1251 }
1252
1253 // Set the secondary action button
1254 ImageView secondaryActionView = views.secondaryActionButton;
1255 Drawable secondaryActionIcon = null;
1256 if (entry.secondaryActionIcon != -1) {
1257 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1258 }
1259 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1260 secondaryActionView.setImageDrawable(secondaryActionIcon);
1261 secondaryActionView.setTag(entry.secondaryIntent);
1262 secondaryActionView.setVisibility(View.VISIBLE);
1263 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1264 } else {
1265 secondaryActionView.setVisibility(View.GONE);
1266 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001267 }
1268 }
1269
1270 private void setMaxLines(TextView textView, int maxLines) {
1271 if (maxLines == 1) {
1272 textView.setSingleLine(true);
1273 textView.setEllipsize(TextUtils.TruncateAt.END);
1274 } else {
1275 textView.setSingleLine(false);
1276 textView.setMaxLines(maxLines);
1277 textView.setEllipsize(null);
1278 }
1279 }
1280 }
1281}