blob: be78bea80a238c7a8c84a1891b0d0d86dfcfef76 [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);
305 addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(), source));
306 }
Evan Millar11d628c2009-09-02 08:55:01 -0700307 }
308
309 /**
310 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
311 *
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700312 * @param rawContactId The contact id associated with the tab.
Evan Millar11d628c2009-09-02 08:55:01 -0700313 * @param view A view to use as the tab indicator.
314 */
315 protected void addTab(long rawContactId, View view) {
316 mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId);
317 mTabWidget.addTab(view);
318 }
319
320
321 protected void clearCurrentTabs() {
322 mTabRawContactIdMap.clear();
323 mTabWidget.removeAllTabs();
324 }
325
326 protected void selectInitialTab() {
327 int selectedTabIndex = 0;
328
329 if (mSelectedRawContactId != null) {
330 selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
331 if (selectedTabIndex == -1) {
332 // If there was no matching tab, just select the first;
333 selectedTabIndex = 0;
334 }
335 }
336
337 mTabWidget.setCurrentTab(selectedTabIndex);
338 onTabSelectionChanged(selectedTabIndex, false);
Evan Millar66210452009-09-21 10:53:43 -0700339 mTabWidget.setVisibility(View.VISIBLE);
340 mTabWidget.postInvalidate();
Evan Millar11d628c2009-09-02 08:55:01 -0700341 }
342
Evan Millar7911ff52009-07-21 15:55:18 -0700343 private void addAllTab() {
Evan Millar56d2caa2009-08-20 20:30:12 -0700344 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator,
345 mTabWidget.getTabParent(), false);
Evan Millar74660912009-08-19 17:36:33 -0700346 allTabIndicator.getBackground().setDither(true);
Evan Millar7911ff52009-07-21 15:55:18 -0700347 addTab(ALL_CONTACTS_ID, allTabIndicator);
348 }
349
350 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
Evan Millar66210452009-09-21 10:53:43 -0700351 Long rawContactId = getTabRawContactId(tabIndex);
352 if (rawContactId != null) {
353 mSelectedRawContactId = rawContactId;
354 bindData();
355 }
Evan Millar7911ff52009-07-21 15:55:18 -0700356 }
357
Evan Millar11d628c2009-09-02 08:55:01 -0700358 /**
359 * Return the RawContact id associated with the tab at an index.
360 *
361 * @param index The index of the tab in question.
362 * @return The contactId associated with the tab at the specified index.
363 */
Evan Millar66210452009-09-21 10:53:43 -0700364 protected Long getTabRawContactId(int index) {
Evan Millar11d628c2009-09-02 08:55:01 -0700365 return mTabRawContactIdMap.get(index);
366 }
Evan Millar2c1cc832009-07-13 11:08:06 -0700367
Evan Millar11d628c2009-09-02 08:55:01 -0700368 /**
369 * Return the tab index associated with the RawContact id.
370 *
371 * @param index The index of the tab in question.
372 * @return The contactId associated with the tab at the specified index.
373 */
374 protected int getTabIndexForRawContactId(long rawContactId) {
375 int numTabs = mTabRawContactIdMap.size();
376 for (int i=0; i < numTabs; i++) {
377 if (mTabRawContactIdMap.get(i) == rawContactId) {
378 return i;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800379 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800380 }
Evan Millar11d628c2009-09-02 08:55:01 -0700381 return -1;
382 }
383
384
385 // QUERY CODE //
386 /** {@inheritDoc} */
387 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
388 try{
389 if (token == TOKEN_QUERY) {
390 clearCurrentTabs();
391 mEntities = readEntities(iterator);
392 bindTabs();
Evan Millar66210452009-09-21 10:53:43 -0700393 selectInitialTab();
Evan Millar11d628c2009-09-02 08:55:01 -0700394 }
395 } finally {
396 if (iterator != null) {
397 iterator.close();
398 }
399 }
400 }
401
402 /** {@inheritDoc} */
403 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700404 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700405 }
406
407 private ArrayList<Entity> readEntities(EntityIterator iterator) {
408 ArrayList<Entity> entities = new ArrayList<Entity>();
409 try {
410 while (iterator.hasNext()) {
411 entities.add(iterator.next());
412 }
413 } catch (RemoteException e) {
414 }
415
416 return entities;
417 }
418
419 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700420 closeCursor();
421
Dmitri Plotnikov0306bbd2009-09-03 14:37:15 -0700422 mUri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700423 if (mLookupUri != null) {
424 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
425 if (mLookupUri != null) {
426 mUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
427 }
428 }
429
430 if (mUri == null) {
431
432 // TODO either figure out a way to prevent a flash of black background or
433 // use some other UI than a toast
434 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
435 Log.e(TAG, "invalid contact uri: " + mLookupUri);
436 finish();
437 return;
438 }
439
440 mCursor = mResolver.query(Uri.withAppendedPath(mUri, Contacts.Data.CONTENT_DIRECTORY),
441 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
442 mCursor.registerContentObserver(mObserver);
443
Evan Millar11d628c2009-09-02 08:55:01 -0700444 long contactId = ContentUris.parseId(mUri);
445 mHandler.startQueryEntities(TOKEN_QUERY, null,
446 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700447
Evan Millar9b1a1242009-09-16 10:26:10 -0700448 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700449 }
450
451 private void closeCursor() {
452 if (mCursor != null) {
453 mCursor.unregisterContentObserver(mObserver);
454 mCursor.close();
455 mCursor = null;
456 }
Evan Millar11d628c2009-09-02 08:55:01 -0700457 }
458
459 private void bindData() {
460
461 // Build up the contact entries
462 buildEntries();
463
464 // Collapse similar data items in select sections.
465 Collapser.collapseList(mPhoneEntries);
466 Collapser.collapseList(mSmsEntries);
467 Collapser.collapseList(mEmailEntries);
468 Collapser.collapseList(mPostalEntries);
469
470 if (mAdapter == null) {
471 mAdapter = new ViewAdapter(this, mSections);
472 mListView.setAdapter(mAdapter);
473 } else {
474 mAdapter.setSections(mSections, SHOW_SEPARATORS);
475 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800476 }
477
478 @Override
479 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700480 super.onCreateOptionsMenu(menu);
481
482 final MenuInflater inflater = getMenuInflater();
483 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800484 return true;
485 }
486
487 @Override
488 public boolean onPrepareOptionsMenu(Menu menu) {
489 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700490
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700491 // Only allow edit if we have a selected tab
492 final boolean contactSelected = (mSelectedRawContactId != null);
493 menu.findItem(R.id.menu_edit).setEnabled(contactSelected);
Evan Millardb5d88c2009-08-28 09:31:57 -0700494
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700495 // Only allow split when more than one contact
496 final boolean isAggregate = (mRawContactIds.size() > 1);
497 menu.findItem(R.id.menu_split).setEnabled(isAggregate);
498
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800499 return true;
500 }
501
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800502 @Override
503 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
504 AdapterView.AdapterContextMenuInfo info;
505 try {
506 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
507 } catch (ClassCastException e) {
508 Log.e(TAG, "bad menuInfo", e);
509 return;
510 }
511
512 // This can be null sometimes, don't crash...
513 if (info == null) {
514 Log.e(TAG, "bad menuInfo");
515 return;
516 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700517
Evan Millar45e0ed32009-06-01 16:44:38 -0700518 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
519 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
520 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700521 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
522 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700523 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800524 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700525 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
526 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700527 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700528 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800529 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700530 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700531 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
532 }
533 // TODO(emillar): add back with group support.
534 /* else if (entry.mimetype.equals()) {
535 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
536 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800537 }
538
539 @Override
540 public boolean onOptionsItemSelected(MenuItem item) {
541 switch (item.getItemId()) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700542 case R.id.menu_edit: {
Evan Millardb5d88c2009-08-28 09:31:57 -0700543 Long rawContactIdToEdit = mSelectedRawContactId;
544 if (rawContactIdToEdit == null) {
545 // This shouldn't be possible. We only show the edit option if
546 // this value is non-null.
547 break;
548 }
Evan Millar8a79cee2009-08-19 17:20:49 -0700549 if (rawContactIdToEdit == ALL_CONTACTS_ID) {
550 // If the "all" tab is selected, edit the next tab.
Evan Millardb5d88c2009-08-28 09:31:57 -0700551 rawContactIdToEdit = getTabRawContactId(mTabWidget.getCurrentTab() + 1);
Evan Millar8a79cee2009-08-19 17:20:49 -0700552 }
553 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
554 rawContactIdToEdit);
555 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
556 REQUEST_EDIT_CONTACT);
557 break;
558 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700559 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800560 // Get confirmation
561 showDialog(DIALOG_CONFIRM_DELETE);
562 return true;
563 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700564 case R.id.menu_split: {
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700565 if (mRawContactIds.size() == 2) {
566 splitContact(mRawContactIds.get(1));
567 } else {
568 showSplitAggregateDialog();
569 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700570 return true;
571 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700572 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700573 showJoinAggregateActivity();
574 return true;
575 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700576 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700577 showOptionsActivity();
578 return true;
579 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700580 case R.id.menu_share: {
581 final Intent intent = new Intent(Intent.ACTION_SEND);
582 intent.setType(Contacts.CONTENT_ITEM_TYPE);
583 intent.putExtra(Intent.EXTRA_STREAM, mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700584
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700585 // Launch chooser to share contact via
586 final CharSequence chooseTitle = getText(R.string.share_via);
587 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700588
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700589 try {
590 startActivity(chooseIntent);
591 } catch (ActivityNotFoundException ex) {
592 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800593 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700594 return true;
595 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800596 }
597 return super.onOptionsItemSelected(item);
598 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700599
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800600 @Override
601 public boolean onContextItemSelected(MenuItem item) {
602 switch (item.getItemId()) {
603 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700604 if (makeItemDefault(item)) {
605 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800606 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700607 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800608 }
609 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700610
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800611 return super.onContextItemSelected(item);
612 }
613
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700614 private boolean makeItemDefault(MenuItem item) {
615 ViewEntry entry = getViewEntryForMenuItem(item);
616 if (entry == null) {
617 return false;
618 }
619
620 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700621 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700622 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700623 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
624 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700625 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700626 return true;
627 }
628
629 /**
630 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
631 * The user picks a contact to be split into its own aggregate or clicks Cancel.
632 */
633 private void showSplitAggregateDialog() {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700634 // Wrap this dialog in a specific theme so that list items have correct text color.
635 final ContextThemeWrapper dialogContext =
636 new ContextThemeWrapper(this, android.R.style.Theme_Light);
637 AlertDialog.Builder builder =
638 new AlertDialog.Builder(dialogContext);
639 builder.setTitle(getString(R.string.splitAggregate_title));
640
641 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
642 builder.setView(view);
643
644 builder.setInverseBackgroundForced(true);
645 builder.setCancelable(true);
646 builder.setNegativeButton(android.R.string.cancel,
647 new OnClickListener() {
648 public void onClick(DialogInterface dialog, int which) {
649 dialog.dismiss();
650 }
651 });
652 final AlertDialog dialog = builder.create();
653
654 view.setOnContactSelectedListener(new OnContactSelectedListener() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700655 public void onContactSelected(long rawContactId) {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700656 dialog.dismiss();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700657 splitContact(rawContactId);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700658 }
659 });
660
661 dialog.show();
662 }
663
664 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700665 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700666 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700667 public void showJoinAggregateActivity() {
668 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
669 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700670 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700671 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700672
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700673 @Override
674 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700675 switch (requestCode) {
676 case REQUEST_JOIN_CONTACT: {
677 if (resultCode == RESULT_OK && intent != null) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700678 final long contactId = ContentUris.parseId(intent.getData());
679 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700680 }
681 break;
682 }
683 case REQUEST_EDIT_CONTACT: {
684 if (resultCode == RESULT_OK && intent != null) {
685 long newInitialSelectedRawContactId = intent.getLongExtra(
686 RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
687 if (newInitialSelectedRawContactId != mSelectedRawContactId) {
688 mSelectedRawContactId = newInitialSelectedRawContactId;
Evan Millar8a79cee2009-08-19 17:20:49 -0700689 }
690 }
691 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700692 }
693 }
694
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700695 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700696 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700697
698 // The split operation may have removed the original aggregate contact, so we need
699 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700700 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700701 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700702 }
703
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700704 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700705 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700706 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700707
708 try {
709 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700710 long rawContactId = c.getLong(0);
711 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700712 }
713 } finally {
714 c.close();
715 }
716
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700717 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
718 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700719 }
720
721 /**
722 * Given a contact ID sets an aggregation exception to either join the contact with the
723 * current aggregate or split off.
724 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700725 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700726 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700727 for (long aRawContactId : mRawContactIds) {
728 if (aRawContactId != rawContactId) {
729 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
730 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
731 values.put(AggregationExceptions.TYPE, exceptionType);
732 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
733 }
734 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700735 }
736
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700737 private void showOptionsActivity() {
738 final Intent intent = new Intent(this, ContactOptionsActivity.class);
739 intent.setData(mUri);
740 startActivity(intent);
741 }
742
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700743 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
744 AdapterView.AdapterContextMenuInfo info;
745 try {
746 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
747 } catch (ClassCastException e) {
748 Log.e(TAG, "bad menuInfo", e);
749 return null;
750 }
751
752 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
753 }
754
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800755 @Override
756 public boolean onKeyDown(int keyCode, KeyEvent event) {
757 switch (keyCode) {
758 case KeyEvent.KEYCODE_CALL: {
759 try {
760 ITelephony phone = ITelephony.Stub.asInterface(
761 ServiceManager.checkService("phone"));
762 if (phone != null && !phone.isIdle()) {
763 // Skip out and let the key be handled at a higher level
764 break;
765 }
766 } catch (RemoteException re) {
767 // Fall through and try to call the contact
768 }
769
Evan Millar7911ff52009-07-21 15:55:18 -0700770 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800771 if (index != -1) {
772 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700773 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
774 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800775 }
776 } else if (mNumPhoneNumbers != 0) {
777 // There isn't anything selected, call the default number
778 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
779 startActivity(intent);
780 }
781 return true;
782 }
783
784 case KeyEvent.KEYCODE_DEL: {
785 showDialog(DIALOG_CONFIRM_DELETE);
786 return true;
787 }
788 }
789
790 return super.onKeyDown(keyCode, event);
791 }
792
Evan Millar7911ff52009-07-21 15:55:18 -0700793 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800794 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
795 if (entry != null) {
796 Intent intent = entry.intent;
797 if (intent != null) {
798 try {
799 startActivity(intent);
800 } catch (ActivityNotFoundException e) {
801 Log.e(TAG, "No activity found for intent: " + intent);
802 signalError();
803 }
804 } else {
805 signalError();
806 }
807 } else {
808 signalError();
809 }
810 }
811
812 /**
813 * Signal an error to the user via a beep, or some other method.
814 */
815 private void signalError() {
816 //TODO: implement this when we have the sonification APIs
817 }
818
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800819 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700820 // 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 -0800821 StringBuilder buf = new StringBuilder("imto://");
822 buf.append(host);
823 buf.append('/');
824 buf.append(data);
825 return Uri.parse(buf.toString());
826 }
827
828 /**
829 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700830 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800831 * @param personCursor the URI for the contact being displayed
832 */
Evan Millar11d628c2009-09-02 08:55:01 -0700833 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800834 // Clear out the old entries
835 final int numSections = mSections.size();
836 for (int i = 0; i < numSections; i++) {
837 mSections.get(i).clear();
838 }
839
Evan Millar7911ff52009-07-21 15:55:18 -0700840 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700841
Evan Millar11d628c2009-09-02 08:55:01 -0700842 Sources sources = Sources.getInstance(this);
843
Evan Millar66388be2009-05-28 15:41:07 -0700844 // Build up method entries
845 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700846 for (Entity entity: mEntities) {
847 final ContentValues entValues = entity.getEntityValues();
848 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Evan Millar7911ff52009-07-21 15:55:18 -0700849 // TODO: entry.contactId should be renamed to entry.rawContactId
Evan Millar11d628c2009-09-02 08:55:01 -0700850 long contactId = 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
854 && mSelectedRawContactId != contactId
855 && 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
Evan Millar11d628c2009-09-02 08:55:01 -0700862 ContentValues entryValues = subValue.values;
863 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
864 if (mimetype == null || accountType == null) {
Evan Millar66388be2009-05-28 15:41:07 -0700865 continue;
866 }
867
Evan Millar11d628c2009-09-02 08:55:01 -0700868 ContactsSource contactsSource = sources.getInflatedSource(accountType,
869 ContactsSource.LEVEL_MIMETYPES);
870 if (contactsSource == null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800871 continue;
872 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700873
Evan Millar11d628c2009-09-02 08:55:01 -0700874 DataKind kind = contactsSource.getKindForMimetype(mimetype);
875 if (kind == null) {
876 continue;
877 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800878
Evan Millar11d628c2009-09-02 08:55:01 -0700879 final long id = entryValues.getAsLong(Data._ID);
880 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
881 entry.contactId = contactId;
882 entry.id = id;
883 entry.uri = uri;
884 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -0700885 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -0700886 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700887 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700888 entry.type = entryValues.getAsInteger(kind.typeColumn);
889 }
890 if (kind.iconRes > 0) {
891 entry.actionIcon = kind.iconRes;
892 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800893
Evan Millar11d628c2009-09-02 08:55:01 -0700894 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700895 if (TextUtils.isEmpty(entry.data)) {
896 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700897 continue;
898 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700899
Evan Millar11d628c2009-09-02 08:55:01 -0700900 if (!mRawContactIds.contains(entry.contactId)) {
901 mRawContactIds.add(entry.contactId);
902 }
903
Evan Millar11d628c2009-09-02 08:55:01 -0700904 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)
905 || CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)
906 || CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.equals(mimetype)
907 || CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
908 final boolean isSuperPrimary = entryValues.getAsInteger(
909 Data.IS_SUPER_PRIMARY) != 0;
910
911 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
912 // Build phone entries
913 mNumPhoneNumbers++;
914
915 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
916 Uri.fromParts("tel", entry.data, null));
917 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
918 Uri.fromParts("sms", entry.data, null));
919 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700920
921 // If data is empty, don't show it.
922 if (TextUtils.isEmpty(entry.data)) {
923 Log.w(TAG, "empty data for contact method " + id);
924 continue;
925 }
926
Evan Millar11d628c2009-09-02 08:55:01 -0700927 entry.isPrimary = isSuperPrimary;
928 mPhoneEntries.add(entry);
929
930 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
931 || mShowSmsLinksForAllPhones) {
932 // Add an SMS entry
933 if (kind.iconAltRes > 0) {
934 entry.secondaryActionIcon = kind.iconAltRes;
935 }
936 }
937 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
938 // Build email entries
939 entry.intent = new Intent(Intent.ACTION_SENDTO,
940 Uri.fromParts("mailto", entry.data, null));
Evan Millar49714ee2009-09-02 16:42:47 -0700941 // Temporary hack until we get real label resources for exchange.
942 if (TextUtils.isEmpty(entry.label)) {
943 entry.label = getString(R.string.email).toLowerCase();
944 }
Evan Millar11d628c2009-09-02 08:55:01 -0700945 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 Sharkey49d17b32009-09-07 02:14:21 -0700959 entry.label = getString(R.string.chat_other).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);
998 }
999
1000
1001 // TODO(emillar) Add group entries
1002 // // Build the group entries
1003 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1004 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1005 // null, null, Groups.DEFAULT_SORT_ORDER);
1006 // if (groupCursor != null) {
1007 // try {
1008 // StringBuilder sb = new StringBuilder();
1009 //
1010 // while (groupCursor.moveToNext()) {
1011 // String systemId = groupCursor.getString(
1012 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1013 //
1014 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1015 // continue;
1016 // }
1017 //
1018 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1019 // if (!TextUtils.isEmpty(name)) {
1020 // if (sb.length() == 0) {
1021 // sb.append(name);
1022 // } else {
1023 // sb.append(getString(R.string.group_list, name));
1024 // }
1025 // }
1026 // }
1027 //
1028 // if (sb.length() > 0) {
1029 // ViewEntry entry = new ViewEntry();
1030 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1031 // entry.label = getString(R.string.label_groups);
1032 // entry.data = sb.toString();
1033 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1034 //
1035 // // TODO: Add an icon for the groups item.
1036 //
1037 // mGroupEntries.add(entry);
1038 // }
1039 // } finally {
1040 // groupCursor.close();
1041 // }
1042 // }
Evan Millar66388be2009-05-28 15:41:07 -07001043 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001044
Evan Millar66388be2009-05-28 15:41:07 -07001045 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001046 }
1047 }
1048
Evan Millar11d628c2009-09-02 08:55:01 -07001049 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1050 if (kind.actionHeader == null) {
1051 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001052 }
Evan Millar11d628c2009-09-02 08:55:01 -07001053 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1054 if (actionHeader == null) {
1055 return null;
1056 }
1057 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1058 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001059
Evan Millar11d628c2009-09-02 08:55:01 -07001060 String buildDataString(DataKind kind, ContentValues values) {
1061 if (kind.actionBody == null) {
1062 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001063 }
Evan Millar11d628c2009-09-02 08:55:01 -07001064 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1065 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001066 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001067
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001068 /**
1069 * A basic structure with the data for a contact entry in the list.
1070 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001071 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001072 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001073 public boolean isPrimary = false;
1074 public int presenceIcon = -1;
1075 public int secondaryActionIcon = -1;
1076 public Intent intent;
1077 public Intent secondaryIntent = null;
1078 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001079 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001080 public ArrayList<Long> ids = new ArrayList<Long>();
1081 public int collapseCount = 0;
1082
1083 public boolean collapseWith(ViewEntry entry) {
1084 // assert equal collapse keys
1085 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1086 return false;
1087 }
1088
1089 // Choose the label associated with the highest type precedence.
1090 if (TypePrecedence.getTypePrecedence(mimetype, type)
1091 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1092 type = entry.type;
1093 label = entry.label;
1094 }
1095
1096 // Choose the max of the maxLines and maxLabelLines values.
1097 maxLines = Math.max(maxLines, entry.maxLines);
1098 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1099
1100 // Choose the presence with the highest precedence.
1101 if (Presence.getPresencePrecedence(status)
1102 < Presence.getPresencePrecedence(entry.status)) {
1103 status = entry.status;
1104 }
1105
1106 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001107 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001108
1109 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1110
1111 // Keep track of all the ids that have been collapsed with this one.
1112 ids.add(entry.id);
1113 collapseCount++;
1114 return true;
1115 }
1116
1117 public String getCollapseKey() {
1118 StringBuilder hashSb = new StringBuilder();
1119 hashSb.append(data);
1120 hashSb.append(mimetype);
1121 hashSb.append((intent != null && intent.getAction() != null)
1122 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001123 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1124 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001125 hashSb.append(actionIcon);
1126 return hashSb.toString();
1127 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001128 }
1129
Evan Millar15e514d2009-08-04 10:14:57 -07001130 /** Cache of the children views of a row */
1131 static class ViewCache {
1132 public TextView label;
1133 public TextView data;
1134 public ImageView actionIcon;
1135 public ImageView presenceIcon;
1136 public ImageView primaryIcon;
1137 public ImageView secondaryActionButton;
1138 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001139
Evan Millar15e514d2009-08-04 10:14:57 -07001140 // Need to keep track of this too
1141 ViewEntry entry;
1142 }
1143
1144 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1145 implements View.OnClickListener {
1146
Evan Millar5c22c3b2009-05-29 11:37:54 -07001147
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001148 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1149 super(context, sections, SHOW_SEPARATORS);
1150 }
1151
Evan Millar15e514d2009-08-04 10:14:57 -07001152 public void onClick(View v) {
1153 Intent intent = (Intent) v.getTag();
1154 startActivity(intent);
1155 }
1156
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001157 @Override
1158 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001159 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001160 View v;
1161
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001162 ViewCache views;
1163
1164 // Check to see if we can reuse convertView
1165 if (convertView != null) {
1166 v = convertView;
1167 views = (ViewCache) v.getTag();
1168 } else {
1169 // Create a new view if needed
1170 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1171
1172 // Cache the children
1173 views = new ViewCache();
1174 views.label = (TextView) v.findViewById(android.R.id.text1);
1175 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001176 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1177 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1178 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1179 views.secondaryActionButton = (ImageView) v.findViewById(
1180 R.id.secondary_action_button);
1181 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001182 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001183 v.setTag(views);
1184 }
1185
1186 // Update the entry in the view cache
1187 views.entry = entry;
1188
1189 // Bind the data to the view
1190 bindView(v, entry);
1191 return v;
1192 }
1193
1194 @Override
1195 protected View newView(int position, ViewGroup parent) {
1196 // getView() handles this
1197 throw new UnsupportedOperationException();
1198 }
1199
1200 @Override
1201 protected void bindView(View view, ViewEntry entry) {
1202 final Resources resources = mContext.getResources();
1203 ViewCache views = (ViewCache) view.getTag();
1204
1205 // Set the label
1206 TextView label = views.label;
1207 setMaxLines(label, entry.maxLabelLines);
1208 label.setText(entry.label);
1209
1210 // Set the data
1211 TextView data = views.data;
1212 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001213 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001214 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001215 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1216 } else {
1217 data.setText(entry.data);
1218 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001219 setMaxLines(data, entry.maxLines);
1220 }
1221
Evan Millar15e514d2009-08-04 10:14:57 -07001222 // Set the primary icon
1223 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1224
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001225 // Set the action icon
1226 ImageView action = views.actionIcon;
1227 if (entry.actionIcon != -1) {
1228 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1229 action.setVisibility(View.VISIBLE);
1230 } else {
1231 // Things should still line up as if there was an icon, so make it invisible
1232 action.setVisibility(View.INVISIBLE);
1233 }
1234
1235 // Set the presence icon
1236 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001237 if (entry.presenceIcon != -1) {
1238 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001239 } else if (entry.status != -1) {
1240 presenceIcon = resources.getDrawable(
1241 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001242 }
Evan Millar15e514d2009-08-04 10:14:57 -07001243 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001244 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001245 presenceIconView.setImageDrawable(presenceIcon);
1246 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001247 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001248 presenceIconView.setVisibility(View.GONE);
1249 }
1250
1251 // Set the secondary action button
1252 ImageView secondaryActionView = views.secondaryActionButton;
1253 Drawable secondaryActionIcon = null;
1254 if (entry.secondaryActionIcon != -1) {
1255 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1256 }
1257 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1258 secondaryActionView.setImageDrawable(secondaryActionIcon);
1259 secondaryActionView.setTag(entry.secondaryIntent);
1260 secondaryActionView.setVisibility(View.VISIBLE);
1261 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1262 } else {
1263 secondaryActionView.setVisibility(View.GONE);
1264 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001265 }
1266 }
1267
1268 private void setMaxLines(TextView textView, int maxLines) {
1269 if (maxLines == 1) {
1270 textView.setSingleLine(true);
1271 textView.setEllipsize(TextUtils.TruncateAt.END);
1272 } else {
1273 textView.setSingleLine(false);
1274 textView.setMaxLines(maxLines);
1275 textView.setEllipsize(null);
1276 }
1277 }
1278 }
1279}