blob: 830dd73ee237681242bc76877db65708cd2fe14b [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.ScrollingTabWidget.OnTabSelectionChangedListener;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070021import com.android.contacts.SplitAggregateView.OnContactSelectedListener;
Evan Millar11d628c2009-09-02 08:55:01 -070022import com.android.contacts.model.ContactsSource;
23import com.android.contacts.model.Sources;
24import com.android.contacts.model.ContactsSource.DataKind;
Evan Millardf181202009-09-30 15:15:51 -070025import com.android.contacts.ui.EditContactActivity;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070026import com.android.contacts.util.Constants;
Evan Millar11d628c2009-09-02 08:55:01 -070027import com.android.contacts.util.NotifyingAsyncQueryHandler;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080028import com.android.internal.telephony.ITelephony;
Evan Millar11d628c2009-09-02 08:55:01 -070029import com.android.internal.widget.ContactHeaderWidget;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080030
Evan Millar11d628c2009-09-02 08:55:01 -070031import android.app.Activity;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080032import android.app.AlertDialog;
33import android.app.Dialog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080034import android.content.ActivityNotFoundException;
35import android.content.ContentResolver;
36import android.content.ContentUris;
37import android.content.ContentValues;
38import android.content.Context;
39import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070040import android.content.Entity;
Evan Millar11d628c2009-09-02 08:55:01 -070041import android.content.EntityIterator;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080042import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070043import android.content.DialogInterface.OnClickListener;
Evan Millar11d628c2009-09-02 08:55:01 -070044import android.content.Entity.NamedContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080045import android.content.res.Resources;
46import android.database.ContentObserver;
47import android.database.Cursor;
48import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080049import android.net.Uri;
50import android.os.Bundle;
51import android.os.Handler;
52import android.os.RemoteException;
53import android.os.ServiceManager;
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -070054import android.provider.ContactsContract;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070055import android.provider.ContactsContract.AggregationExceptions;
56import android.provider.ContactsContract.CommonDataKinds;
Evan Millar11d628c2009-09-02 08:55:01 -070057import android.provider.ContactsContract.Contacts;
Evan Millar66388be2009-05-28 15:41:07 -070058import android.provider.ContactsContract.Data;
Evan Millar54a5c9f2009-06-23 17:41:09 -070059import android.provider.ContactsContract.Presence;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070060import android.provider.ContactsContract.RawContacts;
Evan Millar54a5c9f2009-06-23 17:41:09 -070061import android.provider.ContactsContract.CommonDataKinds.Phone;
62import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080063import android.text.TextUtils;
64import android.util.Log;
Evan Millar11d628c2009-09-02 08:55:01 -070065import android.util.SparseArray;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080066import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070067import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080068import android.view.KeyEvent;
Evan Millar11d628c2009-09-02 08:55:01 -070069import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080070import android.view.Menu;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070071import android.view.MenuInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080072import android.view.MenuItem;
73import android.view.View;
74import android.view.ViewGroup;
Evan Millar11d628c2009-09-02 08:55:01 -070075import android.view.Window;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080076import android.view.ContextMenu.ContextMenuInfo;
Evan Millar2e1b8af2009-09-24 09:39:21 -070077import android.view.animation.Animation;
Evan Millar2e1b8af2009-09-24 09:39:21 -070078import android.view.animation.TranslateAnimation;
79import android.view.animation.Animation.AnimationListener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080080import android.widget.AdapterView;
Evan Millar7911ff52009-07-21 15:55:18 -070081import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080082import android.widget.ImageView;
83import android.widget.ListView;
84import android.widget.TextView;
85import android.widget.Toast;
86
87import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080088
89/**
90 * Displays the details of a specific contact.
91 */
Evan Millar11d628c2009-09-02 08:55:01 -070092public class ViewContactActivity extends Activity
Evan Millar7911ff52009-07-21 15:55:18 -070093 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
Evan Millardf181202009-09-30 15:15:51 -070094 AdapterView.OnItemClickListener, NotifyingAsyncQueryHandler.AsyncQueryListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080095 private static final String TAG = "ViewContact";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080096
97 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080098
99 private static final int DIALOG_CONFIRM_DELETE = 1;
Megha Joshid5afd8a2009-09-22 09:38:41 -0700100 private static final int DIALOG_CONFIRM_READONLY_DELETE = 2;
101 private static final int DIALOG_CONFIRM_MULTIPLE_DELETE = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800102
Evan Millar8a79cee2009-08-19 17:20:49 -0700103 private static final int REQUEST_JOIN_CONTACT = 1;
Evan Millar9cd81242009-09-26 16:02:31 -0700104 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700105
Evan Millar8a79cee2009-08-19 17:20:49 -0700106 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800107
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700108 protected Uri mLookupUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800109 private ContentResolver mResolver;
110 private ViewAdapter mAdapter;
111 private int mNumPhoneNumbers = 0;
112
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700113 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700114 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700115 */
Evan Millar7911ff52009-07-21 15:55:18 -0700116 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700117
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800118 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
119 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
120 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
121 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
122 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
123 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700124 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800125 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
126 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
127
128 private Cursor mCursor;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700129
Evan Millar11d628c2009-09-02 08:55:01 -0700130 protected ContactHeaderWidget mContactHeaderWidget;
131 private NotifyingAsyncQueryHandler mHandler;
132
133 protected LayoutInflater mInflater;
134
Megha Joshid5afd8a2009-09-22 09:38:41 -0700135 protected int mReadOnlySourcesCnt;
136 protected int mWritableSourcesCnt;
137 protected ArrayList<Long> mWritableRawContactIds = new ArrayList<Long>();
Evan Millar11d628c2009-09-02 08:55:01 -0700138
139 private static final int TOKEN_QUERY = 0;
140
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800141 private ContentObserver mObserver = new ContentObserver(new Handler()) {
142 @Override
143 public boolean deliverSelfNotifications() {
144 return true;
145 }
146
147 @Override
148 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700149 if (mCursor != null && !mCursor.isClosed()) {
150 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800151 }
152 }
153 };
154
155 public void onClick(DialogInterface dialog, int which) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700156 closeCursor();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700157 if (mReadOnlySourcesCnt > 0) {
158 for (long rawContactIdToDelete: mWritableRawContactIds) {
159 final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
160 rawContactIdToDelete);
161 getContentResolver().delete(rawContactUri, null, null);
162 }
163 } else {
164 getContentResolver().delete(mLookupUri, null, null);
165 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800166 finish();
167 }
168
Evan Millar7911ff52009-07-21 15:55:18 -0700169 private FrameLayout mTabContentLayout;
170 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800171 private boolean mShowSmsLinksForAllPhones;
Evan Millar11d628c2009-09-02 08:55:01 -0700172 private ArrayList<Entity> mEntities = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800173
174 @Override
175 protected void onCreate(Bundle icicle) {
176 super.onCreate(icicle);
177
Evan Millar11d628c2009-09-02 08:55:01 -0700178 final Intent intent = getIntent();
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700179 Uri data = intent.getData();
180 String authority = data.getAuthority();
181 if (ContactsContract.AUTHORITY.equals(authority)) {
182 mLookupUri = data;
183 } else if (android.provider.Contacts.AUTHORITY.equals(authority)) {
184 final long rawContactId = ContentUris.parseId(data);
185 mLookupUri = RawContacts.getContactLookupUri(getContentResolver(),
186 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700187
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700188 }
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700189 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700190
191 requestWindowFeature(Window.FEATURE_NO_TITLE);
192 setContentView(R.layout.contact_card_layout);
193
194 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
195 mContactHeaderWidget.showStar(true);
Evan Millar11d628c2009-09-02 08:55:01 -0700196 mContactHeaderWidget.setExcludeMimes(new String[] {
197 Contacts.CONTENT_ITEM_TYPE
198 });
199
Evan Millar11d628c2009-09-02 08:55:01 -0700200 mHandler = new NotifyingAsyncQueryHandler(this, this);
201
Evan Millar7911ff52009-07-21 15:55:18 -0700202 mListView = new ListView(this);
203 mListView.setOnCreateContextMenuListener(this);
204 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
205 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800206
Evan Millar7911ff52009-07-21 15:55:18 -0700207 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
208 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800209
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800210 mResolver = getContentResolver();
211
212 // Build the list of sections. The order they're added to mSections dictates the
213 // order they are displayed in the list.
214 mSections.add(mPhoneEntries);
215 mSections.add(mSmsEntries);
216 mSections.add(mEmailEntries);
217 mSections.add(mImEntries);
218 mSections.add(mPostalEntries);
219 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700220 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800221 mSections.add(mOtherEntries);
222
223 //TODO Read this value from a preference
224 mShowSmsLinksForAllPhones = true;
Evan Millar11d628c2009-09-02 08:55:01 -0700225 }
226
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800227 @Override
228 protected void onResume() {
229 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700230 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800231 }
232
233 @Override
234 protected void onPause() {
235 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700236 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800237 }
238
239 @Override
240 protected void onDestroy() {
241 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700242 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800243 }
244
245 @Override
246 protected Dialog onCreateDialog(int id) {
247 switch (id) {
248 case DIALOG_CONFIRM_DELETE:
249 return new AlertDialog.Builder(this)
250 .setTitle(R.string.deleteConfirmation_title)
251 .setIcon(android.R.drawable.ic_dialog_alert)
252 .setMessage(R.string.deleteConfirmation)
253 .setNegativeButton(android.R.string.cancel, null)
254 .setPositiveButton(android.R.string.ok, this)
255 .setCancelable(false)
256 .create();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700257 case DIALOG_CONFIRM_READONLY_DELETE:
258 return new AlertDialog.Builder(this)
259 .setTitle(R.string.deleteConfirmation_title)
260 .setIcon(android.R.drawable.ic_dialog_alert)
261 .setMessage(R.string.readOnlyContactDeleteConfirmation)
262 .setNegativeButton(android.R.string.cancel, null)
263 .setPositiveButton(android.R.string.ok, this)
264 .setCancelable(false)
265 .create();
266 case DIALOG_CONFIRM_MULTIPLE_DELETE:
267 return new AlertDialog.Builder(this)
268 .setTitle(R.string.deleteConfirmation_title)
269 .setIcon(android.R.drawable.ic_dialog_alert)
270 .setMessage(R.string.multipleContactDeleteConfirmation)
271 .setNegativeButton(android.R.string.cancel, null)
272 .setPositiveButton(android.R.string.ok, this)
273 .setCancelable(false)
274 .create();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800275 }
276 return null;
277 }
278
Evan Millar11d628c2009-09-02 08:55:01 -0700279 // QUERY CODE //
280 /** {@inheritDoc} */
281 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
282 try{
283 if (token == TOKEN_QUERY) {
Evan Millar11d628c2009-09-02 08:55:01 -0700284 mEntities = readEntities(iterator);
Evan Millar85863112009-09-21 14:35:18 -0700285 bindData();
Evan Millar11d628c2009-09-02 08:55:01 -0700286 }
287 } finally {
288 if (iterator != null) {
289 iterator.close();
290 }
291 }
292 }
293
294 /** {@inheritDoc} */
295 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700296 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700297 }
298
Evan Millar6a61a1a2009-09-29 14:00:43 -0700299 private long getRefreshedContactId() {
300 Uri freshContactUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
301 if (freshContactUri != null) {
302 return ContentUris.parseId(freshContactUri);
303 }
304 return -1;
305 }
306
Evan Millar11d628c2009-09-02 08:55:01 -0700307 private ArrayList<Entity> readEntities(EntityIterator iterator) {
308 ArrayList<Entity> entities = new ArrayList<Entity>();
309 try {
310 while (iterator.hasNext()) {
311 entities.add(iterator.next());
312 }
313 } catch (RemoteException e) {
314 }
315
316 return entities;
317 }
318
319 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700320 closeCursor();
321
Evan Millar6a61a1a2009-09-29 14:00:43 -0700322 Uri uri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700323 if (mLookupUri != null) {
324 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
325 if (mLookupUri != null) {
Evan Millar6a61a1a2009-09-29 14:00:43 -0700326 uri = Contacts.lookupContact(getContentResolver(), mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700327 }
328 }
329
Evan Millar6a61a1a2009-09-29 14:00:43 -0700330 if (uri == null) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700331
332 // TODO either figure out a way to prevent a flash of black background or
333 // use some other UI than a toast
334 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
335 Log.e(TAG, "invalid contact uri: " + mLookupUri);
336 finish();
337 return;
338 }
339
Evan Millar6a61a1a2009-09-29 14:00:43 -0700340 mCursor = mResolver.query(Uri.withAppendedPath(uri, Contacts.Data.CONTENT_DIRECTORY),
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700341 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
342 mCursor.registerContentObserver(mObserver);
343
Evan Millar6a61a1a2009-09-29 14:00:43 -0700344 long contactId = ContentUris.parseId(uri);
Evan Millar11d628c2009-09-02 08:55:01 -0700345 mHandler.startQueryEntities(TOKEN_QUERY, null,
346 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700347
Evan Millar9b1a1242009-09-16 10:26:10 -0700348 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700349 }
350
351 private void closeCursor() {
352 if (mCursor != null) {
353 mCursor.unregisterContentObserver(mObserver);
354 mCursor.close();
355 mCursor = null;
356 }
Evan Millar11d628c2009-09-02 08:55:01 -0700357 }
358
359 private void bindData() {
360
361 // Build up the contact entries
362 buildEntries();
363
364 // Collapse similar data items in select sections.
365 Collapser.collapseList(mPhoneEntries);
366 Collapser.collapseList(mSmsEntries);
367 Collapser.collapseList(mEmailEntries);
368 Collapser.collapseList(mPostalEntries);
369
370 if (mAdapter == null) {
371 mAdapter = new ViewAdapter(this, mSections);
372 mListView.setAdapter(mAdapter);
373 } else {
374 mAdapter.setSections(mSections, SHOW_SEPARATORS);
375 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800376 }
377
378 @Override
379 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700380 super.onCreateOptionsMenu(menu);
381
382 final MenuInflater inflater = getMenuInflater();
383 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800384 return true;
385 }
386
387 @Override
388 public boolean onPrepareOptionsMenu(Menu menu) {
389 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700390
Evan Millar51514db2009-09-22 11:42:35 -0700391 // Only allow edit when we have at least one raw_contact id
392 final boolean hasRawContact = (mRawContactIds.size() > 0);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700393 menu.findItem(R.id.menu_edit).setEnabled(hasRawContact);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700394
Megha Joshid5afd8a2009-09-22 09:38:41 -0700395 // Disable delete for readonly contact
396 if (mWritableSourcesCnt == 0) {
397 menu.findItem(R.id.menu_delete).setEnabled(false);
398 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800399 return true;
400 }
401
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800402 @Override
403 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
404 AdapterView.AdapterContextMenuInfo info;
405 try {
406 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
407 } catch (ClassCastException e) {
408 Log.e(TAG, "bad menuInfo", e);
409 return;
410 }
411
412 // This can be null sometimes, don't crash...
413 if (info == null) {
414 Log.e(TAG, "bad menuInfo");
415 return;
416 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700417
Evan Millar45e0ed32009-06-01 16:44:38 -0700418 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
Neel Parekha8fb68a2009-09-25 18:05:18 -0700419 menu.setHeaderTitle(R.string.contactOptionsTitle);
Evan Millar45e0ed32009-06-01 16:44:38 -0700420 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
421 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700422 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
423 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700424 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800425 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700426 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
427 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700428 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700429 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800430 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700431 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700432 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
433 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800434 }
435
436 @Override
437 public boolean onOptionsItemSelected(MenuItem item) {
438 switch (item.getItemId()) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700439 case R.id.menu_edit: {
Evan Millardf181202009-09-30 15:15:51 -0700440 Long rawContactIdToEdit = null;
441 if (mRawContactIds.size() > 0) {
442 rawContactIdToEdit = mRawContactIds.get(0);
443 } else {
444 // There is no rawContact to edit.
445 break;
Evan Millardb5d88c2009-08-28 09:31:57 -0700446 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700447 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
448 rawContactIdToEdit);
Evan Millar9cd81242009-09-26 16:02:31 -0700449 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
450 REQUEST_EDIT_CONTACT);
Evan Millar8a79cee2009-08-19 17:20:49 -0700451 break;
452 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700453 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800454 // Get confirmation
Megha Joshid5afd8a2009-09-22 09:38:41 -0700455 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
456 showDialog(DIALOG_CONFIRM_READONLY_DELETE);
457 } else if (mWritableSourcesCnt > 1) {
458 showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
459 } else {
460 showDialog(DIALOG_CONFIRM_DELETE);
461 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800462 return true;
463 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700464 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700465 showJoinAggregateActivity();
466 return true;
467 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700468 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700469 showOptionsActivity();
470 return true;
471 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700472 case R.id.menu_share: {
Jeff Sharkey93049312009-09-27 17:28:13 -0700473 // TODO: Keep around actual LOOKUP_KEY, or formalize method of extracting
474 final String lookupKey = mLookupUri.getPathSegments().get(2);
475 final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
476
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700477 final Intent intent = new Intent(Intent.ACTION_SEND);
Jeff Sharkey93049312009-09-27 17:28:13 -0700478 intent.setType(Contacts.CONTENT_VCARD_TYPE);
479 intent.putExtra(Intent.EXTRA_STREAM, shareUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700480
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700481 // Launch chooser to share contact via
482 final CharSequence chooseTitle = getText(R.string.share_via);
483 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700484
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700485 try {
486 startActivity(chooseIntent);
487 } catch (ActivityNotFoundException ex) {
488 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800489 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700490 return true;
491 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800492 }
493 return super.onOptionsItemSelected(item);
494 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700495
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800496 @Override
497 public boolean onContextItemSelected(MenuItem item) {
498 switch (item.getItemId()) {
499 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700500 if (makeItemDefault(item)) {
501 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800502 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700503 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800504 }
505 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700506
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800507 return super.onContextItemSelected(item);
508 }
509
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700510 private boolean makeItemDefault(MenuItem item) {
511 ViewEntry entry = getViewEntryForMenuItem(item);
512 if (entry == null) {
513 return false;
514 }
515
516 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700517 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700518 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700519 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
520 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700521 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700522 return true;
523 }
524
525 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700526 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700527 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700528 public void showJoinAggregateActivity() {
Evan Millar6a61a1a2009-09-29 14:00:43 -0700529 long freshId = getRefreshedContactId();
530 if (freshId > 0) {
531 String displayName = null;
532 if (mCursor.moveToFirst()) {
533 displayName = mCursor.getString(0);
534 }
535 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
536 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, freshId);
537 if (displayName != null) {
538 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_NAME, displayName);
539 }
540 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikova5cfca32009-09-29 18:13:08 -0700541 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700542 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700543
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700544 @Override
545 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700546 if (requestCode == REQUEST_JOIN_CONTACT) {
547 if (resultCode == RESULT_OK && intent != null) {
548 final long contactId = ContentUris.parseId(intent.getData());
549 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700550 }
Evan Millar9cd81242009-09-26 16:02:31 -0700551 } else if (requestCode == REQUEST_EDIT_CONTACT) {
Evan Millardf181202009-09-30 15:15:51 -0700552 // TODO: Bring this back
553 if (resultCode == EditContactActivity.RESULT_CLOSE_VIEW_ACTIVITY) {
554 finish();
555 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700556 }
557 }
558
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700559 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700560 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700561
562 // The split operation may have removed the original aggregate contact, so we need
563 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700564 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700565 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700566 }
567
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700568 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700569 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700570 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700571
572 try {
573 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700574 long rawContactId = c.getLong(0);
575 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700576 }
577 } finally {
578 c.close();
579 }
580
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700581 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
582 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700583 }
584
585 /**
586 * Given a contact ID sets an aggregation exception to either join the contact with the
587 * current aggregate or split off.
588 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700589 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700590 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700591 for (long aRawContactId : mRawContactIds) {
592 if (aRawContactId != rawContactId) {
593 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
594 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
595 values.put(AggregationExceptions.TYPE, exceptionType);
596 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
597 }
598 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700599 }
600
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700601 private void showOptionsActivity() {
602 final Intent intent = new Intent(this, ContactOptionsActivity.class);
Evan Millar6a61a1a2009-09-29 14:00:43 -0700603 intent.setData(mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700604 startActivity(intent);
605 }
606
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700607 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
608 AdapterView.AdapterContextMenuInfo info;
609 try {
610 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
611 } catch (ClassCastException e) {
612 Log.e(TAG, "bad menuInfo", e);
613 return null;
614 }
615
616 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
617 }
618
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800619 @Override
620 public boolean onKeyDown(int keyCode, KeyEvent event) {
621 switch (keyCode) {
622 case KeyEvent.KEYCODE_CALL: {
623 try {
624 ITelephony phone = ITelephony.Stub.asInterface(
625 ServiceManager.checkService("phone"));
626 if (phone != null && !phone.isIdle()) {
627 // Skip out and let the key be handled at a higher level
628 break;
629 }
630 } catch (RemoteException re) {
631 // Fall through and try to call the contact
632 }
633
Evan Millar7911ff52009-07-21 15:55:18 -0700634 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800635 if (index != -1) {
636 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700637 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
638 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800639 }
640 } else if (mNumPhoneNumbers != 0) {
641 // There isn't anything selected, call the default number
Evan Millar6a61a1a2009-09-29 14:00:43 -0700642 long freshContactId = getRefreshedContactId();
643 if (freshContactId > 0) {
644 Uri hardContacUri = ContentUris.withAppendedId(
645 Contacts.CONTENT_URI, freshContactId);
646 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, hardContacUri);
647 startActivity(intent);
648 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800649 }
650 return true;
651 }
652
653 case KeyEvent.KEYCODE_DEL: {
Megha Joshid5afd8a2009-09-22 09:38:41 -0700654 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
655 showDialog(DIALOG_CONFIRM_READONLY_DELETE);
656 } else if (mWritableSourcesCnt > 1) {
657 showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
658 } else {
659 showDialog(DIALOG_CONFIRM_DELETE);
660 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800661 return true;
662 }
663 }
664
665 return super.onKeyDown(keyCode, event);
666 }
667
Evan Millar7911ff52009-07-21 15:55:18 -0700668 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800669 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
670 if (entry != null) {
671 Intent intent = entry.intent;
Evan Millardf181202009-09-30 15:15:51 -0700672 if (intent != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800673 try {
674 startActivity(intent);
675 } catch (ActivityNotFoundException e) {
676 Log.e(TAG, "No activity found for intent: " + intent);
677 signalError();
678 }
679 } else {
680 signalError();
681 }
682 } else {
683 signalError();
684 }
685 }
686
687 /**
688 * Signal an error to the user via a beep, or some other method.
689 */
690 private void signalError() {
691 //TODO: implement this when we have the sonification APIs
692 }
693
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800694 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700695 // 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 -0800696 StringBuilder buf = new StringBuilder("imto://");
697 buf.append(host);
698 buf.append('/');
699 buf.append(data);
700 return Uri.parse(buf.toString());
701 }
702
703 /**
704 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700705 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800706 * @param personCursor the URI for the contact being displayed
707 */
Evan Millar11d628c2009-09-02 08:55:01 -0700708 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800709 // Clear out the old entries
710 final int numSections = mSections.size();
711 for (int i = 0; i < numSections; i++) {
712 mSections.get(i).clear();
713 }
714
Evan Millar7911ff52009-07-21 15:55:18 -0700715 mRawContactIds.clear();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700716 mReadOnlySourcesCnt = 0;
717 mWritableSourcesCnt = 0;
718 mWritableRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700719
Evan Millar11d628c2009-09-02 08:55:01 -0700720 Sources sources = Sources.getInstance(this);
721
Evan Millar66388be2009-05-28 15:41:07 -0700722 // Build up method entries
Evan Millar6a61a1a2009-09-29 14:00:43 -0700723 if (mLookupUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700724 for (Entity entity: mEntities) {
725 final ContentValues entValues = entity.getEntityValues();
726 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700727 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700728
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700729 if (!mRawContactIds.contains(rawContactId)) {
730 mRawContactIds.add(rawContactId);
731 }
732
Evan Millar11d628c2009-09-02 08:55:01 -0700733 for (NamedContentValues subValue : entity.getSubValues()) {
734 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800735
Jeff Sharkeyab066932009-09-21 09:55:30 -0700736 final ContentValues entryValues = subValue.values;
737 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
738
Evan Millar11d628c2009-09-02 08:55:01 -0700739 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700740 if (mimetype == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -0700741
Jeff Sharkeyab066932009-09-21 09:55:30 -0700742 final DataKind kind = sources.getKindOrFallback(accountType, mimetype, this,
Evan Millar11d628c2009-09-02 08:55:01 -0700743 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700744 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800745
Evan Millar11d628c2009-09-02 08:55:01 -0700746 final long id = entryValues.getAsLong(Data._ID);
747 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700748 entry.contactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700749 entry.id = id;
750 entry.uri = uri;
751 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -0700752 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -0700753 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700754 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700755 entry.type = entryValues.getAsInteger(kind.typeColumn);
756 }
757 if (kind.iconRes > 0) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700758 entry.resPackageName = kind.resPackageName;
Evan Millar11d628c2009-09-02 08:55:01 -0700759 entry.actionIcon = kind.iconRes;
760 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800761
Evan Millar11d628c2009-09-02 08:55:01 -0700762 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700763 if (TextUtils.isEmpty(entry.data)) {
Alex Kennberg87fc3172009-03-28 06:43:06 -0700764 continue;
765 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700766
Evan Millarff04a272009-09-26 15:23:37 -0700767 final boolean isSuperPrimary = entryValues.getAsInteger(
768 Data.IS_SUPER_PRIMARY) != 0;
Evan Millar11d628c2009-09-02 08:55:01 -0700769
Evan Millarff04a272009-09-26 15:23:37 -0700770 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
771 // Build phone entries
772 mNumPhoneNumbers++;
Evan Millar11d628c2009-09-02 08:55:01 -0700773
Evan Millarff04a272009-09-26 15:23:37 -0700774 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
775 Uri.fromParts("tel", entry.data, null));
776 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
777 Uri.fromParts("sms", entry.data, null));
778 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700779
Evan Millarff04a272009-09-26 15:23:37 -0700780 entry.isPrimary = isSuperPrimary;
781 mPhoneEntries.add(entry);
782
783 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
784 || mShowSmsLinksForAllPhones) {
785 // Add an SMS entry
786 if (kind.iconAltRes > 0) {
787 entry.secondaryActionIcon = kind.iconAltRes;
Evan Millar49714ee2009-09-02 16:42:47 -0700788 }
Evan Millarff04a272009-09-26 15:23:37 -0700789 }
790 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
791 // Build email entries
792 entry.intent = new Intent(Intent.ACTION_SENDTO,
793 Uri.fromParts("mailto", entry.data, null));
794 entry.isPrimary = isSuperPrimary;
795 mEmailEntries.add(entry);
796 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
797 equals(mimetype)) {
798 // Build postal entries
799 entry.maxLines = 4;
800 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
801 mPostalEntries.add(entry);
802 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
803 // Build im entries
804 Object protocolObj = entryValues.getAsInteger(CommonDataKinds.Im.PROTOCOL);
805 String host = null;
Evan Millar49714ee2009-09-02 16:42:47 -0700806
Evan Millarff04a272009-09-26 15:23:37 -0700807 if (TextUtils.isEmpty(entry.label)) {
808 entry.label = getString(R.string.chat).toLowerCase();
809 }
Evan Millar11d628c2009-09-02 08:55:01 -0700810
Evan Millarff04a272009-09-26 15:23:37 -0700811 if (protocolObj instanceof Number) {
812 int protocol = ((Number) protocolObj).intValue();
813 host = ContactsUtils.lookupProviderNameFromId(protocol);
814 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
815 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
816 entry.maxLabelLines = 2;
Evan Millar11d628c2009-09-02 08:55:01 -0700817 }
Evan Millarff04a272009-09-26 15:23:37 -0700818 } else if (protocolObj != null) {
819 String providerName = (String) protocolObj;
820 host = providerName.toLowerCase();
821 }
822
823 // Only add the intent if there is a valid host
824 // host is null for CommonDataKinds.Im.PROTOCOL_CUSTOM
825 if (!TextUtils.isEmpty(host)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700826 entry.intent = new Intent(Intent.ACTION_SENDTO,
Evan Millarff04a272009-09-26 15:23:37 -0700827 constructImToUrl(host.toLowerCase(), entry.data));
828 }
829 //TODO(emillar) Add in presence info
Evan Millar11d628c2009-09-02 08:55:01 -0700830 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
831 entry.presenceIcon = Presence.getPresenceIconResourceId(
832 aggCursor.getInt(METHODS_STATUS_COLUMN));
833 entry.status = ...
834 }*/
Evan Millarff04a272009-09-26 15:23:37 -0700835 mImEntries.add(entry);
836 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)
837 || CommonDataKinds.Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) {
838 // Build organization and note entries
839 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -0700840 mOrganizationEntries.add(entry);
841 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
842 // Build note entries
Evan Millar11d628c2009-09-02 08:55:01 -0700843 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -0700844 entry.maxLines = 10;
845 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700846 } else {
847 // Handle showing custom
Evan Millarff04a272009-09-26 15:23:37 -0700848 entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700849 mOtherEntries.add(entry);
Evan Millar11d628c2009-09-02 08:55:01 -0700850 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700851 }
Evan Millar66388be2009-05-28 15:41:07 -0700852 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800853 }
854 }
855
Evan Millar11d628c2009-09-02 08:55:01 -0700856 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
857 if (kind.actionHeader == null) {
858 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700859 }
Evan Millar11d628c2009-09-02 08:55:01 -0700860 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
861 if (actionHeader == null) {
862 return null;
863 }
864 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
865 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700866
Evan Millar11d628c2009-09-02 08:55:01 -0700867 String buildDataString(DataKind kind, ContentValues values) {
868 if (kind.actionBody == null) {
869 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800870 }
Evan Millar11d628c2009-09-02 08:55:01 -0700871 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
872 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800873 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700874
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800875 /**
876 * A basic structure with the data for a contact entry in the list.
877 */
Evan Millaradb0f8c2009-09-28 17:20:50 -0700878 class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700879 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800880 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -0700881 public boolean isPrimary = false;
882 public int presenceIcon = -1;
883 public int secondaryActionIcon = -1;
884 public Intent intent;
885 public Intent secondaryIntent = null;
886 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800887 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700888 public ArrayList<Long> ids = new ArrayList<Long>();
889 public int collapseCount = 0;
890
891 public boolean collapseWith(ViewEntry entry) {
892 // assert equal collapse keys
Evan Millaradb0f8c2009-09-28 17:20:50 -0700893 if (!shouldCollapseWith(entry)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -0700894 return false;
895 }
896
897 // Choose the label associated with the highest type precedence.
898 if (TypePrecedence.getTypePrecedence(mimetype, type)
899 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
900 type = entry.type;
901 label = entry.label;
902 }
903
904 // Choose the max of the maxLines and maxLabelLines values.
905 maxLines = Math.max(maxLines, entry.maxLines);
906 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
907
908 // Choose the presence with the highest precedence.
909 if (Presence.getPresencePrecedence(status)
910 < Presence.getPresencePrecedence(entry.status)) {
911 status = entry.status;
912 }
913
914 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -0700915 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700916
917 // uri, and contactdId, shouldn't make a difference. Just keep the original.
918
919 // Keep track of all the ids that have been collapsed with this one.
920 ids.add(entry.id);
921 collapseCount++;
922 return true;
923 }
924
Evan Millaradb0f8c2009-09-28 17:20:50 -0700925 public boolean shouldCollapseWith(ViewEntry entry) {
926 if (entry == null) {
927 return false;
928 }
929
930 if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)
931 && Phone.CONTENT_ITEM_TYPE.equals(entry.mimetype)) {
932 if (!PhoneNumberUtils.compare(ViewContactActivity.this, data, entry.data)) {
933 return false;
934 }
935 } else {
936 if (!equals(data, entry.data)) {
937 return false;
938 }
939 }
940
941 if (!equals(mimetype, entry.mimetype)
942 || !intentCollapsible(intent, entry.intent)
943 || !intentCollapsible(secondaryIntent, entry.secondaryIntent)
944 || actionIcon != entry.actionIcon) {
945 return false;
946 }
947
948 return true;
949 }
950
951 private boolean equals(Object a, Object b) {
952 return a==b || (a != null && a.equals(b));
953 }
954
955 private boolean intentCollapsible(Intent a, Intent b) {
956 if (a == b) {
957 return true;
958 } else if ((a != null && b != null) && equals(a.getAction(), b.getAction())) {
959 return true;
960 }
961 return false;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700962 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800963 }
964
Evan Millar15e514d2009-08-04 10:14:57 -0700965 /** Cache of the children views of a row */
966 static class ViewCache {
967 public TextView label;
968 public TextView data;
969 public ImageView actionIcon;
970 public ImageView presenceIcon;
971 public ImageView primaryIcon;
972 public ImageView secondaryActionButton;
973 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700974
Evan Millar15e514d2009-08-04 10:14:57 -0700975 // Need to keep track of this too
976 ViewEntry entry;
977 }
978
979 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
980 implements View.OnClickListener {
981
Evan Millar5c22c3b2009-05-29 11:37:54 -0700982
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800983 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
984 super(context, sections, SHOW_SEPARATORS);
985 }
986
Evan Millar15e514d2009-08-04 10:14:57 -0700987 public void onClick(View v) {
988 Intent intent = (Intent) v.getTag();
989 startActivity(intent);
990 }
991
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800992 @Override
993 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -0700994 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800995 View v;
996
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800997 ViewCache views;
998
999 // Check to see if we can reuse convertView
1000 if (convertView != null) {
1001 v = convertView;
1002 views = (ViewCache) v.getTag();
1003 } else {
1004 // Create a new view if needed
1005 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1006
1007 // Cache the children
1008 views = new ViewCache();
1009 views.label = (TextView) v.findViewById(android.R.id.text1);
1010 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001011 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1012 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1013 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1014 views.secondaryActionButton = (ImageView) v.findViewById(
1015 R.id.secondary_action_button);
1016 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001017 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001018 v.setTag(views);
1019 }
1020
1021 // Update the entry in the view cache
1022 views.entry = entry;
1023
1024 // Bind the data to the view
1025 bindView(v, entry);
1026 return v;
1027 }
1028
1029 @Override
1030 protected View newView(int position, ViewGroup parent) {
1031 // getView() handles this
1032 throw new UnsupportedOperationException();
1033 }
1034
1035 @Override
1036 protected void bindView(View view, ViewEntry entry) {
1037 final Resources resources = mContext.getResources();
1038 ViewCache views = (ViewCache) view.getTag();
1039
1040 // Set the label
1041 TextView label = views.label;
1042 setMaxLines(label, entry.maxLabelLines);
1043 label.setText(entry.label);
1044
1045 // Set the data
1046 TextView data = views.data;
1047 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001048 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001049 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001050 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1051 } else {
1052 data.setText(entry.data);
1053 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001054 setMaxLines(data, entry.maxLines);
1055 }
1056
Evan Millar15e514d2009-08-04 10:14:57 -07001057 // Set the primary icon
1058 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1059
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001060 // Set the action icon
1061 ImageView action = views.actionIcon;
1062 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001063 Drawable actionIcon;
1064 if (entry.resPackageName != null) {
1065 // Load external resources through PackageManager
1066 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1067 entry.actionIcon, null);
1068 } else {
1069 actionIcon = resources.getDrawable(entry.actionIcon);
1070 }
1071 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001072 action.setVisibility(View.VISIBLE);
1073 } else {
1074 // Things should still line up as if there was an icon, so make it invisible
1075 action.setVisibility(View.INVISIBLE);
1076 }
1077
1078 // Set the presence icon
1079 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001080 if (entry.presenceIcon != -1) {
1081 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001082 } else if (entry.status != -1) {
1083 presenceIcon = resources.getDrawable(
1084 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001085 }
Evan Millar15e514d2009-08-04 10:14:57 -07001086 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001087 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001088 presenceIconView.setImageDrawable(presenceIcon);
1089 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001090 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001091 presenceIconView.setVisibility(View.GONE);
1092 }
1093
1094 // Set the secondary action button
1095 ImageView secondaryActionView = views.secondaryActionButton;
1096 Drawable secondaryActionIcon = null;
1097 if (entry.secondaryActionIcon != -1) {
1098 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1099 }
1100 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1101 secondaryActionView.setImageDrawable(secondaryActionIcon);
1102 secondaryActionView.setTag(entry.secondaryIntent);
1103 secondaryActionView.setVisibility(View.VISIBLE);
1104 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1105 } else {
1106 secondaryActionView.setVisibility(View.GONE);
1107 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001108 }
1109 }
1110
1111 private void setMaxLines(TextView textView, int maxLines) {
1112 if (maxLines == 1) {
1113 textView.setSingleLine(true);
1114 textView.setEllipsize(TextUtils.TruncateAt.END);
1115 } else {
1116 textView.setSingleLine(false);
1117 textView.setMaxLines(maxLines);
1118 textView.setEllipsize(null);
1119 }
1120 }
1121 }
1122}