blob: 6e993ba7c4d10b822cbdf608303f3b5d28b4af7a [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;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070059import android.provider.ContactsContract.RawContacts;
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -070060import android.provider.ContactsContract.StatusUpdates;
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 if (resultCode == EditContactActivity.RESULT_CLOSE_VIEW_ACTIVITY) {
553 finish();
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -0700554 } else {
555 mLookupUri = intent.getData();
556 if (mLookupUri == null) {
557 finish();
558 }
Evan Millardf181202009-09-30 15:15:51 -0700559 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700560 }
561 }
562
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700563 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700564 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700565
566 // The split operation may have removed the original aggregate contact, so we need
567 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700568 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700569 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700570 }
571
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700572 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700573 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700574 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700575
576 try {
577 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700578 long rawContactId = c.getLong(0);
579 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700580 }
581 } finally {
582 c.close();
583 }
584
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700585 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
586 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700587 }
588
589 /**
590 * Given a contact ID sets an aggregation exception to either join the contact with the
591 * current aggregate or split off.
592 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700593 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700594 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700595 for (long aRawContactId : mRawContactIds) {
596 if (aRawContactId != rawContactId) {
597 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
598 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
599 values.put(AggregationExceptions.TYPE, exceptionType);
600 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
601 }
602 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700603 }
604
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700605 private void showOptionsActivity() {
606 final Intent intent = new Intent(this, ContactOptionsActivity.class);
Evan Millar6a61a1a2009-09-29 14:00:43 -0700607 intent.setData(mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700608 startActivity(intent);
609 }
610
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700611 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
612 AdapterView.AdapterContextMenuInfo info;
613 try {
614 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
615 } catch (ClassCastException e) {
616 Log.e(TAG, "bad menuInfo", e);
617 return null;
618 }
619
620 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
621 }
622
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800623 @Override
624 public boolean onKeyDown(int keyCode, KeyEvent event) {
625 switch (keyCode) {
626 case KeyEvent.KEYCODE_CALL: {
627 try {
628 ITelephony phone = ITelephony.Stub.asInterface(
629 ServiceManager.checkService("phone"));
630 if (phone != null && !phone.isIdle()) {
631 // Skip out and let the key be handled at a higher level
632 break;
633 }
634 } catch (RemoteException re) {
635 // Fall through and try to call the contact
636 }
637
Evan Millar7911ff52009-07-21 15:55:18 -0700638 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800639 if (index != -1) {
640 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700641 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
642 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800643 }
644 } else if (mNumPhoneNumbers != 0) {
645 // There isn't anything selected, call the default number
Evan Millar6a61a1a2009-09-29 14:00:43 -0700646 long freshContactId = getRefreshedContactId();
647 if (freshContactId > 0) {
648 Uri hardContacUri = ContentUris.withAppendedId(
649 Contacts.CONTENT_URI, freshContactId);
650 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, hardContacUri);
651 startActivity(intent);
652 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800653 }
654 return true;
655 }
656
657 case KeyEvent.KEYCODE_DEL: {
Megha Joshid5afd8a2009-09-22 09:38:41 -0700658 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
659 showDialog(DIALOG_CONFIRM_READONLY_DELETE);
660 } else if (mWritableSourcesCnt > 1) {
661 showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
662 } else {
663 showDialog(DIALOG_CONFIRM_DELETE);
664 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800665 return true;
666 }
667 }
668
669 return super.onKeyDown(keyCode, event);
670 }
671
Evan Millar7911ff52009-07-21 15:55:18 -0700672 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800673 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
674 if (entry != null) {
675 Intent intent = entry.intent;
Evan Millardf181202009-09-30 15:15:51 -0700676 if (intent != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800677 try {
678 startActivity(intent);
679 } catch (ActivityNotFoundException e) {
680 Log.e(TAG, "No activity found for intent: " + intent);
681 signalError();
682 }
683 } else {
684 signalError();
685 }
686 } else {
687 signalError();
688 }
689 }
690
691 /**
692 * Signal an error to the user via a beep, or some other method.
693 */
694 private void signalError() {
695 //TODO: implement this when we have the sonification APIs
696 }
697
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800698 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700699 // 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 -0800700 StringBuilder buf = new StringBuilder("imto://");
701 buf.append(host);
702 buf.append('/');
703 buf.append(data);
704 return Uri.parse(buf.toString());
705 }
706
707 /**
708 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700709 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800710 * @param personCursor the URI for the contact being displayed
711 */
Evan Millar11d628c2009-09-02 08:55:01 -0700712 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800713 // Clear out the old entries
714 final int numSections = mSections.size();
715 for (int i = 0; i < numSections; i++) {
716 mSections.get(i).clear();
717 }
718
Evan Millar7911ff52009-07-21 15:55:18 -0700719 mRawContactIds.clear();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700720 mReadOnlySourcesCnt = 0;
721 mWritableSourcesCnt = 0;
722 mWritableRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700723
Evan Millar11d628c2009-09-02 08:55:01 -0700724 Sources sources = Sources.getInstance(this);
725
Evan Millar66388be2009-05-28 15:41:07 -0700726 // Build up method entries
Evan Millar6a61a1a2009-09-29 14:00:43 -0700727 if (mLookupUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700728 for (Entity entity: mEntities) {
729 final ContentValues entValues = entity.getEntityValues();
730 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700731 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700732
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700733 if (!mRawContactIds.contains(rawContactId)) {
734 mRawContactIds.add(rawContactId);
735 }
736
Evan Millar11d628c2009-09-02 08:55:01 -0700737 for (NamedContentValues subValue : entity.getSubValues()) {
738 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800739
Jeff Sharkeyab066932009-09-21 09:55:30 -0700740 final ContentValues entryValues = subValue.values;
741 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
742
Evan Millar11d628c2009-09-02 08:55:01 -0700743 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700744 if (mimetype == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -0700745
Jeff Sharkeyab066932009-09-21 09:55:30 -0700746 final DataKind kind = sources.getKindOrFallback(accountType, mimetype, this,
Evan Millar11d628c2009-09-02 08:55:01 -0700747 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700748 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800749
Evan Millar11d628c2009-09-02 08:55:01 -0700750 final long id = entryValues.getAsLong(Data._ID);
751 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700752 entry.contactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700753 entry.id = id;
754 entry.uri = uri;
755 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -0700756 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -0700757 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700758 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700759 entry.type = entryValues.getAsInteger(kind.typeColumn);
760 }
761 if (kind.iconRes > 0) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700762 entry.resPackageName = kind.resPackageName;
Evan Millar11d628c2009-09-02 08:55:01 -0700763 entry.actionIcon = kind.iconRes;
764 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800765
Evan Millar11d628c2009-09-02 08:55:01 -0700766 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700767 if (TextUtils.isEmpty(entry.data)) {
Alex Kennberg87fc3172009-03-28 06:43:06 -0700768 continue;
769 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700770
Evan Millarff04a272009-09-26 15:23:37 -0700771 final boolean isSuperPrimary = entryValues.getAsInteger(
772 Data.IS_SUPER_PRIMARY) != 0;
Evan Millar11d628c2009-09-02 08:55:01 -0700773
Evan Millarff04a272009-09-26 15:23:37 -0700774 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
775 // Build phone entries
776 mNumPhoneNumbers++;
Evan Millar11d628c2009-09-02 08:55:01 -0700777
Evan Millarff04a272009-09-26 15:23:37 -0700778 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
779 Uri.fromParts("tel", entry.data, null));
780 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
781 Uri.fromParts("sms", entry.data, null));
782 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700783
Evan Millarff04a272009-09-26 15:23:37 -0700784 entry.isPrimary = isSuperPrimary;
785 mPhoneEntries.add(entry);
786
787 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
788 || mShowSmsLinksForAllPhones) {
789 // Add an SMS entry
790 if (kind.iconAltRes > 0) {
791 entry.secondaryActionIcon = kind.iconAltRes;
Evan Millar49714ee2009-09-02 16:42:47 -0700792 }
Evan Millarff04a272009-09-26 15:23:37 -0700793 }
794 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
795 // Build email entries
796 entry.intent = new Intent(Intent.ACTION_SENDTO,
797 Uri.fromParts("mailto", entry.data, null));
798 entry.isPrimary = isSuperPrimary;
799 mEmailEntries.add(entry);
800 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
801 equals(mimetype)) {
802 // Build postal entries
803 entry.maxLines = 4;
804 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
805 mPostalEntries.add(entry);
806 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
807 // Build im entries
808 Object protocolObj = entryValues.getAsInteger(CommonDataKinds.Im.PROTOCOL);
809 String host = null;
Evan Millar49714ee2009-09-02 16:42:47 -0700810
Evan Millarff04a272009-09-26 15:23:37 -0700811 if (TextUtils.isEmpty(entry.label)) {
812 entry.label = getString(R.string.chat).toLowerCase();
813 }
Evan Millar11d628c2009-09-02 08:55:01 -0700814
Evan Millarff04a272009-09-26 15:23:37 -0700815 if (protocolObj instanceof Number) {
816 int protocol = ((Number) protocolObj).intValue();
817 host = ContactsUtils.lookupProviderNameFromId(protocol);
818 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
819 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
820 entry.maxLabelLines = 2;
Evan Millar11d628c2009-09-02 08:55:01 -0700821 }
Evan Millarff04a272009-09-26 15:23:37 -0700822 } else if (protocolObj != null) {
823 String providerName = (String) protocolObj;
824 host = providerName.toLowerCase();
825 }
826
827 // Only add the intent if there is a valid host
828 // host is null for CommonDataKinds.Im.PROTOCOL_CUSTOM
829 if (!TextUtils.isEmpty(host)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700830 entry.intent = new Intent(Intent.ACTION_SENDTO,
Evan Millarff04a272009-09-26 15:23:37 -0700831 constructImToUrl(host.toLowerCase(), entry.data));
832 }
833 //TODO(emillar) Add in presence info
Evan Millar11d628c2009-09-02 08:55:01 -0700834 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
835 entry.presenceIcon = Presence.getPresenceIconResourceId(
836 aggCursor.getInt(METHODS_STATUS_COLUMN));
837 entry.status = ...
838 }*/
Evan Millarff04a272009-09-26 15:23:37 -0700839 mImEntries.add(entry);
840 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)
841 || CommonDataKinds.Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) {
842 // Build organization and note entries
843 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -0700844 mOrganizationEntries.add(entry);
845 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
846 // Build note entries
Evan Millar11d628c2009-09-02 08:55:01 -0700847 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -0700848 entry.maxLines = 10;
849 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700850 } else {
851 // Handle showing custom
Evan Millarff04a272009-09-26 15:23:37 -0700852 entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700853 mOtherEntries.add(entry);
Evan Millar11d628c2009-09-02 08:55:01 -0700854 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700855 }
Evan Millar66388be2009-05-28 15:41:07 -0700856 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800857 }
858 }
859
Evan Millar11d628c2009-09-02 08:55:01 -0700860 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
861 if (kind.actionHeader == null) {
862 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700863 }
Evan Millar11d628c2009-09-02 08:55:01 -0700864 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
865 if (actionHeader == null) {
866 return null;
867 }
868 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
869 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700870
Evan Millar11d628c2009-09-02 08:55:01 -0700871 String buildDataString(DataKind kind, ContentValues values) {
872 if (kind.actionBody == null) {
873 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800874 }
Evan Millar11d628c2009-09-02 08:55:01 -0700875 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
876 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800877 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700878
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800879 /**
880 * A basic structure with the data for a contact entry in the list.
881 */
Evan Millaradb0f8c2009-09-28 17:20:50 -0700882 class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700883 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800884 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -0700885 public boolean isPrimary = false;
886 public int presenceIcon = -1;
887 public int secondaryActionIcon = -1;
888 public Intent intent;
889 public Intent secondaryIntent = null;
890 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800891 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700892 public ArrayList<Long> ids = new ArrayList<Long>();
893 public int collapseCount = 0;
894
895 public boolean collapseWith(ViewEntry entry) {
896 // assert equal collapse keys
Evan Millaradb0f8c2009-09-28 17:20:50 -0700897 if (!shouldCollapseWith(entry)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -0700898 return false;
899 }
900
901 // Choose the label associated with the highest type precedence.
902 if (TypePrecedence.getTypePrecedence(mimetype, type)
903 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
904 type = entry.type;
905 label = entry.label;
906 }
907
908 // Choose the max of the maxLines and maxLabelLines values.
909 maxLines = Math.max(maxLines, entry.maxLines);
910 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
911
912 // Choose the presence with the highest precedence.
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -0700913 if (StatusUpdates.getPresencePrecedence(status)
914 < StatusUpdates.getPresencePrecedence(entry.status)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -0700915 status = entry.status;
916 }
917
918 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -0700919 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700920
921 // uri, and contactdId, shouldn't make a difference. Just keep the original.
922
923 // Keep track of all the ids that have been collapsed with this one.
924 ids.add(entry.id);
925 collapseCount++;
926 return true;
927 }
928
Evan Millaradb0f8c2009-09-28 17:20:50 -0700929 public boolean shouldCollapseWith(ViewEntry entry) {
930 if (entry == null) {
931 return false;
932 }
933
934 if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)
935 && Phone.CONTENT_ITEM_TYPE.equals(entry.mimetype)) {
936 if (!PhoneNumberUtils.compare(ViewContactActivity.this, data, entry.data)) {
937 return false;
938 }
939 } else {
940 if (!equals(data, entry.data)) {
941 return false;
942 }
943 }
944
945 if (!equals(mimetype, entry.mimetype)
946 || !intentCollapsible(intent, entry.intent)
947 || !intentCollapsible(secondaryIntent, entry.secondaryIntent)
948 || actionIcon != entry.actionIcon) {
949 return false;
950 }
951
952 return true;
953 }
954
955 private boolean equals(Object a, Object b) {
956 return a==b || (a != null && a.equals(b));
957 }
958
959 private boolean intentCollapsible(Intent a, Intent b) {
960 if (a == b) {
961 return true;
962 } else if ((a != null && b != null) && equals(a.getAction(), b.getAction())) {
963 return true;
964 }
965 return false;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700966 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800967 }
968
Evan Millar15e514d2009-08-04 10:14:57 -0700969 /** Cache of the children views of a row */
970 static class ViewCache {
971 public TextView label;
972 public TextView data;
973 public ImageView actionIcon;
974 public ImageView presenceIcon;
975 public ImageView primaryIcon;
976 public ImageView secondaryActionButton;
977 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700978
Evan Millar15e514d2009-08-04 10:14:57 -0700979 // Need to keep track of this too
980 ViewEntry entry;
981 }
982
983 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
984 implements View.OnClickListener {
985
Evan Millar5c22c3b2009-05-29 11:37:54 -0700986
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800987 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
988 super(context, sections, SHOW_SEPARATORS);
989 }
990
Evan Millar15e514d2009-08-04 10:14:57 -0700991 public void onClick(View v) {
992 Intent intent = (Intent) v.getTag();
993 startActivity(intent);
994 }
995
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800996 @Override
997 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -0700998 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800999 View v;
1000
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001001 ViewCache views;
1002
1003 // Check to see if we can reuse convertView
1004 if (convertView != null) {
1005 v = convertView;
1006 views = (ViewCache) v.getTag();
1007 } else {
1008 // Create a new view if needed
1009 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1010
1011 // Cache the children
1012 views = new ViewCache();
1013 views.label = (TextView) v.findViewById(android.R.id.text1);
1014 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001015 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1016 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1017 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1018 views.secondaryActionButton = (ImageView) v.findViewById(
1019 R.id.secondary_action_button);
1020 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001021 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001022 v.setTag(views);
1023 }
1024
1025 // Update the entry in the view cache
1026 views.entry = entry;
1027
1028 // Bind the data to the view
1029 bindView(v, entry);
1030 return v;
1031 }
1032
1033 @Override
1034 protected View newView(int position, ViewGroup parent) {
1035 // getView() handles this
1036 throw new UnsupportedOperationException();
1037 }
1038
1039 @Override
1040 protected void bindView(View view, ViewEntry entry) {
1041 final Resources resources = mContext.getResources();
1042 ViewCache views = (ViewCache) view.getTag();
1043
1044 // Set the label
1045 TextView label = views.label;
1046 setMaxLines(label, entry.maxLabelLines);
1047 label.setText(entry.label);
1048
1049 // Set the data
1050 TextView data = views.data;
1051 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001052 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001053 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001054 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1055 } else {
1056 data.setText(entry.data);
1057 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001058 setMaxLines(data, entry.maxLines);
1059 }
1060
Evan Millar15e514d2009-08-04 10:14:57 -07001061 // Set the primary icon
1062 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1063
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001064 // Set the action icon
1065 ImageView action = views.actionIcon;
1066 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001067 Drawable actionIcon;
1068 if (entry.resPackageName != null) {
1069 // Load external resources through PackageManager
1070 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1071 entry.actionIcon, null);
1072 } else {
1073 actionIcon = resources.getDrawable(entry.actionIcon);
1074 }
1075 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001076 action.setVisibility(View.VISIBLE);
1077 } else {
1078 // Things should still line up as if there was an icon, so make it invisible
1079 action.setVisibility(View.INVISIBLE);
1080 }
1081
1082 // Set the presence icon
1083 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001084 if (entry.presenceIcon != -1) {
1085 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001086 } else if (entry.status != -1) {
1087 presenceIcon = resources.getDrawable(
Dmitri Plotnikov160e9da2009-09-30 23:48:12 -07001088 StatusUpdates.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001089 }
Evan Millar15e514d2009-08-04 10:14:57 -07001090 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001091 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001092 presenceIconView.setImageDrawable(presenceIcon);
1093 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001094 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001095 presenceIconView.setVisibility(View.GONE);
1096 }
1097
1098 // Set the secondary action button
1099 ImageView secondaryActionView = views.secondaryActionButton;
1100 Drawable secondaryActionIcon = null;
1101 if (entry.secondaryActionIcon != -1) {
1102 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1103 }
1104 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1105 secondaryActionView.setImageDrawable(secondaryActionIcon);
1106 secondaryActionView.setTag(entry.secondaryIntent);
1107 secondaryActionView.setVisibility(View.VISIBLE);
1108 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1109 } else {
1110 secondaryActionView.setVisibility(View.GONE);
1111 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001112 }
1113 }
1114
1115 private void setMaxLines(TextView textView, int maxLines) {
1116 if (maxLines == 1) {
1117 textView.setSingleLine(true);
1118 textView.setEllipsize(TextUtils.TruncateAt.END);
1119 } else {
1120 textView.setSingleLine(false);
1121 textView.setMaxLines(maxLines);
1122 textView.setEllipsize(null);
1123 }
1124 }
1125 }
1126}