blob: d6ab83c7e7b4720b96842db4c57266a70c59f3ad [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;
Evan Millar11d628c2009-09-02 08:55:01 -070025import com.android.contacts.util.NotifyingAsyncQueryHandler;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080026import com.android.internal.telephony.ITelephony;
Evan Millar11d628c2009-09-02 08:55:01 -070027import com.android.internal.widget.ContactHeaderWidget;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080028
Evan Millar11d628c2009-09-02 08:55:01 -070029import android.app.Activity;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080030import android.app.AlertDialog;
31import android.app.Dialog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080032import android.content.ActivityNotFoundException;
33import android.content.ContentResolver;
34import android.content.ContentUris;
35import android.content.ContentValues;
36import android.content.Context;
37import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070038import android.content.Entity;
Evan Millar11d628c2009-09-02 08:55:01 -070039import android.content.EntityIterator;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080040import android.content.Intent;
Evan Millar11d628c2009-09-02 08:55:01 -070041import android.content.Entity.NamedContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080042import android.content.res.Resources;
43import android.database.ContentObserver;
44import android.database.Cursor;
45import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080046import android.net.Uri;
47import android.os.Bundle;
48import android.os.Handler;
49import android.os.RemoteException;
50import android.os.ServiceManager;
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -070051import android.provider.ContactsContract;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070052import android.provider.ContactsContract.AggregationExceptions;
53import android.provider.ContactsContract.CommonDataKinds;
Evan Millar11d628c2009-09-02 08:55:01 -070054import android.provider.ContactsContract.Contacts;
Evan Millar66388be2009-05-28 15:41:07 -070055import android.provider.ContactsContract.Data;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070056import android.provider.ContactsContract.RawContacts;
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -070057import android.provider.ContactsContract.StatusUpdates;
Evan Millar54a5c9f2009-06-23 17:41:09 -070058import android.provider.ContactsContract.CommonDataKinds.Phone;
59import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080060import android.text.TextUtils;
61import android.util.Log;
62import android.view.ContextMenu;
63import android.view.KeyEvent;
Evan Millar11d628c2009-09-02 08:55:01 -070064import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080065import android.view.Menu;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070066import android.view.MenuInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080067import android.view.MenuItem;
68import android.view.View;
69import android.view.ViewGroup;
Evan Millar11d628c2009-09-02 08:55:01 -070070import android.view.Window;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080071import android.view.ContextMenu.ContextMenuInfo;
72import android.widget.AdapterView;
Evan Millar7911ff52009-07-21 15:55:18 -070073import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080074import android.widget.ImageView;
75import android.widget.ListView;
76import android.widget.TextView;
77import android.widget.Toast;
78
79import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080080
81/**
82 * Displays the details of a specific contact.
83 */
Evan Millar11d628c2009-09-02 08:55:01 -070084public class ViewContactActivity extends Activity
Evan Millar7911ff52009-07-21 15:55:18 -070085 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
Evan Millardf181202009-09-30 15:15:51 -070086 AdapterView.OnItemClickListener, NotifyingAsyncQueryHandler.AsyncQueryListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087 private static final String TAG = "ViewContact";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080088
89 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080090
91 private static final int DIALOG_CONFIRM_DELETE = 1;
Megha Joshid5afd8a2009-09-22 09:38:41 -070092 private static final int DIALOG_CONFIRM_READONLY_DELETE = 2;
93 private static final int DIALOG_CONFIRM_MULTIPLE_DELETE = 3;
Megha Joshic508bd82009-10-01 17:31:49 -070094 private static final int DIALOG_CONFIRM_READONLY_HIDE = 4;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080095
Evan Millar8a79cee2009-08-19 17:20:49 -070096 private static final int REQUEST_JOIN_CONTACT = 1;
Evan Millar9cd81242009-09-26 16:02:31 -070097 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -070098
Evan Millar8a79cee2009-08-19 17:20:49 -070099 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800100
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700101 protected Uri mLookupUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800102 private ContentResolver mResolver;
103 private ViewAdapter mAdapter;
104 private int mNumPhoneNumbers = 0;
105
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700106 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700107 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700108 */
Evan Millar7911ff52009-07-21 15:55:18 -0700109 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700110
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800111 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
112 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
113 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
114 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
115 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
116 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700117 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800118 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
119 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
120
121 private Cursor mCursor;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700122
Evan Millar11d628c2009-09-02 08:55:01 -0700123 protected ContactHeaderWidget mContactHeaderWidget;
124 private NotifyingAsyncQueryHandler mHandler;
125
126 protected LayoutInflater mInflater;
127
Megha Joshid5afd8a2009-09-22 09:38:41 -0700128 protected int mReadOnlySourcesCnt;
129 protected int mWritableSourcesCnt;
130 protected ArrayList<Long> mWritableRawContactIds = new ArrayList<Long>();
Evan Millar11d628c2009-09-02 08:55:01 -0700131
132 private static final int TOKEN_QUERY = 0;
133
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800134 private ContentObserver mObserver = new ContentObserver(new Handler()) {
135 @Override
136 public boolean deliverSelfNotifications() {
137 return true;
138 }
139
140 @Override
141 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700142 if (mCursor != null && !mCursor.isClosed()) {
143 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800144 }
145 }
146 };
147
148 public void onClick(DialogInterface dialog, int which) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700149 closeCursor();
Megha Joshic508bd82009-10-01 17:31:49 -0700150 getContentResolver().delete(mLookupUri, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800151 finish();
152 }
153
Evan Millar7911ff52009-07-21 15:55:18 -0700154 private FrameLayout mTabContentLayout;
155 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800156 private boolean mShowSmsLinksForAllPhones;
Evan Millar11d628c2009-09-02 08:55:01 -0700157 private ArrayList<Entity> mEntities = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800158
159 @Override
160 protected void onCreate(Bundle icicle) {
161 super.onCreate(icicle);
162
Evan Millar11d628c2009-09-02 08:55:01 -0700163 final Intent intent = getIntent();
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700164 Uri data = intent.getData();
165 String authority = data.getAuthority();
166 if (ContactsContract.AUTHORITY.equals(authority)) {
167 mLookupUri = data;
168 } else if (android.provider.Contacts.AUTHORITY.equals(authority)) {
169 final long rawContactId = ContentUris.parseId(data);
170 mLookupUri = RawContacts.getContactLookupUri(getContentResolver(),
171 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700172
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700173 }
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700174 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700175
176 requestWindowFeature(Window.FEATURE_NO_TITLE);
177 setContentView(R.layout.contact_card_layout);
178
179 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
180 mContactHeaderWidget.showStar(true);
Evan Millar11d628c2009-09-02 08:55:01 -0700181 mContactHeaderWidget.setExcludeMimes(new String[] {
182 Contacts.CONTENT_ITEM_TYPE
183 });
184
Evan Millar11d628c2009-09-02 08:55:01 -0700185 mHandler = new NotifyingAsyncQueryHandler(this, this);
186
Evan Millar7911ff52009-07-21 15:55:18 -0700187 mListView = new ListView(this);
188 mListView.setOnCreateContextMenuListener(this);
189 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
190 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800191
Evan Millar7911ff52009-07-21 15:55:18 -0700192 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
193 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800194
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800195 mResolver = getContentResolver();
196
197 // Build the list of sections. The order they're added to mSections dictates the
198 // order they are displayed in the list.
199 mSections.add(mPhoneEntries);
200 mSections.add(mSmsEntries);
201 mSections.add(mEmailEntries);
202 mSections.add(mImEntries);
203 mSections.add(mPostalEntries);
204 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700205 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800206 mSections.add(mOtherEntries);
207
208 //TODO Read this value from a preference
209 mShowSmsLinksForAllPhones = true;
Evan Millar11d628c2009-09-02 08:55:01 -0700210 }
211
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800212 @Override
213 protected void onResume() {
214 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700215 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800216 }
217
218 @Override
219 protected void onPause() {
220 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700221 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800222 }
223
224 @Override
225 protected void onDestroy() {
226 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700227 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800228 }
229
230 @Override
231 protected Dialog onCreateDialog(int id) {
232 switch (id) {
233 case DIALOG_CONFIRM_DELETE:
234 return new AlertDialog.Builder(this)
235 .setTitle(R.string.deleteConfirmation_title)
236 .setIcon(android.R.drawable.ic_dialog_alert)
237 .setMessage(R.string.deleteConfirmation)
238 .setNegativeButton(android.R.string.cancel, null)
239 .setPositiveButton(android.R.string.ok, this)
240 .setCancelable(false)
241 .create();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700242 case DIALOG_CONFIRM_READONLY_DELETE:
243 return new AlertDialog.Builder(this)
244 .setTitle(R.string.deleteConfirmation_title)
245 .setIcon(android.R.drawable.ic_dialog_alert)
246 .setMessage(R.string.readOnlyContactDeleteConfirmation)
247 .setNegativeButton(android.R.string.cancel, null)
248 .setPositiveButton(android.R.string.ok, this)
249 .setCancelable(false)
250 .create();
251 case DIALOG_CONFIRM_MULTIPLE_DELETE:
252 return new AlertDialog.Builder(this)
253 .setTitle(R.string.deleteConfirmation_title)
254 .setIcon(android.R.drawable.ic_dialog_alert)
255 .setMessage(R.string.multipleContactDeleteConfirmation)
256 .setNegativeButton(android.R.string.cancel, null)
257 .setPositiveButton(android.R.string.ok, this)
258 .setCancelable(false)
259 .create();
Megha Joshic508bd82009-10-01 17:31:49 -0700260 case DIALOG_CONFIRM_READONLY_HIDE: {
261 return new AlertDialog.Builder(this)
262 .setTitle(R.string.deleteConfirmation_title)
263 .setIcon(android.R.drawable.ic_dialog_alert)
264 .setMessage(R.string.readOnlyContactWarning)
265 .setPositiveButton(android.R.string.ok, this)
266 .create();
267 }
268
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800269 }
270 return null;
271 }
272
Evan Millar11d628c2009-09-02 08:55:01 -0700273 // QUERY CODE //
274 /** {@inheritDoc} */
275 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
276 try{
277 if (token == TOKEN_QUERY) {
Evan Millar11d628c2009-09-02 08:55:01 -0700278 mEntities = readEntities(iterator);
Evan Millar85863112009-09-21 14:35:18 -0700279 bindData();
Evan Millar11d628c2009-09-02 08:55:01 -0700280 }
281 } finally {
282 if (iterator != null) {
283 iterator.close();
284 }
285 }
286 }
287
288 /** {@inheritDoc} */
289 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700290 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700291 }
292
Evan Millar6a61a1a2009-09-29 14:00:43 -0700293 private long getRefreshedContactId() {
294 Uri freshContactUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
295 if (freshContactUri != null) {
296 return ContentUris.parseId(freshContactUri);
297 }
298 return -1;
299 }
300
Evan Millar11d628c2009-09-02 08:55:01 -0700301 private ArrayList<Entity> readEntities(EntityIterator iterator) {
302 ArrayList<Entity> entities = new ArrayList<Entity>();
303 try {
304 while (iterator.hasNext()) {
305 entities.add(iterator.next());
306 }
307 } catch (RemoteException e) {
308 }
309
310 return entities;
311 }
312
313 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700314 closeCursor();
315
Evan Millar6a61a1a2009-09-29 14:00:43 -0700316 Uri uri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700317 if (mLookupUri != null) {
318 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
319 if (mLookupUri != null) {
Evan Millar6a61a1a2009-09-29 14:00:43 -0700320 uri = Contacts.lookupContact(getContentResolver(), mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700321 }
322 }
323
Evan Millar6a61a1a2009-09-29 14:00:43 -0700324 if (uri == null) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700325
326 // TODO either figure out a way to prevent a flash of black background or
327 // use some other UI than a toast
328 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
329 Log.e(TAG, "invalid contact uri: " + mLookupUri);
330 finish();
331 return;
332 }
333
Evan Millar6a61a1a2009-09-29 14:00:43 -0700334 mCursor = mResolver.query(Uri.withAppendedPath(uri, Contacts.Data.CONTENT_DIRECTORY),
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700335 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
336 mCursor.registerContentObserver(mObserver);
337
Evan Millar6a61a1a2009-09-29 14:00:43 -0700338 long contactId = ContentUris.parseId(uri);
Evan Millar11d628c2009-09-02 08:55:01 -0700339 mHandler.startQueryEntities(TOKEN_QUERY, null,
340 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700341
Evan Millar9b1a1242009-09-16 10:26:10 -0700342 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700343 }
344
345 private void closeCursor() {
346 if (mCursor != null) {
347 mCursor.unregisterContentObserver(mObserver);
348 mCursor.close();
349 mCursor = null;
350 }
Evan Millar11d628c2009-09-02 08:55:01 -0700351 }
352
353 private void bindData() {
354
355 // Build up the contact entries
356 buildEntries();
357
358 // Collapse similar data items in select sections.
359 Collapser.collapseList(mPhoneEntries);
360 Collapser.collapseList(mSmsEntries);
361 Collapser.collapseList(mEmailEntries);
362 Collapser.collapseList(mPostalEntries);
363
364 if (mAdapter == null) {
365 mAdapter = new ViewAdapter(this, mSections);
366 mListView.setAdapter(mAdapter);
367 } else {
368 mAdapter.setSections(mSections, SHOW_SEPARATORS);
369 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800370 }
371
372 @Override
373 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700374 super.onCreateOptionsMenu(menu);
375
376 final MenuInflater inflater = getMenuInflater();
377 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800378 return true;
379 }
380
381 @Override
382 public boolean onPrepareOptionsMenu(Menu menu) {
383 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700384
Evan Millar51514db2009-09-22 11:42:35 -0700385 // Only allow edit when we have at least one raw_contact id
386 final boolean hasRawContact = (mRawContactIds.size() > 0);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700387 menu.findItem(R.id.menu_edit).setEnabled(hasRawContact);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700388
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800389 return true;
390 }
391
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800392 @Override
393 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
394 AdapterView.AdapterContextMenuInfo info;
395 try {
396 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
397 } catch (ClassCastException e) {
398 Log.e(TAG, "bad menuInfo", e);
399 return;
400 }
401
402 // This can be null sometimes, don't crash...
403 if (info == null) {
404 Log.e(TAG, "bad menuInfo");
405 return;
406 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700407
Evan Millar45e0ed32009-06-01 16:44:38 -0700408 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
Neel Parekha8fb68a2009-09-25 18:05:18 -0700409 menu.setHeaderTitle(R.string.contactOptionsTitle);
Evan Millar45e0ed32009-06-01 16:44:38 -0700410 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
411 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700412 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
413 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700414 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800415 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700416 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
417 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700418 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700419 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800420 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700421 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700422 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
423 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800424 }
425
426 @Override
427 public boolean onOptionsItemSelected(MenuItem item) {
428 switch (item.getItemId()) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700429 case R.id.menu_edit: {
Evan Millardf181202009-09-30 15:15:51 -0700430 Long rawContactIdToEdit = null;
431 if (mRawContactIds.size() > 0) {
432 rawContactIdToEdit = mRawContactIds.get(0);
433 } else {
434 // There is no rawContact to edit.
435 break;
Evan Millardb5d88c2009-08-28 09:31:57 -0700436 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700437 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
438 rawContactIdToEdit);
Evan Millar9cd81242009-09-26 16:02:31 -0700439 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
440 REQUEST_EDIT_CONTACT);
Evan Millar8a79cee2009-08-19 17:20:49 -0700441 break;
442 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700443 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800444 // Get confirmation
Megha Joshid5afd8a2009-09-22 09:38:41 -0700445 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
446 showDialog(DIALOG_CONFIRM_READONLY_DELETE);
Megha Joshic508bd82009-10-01 17:31:49 -0700447 } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) {
448 showDialog(DIALOG_CONFIRM_READONLY_HIDE);
449 } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) {
Megha Joshid5afd8a2009-09-22 09:38:41 -0700450 showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
451 } else {
452 showDialog(DIALOG_CONFIRM_DELETE);
453 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800454 return true;
455 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700456 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700457 showJoinAggregateActivity();
458 return true;
459 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700460 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700461 showOptionsActivity();
462 return true;
463 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700464 case R.id.menu_share: {
Jeff Sharkey93049312009-09-27 17:28:13 -0700465 // TODO: Keep around actual LOOKUP_KEY, or formalize method of extracting
466 final String lookupKey = mLookupUri.getPathSegments().get(2);
467 final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
468
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700469 final Intent intent = new Intent(Intent.ACTION_SEND);
Jeff Sharkey93049312009-09-27 17:28:13 -0700470 intent.setType(Contacts.CONTENT_VCARD_TYPE);
471 intent.putExtra(Intent.EXTRA_STREAM, shareUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700472
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700473 // Launch chooser to share contact via
474 final CharSequence chooseTitle = getText(R.string.share_via);
475 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700476
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700477 try {
478 startActivity(chooseIntent);
479 } catch (ActivityNotFoundException ex) {
480 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800481 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700482 return true;
483 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800484 }
485 return super.onOptionsItemSelected(item);
486 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700487
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800488 @Override
489 public boolean onContextItemSelected(MenuItem item) {
490 switch (item.getItemId()) {
491 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700492 if (makeItemDefault(item)) {
493 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800494 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700495 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800496 }
497 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700498
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800499 return super.onContextItemSelected(item);
500 }
501
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700502 private boolean makeItemDefault(MenuItem item) {
503 ViewEntry entry = getViewEntryForMenuItem(item);
504 if (entry == null) {
505 return false;
506 }
507
508 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700509 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700510 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700511 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
512 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700513 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700514 return true;
515 }
516
517 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700518 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700519 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700520 public void showJoinAggregateActivity() {
Evan Millar6a61a1a2009-09-29 14:00:43 -0700521 long freshId = getRefreshedContactId();
522 if (freshId > 0) {
523 String displayName = null;
524 if (mCursor.moveToFirst()) {
525 displayName = mCursor.getString(0);
526 }
527 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
528 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, freshId);
529 if (displayName != null) {
530 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_NAME, displayName);
531 }
532 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikova5cfca32009-09-29 18:13:08 -0700533 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700534 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700535
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700536 @Override
537 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700538 if (requestCode == REQUEST_JOIN_CONTACT) {
539 if (resultCode == RESULT_OK && intent != null) {
540 final long contactId = ContentUris.parseId(intent.getData());
541 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700542 }
Evan Millar9cd81242009-09-26 16:02:31 -0700543 } else if (requestCode == REQUEST_EDIT_CONTACT) {
Evan Millardf181202009-09-30 15:15:51 -0700544 if (resultCode == EditContactActivity.RESULT_CLOSE_VIEW_ACTIVITY) {
545 finish();
Jeff Hamilton5297c6a2009-10-01 02:22:33 -0700546 } else if (resultCode == Activity.RESULT_OK) {
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -0700547 mLookupUri = intent.getData();
548 if (mLookupUri == null) {
549 finish();
550 }
Evan Millardf181202009-09-30 15:15:51 -0700551 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700552 }
553 }
554
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700555 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700556 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700557
558 // The split operation may have removed the original aggregate contact, so we need
559 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700560 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700561 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700562 }
563
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700564 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700565 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700566 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700567
568 try {
569 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700570 long rawContactId = c.getLong(0);
571 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700572 }
573 } finally {
574 c.close();
575 }
576
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700577 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
578 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700579 }
580
581 /**
582 * Given a contact ID sets an aggregation exception to either join the contact with the
583 * current aggregate or split off.
584 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700585 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700586 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700587 for (long aRawContactId : mRawContactIds) {
588 if (aRawContactId != rawContactId) {
589 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
590 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
591 values.put(AggregationExceptions.TYPE, exceptionType);
592 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
593 }
594 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700595 }
596
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700597 private void showOptionsActivity() {
598 final Intent intent = new Intent(this, ContactOptionsActivity.class);
Evan Millar6a61a1a2009-09-29 14:00:43 -0700599 intent.setData(mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700600 startActivity(intent);
601 }
602
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700603 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
604 AdapterView.AdapterContextMenuInfo info;
605 try {
606 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
607 } catch (ClassCastException e) {
608 Log.e(TAG, "bad menuInfo", e);
609 return null;
610 }
611
612 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
613 }
614
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800615 @Override
616 public boolean onKeyDown(int keyCode, KeyEvent event) {
617 switch (keyCode) {
618 case KeyEvent.KEYCODE_CALL: {
619 try {
620 ITelephony phone = ITelephony.Stub.asInterface(
621 ServiceManager.checkService("phone"));
622 if (phone != null && !phone.isIdle()) {
623 // Skip out and let the key be handled at a higher level
624 break;
625 }
626 } catch (RemoteException re) {
627 // Fall through and try to call the contact
628 }
629
Evan Millar7911ff52009-07-21 15:55:18 -0700630 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800631 if (index != -1) {
632 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700633 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
634 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800635 }
636 } else if (mNumPhoneNumbers != 0) {
637 // There isn't anything selected, call the default number
Evan Millar6a61a1a2009-09-29 14:00:43 -0700638 long freshContactId = getRefreshedContactId();
639 if (freshContactId > 0) {
640 Uri hardContacUri = ContentUris.withAppendedId(
641 Contacts.CONTENT_URI, freshContactId);
642 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, hardContacUri);
643 startActivity(intent);
644 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800645 }
646 return true;
647 }
648
649 case KeyEvent.KEYCODE_DEL: {
Megha Joshid5afd8a2009-09-22 09:38:41 -0700650 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
651 showDialog(DIALOG_CONFIRM_READONLY_DELETE);
Megha Joshic508bd82009-10-01 17:31:49 -0700652 } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) {
653 showDialog(DIALOG_CONFIRM_READONLY_HIDE);
654 } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) {
Megha Joshid5afd8a2009-09-22 09:38:41 -0700655 showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
656 } else {
657 showDialog(DIALOG_CONFIRM_DELETE);
658 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800659 return true;
660 }
661 }
662
663 return super.onKeyDown(keyCode, event);
664 }
665
Evan Millar7911ff52009-07-21 15:55:18 -0700666 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800667 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
668 if (entry != null) {
669 Intent intent = entry.intent;
Evan Millardf181202009-09-30 15:15:51 -0700670 if (intent != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800671 try {
672 startActivity(intent);
673 } catch (ActivityNotFoundException e) {
674 Log.e(TAG, "No activity found for intent: " + intent);
675 signalError();
676 }
677 } else {
678 signalError();
679 }
680 } else {
681 signalError();
682 }
683 }
684
685 /**
686 * Signal an error to the user via a beep, or some other method.
687 */
688 private void signalError() {
689 //TODO: implement this when we have the sonification APIs
690 }
691
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800692 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700693 // don't encode the url, because the Activity Manager can't find using the encoded url
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800694 StringBuilder buf = new StringBuilder("imto://");
695 buf.append(host);
696 buf.append('/');
697 buf.append(data);
698 return Uri.parse(buf.toString());
699 }
700
701 /**
702 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700703 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800704 * @param personCursor the URI for the contact being displayed
705 */
Evan Millar11d628c2009-09-02 08:55:01 -0700706 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800707 // Clear out the old entries
708 final int numSections = mSections.size();
709 for (int i = 0; i < numSections; i++) {
710 mSections.get(i).clear();
711 }
712
Evan Millar7911ff52009-07-21 15:55:18 -0700713 mRawContactIds.clear();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700714 mReadOnlySourcesCnt = 0;
715 mWritableSourcesCnt = 0;
716 mWritableRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700717
Evan Millar11d628c2009-09-02 08:55:01 -0700718 Sources sources = Sources.getInstance(this);
719
Evan Millar66388be2009-05-28 15:41:07 -0700720 // Build up method entries
Evan Millar6a61a1a2009-09-29 14:00:43 -0700721 if (mLookupUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700722 for (Entity entity: mEntities) {
723 final ContentValues entValues = entity.getEntityValues();
724 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700725 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700726
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700727 if (!mRawContactIds.contains(rawContactId)) {
728 mRawContactIds.add(rawContactId);
729 }
Megha Joshic508bd82009-10-01 17:31:49 -0700730 ContactsSource contactsSource = sources.getInflatedSource(accountType,
731 ContactsSource.LEVEL_SUMMARY);
732 if (contactsSource != null && contactsSource.readOnly) {
733 mReadOnlySourcesCnt += 1;
734 } else {
735 mWritableSourcesCnt += 1;
736 mWritableRawContactIds.add(rawContactId);
737 }
738
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700739
Evan Millar11d628c2009-09-02 08:55:01 -0700740 for (NamedContentValues subValue : entity.getSubValues()) {
741 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800742
Jeff Sharkeyab066932009-09-21 09:55:30 -0700743 final ContentValues entryValues = subValue.values;
744 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
745
Evan Millar11d628c2009-09-02 08:55:01 -0700746 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700747 if (mimetype == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -0700748
Jeff Sharkeyab066932009-09-21 09:55:30 -0700749 final DataKind kind = sources.getKindOrFallback(accountType, mimetype, this,
Evan Millar11d628c2009-09-02 08:55:01 -0700750 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700751 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800752
Evan Millar11d628c2009-09-02 08:55:01 -0700753 final long id = entryValues.getAsLong(Data._ID);
754 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700755 entry.contactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700756 entry.id = id;
757 entry.uri = uri;
758 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -0700759 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -0700760 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700761 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700762 entry.type = entryValues.getAsInteger(kind.typeColumn);
763 }
764 if (kind.iconRes > 0) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700765 entry.resPackageName = kind.resPackageName;
Evan Millar11d628c2009-09-02 08:55:01 -0700766 entry.actionIcon = kind.iconRes;
767 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800768
Evan Millar11d628c2009-09-02 08:55:01 -0700769 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700770 if (TextUtils.isEmpty(entry.data)) {
Alex Kennberg87fc3172009-03-28 06:43:06 -0700771 continue;
772 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700773
Evan Millarff04a272009-09-26 15:23:37 -0700774 final boolean isSuperPrimary = entryValues.getAsInteger(
775 Data.IS_SUPER_PRIMARY) != 0;
Evan Millar11d628c2009-09-02 08:55:01 -0700776
Evan Millarff04a272009-09-26 15:23:37 -0700777 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
778 // Build phone entries
779 mNumPhoneNumbers++;
Evan Millar11d628c2009-09-02 08:55:01 -0700780
Evan Millarff04a272009-09-26 15:23:37 -0700781 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
782 Uri.fromParts("tel", entry.data, null));
783 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
784 Uri.fromParts("sms", entry.data, null));
785 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700786
Evan Millarff04a272009-09-26 15:23:37 -0700787 entry.isPrimary = isSuperPrimary;
788 mPhoneEntries.add(entry);
789
790 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
791 || mShowSmsLinksForAllPhones) {
792 // Add an SMS entry
793 if (kind.iconAltRes > 0) {
794 entry.secondaryActionIcon = kind.iconAltRes;
Evan Millar49714ee2009-09-02 16:42:47 -0700795 }
Evan Millarff04a272009-09-26 15:23:37 -0700796 }
797 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
798 // Build email entries
799 entry.intent = new Intent(Intent.ACTION_SENDTO,
800 Uri.fromParts("mailto", entry.data, null));
801 entry.isPrimary = isSuperPrimary;
802 mEmailEntries.add(entry);
803 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
804 equals(mimetype)) {
805 // Build postal entries
806 entry.maxLines = 4;
807 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
808 mPostalEntries.add(entry);
809 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
810 // Build im entries
811 Object protocolObj = entryValues.getAsInteger(CommonDataKinds.Im.PROTOCOL);
812 String host = null;
Evan Millar49714ee2009-09-02 16:42:47 -0700813
Evan Millarff04a272009-09-26 15:23:37 -0700814 if (TextUtils.isEmpty(entry.label)) {
815 entry.label = getString(R.string.chat).toLowerCase();
816 }
Evan Millar11d628c2009-09-02 08:55:01 -0700817
Evan Millarff04a272009-09-26 15:23:37 -0700818 if (protocolObj instanceof Number) {
819 int protocol = ((Number) protocolObj).intValue();
820 host = ContactsUtils.lookupProviderNameFromId(protocol);
821 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
822 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
823 entry.maxLabelLines = 2;
Evan Millar11d628c2009-09-02 08:55:01 -0700824 }
Evan Millarff04a272009-09-26 15:23:37 -0700825 } else if (protocolObj != null) {
826 String providerName = (String) protocolObj;
827 host = providerName.toLowerCase();
828 }
829
830 // Only add the intent if there is a valid host
831 // host is null for CommonDataKinds.Im.PROTOCOL_CUSTOM
832 if (!TextUtils.isEmpty(host)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700833 entry.intent = new Intent(Intent.ACTION_SENDTO,
Evan Millarff04a272009-09-26 15:23:37 -0700834 constructImToUrl(host.toLowerCase(), entry.data));
835 }
836 //TODO(emillar) Add in presence info
Evan Millar11d628c2009-09-02 08:55:01 -0700837 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
838 entry.presenceIcon = Presence.getPresenceIconResourceId(
839 aggCursor.getInt(METHODS_STATUS_COLUMN));
840 entry.status = ...
841 }*/
Evan Millarff04a272009-09-26 15:23:37 -0700842 mImEntries.add(entry);
843 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)
844 || CommonDataKinds.Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) {
845 // Build organization and note entries
846 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -0700847 mOrganizationEntries.add(entry);
848 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
849 // Build note entries
Evan Millar11d628c2009-09-02 08:55:01 -0700850 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -0700851 entry.maxLines = 10;
852 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700853 } else {
854 // Handle showing custom
Evan Millarff04a272009-09-26 15:23:37 -0700855 entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700856 mOtherEntries.add(entry);
Evan Millar11d628c2009-09-02 08:55:01 -0700857 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700858 }
Evan Millar66388be2009-05-28 15:41:07 -0700859 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800860 }
861 }
862
Evan Millar11d628c2009-09-02 08:55:01 -0700863 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
864 if (kind.actionHeader == null) {
865 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700866 }
Evan Millar11d628c2009-09-02 08:55:01 -0700867 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
868 if (actionHeader == null) {
869 return null;
870 }
871 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
872 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700873
Evan Millar11d628c2009-09-02 08:55:01 -0700874 String buildDataString(DataKind kind, ContentValues values) {
875 if (kind.actionBody == null) {
876 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800877 }
Evan Millar11d628c2009-09-02 08:55:01 -0700878 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
879 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800880 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700881
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800882 /**
883 * A basic structure with the data for a contact entry in the list.
884 */
Evan Millaradb0f8c2009-09-28 17:20:50 -0700885 class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700886 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800887 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -0700888 public boolean isPrimary = false;
889 public int presenceIcon = -1;
890 public int secondaryActionIcon = -1;
891 public Intent intent;
892 public Intent secondaryIntent = null;
893 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800894 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700895 public ArrayList<Long> ids = new ArrayList<Long>();
896 public int collapseCount = 0;
897
898 public boolean collapseWith(ViewEntry entry) {
899 // assert equal collapse keys
Evan Millaradb0f8c2009-09-28 17:20:50 -0700900 if (!shouldCollapseWith(entry)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -0700901 return false;
902 }
903
904 // Choose the label associated with the highest type precedence.
905 if (TypePrecedence.getTypePrecedence(mimetype, type)
906 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
907 type = entry.type;
908 label = entry.label;
909 }
910
911 // Choose the max of the maxLines and maxLabelLines values.
912 maxLines = Math.max(maxLines, entry.maxLines);
913 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
914
915 // Choose the presence with the highest precedence.
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -0700916 if (StatusUpdates.getPresencePrecedence(status)
917 < StatusUpdates.getPresencePrecedence(entry.status)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -0700918 status = entry.status;
919 }
920
921 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -0700922 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700923
924 // uri, and contactdId, shouldn't make a difference. Just keep the original.
925
926 // Keep track of all the ids that have been collapsed with this one.
927 ids.add(entry.id);
928 collapseCount++;
929 return true;
930 }
931
Evan Millaradb0f8c2009-09-28 17:20:50 -0700932 public boolean shouldCollapseWith(ViewEntry entry) {
933 if (entry == null) {
934 return false;
935 }
936
937 if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)
938 && Phone.CONTENT_ITEM_TYPE.equals(entry.mimetype)) {
939 if (!PhoneNumberUtils.compare(ViewContactActivity.this, data, entry.data)) {
940 return false;
941 }
942 } else {
943 if (!equals(data, entry.data)) {
944 return false;
945 }
946 }
947
948 if (!equals(mimetype, entry.mimetype)
949 || !intentCollapsible(intent, entry.intent)
950 || !intentCollapsible(secondaryIntent, entry.secondaryIntent)
951 || actionIcon != entry.actionIcon) {
952 return false;
953 }
954
955 return true;
956 }
957
958 private boolean equals(Object a, Object b) {
959 return a==b || (a != null && a.equals(b));
960 }
961
962 private boolean intentCollapsible(Intent a, Intent b) {
963 if (a == b) {
964 return true;
965 } else if ((a != null && b != null) && equals(a.getAction(), b.getAction())) {
966 return true;
967 }
968 return false;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700969 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800970 }
971
Evan Millar15e514d2009-08-04 10:14:57 -0700972 /** Cache of the children views of a row */
973 static class ViewCache {
974 public TextView label;
975 public TextView data;
976 public ImageView actionIcon;
977 public ImageView presenceIcon;
978 public ImageView primaryIcon;
979 public ImageView secondaryActionButton;
980 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700981
Evan Millar15e514d2009-08-04 10:14:57 -0700982 // Need to keep track of this too
983 ViewEntry entry;
984 }
985
986 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
987 implements View.OnClickListener {
988
Evan Millar5c22c3b2009-05-29 11:37:54 -0700989
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800990 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
991 super(context, sections, SHOW_SEPARATORS);
992 }
993
Evan Millar15e514d2009-08-04 10:14:57 -0700994 public void onClick(View v) {
995 Intent intent = (Intent) v.getTag();
996 startActivity(intent);
997 }
998
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800999 @Override
1000 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001001 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001002 View v;
1003
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001004 ViewCache views;
1005
1006 // Check to see if we can reuse convertView
1007 if (convertView != null) {
1008 v = convertView;
1009 views = (ViewCache) v.getTag();
1010 } else {
1011 // Create a new view if needed
1012 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1013
1014 // Cache the children
1015 views = new ViewCache();
1016 views.label = (TextView) v.findViewById(android.R.id.text1);
1017 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001018 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1019 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1020 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1021 views.secondaryActionButton = (ImageView) v.findViewById(
1022 R.id.secondary_action_button);
1023 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001024 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001025 v.setTag(views);
1026 }
1027
1028 // Update the entry in the view cache
1029 views.entry = entry;
1030
1031 // Bind the data to the view
1032 bindView(v, entry);
1033 return v;
1034 }
1035
1036 @Override
1037 protected View newView(int position, ViewGroup parent) {
1038 // getView() handles this
1039 throw new UnsupportedOperationException();
1040 }
1041
1042 @Override
1043 protected void bindView(View view, ViewEntry entry) {
1044 final Resources resources = mContext.getResources();
1045 ViewCache views = (ViewCache) view.getTag();
1046
1047 // Set the label
1048 TextView label = views.label;
1049 setMaxLines(label, entry.maxLabelLines);
1050 label.setText(entry.label);
1051
1052 // Set the data
1053 TextView data = views.data;
1054 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001055 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001056 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001057 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1058 } else {
1059 data.setText(entry.data);
1060 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001061 setMaxLines(data, entry.maxLines);
1062 }
1063
Evan Millar15e514d2009-08-04 10:14:57 -07001064 // Set the primary icon
1065 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1066
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001067 // Set the action icon
1068 ImageView action = views.actionIcon;
1069 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001070 Drawable actionIcon;
1071 if (entry.resPackageName != null) {
1072 // Load external resources through PackageManager
1073 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1074 entry.actionIcon, null);
1075 } else {
1076 actionIcon = resources.getDrawable(entry.actionIcon);
1077 }
1078 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001079 action.setVisibility(View.VISIBLE);
1080 } else {
1081 // Things should still line up as if there was an icon, so make it invisible
1082 action.setVisibility(View.INVISIBLE);
1083 }
1084
1085 // Set the presence icon
1086 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001087 if (entry.presenceIcon != -1) {
1088 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001089 } else if (entry.status != -1) {
1090 presenceIcon = resources.getDrawable(
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -07001091 StatusUpdates.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001092 }
Evan Millar15e514d2009-08-04 10:14:57 -07001093 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001094 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001095 presenceIconView.setImageDrawable(presenceIcon);
1096 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001097 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001098 presenceIconView.setVisibility(View.GONE);
1099 }
1100
1101 // Set the secondary action button
1102 ImageView secondaryActionView = views.secondaryActionButton;
1103 Drawable secondaryActionIcon = null;
1104 if (entry.secondaryActionIcon != -1) {
1105 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1106 }
1107 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1108 secondaryActionView.setImageDrawable(secondaryActionIcon);
1109 secondaryActionView.setTag(entry.secondaryIntent);
1110 secondaryActionView.setVisibility(View.VISIBLE);
1111 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1112 } else {
1113 secondaryActionView.setVisibility(View.GONE);
1114 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001115 }
1116 }
1117
1118 private void setMaxLines(TextView textView, int maxLines) {
1119 if (maxLines == 1) {
1120 textView.setSingleLine(true);
1121 textView.setEllipsize(TextUtils.TruncateAt.END);
1122 } else {
1123 textView.setSingleLine(false);
1124 textView.setMaxLines(maxLines);
1125 textView.setEllipsize(null);
1126 }
1127 }
1128 }
1129}