blob: 26763456dcc552f73168240fd1965adc16330847 [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;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070025import com.android.contacts.util.Constants;
Evan Millar11d628c2009-09-02 08:55:01 -070026import com.android.contacts.util.NotifyingAsyncQueryHandler;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080027import com.android.internal.telephony.ITelephony;
Evan Millar11d628c2009-09-02 08:55:01 -070028import com.android.internal.widget.ContactHeaderWidget;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080029
Evan Millar11d628c2009-09-02 08:55:01 -070030import android.app.Activity;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080031import android.app.AlertDialog;
32import android.app.Dialog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080033import android.content.ActivityNotFoundException;
34import android.content.ContentResolver;
35import android.content.ContentUris;
36import android.content.ContentValues;
37import android.content.Context;
38import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070039import android.content.Entity;
Evan Millar11d628c2009-09-02 08:55:01 -070040import android.content.EntityIterator;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080041import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070042import android.content.DialogInterface.OnClickListener;
Evan Millar11d628c2009-09-02 08:55:01 -070043import android.content.Entity.NamedContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080044import android.content.res.Resources;
45import android.database.ContentObserver;
46import android.database.Cursor;
47import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080048import android.net.Uri;
49import android.os.Bundle;
50import android.os.Handler;
51import android.os.RemoteException;
52import android.os.ServiceManager;
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -070053import android.provider.ContactsContract;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070054import android.provider.ContactsContract.AggregationExceptions;
55import android.provider.ContactsContract.CommonDataKinds;
Evan Millar11d628c2009-09-02 08:55:01 -070056import android.provider.ContactsContract.Contacts;
Evan Millar66388be2009-05-28 15:41:07 -070057import android.provider.ContactsContract.Data;
Evan Millar54a5c9f2009-06-23 17:41:09 -070058import android.provider.ContactsContract.Presence;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070059import android.provider.ContactsContract.RawContacts;
Evan Millar54a5c9f2009-06-23 17:41:09 -070060import android.provider.ContactsContract.CommonDataKinds.Phone;
61import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080062import android.text.TextUtils;
63import android.util.Log;
Evan Millar11d628c2009-09-02 08:55:01 -070064import android.util.SparseArray;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080065import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070066import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080067import android.view.KeyEvent;
Evan Millar11d628c2009-09-02 08:55:01 -070068import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080069import android.view.Menu;
Jeff Sharkey49d17b32009-09-07 02:14:21 -070070import android.view.MenuInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080071import android.view.MenuItem;
72import android.view.View;
73import android.view.ViewGroup;
Evan Millar11d628c2009-09-02 08:55:01 -070074import android.view.Window;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080075import android.view.ContextMenu.ContextMenuInfo;
76import android.widget.AdapterView;
Evan Millar7911ff52009-07-21 15:55:18 -070077import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080078import android.widget.ImageView;
79import android.widget.ListView;
80import android.widget.TextView;
81import android.widget.Toast;
82
83import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080084
85/**
86 * Displays the details of a specific contact.
87 */
Evan Millar11d628c2009-09-02 08:55:01 -070088public class ViewContactActivity extends Activity
Evan Millar7911ff52009-07-21 15:55:18 -070089 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
Evan Millar51514db2009-09-22 11:42:35 -070090 AdapterView.OnItemClickListener, NotifyingAsyncQueryHandler.AsyncQueryListener {
91// OnTabSelectionChangedListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080092 private static final String TAG = "ViewContact";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080093
Evan Millar8a79cee2009-08-19 17:20:49 -070094 public static final String RAW_CONTACT_ID_EXTRA = "rawContactIdExtra";
95
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080096 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080097
98 private static final int DIALOG_CONFIRM_DELETE = 1;
99
Evan Millar8a79cee2009-08-19 17:20:49 -0700100 private static final int REQUEST_JOIN_CONTACT = 1;
101 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700102
Evan Millar8a79cee2009-08-19 17:20:49 -0700103 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800104
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700105 protected Uri mLookupUri;
Evan Millar11d628c2009-09-02 08:55:01 -0700106 private Uri mUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800107 private ContentResolver mResolver;
108 private ViewAdapter mAdapter;
109 private int mNumPhoneNumbers = 0;
110
Evan Millar8a79cee2009-08-19 17:20:49 -0700111 private static final long ALL_CONTACTS_ID = -100;
Evan Millar7911ff52009-07-21 15:55:18 -0700112
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 private SparseArray<Long> mTabRawContactIdMap;
131 protected ScrollingTabWidget mTabWidget;
132 protected ContactHeaderWidget mContactHeaderWidget;
133 private NotifyingAsyncQueryHandler mHandler;
134
135 protected LayoutInflater mInflater;
136
137 //Projection used for the query that determines which tabs to add.
138 protected static final String[] TAB_PROJECTION = new String[] {
139 RawContacts._ID,
140 RawContacts.ACCOUNT_NAME,
141 RawContacts.ACCOUNT_TYPE
142 };
143 protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0;
144 protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1;
145 protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2;
146
147 protected static final String SELECTED_RAW_CONTACT_ID_KEY = "selectedRawContact";
148 protected Long mSelectedRawContactId = null;
149
150 private static final int TOKEN_QUERY = 0;
151
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800152 private ContentObserver mObserver = new ContentObserver(new Handler()) {
153 @Override
154 public boolean deliverSelfNotifications() {
155 return true;
156 }
157
158 @Override
159 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700160 if (mCursor != null && !mCursor.isClosed()) {
161 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800162 }
163 }
164 };
165
166 public void onClick(DialogInterface dialog, int which) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700167 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800168 getContentResolver().delete(mUri, null, null);
169 finish();
170 }
171
Evan Millar7911ff52009-07-21 15:55:18 -0700172 private FrameLayout mTabContentLayout;
173 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800174 private boolean mShowSmsLinksForAllPhones;
Evan Millar11d628c2009-09-02 08:55:01 -0700175 private ArrayList<Entity> mEntities = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800176
177 @Override
178 protected void onCreate(Bundle icicle) {
179 super.onCreate(icicle);
180
Evan Millar11d628c2009-09-02 08:55:01 -0700181 final Intent intent = getIntent();
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700182 Uri data = intent.getData();
183 String authority = data.getAuthority();
184 if (ContactsContract.AUTHORITY.equals(authority)) {
185 mLookupUri = data;
186 } else if (android.provider.Contacts.AUTHORITY.equals(authority)) {
187 final long rawContactId = ContentUris.parseId(data);
188 mLookupUri = RawContacts.getContactLookupUri(getContentResolver(),
189 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700190
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700191 }
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700192 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700193
194 requestWindowFeature(Window.FEATURE_NO_TITLE);
195 setContentView(R.layout.contact_card_layout);
196
197 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
198 mContactHeaderWidget.showStar(true);
Evan Millar11d628c2009-09-02 08:55:01 -0700199 mContactHeaderWidget.setExcludeMimes(new String[] {
200 Contacts.CONTENT_ITEM_TYPE
201 });
202
203 mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget);
Evan Millar51514db2009-09-22 11:42:35 -0700204// mTabWidget.setTabSelectionListener(this);
Evan Millar85863112009-09-21 14:35:18 -0700205 mTabWidget.setVisibility(View.GONE);
Evan Millar11d628c2009-09-02 08:55:01 -0700206
207 mTabRawContactIdMap = new SparseArray<Long>();
208
209 mHandler = new NotifyingAsyncQueryHandler(this, this);
210
Evan Millar7911ff52009-07-21 15:55:18 -0700211 mListView = new ListView(this);
212 mListView.setOnCreateContextMenuListener(this);
213 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
214 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800215
Evan Millar7911ff52009-07-21 15:55:18 -0700216 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
217 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800218
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800219 mResolver = getContentResolver();
220
221 // Build the list of sections. The order they're added to mSections dictates the
222 // order they are displayed in the list.
223 mSections.add(mPhoneEntries);
224 mSections.add(mSmsEntries);
225 mSections.add(mEmailEntries);
226 mSections.add(mImEntries);
227 mSections.add(mPostalEntries);
228 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700229 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800230 mSections.add(mOtherEntries);
231
232 //TODO Read this value from a preference
233 mShowSmsLinksForAllPhones = true;
Evan Millar11d628c2009-09-02 08:55:01 -0700234 }
235
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800236 @Override
237 protected void onResume() {
238 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700239 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800240 }
241
242 @Override
243 protected void onPause() {
244 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700245 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800246 }
247
248 @Override
249 protected void onDestroy() {
250 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700251 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800252 }
253
254 @Override
Evan Millar11d628c2009-09-02 08:55:01 -0700255 protected void onRestoreInstanceState(Bundle savedInstanceState) {
256 super.onRestoreInstanceState(savedInstanceState);
Evan Millar51514db2009-09-22 11:42:35 -0700257// mSelectedRawContactId = savedInstanceState.getLong(SELECTED_RAW_CONTACT_ID_KEY);
Evan Millar11d628c2009-09-02 08:55:01 -0700258 }
259
260 @Override
261 protected void onSaveInstanceState(Bundle outState) {
262 super.onSaveInstanceState(outState);
Evan Millar51514db2009-09-22 11:42:35 -0700263// outState.putLong(SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
Evan Millar11d628c2009-09-02 08:55:01 -0700264 }
265
266 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800267 protected Dialog onCreateDialog(int id) {
268 switch (id) {
269 case DIALOG_CONFIRM_DELETE:
270 return new AlertDialog.Builder(this)
271 .setTitle(R.string.deleteConfirmation_title)
272 .setIcon(android.R.drawable.ic_dialog_alert)
273 .setMessage(R.string.deleteConfirmation)
274 .setNegativeButton(android.R.string.cancel, null)
275 .setPositiveButton(android.R.string.ok, this)
276 .setCancelable(false)
277 .create();
278 }
279 return null;
280 }
281
Evan Millar11d628c2009-09-02 08:55:01 -0700282
283 // TAB CODE //
284 /**
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700285 * Adds a tab for each {@link RawContacts} associated with this contact.
Evan Millar11d628c2009-09-02 08:55:01 -0700286 * Override this method if you want to additional tabs and/or different
287 * tabs for your activity.
Evan Millar11d628c2009-09-02 08:55:01 -0700288 */
Evan Millar51514db2009-09-22 11:42:35 -0700289// protected void bindTabs() {
290// if (mEntities.size() > 1) {
291// addAllTab();
292// }
293//
294// final Sources sources = Sources.getInstance(this);
295//
296// for (Entity entity : mEntities) {
297// final String accountType = entity.getEntityValues().
298// getAsString(RawContacts.ACCOUNT_TYPE);
299// final Long rawContactId = entity.getEntityValues().
300// getAsLong(RawContacts._ID);
301//
302// // TODO: ensure inflation on background task so we don't block UI thread here
303// final ContactsSource source = sources.getInflatedSource(accountType,
304// ContactsSource.LEVEL_SUMMARY);
305// addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(),
306// source));
307// }
308// }
309//
310// /**
311// * Add a tab to be displayed in the {@link ScrollingTabWidget}.
312// *
313// * @param rawContactId The contact id associated with the tab.
314// * @param view A view to use as the tab indicator.
315// */
316// protected void addTab(long rawContactId, View view) {
317// mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId);
318// mTabWidget.addTab(view);
319// }
320//
321//
322// protected void clearCurrentTabs() {
323// mTabRawContactIdMap.clear();
324// mTabWidget.removeAllTabs();
325// }
326//
327// protected void selectInitialTab() {
328// int selectedTabIndex = 0;
329//
330// if (mSelectedRawContactId != null) {
331// selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
332// if (selectedTabIndex == -1) {
333// // If there was no matching tab, just select the first;
334// selectedTabIndex = 0;
335// }
336// }
337//
338//// mTabWidget.setCurrentTab(selectedTabIndex);
339// onTabSelectionChanged(selectedTabIndex, false);
340//// mTabWidget.setVisibility(View.VISIBLE);
341//// mTabWidget.postInvalidate();
342// }
343//
344// private void addAllTab() {
345// View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator,
346// mTabWidget.getTabParent(), false);
347// allTabIndicator.getBackground().setDither(true);
348// addTab(ALL_CONTACTS_ID, allTabIndicator);
349// }
350//
351// public void onTabSelectionChanged(int tabIndex, boolean clicked) {
352// Long rawContactId = getTabRawContactId(tabIndex);
353// if (rawContactId != null) {
354// mSelectedRawContactId = rawContactId;
355// bindData();
356// }
357// }
358//
359// /**
360// * Return the RawContact id associated with the tab at an index.
361// *
362// * @param index The index of the tab in question.
363// * @return The contactId associated with the tab at the specified index.
364// */
365// protected Long getTabRawContactId(int index) {
366// return mTabRawContactIdMap.get(index);
367// }
368//
369// /**
370// * Return the tab index associated with the RawContact id.
371// *
372// * @param index The index of the tab in question.
373// * @return The contactId associated with the tab at the specified index.
374// */
375// protected int getTabIndexForRawContactId(long rawContactId) {
376// int numTabs = mTabRawContactIdMap.size();
377// for (int i=0; i < numTabs; i++) {
378// if (mTabRawContactIdMap.get(i) == rawContactId) {
379// return i;
380// }
381// }
382// return -1;
383// }
Evan Millar11d628c2009-09-02 08:55:01 -0700384
385
386 // QUERY CODE //
387 /** {@inheritDoc} */
388 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
389 try{
390 if (token == TOKEN_QUERY) {
Evan Millar85863112009-09-21 14:35:18 -0700391// clearCurrentTabs();
Evan Millar11d628c2009-09-02 08:55:01 -0700392 mEntities = readEntities(iterator);
Evan Millar85863112009-09-21 14:35:18 -0700393 // Show the aggregate badge if this contact is aggregated.
394 if (mEntities.size() > 1) {
395 mContactHeaderWidget.showAggregateBadge(true);
396 }
397// bindTabs();
Evan Millar51514db2009-09-22 11:42:35 -0700398// selectInitialTab();
Evan Millar85863112009-09-21 14:35:18 -0700399 bindData();
Evan Millar11d628c2009-09-02 08:55:01 -0700400 }
401 } finally {
402 if (iterator != null) {
403 iterator.close();
404 }
405 }
406 }
407
408 /** {@inheritDoc} */
409 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700410 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700411 }
412
413 private ArrayList<Entity> readEntities(EntityIterator iterator) {
414 ArrayList<Entity> entities = new ArrayList<Entity>();
415 try {
416 while (iterator.hasNext()) {
417 entities.add(iterator.next());
418 }
419 } catch (RemoteException e) {
420 }
421
422 return entities;
423 }
424
425 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700426 closeCursor();
427
Dmitri Plotnikov0306bbd2009-09-03 14:37:15 -0700428 mUri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700429 if (mLookupUri != null) {
430 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
431 if (mLookupUri != null) {
432 mUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
433 }
434 }
435
436 if (mUri == null) {
437
438 // TODO either figure out a way to prevent a flash of black background or
439 // use some other UI than a toast
440 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
441 Log.e(TAG, "invalid contact uri: " + mLookupUri);
442 finish();
443 return;
444 }
445
446 mCursor = mResolver.query(Uri.withAppendedPath(mUri, Contacts.Data.CONTENT_DIRECTORY),
447 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
448 mCursor.registerContentObserver(mObserver);
449
Evan Millar11d628c2009-09-02 08:55:01 -0700450 long contactId = ContentUris.parseId(mUri);
451 mHandler.startQueryEntities(TOKEN_QUERY, null,
452 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700453
Evan Millar9b1a1242009-09-16 10:26:10 -0700454 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700455 }
456
457 private void closeCursor() {
458 if (mCursor != null) {
459 mCursor.unregisterContentObserver(mObserver);
460 mCursor.close();
461 mCursor = null;
462 }
Evan Millar11d628c2009-09-02 08:55:01 -0700463 }
464
465 private void bindData() {
466
467 // Build up the contact entries
468 buildEntries();
469
470 // Collapse similar data items in select sections.
471 Collapser.collapseList(mPhoneEntries);
472 Collapser.collapseList(mSmsEntries);
473 Collapser.collapseList(mEmailEntries);
474 Collapser.collapseList(mPostalEntries);
475
476 if (mAdapter == null) {
477 mAdapter = new ViewAdapter(this, mSections);
478 mListView.setAdapter(mAdapter);
479 } else {
480 mAdapter.setSections(mSections, SHOW_SEPARATORS);
481 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800482 }
483
484 @Override
485 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700486 super.onCreateOptionsMenu(menu);
487
488 final MenuInflater inflater = getMenuInflater();
489 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800490 return true;
491 }
492
493 @Override
494 public boolean onPrepareOptionsMenu(Menu menu) {
495 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700496
Evan Millar51514db2009-09-22 11:42:35 -0700497 // Only allow edit when we have at least one raw_contact id
498 final boolean hasRawContact = (mRawContactIds.size() > 0);
499 menu.findItem(R.id.menu_split).setEnabled(hasRawContact);
Evan Millardb5d88c2009-08-28 09:31:57 -0700500
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700501 // Only allow split when more than one contact
502 final boolean isAggregate = (mRawContactIds.size() > 1);
503 menu.findItem(R.id.menu_split).setEnabled(isAggregate);
504
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800505 return true;
506 }
507
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800508 @Override
509 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
510 AdapterView.AdapterContextMenuInfo info;
511 try {
512 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
513 } catch (ClassCastException e) {
514 Log.e(TAG, "bad menuInfo", e);
515 return;
516 }
517
518 // This can be null sometimes, don't crash...
519 if (info == null) {
520 Log.e(TAG, "bad menuInfo");
521 return;
522 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700523
Evan Millar45e0ed32009-06-01 16:44:38 -0700524 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
525 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
526 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700527 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
528 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700529 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800530 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700531 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
532 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700533 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700534 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800535 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700536 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700537 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
538 }
539 // TODO(emillar): add back with group support.
540 /* else if (entry.mimetype.equals()) {
541 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
542 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800543 }
544
545 @Override
546 public boolean onOptionsItemSelected(MenuItem item) {
547 switch (item.getItemId()) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700548 case R.id.menu_edit: {
Evan Millar51514db2009-09-22 11:42:35 -0700549// Long rawContactIdToEdit = mSelectedRawContactId;
550// if (rawContactIdToEdit == null) {
551// // This shouldn't be possible. We only show the edit option if
552// // this value is non-null.
553// break;
554// }
555// if (rawContactIdToEdit == ALL_CONTACTS_ID) {
556// // If the "all" tab is selected, edit the next tab.
557// rawContactIdToEdit = getTabRawContactId(mTabWidget.getCurrentTab() + 1);
558// }
559// Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
560// rawContactIdToEdit);
561// startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
562// REQUEST_EDIT_CONTACT);
563 if (mRawContactIds.size() > 0) {
564 long rawContactIdToEdit = mRawContactIds.get(0);
565 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
566 rawContactIdToEdit);
567 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
568 REQUEST_EDIT_CONTACT);
Evan Millardb5d88c2009-08-28 09:31:57 -0700569 }
Evan Millar8a79cee2009-08-19 17:20:49 -0700570 break;
571 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700572 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800573 // Get confirmation
574 showDialog(DIALOG_CONFIRM_DELETE);
575 return true;
576 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700577 case R.id.menu_split: {
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700578 if (mRawContactIds.size() == 2) {
579 splitContact(mRawContactIds.get(1));
580 } else {
581 showSplitAggregateDialog();
582 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700583 return true;
584 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700585 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700586 showJoinAggregateActivity();
587 return true;
588 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700589 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700590 showOptionsActivity();
591 return true;
592 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700593 case R.id.menu_share: {
594 final Intent intent = new Intent(Intent.ACTION_SEND);
595 intent.setType(Contacts.CONTENT_ITEM_TYPE);
596 intent.putExtra(Intent.EXTRA_STREAM, mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700597
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700598 // Launch chooser to share contact via
599 final CharSequence chooseTitle = getText(R.string.share_via);
600 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700601
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700602 try {
603 startActivity(chooseIntent);
604 } catch (ActivityNotFoundException ex) {
605 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800606 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700607 return true;
608 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800609 }
610 return super.onOptionsItemSelected(item);
611 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700612
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800613 @Override
614 public boolean onContextItemSelected(MenuItem item) {
615 switch (item.getItemId()) {
616 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700617 if (makeItemDefault(item)) {
618 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800619 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700620 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800621 }
622 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700623
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800624 return super.onContextItemSelected(item);
625 }
626
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700627 private boolean makeItemDefault(MenuItem item) {
628 ViewEntry entry = getViewEntryForMenuItem(item);
629 if (entry == null) {
630 return false;
631 }
632
633 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700634 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700635 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700636 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
637 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700638 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700639 return true;
640 }
641
642 /**
643 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
644 * The user picks a contact to be split into its own aggregate or clicks Cancel.
645 */
646 private void showSplitAggregateDialog() {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700647 // Wrap this dialog in a specific theme so that list items have correct text color.
648 final ContextThemeWrapper dialogContext =
649 new ContextThemeWrapper(this, android.R.style.Theme_Light);
650 AlertDialog.Builder builder =
651 new AlertDialog.Builder(dialogContext);
652 builder.setTitle(getString(R.string.splitAggregate_title));
653
654 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
655 builder.setView(view);
656
657 builder.setInverseBackgroundForced(true);
658 builder.setCancelable(true);
659 builder.setNegativeButton(android.R.string.cancel,
660 new OnClickListener() {
661 public void onClick(DialogInterface dialog, int which) {
662 dialog.dismiss();
663 }
664 });
665 final AlertDialog dialog = builder.create();
666
667 view.setOnContactSelectedListener(new OnContactSelectedListener() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700668 public void onContactSelected(long rawContactId) {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700669 dialog.dismiss();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700670 splitContact(rawContactId);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700671 }
672 });
673
674 dialog.show();
675 }
676
677 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700678 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700679 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700680 public void showJoinAggregateActivity() {
681 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
682 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700683 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700684 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700685
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700686 @Override
687 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700688 switch (requestCode) {
689 case REQUEST_JOIN_CONTACT: {
690 if (resultCode == RESULT_OK && intent != null) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700691 final long contactId = ContentUris.parseId(intent.getData());
692 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700693 }
694 break;
695 }
Evan Millar51514db2009-09-22 11:42:35 -0700696// case REQUEST_EDIT_CONTACT: {
697// if (resultCode == RESULT_OK && intent != null) {
698// long newInitialSelectedRawContactId = intent.getLongExtra(
699// RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
700// if (newInitialSelectedRawContactId != mSelectedRawContactId) {
701// mSelectedRawContactId = newInitialSelectedRawContactId;
702// }
703// }
704// }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700705 }
706 }
707
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700708 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700709 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700710
711 // The split operation may have removed the original aggregate contact, so we need
712 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700713 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700714 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700715 }
716
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700717 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700718 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700719 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700720
721 try {
722 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700723 long rawContactId = c.getLong(0);
724 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700725 }
726 } finally {
727 c.close();
728 }
729
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700730 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
731 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700732 }
733
734 /**
735 * Given a contact ID sets an aggregation exception to either join the contact with the
736 * current aggregate or split off.
737 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700738 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700739 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700740 for (long aRawContactId : mRawContactIds) {
741 if (aRawContactId != rawContactId) {
742 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
743 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
744 values.put(AggregationExceptions.TYPE, exceptionType);
745 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
746 }
747 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700748 }
749
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700750 private void showOptionsActivity() {
751 final Intent intent = new Intent(this, ContactOptionsActivity.class);
752 intent.setData(mUri);
753 startActivity(intent);
754 }
755
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700756 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
757 AdapterView.AdapterContextMenuInfo info;
758 try {
759 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
760 } catch (ClassCastException e) {
761 Log.e(TAG, "bad menuInfo", e);
762 return null;
763 }
764
765 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
766 }
767
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800768 @Override
769 public boolean onKeyDown(int keyCode, KeyEvent event) {
770 switch (keyCode) {
771 case KeyEvent.KEYCODE_CALL: {
772 try {
773 ITelephony phone = ITelephony.Stub.asInterface(
774 ServiceManager.checkService("phone"));
775 if (phone != null && !phone.isIdle()) {
776 // Skip out and let the key be handled at a higher level
777 break;
778 }
779 } catch (RemoteException re) {
780 // Fall through and try to call the contact
781 }
782
Evan Millar7911ff52009-07-21 15:55:18 -0700783 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800784 if (index != -1) {
785 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700786 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
787 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800788 }
789 } else if (mNumPhoneNumbers != 0) {
790 // There isn't anything selected, call the default number
791 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
792 startActivity(intent);
793 }
794 return true;
795 }
796
797 case KeyEvent.KEYCODE_DEL: {
798 showDialog(DIALOG_CONFIRM_DELETE);
799 return true;
800 }
801 }
802
803 return super.onKeyDown(keyCode, event);
804 }
805
Evan Millar7911ff52009-07-21 15:55:18 -0700806 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800807 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
808 if (entry != null) {
809 Intent intent = entry.intent;
810 if (intent != null) {
811 try {
812 startActivity(intent);
813 } catch (ActivityNotFoundException e) {
814 Log.e(TAG, "No activity found for intent: " + intent);
815 signalError();
816 }
817 } else {
818 signalError();
819 }
820 } else {
821 signalError();
822 }
823 }
824
825 /**
826 * Signal an error to the user via a beep, or some other method.
827 */
828 private void signalError() {
829 //TODO: implement this when we have the sonification APIs
830 }
831
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800832 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700833 // 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 -0800834 StringBuilder buf = new StringBuilder("imto://");
835 buf.append(host);
836 buf.append('/');
837 buf.append(data);
838 return Uri.parse(buf.toString());
839 }
840
841 /**
842 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700843 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800844 * @param personCursor the URI for the contact being displayed
845 */
Evan Millar11d628c2009-09-02 08:55:01 -0700846 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800847 // Clear out the old entries
848 final int numSections = mSections.size();
849 for (int i = 0; i < numSections; i++) {
850 mSections.get(i).clear();
851 }
852
Evan Millar7911ff52009-07-21 15:55:18 -0700853 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700854
Evan Millar11d628c2009-09-02 08:55:01 -0700855 Sources sources = Sources.getInstance(this);
856
Evan Millar66388be2009-05-28 15:41:07 -0700857 // Build up method entries
858 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700859 for (Entity entity: mEntities) {
860 final ContentValues entValues = entity.getEntityValues();
861 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700862 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700863
Evan Millar51514db2009-09-22 11:42:35 -0700864// // This performs the tab filtering
865// if (mSelectedRawContactId != null
866// && mSelectedRawContactId != rawContactId
867// && mSelectedRawContactId != ALL_CONTACTS_ID) {
868// continue;
869// }
Evan Millar66210452009-09-21 10:53:43 -0700870
Evan Millar11d628c2009-09-02 08:55:01 -0700871 for (NamedContentValues subValue : entity.getSubValues()) {
872 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800873
Jeff Sharkeyab066932009-09-21 09:55:30 -0700874 final ContentValues entryValues = subValue.values;
875 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
876
Evan Millar11d628c2009-09-02 08:55:01 -0700877 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700878 if (mimetype == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -0700879
Jeff Sharkeyab066932009-09-21 09:55:30 -0700880 final DataKind kind = sources.getKindOrFallback(accountType, mimetype, this,
Evan Millar11d628c2009-09-02 08:55:01 -0700881 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700882 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800883
Evan Millar11d628c2009-09-02 08:55:01 -0700884 final long id = entryValues.getAsLong(Data._ID);
885 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700886 entry.contactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700887 entry.id = id;
888 entry.uri = uri;
889 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -0700890 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -0700891 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700892 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700893 entry.type = entryValues.getAsInteger(kind.typeColumn);
894 }
895 if (kind.iconRes > 0) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700896 entry.resPackageName = kind.resPackageName;
Evan Millar11d628c2009-09-02 08:55:01 -0700897 entry.actionIcon = kind.iconRes;
898 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800899
Evan Millar11d628c2009-09-02 08:55:01 -0700900 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700901 if (TextUtils.isEmpty(entry.data)) {
Alex Kennberg87fc3172009-03-28 06:43:06 -0700902 continue;
903 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700904
Evan Millar11d628c2009-09-02 08:55:01 -0700905 if (!mRawContactIds.contains(entry.contactId)) {
906 mRawContactIds.add(entry.contactId);
907 }
908
Evan Millar11d628c2009-09-02 08:55:01 -0700909 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)
910 || CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)
911 || CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)
912 || CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
913 final boolean isSuperPrimary = entryValues.getAsInteger(
914 Data.IS_SUPER_PRIMARY) != 0;
915
916 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
917 // Build phone entries
918 mNumPhoneNumbers++;
919
920 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
921 Uri.fromParts("tel", entry.data, null));
922 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
923 Uri.fromParts("sms", entry.data, null));
924 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700925
926 // If data is empty, don't show it.
927 if (TextUtils.isEmpty(entry.data)) {
928 Log.w(TAG, "empty data for contact method " + id);
929 continue;
930 }
931
Evan Millar11d628c2009-09-02 08:55:01 -0700932 entry.isPrimary = isSuperPrimary;
933 mPhoneEntries.add(entry);
934
935 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
936 || mShowSmsLinksForAllPhones) {
937 // Add an SMS entry
938 if (kind.iconAltRes > 0) {
939 entry.secondaryActionIcon = kind.iconAltRes;
940 }
941 }
942 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
943 // Build email entries
944 entry.intent = new Intent(Intent.ACTION_SENDTO,
945 Uri.fromParts("mailto", entry.data, null));
946 entry.isPrimary = isSuperPrimary;
947 mEmailEntries.add(entry);
948 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
949 equals(mimetype)) {
950 // Build postal entries
951 entry.maxLines = 4;
952 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
953 mPostalEntries.add(entry);
954 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
955 // Build im entries
956 Object protocolObj = entryValues.getAsInteger(Data.DATA5);
957 String host = null;
958
Evan Millar49714ee2009-09-02 16:42:47 -0700959 if (TextUtils.isEmpty(entry.label)) {
Jeff Sharkeyae6c2882009-09-21 23:09:35 -0700960 entry.label = getString(R.string.chat).toLowerCase();
Evan Millar49714ee2009-09-02 16:42:47 -0700961 }
962
Evan Millar11d628c2009-09-02 08:55:01 -0700963 if (protocolObj instanceof Number) {
964 int protocol = ((Number) protocolObj).intValue();
965 host = ContactsUtils.lookupProviderNameFromId(
966 protocol).toLowerCase();
967 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
968 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
969 entry.maxLabelLines = 2;
970 }
971 } else if (protocolObj != null) {
972 String providerName = (String) protocolObj;
973 host = providerName.toLowerCase();
974 }
975
976 // Only add the intent if there is a valid host
977 if (!TextUtils.isEmpty(host)) {
978 entry.intent = new Intent(Intent.ACTION_SENDTO,
979 constructImToUrl(host, entry.data));
980 }
981 //TODO(emillar) Add in presence info
982 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
983 entry.presenceIcon = Presence.getPresenceIconResourceId(
984 aggCursor.getInt(METHODS_STATUS_COLUMN));
985 entry.status = ...
986 }*/
987 mImEntries.add(entry);
988 }
989 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
990 // Build organization entries
991 mOrganizationEntries.add(entry);
992 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
993 // Build note entries
994 entry.id = 0;
995 entry.uri = null;
996 entry.intent = null;
997 entry.maxLines = 10;
998 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700999 } else {
1000 // Handle showing custom
1001 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
1002 mOtherEntries.add(entry);
Evan Millar11d628c2009-09-02 08:55:01 -07001003 }
1004
Evan Millar11d628c2009-09-02 08:55:01 -07001005 // TODO(emillar) Add group entries
1006 // // Build the group entries
1007 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1008 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1009 // null, null, Groups.DEFAULT_SORT_ORDER);
1010 // if (groupCursor != null) {
1011 // try {
1012 // StringBuilder sb = new StringBuilder();
1013 //
1014 // while (groupCursor.moveToNext()) {
1015 // String systemId = groupCursor.getString(
1016 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1017 //
1018 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1019 // continue;
1020 // }
1021 //
1022 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1023 // if (!TextUtils.isEmpty(name)) {
1024 // if (sb.length() == 0) {
1025 // sb.append(name);
1026 // } else {
1027 // sb.append(getString(R.string.group_list, name));
1028 // }
1029 // }
1030 // }
1031 //
1032 // if (sb.length() > 0) {
1033 // ViewEntry entry = new ViewEntry();
1034 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1035 // entry.label = getString(R.string.label_groups);
1036 // entry.data = sb.toString();
1037 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1038 //
1039 // // TODO: Add an icon for the groups item.
1040 //
1041 // mGroupEntries.add(entry);
1042 // }
1043 // } finally {
1044 // groupCursor.close();
1045 // }
1046 // }
Evan Millar66388be2009-05-28 15:41:07 -07001047 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001048
Evan Millar66388be2009-05-28 15:41:07 -07001049 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001050 }
1051 }
1052
Evan Millar11d628c2009-09-02 08:55:01 -07001053 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1054 if (kind.actionHeader == null) {
1055 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001056 }
Evan Millar11d628c2009-09-02 08:55:01 -07001057 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1058 if (actionHeader == null) {
1059 return null;
1060 }
1061 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1062 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001063
Evan Millar11d628c2009-09-02 08:55:01 -07001064 String buildDataString(DataKind kind, ContentValues values) {
1065 if (kind.actionBody == null) {
1066 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001067 }
Evan Millar11d628c2009-09-02 08:55:01 -07001068 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1069 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001070 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001071
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001072 /**
1073 * A basic structure with the data for a contact entry in the list.
1074 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001075 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001076 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001077 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001078 public boolean isPrimary = false;
1079 public int presenceIcon = -1;
1080 public int secondaryActionIcon = -1;
1081 public Intent intent;
1082 public Intent secondaryIntent = null;
1083 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001084 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001085 public ArrayList<Long> ids = new ArrayList<Long>();
1086 public int collapseCount = 0;
1087
1088 public boolean collapseWith(ViewEntry entry) {
1089 // assert equal collapse keys
1090 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1091 return false;
1092 }
1093
1094 // Choose the label associated with the highest type precedence.
1095 if (TypePrecedence.getTypePrecedence(mimetype, type)
1096 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1097 type = entry.type;
1098 label = entry.label;
1099 }
1100
1101 // Choose the max of the maxLines and maxLabelLines values.
1102 maxLines = Math.max(maxLines, entry.maxLines);
1103 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1104
1105 // Choose the presence with the highest precedence.
1106 if (Presence.getPresencePrecedence(status)
1107 < Presence.getPresencePrecedence(entry.status)) {
1108 status = entry.status;
1109 }
1110
1111 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001112 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001113
1114 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1115
1116 // Keep track of all the ids that have been collapsed with this one.
1117 ids.add(entry.id);
1118 collapseCount++;
1119 return true;
1120 }
1121
1122 public String getCollapseKey() {
1123 StringBuilder hashSb = new StringBuilder();
1124 hashSb.append(data);
1125 hashSb.append(mimetype);
1126 hashSb.append((intent != null && intent.getAction() != null)
1127 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001128 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1129 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001130 hashSb.append(actionIcon);
1131 return hashSb.toString();
1132 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001133 }
1134
Evan Millar15e514d2009-08-04 10:14:57 -07001135 /** Cache of the children views of a row */
1136 static class ViewCache {
1137 public TextView label;
1138 public TextView data;
1139 public ImageView actionIcon;
1140 public ImageView presenceIcon;
1141 public ImageView primaryIcon;
1142 public ImageView secondaryActionButton;
1143 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001144
Evan Millar15e514d2009-08-04 10:14:57 -07001145 // Need to keep track of this too
1146 ViewEntry entry;
1147 }
1148
1149 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1150 implements View.OnClickListener {
1151
Evan Millar5c22c3b2009-05-29 11:37:54 -07001152
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001153 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1154 super(context, sections, SHOW_SEPARATORS);
1155 }
1156
Evan Millar15e514d2009-08-04 10:14:57 -07001157 public void onClick(View v) {
1158 Intent intent = (Intent) v.getTag();
1159 startActivity(intent);
1160 }
1161
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001162 @Override
1163 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001164 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001165 View v;
1166
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001167 ViewCache views;
1168
1169 // Check to see if we can reuse convertView
1170 if (convertView != null) {
1171 v = convertView;
1172 views = (ViewCache) v.getTag();
1173 } else {
1174 // Create a new view if needed
1175 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1176
1177 // Cache the children
1178 views = new ViewCache();
1179 views.label = (TextView) v.findViewById(android.R.id.text1);
1180 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001181 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1182 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1183 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1184 views.secondaryActionButton = (ImageView) v.findViewById(
1185 R.id.secondary_action_button);
1186 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001187 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001188 v.setTag(views);
1189 }
1190
1191 // Update the entry in the view cache
1192 views.entry = entry;
1193
1194 // Bind the data to the view
1195 bindView(v, entry);
1196 return v;
1197 }
1198
1199 @Override
1200 protected View newView(int position, ViewGroup parent) {
1201 // getView() handles this
1202 throw new UnsupportedOperationException();
1203 }
1204
1205 @Override
1206 protected void bindView(View view, ViewEntry entry) {
1207 final Resources resources = mContext.getResources();
1208 ViewCache views = (ViewCache) view.getTag();
1209
1210 // Set the label
1211 TextView label = views.label;
1212 setMaxLines(label, entry.maxLabelLines);
1213 label.setText(entry.label);
1214
1215 // Set the data
1216 TextView data = views.data;
1217 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001218 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001219 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001220 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1221 } else {
1222 data.setText(entry.data);
1223 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001224 setMaxLines(data, entry.maxLines);
1225 }
1226
Evan Millar15e514d2009-08-04 10:14:57 -07001227 // Set the primary icon
1228 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1229
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001230 // Set the action icon
1231 ImageView action = views.actionIcon;
1232 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001233 Drawable actionIcon;
1234 if (entry.resPackageName != null) {
1235 // Load external resources through PackageManager
1236 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1237 entry.actionIcon, null);
1238 } else {
1239 actionIcon = resources.getDrawable(entry.actionIcon);
1240 }
1241 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001242 action.setVisibility(View.VISIBLE);
1243 } else {
1244 // Things should still line up as if there was an icon, so make it invisible
1245 action.setVisibility(View.INVISIBLE);
1246 }
1247
1248 // Set the presence icon
1249 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001250 if (entry.presenceIcon != -1) {
1251 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001252 } else if (entry.status != -1) {
1253 presenceIcon = resources.getDrawable(
1254 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001255 }
Evan Millar15e514d2009-08-04 10:14:57 -07001256 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001257 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001258 presenceIconView.setImageDrawable(presenceIcon);
1259 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001260 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001261 presenceIconView.setVisibility(View.GONE);
1262 }
1263
1264 // Set the secondary action button
1265 ImageView secondaryActionView = views.secondaryActionButton;
1266 Drawable secondaryActionIcon = null;
1267 if (entry.secondaryActionIcon != -1) {
1268 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1269 }
1270 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1271 secondaryActionView.setImageDrawable(secondaryActionIcon);
1272 secondaryActionView.setTag(entry.secondaryIntent);
1273 secondaryActionView.setVisibility(View.VISIBLE);
1274 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1275 } else {
1276 secondaryActionView.setVisibility(View.GONE);
1277 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001278 }
1279 }
1280
1281 private void setMaxLines(TextView textView, int maxLines) {
1282 if (maxLines == 1) {
1283 textView.setSingleLine(true);
1284 textView.setEllipsize(TextUtils.TruncateAt.END);
1285 } else {
1286 textView.setSingleLine(false);
1287 textView.setMaxLines(maxLines);
1288 textView.setEllipsize(null);
1289 }
1290 }
1291 }
1292}