blob: c15a40dd4a70dad9f70fc366547735aa9758cb17 [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;
Evan Millar54a5c9f2009-06-23 17:41:09 -070018import com.android.contacts.Collapser.Collapsible;
Evan Millar11d628c2009-09-02 08:55:01 -070019import com.android.contacts.model.ContactsSource;
20import com.android.contacts.model.Sources;
21import com.android.contacts.model.ContactsSource.DataKind;
Evan Millardf181202009-09-30 15:15:51 -070022import com.android.contacts.ui.EditContactActivity;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070023import com.android.contacts.util.Constants;
Jeff Sharkey624ddc32009-10-01 21:32:19 -070024import com.android.contacts.util.DataStatus;
Evan Millar11d628c2009-09-02 08:55:01 -070025import com.android.contacts.util.NotifyingAsyncQueryHandler;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080026import com.android.internal.telephony.ITelephony;
Evan Millar11d628c2009-09-02 08:55:01 -070027import com.android.internal.widget.ContactHeaderWidget;
Jeff Sharkey624ddc32009-10-01 21:32:19 -070028import com.google.android.collect.Lists;
29import com.google.android.collect.Maps;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080030
Evan Millar11d628c2009-09-02 08:55:01 -070031import android.app.Activity;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080032import android.app.AlertDialog;
33import android.app.Dialog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080034import android.content.ActivityNotFoundException;
35import android.content.ContentResolver;
36import android.content.ContentUris;
37import android.content.ContentValues;
38import android.content.Context;
39import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070040import android.content.Entity;
Evan Millar11d628c2009-09-02 08:55:01 -070041import android.content.EntityIterator;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080042import android.content.Intent;
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;
Evan Millar951fa9c2009-10-26 14:57:43 -070048import android.net.ParseException;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080049import android.net.Uri;
Evan Millar951fa9c2009-10-26 14:57:43 -070050import android.net.WebAddress;
Brad Fitzpatrick23bc9b72010-03-23 23:27:34 -070051import android.os.AsyncTask;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080052import android.os.Bundle;
53import android.os.Handler;
54import android.os.RemoteException;
55import android.os.ServiceManager;
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -070056import android.provider.ContactsContract;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070057import android.provider.ContactsContract.AggregationExceptions;
58import android.provider.ContactsContract.CommonDataKinds;
Evan Millar11d628c2009-09-02 08:55:01 -070059import android.provider.ContactsContract.Contacts;
Evan Millar66388be2009-05-28 15:41:07 -070060import android.provider.ContactsContract.Data;
Daniel Lehmann350ba6f2010-03-23 18:10:57 -070061import android.provider.ContactsContract.DisplayNameSources;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070062import android.provider.ContactsContract.RawContacts;
Fred Quintana8579e252009-12-07 14:50:12 -080063import android.provider.ContactsContract.RawContactsEntity;
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -070064import android.provider.ContactsContract.StatusUpdates;
Jeff Sharkey624ddc32009-10-01 21:32:19 -070065import android.provider.ContactsContract.CommonDataKinds.Email;
66import android.provider.ContactsContract.CommonDataKinds.Im;
67import android.provider.ContactsContract.CommonDataKinds.Nickname;
68import android.provider.ContactsContract.CommonDataKinds.Note;
69import android.provider.ContactsContract.CommonDataKinds.Organization;
Evan Millar54a5c9f2009-06-23 17:41:09 -070070import android.provider.ContactsContract.CommonDataKinds.Phone;
Jeff Sharkey624ddc32009-10-01 21:32:19 -070071import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
Evan Millar951fa9c2009-10-26 14:57:43 -070072import android.provider.ContactsContract.CommonDataKinds.Website;
Evan Millar54a5c9f2009-06-23 17:41:09 -070073import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080074import android.text.TextUtils;
75import android.util.Log;
76import android.view.ContextMenu;
77import android.view.KeyEvent;
Evan Millar11d628c2009-09-02 08:55:01 -070078import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080079import android.view.Menu;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070080import android.view.MenuInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080081import android.view.MenuItem;
82import android.view.View;
83import android.view.ViewGroup;
Evan Millar11d628c2009-09-02 08:55:01 -070084import android.view.Window;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080085import android.view.ContextMenu.ContextMenuInfo;
86import android.widget.AdapterView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087import android.widget.ImageView;
88import android.widget.ListView;
89import android.widget.TextView;
90import android.widget.Toast;
91
92import java.util.ArrayList;
Jeff Sharkey624ddc32009-10-01 21:32:19 -070093import java.util.HashMap;
Daniel Lehmann4f87c892010-03-30 17:03:45 -070094import java.util.List;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080095
96/**
97 * Displays the details of a specific contact.
98 */
Evan Millar11d628c2009-09-02 08:55:01 -070099public class ViewContactActivity extends Activity
Evan Millar7911ff52009-07-21 15:55:18 -0700100 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
Evan Millardf181202009-09-30 15:15:51 -0700101 AdapterView.OnItemClickListener, NotifyingAsyncQueryHandler.AsyncQueryListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800102 private static final String TAG = "ViewContact";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800103
104 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800105
106 private static final int DIALOG_CONFIRM_DELETE = 1;
Megha Joshid5afd8a2009-09-22 09:38:41 -0700107 private static final int DIALOG_CONFIRM_READONLY_DELETE = 2;
108 private static final int DIALOG_CONFIRM_MULTIPLE_DELETE = 3;
Megha Joshic508bd82009-10-01 17:31:49 -0700109 private static final int DIALOG_CONFIRM_READONLY_HIDE = 4;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800110
Evan Millar8a79cee2009-08-19 17:20:49 -0700111 private static final int REQUEST_JOIN_CONTACT = 1;
Evan Millar9cd81242009-09-26 16:02:31 -0700112 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700113
Evan Millar8a79cee2009-08-19 17:20:49 -0700114 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800115
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700116 protected Uri mLookupUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800117 private ContentResolver mResolver;
118 private ViewAdapter mAdapter;
119 private int mNumPhoneNumbers = 0;
120
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700121 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700122 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700123 */
Evan Millar7911ff52009-07-21 15:55:18 -0700124 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700125
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800126 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
127 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
128 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
129 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
130 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
Daniel Lehmanne12f8a92010-02-24 18:37:56 -0800131 /* package */ ArrayList<ViewEntry> mNicknameEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800132 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700133 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800134 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
135 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
136
137 private Cursor mCursor;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700138
Evan Millar11d628c2009-09-02 08:55:01 -0700139 protected ContactHeaderWidget mContactHeaderWidget;
140 private NotifyingAsyncQueryHandler mHandler;
141
142 protected LayoutInflater mInflater;
143
Megha Joshid5afd8a2009-09-22 09:38:41 -0700144 protected int mReadOnlySourcesCnt;
145 protected int mWritableSourcesCnt;
Jeff Sharkeye0408762009-11-03 12:57:16 -0800146 protected boolean mAllRestricted;
147
Jeff Sharkey827762d2009-12-23 11:06:08 -0800148 protected Uri mPrimaryPhoneUri = null;
149
Megha Joshid5afd8a2009-09-22 09:38:41 -0700150 protected ArrayList<Long> mWritableRawContactIds = new ArrayList<Long>();
Evan Millar11d628c2009-09-02 08:55:01 -0700151
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700152 private static final int TOKEN_ENTITIES = 0;
153 private static final int TOKEN_STATUSES = 1;
154
155 private boolean mHasEntities = false;
156 private boolean mHasStatuses = false;
157
Daniel Lehmann350ba6f2010-03-23 18:10:57 -0700158 private long mNameRawContactId = -1;
159 private int mDisplayNameSource = DisplayNameSources.UNDEFINED;
160
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700161 private ArrayList<Entity> mEntities = Lists.newArrayList();
162 private HashMap<Long, DataStatus> mStatuses = Maps.newHashMap();
Evan Millar11d628c2009-09-02 08:55:01 -0700163
Makoto Onuki7a707382009-11-19 13:35:55 +0900164 /**
165 * The view shown if the detail list is empty.
166 * We set this to the list view when first bind the adapter, so that it won't be shown while
167 * we're loading data.
168 */
169 private View mEmptyView;
170
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800171 private ContentObserver mObserver = new ContentObserver(new Handler()) {
172 @Override
173 public boolean deliverSelfNotifications() {
174 return true;
175 }
176
177 @Override
178 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700179 if (mCursor != null && !mCursor.isClosed()) {
180 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800181 }
182 }
183 };
184
185 public void onClick(DialogInterface dialog, int which) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700186 closeCursor();
Megha Joshic508bd82009-10-01 17:31:49 -0700187 getContentResolver().delete(mLookupUri, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800188 finish();
189 }
190
Evan Millar7911ff52009-07-21 15:55:18 -0700191 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800192 private boolean mShowSmsLinksForAllPhones;
193
194 @Override
195 protected void onCreate(Bundle icicle) {
196 super.onCreate(icicle);
197
Evan Millar11d628c2009-09-02 08:55:01 -0700198 final Intent intent = getIntent();
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700199 Uri data = intent.getData();
200 String authority = data.getAuthority();
201 if (ContactsContract.AUTHORITY.equals(authority)) {
202 mLookupUri = data;
203 } else if (android.provider.Contacts.AUTHORITY.equals(authority)) {
204 final long rawContactId = ContentUris.parseId(data);
205 mLookupUri = RawContacts.getContactLookupUri(getContentResolver(),
206 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700207
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700208 }
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700209 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700210
211 requestWindowFeature(Window.FEATURE_NO_TITLE);
212 setContentView(R.layout.contact_card_layout);
213
214 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
215 mContactHeaderWidget.showStar(true);
Evan Millar11d628c2009-09-02 08:55:01 -0700216 mContactHeaderWidget.setExcludeMimes(new String[] {
217 Contacts.CONTENT_ITEM_TYPE
218 });
219
Evan Millar11d628c2009-09-02 08:55:01 -0700220 mHandler = new NotifyingAsyncQueryHandler(this, this);
221
Evan Millarcdeea942009-10-07 11:47:35 -0700222 mListView = (ListView) findViewById(R.id.contact_data);
Evan Millar7911ff52009-07-21 15:55:18 -0700223 mListView.setOnCreateContextMenuListener(this);
224 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
225 mListView.setOnItemClickListener(this);
Makoto Onuki7a707382009-11-19 13:35:55 +0900226 // Don't set it to mListView yet. We do so later when we bind the adapter.
227 mEmptyView = findViewById(android.R.id.empty);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800228
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800229 mResolver = getContentResolver();
230
231 // Build the list of sections. The order they're added to mSections dictates the
232 // order they are displayed in the list.
233 mSections.add(mPhoneEntries);
234 mSections.add(mSmsEntries);
235 mSections.add(mEmailEntries);
236 mSections.add(mImEntries);
237 mSections.add(mPostalEntries);
Daniel Lehmanne12f8a92010-02-24 18:37:56 -0800238 mSections.add(mNicknameEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800239 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700240 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800241 mSections.add(mOtherEntries);
242
243 //TODO Read this value from a preference
244 mShowSmsLinksForAllPhones = true;
Evan Millar11d628c2009-09-02 08:55:01 -0700245 }
246
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800247 @Override
248 protected void onResume() {
249 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700250 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800251 }
252
253 @Override
254 protected void onPause() {
255 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700256 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800257 }
258
259 @Override
260 protected void onDestroy() {
261 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700262 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800263 }
264
265 @Override
266 protected Dialog onCreateDialog(int id) {
267 switch (id) {
268 case DIALOG_CONFIRM_DELETE:
269 return new AlertDialog.Builder(this)
270 .setTitle(R.string.deleteConfirmation_title)
271 .setIcon(android.R.drawable.ic_dialog_alert)
272 .setMessage(R.string.deleteConfirmation)
273 .setNegativeButton(android.R.string.cancel, null)
274 .setPositiveButton(android.R.string.ok, this)
275 .setCancelable(false)
276 .create();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700277 case DIALOG_CONFIRM_READONLY_DELETE:
278 return new AlertDialog.Builder(this)
279 .setTitle(R.string.deleteConfirmation_title)
280 .setIcon(android.R.drawable.ic_dialog_alert)
281 .setMessage(R.string.readOnlyContactDeleteConfirmation)
282 .setNegativeButton(android.R.string.cancel, null)
283 .setPositiveButton(android.R.string.ok, this)
284 .setCancelable(false)
285 .create();
286 case DIALOG_CONFIRM_MULTIPLE_DELETE:
287 return new AlertDialog.Builder(this)
288 .setTitle(R.string.deleteConfirmation_title)
289 .setIcon(android.R.drawable.ic_dialog_alert)
290 .setMessage(R.string.multipleContactDeleteConfirmation)
291 .setNegativeButton(android.R.string.cancel, null)
292 .setPositiveButton(android.R.string.ok, this)
293 .setCancelable(false)
294 .create();
Evan Millarcdeea942009-10-07 11:47:35 -0700295 case DIALOG_CONFIRM_READONLY_HIDE: {
Megha Joshic508bd82009-10-01 17:31:49 -0700296 return new AlertDialog.Builder(this)
297 .setTitle(R.string.deleteConfirmation_title)
298 .setIcon(android.R.drawable.ic_dialog_alert)
299 .setMessage(R.string.readOnlyContactWarning)
300 .setPositiveButton(android.R.string.ok, this)
Evan Millarcdeea942009-10-07 11:47:35 -0700301 .create();
Megha Joshic508bd82009-10-01 17:31:49 -0700302 }
303
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800304 }
305 return null;
306 }
307
Evan Millar11d628c2009-09-02 08:55:01 -0700308 /** {@inheritDoc} */
Brad Fitzpatrick23bc9b72010-03-23 23:27:34 -0700309 public void onQueryComplete(int token, Object cookie, final Cursor cursor) {
Fred Quintana8579e252009-12-07 14:50:12 -0800310 if (token == TOKEN_STATUSES) {
311 try {
312 // Read available social rows and consider binding
313 readStatuses(cursor);
314 } finally {
315 if (cursor != null) {
316 cursor.close();
317 }
318 }
Brad Fitzpatrick23bc9b72010-03-23 23:27:34 -0700319 considerBindData();
320 return;
Evan Millar11d628c2009-09-02 08:55:01 -0700321 }
Brad Fitzpatrick23bc9b72010-03-23 23:27:34 -0700322
Brad Fitzpatrick1b28a0a2010-03-24 10:53:51 -0700323 // One would think we could just iterate over the Cursor
324 // directly here, as the result set should be small, and we've
325 // already run the query in an AsyncTask, but a lot of ANRs
326 // were being reported in this code nonetheless. See bug
327 // 2539603 for details. The real bug which makes this result
328 // set huge and CPU-heavy may be elsewhere.
329 // TODO: if we keep this async, perhaps the entity iteration
330 // should also be original AsyncTask, rather than ping-ponging
331 // between threads like this.
Brad Fitzpatrick23bc9b72010-03-23 23:27:34 -0700332 final ArrayList<Entity> oldEntities = mEntities;
333 (new AsyncTask<Void, Void, ArrayList<Entity>>() {
334 @Override
335 protected ArrayList<Entity> doInBackground(Void... params) {
Daniel Lehmann4f87c892010-03-30 17:03:45 -0700336 ArrayList<Entity> newEntities = new ArrayList<Entity>(cursor.getCount());
Brad Fitzpatrick23bc9b72010-03-23 23:27:34 -0700337 EntityIterator iterator = RawContacts.newEntityIterator(cursor);
338 try {
339 while (iterator.hasNext()) {
340 Entity entity = iterator.next();
341 newEntities.add(entity);
342 }
Brad Fitzpatrick23bc9b72010-03-23 23:27:34 -0700343 } finally {
344 iterator.close();
345 }
346 return newEntities;
347 }
348
349 @Override
350 protected void onPostExecute(ArrayList<Entity> newEntities) {
351 if (newEntities == null) {
352 // There was an error loading.
353 return;
354 }
355 synchronized (ViewContactActivity.this) {
356 if (mEntities != oldEntities) {
357 // Multiple async tasks were in flight and we
358 // lost the race.
359 return;
360 }
361 mEntities = newEntities;
362 mHasEntities = true;
363 }
364 considerBindData();
365 }
366 }).execute();
Evan Millar11d628c2009-09-02 08:55:01 -0700367 }
368
Evan Millar6a61a1a2009-09-29 14:00:43 -0700369 private long getRefreshedContactId() {
370 Uri freshContactUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
371 if (freshContactUri != null) {
372 return ContentUris.parseId(freshContactUri);
373 }
374 return -1;
375 }
376
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700377 /**
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700378 * Read from the given {@link Cursor} and build a set of {@link DataStatus}
379 * objects to match any valid statuses found.
380 */
381 private synchronized void readStatuses(Cursor cursor) {
382 mStatuses.clear();
383
384 // Walk found statuses, creating internal row for each
385 while (cursor.moveToNext()) {
386 final DataStatus status = new DataStatus(cursor);
387 final long dataId = cursor.getLong(StatusQuery._ID);
388 mStatuses.put(dataId, status);
389 }
390
391 mHasStatuses = true;
392 }
393
Daniel Lehmann4f87c892010-03-30 17:03:45 -0700394 private static Cursor setupContactCursor(ContentResolver resolver, Uri lookupUri) {
395 if (lookupUri == null) {
396 return null;
397 }
398 final List<String> segments = lookupUri.getPathSegments();
399 if (segments.size() != 4) {
400 return null;
401 }
402
403 // Contains an Id.
404 final long uriContactId = Long.parseLong(segments.get(3));
Daniel Lehmanncdf961b2010-03-31 13:54:22 -0700405 final String uriLookupKey = Uri.encode(segments.get(2));
Daniel Lehmann4f87c892010-03-30 17:03:45 -0700406 final Uri dataUri = Uri.withAppendedPath(
407 ContentUris.withAppendedId(Contacts.CONTENT_URI, uriContactId),
408 Contacts.Data.CONTENT_DIRECTORY);
409
410 // This cursor has several purposes:
411 // - Fetch NAME_RAW_CONTACT_ID and DISPLAY_NAME_SOURCE
412 // - Fetch the lookup-key to ensure we are looking at the right record
413 // - Watcher for change events
414 Cursor cursor = resolver.query(dataUri,
415 new String[] {
416 Contacts.NAME_RAW_CONTACT_ID,
417 Contacts.DISPLAY_NAME_SOURCE,
418 Contacts.LOOKUP_KEY
419 }, null, null, null);
420
421 if (cursor.moveToFirst()) {
422 String lookupKey =
423 cursor.getString(cursor.getColumnIndex(Contacts.LOOKUP_KEY));
424 if (!lookupKey.equals(uriLookupKey)) {
425 // ID and lookup key do not match
426 cursor.close();
427 return null;
428 }
429 return cursor;
430 } else {
431 cursor.close();
432 return null;
433 }
434 }
435
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700436 private synchronized void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700437 closeCursor();
438
Daniel Lehmann4f87c892010-03-30 17:03:45 -0700439 // Interprete mLookupUri
440 mCursor = setupContactCursor(mResolver, mLookupUri);
441
442 // If mCursor is null now we did not succeed in using the Uri's Id (or it didn't contain
443 // a Uri). Instead we now have to use the lookup key to find the record
444 if (mCursor == null) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700445 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
Daniel Lehmann4f87c892010-03-30 17:03:45 -0700446 mCursor = setupContactCursor(mResolver, mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700447 }
448
Daniel Lehmann4f87c892010-03-30 17:03:45 -0700449 // If mCursor is still null, we were unsuccessful in finding the record
450 if (mCursor == null) {
451 mNameRawContactId = -1;
452 mDisplayNameSource = DisplayNameSources.UNDEFINED;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700453 // TODO either figure out a way to prevent a flash of black background or
454 // use some other UI than a toast
455 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
456 Log.e(TAG, "invalid contact uri: " + mLookupUri);
457 finish();
458 return;
459 }
460
Daniel Lehmann4f87c892010-03-30 17:03:45 -0700461 final long contactId = ContentUris.parseId(mLookupUri);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700462
Daniel Lehmann4f87c892010-03-30 17:03:45 -0700463 mNameRawContactId =
Daniel Lehmann350ba6f2010-03-23 18:10:57 -0700464 mCursor.getLong(mCursor.getColumnIndex(Contacts.NAME_RAW_CONTACT_ID));
Daniel Lehmann4f87c892010-03-30 17:03:45 -0700465 mDisplayNameSource =
Daniel Lehmann350ba6f2010-03-23 18:10:57 -0700466 mCursor.getInt(mCursor.getColumnIndex(Contacts.DISPLAY_NAME_SOURCE));
Daniel Lehmann350ba6f2010-03-23 18:10:57 -0700467
468 mCursor.registerContentObserver(mObserver);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700469
470 // Clear flags and start queries to data and status
471 mHasEntities = false;
472 mHasStatuses = false;
473
Fred Quintana8579e252009-12-07 14:50:12 -0800474 mHandler.startQuery(TOKEN_ENTITIES, null, RawContactsEntity.CONTENT_URI, null,
Daniel Lehmann4f87c892010-03-30 17:03:45 -0700475 RawContacts.CONTACT_ID + "=?", new String[] {
476 String.valueOf(contactId)
477 }, null);
478 final Uri dataUri = Uri.withAppendedPath(
479 ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId),
480 Contacts.Data.CONTENT_DIRECTORY);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700481 mHandler.startQuery(TOKEN_STATUSES, null, dataUri, StatusQuery.PROJECTION,
482 StatusUpdates.PRESENCE + " IS NOT NULL OR " + StatusUpdates.STATUS
483 + " IS NOT NULL", null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700484
Evan Millar9b1a1242009-09-16 10:26:10 -0700485 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700486 }
487
488 private void closeCursor() {
489 if (mCursor != null) {
490 mCursor.unregisterContentObserver(mObserver);
491 mCursor.close();
492 mCursor = null;
493 }
Evan Millar11d628c2009-09-02 08:55:01 -0700494 }
495
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700496 /**
497 * Consider binding views after any of several background queries has
498 * completed. We check internal flags and only bind when all data has
499 * arrived.
500 */
501 private void considerBindData() {
502 if (mHasEntities && mHasStatuses) {
503 bindData();
504 }
505 }
506
Evan Millar11d628c2009-09-02 08:55:01 -0700507 private void bindData() {
508
509 // Build up the contact entries
510 buildEntries();
511
512 // Collapse similar data items in select sections.
513 Collapser.collapseList(mPhoneEntries);
514 Collapser.collapseList(mSmsEntries);
515 Collapser.collapseList(mEmailEntries);
516 Collapser.collapseList(mPostalEntries);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700517 Collapser.collapseList(mImEntries);
Evan Millar11d628c2009-09-02 08:55:01 -0700518
519 if (mAdapter == null) {
520 mAdapter = new ViewAdapter(this, mSections);
521 mListView.setAdapter(mAdapter);
522 } else {
523 mAdapter.setSections(mSections, SHOW_SEPARATORS);
524 }
Makoto Onuki7a707382009-11-19 13:35:55 +0900525 mListView.setEmptyView(mEmptyView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800526 }
527
528 @Override
529 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700530 super.onCreateOptionsMenu(menu);
531
532 final MenuInflater inflater = getMenuInflater();
533 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800534 return true;
535 }
536
537 @Override
538 public boolean onPrepareOptionsMenu(Menu menu) {
539 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700540
Evan Millar51514db2009-09-22 11:42:35 -0700541 // Only allow edit when we have at least one raw_contact id
542 final boolean hasRawContact = (mRawContactIds.size() > 0);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700543 menu.findItem(R.id.menu_edit).setEnabled(hasRawContact);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700544
Jeff Sharkeye0408762009-11-03 12:57:16 -0800545 // Only allow share when unrestricted contacts available
546 menu.findItem(R.id.menu_share).setEnabled(!mAllRestricted);
547
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800548 return true;
549 }
550
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800551 @Override
552 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
553 AdapterView.AdapterContextMenuInfo info;
554 try {
555 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
556 } catch (ClassCastException e) {
557 Log.e(TAG, "bad menuInfo", e);
558 return;
559 }
560
561 // This can be null sometimes, don't crash...
562 if (info == null) {
563 Log.e(TAG, "bad menuInfo");
564 return;
565 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700566
Evan Millar45e0ed32009-06-01 16:44:38 -0700567 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
Neel Parekha8fb68a2009-09-25 18:05:18 -0700568 menu.setHeaderTitle(R.string.contactOptionsTitle);
Evan Millar45e0ed32009-06-01 16:44:38 -0700569 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
570 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700571 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
572 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700573 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800574 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700575 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
576 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700577 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700578 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800579 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700580 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700581 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
582 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800583 }
584
585 @Override
586 public boolean onOptionsItemSelected(MenuItem item) {
587 switch (item.getItemId()) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700588 case R.id.menu_edit: {
Evan Millardf181202009-09-30 15:15:51 -0700589 Long rawContactIdToEdit = null;
590 if (mRawContactIds.size() > 0) {
591 rawContactIdToEdit = mRawContactIds.get(0);
592 } else {
593 // There is no rawContact to edit.
594 break;
Evan Millardb5d88c2009-08-28 09:31:57 -0700595 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700596 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
597 rawContactIdToEdit);
Evan Millar9cd81242009-09-26 16:02:31 -0700598 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
599 REQUEST_EDIT_CONTACT);
Evan Millar8a79cee2009-08-19 17:20:49 -0700600 break;
601 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700602 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800603 // Get confirmation
Evan Millarcdeea942009-10-07 11:47:35 -0700604 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
Megha Joshid5afd8a2009-09-22 09:38:41 -0700605 showDialog(DIALOG_CONFIRM_READONLY_DELETE);
Evan Millarcdeea942009-10-07 11:47:35 -0700606 } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) {
Megha Joshic508bd82009-10-01 17:31:49 -0700607 showDialog(DIALOG_CONFIRM_READONLY_HIDE);
Evan Millarcdeea942009-10-07 11:47:35 -0700608 } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) {
609 showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
610 } else {
611 showDialog(DIALOG_CONFIRM_DELETE);
612 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800613 return true;
614 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700615 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700616 showJoinAggregateActivity();
617 return true;
618 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700619 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700620 showOptionsActivity();
621 return true;
622 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700623 case R.id.menu_share: {
Jeff Sharkeye0408762009-11-03 12:57:16 -0800624 if (mAllRestricted) return false;
625
Jeff Sharkey93049312009-09-27 17:28:13 -0700626 // TODO: Keep around actual LOOKUP_KEY, or formalize method of extracting
Daniel Lehmanncbf75952010-03-12 13:29:07 -0800627 final String lookupKey = Uri.encode(mLookupUri.getPathSegments().get(2));
Jeff Sharkey93049312009-09-27 17:28:13 -0700628 final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
629
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700630 final Intent intent = new Intent(Intent.ACTION_SEND);
Jeff Sharkey93049312009-09-27 17:28:13 -0700631 intent.setType(Contacts.CONTENT_VCARD_TYPE);
632 intent.putExtra(Intent.EXTRA_STREAM, shareUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700633
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700634 // Launch chooser to share contact via
635 final CharSequence chooseTitle = getText(R.string.share_via);
636 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700637
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700638 try {
639 startActivity(chooseIntent);
640 } catch (ActivityNotFoundException ex) {
641 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800642 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700643 return true;
644 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800645 }
646 return super.onOptionsItemSelected(item);
647 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700648
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800649 @Override
650 public boolean onContextItemSelected(MenuItem item) {
651 switch (item.getItemId()) {
652 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700653 if (makeItemDefault(item)) {
654 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800655 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700656 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800657 }
658 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700659
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800660 return super.onContextItemSelected(item);
661 }
662
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700663 private boolean makeItemDefault(MenuItem item) {
664 ViewEntry entry = getViewEntryForMenuItem(item);
665 if (entry == null) {
666 return false;
667 }
668
669 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700670 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700671 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700672 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
673 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700674 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700675 return true;
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() {
Evan Millar6a61a1a2009-09-29 14:00:43 -0700682 long freshId = getRefreshedContactId();
683 if (freshId > 0) {
684 String displayName = null;
685 if (mCursor.moveToFirst()) {
686 displayName = mCursor.getString(0);
687 }
Dmitri Plotnikov501b7ea2010-04-07 17:20:49 -0700688 Intent intent = new Intent(JoinContactActivity.JOIN_CONTACT);
689 intent.putExtra(JoinContactActivity.EXTRA_TARGET_CONTACT_ID, freshId);
Evan Millar6a61a1a2009-09-29 14:00:43 -0700690 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikova5cfca32009-09-29 18:13:08 -0700691 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700692 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700693
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700694 @Override
695 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700696 if (requestCode == REQUEST_JOIN_CONTACT) {
697 if (resultCode == RESULT_OK && intent != null) {
698 final long contactId = ContentUris.parseId(intent.getData());
699 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700700 }
Evan Millar9cd81242009-09-26 16:02:31 -0700701 } else if (requestCode == REQUEST_EDIT_CONTACT) {
Evan Millardf181202009-09-30 15:15:51 -0700702 if (resultCode == EditContactActivity.RESULT_CLOSE_VIEW_ACTIVITY) {
703 finish();
Jeff Hamilton5297c6a2009-10-01 02:22:33 -0700704 } else if (resultCode == Activity.RESULT_OK) {
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -0700705 mLookupUri = intent.getData();
706 if (mLookupUri == null) {
707 finish();
708 }
Evan Millardf181202009-09-30 15:15:51 -0700709 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700710 }
711 }
712
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700713 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700714 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700715 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700716
717 try {
718 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700719 long rawContactId = c.getLong(0);
720 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700721 }
722 } finally {
723 c.close();
724 }
725
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700726 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
727 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700728 }
729
730 /**
731 * Given a contact ID sets an aggregation exception to either join the contact with the
732 * current aggregate or split off.
733 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700734 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700735 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700736 for (long aRawContactId : mRawContactIds) {
737 if (aRawContactId != rawContactId) {
738 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
739 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
740 values.put(AggregationExceptions.TYPE, exceptionType);
741 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
742 }
743 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700744 }
745
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700746 private void showOptionsActivity() {
747 final Intent intent = new Intent(this, ContactOptionsActivity.class);
Evan Millar6a61a1a2009-09-29 14:00:43 -0700748 intent.setData(mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700749 startActivity(intent);
750 }
751
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700752 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
753 AdapterView.AdapterContextMenuInfo info;
754 try {
755 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
756 } catch (ClassCastException e) {
757 Log.e(TAG, "bad menuInfo", e);
758 return null;
759 }
760
761 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
762 }
763
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800764 @Override
765 public boolean onKeyDown(int keyCode, KeyEvent event) {
766 switch (keyCode) {
767 case KeyEvent.KEYCODE_CALL: {
768 try {
769 ITelephony phone = ITelephony.Stub.asInterface(
770 ServiceManager.checkService("phone"));
771 if (phone != null && !phone.isIdle()) {
772 // Skip out and let the key be handled at a higher level
773 break;
774 }
775 } catch (RemoteException re) {
776 // Fall through and try to call the contact
777 }
778
Evan Millar7911ff52009-07-21 15:55:18 -0700779 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800780 if (index != -1) {
781 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Bai Tao67205ed2010-03-16 08:09:11 +0800782 if (entry != null &&
783 entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
Evan Millar66388be2009-05-28 15:41:07 -0700784 startActivity(entry.intent);
Bai Tao67205ed2010-03-16 08:09:11 +0800785 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800786 }
Jeff Sharkey827762d2009-12-23 11:06:08 -0800787 } else if (mPrimaryPhoneUri != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800788 // There isn't anything selected, call the default number
Jeff Sharkey827762d2009-12-23 11:06:08 -0800789 final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
790 mPrimaryPhoneUri);
791 startActivity(intent);
Bai Tao67205ed2010-03-16 08:09:11 +0800792 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800793 }
Bai Tao67205ed2010-03-16 08:09:11 +0800794 return false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800795 }
796
797 case KeyEvent.KEYCODE_DEL: {
Evan Millarcdeea942009-10-07 11:47:35 -0700798 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
Megha Joshid5afd8a2009-09-22 09:38:41 -0700799 showDialog(DIALOG_CONFIRM_READONLY_DELETE);
Evan Millarcdeea942009-10-07 11:47:35 -0700800 } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) {
Megha Joshic508bd82009-10-01 17:31:49 -0700801 showDialog(DIALOG_CONFIRM_READONLY_HIDE);
Evan Millarcdeea942009-10-07 11:47:35 -0700802 } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) {
803 showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
804 } else {
805 showDialog(DIALOG_CONFIRM_DELETE);
806 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800807 return true;
808 }
809 }
810
811 return super.onKeyDown(keyCode, event);
812 }
813
Evan Millar7911ff52009-07-21 15:55:18 -0700814 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800815 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
816 if (entry != null) {
817 Intent intent = entry.intent;
Evan Millardf181202009-09-30 15:15:51 -0700818 if (intent != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800819 try {
820 startActivity(intent);
821 } catch (ActivityNotFoundException e) {
822 Log.e(TAG, "No activity found for intent: " + intent);
823 signalError();
824 }
825 } else {
826 signalError();
827 }
828 } else {
829 signalError();
830 }
831 }
832
833 /**
834 * Signal an error to the user via a beep, or some other method.
835 */
836 private void signalError() {
837 //TODO: implement this when we have the sonification APIs
838 }
839
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800840 /**
841 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700842 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800843 * @param personCursor the URI for the contact being displayed
844 */
Evan Millar11d628c2009-09-02 08:55:01 -0700845 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800846 // Clear out the old entries
847 final int numSections = mSections.size();
848 for (int i = 0; i < numSections; i++) {
849 mSections.get(i).clear();
850 }
851
Evan Millar7911ff52009-07-21 15:55:18 -0700852 mRawContactIds.clear();
Jeff Sharkeye0408762009-11-03 12:57:16 -0800853
Megha Joshid5afd8a2009-09-22 09:38:41 -0700854 mReadOnlySourcesCnt = 0;
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700855 mWritableSourcesCnt = 0;
Jeff Sharkeye0408762009-11-03 12:57:16 -0800856 mAllRestricted = true;
Jeff Sharkey827762d2009-12-23 11:06:08 -0800857 mPrimaryPhoneUri = null;
Jeff Sharkeye0408762009-11-03 12:57:16 -0800858
Megha Joshid5afd8a2009-09-22 09:38:41 -0700859 mWritableRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700860
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700861 final Context context = this;
862 final Sources sources = Sources.getInstance(context);
Evan Millar11d628c2009-09-02 08:55:01 -0700863
Evan Millar66388be2009-05-28 15:41:07 -0700864 // Build up method entries
Evan Millar6a61a1a2009-09-29 14:00:43 -0700865 if (mLookupUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700866 for (Entity entity: mEntities) {
867 final ContentValues entValues = entity.getEntityValues();
868 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700869 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700870
Jeff Sharkeye0408762009-11-03 12:57:16 -0800871 // Mark when this contact has any unrestricted components
872 final boolean isRestricted = entValues.getAsInteger(RawContacts.IS_RESTRICTED) != 0;
873 if (!isRestricted) mAllRestricted = false;
874
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700875 if (!mRawContactIds.contains(rawContactId)) {
876 mRawContactIds.add(rawContactId);
877 }
Megha Joshic508bd82009-10-01 17:31:49 -0700878 ContactsSource contactsSource = sources.getInflatedSource(accountType,
879 ContactsSource.LEVEL_SUMMARY);
880 if (contactsSource != null && contactsSource.readOnly) {
881 mReadOnlySourcesCnt += 1;
882 } else {
883 mWritableSourcesCnt += 1;
Evan Millarcdeea942009-10-07 11:47:35 -0700884 mWritableRawContactIds.add(rawContactId);
885 }
Megha Joshic508bd82009-10-01 17:31:49 -0700886
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700887
Evan Millar11d628c2009-09-02 08:55:01 -0700888 for (NamedContentValues subValue : entity.getSubValues()) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700889 final ContentValues entryValues = subValue.values;
890 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
891
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700892 final long dataId = entryValues.getAsLong(Data._ID);
893 final String mimeType = entryValues.getAsString(Data.MIMETYPE);
894 if (mimeType == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -0700895
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700896 final DataKind kind = sources.getKindOrFallback(accountType, mimeType, this,
Evan Millar11d628c2009-09-02 08:55:01 -0700897 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700898 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800899
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700900 final ViewEntry entry = ViewEntry.fromValues(context, mimeType, kind,
901 rawContactId, dataId, entryValues);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800902
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700903 final boolean hasData = !TextUtils.isEmpty(entry.data);
Evan Millarff04a272009-09-26 15:23:37 -0700904 final boolean isSuperPrimary = entryValues.getAsInteger(
905 Data.IS_SUPER_PRIMARY) != 0;
Evan Millar11d628c2009-09-02 08:55:01 -0700906
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700907 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
Evan Millarff04a272009-09-26 15:23:37 -0700908 // Build phone entries
909 mNumPhoneNumbers++;
Evan Millar11d628c2009-09-02 08:55:01 -0700910
Evan Millarff04a272009-09-26 15:23:37 -0700911 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700912 Uri.fromParts(Constants.SCHEME_TEL, entry.data, null));
Evan Millarff04a272009-09-26 15:23:37 -0700913 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700914 Uri.fromParts(Constants.SCHEME_SMSTO, entry.data, null));
Evan Millar49714ee2009-09-02 16:42:47 -0700915
Jeff Sharkey827762d2009-12-23 11:06:08 -0800916 // Remember super-primary phone
917 if (isSuperPrimary) mPrimaryPhoneUri = entry.uri;
918
Evan Millarff04a272009-09-26 15:23:37 -0700919 entry.isPrimary = isSuperPrimary;
920 mPhoneEntries.add(entry);
921
922 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
923 || mShowSmsLinksForAllPhones) {
924 // Add an SMS entry
925 if (kind.iconAltRes > 0) {
926 entry.secondaryActionIcon = kind.iconAltRes;
Evan Millar49714ee2009-09-02 16:42:47 -0700927 }
Evan Millarff04a272009-09-26 15:23:37 -0700928 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700929 } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
Evan Millarff04a272009-09-26 15:23:37 -0700930 // Build email entries
931 entry.intent = new Intent(Intent.ACTION_SENDTO,
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700932 Uri.fromParts(Constants.SCHEME_MAILTO, entry.data, null));
Evan Millarff04a272009-09-26 15:23:37 -0700933 entry.isPrimary = isSuperPrimary;
934 mEmailEntries.add(entry);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700935
936 // When Email rows have status, create additional Im row
937 final DataStatus status = mStatuses.get(entry.id);
938 if (status != null) {
939 final String imMime = Im.CONTENT_ITEM_TYPE;
940 final DataKind imKind = sources.getKindOrFallback(accountType,
941 imMime, this, ContactsSource.LEVEL_MIMETYPES);
942 final ViewEntry imEntry = ViewEntry.fromValues(context,
943 imMime, imKind, rawContactId, dataId, entryValues);
944 imEntry.intent = ContactsUtils.buildImIntent(entryValues);
945 imEntry.applyStatus(status, false);
946 mImEntries.add(imEntry);
947 }
948 } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
Evan Millarff04a272009-09-26 15:23:37 -0700949 // Build postal entries
950 entry.maxLines = 4;
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700951 entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri);
Evan Millarff04a272009-09-26 15:23:37 -0700952 mPostalEntries.add(entry);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700953 } else if (Im.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
954 // Build IM entries
955 entry.intent = ContactsUtils.buildImIntent(entryValues);
Evan Millarff04a272009-09-26 15:23:37 -0700956 if (TextUtils.isEmpty(entry.label)) {
957 entry.label = getString(R.string.chat).toLowerCase();
958 }
Evan Millar11d628c2009-09-02 08:55:01 -0700959
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700960 // Apply presence and status details when available
961 final DataStatus status = mStatuses.get(entry.id);
962 if (status != null) {
963 entry.applyStatus(status, false);
Evan Millarff04a272009-09-26 15:23:37 -0700964 }
Evan Millarff04a272009-09-26 15:23:37 -0700965 mImEntries.add(entry);
Daniel Lehmanne12f8a92010-02-24 18:37:56 -0800966 } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType) &&
967 (hasData || !TextUtils.isEmpty(entry.label))) {
968 // Build organization entries
Daniel Lehmann350ba6f2010-03-23 18:10:57 -0700969 final boolean isNameRawContact = (mNameRawContactId == rawContactId);
970
971 final boolean duplicatesTitle =
972 isNameRawContact
973 && mDisplayNameSource == DisplayNameSources.ORGANIZATION
Daniel Lehmann15a194a2010-03-24 13:45:05 -0700974 && (!hasData || TextUtils.isEmpty(entry.label));
Daniel Lehmann350ba6f2010-03-23 18:10:57 -0700975
976 if (!duplicatesTitle) {
977 entry.uri = null;
Daniel Lehmann15a194a2010-03-24 13:45:05 -0700978
979 if (TextUtils.isEmpty(entry.label)) {
980 entry.label = entry.data;
981 entry.data = "";
982 }
983
Daniel Lehmann350ba6f2010-03-23 18:10:57 -0700984 mOrganizationEntries.add(entry);
985 }
Daniel Lehmanne12f8a92010-02-24 18:37:56 -0800986 } else if (Nickname.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
987 // Build nickname entries
Daniel Lehmann350ba6f2010-03-23 18:10:57 -0700988 final boolean isNameRawContact = (mNameRawContactId == rawContactId);
989
990 final boolean duplicatesTitle =
991 isNameRawContact
992 && mDisplayNameSource == DisplayNameSources.NICKNAME;
993
994 if (!duplicatesTitle) {
995 entry.uri = null;
996 mNicknameEntries.add(entry);
997 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700998 } else if (Note.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
Evan Millar11d628c2009-09-02 08:55:01 -0700999 // Build note entries
Evan Millar11d628c2009-09-02 08:55:01 -07001000 entry.uri = null;
Daniel Lehmann0e322012010-02-25 16:09:46 -08001001 entry.maxLines = 100;
Evan Millar11d628c2009-09-02 08:55:01 -07001002 mOtherEntries.add(entry);
Evan Millar951fa9c2009-10-26 14:57:43 -07001003 } else if (Website.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
1004 // Build note entries
1005 entry.uri = null;
1006 entry.maxLines = 10;
1007 try {
1008 WebAddress webAddress = new WebAddress(entry.data);
Makoto Onuki7a707382009-11-19 13:35:55 +09001009 entry.intent = new Intent(Intent.ACTION_VIEW,
Evan Millar951fa9c2009-10-26 14:57:43 -07001010 Uri.parse(webAddress.toString()));
1011 } catch (ParseException e) {
1012 Log.e(TAG, "Couldn't parse website: " + entry.data);
1013 }
1014 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -07001015 } else {
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001016 // Handle showing custom rows
Evan Millarff04a272009-09-26 15:23:37 -07001017 entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri);
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001018
1019 // Use social summary when requested by external source
1020 final DataStatus status = mStatuses.get(entry.id);
1021 final boolean hasSocial = kind.actionBodySocial && status != null;
1022 if (hasSocial) {
1023 entry.applyStatus(status, true);
1024 }
1025
1026 if (hasSocial || hasData) {
1027 mOtherEntries.add(entry);
1028 }
Evan Millar11d628c2009-09-02 08:55:01 -07001029 }
Evan Millar2e1b8af2009-09-24 09:39:21 -07001030 }
Evan Millar66388be2009-05-28 15:41:07 -07001031 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001032 }
1033 }
1034
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001035 static String buildActionString(DataKind kind, ContentValues values, boolean lowerCase,
1036 Context context) {
Evan Millar11d628c2009-09-02 08:55:01 -07001037 if (kind.actionHeader == null) {
1038 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001039 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001040 CharSequence actionHeader = kind.actionHeader.inflateUsing(context, values);
Evan Millar11d628c2009-09-02 08:55:01 -07001041 if (actionHeader == null) {
1042 return null;
1043 }
1044 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1045 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001046
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001047 static String buildDataString(DataKind kind, ContentValues values, Context context) {
Evan Millar11d628c2009-09-02 08:55:01 -07001048 if (kind.actionBody == null) {
1049 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001050 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001051 CharSequence actionBody = kind.actionBody.inflateUsing(context, values);
Evan Millar11d628c2009-09-02 08:55:01 -07001052 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001053 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001054
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001055 /**
1056 * A basic structure with the data for a contact entry in the list.
1057 */
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001058 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
1059 public Context context = null;
Jeff Sharkeyab066932009-09-21 09:55:30 -07001060 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001061 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001062 public boolean isPrimary = false;
Evan Millar15e514d2009-08-04 10:14:57 -07001063 public int secondaryActionIcon = -1;
1064 public Intent intent;
1065 public Intent secondaryIntent = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001066 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001067 public ArrayList<Long> ids = new ArrayList<Long>();
1068 public int collapseCount = 0;
1069
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001070 public int presence = -1;
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001071
1072 public CharSequence footerLine = null;
1073
1074 private ViewEntry() {
1075 }
1076
1077 /**
1078 * Build new {@link ViewEntry} and populate from the given values.
1079 */
1080 public static ViewEntry fromValues(Context context, String mimeType, DataKind kind,
1081 long rawContactId, long dataId, ContentValues values) {
1082 final ViewEntry entry = new ViewEntry();
1083 entry.context = context;
1084 entry.contactId = rawContactId;
1085 entry.id = dataId;
1086 entry.uri = ContentUris.withAppendedId(Data.CONTENT_URI, entry.id);
1087 entry.mimetype = mimeType;
1088 entry.label = buildActionString(kind, values, false, context);
1089 entry.data = buildDataString(kind, values, context);
1090
1091 if (kind.typeColumn != null && values.containsKey(kind.typeColumn)) {
1092 entry.type = values.getAsInteger(kind.typeColumn);
1093 }
1094 if (kind.iconRes > 0) {
1095 entry.resPackageName = kind.resPackageName;
1096 entry.actionIcon = kind.iconRes;
1097 }
1098
1099 return entry;
1100 }
1101
1102 /**
1103 * Apply given {@link DataStatus} values over this {@link ViewEntry}
1104 *
1105 * @param fillData When true, the given status replaces {@link #data}
1106 * and {@link #footerLine}. Otherwise only {@link #presence}
1107 * is updated.
1108 */
1109 public ViewEntry applyStatus(DataStatus status, boolean fillData) {
1110 presence = status.getPresence();
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001111 if (fillData && status.isValid()) {
1112 this.data = status.getStatus().toString();
1113 this.footerLine = status.getTimestampLabel(context);
1114 }
1115
1116 return this;
1117 }
1118
Evan Millar54a5c9f2009-06-23 17:41:09 -07001119 public boolean collapseWith(ViewEntry entry) {
1120 // assert equal collapse keys
Evan Millaradb0f8c2009-09-28 17:20:50 -07001121 if (!shouldCollapseWith(entry)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001122 return false;
1123 }
1124
1125 // Choose the label associated with the highest type precedence.
1126 if (TypePrecedence.getTypePrecedence(mimetype, type)
1127 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1128 type = entry.type;
1129 label = entry.label;
1130 }
1131
1132 // Choose the max of the maxLines and maxLabelLines values.
1133 maxLines = Math.max(maxLines, entry.maxLines);
1134 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1135
1136 // Choose the presence with the highest precedence.
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001137 if (StatusUpdates.getPresencePrecedence(presence)
1138 < StatusUpdates.getPresencePrecedence(entry.presence)) {
1139 presence = entry.presence;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001140 }
1141
1142 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001143 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001144
1145 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1146
1147 // Keep track of all the ids that have been collapsed with this one.
1148 ids.add(entry.id);
1149 collapseCount++;
1150 return true;
1151 }
1152
Evan Millaradb0f8c2009-09-28 17:20:50 -07001153 public boolean shouldCollapseWith(ViewEntry entry) {
1154 if (entry == null) {
1155 return false;
1156 }
1157
Daniel Lehmannd8b0a052010-03-25 17:41:00 -07001158 if (!ContactsUtils.shouldCollapse(context, mimetype, data, entry.mimetype,
1159 entry.data)) {
Makoto Onukic710b0e2009-10-13 15:43:24 -07001160 return false;
Evan Millaradb0f8c2009-09-28 17:20:50 -07001161 }
1162
Makoto Onukic710b0e2009-10-13 15:43:24 -07001163 if (!TextUtils.equals(mimetype, entry.mimetype)
1164 || !ContactsUtils.areIntentActionEqual(intent, entry.intent)
1165 || !ContactsUtils.areIntentActionEqual(secondaryIntent, entry.secondaryIntent)
Evan Millaradb0f8c2009-09-28 17:20:50 -07001166 || actionIcon != entry.actionIcon) {
1167 return false;
1168 }
1169
1170 return true;
1171 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001172 }
1173
Evan Millar15e514d2009-08-04 10:14:57 -07001174 /** Cache of the children views of a row */
1175 static class ViewCache {
1176 public TextView label;
1177 public TextView data;
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001178 public TextView footer;
Evan Millar15e514d2009-08-04 10:14:57 -07001179 public ImageView actionIcon;
1180 public ImageView presenceIcon;
1181 public ImageView primaryIcon;
1182 public ImageView secondaryActionButton;
1183 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001184
Evan Millar15e514d2009-08-04 10:14:57 -07001185 // Need to keep track of this too
1186 ViewEntry entry;
1187 }
1188
1189 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1190 implements View.OnClickListener {
1191
Evan Millar5c22c3b2009-05-29 11:37:54 -07001192
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001193 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1194 super(context, sections, SHOW_SEPARATORS);
1195 }
1196
Evan Millar15e514d2009-08-04 10:14:57 -07001197 public void onClick(View v) {
1198 Intent intent = (Intent) v.getTag();
1199 startActivity(intent);
1200 }
1201
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001202 @Override
1203 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001204 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001205 View v;
1206
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001207 ViewCache views;
1208
1209 // Check to see if we can reuse convertView
1210 if (convertView != null) {
1211 v = convertView;
1212 views = (ViewCache) v.getTag();
1213 } else {
1214 // Create a new view if needed
1215 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1216
1217 // Cache the children
1218 views = new ViewCache();
1219 views.label = (TextView) v.findViewById(android.R.id.text1);
1220 views.data = (TextView) v.findViewById(android.R.id.text2);
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001221 views.footer = (TextView) v.findViewById(R.id.footer);
Evan Millar15e514d2009-08-04 10:14:57 -07001222 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1223 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1224 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1225 views.secondaryActionButton = (ImageView) v.findViewById(
1226 R.id.secondary_action_button);
1227 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001228 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001229 v.setTag(views);
1230 }
1231
1232 // Update the entry in the view cache
1233 views.entry = entry;
1234
1235 // Bind the data to the view
1236 bindView(v, entry);
1237 return v;
1238 }
1239
1240 @Override
1241 protected View newView(int position, ViewGroup parent) {
1242 // getView() handles this
1243 throw new UnsupportedOperationException();
1244 }
1245
1246 @Override
1247 protected void bindView(View view, ViewEntry entry) {
1248 final Resources resources = mContext.getResources();
1249 ViewCache views = (ViewCache) view.getTag();
1250
1251 // Set the label
1252 TextView label = views.label;
1253 setMaxLines(label, entry.maxLabelLines);
1254 label.setText(entry.label);
1255
1256 // Set the data
1257 TextView data = views.data;
1258 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001259 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001260 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001261 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1262 } else {
1263 data.setText(entry.data);
1264 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001265 setMaxLines(data, entry.maxLines);
1266 }
1267
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001268 // Set the footer
1269 if (!TextUtils.isEmpty(entry.footerLine)) {
1270 views.footer.setText(entry.footerLine);
1271 views.footer.setVisibility(View.VISIBLE);
1272 } else {
1273 views.footer.setVisibility(View.GONE);
1274 }
1275
Evan Millar15e514d2009-08-04 10:14:57 -07001276 // Set the primary icon
1277 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1278
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001279 // Set the action icon
1280 ImageView action = views.actionIcon;
1281 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001282 Drawable actionIcon;
1283 if (entry.resPackageName != null) {
1284 // Load external resources through PackageManager
1285 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1286 entry.actionIcon, null);
1287 } else {
1288 actionIcon = resources.getDrawable(entry.actionIcon);
1289 }
1290 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001291 action.setVisibility(View.VISIBLE);
1292 } else {
1293 // Things should still line up as if there was an icon, so make it invisible
1294 action.setVisibility(View.INVISIBLE);
1295 }
1296
1297 // Set the presence icon
Bai Tao107736c2010-03-12 08:00:42 +08001298 Drawable presenceIcon = ContactPresenceIconUtil.getPresenceIcon(
1299 mContext, entry.presence);
Evan Millar15e514d2009-08-04 10:14:57 -07001300 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001301 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001302 presenceIconView.setImageDrawable(presenceIcon);
1303 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001304 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001305 presenceIconView.setVisibility(View.GONE);
1306 }
1307
1308 // Set the secondary action button
1309 ImageView secondaryActionView = views.secondaryActionButton;
1310 Drawable secondaryActionIcon = null;
1311 if (entry.secondaryActionIcon != -1) {
1312 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1313 }
1314 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1315 secondaryActionView.setImageDrawable(secondaryActionIcon);
1316 secondaryActionView.setTag(entry.secondaryIntent);
1317 secondaryActionView.setVisibility(View.VISIBLE);
1318 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1319 } else {
1320 secondaryActionView.setVisibility(View.GONE);
1321 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001322 }
1323 }
1324
1325 private void setMaxLines(TextView textView, int maxLines) {
1326 if (maxLines == 1) {
1327 textView.setSingleLine(true);
1328 textView.setEllipsize(TextUtils.TruncateAt.END);
1329 } else {
1330 textView.setSingleLine(false);
1331 textView.setMaxLines(maxLines);
1332 textView.setEllipsize(null);
1333 }
1334 }
1335 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001336
1337 private interface StatusQuery {
1338 final String[] PROJECTION = new String[] {
1339 Data._ID,
1340 Data.STATUS,
1341 Data.STATUS_RES_PACKAGE,
1342 Data.STATUS_ICON,
1343 Data.STATUS_LABEL,
1344 Data.STATUS_TIMESTAMP,
1345 Data.PRESENCE,
1346 };
1347
1348 final int _ID = 0;
1349 }
Dmitri Plotnikov8e86b752010-02-22 17:47:57 -08001350
1351 @Override
1352 public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData,
1353 boolean globalSearch) {
1354 if (globalSearch) {
1355 super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
1356 } else {
1357 ContactsSearchManager.startSearch(this, initialQuery);
1358 }
1359 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001360}