blob: ad623cac611e487da1c95650d07bd4b697ef7d34 [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);
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);
257 mSelectedRawContactId = savedInstanceState.getLong(SELECTED_RAW_CONTACT_ID_KEY);
258 }
259
260 @Override
261 protected void onSaveInstanceState(Bundle outState) {
262 super.onSaveInstanceState(outState);
263 outState.putLong(SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
264 }
265
266 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800267 protected Dialog onCreateDialog(int id) {
268 switch (id) {
269 case DIALOG_CONFIRM_DELETE:
270 return new AlertDialog.Builder(this)
271 .setTitle(R.string.deleteConfirmation_title)
272 .setIcon(android.R.drawable.ic_dialog_alert)
273 .setMessage(R.string.deleteConfirmation)
274 .setNegativeButton(android.R.string.cancel, null)
275 .setPositiveButton(android.R.string.ok, this)
276 .setCancelable(false)
277 .create();
278 }
279 return null;
280 }
281
Evan Millar11d628c2009-09-02 08:55:01 -0700282
283 // TAB CODE //
284 /**
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700285 * Adds a tab for each {@link RawContacts} associated with this contact.
Evan Millar11d628c2009-09-02 08:55:01 -0700286 * Override this method if you want to additional tabs and/or different
287 * tabs for your activity.
Evan Millar11d628c2009-09-02 08:55:01 -0700288 */
289 protected void bindTabs() {
290 if (mEntities.size() > 1) {
Evan Millar7911ff52009-07-21 15:55:18 -0700291 addAllTab();
292 }
Evan Millar11d628c2009-09-02 08:55:01 -0700293
294 final Sources sources = Sources.getInstance(this);
295
296 for (Entity entity : mEntities) {
297 final String accountType = entity.getEntityValues().
298 getAsString(RawContacts.ACCOUNT_TYPE);
299 final Long rawContactId = entity.getEntityValues().
300 getAsLong(RawContacts._ID);
301
302 // TODO: ensure inflation on background task so we don't block UI thread here
303 final ContactsSource source = sources.getInflatedSource(accountType,
304 ContactsSource.LEVEL_SUMMARY);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700305 addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(),
306 source));
Evan Millar11d628c2009-09-02 08:55:01 -0700307 }
Evan Millar11d628c2009-09-02 08:55:01 -0700308 }
309
310 /**
311 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
312 *
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700313 * @param rawContactId The contact id associated with the tab.
Evan Millar11d628c2009-09-02 08:55:01 -0700314 * @param view A view to use as the tab indicator.
315 */
316 protected void addTab(long rawContactId, View view) {
317 mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId);
318 mTabWidget.addTab(view);
319 }
320
321
322 protected void clearCurrentTabs() {
323 mTabRawContactIdMap.clear();
324 mTabWidget.removeAllTabs();
325 }
326
327 protected void selectInitialTab() {
328 int selectedTabIndex = 0;
329
330 if (mSelectedRawContactId != null) {
331 selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
332 if (selectedTabIndex == -1) {
333 // If there was no matching tab, just select the first;
334 selectedTabIndex = 0;
335 }
336 }
337
338 mTabWidget.setCurrentTab(selectedTabIndex);
339 onTabSelectionChanged(selectedTabIndex, false);
Evan Millar66210452009-09-21 10:53:43 -0700340 mTabWidget.setVisibility(View.VISIBLE);
341 mTabWidget.postInvalidate();
Evan Millar11d628c2009-09-02 08:55:01 -0700342 }
343
Evan Millar7911ff52009-07-21 15:55:18 -0700344 private void addAllTab() {
Evan Millar56d2caa2009-08-20 20:30:12 -0700345 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator,
346 mTabWidget.getTabParent(), false);
Evan Millar74660912009-08-19 17:36:33 -0700347 allTabIndicator.getBackground().setDither(true);
Evan Millar7911ff52009-07-21 15:55:18 -0700348 addTab(ALL_CONTACTS_ID, allTabIndicator);
349 }
350
351 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
Evan Millar66210452009-09-21 10:53:43 -0700352 Long rawContactId = getTabRawContactId(tabIndex);
353 if (rawContactId != null) {
354 mSelectedRawContactId = rawContactId;
355 bindData();
356 }
Evan Millar7911ff52009-07-21 15:55:18 -0700357 }
358
Evan Millar11d628c2009-09-02 08:55:01 -0700359 /**
360 * Return the RawContact id associated with the tab at an index.
361 *
362 * @param index The index of the tab in question.
363 * @return The contactId associated with the tab at the specified index.
364 */
Evan Millar66210452009-09-21 10:53:43 -0700365 protected Long getTabRawContactId(int index) {
Evan Millar11d628c2009-09-02 08:55:01 -0700366 return mTabRawContactIdMap.get(index);
367 }
Evan Millar2c1cc832009-07-13 11:08:06 -0700368
Evan Millar11d628c2009-09-02 08:55:01 -0700369 /**
370 * Return the tab index associated with the RawContact id.
371 *
372 * @param index The index of the tab in question.
373 * @return The contactId associated with the tab at the specified index.
374 */
375 protected int getTabIndexForRawContactId(long rawContactId) {
376 int numTabs = mTabRawContactIdMap.size();
377 for (int i=0; i < numTabs; i++) {
378 if (mTabRawContactIdMap.get(i) == rawContactId) {
379 return i;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800380 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800381 }
Evan Millar11d628c2009-09-02 08:55:01 -0700382 return -1;
383 }
384
385
386 // QUERY CODE //
387 /** {@inheritDoc} */
388 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
389 try{
390 if (token == TOKEN_QUERY) {
Evan Millar85863112009-09-21 14:35:18 -0700391// clearCurrentTabs();
Evan Millar11d628c2009-09-02 08:55:01 -0700392 mEntities = readEntities(iterator);
Evan Millar85863112009-09-21 14:35:18 -0700393 // Show the aggregate badge if this contact is aggregated.
394 if (mEntities.size() > 1) {
395 mContactHeaderWidget.showAggregateBadge(true);
396 }
397// bindTabs();
398 bindData();
Evan Millar11d628c2009-09-02 08:55:01 -0700399 }
400 } finally {
401 if (iterator != null) {
402 iterator.close();
403 }
404 }
405 }
406
407 /** {@inheritDoc} */
408 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700409 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700410 }
411
412 private ArrayList<Entity> readEntities(EntityIterator iterator) {
413 ArrayList<Entity> entities = new ArrayList<Entity>();
414 try {
415 while (iterator.hasNext()) {
416 entities.add(iterator.next());
417 }
418 } catch (RemoteException e) {
419 }
420
421 return entities;
422 }
423
424 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700425 closeCursor();
426
Dmitri Plotnikov0306bbd2009-09-03 14:37:15 -0700427 mUri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700428 if (mLookupUri != null) {
429 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
430 if (mLookupUri != null) {
431 mUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
432 }
433 }
434
435 if (mUri == null) {
436
437 // TODO either figure out a way to prevent a flash of black background or
438 // use some other UI than a toast
439 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
440 Log.e(TAG, "invalid contact uri: " + mLookupUri);
441 finish();
442 return;
443 }
444
445 mCursor = mResolver.query(Uri.withAppendedPath(mUri, Contacts.Data.CONTENT_DIRECTORY),
446 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
447 mCursor.registerContentObserver(mObserver);
448
Evan Millar11d628c2009-09-02 08:55:01 -0700449 long contactId = ContentUris.parseId(mUri);
450 mHandler.startQueryEntities(TOKEN_QUERY, null,
451 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700452
Evan Millar9b1a1242009-09-16 10:26:10 -0700453 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700454 }
455
456 private void closeCursor() {
457 if (mCursor != null) {
458 mCursor.unregisterContentObserver(mObserver);
459 mCursor.close();
460 mCursor = null;
461 }
Evan Millar11d628c2009-09-02 08:55:01 -0700462 }
463
464 private void bindData() {
465
466 // Build up the contact entries
467 buildEntries();
468
469 // Collapse similar data items in select sections.
470 Collapser.collapseList(mPhoneEntries);
471 Collapser.collapseList(mSmsEntries);
472 Collapser.collapseList(mEmailEntries);
473 Collapser.collapseList(mPostalEntries);
474
475 if (mAdapter == null) {
476 mAdapter = new ViewAdapter(this, mSections);
477 mListView.setAdapter(mAdapter);
478 } else {
479 mAdapter.setSections(mSections, SHOW_SEPARATORS);
480 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800481 }
482
483 @Override
484 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700485 super.onCreateOptionsMenu(menu);
486
487 final MenuInflater inflater = getMenuInflater();
488 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800489 return true;
490 }
491
492 @Override
493 public boolean onPrepareOptionsMenu(Menu menu) {
494 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700495
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700496 // Only allow edit if we have a selected tab
497 final boolean contactSelected = (mSelectedRawContactId != null);
498 menu.findItem(R.id.menu_edit).setEnabled(contactSelected);
Evan Millardb5d88c2009-08-28 09:31:57 -0700499
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700500 // Only allow split when more than one contact
501 final boolean isAggregate = (mRawContactIds.size() > 1);
502 menu.findItem(R.id.menu_split).setEnabled(isAggregate);
503
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800504 return true;
505 }
506
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800507 @Override
508 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
509 AdapterView.AdapterContextMenuInfo info;
510 try {
511 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
512 } catch (ClassCastException e) {
513 Log.e(TAG, "bad menuInfo", e);
514 return;
515 }
516
517 // This can be null sometimes, don't crash...
518 if (info == null) {
519 Log.e(TAG, "bad menuInfo");
520 return;
521 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700522
Evan Millar45e0ed32009-06-01 16:44:38 -0700523 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
524 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
525 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700526 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
527 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700528 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800529 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700530 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
531 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700532 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700533 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800534 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700535 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700536 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
537 }
538 // TODO(emillar): add back with group support.
539 /* else if (entry.mimetype.equals()) {
540 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
541 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800542 }
543
544 @Override
545 public boolean onOptionsItemSelected(MenuItem item) {
546 switch (item.getItemId()) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700547 case R.id.menu_edit: {
Evan Millardb5d88c2009-08-28 09:31:57 -0700548 Long rawContactIdToEdit = mSelectedRawContactId;
549 if (rawContactIdToEdit == null) {
550 // This shouldn't be possible. We only show the edit option if
551 // this value is non-null.
552 break;
553 }
Evan Millar8a79cee2009-08-19 17:20:49 -0700554 if (rawContactIdToEdit == ALL_CONTACTS_ID) {
555 // If the "all" tab is selected, edit the next tab.
Evan Millardb5d88c2009-08-28 09:31:57 -0700556 rawContactIdToEdit = getTabRawContactId(mTabWidget.getCurrentTab() + 1);
Evan Millar8a79cee2009-08-19 17:20:49 -0700557 }
558 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
559 rawContactIdToEdit);
560 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
561 REQUEST_EDIT_CONTACT);
562 break;
563 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700564 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800565 // Get confirmation
566 showDialog(DIALOG_CONFIRM_DELETE);
567 return true;
568 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700569 case R.id.menu_split: {
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700570 if (mRawContactIds.size() == 2) {
571 splitContact(mRawContactIds.get(1));
572 } else {
573 showSplitAggregateDialog();
574 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700575 return true;
576 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700577 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700578 showJoinAggregateActivity();
579 return true;
580 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700581 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700582 showOptionsActivity();
583 return true;
584 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700585 case R.id.menu_share: {
586 final Intent intent = new Intent(Intent.ACTION_SEND);
587 intent.setType(Contacts.CONTENT_ITEM_TYPE);
588 intent.putExtra(Intent.EXTRA_STREAM, mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700589
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700590 // Launch chooser to share contact via
591 final CharSequence chooseTitle = getText(R.string.share_via);
592 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700593
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700594 try {
595 startActivity(chooseIntent);
596 } catch (ActivityNotFoundException ex) {
597 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800598 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700599 return true;
600 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800601 }
602 return super.onOptionsItemSelected(item);
603 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700604
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800605 @Override
606 public boolean onContextItemSelected(MenuItem item) {
607 switch (item.getItemId()) {
608 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700609 if (makeItemDefault(item)) {
610 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800611 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700612 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800613 }
614 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700615
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800616 return super.onContextItemSelected(item);
617 }
618
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700619 private boolean makeItemDefault(MenuItem item) {
620 ViewEntry entry = getViewEntryForMenuItem(item);
621 if (entry == null) {
622 return false;
623 }
624
625 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700626 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700627 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700628 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
629 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700630 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700631 return true;
632 }
633
634 /**
635 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
636 * The user picks a contact to be split into its own aggregate or clicks Cancel.
637 */
638 private void showSplitAggregateDialog() {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700639 // Wrap this dialog in a specific theme so that list items have correct text color.
640 final ContextThemeWrapper dialogContext =
641 new ContextThemeWrapper(this, android.R.style.Theme_Light);
642 AlertDialog.Builder builder =
643 new AlertDialog.Builder(dialogContext);
644 builder.setTitle(getString(R.string.splitAggregate_title));
645
646 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
647 builder.setView(view);
648
649 builder.setInverseBackgroundForced(true);
650 builder.setCancelable(true);
651 builder.setNegativeButton(android.R.string.cancel,
652 new OnClickListener() {
653 public void onClick(DialogInterface dialog, int which) {
654 dialog.dismiss();
655 }
656 });
657 final AlertDialog dialog = builder.create();
658
659 view.setOnContactSelectedListener(new OnContactSelectedListener() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700660 public void onContactSelected(long rawContactId) {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700661 dialog.dismiss();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700662 splitContact(rawContactId);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700663 }
664 });
665
666 dialog.show();
667 }
668
669 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700670 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700671 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700672 public void showJoinAggregateActivity() {
673 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
674 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700675 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700676 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700677
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700678 @Override
679 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700680 switch (requestCode) {
681 case REQUEST_JOIN_CONTACT: {
682 if (resultCode == RESULT_OK && intent != null) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700683 final long contactId = ContentUris.parseId(intent.getData());
684 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700685 }
686 break;
687 }
688 case REQUEST_EDIT_CONTACT: {
689 if (resultCode == RESULT_OK && intent != null) {
690 long newInitialSelectedRawContactId = intent.getLongExtra(
691 RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
692 if (newInitialSelectedRawContactId != mSelectedRawContactId) {
693 mSelectedRawContactId = newInitialSelectedRawContactId;
Evan Millar8a79cee2009-08-19 17:20:49 -0700694 }
695 }
696 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700697 }
698 }
699
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700700 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700701 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700702
703 // The split operation may have removed the original aggregate contact, so we need
704 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700705 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700706 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700707 }
708
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700709 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700710 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700711 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700712
713 try {
714 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700715 long rawContactId = c.getLong(0);
716 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700717 }
718 } finally {
719 c.close();
720 }
721
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700722 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
723 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700724 }
725
726 /**
727 * Given a contact ID sets an aggregation exception to either join the contact with the
728 * current aggregate or split off.
729 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700730 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700731 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700732 for (long aRawContactId : mRawContactIds) {
733 if (aRawContactId != rawContactId) {
734 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
735 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
736 values.put(AggregationExceptions.TYPE, exceptionType);
737 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
738 }
739 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700740 }
741
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700742 private void showOptionsActivity() {
743 final Intent intent = new Intent(this, ContactOptionsActivity.class);
744 intent.setData(mUri);
745 startActivity(intent);
746 }
747
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700748 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
749 AdapterView.AdapterContextMenuInfo info;
750 try {
751 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
752 } catch (ClassCastException e) {
753 Log.e(TAG, "bad menuInfo", e);
754 return null;
755 }
756
757 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
758 }
759
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800760 @Override
761 public boolean onKeyDown(int keyCode, KeyEvent event) {
762 switch (keyCode) {
763 case KeyEvent.KEYCODE_CALL: {
764 try {
765 ITelephony phone = ITelephony.Stub.asInterface(
766 ServiceManager.checkService("phone"));
767 if (phone != null && !phone.isIdle()) {
768 // Skip out and let the key be handled at a higher level
769 break;
770 }
771 } catch (RemoteException re) {
772 // Fall through and try to call the contact
773 }
774
Evan Millar7911ff52009-07-21 15:55:18 -0700775 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800776 if (index != -1) {
777 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700778 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
779 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800780 }
781 } else if (mNumPhoneNumbers != 0) {
782 // There isn't anything selected, call the default number
783 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
784 startActivity(intent);
785 }
786 return true;
787 }
788
789 case KeyEvent.KEYCODE_DEL: {
790 showDialog(DIALOG_CONFIRM_DELETE);
791 return true;
792 }
793 }
794
795 return super.onKeyDown(keyCode, event);
796 }
797
Evan Millar7911ff52009-07-21 15:55:18 -0700798 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800799 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
800 if (entry != null) {
801 Intent intent = entry.intent;
802 if (intent != null) {
803 try {
804 startActivity(intent);
805 } catch (ActivityNotFoundException e) {
806 Log.e(TAG, "No activity found for intent: " + intent);
807 signalError();
808 }
809 } else {
810 signalError();
811 }
812 } else {
813 signalError();
814 }
815 }
816
817 /**
818 * Signal an error to the user via a beep, or some other method.
819 */
820 private void signalError() {
821 //TODO: implement this when we have the sonification APIs
822 }
823
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800824 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700825 // 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 -0800826 StringBuilder buf = new StringBuilder("imto://");
827 buf.append(host);
828 buf.append('/');
829 buf.append(data);
830 return Uri.parse(buf.toString());
831 }
832
833 /**
834 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700835 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800836 * @param personCursor the URI for the contact being displayed
837 */
Evan Millar11d628c2009-09-02 08:55:01 -0700838 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800839 // Clear out the old entries
840 final int numSections = mSections.size();
841 for (int i = 0; i < numSections; i++) {
842 mSections.get(i).clear();
843 }
844
Evan Millar7911ff52009-07-21 15:55:18 -0700845 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700846
Evan Millar11d628c2009-09-02 08:55:01 -0700847 Sources sources = Sources.getInstance(this);
848
Evan Millar66388be2009-05-28 15:41:07 -0700849 // Build up method entries
850 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700851 for (Entity entity: mEntities) {
852 final ContentValues entValues = entity.getEntityValues();
853 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700854 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700855
Evan Millar66210452009-09-21 10:53:43 -0700856 // This performs the tab filtering
857 if (mSelectedRawContactId != null
Jeff Sharkeyab066932009-09-21 09:55:30 -0700858 && mSelectedRawContactId != rawContactId
Evan Millar66210452009-09-21 10:53:43 -0700859 && mSelectedRawContactId != ALL_CONTACTS_ID) {
860 continue;
861 }
862
Evan Millar11d628c2009-09-02 08:55:01 -0700863 for (NamedContentValues subValue : entity.getSubValues()) {
864 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800865
Jeff Sharkeyab066932009-09-21 09:55:30 -0700866 final ContentValues entryValues = subValue.values;
867 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
868
Evan Millar11d628c2009-09-02 08:55:01 -0700869 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700870 if (mimetype == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -0700871
Jeff Sharkeyab066932009-09-21 09:55:30 -0700872 final DataKind kind = sources.getKindOrFallback(accountType, mimetype, this,
Evan Millar11d628c2009-09-02 08:55:01 -0700873 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700874 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800875
Evan Millar11d628c2009-09-02 08:55:01 -0700876 final long id = entryValues.getAsLong(Data._ID);
877 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700878 entry.contactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700879 entry.id = id;
880 entry.uri = uri;
881 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -0700882 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -0700883 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700884 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700885 entry.type = entryValues.getAsInteger(kind.typeColumn);
886 }
887 if (kind.iconRes > 0) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700888 entry.resPackageName = kind.resPackageName;
Evan Millar11d628c2009-09-02 08:55:01 -0700889 entry.actionIcon = kind.iconRes;
890 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800891
Evan Millar11d628c2009-09-02 08:55:01 -0700892 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700893 if (TextUtils.isEmpty(entry.data)) {
Alex Kennberg87fc3172009-03-28 06:43:06 -0700894 continue;
895 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700896
Evan Millar11d628c2009-09-02 08:55:01 -0700897 if (!mRawContactIds.contains(entry.contactId)) {
898 mRawContactIds.add(entry.contactId);
899 }
900
Evan Millar11d628c2009-09-02 08:55:01 -0700901 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)
902 || CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)
903 || CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)
904 || CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
905 final boolean isSuperPrimary = entryValues.getAsInteger(
906 Data.IS_SUPER_PRIMARY) != 0;
907
908 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
909 // Build phone entries
910 mNumPhoneNumbers++;
911
912 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
913 Uri.fromParts("tel", entry.data, null));
914 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
915 Uri.fromParts("sms", entry.data, null));
916 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700917
918 // If data is empty, don't show it.
919 if (TextUtils.isEmpty(entry.data)) {
920 Log.w(TAG, "empty data for contact method " + id);
921 continue;
922 }
923
Evan Millar11d628c2009-09-02 08:55:01 -0700924 entry.isPrimary = isSuperPrimary;
925 mPhoneEntries.add(entry);
926
927 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
928 || mShowSmsLinksForAllPhones) {
929 // Add an SMS entry
930 if (kind.iconAltRes > 0) {
931 entry.secondaryActionIcon = kind.iconAltRes;
932 }
933 }
934 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
935 // Build email entries
936 entry.intent = new Intent(Intent.ACTION_SENDTO,
937 Uri.fromParts("mailto", entry.data, null));
938 entry.isPrimary = isSuperPrimary;
939 mEmailEntries.add(entry);
940 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
941 equals(mimetype)) {
942 // Build postal entries
943 entry.maxLines = 4;
944 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
945 mPostalEntries.add(entry);
946 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
947 // Build im entries
948 Object protocolObj = entryValues.getAsInteger(Data.DATA5);
949 String host = null;
950
Evan Millar49714ee2009-09-02 16:42:47 -0700951 if (TextUtils.isEmpty(entry.label)) {
Jeff Sharkeyae6c2882009-09-21 23:09:35 -0700952 entry.label = getString(R.string.chat).toLowerCase();
Evan Millar49714ee2009-09-02 16:42:47 -0700953 }
954
Evan Millar11d628c2009-09-02 08:55:01 -0700955 if (protocolObj instanceof Number) {
956 int protocol = ((Number) protocolObj).intValue();
957 host = ContactsUtils.lookupProviderNameFromId(
958 protocol).toLowerCase();
959 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
960 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
961 entry.maxLabelLines = 2;
962 }
963 } else if (protocolObj != null) {
964 String providerName = (String) protocolObj;
965 host = providerName.toLowerCase();
966 }
967
968 // Only add the intent if there is a valid host
969 if (!TextUtils.isEmpty(host)) {
970 entry.intent = new Intent(Intent.ACTION_SENDTO,
971 constructImToUrl(host, entry.data));
972 }
973 //TODO(emillar) Add in presence info
974 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
975 entry.presenceIcon = Presence.getPresenceIconResourceId(
976 aggCursor.getInt(METHODS_STATUS_COLUMN));
977 entry.status = ...
978 }*/
979 mImEntries.add(entry);
980 }
981 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
982 // Build organization entries
983 mOrganizationEntries.add(entry);
984 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
985 // Build note entries
986 entry.id = 0;
987 entry.uri = null;
988 entry.intent = null;
989 entry.maxLines = 10;
990 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700991 } else {
992 // Handle showing custom
993 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
994 mOtherEntries.add(entry);
Evan Millar11d628c2009-09-02 08:55:01 -0700995 }
996
Evan Millar11d628c2009-09-02 08:55:01 -0700997 // TODO(emillar) Add group entries
998 // // Build the group entries
999 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1000 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1001 // null, null, Groups.DEFAULT_SORT_ORDER);
1002 // if (groupCursor != null) {
1003 // try {
1004 // StringBuilder sb = new StringBuilder();
1005 //
1006 // while (groupCursor.moveToNext()) {
1007 // String systemId = groupCursor.getString(
1008 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1009 //
1010 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1011 // continue;
1012 // }
1013 //
1014 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1015 // if (!TextUtils.isEmpty(name)) {
1016 // if (sb.length() == 0) {
1017 // sb.append(name);
1018 // } else {
1019 // sb.append(getString(R.string.group_list, name));
1020 // }
1021 // }
1022 // }
1023 //
1024 // if (sb.length() > 0) {
1025 // ViewEntry entry = new ViewEntry();
1026 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1027 // entry.label = getString(R.string.label_groups);
1028 // entry.data = sb.toString();
1029 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1030 //
1031 // // TODO: Add an icon for the groups item.
1032 //
1033 // mGroupEntries.add(entry);
1034 // }
1035 // } finally {
1036 // groupCursor.close();
1037 // }
1038 // }
Evan Millar66388be2009-05-28 15:41:07 -07001039 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001040
Evan Millar66388be2009-05-28 15:41:07 -07001041 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001042 }
1043 }
1044
Evan Millar11d628c2009-09-02 08:55:01 -07001045 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1046 if (kind.actionHeader == null) {
1047 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001048 }
Evan Millar11d628c2009-09-02 08:55:01 -07001049 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1050 if (actionHeader == null) {
1051 return null;
1052 }
1053 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1054 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001055
Evan Millar11d628c2009-09-02 08:55:01 -07001056 String buildDataString(DataKind kind, ContentValues values) {
1057 if (kind.actionBody == null) {
1058 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001059 }
Evan Millar11d628c2009-09-02 08:55:01 -07001060 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1061 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001062 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001063
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001064 /**
1065 * A basic structure with the data for a contact entry in the list.
1066 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001067 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001068 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001069 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001070 public boolean isPrimary = false;
1071 public int presenceIcon = -1;
1072 public int secondaryActionIcon = -1;
1073 public Intent intent;
1074 public Intent secondaryIntent = null;
1075 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001076 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001077 public ArrayList<Long> ids = new ArrayList<Long>();
1078 public int collapseCount = 0;
1079
1080 public boolean collapseWith(ViewEntry entry) {
1081 // assert equal collapse keys
1082 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1083 return false;
1084 }
1085
1086 // Choose the label associated with the highest type precedence.
1087 if (TypePrecedence.getTypePrecedence(mimetype, type)
1088 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1089 type = entry.type;
1090 label = entry.label;
1091 }
1092
1093 // Choose the max of the maxLines and maxLabelLines values.
1094 maxLines = Math.max(maxLines, entry.maxLines);
1095 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1096
1097 // Choose the presence with the highest precedence.
1098 if (Presence.getPresencePrecedence(status)
1099 < Presence.getPresencePrecedence(entry.status)) {
1100 status = entry.status;
1101 }
1102
1103 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001104 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001105
1106 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1107
1108 // Keep track of all the ids that have been collapsed with this one.
1109 ids.add(entry.id);
1110 collapseCount++;
1111 return true;
1112 }
1113
1114 public String getCollapseKey() {
1115 StringBuilder hashSb = new StringBuilder();
1116 hashSb.append(data);
1117 hashSb.append(mimetype);
1118 hashSb.append((intent != null && intent.getAction() != null)
1119 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001120 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1121 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001122 hashSb.append(actionIcon);
1123 return hashSb.toString();
1124 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001125 }
1126
Evan Millar15e514d2009-08-04 10:14:57 -07001127 /** Cache of the children views of a row */
1128 static class ViewCache {
1129 public TextView label;
1130 public TextView data;
1131 public ImageView actionIcon;
1132 public ImageView presenceIcon;
1133 public ImageView primaryIcon;
1134 public ImageView secondaryActionButton;
1135 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001136
Evan Millar15e514d2009-08-04 10:14:57 -07001137 // Need to keep track of this too
1138 ViewEntry entry;
1139 }
1140
1141 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1142 implements View.OnClickListener {
1143
Evan Millar5c22c3b2009-05-29 11:37:54 -07001144
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001145 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1146 super(context, sections, SHOW_SEPARATORS);
1147 }
1148
Evan Millar15e514d2009-08-04 10:14:57 -07001149 public void onClick(View v) {
1150 Intent intent = (Intent) v.getTag();
1151 startActivity(intent);
1152 }
1153
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001154 @Override
1155 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001156 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001157 View v;
1158
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001159 ViewCache views;
1160
1161 // Check to see if we can reuse convertView
1162 if (convertView != null) {
1163 v = convertView;
1164 views = (ViewCache) v.getTag();
1165 } else {
1166 // Create a new view if needed
1167 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1168
1169 // Cache the children
1170 views = new ViewCache();
1171 views.label = (TextView) v.findViewById(android.R.id.text1);
1172 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001173 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1174 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1175 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1176 views.secondaryActionButton = (ImageView) v.findViewById(
1177 R.id.secondary_action_button);
1178 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001179 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001180 v.setTag(views);
1181 }
1182
1183 // Update the entry in the view cache
1184 views.entry = entry;
1185
1186 // Bind the data to the view
1187 bindView(v, entry);
1188 return v;
1189 }
1190
1191 @Override
1192 protected View newView(int position, ViewGroup parent) {
1193 // getView() handles this
1194 throw new UnsupportedOperationException();
1195 }
1196
1197 @Override
1198 protected void bindView(View view, ViewEntry entry) {
1199 final Resources resources = mContext.getResources();
1200 ViewCache views = (ViewCache) view.getTag();
1201
1202 // Set the label
1203 TextView label = views.label;
1204 setMaxLines(label, entry.maxLabelLines);
1205 label.setText(entry.label);
1206
1207 // Set the data
1208 TextView data = views.data;
1209 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001210 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001211 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001212 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1213 } else {
1214 data.setText(entry.data);
1215 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001216 setMaxLines(data, entry.maxLines);
1217 }
1218
Evan Millar15e514d2009-08-04 10:14:57 -07001219 // Set the primary icon
1220 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1221
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001222 // Set the action icon
1223 ImageView action = views.actionIcon;
1224 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001225 Drawable actionIcon;
1226 if (entry.resPackageName != null) {
1227 // Load external resources through PackageManager
1228 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1229 entry.actionIcon, null);
1230 } else {
1231 actionIcon = resources.getDrawable(entry.actionIcon);
1232 }
1233 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001234 action.setVisibility(View.VISIBLE);
1235 } else {
1236 // Things should still line up as if there was an icon, so make it invisible
1237 action.setVisibility(View.INVISIBLE);
1238 }
1239
1240 // Set the presence icon
1241 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001242 if (entry.presenceIcon != -1) {
1243 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001244 } else if (entry.status != -1) {
1245 presenceIcon = resources.getDrawable(
1246 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001247 }
Evan Millar15e514d2009-08-04 10:14:57 -07001248 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001249 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001250 presenceIconView.setImageDrawable(presenceIcon);
1251 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001252 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001253 presenceIconView.setVisibility(View.GONE);
1254 }
1255
1256 // Set the secondary action button
1257 ImageView secondaryActionView = views.secondaryActionButton;
1258 Drawable secondaryActionIcon = null;
1259 if (entry.secondaryActionIcon != -1) {
1260 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1261 }
1262 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1263 secondaryActionView.setImageDrawable(secondaryActionIcon);
1264 secondaryActionView.setTag(entry.secondaryIntent);
1265 secondaryActionView.setVisibility(View.VISIBLE);
1266 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1267 } else {
1268 secondaryActionView.setVisibility(View.GONE);
1269 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001270 }
1271 }
1272
1273 private void setMaxLines(TextView textView, int maxLines) {
1274 if (maxLines == 1) {
1275 textView.setSingleLine(true);
1276 textView.setEllipsize(TextUtils.TruncateAt.END);
1277 } else {
1278 textView.setSingleLine(false);
1279 textView.setMaxLines(maxLines);
1280 textView.setEllipsize(null);
1281 }
1282 }
1283 }
1284}