blob: 76c7b7e2e86586c25e0307fa01ce4e143b47c325 [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 Millar11d628c2009-09-02 08:55:01 -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);
204 mTabWidget.setTabSelectionListener(this);
205 mTabWidget.setVisibility(View.INVISIBLE);
206
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);
257 mSelectedRawContactId = savedInstanceState.getLong(SELECTED_RAW_CONTACT_ID_KEY);
258 }
259
260 @Override
261 protected void onSaveInstanceState(Bundle outState) {
262 super.onSaveInstanceState(outState);
263 outState.putLong(SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
264 }
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 */
289 protected void bindTabs() {
290 if (mEntities.size() > 1) {
Evan Millar7911ff52009-07-21 15:55:18 -0700291 addAllTab();
292 }
Evan Millar11d628c2009-09-02 08:55:01 -0700293
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);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700305 addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(),
306 source));
Evan Millar11d628c2009-09-02 08:55:01 -0700307 }
Evan Millar11d628c2009-09-02 08:55:01 -0700308 }
309
310 /**
311 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
312 *
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700313 * @param rawContactId The contact id associated with the tab.
Evan Millar11d628c2009-09-02 08:55:01 -0700314 * @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);
Evan Millar66210452009-09-21 10:53:43 -0700340 mTabWidget.setVisibility(View.VISIBLE);
341 mTabWidget.postInvalidate();
Evan Millar11d628c2009-09-02 08:55:01 -0700342 }
343
Evan Millar7911ff52009-07-21 15:55:18 -0700344 private void addAllTab() {
Evan Millar56d2caa2009-08-20 20:30:12 -0700345 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator,
346 mTabWidget.getTabParent(), false);
Evan Millar74660912009-08-19 17:36:33 -0700347 allTabIndicator.getBackground().setDither(true);
Evan Millar7911ff52009-07-21 15:55:18 -0700348 addTab(ALL_CONTACTS_ID, allTabIndicator);
349 }
350
351 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
Evan Millar66210452009-09-21 10:53:43 -0700352 Long rawContactId = getTabRawContactId(tabIndex);
353 if (rawContactId != null) {
354 mSelectedRawContactId = rawContactId;
355 bindData();
356 }
Evan Millar7911ff52009-07-21 15:55:18 -0700357 }
358
Evan Millar11d628c2009-09-02 08:55:01 -0700359 /**
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 */
Evan Millar66210452009-09-21 10:53:43 -0700365 protected Long getTabRawContactId(int index) {
Evan Millar11d628c2009-09-02 08:55:01 -0700366 return mTabRawContactIdMap.get(index);
367 }
Evan Millar2c1cc832009-07-13 11:08:06 -0700368
Evan Millar11d628c2009-09-02 08:55:01 -0700369 /**
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;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800380 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800381 }
Evan Millar11d628c2009-09-02 08:55:01 -0700382 return -1;
383 }
384
385
386 // QUERY CODE //
387 /** {@inheritDoc} */
388 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
389 try{
390 if (token == TOKEN_QUERY) {
391 clearCurrentTabs();
392 mEntities = readEntities(iterator);
393 bindTabs();
Evan Millar66210452009-09-21 10:53:43 -0700394 selectInitialTab();
Evan Millar11d628c2009-09-02 08:55:01 -0700395 }
396 } finally {
397 if (iterator != null) {
398 iterator.close();
399 }
400 }
401 }
402
403 /** {@inheritDoc} */
404 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700405 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700406 }
407
408 private ArrayList<Entity> readEntities(EntityIterator iterator) {
409 ArrayList<Entity> entities = new ArrayList<Entity>();
410 try {
411 while (iterator.hasNext()) {
412 entities.add(iterator.next());
413 }
414 } catch (RemoteException e) {
415 }
416
417 return entities;
418 }
419
420 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700421 closeCursor();
422
Dmitri Plotnikov0306bbd2009-09-03 14:37:15 -0700423 mUri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700424 if (mLookupUri != null) {
425 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
426 if (mLookupUri != null) {
427 mUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
428 }
429 }
430
431 if (mUri == null) {
432
433 // TODO either figure out a way to prevent a flash of black background or
434 // use some other UI than a toast
435 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
436 Log.e(TAG, "invalid contact uri: " + mLookupUri);
437 finish();
438 return;
439 }
440
441 mCursor = mResolver.query(Uri.withAppendedPath(mUri, Contacts.Data.CONTENT_DIRECTORY),
442 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
443 mCursor.registerContentObserver(mObserver);
444
Evan Millar11d628c2009-09-02 08:55:01 -0700445 long contactId = ContentUris.parseId(mUri);
446 mHandler.startQueryEntities(TOKEN_QUERY, null,
447 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700448
Evan Millar9b1a1242009-09-16 10:26:10 -0700449 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700450 }
451
452 private void closeCursor() {
453 if (mCursor != null) {
454 mCursor.unregisterContentObserver(mObserver);
455 mCursor.close();
456 mCursor = null;
457 }
Evan Millar11d628c2009-09-02 08:55:01 -0700458 }
459
460 private void bindData() {
461
462 // Build up the contact entries
463 buildEntries();
464
465 // Collapse similar data items in select sections.
466 Collapser.collapseList(mPhoneEntries);
467 Collapser.collapseList(mSmsEntries);
468 Collapser.collapseList(mEmailEntries);
469 Collapser.collapseList(mPostalEntries);
470
471 if (mAdapter == null) {
472 mAdapter = new ViewAdapter(this, mSections);
473 mListView.setAdapter(mAdapter);
474 } else {
475 mAdapter.setSections(mSections, SHOW_SEPARATORS);
476 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800477 }
478
479 @Override
480 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700481 super.onCreateOptionsMenu(menu);
482
483 final MenuInflater inflater = getMenuInflater();
484 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800485 return true;
486 }
487
488 @Override
489 public boolean onPrepareOptionsMenu(Menu menu) {
490 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700491
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700492 // Only allow edit if we have a selected tab
493 final boolean contactSelected = (mSelectedRawContactId != null);
494 menu.findItem(R.id.menu_edit).setEnabled(contactSelected);
Evan Millardb5d88c2009-08-28 09:31:57 -0700495
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700496 // Only allow split when more than one contact
497 final boolean isAggregate = (mRawContactIds.size() > 1);
498 menu.findItem(R.id.menu_split).setEnabled(isAggregate);
499
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800500 return true;
501 }
502
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800503 @Override
504 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
505 AdapterView.AdapterContextMenuInfo info;
506 try {
507 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
508 } catch (ClassCastException e) {
509 Log.e(TAG, "bad menuInfo", e);
510 return;
511 }
512
513 // This can be null sometimes, don't crash...
514 if (info == null) {
515 Log.e(TAG, "bad menuInfo");
516 return;
517 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700518
Evan Millar45e0ed32009-06-01 16:44:38 -0700519 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
520 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
521 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700522 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
523 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700524 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800525 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700526 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
527 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700528 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700529 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800530 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700531 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700532 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
533 }
534 // TODO(emillar): add back with group support.
535 /* else if (entry.mimetype.equals()) {
536 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
537 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800538 }
539
540 @Override
541 public boolean onOptionsItemSelected(MenuItem item) {
542 switch (item.getItemId()) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700543 case R.id.menu_edit: {
Evan Millardb5d88c2009-08-28 09:31:57 -0700544 Long rawContactIdToEdit = mSelectedRawContactId;
545 if (rawContactIdToEdit == null) {
546 // This shouldn't be possible. We only show the edit option if
547 // this value is non-null.
548 break;
549 }
Evan Millar8a79cee2009-08-19 17:20:49 -0700550 if (rawContactIdToEdit == ALL_CONTACTS_ID) {
551 // If the "all" tab is selected, edit the next tab.
Evan Millardb5d88c2009-08-28 09:31:57 -0700552 rawContactIdToEdit = getTabRawContactId(mTabWidget.getCurrentTab() + 1);
Evan Millar8a79cee2009-08-19 17:20:49 -0700553 }
554 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
555 rawContactIdToEdit);
556 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
557 REQUEST_EDIT_CONTACT);
558 break;
559 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700560 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800561 // Get confirmation
562 showDialog(DIALOG_CONFIRM_DELETE);
563 return true;
564 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700565 case R.id.menu_split: {
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700566 if (mRawContactIds.size() == 2) {
567 splitContact(mRawContactIds.get(1));
568 } else {
569 showSplitAggregateDialog();
570 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700571 return true;
572 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700573 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700574 showJoinAggregateActivity();
575 return true;
576 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700577 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700578 showOptionsActivity();
579 return true;
580 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700581 case R.id.menu_share: {
582 final Intent intent = new Intent(Intent.ACTION_SEND);
583 intent.setType(Contacts.CONTENT_ITEM_TYPE);
584 intent.putExtra(Intent.EXTRA_STREAM, mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700585
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700586 // Launch chooser to share contact via
587 final CharSequence chooseTitle = getText(R.string.share_via);
588 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700589
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700590 try {
591 startActivity(chooseIntent);
592 } catch (ActivityNotFoundException ex) {
593 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800594 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700595 return true;
596 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800597 }
598 return super.onOptionsItemSelected(item);
599 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700600
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800601 @Override
602 public boolean onContextItemSelected(MenuItem item) {
603 switch (item.getItemId()) {
604 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700605 if (makeItemDefault(item)) {
606 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800607 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700608 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800609 }
610 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700611
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800612 return super.onContextItemSelected(item);
613 }
614
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700615 private boolean makeItemDefault(MenuItem item) {
616 ViewEntry entry = getViewEntryForMenuItem(item);
617 if (entry == null) {
618 return false;
619 }
620
621 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700622 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700623 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700624 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
625 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700626 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700627 return true;
628 }
629
630 /**
631 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
632 * The user picks a contact to be split into its own aggregate or clicks Cancel.
633 */
634 private void showSplitAggregateDialog() {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700635 // Wrap this dialog in a specific theme so that list items have correct text color.
636 final ContextThemeWrapper dialogContext =
637 new ContextThemeWrapper(this, android.R.style.Theme_Light);
638 AlertDialog.Builder builder =
639 new AlertDialog.Builder(dialogContext);
640 builder.setTitle(getString(R.string.splitAggregate_title));
641
642 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
643 builder.setView(view);
644
645 builder.setInverseBackgroundForced(true);
646 builder.setCancelable(true);
647 builder.setNegativeButton(android.R.string.cancel,
648 new OnClickListener() {
649 public void onClick(DialogInterface dialog, int which) {
650 dialog.dismiss();
651 }
652 });
653 final AlertDialog dialog = builder.create();
654
655 view.setOnContactSelectedListener(new OnContactSelectedListener() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700656 public void onContactSelected(long rawContactId) {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700657 dialog.dismiss();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700658 splitContact(rawContactId);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700659 }
660 });
661
662 dialog.show();
663 }
664
665 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700666 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700667 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700668 public void showJoinAggregateActivity() {
669 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
670 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700671 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700672 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700673
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700674 @Override
675 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700676 switch (requestCode) {
677 case REQUEST_JOIN_CONTACT: {
678 if (resultCode == RESULT_OK && intent != null) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700679 final long contactId = ContentUris.parseId(intent.getData());
680 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700681 }
682 break;
683 }
684 case REQUEST_EDIT_CONTACT: {
685 if (resultCode == RESULT_OK && intent != null) {
686 long newInitialSelectedRawContactId = intent.getLongExtra(
687 RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
688 if (newInitialSelectedRawContactId != mSelectedRawContactId) {
689 mSelectedRawContactId = newInitialSelectedRawContactId;
Evan Millar8a79cee2009-08-19 17:20:49 -0700690 }
691 }
692 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700693 }
694 }
695
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700696 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700697 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700698
699 // The split operation may have removed the original aggregate contact, so we need
700 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700701 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700702 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700703 }
704
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700705 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700706 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700707 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700708
709 try {
710 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700711 long rawContactId = c.getLong(0);
712 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700713 }
714 } finally {
715 c.close();
716 }
717
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700718 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
719 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700720 }
721
722 /**
723 * Given a contact ID sets an aggregation exception to either join the contact with the
724 * current aggregate or split off.
725 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700726 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700727 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700728 for (long aRawContactId : mRawContactIds) {
729 if (aRawContactId != rawContactId) {
730 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
731 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
732 values.put(AggregationExceptions.TYPE, exceptionType);
733 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
734 }
735 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700736 }
737
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700738 private void showOptionsActivity() {
739 final Intent intent = new Intent(this, ContactOptionsActivity.class);
740 intent.setData(mUri);
741 startActivity(intent);
742 }
743
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700744 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
745 AdapterView.AdapterContextMenuInfo info;
746 try {
747 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
748 } catch (ClassCastException e) {
749 Log.e(TAG, "bad menuInfo", e);
750 return null;
751 }
752
753 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
754 }
755
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800756 @Override
757 public boolean onKeyDown(int keyCode, KeyEvent event) {
758 switch (keyCode) {
759 case KeyEvent.KEYCODE_CALL: {
760 try {
761 ITelephony phone = ITelephony.Stub.asInterface(
762 ServiceManager.checkService("phone"));
763 if (phone != null && !phone.isIdle()) {
764 // Skip out and let the key be handled at a higher level
765 break;
766 }
767 } catch (RemoteException re) {
768 // Fall through and try to call the contact
769 }
770
Evan Millar7911ff52009-07-21 15:55:18 -0700771 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800772 if (index != -1) {
773 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700774 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
775 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800776 }
777 } else if (mNumPhoneNumbers != 0) {
778 // There isn't anything selected, call the default number
779 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
780 startActivity(intent);
781 }
782 return true;
783 }
784
785 case KeyEvent.KEYCODE_DEL: {
786 showDialog(DIALOG_CONFIRM_DELETE);
787 return true;
788 }
789 }
790
791 return super.onKeyDown(keyCode, event);
792 }
793
Evan Millar7911ff52009-07-21 15:55:18 -0700794 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800795 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
796 if (entry != null) {
797 Intent intent = entry.intent;
798 if (intent != null) {
799 try {
800 startActivity(intent);
801 } catch (ActivityNotFoundException e) {
802 Log.e(TAG, "No activity found for intent: " + intent);
803 signalError();
804 }
805 } else {
806 signalError();
807 }
808 } else {
809 signalError();
810 }
811 }
812
813 /**
814 * Signal an error to the user via a beep, or some other method.
815 */
816 private void signalError() {
817 //TODO: implement this when we have the sonification APIs
818 }
819
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800820 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700821 // 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 -0800822 StringBuilder buf = new StringBuilder("imto://");
823 buf.append(host);
824 buf.append('/');
825 buf.append(data);
826 return Uri.parse(buf.toString());
827 }
828
829 /**
830 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700831 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800832 * @param personCursor the URI for the contact being displayed
833 */
Evan Millar11d628c2009-09-02 08:55:01 -0700834 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800835 // Clear out the old entries
836 final int numSections = mSections.size();
837 for (int i = 0; i < numSections; i++) {
838 mSections.get(i).clear();
839 }
840
Evan Millar7911ff52009-07-21 15:55:18 -0700841 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700842
Evan Millar11d628c2009-09-02 08:55:01 -0700843 Sources sources = Sources.getInstance(this);
844
Evan Millar66388be2009-05-28 15:41:07 -0700845 // Build up method entries
846 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700847 for (Entity entity: mEntities) {
848 final ContentValues entValues = entity.getEntityValues();
849 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700850 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700851
Evan Millar66210452009-09-21 10:53:43 -0700852 // This performs the tab filtering
853 if (mSelectedRawContactId != null
Jeff Sharkeyab066932009-09-21 09:55:30 -0700854 && mSelectedRawContactId != rawContactId
Evan Millar66210452009-09-21 10:53:43 -0700855 && mSelectedRawContactId != ALL_CONTACTS_ID) {
856 continue;
857 }
858
Evan Millar11d628c2009-09-02 08:55:01 -0700859 for (NamedContentValues subValue : entity.getSubValues()) {
860 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800861
Jeff Sharkeyab066932009-09-21 09:55:30 -0700862 final ContentValues entryValues = subValue.values;
863 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
864
Evan Millar11d628c2009-09-02 08:55:01 -0700865 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700866 if (mimetype == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -0700867
Jeff Sharkeyab066932009-09-21 09:55:30 -0700868 final DataKind kind = sources.getKindOrFallback(accountType, mimetype, this,
Evan Millar11d628c2009-09-02 08:55:01 -0700869 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700870 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800871
Evan Millar11d628c2009-09-02 08:55:01 -0700872 final long id = entryValues.getAsLong(Data._ID);
873 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700874 entry.contactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700875 entry.id = id;
876 entry.uri = uri;
877 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -0700878 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -0700879 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700880 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700881 entry.type = entryValues.getAsInteger(kind.typeColumn);
882 }
883 if (kind.iconRes > 0) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700884 entry.resPackageName = kind.resPackageName;
Evan Millar11d628c2009-09-02 08:55:01 -0700885 entry.actionIcon = kind.iconRes;
886 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800887
Evan Millar11d628c2009-09-02 08:55:01 -0700888 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700889 if (TextUtils.isEmpty(entry.data)) {
Alex Kennberg87fc3172009-03-28 06:43:06 -0700890 continue;
891 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700892
Evan Millar11d628c2009-09-02 08:55:01 -0700893 if (!mRawContactIds.contains(entry.contactId)) {
894 mRawContactIds.add(entry.contactId);
895 }
896
Evan Millar11d628c2009-09-02 08:55:01 -0700897 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)
898 || CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)
899 || CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)
900 || CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
901 final boolean isSuperPrimary = entryValues.getAsInteger(
902 Data.IS_SUPER_PRIMARY) != 0;
903
904 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
905 // Build phone entries
906 mNumPhoneNumbers++;
907
908 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
909 Uri.fromParts("tel", entry.data, null));
910 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
911 Uri.fromParts("sms", entry.data, null));
912 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700913
914 // If data is empty, don't show it.
915 if (TextUtils.isEmpty(entry.data)) {
916 Log.w(TAG, "empty data for contact method " + id);
917 continue;
918 }
919
Evan Millar11d628c2009-09-02 08:55:01 -0700920 entry.isPrimary = isSuperPrimary;
921 mPhoneEntries.add(entry);
922
923 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
924 || mShowSmsLinksForAllPhones) {
925 // Add an SMS entry
926 if (kind.iconAltRes > 0) {
927 entry.secondaryActionIcon = kind.iconAltRes;
928 }
929 }
930 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
931 // Build email entries
932 entry.intent = new Intent(Intent.ACTION_SENDTO,
933 Uri.fromParts("mailto", entry.data, null));
934 entry.isPrimary = isSuperPrimary;
935 mEmailEntries.add(entry);
936 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
937 equals(mimetype)) {
938 // Build postal entries
939 entry.maxLines = 4;
940 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
941 mPostalEntries.add(entry);
942 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
943 // Build im entries
944 Object protocolObj = entryValues.getAsInteger(Data.DATA5);
945 String host = null;
946
Evan Millar49714ee2009-09-02 16:42:47 -0700947 if (TextUtils.isEmpty(entry.label)) {
Jeff Sharkeyae6c2882009-09-21 23:09:35 -0700948 entry.label = getString(R.string.chat).toLowerCase();
Evan Millar49714ee2009-09-02 16:42:47 -0700949 }
950
Evan Millar11d628c2009-09-02 08:55:01 -0700951 if (protocolObj instanceof Number) {
952 int protocol = ((Number) protocolObj).intValue();
953 host = ContactsUtils.lookupProviderNameFromId(
954 protocol).toLowerCase();
955 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
956 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
957 entry.maxLabelLines = 2;
958 }
959 } else if (protocolObj != null) {
960 String providerName = (String) protocolObj;
961 host = providerName.toLowerCase();
962 }
963
964 // Only add the intent if there is a valid host
965 if (!TextUtils.isEmpty(host)) {
966 entry.intent = new Intent(Intent.ACTION_SENDTO,
967 constructImToUrl(host, entry.data));
968 }
969 //TODO(emillar) Add in presence info
970 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
971 entry.presenceIcon = Presence.getPresenceIconResourceId(
972 aggCursor.getInt(METHODS_STATUS_COLUMN));
973 entry.status = ...
974 }*/
975 mImEntries.add(entry);
976 }
977 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)) {
978 // Build organization entries
979 mOrganizationEntries.add(entry);
980 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
981 // Build note entries
982 entry.id = 0;
983 entry.uri = null;
984 entry.intent = null;
985 entry.maxLines = 10;
986 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700987 } else {
988 // Handle showing custom
989 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
990 mOtherEntries.add(entry);
Evan Millar11d628c2009-09-02 08:55:01 -0700991 }
992
Evan Millar11d628c2009-09-02 08:55:01 -0700993 // TODO(emillar) Add group entries
994 // // Build the group entries
995 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
996 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
997 // null, null, Groups.DEFAULT_SORT_ORDER);
998 // if (groupCursor != null) {
999 // try {
1000 // StringBuilder sb = new StringBuilder();
1001 //
1002 // while (groupCursor.moveToNext()) {
1003 // String systemId = groupCursor.getString(
1004 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1005 //
1006 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1007 // continue;
1008 // }
1009 //
1010 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1011 // if (!TextUtils.isEmpty(name)) {
1012 // if (sb.length() == 0) {
1013 // sb.append(name);
1014 // } else {
1015 // sb.append(getString(R.string.group_list, name));
1016 // }
1017 // }
1018 // }
1019 //
1020 // if (sb.length() > 0) {
1021 // ViewEntry entry = new ViewEntry();
1022 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1023 // entry.label = getString(R.string.label_groups);
1024 // entry.data = sb.toString();
1025 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1026 //
1027 // // TODO: Add an icon for the groups item.
1028 //
1029 // mGroupEntries.add(entry);
1030 // }
1031 // } finally {
1032 // groupCursor.close();
1033 // }
1034 // }
Evan Millar66388be2009-05-28 15:41:07 -07001035 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001036
Evan Millar66388be2009-05-28 15:41:07 -07001037 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001038 }
1039 }
1040
Evan Millar11d628c2009-09-02 08:55:01 -07001041 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1042 if (kind.actionHeader == null) {
1043 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001044 }
Evan Millar11d628c2009-09-02 08:55:01 -07001045 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1046 if (actionHeader == null) {
1047 return null;
1048 }
1049 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1050 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001051
Evan Millar11d628c2009-09-02 08:55:01 -07001052 String buildDataString(DataKind kind, ContentValues values) {
1053 if (kind.actionBody == null) {
1054 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001055 }
Evan Millar11d628c2009-09-02 08:55:01 -07001056 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1057 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001058 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001059
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001060 /**
1061 * A basic structure with the data for a contact entry in the list.
1062 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001063 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001064 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001065 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001066 public boolean isPrimary = false;
1067 public int presenceIcon = -1;
1068 public int secondaryActionIcon = -1;
1069 public Intent intent;
1070 public Intent secondaryIntent = null;
1071 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001072 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001073 public ArrayList<Long> ids = new ArrayList<Long>();
1074 public int collapseCount = 0;
1075
1076 public boolean collapseWith(ViewEntry entry) {
1077 // assert equal collapse keys
1078 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1079 return false;
1080 }
1081
1082 // Choose the label associated with the highest type precedence.
1083 if (TypePrecedence.getTypePrecedence(mimetype, type)
1084 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1085 type = entry.type;
1086 label = entry.label;
1087 }
1088
1089 // Choose the max of the maxLines and maxLabelLines values.
1090 maxLines = Math.max(maxLines, entry.maxLines);
1091 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1092
1093 // Choose the presence with the highest precedence.
1094 if (Presence.getPresencePrecedence(status)
1095 < Presence.getPresencePrecedence(entry.status)) {
1096 status = entry.status;
1097 }
1098
1099 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001100 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001101
1102 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1103
1104 // Keep track of all the ids that have been collapsed with this one.
1105 ids.add(entry.id);
1106 collapseCount++;
1107 return true;
1108 }
1109
1110 public String getCollapseKey() {
1111 StringBuilder hashSb = new StringBuilder();
1112 hashSb.append(data);
1113 hashSb.append(mimetype);
1114 hashSb.append((intent != null && intent.getAction() != null)
1115 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001116 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1117 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001118 hashSb.append(actionIcon);
1119 return hashSb.toString();
1120 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001121 }
1122
Evan Millar15e514d2009-08-04 10:14:57 -07001123 /** Cache of the children views of a row */
1124 static class ViewCache {
1125 public TextView label;
1126 public TextView data;
1127 public ImageView actionIcon;
1128 public ImageView presenceIcon;
1129 public ImageView primaryIcon;
1130 public ImageView secondaryActionButton;
1131 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001132
Evan Millar15e514d2009-08-04 10:14:57 -07001133 // Need to keep track of this too
1134 ViewEntry entry;
1135 }
1136
1137 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1138 implements View.OnClickListener {
1139
Evan Millar5c22c3b2009-05-29 11:37:54 -07001140
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001141 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1142 super(context, sections, SHOW_SEPARATORS);
1143 }
1144
Evan Millar15e514d2009-08-04 10:14:57 -07001145 public void onClick(View v) {
1146 Intent intent = (Intent) v.getTag();
1147 startActivity(intent);
1148 }
1149
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001150 @Override
1151 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001152 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001153 View v;
1154
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001155 ViewCache views;
1156
1157 // Check to see if we can reuse convertView
1158 if (convertView != null) {
1159 v = convertView;
1160 views = (ViewCache) v.getTag();
1161 } else {
1162 // Create a new view if needed
1163 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1164
1165 // Cache the children
1166 views = new ViewCache();
1167 views.label = (TextView) v.findViewById(android.R.id.text1);
1168 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001169 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1170 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1171 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1172 views.secondaryActionButton = (ImageView) v.findViewById(
1173 R.id.secondary_action_button);
1174 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001175 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001176 v.setTag(views);
1177 }
1178
1179 // Update the entry in the view cache
1180 views.entry = entry;
1181
1182 // Bind the data to the view
1183 bindView(v, entry);
1184 return v;
1185 }
1186
1187 @Override
1188 protected View newView(int position, ViewGroup parent) {
1189 // getView() handles this
1190 throw new UnsupportedOperationException();
1191 }
1192
1193 @Override
1194 protected void bindView(View view, ViewEntry entry) {
1195 final Resources resources = mContext.getResources();
1196 ViewCache views = (ViewCache) view.getTag();
1197
1198 // Set the label
1199 TextView label = views.label;
1200 setMaxLines(label, entry.maxLabelLines);
1201 label.setText(entry.label);
1202
1203 // Set the data
1204 TextView data = views.data;
1205 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001206 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001207 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001208 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1209 } else {
1210 data.setText(entry.data);
1211 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001212 setMaxLines(data, entry.maxLines);
1213 }
1214
Evan Millar15e514d2009-08-04 10:14:57 -07001215 // Set the primary icon
1216 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1217
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001218 // Set the action icon
1219 ImageView action = views.actionIcon;
1220 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001221 Drawable actionIcon;
1222 if (entry.resPackageName != null) {
1223 // Load external resources through PackageManager
1224 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1225 entry.actionIcon, null);
1226 } else {
1227 actionIcon = resources.getDrawable(entry.actionIcon);
1228 }
1229 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001230 action.setVisibility(View.VISIBLE);
1231 } else {
1232 // Things should still line up as if there was an icon, so make it invisible
1233 action.setVisibility(View.INVISIBLE);
1234 }
1235
1236 // Set the presence icon
1237 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001238 if (entry.presenceIcon != -1) {
1239 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001240 } else if (entry.status != -1) {
1241 presenceIcon = resources.getDrawable(
1242 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001243 }
Evan Millar15e514d2009-08-04 10:14:57 -07001244 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001245 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001246 presenceIconView.setImageDrawable(presenceIcon);
1247 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001248 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001249 presenceIconView.setVisibility(View.GONE);
1250 }
1251
1252 // Set the secondary action button
1253 ImageView secondaryActionView = views.secondaryActionButton;
1254 Drawable secondaryActionIcon = null;
1255 if (entry.secondaryActionIcon != -1) {
1256 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1257 }
1258 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1259 secondaryActionView.setImageDrawable(secondaryActionIcon);
1260 secondaryActionView.setTag(entry.secondaryIntent);
1261 secondaryActionView.setVisibility(View.VISIBLE);
1262 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1263 } else {
1264 secondaryActionView.setVisibility(View.GONE);
1265 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001266 }
1267 }
1268
1269 private void setMaxLines(TextView textView, int maxLines) {
1270 if (maxLines == 1) {
1271 textView.setSingleLine(true);
1272 textView.setEllipsize(TextUtils.TruncateAt.END);
1273 } else {
1274 textView.setSingleLine(false);
1275 textView.setMaxLines(maxLines);
1276 textView.setEllipsize(null);
1277 }
1278 }
1279 }
1280}