blob: ab8e931abfc04c05bea62685ea7f5c30826a558e [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;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080094
Evan Millar8a79cee2009-08-19 17:20:49 -070095 private static final int REQUEST_JOIN_CONTACT = 1;
Evan Millar9cd81242009-09-26 16:02:31 -070096 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -070097
Evan Millar8a79cee2009-08-19 17:20:49 -070098 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080099
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700100 protected Uri mLookupUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800101 private ContentResolver mResolver;
102 private ViewAdapter mAdapter;
103 private int mNumPhoneNumbers = 0;
104
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700105 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700106 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700107 */
Evan Millar7911ff52009-07-21 15:55:18 -0700108 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700109
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800110 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
111 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
112 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
113 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
114 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
115 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700116 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800117 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
118 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
119
120 private Cursor mCursor;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700121
Evan Millar11d628c2009-09-02 08:55:01 -0700122 protected ContactHeaderWidget mContactHeaderWidget;
123 private NotifyingAsyncQueryHandler mHandler;
124
125 protected LayoutInflater mInflater;
126
Megha Joshid5afd8a2009-09-22 09:38:41 -0700127 protected int mReadOnlySourcesCnt;
128 protected int mWritableSourcesCnt;
129 protected ArrayList<Long> mWritableRawContactIds = new ArrayList<Long>();
Evan Millar11d628c2009-09-02 08:55:01 -0700130
131 private static final int TOKEN_QUERY = 0;
132
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800133 private ContentObserver mObserver = new ContentObserver(new Handler()) {
134 @Override
135 public boolean deliverSelfNotifications() {
136 return true;
137 }
138
139 @Override
140 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700141 if (mCursor != null && !mCursor.isClosed()) {
142 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800143 }
144 }
145 };
146
147 public void onClick(DialogInterface dialog, int which) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700148 closeCursor();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700149 if (mReadOnlySourcesCnt > 0) {
150 for (long rawContactIdToDelete: mWritableRawContactIds) {
151 final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
152 rawContactIdToDelete);
153 getContentResolver().delete(rawContactUri, null, null);
154 }
155 } else {
156 getContentResolver().delete(mLookupUri, null, null);
157 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800158 finish();
159 }
160
Evan Millar7911ff52009-07-21 15:55:18 -0700161 private FrameLayout mTabContentLayout;
162 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800163 private boolean mShowSmsLinksForAllPhones;
Evan Millar11d628c2009-09-02 08:55:01 -0700164 private ArrayList<Entity> mEntities = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800165
166 @Override
167 protected void onCreate(Bundle icicle) {
168 super.onCreate(icicle);
169
Evan Millar11d628c2009-09-02 08:55:01 -0700170 final Intent intent = getIntent();
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700171 Uri data = intent.getData();
172 String authority = data.getAuthority();
173 if (ContactsContract.AUTHORITY.equals(authority)) {
174 mLookupUri = data;
175 } else if (android.provider.Contacts.AUTHORITY.equals(authority)) {
176 final long rawContactId = ContentUris.parseId(data);
177 mLookupUri = RawContacts.getContactLookupUri(getContentResolver(),
178 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700179
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700180 }
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700181 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700182
183 requestWindowFeature(Window.FEATURE_NO_TITLE);
184 setContentView(R.layout.contact_card_layout);
185
186 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
187 mContactHeaderWidget.showStar(true);
Evan Millar11d628c2009-09-02 08:55:01 -0700188 mContactHeaderWidget.setExcludeMimes(new String[] {
189 Contacts.CONTENT_ITEM_TYPE
190 });
191
Evan Millar11d628c2009-09-02 08:55:01 -0700192 mHandler = new NotifyingAsyncQueryHandler(this, this);
193
Evan Millar7911ff52009-07-21 15:55:18 -0700194 mListView = new ListView(this);
195 mListView.setOnCreateContextMenuListener(this);
196 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
197 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800198
Evan Millar7911ff52009-07-21 15:55:18 -0700199 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
200 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800201
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800202 mResolver = getContentResolver();
203
204 // Build the list of sections. The order they're added to mSections dictates the
205 // order they are displayed in the list.
206 mSections.add(mPhoneEntries);
207 mSections.add(mSmsEntries);
208 mSections.add(mEmailEntries);
209 mSections.add(mImEntries);
210 mSections.add(mPostalEntries);
211 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700212 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800213 mSections.add(mOtherEntries);
214
215 //TODO Read this value from a preference
216 mShowSmsLinksForAllPhones = true;
Evan Millar11d628c2009-09-02 08:55:01 -0700217 }
218
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800219 @Override
220 protected void onResume() {
221 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700222 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800223 }
224
225 @Override
226 protected void onPause() {
227 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700228 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800229 }
230
231 @Override
232 protected void onDestroy() {
233 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700234 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800235 }
236
237 @Override
238 protected Dialog onCreateDialog(int id) {
239 switch (id) {
240 case DIALOG_CONFIRM_DELETE:
241 return new AlertDialog.Builder(this)
242 .setTitle(R.string.deleteConfirmation_title)
243 .setIcon(android.R.drawable.ic_dialog_alert)
244 .setMessage(R.string.deleteConfirmation)
245 .setNegativeButton(android.R.string.cancel, null)
246 .setPositiveButton(android.R.string.ok, this)
247 .setCancelable(false)
248 .create();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700249 case DIALOG_CONFIRM_READONLY_DELETE:
250 return new AlertDialog.Builder(this)
251 .setTitle(R.string.deleteConfirmation_title)
252 .setIcon(android.R.drawable.ic_dialog_alert)
253 .setMessage(R.string.readOnlyContactDeleteConfirmation)
254 .setNegativeButton(android.R.string.cancel, null)
255 .setPositiveButton(android.R.string.ok, this)
256 .setCancelable(false)
257 .create();
258 case DIALOG_CONFIRM_MULTIPLE_DELETE:
259 return new AlertDialog.Builder(this)
260 .setTitle(R.string.deleteConfirmation_title)
261 .setIcon(android.R.drawable.ic_dialog_alert)
262 .setMessage(R.string.multipleContactDeleteConfirmation)
263 .setNegativeButton(android.R.string.cancel, null)
264 .setPositiveButton(android.R.string.ok, this)
265 .setCancelable(false)
266 .create();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800267 }
268 return null;
269 }
270
Evan Millar11d628c2009-09-02 08:55:01 -0700271 // QUERY CODE //
272 /** {@inheritDoc} */
273 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
274 try{
275 if (token == TOKEN_QUERY) {
Evan Millar11d628c2009-09-02 08:55:01 -0700276 mEntities = readEntities(iterator);
Evan Millar85863112009-09-21 14:35:18 -0700277 bindData();
Evan Millar11d628c2009-09-02 08:55:01 -0700278 }
279 } finally {
280 if (iterator != null) {
281 iterator.close();
282 }
283 }
284 }
285
286 /** {@inheritDoc} */
287 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700288 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700289 }
290
Evan Millar6a61a1a2009-09-29 14:00:43 -0700291 private long getRefreshedContactId() {
292 Uri freshContactUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
293 if (freshContactUri != null) {
294 return ContentUris.parseId(freshContactUri);
295 }
296 return -1;
297 }
298
Evan Millar11d628c2009-09-02 08:55:01 -0700299 private ArrayList<Entity> readEntities(EntityIterator iterator) {
300 ArrayList<Entity> entities = new ArrayList<Entity>();
301 try {
302 while (iterator.hasNext()) {
303 entities.add(iterator.next());
304 }
305 } catch (RemoteException e) {
306 }
307
308 return entities;
309 }
310
311 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700312 closeCursor();
313
Evan Millar6a61a1a2009-09-29 14:00:43 -0700314 Uri uri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700315 if (mLookupUri != null) {
316 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
317 if (mLookupUri != null) {
Evan Millar6a61a1a2009-09-29 14:00:43 -0700318 uri = Contacts.lookupContact(getContentResolver(), mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700319 }
320 }
321
Evan Millar6a61a1a2009-09-29 14:00:43 -0700322 if (uri == null) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700323
324 // TODO either figure out a way to prevent a flash of black background or
325 // use some other UI than a toast
326 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
327 Log.e(TAG, "invalid contact uri: " + mLookupUri);
328 finish();
329 return;
330 }
331
Evan Millar6a61a1a2009-09-29 14:00:43 -0700332 mCursor = mResolver.query(Uri.withAppendedPath(uri, Contacts.Data.CONTENT_DIRECTORY),
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700333 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
334 mCursor.registerContentObserver(mObserver);
335
Evan Millar6a61a1a2009-09-29 14:00:43 -0700336 long contactId = ContentUris.parseId(uri);
Evan Millar11d628c2009-09-02 08:55:01 -0700337 mHandler.startQueryEntities(TOKEN_QUERY, null,
338 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700339
Evan Millar9b1a1242009-09-16 10:26:10 -0700340 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700341 }
342
343 private void closeCursor() {
344 if (mCursor != null) {
345 mCursor.unregisterContentObserver(mObserver);
346 mCursor.close();
347 mCursor = null;
348 }
Evan Millar11d628c2009-09-02 08:55:01 -0700349 }
350
351 private void bindData() {
352
353 // Build up the contact entries
354 buildEntries();
355
356 // Collapse similar data items in select sections.
357 Collapser.collapseList(mPhoneEntries);
358 Collapser.collapseList(mSmsEntries);
359 Collapser.collapseList(mEmailEntries);
360 Collapser.collapseList(mPostalEntries);
361
362 if (mAdapter == null) {
363 mAdapter = new ViewAdapter(this, mSections);
364 mListView.setAdapter(mAdapter);
365 } else {
366 mAdapter.setSections(mSections, SHOW_SEPARATORS);
367 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800368 }
369
370 @Override
371 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700372 super.onCreateOptionsMenu(menu);
373
374 final MenuInflater inflater = getMenuInflater();
375 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800376 return true;
377 }
378
379 @Override
380 public boolean onPrepareOptionsMenu(Menu menu) {
381 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700382
Evan Millar51514db2009-09-22 11:42:35 -0700383 // Only allow edit when we have at least one raw_contact id
384 final boolean hasRawContact = (mRawContactIds.size() > 0);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700385 menu.findItem(R.id.menu_edit).setEnabled(hasRawContact);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700386
Megha Joshid5afd8a2009-09-22 09:38:41 -0700387 // Disable delete for readonly contact
388 if (mWritableSourcesCnt == 0) {
389 menu.findItem(R.id.menu_delete).setEnabled(false);
390 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800391 return true;
392 }
393
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800394 @Override
395 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
396 AdapterView.AdapterContextMenuInfo info;
397 try {
398 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
399 } catch (ClassCastException e) {
400 Log.e(TAG, "bad menuInfo", e);
401 return;
402 }
403
404 // This can be null sometimes, don't crash...
405 if (info == null) {
406 Log.e(TAG, "bad menuInfo");
407 return;
408 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700409
Evan Millar45e0ed32009-06-01 16:44:38 -0700410 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
Neel Parekha8fb68a2009-09-25 18:05:18 -0700411 menu.setHeaderTitle(R.string.contactOptionsTitle);
Evan Millar45e0ed32009-06-01 16:44:38 -0700412 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
413 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700414 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
415 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700416 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800417 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700418 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
419 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700420 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700421 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800422 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700423 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700424 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
425 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800426 }
427
428 @Override
429 public boolean onOptionsItemSelected(MenuItem item) {
430 switch (item.getItemId()) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700431 case R.id.menu_edit: {
Evan Millardf181202009-09-30 15:15:51 -0700432 Long rawContactIdToEdit = null;
433 if (mRawContactIds.size() > 0) {
434 rawContactIdToEdit = mRawContactIds.get(0);
435 } else {
436 // There is no rawContact to edit.
437 break;
Evan Millardb5d88c2009-08-28 09:31:57 -0700438 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700439 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
440 rawContactIdToEdit);
Evan Millar9cd81242009-09-26 16:02:31 -0700441 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
442 REQUEST_EDIT_CONTACT);
Evan Millar8a79cee2009-08-19 17:20:49 -0700443 break;
444 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700445 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800446 // Get confirmation
Megha Joshid5afd8a2009-09-22 09:38:41 -0700447 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
448 showDialog(DIALOG_CONFIRM_READONLY_DELETE);
449 } else if (mWritableSourcesCnt > 1) {
450 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);
652 } else if (mWritableSourcesCnt > 1) {
653 showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
654 } else {
655 showDialog(DIALOG_CONFIRM_DELETE);
656 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800657 return true;
658 }
659 }
660
661 return super.onKeyDown(keyCode, event);
662 }
663
Evan Millar7911ff52009-07-21 15:55:18 -0700664 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800665 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
666 if (entry != null) {
667 Intent intent = entry.intent;
Evan Millardf181202009-09-30 15:15:51 -0700668 if (intent != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800669 try {
670 startActivity(intent);
671 } catch (ActivityNotFoundException e) {
672 Log.e(TAG, "No activity found for intent: " + intent);
673 signalError();
674 }
675 } else {
676 signalError();
677 }
678 } else {
679 signalError();
680 }
681 }
682
683 /**
684 * Signal an error to the user via a beep, or some other method.
685 */
686 private void signalError() {
687 //TODO: implement this when we have the sonification APIs
688 }
689
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800690 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700691 // 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 -0800692 StringBuilder buf = new StringBuilder("imto://");
693 buf.append(host);
694 buf.append('/');
695 buf.append(data);
696 return Uri.parse(buf.toString());
697 }
698
699 /**
700 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700701 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800702 * @param personCursor the URI for the contact being displayed
703 */
Evan Millar11d628c2009-09-02 08:55:01 -0700704 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800705 // Clear out the old entries
706 final int numSections = mSections.size();
707 for (int i = 0; i < numSections; i++) {
708 mSections.get(i).clear();
709 }
710
Evan Millar7911ff52009-07-21 15:55:18 -0700711 mRawContactIds.clear();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700712 mReadOnlySourcesCnt = 0;
713 mWritableSourcesCnt = 0;
714 mWritableRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700715
Evan Millar11d628c2009-09-02 08:55:01 -0700716 Sources sources = Sources.getInstance(this);
717
Evan Millar66388be2009-05-28 15:41:07 -0700718 // Build up method entries
Evan Millar6a61a1a2009-09-29 14:00:43 -0700719 if (mLookupUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700720 for (Entity entity: mEntities) {
721 final ContentValues entValues = entity.getEntityValues();
722 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700723 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700724
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700725 if (!mRawContactIds.contains(rawContactId)) {
726 mRawContactIds.add(rawContactId);
727 }
728
Evan Millar11d628c2009-09-02 08:55:01 -0700729 for (NamedContentValues subValue : entity.getSubValues()) {
730 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800731
Jeff Sharkeyab066932009-09-21 09:55:30 -0700732 final ContentValues entryValues = subValue.values;
733 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
734
Evan Millar11d628c2009-09-02 08:55:01 -0700735 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700736 if (mimetype == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -0700737
Jeff Sharkeyab066932009-09-21 09:55:30 -0700738 final DataKind kind = sources.getKindOrFallback(accountType, mimetype, this,
Evan Millar11d628c2009-09-02 08:55:01 -0700739 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700740 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800741
Evan Millar11d628c2009-09-02 08:55:01 -0700742 final long id = entryValues.getAsLong(Data._ID);
743 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700744 entry.contactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700745 entry.id = id;
746 entry.uri = uri;
747 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -0700748 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -0700749 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700750 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700751 entry.type = entryValues.getAsInteger(kind.typeColumn);
752 }
753 if (kind.iconRes > 0) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700754 entry.resPackageName = kind.resPackageName;
Evan Millar11d628c2009-09-02 08:55:01 -0700755 entry.actionIcon = kind.iconRes;
756 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800757
Evan Millar11d628c2009-09-02 08:55:01 -0700758 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700759 if (TextUtils.isEmpty(entry.data)) {
Alex Kennberg87fc3172009-03-28 06:43:06 -0700760 continue;
761 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700762
Evan Millarff04a272009-09-26 15:23:37 -0700763 final boolean isSuperPrimary = entryValues.getAsInteger(
764 Data.IS_SUPER_PRIMARY) != 0;
Evan Millar11d628c2009-09-02 08:55:01 -0700765
Evan Millarff04a272009-09-26 15:23:37 -0700766 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
767 // Build phone entries
768 mNumPhoneNumbers++;
Evan Millar11d628c2009-09-02 08:55:01 -0700769
Evan Millarff04a272009-09-26 15:23:37 -0700770 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
771 Uri.fromParts("tel", entry.data, null));
772 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
773 Uri.fromParts("sms", entry.data, null));
774 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700775
Evan Millarff04a272009-09-26 15:23:37 -0700776 entry.isPrimary = isSuperPrimary;
777 mPhoneEntries.add(entry);
778
779 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
780 || mShowSmsLinksForAllPhones) {
781 // Add an SMS entry
782 if (kind.iconAltRes > 0) {
783 entry.secondaryActionIcon = kind.iconAltRes;
Evan Millar49714ee2009-09-02 16:42:47 -0700784 }
Evan Millarff04a272009-09-26 15:23:37 -0700785 }
786 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
787 // Build email entries
788 entry.intent = new Intent(Intent.ACTION_SENDTO,
789 Uri.fromParts("mailto", entry.data, null));
790 entry.isPrimary = isSuperPrimary;
791 mEmailEntries.add(entry);
792 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
793 equals(mimetype)) {
794 // Build postal entries
795 entry.maxLines = 4;
796 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
797 mPostalEntries.add(entry);
798 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
799 // Build im entries
800 Object protocolObj = entryValues.getAsInteger(CommonDataKinds.Im.PROTOCOL);
801 String host = null;
Evan Millar49714ee2009-09-02 16:42:47 -0700802
Evan Millarff04a272009-09-26 15:23:37 -0700803 if (TextUtils.isEmpty(entry.label)) {
804 entry.label = getString(R.string.chat).toLowerCase();
805 }
Evan Millar11d628c2009-09-02 08:55:01 -0700806
Evan Millarff04a272009-09-26 15:23:37 -0700807 if (protocolObj instanceof Number) {
808 int protocol = ((Number) protocolObj).intValue();
809 host = ContactsUtils.lookupProviderNameFromId(protocol);
810 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
811 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
812 entry.maxLabelLines = 2;
Evan Millar11d628c2009-09-02 08:55:01 -0700813 }
Evan Millarff04a272009-09-26 15:23:37 -0700814 } else if (protocolObj != null) {
815 String providerName = (String) protocolObj;
816 host = providerName.toLowerCase();
817 }
818
819 // Only add the intent if there is a valid host
820 // host is null for CommonDataKinds.Im.PROTOCOL_CUSTOM
821 if (!TextUtils.isEmpty(host)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700822 entry.intent = new Intent(Intent.ACTION_SENDTO,
Evan Millarff04a272009-09-26 15:23:37 -0700823 constructImToUrl(host.toLowerCase(), entry.data));
824 }
825 //TODO(emillar) Add in presence info
Evan Millar11d628c2009-09-02 08:55:01 -0700826 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
827 entry.presenceIcon = Presence.getPresenceIconResourceId(
828 aggCursor.getInt(METHODS_STATUS_COLUMN));
829 entry.status = ...
830 }*/
Evan Millarff04a272009-09-26 15:23:37 -0700831 mImEntries.add(entry);
832 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)
833 || CommonDataKinds.Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) {
834 // Build organization and note entries
835 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -0700836 mOrganizationEntries.add(entry);
837 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
838 // Build note entries
Evan Millar11d628c2009-09-02 08:55:01 -0700839 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -0700840 entry.maxLines = 10;
841 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700842 } else {
843 // Handle showing custom
Evan Millarff04a272009-09-26 15:23:37 -0700844 entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700845 mOtherEntries.add(entry);
Evan Millar11d628c2009-09-02 08:55:01 -0700846 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700847 }
Evan Millar66388be2009-05-28 15:41:07 -0700848 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800849 }
850 }
851
Evan Millar11d628c2009-09-02 08:55:01 -0700852 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
853 if (kind.actionHeader == null) {
854 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700855 }
Evan Millar11d628c2009-09-02 08:55:01 -0700856 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
857 if (actionHeader == null) {
858 return null;
859 }
860 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
861 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700862
Evan Millar11d628c2009-09-02 08:55:01 -0700863 String buildDataString(DataKind kind, ContentValues values) {
864 if (kind.actionBody == null) {
865 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800866 }
Evan Millar11d628c2009-09-02 08:55:01 -0700867 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
868 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800869 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700870
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800871 /**
872 * A basic structure with the data for a contact entry in the list.
873 */
Evan Millaradb0f8c2009-09-28 17:20:50 -0700874 class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700875 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800876 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -0700877 public boolean isPrimary = false;
878 public int presenceIcon = -1;
879 public int secondaryActionIcon = -1;
880 public Intent intent;
881 public Intent secondaryIntent = null;
882 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800883 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700884 public ArrayList<Long> ids = new ArrayList<Long>();
885 public int collapseCount = 0;
886
887 public boolean collapseWith(ViewEntry entry) {
888 // assert equal collapse keys
Evan Millaradb0f8c2009-09-28 17:20:50 -0700889 if (!shouldCollapseWith(entry)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -0700890 return false;
891 }
892
893 // Choose the label associated with the highest type precedence.
894 if (TypePrecedence.getTypePrecedence(mimetype, type)
895 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
896 type = entry.type;
897 label = entry.label;
898 }
899
900 // Choose the max of the maxLines and maxLabelLines values.
901 maxLines = Math.max(maxLines, entry.maxLines);
902 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
903
904 // Choose the presence with the highest precedence.
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -0700905 if (StatusUpdates.getPresencePrecedence(status)
906 < StatusUpdates.getPresencePrecedence(entry.status)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -0700907 status = entry.status;
908 }
909
910 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -0700911 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700912
913 // uri, and contactdId, shouldn't make a difference. Just keep the original.
914
915 // Keep track of all the ids that have been collapsed with this one.
916 ids.add(entry.id);
917 collapseCount++;
918 return true;
919 }
920
Evan Millaradb0f8c2009-09-28 17:20:50 -0700921 public boolean shouldCollapseWith(ViewEntry entry) {
922 if (entry == null) {
923 return false;
924 }
925
926 if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)
927 && Phone.CONTENT_ITEM_TYPE.equals(entry.mimetype)) {
928 if (!PhoneNumberUtils.compare(ViewContactActivity.this, data, entry.data)) {
929 return false;
930 }
931 } else {
932 if (!equals(data, entry.data)) {
933 return false;
934 }
935 }
936
937 if (!equals(mimetype, entry.mimetype)
938 || !intentCollapsible(intent, entry.intent)
939 || !intentCollapsible(secondaryIntent, entry.secondaryIntent)
940 || actionIcon != entry.actionIcon) {
941 return false;
942 }
943
944 return true;
945 }
946
947 private boolean equals(Object a, Object b) {
948 return a==b || (a != null && a.equals(b));
949 }
950
951 private boolean intentCollapsible(Intent a, Intent b) {
952 if (a == b) {
953 return true;
954 } else if ((a != null && b != null) && equals(a.getAction(), b.getAction())) {
955 return true;
956 }
957 return false;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700958 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800959 }
960
Evan Millar15e514d2009-08-04 10:14:57 -0700961 /** Cache of the children views of a row */
962 static class ViewCache {
963 public TextView label;
964 public TextView data;
965 public ImageView actionIcon;
966 public ImageView presenceIcon;
967 public ImageView primaryIcon;
968 public ImageView secondaryActionButton;
969 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700970
Evan Millar15e514d2009-08-04 10:14:57 -0700971 // Need to keep track of this too
972 ViewEntry entry;
973 }
974
975 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
976 implements View.OnClickListener {
977
Evan Millar5c22c3b2009-05-29 11:37:54 -0700978
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800979 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
980 super(context, sections, SHOW_SEPARATORS);
981 }
982
Evan Millar15e514d2009-08-04 10:14:57 -0700983 public void onClick(View v) {
984 Intent intent = (Intent) v.getTag();
985 startActivity(intent);
986 }
987
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800988 @Override
989 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -0700990 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800991 View v;
992
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800993 ViewCache views;
994
995 // Check to see if we can reuse convertView
996 if (convertView != null) {
997 v = convertView;
998 views = (ViewCache) v.getTag();
999 } else {
1000 // Create a new view if needed
1001 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1002
1003 // Cache the children
1004 views = new ViewCache();
1005 views.label = (TextView) v.findViewById(android.R.id.text1);
1006 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001007 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1008 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1009 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1010 views.secondaryActionButton = (ImageView) v.findViewById(
1011 R.id.secondary_action_button);
1012 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001013 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001014 v.setTag(views);
1015 }
1016
1017 // Update the entry in the view cache
1018 views.entry = entry;
1019
1020 // Bind the data to the view
1021 bindView(v, entry);
1022 return v;
1023 }
1024
1025 @Override
1026 protected View newView(int position, ViewGroup parent) {
1027 // getView() handles this
1028 throw new UnsupportedOperationException();
1029 }
1030
1031 @Override
1032 protected void bindView(View view, ViewEntry entry) {
1033 final Resources resources = mContext.getResources();
1034 ViewCache views = (ViewCache) view.getTag();
1035
1036 // Set the label
1037 TextView label = views.label;
1038 setMaxLines(label, entry.maxLabelLines);
1039 label.setText(entry.label);
1040
1041 // Set the data
1042 TextView data = views.data;
1043 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001044 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001045 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001046 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1047 } else {
1048 data.setText(entry.data);
1049 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001050 setMaxLines(data, entry.maxLines);
1051 }
1052
Evan Millar15e514d2009-08-04 10:14:57 -07001053 // Set the primary icon
1054 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1055
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001056 // Set the action icon
1057 ImageView action = views.actionIcon;
1058 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001059 Drawable actionIcon;
1060 if (entry.resPackageName != null) {
1061 // Load external resources through PackageManager
1062 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1063 entry.actionIcon, null);
1064 } else {
1065 actionIcon = resources.getDrawable(entry.actionIcon);
1066 }
1067 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001068 action.setVisibility(View.VISIBLE);
1069 } else {
1070 // Things should still line up as if there was an icon, so make it invisible
1071 action.setVisibility(View.INVISIBLE);
1072 }
1073
1074 // Set the presence icon
1075 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001076 if (entry.presenceIcon != -1) {
1077 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001078 } else if (entry.status != -1) {
1079 presenceIcon = resources.getDrawable(
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -07001080 StatusUpdates.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001081 }
Evan Millar15e514d2009-08-04 10:14:57 -07001082 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001083 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001084 presenceIconView.setImageDrawable(presenceIcon);
1085 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001086 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001087 presenceIconView.setVisibility(View.GONE);
1088 }
1089
1090 // Set the secondary action button
1091 ImageView secondaryActionView = views.secondaryActionButton;
1092 Drawable secondaryActionIcon = null;
1093 if (entry.secondaryActionIcon != -1) {
1094 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1095 }
1096 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1097 secondaryActionView.setImageDrawable(secondaryActionIcon);
1098 secondaryActionView.setTag(entry.secondaryIntent);
1099 secondaryActionView.setVisibility(View.VISIBLE);
1100 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1101 } else {
1102 secondaryActionView.setVisibility(View.GONE);
1103 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001104 }
1105 }
1106
1107 private void setMaxLines(TextView textView, int maxLines) {
1108 if (maxLines == 1) {
1109 textView.setSingleLine(true);
1110 textView.setEllipsize(TextUtils.TruncateAt.END);
1111 } else {
1112 textView.setSingleLine(false);
1113 textView.setMaxLines(maxLines);
1114 textView.setEllipsize(null);
1115 }
1116 }
1117 }
1118}