blob: fe115757216ee3770e9beac2a2439cb54fdb6637 [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.
Evan Millarcc87fb92009-09-22 15:27:47 -0700394 boolean isAggregate = mEntities.size() > 1;
395 mContactHeaderWidget.showAggregateBadge(isAggregate);
Evan Millar85863112009-09-21 14:35:18 -0700396// bindTabs();
Evan Millar51514db2009-09-22 11:42:35 -0700397// selectInitialTab();
Evan Millar85863112009-09-21 14:35:18 -0700398 bindData();
Evan Millar11d628c2009-09-02 08:55:01 -0700399 }
400 } finally {
401 if (iterator != null) {
402 iterator.close();
403 }
404 }
405 }
406
407 /** {@inheritDoc} */
408 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700409 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700410 }
411
412 private ArrayList<Entity> readEntities(EntityIterator iterator) {
413 ArrayList<Entity> entities = new ArrayList<Entity>();
414 try {
415 while (iterator.hasNext()) {
416 entities.add(iterator.next());
417 }
418 } catch (RemoteException e) {
419 }
420
421 return entities;
422 }
423
424 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700425 closeCursor();
426
Dmitri Plotnikov0306bbd2009-09-03 14:37:15 -0700427 mUri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700428 if (mLookupUri != null) {
429 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
430 if (mLookupUri != null) {
431 mUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
432 }
433 }
434
435 if (mUri == null) {
436
437 // TODO either figure out a way to prevent a flash of black background or
438 // use some other UI than a toast
439 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
440 Log.e(TAG, "invalid contact uri: " + mLookupUri);
441 finish();
442 return;
443 }
444
445 mCursor = mResolver.query(Uri.withAppendedPath(mUri, Contacts.Data.CONTENT_DIRECTORY),
446 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
447 mCursor.registerContentObserver(mObserver);
448
Evan Millar11d628c2009-09-02 08:55:01 -0700449 long contactId = ContentUris.parseId(mUri);
450 mHandler.startQueryEntities(TOKEN_QUERY, null,
451 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700452
Evan Millar9b1a1242009-09-16 10:26:10 -0700453 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700454 }
455
456 private void closeCursor() {
457 if (mCursor != null) {
458 mCursor.unregisterContentObserver(mObserver);
459 mCursor.close();
460 mCursor = null;
461 }
Evan Millar11d628c2009-09-02 08:55:01 -0700462 }
463
464 private void bindData() {
465
466 // Build up the contact entries
467 buildEntries();
468
469 // Collapse similar data items in select sections.
470 Collapser.collapseList(mPhoneEntries);
471 Collapser.collapseList(mSmsEntries);
472 Collapser.collapseList(mEmailEntries);
473 Collapser.collapseList(mPostalEntries);
474
475 if (mAdapter == null) {
476 mAdapter = new ViewAdapter(this, mSections);
477 mListView.setAdapter(mAdapter);
478 } else {
479 mAdapter.setSections(mSections, SHOW_SEPARATORS);
480 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800481 }
482
483 @Override
484 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700485 super.onCreateOptionsMenu(menu);
486
487 final MenuInflater inflater = getMenuInflater();
488 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800489 return true;
490 }
491
492 @Override
493 public boolean onPrepareOptionsMenu(Menu menu) {
494 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700495
Evan Millar51514db2009-09-22 11:42:35 -0700496 // Only allow edit when we have at least one raw_contact id
497 final boolean hasRawContact = (mRawContactIds.size() > 0);
498 menu.findItem(R.id.menu_split).setEnabled(hasRawContact);
Evan Millardb5d88c2009-08-28 09:31:57 -0700499
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700500 // Only allow split when more than one contact
501 final boolean isAggregate = (mRawContactIds.size() > 1);
502 menu.findItem(R.id.menu_split).setEnabled(isAggregate);
503
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800504 return true;
505 }
506
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800507 @Override
508 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
509 AdapterView.AdapterContextMenuInfo info;
510 try {
511 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
512 } catch (ClassCastException e) {
513 Log.e(TAG, "bad menuInfo", e);
514 return;
515 }
516
517 // This can be null sometimes, don't crash...
518 if (info == null) {
519 Log.e(TAG, "bad menuInfo");
520 return;
521 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700522
Evan Millar45e0ed32009-06-01 16:44:38 -0700523 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
524 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
525 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700526 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
527 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700528 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800529 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700530 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
531 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700532 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700533 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800534 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700535 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700536 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
537 }
538 // TODO(emillar): add back with group support.
539 /* else if (entry.mimetype.equals()) {
540 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
541 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800542 }
543
544 @Override
545 public boolean onOptionsItemSelected(MenuItem item) {
546 switch (item.getItemId()) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700547 case R.id.menu_edit: {
Evan Millar51514db2009-09-22 11:42:35 -0700548// Long rawContactIdToEdit = mSelectedRawContactId;
549// if (rawContactIdToEdit == null) {
550// // This shouldn't be possible. We only show the edit option if
551// // this value is non-null.
552// break;
553// }
554// if (rawContactIdToEdit == ALL_CONTACTS_ID) {
555// // If the "all" tab is selected, edit the next tab.
556// rawContactIdToEdit = getTabRawContactId(mTabWidget.getCurrentTab() + 1);
557// }
558// Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
559// rawContactIdToEdit);
560// startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
561// REQUEST_EDIT_CONTACT);
562 if (mRawContactIds.size() > 0) {
563 long rawContactIdToEdit = mRawContactIds.get(0);
564 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
565 rawContactIdToEdit);
566 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
567 REQUEST_EDIT_CONTACT);
Evan Millardb5d88c2009-08-28 09:31:57 -0700568 }
Evan Millar8a79cee2009-08-19 17:20:49 -0700569 break;
570 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700571 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800572 // Get confirmation
573 showDialog(DIALOG_CONFIRM_DELETE);
574 return true;
575 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700576 case R.id.menu_split: {
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700577 if (mRawContactIds.size() == 2) {
578 splitContact(mRawContactIds.get(1));
579 } else {
580 showSplitAggregateDialog();
581 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700582 return true;
583 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700584 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700585 showJoinAggregateActivity();
586 return true;
587 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700588 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700589 showOptionsActivity();
590 return true;
591 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700592 case R.id.menu_share: {
593 final Intent intent = new Intent(Intent.ACTION_SEND);
594 intent.setType(Contacts.CONTENT_ITEM_TYPE);
595 intent.putExtra(Intent.EXTRA_STREAM, mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700596
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700597 // Launch chooser to share contact via
598 final CharSequence chooseTitle = getText(R.string.share_via);
599 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700600
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700601 try {
602 startActivity(chooseIntent);
603 } catch (ActivityNotFoundException ex) {
604 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800605 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700606 return true;
607 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800608 }
609 return super.onOptionsItemSelected(item);
610 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700611
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800612 @Override
613 public boolean onContextItemSelected(MenuItem item) {
614 switch (item.getItemId()) {
615 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700616 if (makeItemDefault(item)) {
617 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800618 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700619 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800620 }
621 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700622
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800623 return super.onContextItemSelected(item);
624 }
625
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700626 private boolean makeItemDefault(MenuItem item) {
627 ViewEntry entry = getViewEntryForMenuItem(item);
628 if (entry == null) {
629 return false;
630 }
631
632 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700633 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700634 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700635 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
636 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700637 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700638 return true;
639 }
640
641 /**
642 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
643 * The user picks a contact to be split into its own aggregate or clicks Cancel.
644 */
645 private void showSplitAggregateDialog() {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700646 // Wrap this dialog in a specific theme so that list items have correct text color.
647 final ContextThemeWrapper dialogContext =
648 new ContextThemeWrapper(this, android.R.style.Theme_Light);
649 AlertDialog.Builder builder =
650 new AlertDialog.Builder(dialogContext);
651 builder.setTitle(getString(R.string.splitAggregate_title));
652
653 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
654 builder.setView(view);
655
656 builder.setInverseBackgroundForced(true);
657 builder.setCancelable(true);
658 builder.setNegativeButton(android.R.string.cancel,
659 new OnClickListener() {
660 public void onClick(DialogInterface dialog, int which) {
661 dialog.dismiss();
662 }
663 });
664 final AlertDialog dialog = builder.create();
665
666 view.setOnContactSelectedListener(new OnContactSelectedListener() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700667 public void onContactSelected(long rawContactId) {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700668 dialog.dismiss();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700669 splitContact(rawContactId);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700670 }
671 });
672
673 dialog.show();
674 }
675
676 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700677 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700678 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700679 public void showJoinAggregateActivity() {
680 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
681 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700682 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700683 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700684
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700685 @Override
686 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700687 switch (requestCode) {
688 case REQUEST_JOIN_CONTACT: {
689 if (resultCode == RESULT_OK && intent != null) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700690 final long contactId = ContentUris.parseId(intent.getData());
691 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700692 }
693 break;
694 }
Evan Millar51514db2009-09-22 11:42:35 -0700695// case REQUEST_EDIT_CONTACT: {
696// if (resultCode == RESULT_OK && intent != null) {
697// long newInitialSelectedRawContactId = intent.getLongExtra(
698// RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
699// if (newInitialSelectedRawContactId != mSelectedRawContactId) {
700// mSelectedRawContactId = newInitialSelectedRawContactId;
701// }
702// }
703// }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700704 }
705 }
706
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700707 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700708 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700709
710 // The split operation may have removed the original aggregate contact, so we need
711 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700712 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700713 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700714 }
715
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700716 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700717 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700718 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700719
720 try {
721 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700722 long rawContactId = c.getLong(0);
723 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700724 }
725 } finally {
726 c.close();
727 }
728
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700729 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
730 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700731 }
732
733 /**
734 * Given a contact ID sets an aggregation exception to either join the contact with the
735 * current aggregate or split off.
736 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700737 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700738 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700739 for (long aRawContactId : mRawContactIds) {
740 if (aRawContactId != rawContactId) {
741 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
742 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
743 values.put(AggregationExceptions.TYPE, exceptionType);
744 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
745 }
746 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700747 }
748
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700749 private void showOptionsActivity() {
750 final Intent intent = new Intent(this, ContactOptionsActivity.class);
751 intent.setData(mUri);
752 startActivity(intent);
753 }
754
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700755 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
756 AdapterView.AdapterContextMenuInfo info;
757 try {
758 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
759 } catch (ClassCastException e) {
760 Log.e(TAG, "bad menuInfo", e);
761 return null;
762 }
763
764 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
765 }
766
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800767 @Override
768 public boolean onKeyDown(int keyCode, KeyEvent event) {
769 switch (keyCode) {
770 case KeyEvent.KEYCODE_CALL: {
771 try {
772 ITelephony phone = ITelephony.Stub.asInterface(
773 ServiceManager.checkService("phone"));
774 if (phone != null && !phone.isIdle()) {
775 // Skip out and let the key be handled at a higher level
776 break;
777 }
778 } catch (RemoteException re) {
779 // Fall through and try to call the contact
780 }
781
Evan Millar7911ff52009-07-21 15:55:18 -0700782 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800783 if (index != -1) {
784 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700785 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
786 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800787 }
788 } else if (mNumPhoneNumbers != 0) {
789 // There isn't anything selected, call the default number
790 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
791 startActivity(intent);
792 }
793 return true;
794 }
795
796 case KeyEvent.KEYCODE_DEL: {
797 showDialog(DIALOG_CONFIRM_DELETE);
798 return true;
799 }
800 }
801
802 return super.onKeyDown(keyCode, event);
803 }
804
Evan Millar7911ff52009-07-21 15:55:18 -0700805 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800806 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
807 if (entry != null) {
808 Intent intent = entry.intent;
809 if (intent != null) {
810 try {
811 startActivity(intent);
812 } catch (ActivityNotFoundException e) {
813 Log.e(TAG, "No activity found for intent: " + intent);
814 signalError();
815 }
816 } else {
817 signalError();
818 }
819 } else {
820 signalError();
821 }
822 }
823
824 /**
825 * Signal an error to the user via a beep, or some other method.
826 */
827 private void signalError() {
828 //TODO: implement this when we have the sonification APIs
829 }
830
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800831 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700832 // 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 -0800833 StringBuilder buf = new StringBuilder("imto://");
834 buf.append(host);
835 buf.append('/');
836 buf.append(data);
837 return Uri.parse(buf.toString());
838 }
839
840 /**
841 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700842 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800843 * @param personCursor the URI for the contact being displayed
844 */
Evan Millar11d628c2009-09-02 08:55:01 -0700845 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800846 // Clear out the old entries
847 final int numSections = mSections.size();
848 for (int i = 0; i < numSections; i++) {
849 mSections.get(i).clear();
850 }
851
Evan Millar7911ff52009-07-21 15:55:18 -0700852 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700853
Evan Millar11d628c2009-09-02 08:55:01 -0700854 Sources sources = Sources.getInstance(this);
855
Evan Millar66388be2009-05-28 15:41:07 -0700856 // Build up method entries
857 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700858 for (Entity entity: mEntities) {
859 final ContentValues entValues = entity.getEntityValues();
860 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700861 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700862
Evan Millar51514db2009-09-22 11:42:35 -0700863// // This performs the tab filtering
864// if (mSelectedRawContactId != null
865// && mSelectedRawContactId != rawContactId
866// && mSelectedRawContactId != ALL_CONTACTS_ID) {
867// continue;
868// }
Evan Millar66210452009-09-21 10:53:43 -0700869
Evan Millar11d628c2009-09-02 08:55:01 -0700870 for (NamedContentValues subValue : entity.getSubValues()) {
871 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800872
Jeff Sharkeyab066932009-09-21 09:55:30 -0700873 final ContentValues entryValues = subValue.values;
874 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
875
Evan Millar11d628c2009-09-02 08:55:01 -0700876 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700877 if (mimetype == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -0700878
Jeff Sharkeyab066932009-09-21 09:55:30 -0700879 final DataKind kind = sources.getKindOrFallback(accountType, mimetype, this,
Evan Millar11d628c2009-09-02 08:55:01 -0700880 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700881 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800882
Evan Millar11d628c2009-09-02 08:55:01 -0700883 final long id = entryValues.getAsLong(Data._ID);
884 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700885 entry.contactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700886 entry.id = id;
887 entry.uri = uri;
888 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -0700889 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -0700890 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700891 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700892 entry.type = entryValues.getAsInteger(kind.typeColumn);
893 }
894 if (kind.iconRes > 0) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700895 entry.resPackageName = kind.resPackageName;
Evan Millar11d628c2009-09-02 08:55:01 -0700896 entry.actionIcon = kind.iconRes;
897 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800898
Evan Millar11d628c2009-09-02 08:55:01 -0700899 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700900 if (TextUtils.isEmpty(entry.data)) {
Alex Kennberg87fc3172009-03-28 06:43:06 -0700901 continue;
902 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700903
Evan Millar11d628c2009-09-02 08:55:01 -0700904 if (!mRawContactIds.contains(entry.contactId)) {
905 mRawContactIds.add(entry.contactId);
906 }
907
Evan Millar11d628c2009-09-02 08:55:01 -0700908 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)
909 || CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)
910 || CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)
911 || CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
912 final boolean isSuperPrimary = entryValues.getAsInteger(
913 Data.IS_SUPER_PRIMARY) != 0;
914
915 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
916 // Build phone entries
917 mNumPhoneNumbers++;
918
919 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
920 Uri.fromParts("tel", entry.data, null));
921 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
922 Uri.fromParts("sms", entry.data, null));
923 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700924
925 // If data is empty, don't show it.
926 if (TextUtils.isEmpty(entry.data)) {
927 Log.w(TAG, "empty data for contact method " + id);
928 continue;
929 }
930
Evan Millar11d628c2009-09-02 08:55:01 -0700931 entry.isPrimary = isSuperPrimary;
932 mPhoneEntries.add(entry);
933
934 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
935 || mShowSmsLinksForAllPhones) {
936 // Add an SMS entry
937 if (kind.iconAltRes > 0) {
938 entry.secondaryActionIcon = kind.iconAltRes;
939 }
940 }
941 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
942 // Build email entries
943 entry.intent = new Intent(Intent.ACTION_SENDTO,
944 Uri.fromParts("mailto", entry.data, null));
945 entry.isPrimary = isSuperPrimary;
946 mEmailEntries.add(entry);
947 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
948 equals(mimetype)) {
949 // Build postal entries
950 entry.maxLines = 4;
951 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
952 mPostalEntries.add(entry);
953 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
954 // Build im entries
955 Object protocolObj = entryValues.getAsInteger(Data.DATA5);
956 String host = null;
957
Evan Millar49714ee2009-09-02 16:42:47 -0700958 if (TextUtils.isEmpty(entry.label)) {
Jeff Sharkeyae6c2882009-09-21 23:09:35 -0700959 entry.label = getString(R.string.chat).toLowerCase();
Evan Millar49714ee2009-09-02 16:42:47 -0700960 }
961
Evan Millar11d628c2009-09-02 08:55:01 -0700962 if (protocolObj instanceof Number) {
963 int protocol = ((Number) protocolObj).intValue();
964 host = ContactsUtils.lookupProviderNameFromId(
965 protocol).toLowerCase();
966 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
967 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
968 entry.maxLabelLines = 2;
969 }
970 } else if (protocolObj != null) {
971 String providerName = (String) protocolObj;
972 host = providerName.toLowerCase();
973 }
974
975 // Only add the intent if there is a valid host
976 if (!TextUtils.isEmpty(host)) {
977 entry.intent = new Intent(Intent.ACTION_SENDTO,
978 constructImToUrl(host, entry.data));
979 }
980 //TODO(emillar) Add in presence info
981 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
982 entry.presenceIcon = Presence.getPresenceIconResourceId(
983 aggCursor.getInt(METHODS_STATUS_COLUMN));
984 entry.status = ...
985 }*/
986 mImEntries.add(entry);
987 }
988 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
989 // Build organization entries
990 mOrganizationEntries.add(entry);
991 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
992 // Build note entries
993 entry.id = 0;
994 entry.uri = null;
995 entry.intent = null;
996 entry.maxLines = 10;
997 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700998 } else {
999 // Handle showing custom
1000 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
1001 mOtherEntries.add(entry);
Evan Millar11d628c2009-09-02 08:55:01 -07001002 }
1003
Evan Millar11d628c2009-09-02 08:55:01 -07001004 // TODO(emillar) Add group entries
1005 // // Build the group entries
1006 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1007 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1008 // null, null, Groups.DEFAULT_SORT_ORDER);
1009 // if (groupCursor != null) {
1010 // try {
1011 // StringBuilder sb = new StringBuilder();
1012 //
1013 // while (groupCursor.moveToNext()) {
1014 // String systemId = groupCursor.getString(
1015 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1016 //
1017 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1018 // continue;
1019 // }
1020 //
1021 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1022 // if (!TextUtils.isEmpty(name)) {
1023 // if (sb.length() == 0) {
1024 // sb.append(name);
1025 // } else {
1026 // sb.append(getString(R.string.group_list, name));
1027 // }
1028 // }
1029 // }
1030 //
1031 // if (sb.length() > 0) {
1032 // ViewEntry entry = new ViewEntry();
1033 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1034 // entry.label = getString(R.string.label_groups);
1035 // entry.data = sb.toString();
1036 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1037 //
1038 // // TODO: Add an icon for the groups item.
1039 //
1040 // mGroupEntries.add(entry);
1041 // }
1042 // } finally {
1043 // groupCursor.close();
1044 // }
1045 // }
Evan Millar66388be2009-05-28 15:41:07 -07001046 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001047
Evan Millar66388be2009-05-28 15:41:07 -07001048 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001049 }
1050 }
1051
Evan Millar11d628c2009-09-02 08:55:01 -07001052 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1053 if (kind.actionHeader == null) {
1054 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001055 }
Evan Millar11d628c2009-09-02 08:55:01 -07001056 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1057 if (actionHeader == null) {
1058 return null;
1059 }
1060 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1061 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001062
Evan Millar11d628c2009-09-02 08:55:01 -07001063 String buildDataString(DataKind kind, ContentValues values) {
1064 if (kind.actionBody == null) {
1065 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001066 }
Evan Millar11d628c2009-09-02 08:55:01 -07001067 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1068 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001069 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001070
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001071 /**
1072 * A basic structure with the data for a contact entry in the list.
1073 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001074 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001075 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001076 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001077 public boolean isPrimary = false;
1078 public int presenceIcon = -1;
1079 public int secondaryActionIcon = -1;
1080 public Intent intent;
1081 public Intent secondaryIntent = null;
1082 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001083 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001084 public ArrayList<Long> ids = new ArrayList<Long>();
1085 public int collapseCount = 0;
1086
1087 public boolean collapseWith(ViewEntry entry) {
1088 // assert equal collapse keys
1089 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1090 return false;
1091 }
1092
1093 // Choose the label associated with the highest type precedence.
1094 if (TypePrecedence.getTypePrecedence(mimetype, type)
1095 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1096 type = entry.type;
1097 label = entry.label;
1098 }
1099
1100 // Choose the max of the maxLines and maxLabelLines values.
1101 maxLines = Math.max(maxLines, entry.maxLines);
1102 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1103
1104 // Choose the presence with the highest precedence.
1105 if (Presence.getPresencePrecedence(status)
1106 < Presence.getPresencePrecedence(entry.status)) {
1107 status = entry.status;
1108 }
1109
1110 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001111 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001112
1113 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1114
1115 // Keep track of all the ids that have been collapsed with this one.
1116 ids.add(entry.id);
1117 collapseCount++;
1118 return true;
1119 }
1120
1121 public String getCollapseKey() {
1122 StringBuilder hashSb = new StringBuilder();
1123 hashSb.append(data);
1124 hashSb.append(mimetype);
1125 hashSb.append((intent != null && intent.getAction() != null)
1126 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001127 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1128 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001129 hashSb.append(actionIcon);
1130 return hashSb.toString();
1131 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001132 }
1133
Evan Millar15e514d2009-08-04 10:14:57 -07001134 /** Cache of the children views of a row */
1135 static class ViewCache {
1136 public TextView label;
1137 public TextView data;
1138 public ImageView actionIcon;
1139 public ImageView presenceIcon;
1140 public ImageView primaryIcon;
1141 public ImageView secondaryActionButton;
1142 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001143
Evan Millar15e514d2009-08-04 10:14:57 -07001144 // Need to keep track of this too
1145 ViewEntry entry;
1146 }
1147
1148 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1149 implements View.OnClickListener {
1150
Evan Millar5c22c3b2009-05-29 11:37:54 -07001151
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001152 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1153 super(context, sections, SHOW_SEPARATORS);
1154 }
1155
Evan Millar15e514d2009-08-04 10:14:57 -07001156 public void onClick(View v) {
1157 Intent intent = (Intent) v.getTag();
1158 startActivity(intent);
1159 }
1160
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001161 @Override
1162 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001163 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001164 View v;
1165
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001166 ViewCache views;
1167
1168 // Check to see if we can reuse convertView
1169 if (convertView != null) {
1170 v = convertView;
1171 views = (ViewCache) v.getTag();
1172 } else {
1173 // Create a new view if needed
1174 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1175
1176 // Cache the children
1177 views = new ViewCache();
1178 views.label = (TextView) v.findViewById(android.R.id.text1);
1179 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001180 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1181 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1182 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1183 views.secondaryActionButton = (ImageView) v.findViewById(
1184 R.id.secondary_action_button);
1185 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001186 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001187 v.setTag(views);
1188 }
1189
1190 // Update the entry in the view cache
1191 views.entry = entry;
1192
1193 // Bind the data to the view
1194 bindView(v, entry);
1195 return v;
1196 }
1197
1198 @Override
1199 protected View newView(int position, ViewGroup parent) {
1200 // getView() handles this
1201 throw new UnsupportedOperationException();
1202 }
1203
1204 @Override
1205 protected void bindView(View view, ViewEntry entry) {
1206 final Resources resources = mContext.getResources();
1207 ViewCache views = (ViewCache) view.getTag();
1208
1209 // Set the label
1210 TextView label = views.label;
1211 setMaxLines(label, entry.maxLabelLines);
1212 label.setText(entry.label);
1213
1214 // Set the data
1215 TextView data = views.data;
1216 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001217 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001218 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001219 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1220 } else {
1221 data.setText(entry.data);
1222 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001223 setMaxLines(data, entry.maxLines);
1224 }
1225
Evan Millar15e514d2009-08-04 10:14:57 -07001226 // Set the primary icon
1227 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1228
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001229 // Set the action icon
1230 ImageView action = views.actionIcon;
1231 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001232 Drawable actionIcon;
1233 if (entry.resPackageName != null) {
1234 // Load external resources through PackageManager
1235 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1236 entry.actionIcon, null);
1237 } else {
1238 actionIcon = resources.getDrawable(entry.actionIcon);
1239 }
1240 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001241 action.setVisibility(View.VISIBLE);
1242 } else {
1243 // Things should still line up as if there was an icon, so make it invisible
1244 action.setVisibility(View.INVISIBLE);
1245 }
1246
1247 // Set the presence icon
1248 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001249 if (entry.presenceIcon != -1) {
1250 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001251 } else if (entry.status != -1) {
1252 presenceIcon = resources.getDrawable(
1253 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001254 }
Evan Millar15e514d2009-08-04 10:14:57 -07001255 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001256 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001257 presenceIconView.setImageDrawable(presenceIcon);
1258 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001259 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001260 presenceIconView.setVisibility(View.GONE);
1261 }
1262
1263 // Set the secondary action button
1264 ImageView secondaryActionView = views.secondaryActionButton;
1265 Drawable secondaryActionIcon = null;
1266 if (entry.secondaryActionIcon != -1) {
1267 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1268 }
1269 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1270 secondaryActionView.setImageDrawable(secondaryActionIcon);
1271 secondaryActionView.setTag(entry.secondaryIntent);
1272 secondaryActionView.setVisibility(View.VISIBLE);
1273 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1274 } else {
1275 secondaryActionView.setVisibility(View.GONE);
1276 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001277 }
1278 }
1279
1280 private void setMaxLines(TextView textView, int maxLines) {
1281 if (maxLines == 1) {
1282 textView.setSingleLine(true);
1283 textView.setEllipsize(TextUtils.TruncateAt.END);
1284 } else {
1285 textView.setSingleLine(false);
1286 textView.setMaxLines(maxLines);
1287 textView.setEllipsize(null);
1288 }
1289 }
1290 }
1291}