blob: 8402b3fbfb6ea46e602123bb2f8df49f674cdabf [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.model.ContactsSource;
21import com.android.contacts.model.Sources;
22import com.android.contacts.model.ContactsSource.DataKind;
Evan Millardf181202009-09-30 15:15:51 -070023import com.android.contacts.ui.EditContactActivity;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070024import com.android.contacts.util.Constants;
Jeff Sharkey624ddc32009-10-01 21:32:19 -070025import com.android.contacts.util.DataStatus;
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;
Jeff Sharkey624ddc32009-10-01 21:32:19 -070029import com.google.android.collect.Lists;
30import com.google.android.collect.Maps;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080031
Evan Millar11d628c2009-09-02 08:55:01 -070032import android.app.Activity;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080033import android.app.AlertDialog;
34import android.app.Dialog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080035import android.content.ActivityNotFoundException;
36import android.content.ContentResolver;
37import android.content.ContentUris;
38import android.content.ContentValues;
39import android.content.Context;
40import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070041import android.content.Entity;
Evan Millar11d628c2009-09-02 08:55:01 -070042import android.content.EntityIterator;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080043import android.content.Intent;
Evan Millar11d628c2009-09-02 08:55:01 -070044import android.content.Entity.NamedContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080045import android.content.res.Resources;
46import android.database.ContentObserver;
47import android.database.Cursor;
48import android.graphics.drawable.Drawable;
Evan Millar951fa9c2009-10-26 14:57:43 -070049import android.net.ParseException;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080050import android.net.Uri;
Evan Millar951fa9c2009-10-26 14:57:43 -070051import android.net.WebAddress;
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;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070061import android.provider.ContactsContract.RawContacts;
Fred Quintana8579e252009-12-07 14:50:12 -080062import android.provider.ContactsContract.RawContactsEntity;
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -070063import android.provider.ContactsContract.StatusUpdates;
Jeff Sharkey624ddc32009-10-01 21:32:19 -070064import android.provider.ContactsContract.CommonDataKinds.Email;
65import android.provider.ContactsContract.CommonDataKinds.Im;
66import android.provider.ContactsContract.CommonDataKinds.Nickname;
67import android.provider.ContactsContract.CommonDataKinds.Note;
68import android.provider.ContactsContract.CommonDataKinds.Organization;
Evan Millar54a5c9f2009-06-23 17:41:09 -070069import android.provider.ContactsContract.CommonDataKinds.Phone;
Jeff Sharkey624ddc32009-10-01 21:32:19 -070070import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
Evan Millar951fa9c2009-10-26 14:57:43 -070071import android.provider.ContactsContract.CommonDataKinds.Website;
Evan Millar54a5c9f2009-06-23 17:41:09 -070072import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080073import android.text.TextUtils;
74import android.util.Log;
75import android.view.ContextMenu;
76import android.view.KeyEvent;
Evan Millar11d628c2009-09-02 08:55:01 -070077import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080078import android.view.Menu;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070079import android.view.MenuInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080080import android.view.MenuItem;
81import android.view.View;
82import android.view.ViewGroup;
Evan Millar11d628c2009-09-02 08:55:01 -070083import android.view.Window;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080084import android.view.ContextMenu.ContextMenuInfo;
85import android.widget.AdapterView;
Evan Millar7911ff52009-07-21 15:55:18 -070086import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087import android.widget.ImageView;
88import android.widget.ListView;
Evan Millarcdeea942009-10-07 11:47:35 -070089import android.widget.ScrollView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080090import android.widget.TextView;
91import android.widget.Toast;
92
93import java.util.ArrayList;
Jeff Sharkey624ddc32009-10-01 21:32:19 -070094import java.util.HashMap;
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>();
131 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700132 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800133 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
134 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
135
136 private Cursor mCursor;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700137
Evan Millar11d628c2009-09-02 08:55:01 -0700138 protected ContactHeaderWidget mContactHeaderWidget;
139 private NotifyingAsyncQueryHandler mHandler;
140
141 protected LayoutInflater mInflater;
142
Megha Joshid5afd8a2009-09-22 09:38:41 -0700143 protected int mReadOnlySourcesCnt;
144 protected int mWritableSourcesCnt;
Jeff Sharkeye0408762009-11-03 12:57:16 -0800145 protected boolean mAllRestricted;
146
Jeff Sharkey827762d2009-12-23 11:06:08 -0800147 protected Uri mPrimaryPhoneUri = null;
148
Megha Joshid5afd8a2009-09-22 09:38:41 -0700149 protected ArrayList<Long> mWritableRawContactIds = new ArrayList<Long>();
Evan Millar11d628c2009-09-02 08:55:01 -0700150
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700151 private static final int TOKEN_ENTITIES = 0;
152 private static final int TOKEN_STATUSES = 1;
153
154 private boolean mHasEntities = false;
155 private boolean mHasStatuses = false;
156
157 private ArrayList<Entity> mEntities = Lists.newArrayList();
158 private HashMap<Long, DataStatus> mStatuses = Maps.newHashMap();
Evan Millar11d628c2009-09-02 08:55:01 -0700159
Makoto Onuki7a707382009-11-19 13:35:55 +0900160 /**
161 * The view shown if the detail list is empty.
162 * We set this to the list view when first bind the adapter, so that it won't be shown while
163 * we're loading data.
164 */
165 private View mEmptyView;
166
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800167 private ContentObserver mObserver = new ContentObserver(new Handler()) {
168 @Override
169 public boolean deliverSelfNotifications() {
170 return true;
171 }
172
173 @Override
174 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700175 if (mCursor != null && !mCursor.isClosed()) {
176 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800177 }
178 }
179 };
180
181 public void onClick(DialogInterface dialog, int which) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700182 closeCursor();
Megha Joshic508bd82009-10-01 17:31:49 -0700183 getContentResolver().delete(mLookupUri, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800184 finish();
185 }
186
Evan Millar7911ff52009-07-21 15:55:18 -0700187 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800188 private boolean mShowSmsLinksForAllPhones;
189
190 @Override
191 protected void onCreate(Bundle icicle) {
192 super.onCreate(icicle);
193
Evan Millar11d628c2009-09-02 08:55:01 -0700194 final Intent intent = getIntent();
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700195 Uri data = intent.getData();
196 String authority = data.getAuthority();
197 if (ContactsContract.AUTHORITY.equals(authority)) {
198 mLookupUri = data;
199 } else if (android.provider.Contacts.AUTHORITY.equals(authority)) {
200 final long rawContactId = ContentUris.parseId(data);
201 mLookupUri = RawContacts.getContactLookupUri(getContentResolver(),
202 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700203
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700204 }
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700205 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700206
207 requestWindowFeature(Window.FEATURE_NO_TITLE);
208 setContentView(R.layout.contact_card_layout);
209
210 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
211 mContactHeaderWidget.showStar(true);
Evan Millar11d628c2009-09-02 08:55:01 -0700212 mContactHeaderWidget.setExcludeMimes(new String[] {
213 Contacts.CONTENT_ITEM_TYPE
214 });
215
Evan Millar11d628c2009-09-02 08:55:01 -0700216 mHandler = new NotifyingAsyncQueryHandler(this, this);
217
Evan Millarcdeea942009-10-07 11:47:35 -0700218 mListView = (ListView) findViewById(R.id.contact_data);
Evan Millar7911ff52009-07-21 15:55:18 -0700219 mListView.setOnCreateContextMenuListener(this);
220 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
221 mListView.setOnItemClickListener(this);
Makoto Onuki7a707382009-11-19 13:35:55 +0900222 // Don't set it to mListView yet. We do so later when we bind the adapter.
223 mEmptyView = findViewById(android.R.id.empty);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800224
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800225 mResolver = getContentResolver();
226
227 // Build the list of sections. The order they're added to mSections dictates the
228 // order they are displayed in the list.
229 mSections.add(mPhoneEntries);
230 mSections.add(mSmsEntries);
231 mSections.add(mEmailEntries);
232 mSections.add(mImEntries);
233 mSections.add(mPostalEntries);
234 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700235 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800236 mSections.add(mOtherEntries);
237
238 //TODO Read this value from a preference
239 mShowSmsLinksForAllPhones = true;
Evan Millar11d628c2009-09-02 08:55:01 -0700240 }
241
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800242 @Override
243 protected void onResume() {
244 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700245 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800246 }
247
248 @Override
249 protected void onPause() {
250 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700251 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800252 }
253
254 @Override
255 protected void onDestroy() {
256 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700257 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800258 }
259
260 @Override
261 protected Dialog onCreateDialog(int id) {
262 switch (id) {
263 case DIALOG_CONFIRM_DELETE:
264 return new AlertDialog.Builder(this)
265 .setTitle(R.string.deleteConfirmation_title)
266 .setIcon(android.R.drawable.ic_dialog_alert)
267 .setMessage(R.string.deleteConfirmation)
268 .setNegativeButton(android.R.string.cancel, null)
269 .setPositiveButton(android.R.string.ok, this)
270 .setCancelable(false)
271 .create();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700272 case DIALOG_CONFIRM_READONLY_DELETE:
273 return new AlertDialog.Builder(this)
274 .setTitle(R.string.deleteConfirmation_title)
275 .setIcon(android.R.drawable.ic_dialog_alert)
276 .setMessage(R.string.readOnlyContactDeleteConfirmation)
277 .setNegativeButton(android.R.string.cancel, null)
278 .setPositiveButton(android.R.string.ok, this)
279 .setCancelable(false)
280 .create();
281 case DIALOG_CONFIRM_MULTIPLE_DELETE:
282 return new AlertDialog.Builder(this)
283 .setTitle(R.string.deleteConfirmation_title)
284 .setIcon(android.R.drawable.ic_dialog_alert)
285 .setMessage(R.string.multipleContactDeleteConfirmation)
286 .setNegativeButton(android.R.string.cancel, null)
287 .setPositiveButton(android.R.string.ok, this)
288 .setCancelable(false)
289 .create();
Evan Millarcdeea942009-10-07 11:47:35 -0700290 case DIALOG_CONFIRM_READONLY_HIDE: {
Megha Joshic508bd82009-10-01 17:31:49 -0700291 return new AlertDialog.Builder(this)
292 .setTitle(R.string.deleteConfirmation_title)
293 .setIcon(android.R.drawable.ic_dialog_alert)
294 .setMessage(R.string.readOnlyContactWarning)
295 .setPositiveButton(android.R.string.ok, this)
Evan Millarcdeea942009-10-07 11:47:35 -0700296 .create();
Megha Joshic508bd82009-10-01 17:31:49 -0700297 }
298
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800299 }
300 return null;
301 }
302
Evan Millar11d628c2009-09-02 08:55:01 -0700303 /** {@inheritDoc} */
Fred Quintana8579e252009-12-07 14:50:12 -0800304 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
305 if (token == TOKEN_STATUSES) {
306 try {
307 // Read available social rows and consider binding
308 readStatuses(cursor);
309 } finally {
310 if (cursor != null) {
311 cursor.close();
312 }
313 }
314 } else {
315 EntityIterator iterator = RawContacts.newEntityIterator(cursor);
316 try {
317 // Read incoming entities and consider binding
318 readEntities(iterator);
319 } finally {
Evan Millar11d628c2009-09-02 08:55:01 -0700320 iterator.close();
321 }
322 }
Fred Quintana8579e252009-12-07 14:50:12 -0800323 considerBindData();
Evan Millar11d628c2009-09-02 08:55:01 -0700324 }
325
Evan Millar6a61a1a2009-09-29 14:00:43 -0700326 private long getRefreshedContactId() {
327 Uri freshContactUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
328 if (freshContactUri != null) {
329 return ContentUris.parseId(freshContactUri);
330 }
331 return -1;
332 }
333
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700334 /**
335 * Read from the given {@link EntityIterator} to build internal set of
336 * {@link #mEntities} for data display.
337 */
338 private synchronized void readEntities(EntityIterator iterator) {
339 mEntities.clear();
Evan Millar11d628c2009-09-02 08:55:01 -0700340 try {
341 while (iterator.hasNext()) {
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700342 mEntities.add(iterator.next());
Evan Millar11d628c2009-09-02 08:55:01 -0700343 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700344 mHasEntities = true;
Evan Millar11d628c2009-09-02 08:55:01 -0700345 } catch (RemoteException e) {
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700346 Log.w(TAG, "Problem reading contact data: " + e.toString());
Evan Millar11d628c2009-09-02 08:55:01 -0700347 }
Evan Millar11d628c2009-09-02 08:55:01 -0700348 }
349
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700350 /**
351 * Read from the given {@link Cursor} and build a set of {@link DataStatus}
352 * objects to match any valid statuses found.
353 */
354 private synchronized void readStatuses(Cursor cursor) {
355 mStatuses.clear();
356
357 // Walk found statuses, creating internal row for each
358 while (cursor.moveToNext()) {
359 final DataStatus status = new DataStatus(cursor);
360 final long dataId = cursor.getLong(StatusQuery._ID);
361 mStatuses.put(dataId, status);
362 }
363
364 mHasStatuses = true;
365 }
366
367 private synchronized void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700368 closeCursor();
369
Evan Millar6a61a1a2009-09-29 14:00:43 -0700370 Uri uri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700371 if (mLookupUri != null) {
372 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
373 if (mLookupUri != null) {
Evan Millar6a61a1a2009-09-29 14:00:43 -0700374 uri = Contacts.lookupContact(getContentResolver(), mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700375 }
376 }
377
Evan Millar6a61a1a2009-09-29 14:00:43 -0700378 if (uri == null) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700379
380 // TODO either figure out a way to prevent a flash of black background or
381 // use some other UI than a toast
382 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
383 Log.e(TAG, "invalid contact uri: " + mLookupUri);
384 finish();
385 return;
386 }
387
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700388 final Uri dataUri = Uri.withAppendedPath(uri, Contacts.Data.CONTENT_DIRECTORY);
389
390 // Keep stub cursor open on side to watch for change events
391 mCursor = mResolver.query(dataUri,
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700392 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
393 mCursor.registerContentObserver(mObserver);
394
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700395 final long contactId = ContentUris.parseId(uri);
396
397 // Clear flags and start queries to data and status
398 mHasEntities = false;
399 mHasStatuses = false;
400
Fred Quintana8579e252009-12-07 14:50:12 -0800401 mHandler.startQuery(TOKEN_ENTITIES, null, RawContactsEntity.CONTENT_URI, null,
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700402 RawContacts.CONTACT_ID + "=" + contactId, null, null);
403 mHandler.startQuery(TOKEN_STATUSES, null, dataUri, StatusQuery.PROJECTION,
404 StatusUpdates.PRESENCE + " IS NOT NULL OR " + StatusUpdates.STATUS
405 + " IS NOT NULL", null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700406
Evan Millar9b1a1242009-09-16 10:26:10 -0700407 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700408 }
409
410 private void closeCursor() {
411 if (mCursor != null) {
412 mCursor.unregisterContentObserver(mObserver);
413 mCursor.close();
414 mCursor = null;
415 }
Evan Millar11d628c2009-09-02 08:55:01 -0700416 }
417
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700418 /**
419 * Consider binding views after any of several background queries has
420 * completed. We check internal flags and only bind when all data has
421 * arrived.
422 */
423 private void considerBindData() {
424 if (mHasEntities && mHasStatuses) {
425 bindData();
426 }
427 }
428
Evan Millar11d628c2009-09-02 08:55:01 -0700429 private void bindData() {
430
431 // Build up the contact entries
432 buildEntries();
433
434 // Collapse similar data items in select sections.
435 Collapser.collapseList(mPhoneEntries);
436 Collapser.collapseList(mSmsEntries);
437 Collapser.collapseList(mEmailEntries);
438 Collapser.collapseList(mPostalEntries);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700439 Collapser.collapseList(mImEntries);
Evan Millar11d628c2009-09-02 08:55:01 -0700440
441 if (mAdapter == null) {
442 mAdapter = new ViewAdapter(this, mSections);
443 mListView.setAdapter(mAdapter);
444 } else {
445 mAdapter.setSections(mSections, SHOW_SEPARATORS);
446 }
Makoto Onuki7a707382009-11-19 13:35:55 +0900447 mListView.setEmptyView(mEmptyView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800448 }
449
450 @Override
451 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700452 super.onCreateOptionsMenu(menu);
453
454 final MenuInflater inflater = getMenuInflater();
455 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800456 return true;
457 }
458
459 @Override
460 public boolean onPrepareOptionsMenu(Menu menu) {
461 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700462
Evan Millar51514db2009-09-22 11:42:35 -0700463 // Only allow edit when we have at least one raw_contact id
464 final boolean hasRawContact = (mRawContactIds.size() > 0);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700465 menu.findItem(R.id.menu_edit).setEnabled(hasRawContact);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700466
Jeff Sharkeye0408762009-11-03 12:57:16 -0800467 // Only allow share when unrestricted contacts available
468 menu.findItem(R.id.menu_share).setEnabled(!mAllRestricted);
469
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800470 return true;
471 }
472
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800473 @Override
474 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
475 AdapterView.AdapterContextMenuInfo info;
476 try {
477 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
478 } catch (ClassCastException e) {
479 Log.e(TAG, "bad menuInfo", e);
480 return;
481 }
482
483 // This can be null sometimes, don't crash...
484 if (info == null) {
485 Log.e(TAG, "bad menuInfo");
486 return;
487 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700488
Evan Millar45e0ed32009-06-01 16:44:38 -0700489 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
Neel Parekha8fb68a2009-09-25 18:05:18 -0700490 menu.setHeaderTitle(R.string.contactOptionsTitle);
Evan Millar45e0ed32009-06-01 16:44:38 -0700491 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
492 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700493 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
494 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700495 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800496 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700497 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
498 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700499 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700500 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800501 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700502 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700503 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
504 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800505 }
506
507 @Override
508 public boolean onOptionsItemSelected(MenuItem item) {
509 switch (item.getItemId()) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700510 case R.id.menu_edit: {
Evan Millardf181202009-09-30 15:15:51 -0700511 Long rawContactIdToEdit = null;
512 if (mRawContactIds.size() > 0) {
513 rawContactIdToEdit = mRawContactIds.get(0);
514 } else {
515 // There is no rawContact to edit.
516 break;
Evan Millardb5d88c2009-08-28 09:31:57 -0700517 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700518 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
519 rawContactIdToEdit);
Evan Millar9cd81242009-09-26 16:02:31 -0700520 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
521 REQUEST_EDIT_CONTACT);
Evan Millar8a79cee2009-08-19 17:20:49 -0700522 break;
523 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700524 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800525 // Get confirmation
Evan Millarcdeea942009-10-07 11:47:35 -0700526 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
Megha Joshid5afd8a2009-09-22 09:38:41 -0700527 showDialog(DIALOG_CONFIRM_READONLY_DELETE);
Evan Millarcdeea942009-10-07 11:47:35 -0700528 } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) {
Megha Joshic508bd82009-10-01 17:31:49 -0700529 showDialog(DIALOG_CONFIRM_READONLY_HIDE);
Evan Millarcdeea942009-10-07 11:47:35 -0700530 } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) {
531 showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
532 } else {
533 showDialog(DIALOG_CONFIRM_DELETE);
534 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800535 return true;
536 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700537 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700538 showJoinAggregateActivity();
539 return true;
540 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700541 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700542 showOptionsActivity();
543 return true;
544 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700545 case R.id.menu_share: {
Jeff Sharkeye0408762009-11-03 12:57:16 -0800546 if (mAllRestricted) return false;
547
Jeff Sharkey93049312009-09-27 17:28:13 -0700548 // TODO: Keep around actual LOOKUP_KEY, or formalize method of extracting
549 final String lookupKey = mLookupUri.getPathSegments().get(2);
550 final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
551
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700552 final Intent intent = new Intent(Intent.ACTION_SEND);
Jeff Sharkey93049312009-09-27 17:28:13 -0700553 intent.setType(Contacts.CONTENT_VCARD_TYPE);
554 intent.putExtra(Intent.EXTRA_STREAM, shareUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700555
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700556 // Launch chooser to share contact via
557 final CharSequence chooseTitle = getText(R.string.share_via);
558 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700559
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700560 try {
561 startActivity(chooseIntent);
562 } catch (ActivityNotFoundException ex) {
563 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800564 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700565 return true;
566 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800567 }
568 return super.onOptionsItemSelected(item);
569 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700570
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800571 @Override
572 public boolean onContextItemSelected(MenuItem item) {
573 switch (item.getItemId()) {
574 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700575 if (makeItemDefault(item)) {
576 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800577 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700578 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800579 }
580 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700581
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800582 return super.onContextItemSelected(item);
583 }
584
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700585 private boolean makeItemDefault(MenuItem item) {
586 ViewEntry entry = getViewEntryForMenuItem(item);
587 if (entry == null) {
588 return false;
589 }
590
591 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700592 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700593 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700594 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
595 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700596 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700597 return true;
598 }
599
600 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700601 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700602 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700603 public void showJoinAggregateActivity() {
Evan Millar6a61a1a2009-09-29 14:00:43 -0700604 long freshId = getRefreshedContactId();
605 if (freshId > 0) {
606 String displayName = null;
607 if (mCursor.moveToFirst()) {
608 displayName = mCursor.getString(0);
609 }
610 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
611 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, freshId);
612 if (displayName != null) {
613 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_NAME, displayName);
614 }
615 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikova5cfca32009-09-29 18:13:08 -0700616 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700617 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700618
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700619 @Override
620 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700621 if (requestCode == REQUEST_JOIN_CONTACT) {
622 if (resultCode == RESULT_OK && intent != null) {
623 final long contactId = ContentUris.parseId(intent.getData());
624 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700625 }
Evan Millar9cd81242009-09-26 16:02:31 -0700626 } else if (requestCode == REQUEST_EDIT_CONTACT) {
Evan Millardf181202009-09-30 15:15:51 -0700627 if (resultCode == EditContactActivity.RESULT_CLOSE_VIEW_ACTIVITY) {
628 finish();
Jeff Hamilton5297c6a2009-10-01 02:22:33 -0700629 } else if (resultCode == Activity.RESULT_OK) {
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -0700630 mLookupUri = intent.getData();
631 if (mLookupUri == null) {
632 finish();
633 }
Evan Millardf181202009-09-30 15:15:51 -0700634 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700635 }
636 }
637
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700638 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700639 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700640
641 // The split operation may have removed the original aggregate contact, so we need
642 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700643 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700644 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700645 }
646
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700647 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700648 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700649 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700650
651 try {
652 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700653 long rawContactId = c.getLong(0);
654 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700655 }
656 } finally {
657 c.close();
658 }
659
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700660 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
661 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700662 }
663
664 /**
665 * Given a contact ID sets an aggregation exception to either join the contact with the
666 * current aggregate or split off.
667 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700668 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700669 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700670 for (long aRawContactId : mRawContactIds) {
671 if (aRawContactId != rawContactId) {
672 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
673 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
674 values.put(AggregationExceptions.TYPE, exceptionType);
675 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
676 }
677 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700678 }
679
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700680 private void showOptionsActivity() {
681 final Intent intent = new Intent(this, ContactOptionsActivity.class);
Evan Millar6a61a1a2009-09-29 14:00:43 -0700682 intent.setData(mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700683 startActivity(intent);
684 }
685
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700686 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
687 AdapterView.AdapterContextMenuInfo info;
688 try {
689 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
690 } catch (ClassCastException e) {
691 Log.e(TAG, "bad menuInfo", e);
692 return null;
693 }
694
695 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
696 }
697
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800698 @Override
699 public boolean onKeyDown(int keyCode, KeyEvent event) {
700 switch (keyCode) {
701 case KeyEvent.KEYCODE_CALL: {
702 try {
703 ITelephony phone = ITelephony.Stub.asInterface(
704 ServiceManager.checkService("phone"));
705 if (phone != null && !phone.isIdle()) {
706 // Skip out and let the key be handled at a higher level
707 break;
708 }
709 } catch (RemoteException re) {
710 // Fall through and try to call the contact
711 }
712
Evan Millar7911ff52009-07-21 15:55:18 -0700713 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800714 if (index != -1) {
715 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700716 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
717 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800718 }
Jeff Sharkey827762d2009-12-23 11:06:08 -0800719 } else if (mPrimaryPhoneUri != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800720 // There isn't anything selected, call the default number
Jeff Sharkey827762d2009-12-23 11:06:08 -0800721 final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
722 mPrimaryPhoneUri);
723 startActivity(intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800724 }
725 return true;
726 }
727
728 case KeyEvent.KEYCODE_DEL: {
Evan Millarcdeea942009-10-07 11:47:35 -0700729 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
Megha Joshid5afd8a2009-09-22 09:38:41 -0700730 showDialog(DIALOG_CONFIRM_READONLY_DELETE);
Evan Millarcdeea942009-10-07 11:47:35 -0700731 } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) {
Megha Joshic508bd82009-10-01 17:31:49 -0700732 showDialog(DIALOG_CONFIRM_READONLY_HIDE);
Evan Millarcdeea942009-10-07 11:47:35 -0700733 } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) {
734 showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
735 } else {
736 showDialog(DIALOG_CONFIRM_DELETE);
737 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800738 return true;
739 }
740 }
741
742 return super.onKeyDown(keyCode, event);
743 }
744
Evan Millar7911ff52009-07-21 15:55:18 -0700745 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800746 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
747 if (entry != null) {
748 Intent intent = entry.intent;
Evan Millardf181202009-09-30 15:15:51 -0700749 if (intent != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800750 try {
751 startActivity(intent);
752 } catch (ActivityNotFoundException e) {
753 Log.e(TAG, "No activity found for intent: " + intent);
754 signalError();
755 }
756 } else {
757 signalError();
758 }
759 } else {
760 signalError();
761 }
762 }
763
764 /**
765 * Signal an error to the user via a beep, or some other method.
766 */
767 private void signalError() {
768 //TODO: implement this when we have the sonification APIs
769 }
770
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800771 /**
772 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700773 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800774 * @param personCursor the URI for the contact being displayed
775 */
Evan Millar11d628c2009-09-02 08:55:01 -0700776 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800777 // Clear out the old entries
778 final int numSections = mSections.size();
779 for (int i = 0; i < numSections; i++) {
780 mSections.get(i).clear();
781 }
782
Evan Millar7911ff52009-07-21 15:55:18 -0700783 mRawContactIds.clear();
Jeff Sharkeye0408762009-11-03 12:57:16 -0800784
Megha Joshid5afd8a2009-09-22 09:38:41 -0700785 mReadOnlySourcesCnt = 0;
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700786 mWritableSourcesCnt = 0;
Jeff Sharkeye0408762009-11-03 12:57:16 -0800787 mAllRestricted = true;
Jeff Sharkey827762d2009-12-23 11:06:08 -0800788 mPrimaryPhoneUri = null;
Jeff Sharkeye0408762009-11-03 12:57:16 -0800789
Megha Joshid5afd8a2009-09-22 09:38:41 -0700790 mWritableRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700791
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700792 final Context context = this;
793 final Sources sources = Sources.getInstance(context);
Evan Millar11d628c2009-09-02 08:55:01 -0700794
Evan Millar66388be2009-05-28 15:41:07 -0700795 // Build up method entries
Evan Millar6a61a1a2009-09-29 14:00:43 -0700796 if (mLookupUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700797 for (Entity entity: mEntities) {
798 final ContentValues entValues = entity.getEntityValues();
799 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700800 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700801
Jeff Sharkeye0408762009-11-03 12:57:16 -0800802 // Mark when this contact has any unrestricted components
803 final boolean isRestricted = entValues.getAsInteger(RawContacts.IS_RESTRICTED) != 0;
804 if (!isRestricted) mAllRestricted = false;
805
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700806 if (!mRawContactIds.contains(rawContactId)) {
807 mRawContactIds.add(rawContactId);
808 }
Megha Joshic508bd82009-10-01 17:31:49 -0700809 ContactsSource contactsSource = sources.getInflatedSource(accountType,
810 ContactsSource.LEVEL_SUMMARY);
811 if (contactsSource != null && contactsSource.readOnly) {
812 mReadOnlySourcesCnt += 1;
813 } else {
814 mWritableSourcesCnt += 1;
Evan Millarcdeea942009-10-07 11:47:35 -0700815 mWritableRawContactIds.add(rawContactId);
816 }
Megha Joshic508bd82009-10-01 17:31:49 -0700817
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700818
Evan Millar11d628c2009-09-02 08:55:01 -0700819 for (NamedContentValues subValue : entity.getSubValues()) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700820 final ContentValues entryValues = subValue.values;
821 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
822
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700823 final long dataId = entryValues.getAsLong(Data._ID);
824 final String mimeType = entryValues.getAsString(Data.MIMETYPE);
825 if (mimeType == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -0700826
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700827 final DataKind kind = sources.getKindOrFallback(accountType, mimeType, this,
Evan Millar11d628c2009-09-02 08:55:01 -0700828 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700829 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800830
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700831 final ViewEntry entry = ViewEntry.fromValues(context, mimeType, kind,
832 rawContactId, dataId, entryValues);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800833
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700834 final boolean hasData = !TextUtils.isEmpty(entry.data);
Evan Millarff04a272009-09-26 15:23:37 -0700835 final boolean isSuperPrimary = entryValues.getAsInteger(
836 Data.IS_SUPER_PRIMARY) != 0;
Evan Millar11d628c2009-09-02 08:55:01 -0700837
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700838 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
Evan Millarff04a272009-09-26 15:23:37 -0700839 // Build phone entries
840 mNumPhoneNumbers++;
Evan Millar11d628c2009-09-02 08:55:01 -0700841
Evan Millarff04a272009-09-26 15:23:37 -0700842 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700843 Uri.fromParts(Constants.SCHEME_TEL, entry.data, null));
Evan Millarff04a272009-09-26 15:23:37 -0700844 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700845 Uri.fromParts(Constants.SCHEME_SMSTO, entry.data, null));
Evan Millar49714ee2009-09-02 16:42:47 -0700846
Jeff Sharkey827762d2009-12-23 11:06:08 -0800847 // Remember super-primary phone
848 if (isSuperPrimary) mPrimaryPhoneUri = entry.uri;
849
Evan Millarff04a272009-09-26 15:23:37 -0700850 entry.isPrimary = isSuperPrimary;
851 mPhoneEntries.add(entry);
852
853 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
854 || mShowSmsLinksForAllPhones) {
855 // Add an SMS entry
856 if (kind.iconAltRes > 0) {
857 entry.secondaryActionIcon = kind.iconAltRes;
Evan Millar49714ee2009-09-02 16:42:47 -0700858 }
Evan Millarff04a272009-09-26 15:23:37 -0700859 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700860 } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
Evan Millarff04a272009-09-26 15:23:37 -0700861 // Build email entries
862 entry.intent = new Intent(Intent.ACTION_SENDTO,
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700863 Uri.fromParts(Constants.SCHEME_MAILTO, entry.data, null));
Evan Millarff04a272009-09-26 15:23:37 -0700864 entry.isPrimary = isSuperPrimary;
865 mEmailEntries.add(entry);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700866
867 // When Email rows have status, create additional Im row
868 final DataStatus status = mStatuses.get(entry.id);
869 if (status != null) {
870 final String imMime = Im.CONTENT_ITEM_TYPE;
871 final DataKind imKind = sources.getKindOrFallback(accountType,
872 imMime, this, ContactsSource.LEVEL_MIMETYPES);
873 final ViewEntry imEntry = ViewEntry.fromValues(context,
874 imMime, imKind, rawContactId, dataId, entryValues);
875 imEntry.intent = ContactsUtils.buildImIntent(entryValues);
876 imEntry.applyStatus(status, false);
877 mImEntries.add(imEntry);
878 }
879 } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
Evan Millarff04a272009-09-26 15:23:37 -0700880 // Build postal entries
881 entry.maxLines = 4;
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700882 entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri);
Evan Millarff04a272009-09-26 15:23:37 -0700883 mPostalEntries.add(entry);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700884 } else if (Im.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
885 // Build IM entries
886 entry.intent = ContactsUtils.buildImIntent(entryValues);
Evan Millarff04a272009-09-26 15:23:37 -0700887 if (TextUtils.isEmpty(entry.label)) {
888 entry.label = getString(R.string.chat).toLowerCase();
889 }
Evan Millar11d628c2009-09-02 08:55:01 -0700890
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700891 // Apply presence and status details when available
892 final DataStatus status = mStatuses.get(entry.id);
893 if (status != null) {
894 entry.applyStatus(status, false);
Evan Millarff04a272009-09-26 15:23:37 -0700895 }
Evan Millarff04a272009-09-26 15:23:37 -0700896 mImEntries.add(entry);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700897 } else if ((Organization.CONTENT_ITEM_TYPE.equals(mimeType)
898 || Nickname.CONTENT_ITEM_TYPE.equals(mimeType)) && hasData) {
Evan Millarff04a272009-09-26 15:23:37 -0700899 // Build organization and note entries
900 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -0700901 mOrganizationEntries.add(entry);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700902 } else if (Note.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
Evan Millar11d628c2009-09-02 08:55:01 -0700903 // Build note entries
Evan Millar11d628c2009-09-02 08:55:01 -0700904 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -0700905 entry.maxLines = 10;
906 mOtherEntries.add(entry);
Evan Millar951fa9c2009-10-26 14:57:43 -0700907 } else if (Website.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
908 // Build note entries
909 entry.uri = null;
910 entry.maxLines = 10;
911 try {
912 WebAddress webAddress = new WebAddress(entry.data);
Makoto Onuki7a707382009-11-19 13:35:55 +0900913 entry.intent = new Intent(Intent.ACTION_VIEW,
Evan Millar951fa9c2009-10-26 14:57:43 -0700914 Uri.parse(webAddress.toString()));
915 } catch (ParseException e) {
916 Log.e(TAG, "Couldn't parse website: " + entry.data);
917 }
918 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700919 } else {
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700920 // Handle showing custom rows
Evan Millarff04a272009-09-26 15:23:37 -0700921 entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700922
923 // Use social summary when requested by external source
924 final DataStatus status = mStatuses.get(entry.id);
925 final boolean hasSocial = kind.actionBodySocial && status != null;
926 if (hasSocial) {
927 entry.applyStatus(status, true);
928 }
929
930 if (hasSocial || hasData) {
931 mOtherEntries.add(entry);
932 }
Evan Millar11d628c2009-09-02 08:55:01 -0700933 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700934 }
Evan Millar66388be2009-05-28 15:41:07 -0700935 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800936 }
937 }
938
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700939 static String buildActionString(DataKind kind, ContentValues values, boolean lowerCase,
940 Context context) {
Evan Millar11d628c2009-09-02 08:55:01 -0700941 if (kind.actionHeader == null) {
942 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700943 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700944 CharSequence actionHeader = kind.actionHeader.inflateUsing(context, values);
Evan Millar11d628c2009-09-02 08:55:01 -0700945 if (actionHeader == null) {
946 return null;
947 }
948 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
949 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700950
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700951 static String buildDataString(DataKind kind, ContentValues values, Context context) {
Evan Millar11d628c2009-09-02 08:55:01 -0700952 if (kind.actionBody == null) {
953 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800954 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700955 CharSequence actionBody = kind.actionBody.inflateUsing(context, values);
Evan Millar11d628c2009-09-02 08:55:01 -0700956 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800957 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700958
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800959 /**
960 * A basic structure with the data for a contact entry in the list.
961 */
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700962 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
963 public Context context = null;
Jeff Sharkeyab066932009-09-21 09:55:30 -0700964 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800965 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -0700966 public boolean isPrimary = false;
Evan Millar15e514d2009-08-04 10:14:57 -0700967 public int secondaryActionIcon = -1;
968 public Intent intent;
969 public Intent secondaryIntent = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800970 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700971 public ArrayList<Long> ids = new ArrayList<Long>();
972 public int collapseCount = 0;
973
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700974 public int presence = -1;
975 public int presenceIcon = -1;
976
977 public CharSequence footerLine = null;
978
979 private ViewEntry() {
980 }
981
982 /**
983 * Build new {@link ViewEntry} and populate from the given values.
984 */
985 public static ViewEntry fromValues(Context context, String mimeType, DataKind kind,
986 long rawContactId, long dataId, ContentValues values) {
987 final ViewEntry entry = new ViewEntry();
988 entry.context = context;
989 entry.contactId = rawContactId;
990 entry.id = dataId;
991 entry.uri = ContentUris.withAppendedId(Data.CONTENT_URI, entry.id);
992 entry.mimetype = mimeType;
993 entry.label = buildActionString(kind, values, false, context);
994 entry.data = buildDataString(kind, values, context);
995
996 if (kind.typeColumn != null && values.containsKey(kind.typeColumn)) {
997 entry.type = values.getAsInteger(kind.typeColumn);
998 }
999 if (kind.iconRes > 0) {
1000 entry.resPackageName = kind.resPackageName;
1001 entry.actionIcon = kind.iconRes;
1002 }
1003
1004 return entry;
1005 }
1006
1007 /**
1008 * Apply given {@link DataStatus} values over this {@link ViewEntry}
1009 *
1010 * @param fillData When true, the given status replaces {@link #data}
1011 * and {@link #footerLine}. Otherwise only {@link #presence}
1012 * is updated.
1013 */
1014 public ViewEntry applyStatus(DataStatus status, boolean fillData) {
1015 presence = status.getPresence();
1016 presenceIcon = (presence == -1) ? -1 :
1017 StatusUpdates.getPresenceIconResourceId(this.presence);
1018
1019 if (fillData && status.isValid()) {
1020 this.data = status.getStatus().toString();
1021 this.footerLine = status.getTimestampLabel(context);
1022 }
1023
1024 return this;
1025 }
1026
Evan Millar54a5c9f2009-06-23 17:41:09 -07001027 public boolean collapseWith(ViewEntry entry) {
1028 // assert equal collapse keys
Evan Millaradb0f8c2009-09-28 17:20:50 -07001029 if (!shouldCollapseWith(entry)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001030 return false;
1031 }
1032
1033 // Choose the label associated with the highest type precedence.
1034 if (TypePrecedence.getTypePrecedence(mimetype, type)
1035 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1036 type = entry.type;
1037 label = entry.label;
1038 }
1039
1040 // Choose the max of the maxLines and maxLabelLines values.
1041 maxLines = Math.max(maxLines, entry.maxLines);
1042 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1043
1044 // Choose the presence with the highest precedence.
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001045 if (StatusUpdates.getPresencePrecedence(presence)
1046 < StatusUpdates.getPresencePrecedence(entry.presence)) {
1047 presence = entry.presence;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001048 }
1049
1050 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001051 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001052
1053 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1054
1055 // Keep track of all the ids that have been collapsed with this one.
1056 ids.add(entry.id);
1057 collapseCount++;
1058 return true;
1059 }
1060
Evan Millaradb0f8c2009-09-28 17:20:50 -07001061 public boolean shouldCollapseWith(ViewEntry entry) {
1062 if (entry == null) {
1063 return false;
1064 }
1065
Makoto Onukic710b0e2009-10-13 15:43:24 -07001066 if (!ContactsUtils.areDataEqual(context, mimetype, data, entry.mimetype, entry.data)) {
1067 return false;
Evan Millaradb0f8c2009-09-28 17:20:50 -07001068 }
1069
Makoto Onukic710b0e2009-10-13 15:43:24 -07001070 if (!TextUtils.equals(mimetype, entry.mimetype)
1071 || !ContactsUtils.areIntentActionEqual(intent, entry.intent)
1072 || !ContactsUtils.areIntentActionEqual(secondaryIntent, entry.secondaryIntent)
Evan Millaradb0f8c2009-09-28 17:20:50 -07001073 || actionIcon != entry.actionIcon) {
1074 return false;
1075 }
1076
1077 return true;
1078 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001079 }
1080
Evan Millar15e514d2009-08-04 10:14:57 -07001081 /** Cache of the children views of a row */
1082 static class ViewCache {
1083 public TextView label;
1084 public TextView data;
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001085 public TextView footer;
Evan Millar15e514d2009-08-04 10:14:57 -07001086 public ImageView actionIcon;
1087 public ImageView presenceIcon;
1088 public ImageView primaryIcon;
1089 public ImageView secondaryActionButton;
1090 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001091
Evan Millar15e514d2009-08-04 10:14:57 -07001092 // Need to keep track of this too
1093 ViewEntry entry;
1094 }
1095
1096 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1097 implements View.OnClickListener {
1098
Evan Millar5c22c3b2009-05-29 11:37:54 -07001099
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001100 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1101 super(context, sections, SHOW_SEPARATORS);
1102 }
1103
Evan Millar15e514d2009-08-04 10:14:57 -07001104 public void onClick(View v) {
1105 Intent intent = (Intent) v.getTag();
1106 startActivity(intent);
1107 }
1108
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001109 @Override
1110 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001111 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001112 View v;
1113
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001114 ViewCache views;
1115
1116 // Check to see if we can reuse convertView
1117 if (convertView != null) {
1118 v = convertView;
1119 views = (ViewCache) v.getTag();
1120 } else {
1121 // Create a new view if needed
1122 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1123
1124 // Cache the children
1125 views = new ViewCache();
1126 views.label = (TextView) v.findViewById(android.R.id.text1);
1127 views.data = (TextView) v.findViewById(android.R.id.text2);
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001128 views.footer = (TextView) v.findViewById(R.id.footer);
Evan Millar15e514d2009-08-04 10:14:57 -07001129 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1130 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1131 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1132 views.secondaryActionButton = (ImageView) v.findViewById(
1133 R.id.secondary_action_button);
1134 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001135 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001136 v.setTag(views);
1137 }
1138
1139 // Update the entry in the view cache
1140 views.entry = entry;
1141
1142 // Bind the data to the view
1143 bindView(v, entry);
1144 return v;
1145 }
1146
1147 @Override
1148 protected View newView(int position, ViewGroup parent) {
1149 // getView() handles this
1150 throw new UnsupportedOperationException();
1151 }
1152
1153 @Override
1154 protected void bindView(View view, ViewEntry entry) {
1155 final Resources resources = mContext.getResources();
1156 ViewCache views = (ViewCache) view.getTag();
1157
1158 // Set the label
1159 TextView label = views.label;
1160 setMaxLines(label, entry.maxLabelLines);
1161 label.setText(entry.label);
1162
1163 // Set the data
1164 TextView data = views.data;
1165 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001166 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001167 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001168 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1169 } else {
1170 data.setText(entry.data);
1171 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001172 setMaxLines(data, entry.maxLines);
1173 }
1174
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001175 // Set the footer
1176 if (!TextUtils.isEmpty(entry.footerLine)) {
1177 views.footer.setText(entry.footerLine);
1178 views.footer.setVisibility(View.VISIBLE);
1179 } else {
1180 views.footer.setVisibility(View.GONE);
1181 }
1182
Evan Millar15e514d2009-08-04 10:14:57 -07001183 // Set the primary icon
1184 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1185
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001186 // Set the action icon
1187 ImageView action = views.actionIcon;
1188 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001189 Drawable actionIcon;
1190 if (entry.resPackageName != null) {
1191 // Load external resources through PackageManager
1192 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1193 entry.actionIcon, null);
1194 } else {
1195 actionIcon = resources.getDrawable(entry.actionIcon);
1196 }
1197 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001198 action.setVisibility(View.VISIBLE);
1199 } else {
1200 // Things should still line up as if there was an icon, so make it invisible
1201 action.setVisibility(View.INVISIBLE);
1202 }
1203
1204 // Set the presence icon
1205 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001206 if (entry.presenceIcon != -1) {
1207 presenceIcon = resources.getDrawable(entry.presenceIcon);
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001208 } else if (entry.presence != -1) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001209 presenceIcon = resources.getDrawable(
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001210 StatusUpdates.getPresenceIconResourceId(entry.presence));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001211 }
Evan Millar15e514d2009-08-04 10:14:57 -07001212 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001213 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001214 presenceIconView.setImageDrawable(presenceIcon);
1215 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001216 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001217 presenceIconView.setVisibility(View.GONE);
1218 }
1219
1220 // Set the secondary action button
1221 ImageView secondaryActionView = views.secondaryActionButton;
1222 Drawable secondaryActionIcon = null;
1223 if (entry.secondaryActionIcon != -1) {
1224 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1225 }
1226 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1227 secondaryActionView.setImageDrawable(secondaryActionIcon);
1228 secondaryActionView.setTag(entry.secondaryIntent);
1229 secondaryActionView.setVisibility(View.VISIBLE);
1230 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1231 } else {
1232 secondaryActionView.setVisibility(View.GONE);
1233 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001234 }
1235 }
1236
1237 private void setMaxLines(TextView textView, int maxLines) {
1238 if (maxLines == 1) {
1239 textView.setSingleLine(true);
1240 textView.setEllipsize(TextUtils.TruncateAt.END);
1241 } else {
1242 textView.setSingleLine(false);
1243 textView.setMaxLines(maxLines);
1244 textView.setEllipsize(null);
1245 }
1246 }
1247 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001248
1249 private interface StatusQuery {
1250 final String[] PROJECTION = new String[] {
1251 Data._ID,
1252 Data.STATUS,
1253 Data.STATUS_RES_PACKAGE,
1254 Data.STATUS_ICON,
1255 Data.STATUS_LABEL,
1256 Data.STATUS_TIMESTAMP,
1257 Data.PRESENCE,
1258 };
1259
1260 final int _ID = 0;
1261 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001262}