blob: eb5aec546683a5c73821f96509f04574845a3c09 [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;
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -070062import android.provider.ContactsContract.StatusUpdates;
Jeff Sharkey624ddc32009-10-01 21:32:19 -070063import android.provider.ContactsContract.CommonDataKinds.Email;
64import android.provider.ContactsContract.CommonDataKinds.Im;
65import android.provider.ContactsContract.CommonDataKinds.Nickname;
66import android.provider.ContactsContract.CommonDataKinds.Note;
67import android.provider.ContactsContract.CommonDataKinds.Organization;
Evan Millar54a5c9f2009-06-23 17:41:09 -070068import android.provider.ContactsContract.CommonDataKinds.Phone;
Jeff Sharkey624ddc32009-10-01 21:32:19 -070069import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
Evan Millar951fa9c2009-10-26 14:57:43 -070070import android.provider.ContactsContract.CommonDataKinds.Website;
Evan Millar54a5c9f2009-06-23 17:41:09 -070071import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080072import android.text.TextUtils;
73import android.util.Log;
74import android.view.ContextMenu;
75import android.view.KeyEvent;
Evan Millar11d628c2009-09-02 08:55:01 -070076import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080077import android.view.Menu;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070078import android.view.MenuInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080079import android.view.MenuItem;
80import android.view.View;
81import android.view.ViewGroup;
Evan Millar11d628c2009-09-02 08:55:01 -070082import android.view.Window;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080083import android.view.ContextMenu.ContextMenuInfo;
84import android.widget.AdapterView;
Evan Millar7911ff52009-07-21 15:55:18 -070085import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080086import android.widget.ImageView;
87import android.widget.ListView;
Evan Millarcdeea942009-10-07 11:47:35 -070088import android.widget.ScrollView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080089import android.widget.TextView;
90import android.widget.Toast;
91
92import java.util.ArrayList;
Jeff Sharkey624ddc32009-10-01 21:32:19 -070093import java.util.HashMap;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080094
95/**
96 * Displays the details of a specific contact.
97 */
Evan Millar11d628c2009-09-02 08:55:01 -070098public class ViewContactActivity extends Activity
Evan Millar7911ff52009-07-21 15:55:18 -070099 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
Evan Millardf181202009-09-30 15:15:51 -0700100 AdapterView.OnItemClickListener, NotifyingAsyncQueryHandler.AsyncQueryListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800101 private static final String TAG = "ViewContact";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800102
103 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800104
105 private static final int DIALOG_CONFIRM_DELETE = 1;
Megha Joshid5afd8a2009-09-22 09:38:41 -0700106 private static final int DIALOG_CONFIRM_READONLY_DELETE = 2;
107 private static final int DIALOG_CONFIRM_MULTIPLE_DELETE = 3;
Megha Joshic508bd82009-10-01 17:31:49 -0700108 private static final int DIALOG_CONFIRM_READONLY_HIDE = 4;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800109
Evan Millar8a79cee2009-08-19 17:20:49 -0700110 private static final int REQUEST_JOIN_CONTACT = 1;
Evan Millar9cd81242009-09-26 16:02:31 -0700111 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700112
Evan Millar8a79cee2009-08-19 17:20:49 -0700113 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800114
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700115 protected Uri mLookupUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800116 private ContentResolver mResolver;
117 private ViewAdapter mAdapter;
118 private int mNumPhoneNumbers = 0;
119
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700120 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700121 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700122 */
Evan Millar7911ff52009-07-21 15:55:18 -0700123 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700124
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800125 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
126 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
127 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
128 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
129 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
130 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700131 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800132 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
133 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
134
135 private Cursor mCursor;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700136
Evan Millar11d628c2009-09-02 08:55:01 -0700137 protected ContactHeaderWidget mContactHeaderWidget;
138 private NotifyingAsyncQueryHandler mHandler;
139
140 protected LayoutInflater mInflater;
141
Megha Joshid5afd8a2009-09-22 09:38:41 -0700142 protected int mReadOnlySourcesCnt;
143 protected int mWritableSourcesCnt;
Jeff Sharkeye0408762009-11-03 12:57:16 -0800144 protected boolean mAllRestricted;
145
Megha Joshid5afd8a2009-09-22 09:38:41 -0700146 protected ArrayList<Long> mWritableRawContactIds = new ArrayList<Long>();
Evan Millar11d628c2009-09-02 08:55:01 -0700147
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700148 private static final int TOKEN_ENTITIES = 0;
149 private static final int TOKEN_STATUSES = 1;
150
151 private boolean mHasEntities = false;
152 private boolean mHasStatuses = false;
153
154 private ArrayList<Entity> mEntities = Lists.newArrayList();
155 private HashMap<Long, DataStatus> mStatuses = Maps.newHashMap();
Evan Millar11d628c2009-09-02 08:55:01 -0700156
Makoto Onuki7a707382009-11-19 13:35:55 +0900157 /**
158 * The view shown if the detail list is empty.
159 * We set this to the list view when first bind the adapter, so that it won't be shown while
160 * we're loading data.
161 */
162 private View mEmptyView;
163
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800164 private ContentObserver mObserver = new ContentObserver(new Handler()) {
165 @Override
166 public boolean deliverSelfNotifications() {
167 return true;
168 }
169
170 @Override
171 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700172 if (mCursor != null && !mCursor.isClosed()) {
173 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800174 }
175 }
176 };
177
178 public void onClick(DialogInterface dialog, int which) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700179 closeCursor();
Megha Joshic508bd82009-10-01 17:31:49 -0700180 getContentResolver().delete(mLookupUri, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800181 finish();
182 }
183
Evan Millar7911ff52009-07-21 15:55:18 -0700184 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800185 private boolean mShowSmsLinksForAllPhones;
186
187 @Override
188 protected void onCreate(Bundle icicle) {
189 super.onCreate(icicle);
190
Evan Millar11d628c2009-09-02 08:55:01 -0700191 final Intent intent = getIntent();
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700192 Uri data = intent.getData();
193 String authority = data.getAuthority();
194 if (ContactsContract.AUTHORITY.equals(authority)) {
195 mLookupUri = data;
196 } else if (android.provider.Contacts.AUTHORITY.equals(authority)) {
197 final long rawContactId = ContentUris.parseId(data);
198 mLookupUri = RawContacts.getContactLookupUri(getContentResolver(),
199 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700200
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700201 }
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700202 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700203
204 requestWindowFeature(Window.FEATURE_NO_TITLE);
205 setContentView(R.layout.contact_card_layout);
206
207 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
208 mContactHeaderWidget.showStar(true);
Evan Millar11d628c2009-09-02 08:55:01 -0700209 mContactHeaderWidget.setExcludeMimes(new String[] {
210 Contacts.CONTENT_ITEM_TYPE
211 });
212
Evan Millar11d628c2009-09-02 08:55:01 -0700213 mHandler = new NotifyingAsyncQueryHandler(this, this);
214
Evan Millarcdeea942009-10-07 11:47:35 -0700215 mListView = (ListView) findViewById(R.id.contact_data);
Evan Millar7911ff52009-07-21 15:55:18 -0700216 mListView.setOnCreateContextMenuListener(this);
217 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
218 mListView.setOnItemClickListener(this);
Makoto Onuki7a707382009-11-19 13:35:55 +0900219 // Don't set it to mListView yet. We do so later when we bind the adapter.
220 mEmptyView = findViewById(android.R.id.empty);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800221
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800222 mResolver = getContentResolver();
223
224 // Build the list of sections. The order they're added to mSections dictates the
225 // order they are displayed in the list.
226 mSections.add(mPhoneEntries);
227 mSections.add(mSmsEntries);
228 mSections.add(mEmailEntries);
229 mSections.add(mImEntries);
230 mSections.add(mPostalEntries);
231 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700232 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800233 mSections.add(mOtherEntries);
234
235 //TODO Read this value from a preference
236 mShowSmsLinksForAllPhones = true;
Evan Millar11d628c2009-09-02 08:55:01 -0700237 }
238
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800239 @Override
240 protected void onResume() {
241 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700242 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800243 }
244
245 @Override
246 protected void onPause() {
247 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700248 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800249 }
250
251 @Override
252 protected void onDestroy() {
253 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700254 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800255 }
256
257 @Override
258 protected Dialog onCreateDialog(int id) {
259 switch (id) {
260 case DIALOG_CONFIRM_DELETE:
261 return new AlertDialog.Builder(this)
262 .setTitle(R.string.deleteConfirmation_title)
263 .setIcon(android.R.drawable.ic_dialog_alert)
264 .setMessage(R.string.deleteConfirmation)
265 .setNegativeButton(android.R.string.cancel, null)
266 .setPositiveButton(android.R.string.ok, this)
267 .setCancelable(false)
268 .create();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700269 case DIALOG_CONFIRM_READONLY_DELETE:
270 return new AlertDialog.Builder(this)
271 .setTitle(R.string.deleteConfirmation_title)
272 .setIcon(android.R.drawable.ic_dialog_alert)
273 .setMessage(R.string.readOnlyContactDeleteConfirmation)
274 .setNegativeButton(android.R.string.cancel, null)
275 .setPositiveButton(android.R.string.ok, this)
276 .setCancelable(false)
277 .create();
278 case DIALOG_CONFIRM_MULTIPLE_DELETE:
279 return new AlertDialog.Builder(this)
280 .setTitle(R.string.deleteConfirmation_title)
281 .setIcon(android.R.drawable.ic_dialog_alert)
282 .setMessage(R.string.multipleContactDeleteConfirmation)
283 .setNegativeButton(android.R.string.cancel, null)
284 .setPositiveButton(android.R.string.ok, this)
285 .setCancelable(false)
286 .create();
Evan Millarcdeea942009-10-07 11:47:35 -0700287 case DIALOG_CONFIRM_READONLY_HIDE: {
Megha Joshic508bd82009-10-01 17:31:49 -0700288 return new AlertDialog.Builder(this)
289 .setTitle(R.string.deleteConfirmation_title)
290 .setIcon(android.R.drawable.ic_dialog_alert)
291 .setMessage(R.string.readOnlyContactWarning)
292 .setPositiveButton(android.R.string.ok, this)
Evan Millarcdeea942009-10-07 11:47:35 -0700293 .create();
Megha Joshic508bd82009-10-01 17:31:49 -0700294 }
295
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800296 }
297 return null;
298 }
299
Evan Millar11d628c2009-09-02 08:55:01 -0700300 // QUERY CODE //
301 /** {@inheritDoc} */
302 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700303 try {
304 // Read incoming entities and consider binding
305 readEntities(iterator);
306 considerBindData();
Evan Millar11d628c2009-09-02 08:55:01 -0700307 } finally {
308 if (iterator != null) {
309 iterator.close();
310 }
311 }
312 }
313
314 /** {@inheritDoc} */
315 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700316 try {
317 // Read available social rows and consider binding
318 readStatuses(cursor);
319 considerBindData();
320 } finally {
321 if (cursor != null) {
322 cursor.close();
323 }
324 }
Evan Millar11d628c2009-09-02 08:55:01 -0700325 }
326
Evan Millar6a61a1a2009-09-29 14:00:43 -0700327 private long getRefreshedContactId() {
328 Uri freshContactUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
329 if (freshContactUri != null) {
330 return ContentUris.parseId(freshContactUri);
331 }
332 return -1;
333 }
334
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700335 /**
336 * Read from the given {@link EntityIterator} to build internal set of
337 * {@link #mEntities} for data display.
338 */
339 private synchronized void readEntities(EntityIterator iterator) {
340 mEntities.clear();
Evan Millar11d628c2009-09-02 08:55:01 -0700341 try {
342 while (iterator.hasNext()) {
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700343 mEntities.add(iterator.next());
Evan Millar11d628c2009-09-02 08:55:01 -0700344 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700345 mHasEntities = true;
Evan Millar11d628c2009-09-02 08:55:01 -0700346 } catch (RemoteException e) {
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700347 Log.w(TAG, "Problem reading contact data: " + e.toString());
Evan Millar11d628c2009-09-02 08:55:01 -0700348 }
Evan Millar11d628c2009-09-02 08:55:01 -0700349 }
350
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700351 /**
352 * Read from the given {@link Cursor} and build a set of {@link DataStatus}
353 * objects to match any valid statuses found.
354 */
355 private synchronized void readStatuses(Cursor cursor) {
356 mStatuses.clear();
357
358 // Walk found statuses, creating internal row for each
359 while (cursor.moveToNext()) {
360 final DataStatus status = new DataStatus(cursor);
361 final long dataId = cursor.getLong(StatusQuery._ID);
362 mStatuses.put(dataId, status);
363 }
364
365 mHasStatuses = true;
366 }
367
368 private synchronized void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700369 closeCursor();
370
Evan Millar6a61a1a2009-09-29 14:00:43 -0700371 Uri uri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700372 if (mLookupUri != null) {
373 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
374 if (mLookupUri != null) {
Evan Millar6a61a1a2009-09-29 14:00:43 -0700375 uri = Contacts.lookupContact(getContentResolver(), mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700376 }
377 }
378
Evan Millar6a61a1a2009-09-29 14:00:43 -0700379 if (uri == null) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700380
381 // TODO either figure out a way to prevent a flash of black background or
382 // use some other UI than a toast
383 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
384 Log.e(TAG, "invalid contact uri: " + mLookupUri);
385 finish();
386 return;
387 }
388
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700389 final Uri dataUri = Uri.withAppendedPath(uri, Contacts.Data.CONTENT_DIRECTORY);
390
391 // Keep stub cursor open on side to watch for change events
392 mCursor = mResolver.query(dataUri,
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700393 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
394 mCursor.registerContentObserver(mObserver);
395
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700396 final long contactId = ContentUris.parseId(uri);
397
398 // Clear flags and start queries to data and status
399 mHasEntities = false;
400 mHasStatuses = false;
401
402 mHandler.startQueryEntities(TOKEN_ENTITIES, null, RawContacts.CONTENT_URI,
403 RawContacts.CONTACT_ID + "=" + contactId, null, null);
404 mHandler.startQuery(TOKEN_STATUSES, null, dataUri, StatusQuery.PROJECTION,
405 StatusUpdates.PRESENCE + " IS NOT NULL OR " + StatusUpdates.STATUS
406 + " IS NOT NULL", null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700407
Evan Millar9b1a1242009-09-16 10:26:10 -0700408 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700409 }
410
411 private void closeCursor() {
412 if (mCursor != null) {
413 mCursor.unregisterContentObserver(mObserver);
414 mCursor.close();
415 mCursor = null;
416 }
Evan Millar11d628c2009-09-02 08:55:01 -0700417 }
418
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700419 /**
420 * Consider binding views after any of several background queries has
421 * completed. We check internal flags and only bind when all data has
422 * arrived.
423 */
424 private void considerBindData() {
425 if (mHasEntities && mHasStatuses) {
426 bindData();
427 }
428 }
429
Evan Millar11d628c2009-09-02 08:55:01 -0700430 private void bindData() {
431
432 // Build up the contact entries
433 buildEntries();
434
435 // Collapse similar data items in select sections.
436 Collapser.collapseList(mPhoneEntries);
437 Collapser.collapseList(mSmsEntries);
438 Collapser.collapseList(mEmailEntries);
439 Collapser.collapseList(mPostalEntries);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700440 Collapser.collapseList(mImEntries);
Evan Millar11d628c2009-09-02 08:55:01 -0700441
442 if (mAdapter == null) {
443 mAdapter = new ViewAdapter(this, mSections);
444 mListView.setAdapter(mAdapter);
445 } else {
446 mAdapter.setSections(mSections, SHOW_SEPARATORS);
447 }
Makoto Onuki7a707382009-11-19 13:35:55 +0900448 mListView.setEmptyView(mEmptyView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800449 }
450
451 @Override
452 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700453 super.onCreateOptionsMenu(menu);
454
455 final MenuInflater inflater = getMenuInflater();
456 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800457 return true;
458 }
459
460 @Override
461 public boolean onPrepareOptionsMenu(Menu menu) {
462 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700463
Evan Millar51514db2009-09-22 11:42:35 -0700464 // Only allow edit when we have at least one raw_contact id
465 final boolean hasRawContact = (mRawContactIds.size() > 0);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700466 menu.findItem(R.id.menu_edit).setEnabled(hasRawContact);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700467
Jeff Sharkeye0408762009-11-03 12:57:16 -0800468 // Only allow share when unrestricted contacts available
469 menu.findItem(R.id.menu_share).setEnabled(!mAllRestricted);
470
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800471 return true;
472 }
473
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800474 @Override
475 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
476 AdapterView.AdapterContextMenuInfo info;
477 try {
478 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
479 } catch (ClassCastException e) {
480 Log.e(TAG, "bad menuInfo", e);
481 return;
482 }
483
484 // This can be null sometimes, don't crash...
485 if (info == null) {
486 Log.e(TAG, "bad menuInfo");
487 return;
488 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700489
Evan Millar45e0ed32009-06-01 16:44:38 -0700490 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
Neel Parekha8fb68a2009-09-25 18:05:18 -0700491 menu.setHeaderTitle(R.string.contactOptionsTitle);
Evan Millar45e0ed32009-06-01 16:44:38 -0700492 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
493 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700494 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
495 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700496 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800497 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700498 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
499 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700500 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700501 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800502 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700503 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700504 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
505 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800506 }
507
508 @Override
509 public boolean onOptionsItemSelected(MenuItem item) {
510 switch (item.getItemId()) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700511 case R.id.menu_edit: {
Evan Millardf181202009-09-30 15:15:51 -0700512 Long rawContactIdToEdit = null;
513 if (mRawContactIds.size() > 0) {
514 rawContactIdToEdit = mRawContactIds.get(0);
515 } else {
516 // There is no rawContact to edit.
517 break;
Evan Millardb5d88c2009-08-28 09:31:57 -0700518 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700519 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
520 rawContactIdToEdit);
Evan Millar9cd81242009-09-26 16:02:31 -0700521 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
522 REQUEST_EDIT_CONTACT);
Evan Millar8a79cee2009-08-19 17:20:49 -0700523 break;
524 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700525 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800526 // Get confirmation
Evan Millarcdeea942009-10-07 11:47:35 -0700527 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
Megha Joshid5afd8a2009-09-22 09:38:41 -0700528 showDialog(DIALOG_CONFIRM_READONLY_DELETE);
Evan Millarcdeea942009-10-07 11:47:35 -0700529 } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) {
Megha Joshic508bd82009-10-01 17:31:49 -0700530 showDialog(DIALOG_CONFIRM_READONLY_HIDE);
Evan Millarcdeea942009-10-07 11:47:35 -0700531 } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) {
532 showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
533 } else {
534 showDialog(DIALOG_CONFIRM_DELETE);
535 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800536 return true;
537 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700538 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700539 showJoinAggregateActivity();
540 return true;
541 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700542 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700543 showOptionsActivity();
544 return true;
545 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700546 case R.id.menu_share: {
Jeff Sharkeye0408762009-11-03 12:57:16 -0800547 if (mAllRestricted) return false;
548
Jeff Sharkey93049312009-09-27 17:28:13 -0700549 // TODO: Keep around actual LOOKUP_KEY, or formalize method of extracting
550 final String lookupKey = mLookupUri.getPathSegments().get(2);
551 final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
552
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700553 final Intent intent = new Intent(Intent.ACTION_SEND);
Jeff Sharkey93049312009-09-27 17:28:13 -0700554 intent.setType(Contacts.CONTENT_VCARD_TYPE);
555 intent.putExtra(Intent.EXTRA_STREAM, shareUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700556
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700557 // Launch chooser to share contact via
558 final CharSequence chooseTitle = getText(R.string.share_via);
559 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700560
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700561 try {
562 startActivity(chooseIntent);
563 } catch (ActivityNotFoundException ex) {
564 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800565 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700566 return true;
567 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800568 }
569 return super.onOptionsItemSelected(item);
570 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700571
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800572 @Override
573 public boolean onContextItemSelected(MenuItem item) {
574 switch (item.getItemId()) {
575 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700576 if (makeItemDefault(item)) {
577 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800578 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700579 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800580 }
581 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700582
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800583 return super.onContextItemSelected(item);
584 }
585
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700586 private boolean makeItemDefault(MenuItem item) {
587 ViewEntry entry = getViewEntryForMenuItem(item);
588 if (entry == null) {
589 return false;
590 }
591
592 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700593 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700594 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700595 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
596 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700597 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700598 return true;
599 }
600
601 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700602 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700603 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700604 public void showJoinAggregateActivity() {
Evan Millar6a61a1a2009-09-29 14:00:43 -0700605 long freshId = getRefreshedContactId();
606 if (freshId > 0) {
607 String displayName = null;
608 if (mCursor.moveToFirst()) {
609 displayName = mCursor.getString(0);
610 }
611 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
612 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, freshId);
613 if (displayName != null) {
614 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_NAME, displayName);
615 }
616 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikova5cfca32009-09-29 18:13:08 -0700617 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700618 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700619
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700620 @Override
621 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700622 if (requestCode == REQUEST_JOIN_CONTACT) {
623 if (resultCode == RESULT_OK && intent != null) {
624 final long contactId = ContentUris.parseId(intent.getData());
625 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700626 }
Evan Millar9cd81242009-09-26 16:02:31 -0700627 } else if (requestCode == REQUEST_EDIT_CONTACT) {
Evan Millardf181202009-09-30 15:15:51 -0700628 if (resultCode == EditContactActivity.RESULT_CLOSE_VIEW_ACTIVITY) {
629 finish();
Jeff Hamilton5297c6a2009-10-01 02:22:33 -0700630 } else if (resultCode == Activity.RESULT_OK) {
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -0700631 mLookupUri = intent.getData();
632 if (mLookupUri == null) {
633 finish();
634 }
Evan Millardf181202009-09-30 15:15:51 -0700635 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700636 }
637 }
638
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700639 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700640 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700641
642 // The split operation may have removed the original aggregate contact, so we need
643 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700644 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700645 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700646 }
647
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700648 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700649 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700650 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700651
652 try {
653 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700654 long rawContactId = c.getLong(0);
655 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700656 }
657 } finally {
658 c.close();
659 }
660
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700661 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
662 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700663 }
664
665 /**
666 * Given a contact ID sets an aggregation exception to either join the contact with the
667 * current aggregate or split off.
668 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700669 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700670 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700671 for (long aRawContactId : mRawContactIds) {
672 if (aRawContactId != rawContactId) {
673 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
674 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
675 values.put(AggregationExceptions.TYPE, exceptionType);
676 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
677 }
678 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700679 }
680
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700681 private void showOptionsActivity() {
682 final Intent intent = new Intent(this, ContactOptionsActivity.class);
Evan Millar6a61a1a2009-09-29 14:00:43 -0700683 intent.setData(mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700684 startActivity(intent);
685 }
686
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700687 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
688 AdapterView.AdapterContextMenuInfo info;
689 try {
690 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
691 } catch (ClassCastException e) {
692 Log.e(TAG, "bad menuInfo", e);
693 return null;
694 }
695
696 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
697 }
698
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800699 @Override
700 public boolean onKeyDown(int keyCode, KeyEvent event) {
701 switch (keyCode) {
702 case KeyEvent.KEYCODE_CALL: {
703 try {
704 ITelephony phone = ITelephony.Stub.asInterface(
705 ServiceManager.checkService("phone"));
706 if (phone != null && !phone.isIdle()) {
707 // Skip out and let the key be handled at a higher level
708 break;
709 }
710 } catch (RemoteException re) {
711 // Fall through and try to call the contact
712 }
713
Evan Millar7911ff52009-07-21 15:55:18 -0700714 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800715 if (index != -1) {
716 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700717 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
718 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800719 }
720 } else if (mNumPhoneNumbers != 0) {
721 // There isn't anything selected, call the default number
Evan Millar6a61a1a2009-09-29 14:00:43 -0700722 long freshContactId = getRefreshedContactId();
723 if (freshContactId > 0) {
724 Uri hardContacUri = ContentUris.withAppendedId(
725 Contacts.CONTENT_URI, freshContactId);
726 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, hardContacUri);
727 startActivity(intent);
728 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800729 }
730 return true;
731 }
732
733 case KeyEvent.KEYCODE_DEL: {
Evan Millarcdeea942009-10-07 11:47:35 -0700734 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
Megha Joshid5afd8a2009-09-22 09:38:41 -0700735 showDialog(DIALOG_CONFIRM_READONLY_DELETE);
Evan Millarcdeea942009-10-07 11:47:35 -0700736 } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) {
Megha Joshic508bd82009-10-01 17:31:49 -0700737 showDialog(DIALOG_CONFIRM_READONLY_HIDE);
Evan Millarcdeea942009-10-07 11:47:35 -0700738 } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) {
739 showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
740 } else {
741 showDialog(DIALOG_CONFIRM_DELETE);
742 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800743 return true;
744 }
745 }
746
747 return super.onKeyDown(keyCode, event);
748 }
749
Evan Millar7911ff52009-07-21 15:55:18 -0700750 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800751 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
752 if (entry != null) {
753 Intent intent = entry.intent;
Evan Millardf181202009-09-30 15:15:51 -0700754 if (intent != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800755 try {
756 startActivity(intent);
757 } catch (ActivityNotFoundException e) {
758 Log.e(TAG, "No activity found for intent: " + intent);
759 signalError();
760 }
761 } else {
762 signalError();
763 }
764 } else {
765 signalError();
766 }
767 }
768
769 /**
770 * Signal an error to the user via a beep, or some other method.
771 */
772 private void signalError() {
773 //TODO: implement this when we have the sonification APIs
774 }
775
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800776 /**
777 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700778 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800779 * @param personCursor the URI for the contact being displayed
780 */
Evan Millar11d628c2009-09-02 08:55:01 -0700781 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800782 // Clear out the old entries
783 final int numSections = mSections.size();
784 for (int i = 0; i < numSections; i++) {
785 mSections.get(i).clear();
786 }
787
Evan Millar7911ff52009-07-21 15:55:18 -0700788 mRawContactIds.clear();
Jeff Sharkeye0408762009-11-03 12:57:16 -0800789
Megha Joshid5afd8a2009-09-22 09:38:41 -0700790 mReadOnlySourcesCnt = 0;
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700791 mWritableSourcesCnt = 0;
Jeff Sharkeye0408762009-11-03 12:57:16 -0800792 mAllRestricted = true;
793
Megha Joshid5afd8a2009-09-22 09:38:41 -0700794 mWritableRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700795
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700796 final Context context = this;
797 final Sources sources = Sources.getInstance(context);
Evan Millar11d628c2009-09-02 08:55:01 -0700798
Evan Millar66388be2009-05-28 15:41:07 -0700799 // Build up method entries
Evan Millar6a61a1a2009-09-29 14:00:43 -0700800 if (mLookupUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700801 for (Entity entity: mEntities) {
802 final ContentValues entValues = entity.getEntityValues();
803 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700804 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700805
Jeff Sharkeye0408762009-11-03 12:57:16 -0800806 // Mark when this contact has any unrestricted components
807 final boolean isRestricted = entValues.getAsInteger(RawContacts.IS_RESTRICTED) != 0;
808 if (!isRestricted) mAllRestricted = false;
809
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700810 if (!mRawContactIds.contains(rawContactId)) {
811 mRawContactIds.add(rawContactId);
812 }
Megha Joshic508bd82009-10-01 17:31:49 -0700813 ContactsSource contactsSource = sources.getInflatedSource(accountType,
814 ContactsSource.LEVEL_SUMMARY);
815 if (contactsSource != null && contactsSource.readOnly) {
816 mReadOnlySourcesCnt += 1;
817 } else {
818 mWritableSourcesCnt += 1;
Evan Millarcdeea942009-10-07 11:47:35 -0700819 mWritableRawContactIds.add(rawContactId);
820 }
Megha Joshic508bd82009-10-01 17:31:49 -0700821
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700822
Evan Millar11d628c2009-09-02 08:55:01 -0700823 for (NamedContentValues subValue : entity.getSubValues()) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700824 final ContentValues entryValues = subValue.values;
825 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
826
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700827 final long dataId = entryValues.getAsLong(Data._ID);
828 final String mimeType = entryValues.getAsString(Data.MIMETYPE);
829 if (mimeType == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -0700830
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700831 final DataKind kind = sources.getKindOrFallback(accountType, mimeType, this,
Evan Millar11d628c2009-09-02 08:55:01 -0700832 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700833 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800834
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700835 final ViewEntry entry = ViewEntry.fromValues(context, mimeType, kind,
836 rawContactId, dataId, entryValues);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800837
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700838 final boolean hasData = !TextUtils.isEmpty(entry.data);
Evan Millarff04a272009-09-26 15:23:37 -0700839 final boolean isSuperPrimary = entryValues.getAsInteger(
840 Data.IS_SUPER_PRIMARY) != 0;
Evan Millar11d628c2009-09-02 08:55:01 -0700841
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700842 if (Phone.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
Evan Millarff04a272009-09-26 15:23:37 -0700843 // Build phone entries
844 mNumPhoneNumbers++;
Evan Millar11d628c2009-09-02 08:55:01 -0700845
Evan Millarff04a272009-09-26 15:23:37 -0700846 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700847 Uri.fromParts(Constants.SCHEME_TEL, entry.data, null));
Evan Millarff04a272009-09-26 15:23:37 -0700848 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700849 Uri.fromParts(Constants.SCHEME_SMSTO, entry.data, null));
Evan Millar49714ee2009-09-02 16:42:47 -0700850
Evan Millarff04a272009-09-26 15:23:37 -0700851 entry.isPrimary = isSuperPrimary;
852 mPhoneEntries.add(entry);
853
854 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
855 || mShowSmsLinksForAllPhones) {
856 // Add an SMS entry
857 if (kind.iconAltRes > 0) {
858 entry.secondaryActionIcon = kind.iconAltRes;
Evan Millar49714ee2009-09-02 16:42:47 -0700859 }
Evan Millarff04a272009-09-26 15:23:37 -0700860 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700861 } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
Evan Millarff04a272009-09-26 15:23:37 -0700862 // Build email entries
863 entry.intent = new Intent(Intent.ACTION_SENDTO,
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700864 Uri.fromParts(Constants.SCHEME_MAILTO, entry.data, null));
Evan Millarff04a272009-09-26 15:23:37 -0700865 entry.isPrimary = isSuperPrimary;
866 mEmailEntries.add(entry);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700867
868 // When Email rows have status, create additional Im row
869 final DataStatus status = mStatuses.get(entry.id);
870 if (status != null) {
871 final String imMime = Im.CONTENT_ITEM_TYPE;
872 final DataKind imKind = sources.getKindOrFallback(accountType,
873 imMime, this, ContactsSource.LEVEL_MIMETYPES);
874 final ViewEntry imEntry = ViewEntry.fromValues(context,
875 imMime, imKind, rawContactId, dataId, entryValues);
876 imEntry.intent = ContactsUtils.buildImIntent(entryValues);
877 imEntry.applyStatus(status, false);
878 mImEntries.add(imEntry);
879 }
880 } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
Evan Millarff04a272009-09-26 15:23:37 -0700881 // Build postal entries
882 entry.maxLines = 4;
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700883 entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri);
Evan Millarff04a272009-09-26 15:23:37 -0700884 mPostalEntries.add(entry);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700885 } else if (Im.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
886 // Build IM entries
887 entry.intent = ContactsUtils.buildImIntent(entryValues);
Evan Millarff04a272009-09-26 15:23:37 -0700888 if (TextUtils.isEmpty(entry.label)) {
889 entry.label = getString(R.string.chat).toLowerCase();
890 }
Evan Millar11d628c2009-09-02 08:55:01 -0700891
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700892 // Apply presence and status details when available
893 final DataStatus status = mStatuses.get(entry.id);
894 if (status != null) {
895 entry.applyStatus(status, false);
Evan Millarff04a272009-09-26 15:23:37 -0700896 }
Evan Millarff04a272009-09-26 15:23:37 -0700897 mImEntries.add(entry);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700898 } else if ((Organization.CONTENT_ITEM_TYPE.equals(mimeType)
899 || Nickname.CONTENT_ITEM_TYPE.equals(mimeType)) && hasData) {
Evan Millarff04a272009-09-26 15:23:37 -0700900 // Build organization and note entries
901 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -0700902 mOrganizationEntries.add(entry);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700903 } else if (Note.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
Evan Millar11d628c2009-09-02 08:55:01 -0700904 // Build note entries
Evan Millar11d628c2009-09-02 08:55:01 -0700905 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -0700906 entry.maxLines = 10;
907 mOtherEntries.add(entry);
Evan Millar951fa9c2009-10-26 14:57:43 -0700908 } else if (Website.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) {
909 // Build note entries
910 entry.uri = null;
911 entry.maxLines = 10;
912 try {
913 WebAddress webAddress = new WebAddress(entry.data);
Makoto Onuki7a707382009-11-19 13:35:55 +0900914 entry.intent = new Intent(Intent.ACTION_VIEW,
Evan Millar951fa9c2009-10-26 14:57:43 -0700915 Uri.parse(webAddress.toString()));
916 } catch (ParseException e) {
917 Log.e(TAG, "Couldn't parse website: " + entry.data);
918 }
919 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700920 } else {
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700921 // Handle showing custom rows
Evan Millarff04a272009-09-26 15:23:37 -0700922 entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri);
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700923
924 // Use social summary when requested by external source
925 final DataStatus status = mStatuses.get(entry.id);
926 final boolean hasSocial = kind.actionBodySocial && status != null;
927 if (hasSocial) {
928 entry.applyStatus(status, true);
929 }
930
931 if (hasSocial || hasData) {
932 mOtherEntries.add(entry);
933 }
Evan Millar11d628c2009-09-02 08:55:01 -0700934 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700935 }
Evan Millar66388be2009-05-28 15:41:07 -0700936 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800937 }
938 }
939
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700940 static String buildActionString(DataKind kind, ContentValues values, boolean lowerCase,
941 Context context) {
Evan Millar11d628c2009-09-02 08:55:01 -0700942 if (kind.actionHeader == null) {
943 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700944 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700945 CharSequence actionHeader = kind.actionHeader.inflateUsing(context, values);
Evan Millar11d628c2009-09-02 08:55:01 -0700946 if (actionHeader == null) {
947 return null;
948 }
949 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
950 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700951
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700952 static String buildDataString(DataKind kind, ContentValues values, Context context) {
Evan Millar11d628c2009-09-02 08:55:01 -0700953 if (kind.actionBody == null) {
954 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800955 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700956 CharSequence actionBody = kind.actionBody.inflateUsing(context, values);
Evan Millar11d628c2009-09-02 08:55:01 -0700957 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800958 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700959
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800960 /**
961 * A basic structure with the data for a contact entry in the list.
962 */
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700963 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
964 public Context context = null;
Jeff Sharkeyab066932009-09-21 09:55:30 -0700965 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800966 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -0700967 public boolean isPrimary = false;
Evan Millar15e514d2009-08-04 10:14:57 -0700968 public int secondaryActionIcon = -1;
969 public Intent intent;
970 public Intent secondaryIntent = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800971 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700972 public ArrayList<Long> ids = new ArrayList<Long>();
973 public int collapseCount = 0;
974
Jeff Sharkey624ddc32009-10-01 21:32:19 -0700975 public int presence = -1;
976 public int presenceIcon = -1;
977
978 public CharSequence footerLine = null;
979
980 private ViewEntry() {
981 }
982
983 /**
984 * Build new {@link ViewEntry} and populate from the given values.
985 */
986 public static ViewEntry fromValues(Context context, String mimeType, DataKind kind,
987 long rawContactId, long dataId, ContentValues values) {
988 final ViewEntry entry = new ViewEntry();
989 entry.context = context;
990 entry.contactId = rawContactId;
991 entry.id = dataId;
992 entry.uri = ContentUris.withAppendedId(Data.CONTENT_URI, entry.id);
993 entry.mimetype = mimeType;
994 entry.label = buildActionString(kind, values, false, context);
995 entry.data = buildDataString(kind, values, context);
996
997 if (kind.typeColumn != null && values.containsKey(kind.typeColumn)) {
998 entry.type = values.getAsInteger(kind.typeColumn);
999 }
1000 if (kind.iconRes > 0) {
1001 entry.resPackageName = kind.resPackageName;
1002 entry.actionIcon = kind.iconRes;
1003 }
1004
1005 return entry;
1006 }
1007
1008 /**
1009 * Apply given {@link DataStatus} values over this {@link ViewEntry}
1010 *
1011 * @param fillData When true, the given status replaces {@link #data}
1012 * and {@link #footerLine}. Otherwise only {@link #presence}
1013 * is updated.
1014 */
1015 public ViewEntry applyStatus(DataStatus status, boolean fillData) {
1016 presence = status.getPresence();
1017 presenceIcon = (presence == -1) ? -1 :
1018 StatusUpdates.getPresenceIconResourceId(this.presence);
1019
1020 if (fillData && status.isValid()) {
1021 this.data = status.getStatus().toString();
1022 this.footerLine = status.getTimestampLabel(context);
1023 }
1024
1025 return this;
1026 }
1027
Evan Millar54a5c9f2009-06-23 17:41:09 -07001028 public boolean collapseWith(ViewEntry entry) {
1029 // assert equal collapse keys
Evan Millaradb0f8c2009-09-28 17:20:50 -07001030 if (!shouldCollapseWith(entry)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001031 return false;
1032 }
1033
1034 // Choose the label associated with the highest type precedence.
1035 if (TypePrecedence.getTypePrecedence(mimetype, type)
1036 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1037 type = entry.type;
1038 label = entry.label;
1039 }
1040
1041 // Choose the max of the maxLines and maxLabelLines values.
1042 maxLines = Math.max(maxLines, entry.maxLines);
1043 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1044
1045 // Choose the presence with the highest precedence.
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001046 if (StatusUpdates.getPresencePrecedence(presence)
1047 < StatusUpdates.getPresencePrecedence(entry.presence)) {
1048 presence = entry.presence;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001049 }
1050
1051 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001052 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001053
1054 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1055
1056 // Keep track of all the ids that have been collapsed with this one.
1057 ids.add(entry.id);
1058 collapseCount++;
1059 return true;
1060 }
1061
Evan Millaradb0f8c2009-09-28 17:20:50 -07001062 public boolean shouldCollapseWith(ViewEntry entry) {
1063 if (entry == null) {
1064 return false;
1065 }
1066
Makoto Onukic710b0e2009-10-13 15:43:24 -07001067 if (!ContactsUtils.areDataEqual(context, mimetype, data, entry.mimetype, entry.data)) {
1068 return false;
Evan Millaradb0f8c2009-09-28 17:20:50 -07001069 }
1070
Makoto Onukic710b0e2009-10-13 15:43:24 -07001071 if (!TextUtils.equals(mimetype, entry.mimetype)
1072 || !ContactsUtils.areIntentActionEqual(intent, entry.intent)
1073 || !ContactsUtils.areIntentActionEqual(secondaryIntent, entry.secondaryIntent)
Evan Millaradb0f8c2009-09-28 17:20:50 -07001074 || actionIcon != entry.actionIcon) {
1075 return false;
1076 }
1077
1078 return true;
1079 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001080 }
1081
Evan Millar15e514d2009-08-04 10:14:57 -07001082 /** Cache of the children views of a row */
1083 static class ViewCache {
1084 public TextView label;
1085 public TextView data;
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001086 public TextView footer;
Evan Millar15e514d2009-08-04 10:14:57 -07001087 public ImageView actionIcon;
1088 public ImageView presenceIcon;
1089 public ImageView primaryIcon;
1090 public ImageView secondaryActionButton;
1091 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001092
Evan Millar15e514d2009-08-04 10:14:57 -07001093 // Need to keep track of this too
1094 ViewEntry entry;
1095 }
1096
1097 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1098 implements View.OnClickListener {
1099
Evan Millar5c22c3b2009-05-29 11:37:54 -07001100
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001101 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1102 super(context, sections, SHOW_SEPARATORS);
1103 }
1104
Evan Millar15e514d2009-08-04 10:14:57 -07001105 public void onClick(View v) {
1106 Intent intent = (Intent) v.getTag();
1107 startActivity(intent);
1108 }
1109
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001110 @Override
1111 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001112 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001113 View v;
1114
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001115 ViewCache views;
1116
1117 // Check to see if we can reuse convertView
1118 if (convertView != null) {
1119 v = convertView;
1120 views = (ViewCache) v.getTag();
1121 } else {
1122 // Create a new view if needed
1123 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1124
1125 // Cache the children
1126 views = new ViewCache();
1127 views.label = (TextView) v.findViewById(android.R.id.text1);
1128 views.data = (TextView) v.findViewById(android.R.id.text2);
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001129 views.footer = (TextView) v.findViewById(R.id.footer);
Evan Millar15e514d2009-08-04 10:14:57 -07001130 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1131 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1132 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1133 views.secondaryActionButton = (ImageView) v.findViewById(
1134 R.id.secondary_action_button);
1135 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001136 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001137 v.setTag(views);
1138 }
1139
1140 // Update the entry in the view cache
1141 views.entry = entry;
1142
1143 // Bind the data to the view
1144 bindView(v, entry);
1145 return v;
1146 }
1147
1148 @Override
1149 protected View newView(int position, ViewGroup parent) {
1150 // getView() handles this
1151 throw new UnsupportedOperationException();
1152 }
1153
1154 @Override
1155 protected void bindView(View view, ViewEntry entry) {
1156 final Resources resources = mContext.getResources();
1157 ViewCache views = (ViewCache) view.getTag();
1158
1159 // Set the label
1160 TextView label = views.label;
1161 setMaxLines(label, entry.maxLabelLines);
1162 label.setText(entry.label);
1163
1164 // Set the data
1165 TextView data = views.data;
1166 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001167 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001168 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001169 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1170 } else {
1171 data.setText(entry.data);
1172 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001173 setMaxLines(data, entry.maxLines);
1174 }
1175
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001176 // Set the footer
1177 if (!TextUtils.isEmpty(entry.footerLine)) {
1178 views.footer.setText(entry.footerLine);
1179 views.footer.setVisibility(View.VISIBLE);
1180 } else {
1181 views.footer.setVisibility(View.GONE);
1182 }
1183
Evan Millar15e514d2009-08-04 10:14:57 -07001184 // Set the primary icon
1185 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1186
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001187 // Set the action icon
1188 ImageView action = views.actionIcon;
1189 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001190 Drawable actionIcon;
1191 if (entry.resPackageName != null) {
1192 // Load external resources through PackageManager
1193 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1194 entry.actionIcon, null);
1195 } else {
1196 actionIcon = resources.getDrawable(entry.actionIcon);
1197 }
1198 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001199 action.setVisibility(View.VISIBLE);
1200 } else {
1201 // Things should still line up as if there was an icon, so make it invisible
1202 action.setVisibility(View.INVISIBLE);
1203 }
1204
1205 // Set the presence icon
1206 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001207 if (entry.presenceIcon != -1) {
1208 presenceIcon = resources.getDrawable(entry.presenceIcon);
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001209 } else if (entry.presence != -1) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001210 presenceIcon = resources.getDrawable(
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001211 StatusUpdates.getPresenceIconResourceId(entry.presence));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001212 }
Evan Millar15e514d2009-08-04 10:14:57 -07001213 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001214 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001215 presenceIconView.setImageDrawable(presenceIcon);
1216 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001217 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001218 presenceIconView.setVisibility(View.GONE);
1219 }
1220
1221 // Set the secondary action button
1222 ImageView secondaryActionView = views.secondaryActionButton;
1223 Drawable secondaryActionIcon = null;
1224 if (entry.secondaryActionIcon != -1) {
1225 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1226 }
1227 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1228 secondaryActionView.setImageDrawable(secondaryActionIcon);
1229 secondaryActionView.setTag(entry.secondaryIntent);
1230 secondaryActionView.setVisibility(View.VISIBLE);
1231 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1232 } else {
1233 secondaryActionView.setVisibility(View.GONE);
1234 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001235 }
1236 }
1237
1238 private void setMaxLines(TextView textView, int maxLines) {
1239 if (maxLines == 1) {
1240 textView.setSingleLine(true);
1241 textView.setEllipsize(TextUtils.TruncateAt.END);
1242 } else {
1243 textView.setSingleLine(false);
1244 textView.setMaxLines(maxLines);
1245 textView.setEllipsize(null);
1246 }
1247 }
1248 }
Jeff Sharkey624ddc32009-10-01 21:32:19 -07001249
1250 private interface StatusQuery {
1251 final String[] PROJECTION = new String[] {
1252 Data._ID,
1253 Data.STATUS,
1254 Data.STATUS_RES_PACKAGE,
1255 Data.STATUS_ICON,
1256 Data.STATUS_LABEL,
1257 Data.STATUS_TIMESTAMP,
1258 Data.PRESENCE,
1259 };
1260
1261 final int _ID = 0;
1262 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001263}