blob: 9b22b4b101085c30437a689a72ab03892543c74d [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 Millar51514db2009-09-22 11:42:35 -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);
Evan Millar51514db2009-09-22 11:42:35 -0700204// mTabWidget.setTabSelectionListener(this);
Evan Millar85863112009-09-21 14:35:18 -0700205 mTabWidget.setVisibility(View.GONE);
Evan Millar11d628c2009-09-02 08:55:01 -0700206
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;
Evan Millar11d628c2009-09-02 08:55:01 -0700234 }
235
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800236 @Override
237 protected void onResume() {
238 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700239 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800240 }
241
242 @Override
243 protected void onPause() {
244 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700245 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800246 }
247
248 @Override
249 protected void onDestroy() {
250 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700251 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800252 }
253
254 @Override
Evan Millar11d628c2009-09-02 08:55:01 -0700255 protected void onRestoreInstanceState(Bundle savedInstanceState) {
256 super.onRestoreInstanceState(savedInstanceState);
Evan Millar51514db2009-09-22 11:42:35 -0700257// mSelectedRawContactId = savedInstanceState.getLong(SELECTED_RAW_CONTACT_ID_KEY);
Evan Millar11d628c2009-09-02 08:55:01 -0700258 }
259
260 @Override
261 protected void onSaveInstanceState(Bundle outState) {
262 super.onSaveInstanceState(outState);
Megha Joshiec63f902009-09-22 18:47:39 -0700263 if (mSelectedRawContactId != null) {
264 outState.putLong(SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
265 }
Evan Millar11d628c2009-09-02 08:55:01 -0700266 }
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 */
Evan Millar51514db2009-09-22 11:42:35 -0700291// protected void bindTabs() {
292// if (mEntities.size() > 1) {
293// addAllTab();
294// }
295//
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(),
308// source));
309// }
310// }
311//
312// /**
313// * Add a tab to be displayed in the {@link ScrollingTabWidget}.
314// *
315// * @param rawContactId The contact id associated with the tab.
316// * @param view A view to use as the tab indicator.
317// */
318// protected void addTab(long rawContactId, View view) {
319// mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId);
320// mTabWidget.addTab(view);
321// }
322//
323//
324// protected void clearCurrentTabs() {
325// mTabRawContactIdMap.clear();
326// mTabWidget.removeAllTabs();
327// }
328//
329// protected void selectInitialTab() {
330// int selectedTabIndex = 0;
331//
332// if (mSelectedRawContactId != null) {
333// selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
334// if (selectedTabIndex == -1) {
335// // If there was no matching tab, just select the first;
336// selectedTabIndex = 0;
337// }
338// }
339//
340//// mTabWidget.setCurrentTab(selectedTabIndex);
341// onTabSelectionChanged(selectedTabIndex, false);
342//// mTabWidget.setVisibility(View.VISIBLE);
343//// mTabWidget.postInvalidate();
344// }
345//
346// private void addAllTab() {
347// View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator,
348// mTabWidget.getTabParent(), false);
349// allTabIndicator.getBackground().setDither(true);
350// addTab(ALL_CONTACTS_ID, allTabIndicator);
351// }
352//
353// public void onTabSelectionChanged(int tabIndex, boolean clicked) {
354// Long rawContactId = getTabRawContactId(tabIndex);
355// if (rawContactId != null) {
356// mSelectedRawContactId = rawContactId;
357// bindData();
358// }
359// }
360//
361// /**
362// * Return the RawContact id associated with the tab at an index.
363// *
364// * @param index The index of the tab in question.
365// * @return The contactId associated with the tab at the specified index.
366// */
367// protected Long getTabRawContactId(int index) {
368// return mTabRawContactIdMap.get(index);
369// }
370//
371// /**
372// * Return the tab index associated with the RawContact id.
373// *
374// * @param index The index of the tab in question.
375// * @return The contactId associated with the tab at the specified index.
376// */
377// protected int getTabIndexForRawContactId(long rawContactId) {
378// int numTabs = mTabRawContactIdMap.size();
379// for (int i=0; i < numTabs; i++) {
380// if (mTabRawContactIdMap.get(i) == rawContactId) {
381// return i;
382// }
383// }
384// return -1;
385// }
Evan Millar11d628c2009-09-02 08:55:01 -0700386
387
388 // QUERY CODE //
389 /** {@inheritDoc} */
390 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
391 try{
392 if (token == TOKEN_QUERY) {
Evan Millar85863112009-09-21 14:35:18 -0700393// clearCurrentTabs();
Evan Millar11d628c2009-09-02 08:55:01 -0700394 mEntities = readEntities(iterator);
Evan Millar85863112009-09-21 14:35:18 -0700395 // Show the aggregate badge if this contact is aggregated.
Evan Millarcc87fb92009-09-22 15:27:47 -0700396 boolean isAggregate = mEntities.size() > 1;
397 mContactHeaderWidget.showAggregateBadge(isAggregate);
Evan Millar85863112009-09-21 14:35:18 -0700398// bindTabs();
Evan Millar51514db2009-09-22 11:42:35 -0700399// selectInitialTab();
Evan Millar85863112009-09-21 14:35:18 -0700400 bindData();
Evan Millar11d628c2009-09-02 08:55:01 -0700401 }
402 } finally {
403 if (iterator != null) {
404 iterator.close();
405 }
406 }
407 }
408
409 /** {@inheritDoc} */
410 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700411 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700412 }
413
414 private ArrayList<Entity> readEntities(EntityIterator iterator) {
415 ArrayList<Entity> entities = new ArrayList<Entity>();
416 try {
417 while (iterator.hasNext()) {
418 entities.add(iterator.next());
419 }
420 } catch (RemoteException e) {
421 }
422
423 return entities;
424 }
425
426 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700427 closeCursor();
428
Dmitri Plotnikov0306bbd2009-09-03 14:37:15 -0700429 mUri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700430 if (mLookupUri != null) {
431 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
432 if (mLookupUri != null) {
433 mUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
434 }
435 }
436
437 if (mUri == null) {
438
439 // TODO either figure out a way to prevent a flash of black background or
440 // use some other UI than a toast
441 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
442 Log.e(TAG, "invalid contact uri: " + mLookupUri);
443 finish();
444 return;
445 }
446
447 mCursor = mResolver.query(Uri.withAppendedPath(mUri, Contacts.Data.CONTENT_DIRECTORY),
448 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
449 mCursor.registerContentObserver(mObserver);
450
Evan Millar11d628c2009-09-02 08:55:01 -0700451 long contactId = ContentUris.parseId(mUri);
452 mHandler.startQueryEntities(TOKEN_QUERY, null,
453 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700454
Evan Millar9b1a1242009-09-16 10:26:10 -0700455 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700456 }
457
458 private void closeCursor() {
459 if (mCursor != null) {
460 mCursor.unregisterContentObserver(mObserver);
461 mCursor.close();
462 mCursor = null;
463 }
Evan Millar11d628c2009-09-02 08:55:01 -0700464 }
465
466 private void bindData() {
467
468 // Build up the contact entries
469 buildEntries();
470
471 // Collapse similar data items in select sections.
472 Collapser.collapseList(mPhoneEntries);
473 Collapser.collapseList(mSmsEntries);
474 Collapser.collapseList(mEmailEntries);
475 Collapser.collapseList(mPostalEntries);
476
477 if (mAdapter == null) {
478 mAdapter = new ViewAdapter(this, mSections);
479 mListView.setAdapter(mAdapter);
480 } else {
481 mAdapter.setSections(mSections, SHOW_SEPARATORS);
482 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800483 }
484
485 @Override
486 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700487 super.onCreateOptionsMenu(menu);
488
489 final MenuInflater inflater = getMenuInflater();
490 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800491 return true;
492 }
493
494 @Override
495 public boolean onPrepareOptionsMenu(Menu menu) {
496 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700497
Evan Millar51514db2009-09-22 11:42:35 -0700498 // Only allow edit when we have at least one raw_contact id
499 final boolean hasRawContact = (mRawContactIds.size() > 0);
500 menu.findItem(R.id.menu_split).setEnabled(hasRawContact);
Evan Millardb5d88c2009-08-28 09:31:57 -0700501
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700502 // Only allow split when more than one contact
503 final boolean isAggregate = (mRawContactIds.size() > 1);
504 menu.findItem(R.id.menu_split).setEnabled(isAggregate);
505
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800506 return true;
507 }
508
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800509 @Override
510 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
511 AdapterView.AdapterContextMenuInfo info;
512 try {
513 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
514 } catch (ClassCastException e) {
515 Log.e(TAG, "bad menuInfo", e);
516 return;
517 }
518
519 // This can be null sometimes, don't crash...
520 if (info == null) {
521 Log.e(TAG, "bad menuInfo");
522 return;
523 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700524
Evan Millar45e0ed32009-06-01 16:44:38 -0700525 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
526 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
527 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700528 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
529 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700530 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800531 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700532 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
533 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700534 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700535 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800536 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700537 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700538 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
539 }
540 // TODO(emillar): add back with group support.
541 /* else if (entry.mimetype.equals()) {
542 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
543 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800544 }
545
546 @Override
547 public boolean onOptionsItemSelected(MenuItem item) {
548 switch (item.getItemId()) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700549 case R.id.menu_edit: {
Evan Millar51514db2009-09-22 11:42:35 -0700550// Long rawContactIdToEdit = mSelectedRawContactId;
551// if (rawContactIdToEdit == null) {
552// // This shouldn't be possible. We only show the edit option if
553// // this value is non-null.
554// break;
555// }
556// if (rawContactIdToEdit == ALL_CONTACTS_ID) {
557// // If the "all" tab is selected, edit the next tab.
558// rawContactIdToEdit = getTabRawContactId(mTabWidget.getCurrentTab() + 1);
559// }
560// Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
561// rawContactIdToEdit);
562// startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
563// REQUEST_EDIT_CONTACT);
564 if (mRawContactIds.size() > 0) {
565 long rawContactIdToEdit = mRawContactIds.get(0);
566 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
567 rawContactIdToEdit);
568 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
569 REQUEST_EDIT_CONTACT);
Evan Millardb5d88c2009-08-28 09:31:57 -0700570 }
Evan Millar8a79cee2009-08-19 17:20:49 -0700571 break;
572 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700573 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800574 // Get confirmation
575 showDialog(DIALOG_CONFIRM_DELETE);
576 return true;
577 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700578 case R.id.menu_split: {
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700579 if (mRawContactIds.size() == 2) {
580 splitContact(mRawContactIds.get(1));
581 } else {
582 showSplitAggregateDialog();
583 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700584 return true;
585 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700586 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700587 showJoinAggregateActivity();
588 return true;
589 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700590 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700591 showOptionsActivity();
592 return true;
593 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700594 case R.id.menu_share: {
595 final Intent intent = new Intent(Intent.ACTION_SEND);
596 intent.setType(Contacts.CONTENT_ITEM_TYPE);
597 intent.putExtra(Intent.EXTRA_STREAM, mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700598
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700599 // Launch chooser to share contact via
600 final CharSequence chooseTitle = getText(R.string.share_via);
601 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700602
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700603 try {
604 startActivity(chooseIntent);
605 } catch (ActivityNotFoundException ex) {
606 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800607 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700608 return true;
609 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800610 }
611 return super.onOptionsItemSelected(item);
612 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700613
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800614 @Override
615 public boolean onContextItemSelected(MenuItem item) {
616 switch (item.getItemId()) {
617 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700618 if (makeItemDefault(item)) {
619 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800620 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700621 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800622 }
623 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700624
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800625 return super.onContextItemSelected(item);
626 }
627
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700628 private boolean makeItemDefault(MenuItem item) {
629 ViewEntry entry = getViewEntryForMenuItem(item);
630 if (entry == null) {
631 return false;
632 }
633
634 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700635 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700636 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700637 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
638 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700639 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700640 return true;
641 }
642
643 /**
644 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
645 * The user picks a contact to be split into its own aggregate or clicks Cancel.
646 */
647 private void showSplitAggregateDialog() {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700648 // Wrap this dialog in a specific theme so that list items have correct text color.
649 final ContextThemeWrapper dialogContext =
650 new ContextThemeWrapper(this, android.R.style.Theme_Light);
651 AlertDialog.Builder builder =
652 new AlertDialog.Builder(dialogContext);
653 builder.setTitle(getString(R.string.splitAggregate_title));
654
655 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
656 builder.setView(view);
657
658 builder.setInverseBackgroundForced(true);
659 builder.setCancelable(true);
660 builder.setNegativeButton(android.R.string.cancel,
661 new OnClickListener() {
662 public void onClick(DialogInterface dialog, int which) {
663 dialog.dismiss();
664 }
665 });
666 final AlertDialog dialog = builder.create();
667
668 view.setOnContactSelectedListener(new OnContactSelectedListener() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700669 public void onContactSelected(long rawContactId) {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700670 dialog.dismiss();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700671 splitContact(rawContactId);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700672 }
673 });
674
675 dialog.show();
676 }
677
678 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700679 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700680 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700681 public void showJoinAggregateActivity() {
682 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
683 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700684 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700685 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700686
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700687 @Override
688 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700689 switch (requestCode) {
690 case REQUEST_JOIN_CONTACT: {
691 if (resultCode == RESULT_OK && intent != null) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700692 final long contactId = ContentUris.parseId(intent.getData());
693 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700694 }
695 break;
696 }
Evan Millar51514db2009-09-22 11:42:35 -0700697// case REQUEST_EDIT_CONTACT: {
698// if (resultCode == RESULT_OK && intent != null) {
699// long newInitialSelectedRawContactId = intent.getLongExtra(
700// RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
701// if (newInitialSelectedRawContactId != mSelectedRawContactId) {
702// mSelectedRawContactId = newInitialSelectedRawContactId;
703// }
704// }
705// }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700706 }
707 }
708
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700709 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700710 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700711
712 // The split operation may have removed the original aggregate contact, so we need
713 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700714 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700715 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700716 }
717
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700718 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700719 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700720 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700721
722 try {
723 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700724 long rawContactId = c.getLong(0);
725 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700726 }
727 } finally {
728 c.close();
729 }
730
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700731 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
732 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700733 }
734
735 /**
736 * Given a contact ID sets an aggregation exception to either join the contact with the
737 * current aggregate or split off.
738 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700739 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700740 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700741 for (long aRawContactId : mRawContactIds) {
742 if (aRawContactId != rawContactId) {
743 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
744 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
745 values.put(AggregationExceptions.TYPE, exceptionType);
746 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
747 }
748 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700749 }
750
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700751 private void showOptionsActivity() {
752 final Intent intent = new Intent(this, ContactOptionsActivity.class);
753 intent.setData(mUri);
754 startActivity(intent);
755 }
756
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700757 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
758 AdapterView.AdapterContextMenuInfo info;
759 try {
760 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
761 } catch (ClassCastException e) {
762 Log.e(TAG, "bad menuInfo", e);
763 return null;
764 }
765
766 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
767 }
768
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800769 @Override
770 public boolean onKeyDown(int keyCode, KeyEvent event) {
771 switch (keyCode) {
772 case KeyEvent.KEYCODE_CALL: {
773 try {
774 ITelephony phone = ITelephony.Stub.asInterface(
775 ServiceManager.checkService("phone"));
776 if (phone != null && !phone.isIdle()) {
777 // Skip out and let the key be handled at a higher level
778 break;
779 }
780 } catch (RemoteException re) {
781 // Fall through and try to call the contact
782 }
783
Evan Millar7911ff52009-07-21 15:55:18 -0700784 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800785 if (index != -1) {
786 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700787 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
788 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800789 }
790 } else if (mNumPhoneNumbers != 0) {
791 // There isn't anything selected, call the default number
792 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
793 startActivity(intent);
794 }
795 return true;
796 }
797
798 case KeyEvent.KEYCODE_DEL: {
799 showDialog(DIALOG_CONFIRM_DELETE);
800 return true;
801 }
802 }
803
804 return super.onKeyDown(keyCode, event);
805 }
806
Evan Millar7911ff52009-07-21 15:55:18 -0700807 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800808 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
809 if (entry != null) {
810 Intent intent = entry.intent;
811 if (intent != null) {
812 try {
813 startActivity(intent);
814 } catch (ActivityNotFoundException e) {
815 Log.e(TAG, "No activity found for intent: " + intent);
816 signalError();
817 }
818 } else {
819 signalError();
820 }
821 } else {
822 signalError();
823 }
824 }
825
826 /**
827 * Signal an error to the user via a beep, or some other method.
828 */
829 private void signalError() {
830 //TODO: implement this when we have the sonification APIs
831 }
832
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800833 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700834 // 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 -0800835 StringBuilder buf = new StringBuilder("imto://");
836 buf.append(host);
837 buf.append('/');
838 buf.append(data);
839 return Uri.parse(buf.toString());
840 }
841
842 /**
843 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700844 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800845 * @param personCursor the URI for the contact being displayed
846 */
Evan Millar11d628c2009-09-02 08:55:01 -0700847 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800848 // Clear out the old entries
849 final int numSections = mSections.size();
850 for (int i = 0; i < numSections; i++) {
851 mSections.get(i).clear();
852 }
853
Evan Millar7911ff52009-07-21 15:55:18 -0700854 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700855
Evan Millar11d628c2009-09-02 08:55:01 -0700856 Sources sources = Sources.getInstance(this);
857
Evan Millar66388be2009-05-28 15:41:07 -0700858 // Build up method entries
859 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700860 for (Entity entity: mEntities) {
861 final ContentValues entValues = entity.getEntityValues();
862 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700863 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700864
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700865 if (!mRawContactIds.contains(rawContactId)) {
866 mRawContactIds.add(rawContactId);
867 }
868
869
Evan Millar51514db2009-09-22 11:42:35 -0700870// // This performs the tab filtering
871// if (mSelectedRawContactId != null
872// && mSelectedRawContactId != rawContactId
873// && mSelectedRawContactId != ALL_CONTACTS_ID) {
874// continue;
875// }
Evan Millar66210452009-09-21 10:53:43 -0700876
Evan Millar11d628c2009-09-02 08:55:01 -0700877 for (NamedContentValues subValue : entity.getSubValues()) {
878 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800879
Jeff Sharkeyab066932009-09-21 09:55:30 -0700880 final ContentValues entryValues = subValue.values;
881 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
882
Evan Millar11d628c2009-09-02 08:55:01 -0700883 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700884 if (mimetype == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -0700885
Jeff Sharkeyab066932009-09-21 09:55:30 -0700886 final DataKind kind = sources.getKindOrFallback(accountType, mimetype, this,
Evan Millar11d628c2009-09-02 08:55:01 -0700887 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700888 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800889
Evan Millar11d628c2009-09-02 08:55:01 -0700890 final long id = entryValues.getAsLong(Data._ID);
891 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700892 entry.contactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700893 entry.id = id;
894 entry.uri = uri;
895 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -0700896 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -0700897 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700898 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700899 entry.type = entryValues.getAsInteger(kind.typeColumn);
900 }
901 if (kind.iconRes > 0) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700902 entry.resPackageName = kind.resPackageName;
Evan Millar11d628c2009-09-02 08:55:01 -0700903 entry.actionIcon = kind.iconRes;
904 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800905
Evan Millar11d628c2009-09-02 08:55:01 -0700906 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700907 if (TextUtils.isEmpty(entry.data)) {
Alex Kennberg87fc3172009-03-28 06:43:06 -0700908 continue;
909 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700910
Evan Millar11d628c2009-09-02 08:55:01 -0700911 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)
912 || CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)
913 || CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)
914 || CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
915 final boolean isSuperPrimary = entryValues.getAsInteger(
916 Data.IS_SUPER_PRIMARY) != 0;
917
918 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
919 // Build phone entries
920 mNumPhoneNumbers++;
921
922 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
923 Uri.fromParts("tel", entry.data, null));
924 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
925 Uri.fromParts("sms", entry.data, null));
926 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700927
928 // If data is empty, don't show it.
929 if (TextUtils.isEmpty(entry.data)) {
930 Log.w(TAG, "empty data for contact method " + id);
931 continue;
932 }
933
Evan Millar11d628c2009-09-02 08:55:01 -0700934 entry.isPrimary = isSuperPrimary;
935 mPhoneEntries.add(entry);
936
937 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
938 || mShowSmsLinksForAllPhones) {
939 // Add an SMS entry
940 if (kind.iconAltRes > 0) {
941 entry.secondaryActionIcon = kind.iconAltRes;
942 }
943 }
944 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
945 // Build email entries
946 entry.intent = new Intent(Intent.ACTION_SENDTO,
947 Uri.fromParts("mailto", entry.data, null));
948 entry.isPrimary = isSuperPrimary;
949 mEmailEntries.add(entry);
950 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
951 equals(mimetype)) {
952 // Build postal entries
953 entry.maxLines = 4;
954 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
955 mPostalEntries.add(entry);
956 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
957 // Build im entries
958 Object protocolObj = entryValues.getAsInteger(Data.DATA5);
959 String host = null;
960
Evan Millar49714ee2009-09-02 16:42:47 -0700961 if (TextUtils.isEmpty(entry.label)) {
Jeff Sharkeyae6c2882009-09-21 23:09:35 -0700962 entry.label = getString(R.string.chat).toLowerCase();
Evan Millar49714ee2009-09-02 16:42:47 -0700963 }
964
Evan Millar11d628c2009-09-02 08:55:01 -0700965 if (protocolObj instanceof Number) {
966 int protocol = ((Number) protocolObj).intValue();
Vasu Nori8eecbe62009-09-22 17:23:38 -0700967 host = ContactsUtils.lookupProviderNameFromId(protocol);
Evan Millar11d628c2009-09-02 08:55:01 -0700968 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
Vasu Nori8eecbe62009-09-22 17:23:38 -0700978 // host is null for CommonDataKinds.Im.PROTOCOL_CUSTOM
Evan Millar11d628c2009-09-02 08:55:01 -0700979 if (!TextUtils.isEmpty(host)) {
980 entry.intent = new Intent(Intent.ACTION_SENDTO,
Vasu Nori8eecbe62009-09-22 17:23:38 -0700981 constructImToUrl(host.toLowerCase(), entry.data));
Evan Millar11d628c2009-09-02 08:55:01 -0700982 }
983 //TODO(emillar) Add in presence info
984 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
985 entry.presenceIcon = Presence.getPresenceIconResourceId(
986 aggCursor.getInt(METHODS_STATUS_COLUMN));
987 entry.status = ...
988 }*/
989 mImEntries.add(entry);
990 }
991 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
992 // Build organization entries
993 mOrganizationEntries.add(entry);
994 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
995 // Build note entries
996 entry.id = 0;
997 entry.uri = null;
998 entry.intent = null;
999 entry.maxLines = 10;
1000 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -07001001 } else {
1002 // Handle showing custom
1003 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
1004 mOtherEntries.add(entry);
Evan Millar11d628c2009-09-02 08:55:01 -07001005 }
1006
Evan Millar11d628c2009-09-02 08:55:01 -07001007 // TODO(emillar) Add group entries
1008 // // Build the group entries
1009 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1010 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1011 // null, null, Groups.DEFAULT_SORT_ORDER);
1012 // if (groupCursor != null) {
1013 // try {
1014 // StringBuilder sb = new StringBuilder();
1015 //
1016 // while (groupCursor.moveToNext()) {
1017 // String systemId = groupCursor.getString(
1018 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1019 //
1020 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1021 // continue;
1022 // }
1023 //
1024 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1025 // if (!TextUtils.isEmpty(name)) {
1026 // if (sb.length() == 0) {
1027 // sb.append(name);
1028 // } else {
1029 // sb.append(getString(R.string.group_list, name));
1030 // }
1031 // }
1032 // }
1033 //
1034 // if (sb.length() > 0) {
1035 // ViewEntry entry = new ViewEntry();
1036 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1037 // entry.label = getString(R.string.label_groups);
1038 // entry.data = sb.toString();
1039 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1040 //
1041 // // TODO: Add an icon for the groups item.
1042 //
1043 // mGroupEntries.add(entry);
1044 // }
1045 // } finally {
1046 // groupCursor.close();
1047 // }
1048 // }
Evan Millar66388be2009-05-28 15:41:07 -07001049 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001050
Evan Millar66388be2009-05-28 15:41:07 -07001051 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001052 }
1053 }
1054
Evan Millar11d628c2009-09-02 08:55:01 -07001055 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1056 if (kind.actionHeader == null) {
1057 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001058 }
Evan Millar11d628c2009-09-02 08:55:01 -07001059 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1060 if (actionHeader == null) {
1061 return null;
1062 }
1063 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1064 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001065
Evan Millar11d628c2009-09-02 08:55:01 -07001066 String buildDataString(DataKind kind, ContentValues values) {
1067 if (kind.actionBody == null) {
1068 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001069 }
Evan Millar11d628c2009-09-02 08:55:01 -07001070 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1071 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001072 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001073
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001074 /**
1075 * A basic structure with the data for a contact entry in the list.
1076 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001077 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001078 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001079 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001080 public boolean isPrimary = false;
1081 public int presenceIcon = -1;
1082 public int secondaryActionIcon = -1;
1083 public Intent intent;
1084 public Intent secondaryIntent = null;
1085 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001086 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001087 public ArrayList<Long> ids = new ArrayList<Long>();
1088 public int collapseCount = 0;
1089
1090 public boolean collapseWith(ViewEntry entry) {
1091 // assert equal collapse keys
1092 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1093 return false;
1094 }
1095
1096 // Choose the label associated with the highest type precedence.
1097 if (TypePrecedence.getTypePrecedence(mimetype, type)
1098 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1099 type = entry.type;
1100 label = entry.label;
1101 }
1102
1103 // Choose the max of the maxLines and maxLabelLines values.
1104 maxLines = Math.max(maxLines, entry.maxLines);
1105 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1106
1107 // Choose the presence with the highest precedence.
1108 if (Presence.getPresencePrecedence(status)
1109 < Presence.getPresencePrecedence(entry.status)) {
1110 status = entry.status;
1111 }
1112
1113 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001114 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001115
1116 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1117
1118 // Keep track of all the ids that have been collapsed with this one.
1119 ids.add(entry.id);
1120 collapseCount++;
1121 return true;
1122 }
1123
1124 public String getCollapseKey() {
1125 StringBuilder hashSb = new StringBuilder();
1126 hashSb.append(data);
1127 hashSb.append(mimetype);
1128 hashSb.append((intent != null && intent.getAction() != null)
1129 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001130 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1131 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001132 hashSb.append(actionIcon);
1133 return hashSb.toString();
1134 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001135 }
1136
Evan Millar15e514d2009-08-04 10:14:57 -07001137 /** Cache of the children views of a row */
1138 static class ViewCache {
1139 public TextView label;
1140 public TextView data;
1141 public ImageView actionIcon;
1142 public ImageView presenceIcon;
1143 public ImageView primaryIcon;
1144 public ImageView secondaryActionButton;
1145 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001146
Evan Millar15e514d2009-08-04 10:14:57 -07001147 // Need to keep track of this too
1148 ViewEntry entry;
1149 }
1150
1151 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1152 implements View.OnClickListener {
1153
Evan Millar5c22c3b2009-05-29 11:37:54 -07001154
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001155 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1156 super(context, sections, SHOW_SEPARATORS);
1157 }
1158
Evan Millar15e514d2009-08-04 10:14:57 -07001159 public void onClick(View v) {
1160 Intent intent = (Intent) v.getTag();
1161 startActivity(intent);
1162 }
1163
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001164 @Override
1165 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001166 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001167 View v;
1168
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001169 ViewCache views;
1170
1171 // Check to see if we can reuse convertView
1172 if (convertView != null) {
1173 v = convertView;
1174 views = (ViewCache) v.getTag();
1175 } else {
1176 // Create a new view if needed
1177 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1178
1179 // Cache the children
1180 views = new ViewCache();
1181 views.label = (TextView) v.findViewById(android.R.id.text1);
1182 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001183 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1184 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1185 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1186 views.secondaryActionButton = (ImageView) v.findViewById(
1187 R.id.secondary_action_button);
1188 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001189 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001190 v.setTag(views);
1191 }
1192
1193 // Update the entry in the view cache
1194 views.entry = entry;
1195
1196 // Bind the data to the view
1197 bindView(v, entry);
1198 return v;
1199 }
1200
1201 @Override
1202 protected View newView(int position, ViewGroup parent) {
1203 // getView() handles this
1204 throw new UnsupportedOperationException();
1205 }
1206
1207 @Override
1208 protected void bindView(View view, ViewEntry entry) {
1209 final Resources resources = mContext.getResources();
1210 ViewCache views = (ViewCache) view.getTag();
1211
1212 // Set the label
1213 TextView label = views.label;
1214 setMaxLines(label, entry.maxLabelLines);
1215 label.setText(entry.label);
1216
1217 // Set the data
1218 TextView data = views.data;
1219 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001220 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001221 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001222 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1223 } else {
1224 data.setText(entry.data);
1225 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001226 setMaxLines(data, entry.maxLines);
1227 }
1228
Evan Millar15e514d2009-08-04 10:14:57 -07001229 // Set the primary icon
1230 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1231
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001232 // Set the action icon
1233 ImageView action = views.actionIcon;
1234 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001235 Drawable actionIcon;
1236 if (entry.resPackageName != null) {
1237 // Load external resources through PackageManager
1238 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1239 entry.actionIcon, null);
1240 } else {
1241 actionIcon = resources.getDrawable(entry.actionIcon);
1242 }
1243 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001244 action.setVisibility(View.VISIBLE);
1245 } else {
1246 // Things should still line up as if there was an icon, so make it invisible
1247 action.setVisibility(View.INVISIBLE);
1248 }
1249
1250 // Set the presence icon
1251 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001252 if (entry.presenceIcon != -1) {
1253 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001254 } else if (entry.status != -1) {
1255 presenceIcon = resources.getDrawable(
1256 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001257 }
Evan Millar15e514d2009-08-04 10:14:57 -07001258 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001259 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001260 presenceIconView.setImageDrawable(presenceIcon);
1261 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001262 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001263 presenceIconView.setVisibility(View.GONE);
1264 }
1265
1266 // Set the secondary action button
1267 ImageView secondaryActionView = views.secondaryActionButton;
1268 Drawable secondaryActionIcon = null;
1269 if (entry.secondaryActionIcon != -1) {
1270 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1271 }
1272 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1273 secondaryActionView.setImageDrawable(secondaryActionIcon);
1274 secondaryActionView.setTag(entry.secondaryIntent);
1275 secondaryActionView.setVisibility(View.VISIBLE);
1276 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1277 } else {
1278 secondaryActionView.setVisibility(View.GONE);
1279 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001280 }
1281 }
1282
1283 private void setMaxLines(TextView textView, int maxLines) {
1284 if (maxLines == 1) {
1285 textView.setSingleLine(true);
1286 textView.setEllipsize(TextUtils.TruncateAt.END);
1287 } else {
1288 textView.setSingleLine(false);
1289 textView.setMaxLines(maxLines);
1290 textView.setEllipsize(null);
1291 }
1292 }
1293 }
1294}