blob: 0e03cf3f3fde02c342df9e44e31adbde0eed71c7 [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;
Evan Millar2e1b8af2009-09-24 09:39:21 -070076import android.view.animation.Animation;
Evan Millar2e1b8af2009-09-24 09:39:21 -070077import android.view.animation.TranslateAnimation;
78import android.view.animation.Animation.AnimationListener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080079import android.widget.AdapterView;
Evan Millar7911ff52009-07-21 15:55:18 -070080import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080081import android.widget.ImageView;
82import android.widget.ListView;
83import android.widget.TextView;
84import android.widget.Toast;
85
86import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087
88/**
89 * Displays the details of a specific contact.
90 */
Evan Millar11d628c2009-09-02 08:55:01 -070091public class ViewContactActivity extends Activity
Evan Millar7911ff52009-07-21 15:55:18 -070092 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
Evan Millar2e1b8af2009-09-24 09:39:21 -070093 AdapterView.OnItemClickListener, NotifyingAsyncQueryHandler.AsyncQueryListener,
94 OnTabSelectionChangedListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080095 private static final String TAG = "ViewContact";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080096
Evan Millar8a79cee2009-08-19 17:20:49 -070097 public static final String RAW_CONTACT_ID_EXTRA = "rawContactIdExtra";
98
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080099 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800100
101 private static final int DIALOG_CONFIRM_DELETE = 1;
Megha Joshid5afd8a2009-09-22 09:38:41 -0700102 private static final int DIALOG_CONFIRM_READONLY_DELETE = 2;
103 private static final int DIALOG_CONFIRM_MULTIPLE_DELETE = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800104
Evan Millar8a79cee2009-08-19 17:20:49 -0700105 private static final int REQUEST_JOIN_CONTACT = 1;
Evan Millar9cd81242009-09-26 16:02:31 -0700106 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700107
Evan Millar8a79cee2009-08-19 17:20:49 -0700108 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800109
Evan Millar2e1b8af2009-09-24 09:39:21 -0700110 private static final String SPLIT_MIMETYPE = "split_mimetype";
111
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700112 protected Uri mLookupUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800113 private ContentResolver mResolver;
114 private ViewAdapter mAdapter;
115 private int mNumPhoneNumbers = 0;
116
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700117 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700118 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700119 */
Evan Millar7911ff52009-07-21 15:55:18 -0700120 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700121
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800122 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
123 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
124 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
125 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
126 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
127 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700128 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800129 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
Evan Millar2e1b8af2009-09-24 09:39:21 -0700130 /* package */ ArrayList<ViewEntry> mSplitEntry = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800131 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
132
133 private Cursor mCursor;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700134
Evan Millar11d628c2009-09-02 08:55:01 -0700135 private SparseArray<Long> mTabRawContactIdMap;
136 protected ScrollingTabWidget mTabWidget;
137 protected ContactHeaderWidget mContactHeaderWidget;
Evan Millar2e1b8af2009-09-24 09:39:21 -0700138 protected View mBelowHeader;
Tadashi G. Takaoka33aa1c62009-09-27 14:22:28 -0700139 protected TextView mAccountName;
Evan Millar2e1b8af2009-09-24 09:39:21 -0700140 protected View mBufferView;
Evan Millar11d628c2009-09-02 08:55:01 -0700141 private NotifyingAsyncQueryHandler mHandler;
142
143 protected LayoutInflater mInflater;
144
145 //Projection used for the query that determines which tabs to add.
146 protected static final String[] TAB_PROJECTION = new String[] {
147 RawContacts._ID,
148 RawContacts.ACCOUNT_NAME,
149 RawContacts.ACCOUNT_TYPE
150 };
151 protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0;
152 protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1;
153 protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2;
154
Evan Millar9cd81242009-09-26 16:02:31 -0700155 private static final String SAVED_STATE_SELECTED_RAW_CONTACT_ID_KEY = "selectedRawContactKey";
156 private static final String SAVED_STATE_TABS_VISIBLE_KEY = "tabsVisibleKey";
157
Evan Millar11d628c2009-09-02 08:55:01 -0700158 protected Long mSelectedRawContactId = null;
Megha Joshid5afd8a2009-09-22 09:38:41 -0700159 protected int mReadOnlySourcesCnt;
160 protected int mWritableSourcesCnt;
161 protected ArrayList<Long> mWritableRawContactIds = new ArrayList<Long>();
Evan Millar11d628c2009-09-02 08:55:01 -0700162
163 private static final int TOKEN_QUERY = 0;
164
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800165 private ContentObserver mObserver = new ContentObserver(new Handler()) {
166 @Override
167 public boolean deliverSelfNotifications() {
168 return true;
169 }
170
171 @Override
172 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700173 if (mCursor != null && !mCursor.isClosed()) {
174 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800175 }
176 }
177 };
178
179 public void onClick(DialogInterface dialog, int which) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700180 closeCursor();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700181 if (mReadOnlySourcesCnt > 0) {
182 for (long rawContactIdToDelete: mWritableRawContactIds) {
183 final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
184 rawContactIdToDelete);
185 getContentResolver().delete(rawContactUri, null, null);
186 }
187 } else {
188 getContentResolver().delete(mLookupUri, null, null);
189 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800190 finish();
191 }
192
Evan Millar7911ff52009-07-21 15:55:18 -0700193 private FrameLayout mTabContentLayout;
194 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800195 private boolean mShowSmsLinksForAllPhones;
Evan Millar11d628c2009-09-02 08:55:01 -0700196 private ArrayList<Entity> mEntities = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800197
Evan Millar2e1b8af2009-09-24 09:39:21 -0700198 private boolean mTabsVisible;
199
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800200 @Override
201 protected void onCreate(Bundle icicle) {
202 super.onCreate(icicle);
203
Evan Millar11d628c2009-09-02 08:55:01 -0700204 final Intent intent = getIntent();
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700205 Uri data = intent.getData();
206 String authority = data.getAuthority();
207 if (ContactsContract.AUTHORITY.equals(authority)) {
208 mLookupUri = data;
209 } else if (android.provider.Contacts.AUTHORITY.equals(authority)) {
210 final long rawContactId = ContentUris.parseId(data);
211 mLookupUri = RawContacts.getContactLookupUri(getContentResolver(),
212 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700213
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700214 }
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700215 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700216
217 requestWindowFeature(Window.FEATURE_NO_TITLE);
218 setContentView(R.layout.contact_card_layout);
219
220 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
221 mContactHeaderWidget.showStar(true);
Evan Millar11d628c2009-09-02 08:55:01 -0700222 mContactHeaderWidget.setExcludeMimes(new String[] {
223 Contacts.CONTENT_ITEM_TYPE
224 });
225
226 mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700227 mTabWidget.setTabSelectionListener(this);
Evan Millar85863112009-09-21 14:35:18 -0700228 mTabWidget.setVisibility(View.GONE);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700229 mTabsVisible = false;
Tadashi G. Takaokab4a72982009-09-29 13:08:28 -0700230 mAccountName = (TextView) mTabWidget.findViewById(R.id.account_name);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700231
232 mBelowHeader = findViewById(R.id.below_header);
Evan Millar11d628c2009-09-02 08:55:01 -0700233
234 mTabRawContactIdMap = new SparseArray<Long>();
235
236 mHandler = new NotifyingAsyncQueryHandler(this, this);
237
Evan Millar7911ff52009-07-21 15:55:18 -0700238 mListView = new ListView(this);
239 mListView.setOnCreateContextMenuListener(this);
240 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
241 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800242
Evan Millar7911ff52009-07-21 15:55:18 -0700243 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
244 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800245
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800246 mResolver = getContentResolver();
247
248 // Build the list of sections. The order they're added to mSections dictates the
249 // order they are displayed in the list.
250 mSections.add(mPhoneEntries);
251 mSections.add(mSmsEntries);
252 mSections.add(mEmailEntries);
253 mSections.add(mImEntries);
254 mSections.add(mPostalEntries);
255 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700256 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800257 mSections.add(mOtherEntries);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700258 mSections.add(mSplitEntry);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800259
260 //TODO Read this value from a preference
261 mShowSmsLinksForAllPhones = true;
Evan Millar11d628c2009-09-02 08:55:01 -0700262 }
263
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800264 @Override
Evan Millar9cd81242009-09-26 16:02:31 -0700265 protected void onRestoreInstanceState(Bundle savedInstanceState) {
266 super.onRestoreInstanceState(savedInstanceState);
267 long restoredRawContactId = savedInstanceState.getLong(
268 SAVED_STATE_SELECTED_RAW_CONTACT_ID_KEY, -1);
269 mSelectedRawContactId = restoredRawContactId != -1 ? restoredRawContactId : null;
270 mTabsVisible = savedInstanceState.getBoolean(SAVED_STATE_TABS_VISIBLE_KEY);
271 }
272
273 @Override
274 protected void onSaveInstanceState(Bundle outState) {
275 super.onSaveInstanceState(outState);
276 if (mSelectedRawContactId != null) {
277 outState.putLong(SAVED_STATE_SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
278
279 }
280 outState.putBoolean(SAVED_STATE_TABS_VISIBLE_KEY, mTabsVisible);
281 }
282
283 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800284 protected void onResume() {
285 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700286 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800287 }
288
289 @Override
290 protected void onPause() {
291 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700292 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800293 }
294
295 @Override
296 protected void onDestroy() {
297 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700298 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800299 }
300
301 @Override
302 protected Dialog onCreateDialog(int id) {
303 switch (id) {
304 case DIALOG_CONFIRM_DELETE:
305 return new AlertDialog.Builder(this)
306 .setTitle(R.string.deleteConfirmation_title)
307 .setIcon(android.R.drawable.ic_dialog_alert)
308 .setMessage(R.string.deleteConfirmation)
309 .setNegativeButton(android.R.string.cancel, null)
310 .setPositiveButton(android.R.string.ok, this)
311 .setCancelable(false)
312 .create();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700313 case DIALOG_CONFIRM_READONLY_DELETE:
314 return new AlertDialog.Builder(this)
315 .setTitle(R.string.deleteConfirmation_title)
316 .setIcon(android.R.drawable.ic_dialog_alert)
317 .setMessage(R.string.readOnlyContactDeleteConfirmation)
318 .setNegativeButton(android.R.string.cancel, null)
319 .setPositiveButton(android.R.string.ok, this)
320 .setCancelable(false)
321 .create();
322 case DIALOG_CONFIRM_MULTIPLE_DELETE:
323 return new AlertDialog.Builder(this)
324 .setTitle(R.string.deleteConfirmation_title)
325 .setIcon(android.R.drawable.ic_dialog_alert)
326 .setMessage(R.string.multipleContactDeleteConfirmation)
327 .setNegativeButton(android.R.string.cancel, null)
328 .setPositiveButton(android.R.string.ok, this)
329 .setCancelable(false)
330 .create();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800331 }
332 return null;
333 }
334
Evan Millar11d628c2009-09-02 08:55:01 -0700335
336 // TAB CODE //
337 /**
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700338 * Adds a tab for each {@link RawContacts} associated with this contact.
Evan Millar11d628c2009-09-02 08:55:01 -0700339 * Override this method if you want to additional tabs and/or different
340 * tabs for your activity.
Evan Millar11d628c2009-09-02 08:55:01 -0700341 */
Evan Millar2e1b8af2009-09-24 09:39:21 -0700342 protected void bindTabs() {
343 final Sources sources = Sources.getInstance(this);
344
345 for (Entity entity : mEntities) {
346 final String accountType = entity.getEntityValues().
347 getAsString(RawContacts.ACCOUNT_TYPE);
348 final Long rawContactId = entity.getEntityValues().
349 getAsLong(RawContacts._ID);
350
351 // TODO: ensure inflation on background task so we don't block UI thread here
352 final ContactsSource source = sources.getInflatedSource(accountType,
353 ContactsSource.LEVEL_SUMMARY);
354 addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(),
355 source));
356 }
357 }
358
359 /**
360 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
361 *
362 * @param rawContactId The contact id associated with the tab.
363 * @param view A view to use as the tab indicator.
364 */
365 protected void addTab(long rawContactId, View view) {
366 mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId);
367 mTabWidget.addTab(view);
368 }
369
370
371 protected void clearCurrentTabs() {
372 mTabRawContactIdMap.clear();
373 mTabWidget.removeAllTabs();
374 }
375
376 protected void selectInitialTab() {
377 int selectedTabIndex = 0;
378
379 if (mSelectedRawContactId != null) {
380 selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
381 if (selectedTabIndex == -1) {
382 // If there was no matching tab, just select the first;
383 selectedTabIndex = 0;
384 }
385 }
386
387 mTabWidget.setCurrentTab(selectedTabIndex);
388 onTabSelectionChanged(selectedTabIndex, false);
389 }
390
391 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
392 Long rawContactId = getTabRawContactId(tabIndex);
393 if (rawContactId != null) {
394 mSelectedRawContactId = rawContactId;
395 bindData();
396 }
397 }
398
399 /**
400 * Return the RawContact id associated with the tab at an index.
401 *
402 * @param index The index of the tab in question.
403 * @return The contactId associated with the tab at the specified index.
404 */
405 protected Long getTabRawContactId(int index) {
406 return mTabRawContactIdMap.get(index);
407 }
408
409 /**
410 * Return the tab index associated with the RawContact id.
411 *
412 * @param index The index of the tab in question.
413 * @return The contactId associated with the tab at the specified index.
414 */
415 protected int getTabIndexForRawContactId(long rawContactId) {
416 int numTabs = mTabRawContactIdMap.size();
417 for (int i=0; i < numTabs; i++) {
418 if (mTabRawContactIdMap.get(i) == rawContactId) {
419 return i;
420 }
421 }
422 return -1;
423 }
424
Evan Millar9cd81242009-09-26 16:02:31 -0700425 protected void showTabs(boolean show) {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700426 if (mTabsVisible == show) {
427 return;
428 }
429
Tadashi G. Takaokab4a72982009-09-29 13:08:28 -0700430 final Resources resources = getResources();
431 final float tabHeight = resources.getDimension(R.dimen.tab_height)
432 + resources.getDimension(R.dimen.account_name_height);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700433 if (show) {
Evan Millar9cd81242009-09-26 16:02:31 -0700434 TranslateAnimation showAnimation = new TranslateAnimation(
435 Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
436 Animation.ABSOLUTE, -tabHeight, Animation.ABSOLUTE, 0);
Tadashi G. Takaokab4a72982009-09-29 13:08:28 -0700437 showAnimation.setDuration(resources.getInteger(
Evan Millar9cd81242009-09-26 16:02:31 -0700438 android.R.integer.config_longAnimTime));
Evan Millar2e1b8af2009-09-24 09:39:21 -0700439
Evan Millar9cd81242009-09-26 16:02:31 -0700440 showAnimation.setAnimationListener(new AnimationListener() {
441 public void onAnimationEnd(Animation animation) {
442 selectInitialTab();
443 bindData();
444 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700445
Evan Millar9cd81242009-09-26 16:02:31 -0700446 public void onAnimationRepeat(Animation animation) {
447 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700448
Evan Millar9cd81242009-09-26 16:02:31 -0700449 public void onAnimationStart(Animation animation) {
450 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700451
Evan Millar9cd81242009-09-26 16:02:31 -0700452 });
Evan Millar2e1b8af2009-09-24 09:39:21 -0700453
Evan Millar9cd81242009-09-26 16:02:31 -0700454 mBelowHeader.startAnimation(showAnimation);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700455 mTabWidget.setVisibility(View.VISIBLE);
456 mTabsVisible = true;
457 clearCurrentTabs();
458 bindTabs();
Evan Millar2e1b8af2009-09-24 09:39:21 -0700459 } else {
Evan Millar9cd81242009-09-26 16:02:31 -0700460 TranslateAnimation hideTabsAnimation = new TranslateAnimation(
461 Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
462 Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -tabHeight);
Tadashi G. Takaokab4a72982009-09-29 13:08:28 -0700463 hideTabsAnimation.setDuration(resources.getInteger(
Evan Millar9cd81242009-09-26 16:02:31 -0700464 android.R.integer.config_longAnimTime));
465 hideTabsAnimation.setAnimationListener(new AnimationListener() {
466 public void onAnimationEnd(Animation animation) {
467 bindData();
468 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700469
Evan Millar9cd81242009-09-26 16:02:31 -0700470 public void onAnimationRepeat(Animation animation) {
471 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700472
Evan Millar9cd81242009-09-26 16:02:31 -0700473 public void onAnimationStart(Animation animation) {
474 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700475
Evan Millar9cd81242009-09-26 16:02:31 -0700476 });
Evan Millar2e1b8af2009-09-24 09:39:21 -0700477
Evan Millar9cd81242009-09-26 16:02:31 -0700478 TranslateAnimation hideListAnimation = new TranslateAnimation(
479 Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
480 Animation.ABSOLUTE, tabHeight, Animation.ABSOLUTE, 0);
Tadashi G. Takaokab4a72982009-09-29 13:08:28 -0700481 hideListAnimation.setDuration(resources.getInteger(
Evan Millar9cd81242009-09-26 16:02:31 -0700482 android.R.integer.config_longAnimTime));
Evan Millar2e1b8af2009-09-24 09:39:21 -0700483
484
Evan Millar9cd81242009-09-26 16:02:31 -0700485 mTabWidget.startAnimation(hideTabsAnimation);
486 mTabContentLayout.startAnimation(hideListAnimation);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700487 mTabWidget.setVisibility(View.GONE);
488 mTabsVisible = false;
489 mSelectedRawContactId = null;
Evan Millar2e1b8af2009-09-24 09:39:21 -0700490 }
491 }
Evan Millar11d628c2009-09-02 08:55:01 -0700492
493
494 // QUERY CODE //
495 /** {@inheritDoc} */
496 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
497 try{
498 if (token == TOKEN_QUERY) {
Evan Millar11d628c2009-09-02 08:55:01 -0700499 mEntities = readEntities(iterator);
Evan Millar85863112009-09-21 14:35:18 -0700500 // Show the aggregate badge if this contact is aggregated.
Evan Millarcc87fb92009-09-22 15:27:47 -0700501 boolean isAggregate = mEntities.size() > 1;
502 mContactHeaderWidget.showAggregateBadge(isAggregate);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700503 if (mTabsVisible) {
Evan Millar9cd81242009-09-26 16:02:31 -0700504 mTabWidget.setVisibility(View.VISIBLE);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700505 clearCurrentTabs();
506 bindTabs();
507 selectInitialTab();
Evan Millar9cd81242009-09-26 16:02:31 -0700508 } else {
509 mTabWidget.setVisibility(View.GONE);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700510 }
Evan Millar85863112009-09-21 14:35:18 -0700511 bindData();
Evan Millar11d628c2009-09-02 08:55:01 -0700512 }
513 } finally {
514 if (iterator != null) {
515 iterator.close();
516 }
517 }
518 }
519
520 /** {@inheritDoc} */
521 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700522 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700523 }
524
Evan Millar6a61a1a2009-09-29 14:00:43 -0700525 private long getRefreshedContactId() {
526 Uri freshContactUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
527 if (freshContactUri != null) {
528 return ContentUris.parseId(freshContactUri);
529 }
530 return -1;
531 }
532
Evan Millar11d628c2009-09-02 08:55:01 -0700533 private ArrayList<Entity> readEntities(EntityIterator iterator) {
534 ArrayList<Entity> entities = new ArrayList<Entity>();
535 try {
536 while (iterator.hasNext()) {
537 entities.add(iterator.next());
538 }
539 } catch (RemoteException e) {
540 }
541
542 return entities;
543 }
544
545 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700546 closeCursor();
547
Evan Millar6a61a1a2009-09-29 14:00:43 -0700548 Uri uri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700549 if (mLookupUri != null) {
550 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
551 if (mLookupUri != null) {
Evan Millar6a61a1a2009-09-29 14:00:43 -0700552 uri = Contacts.lookupContact(getContentResolver(), mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700553 }
554 }
555
Evan Millar6a61a1a2009-09-29 14:00:43 -0700556 if (uri == null) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700557
558 // TODO either figure out a way to prevent a flash of black background or
559 // use some other UI than a toast
560 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
561 Log.e(TAG, "invalid contact uri: " + mLookupUri);
562 finish();
563 return;
564 }
565
Evan Millar6a61a1a2009-09-29 14:00:43 -0700566 mCursor = mResolver.query(Uri.withAppendedPath(uri, Contacts.Data.CONTENT_DIRECTORY),
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700567 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
568 mCursor.registerContentObserver(mObserver);
569
Evan Millar6a61a1a2009-09-29 14:00:43 -0700570 long contactId = ContentUris.parseId(uri);
Evan Millar11d628c2009-09-02 08:55:01 -0700571 mHandler.startQueryEntities(TOKEN_QUERY, null,
572 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700573
Evan Millar9b1a1242009-09-16 10:26:10 -0700574 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700575 }
576
577 private void closeCursor() {
578 if (mCursor != null) {
579 mCursor.unregisterContentObserver(mObserver);
580 mCursor.close();
581 mCursor = null;
582 }
Evan Millar11d628c2009-09-02 08:55:01 -0700583 }
584
585 private void bindData() {
586
587 // Build up the contact entries
588 buildEntries();
589
590 // Collapse similar data items in select sections.
591 Collapser.collapseList(mPhoneEntries);
592 Collapser.collapseList(mSmsEntries);
593 Collapser.collapseList(mEmailEntries);
594 Collapser.collapseList(mPostalEntries);
595
596 if (mAdapter == null) {
597 mAdapter = new ViewAdapter(this, mSections);
598 mListView.setAdapter(mAdapter);
599 } else {
600 mAdapter.setSections(mSections, SHOW_SEPARATORS);
601 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800602 }
603
604 @Override
605 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700606 super.onCreateOptionsMenu(menu);
607
608 final MenuInflater inflater = getMenuInflater();
609 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800610 return true;
611 }
612
613 @Override
614 public boolean onPrepareOptionsMenu(Menu menu) {
615 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700616
Evan Millar2e1b8af2009-09-24 09:39:21 -0700617 // If tabWidget is not shown enable "show sources", otherwise
618 // enable "hide sources"
619 if (mTabsVisible) {
620 menu.findItem(R.id.menu_show_sources).setVisible(false);
621 menu.findItem(R.id.menu_hide_sources).setVisible(true);
622 } else {
623 menu.findItem(R.id.menu_show_sources).setVisible(true);
624 menu.findItem(R.id.menu_hide_sources).setVisible(false);
625 }
626
Evan Millar51514db2009-09-22 11:42:35 -0700627 // Only allow edit when we have at least one raw_contact id
628 final boolean hasRawContact = (mRawContactIds.size() > 0);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700629 menu.findItem(R.id.menu_edit).setEnabled(hasRawContact);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700630
Megha Joshid5afd8a2009-09-22 09:38:41 -0700631 // Disable delete for readonly contact
632 if (mWritableSourcesCnt == 0) {
633 menu.findItem(R.id.menu_delete).setEnabled(false);
634 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800635 return true;
636 }
637
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800638 @Override
639 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
640 AdapterView.AdapterContextMenuInfo info;
641 try {
642 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
643 } catch (ClassCastException e) {
644 Log.e(TAG, "bad menuInfo", e);
645 return;
646 }
647
648 // This can be null sometimes, don't crash...
649 if (info == null) {
650 Log.e(TAG, "bad menuInfo");
651 return;
652 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700653
Evan Millar45e0ed32009-06-01 16:44:38 -0700654 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
Neel Parekha8fb68a2009-09-25 18:05:18 -0700655 menu.setHeaderTitle(R.string.contactOptionsTitle);
Evan Millar45e0ed32009-06-01 16:44:38 -0700656 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
657 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700658 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
659 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700660 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800661 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700662 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
663 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700664 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700665 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800666 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700667 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700668 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
669 }
670 // TODO(emillar): add back with group support.
671 /* else if (entry.mimetype.equals()) {
672 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
673 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800674 }
675
676 @Override
677 public boolean onOptionsItemSelected(MenuItem item) {
678 switch (item.getItemId()) {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700679 case R.id.menu_show_sources: {
Evan Millar9cd81242009-09-26 16:02:31 -0700680 showTabs(true);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700681 break;
682 }
683 case R.id.menu_hide_sources: {
Evan Millar9cd81242009-09-26 16:02:31 -0700684 showTabs(false);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700685 break;
686 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700687 case R.id.menu_edit: {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700688 Long rawContactIdToEdit = mSelectedRawContactId;
689 if (rawContactIdToEdit == null) {
690 if (mRawContactIds.size() > 0) {
691 rawContactIdToEdit = mRawContactIds.get(0);
692 } else {
693 // There is no rawContact to edit.
694 break;
695 }
Evan Millardb5d88c2009-08-28 09:31:57 -0700696 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700697 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
698 rawContactIdToEdit);
Evan Millar9cd81242009-09-26 16:02:31 -0700699 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
700 REQUEST_EDIT_CONTACT);
Evan Millar8a79cee2009-08-19 17:20:49 -0700701 break;
702 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700703 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800704 // Get confirmation
Megha Joshid5afd8a2009-09-22 09:38:41 -0700705 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
706 showDialog(DIALOG_CONFIRM_READONLY_DELETE);
707 } else if (mWritableSourcesCnt > 1) {
708 showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
709 } else {
710 showDialog(DIALOG_CONFIRM_DELETE);
711 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800712 return true;
713 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700714 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700715 showJoinAggregateActivity();
716 return true;
717 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700718 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700719 showOptionsActivity();
720 return true;
721 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700722 case R.id.menu_share: {
Jeff Sharkey93049312009-09-27 17:28:13 -0700723 // TODO: Keep around actual LOOKUP_KEY, or formalize method of extracting
724 final String lookupKey = mLookupUri.getPathSegments().get(2);
725 final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
726
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700727 final Intent intent = new Intent(Intent.ACTION_SEND);
Jeff Sharkey93049312009-09-27 17:28:13 -0700728 intent.setType(Contacts.CONTENT_VCARD_TYPE);
729 intent.putExtra(Intent.EXTRA_STREAM, shareUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700730
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700731 // Launch chooser to share contact via
732 final CharSequence chooseTitle = getText(R.string.share_via);
733 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700734
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700735 try {
736 startActivity(chooseIntent);
737 } catch (ActivityNotFoundException ex) {
738 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800739 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700740 return true;
741 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800742 }
743 return super.onOptionsItemSelected(item);
744 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700745
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800746 @Override
747 public boolean onContextItemSelected(MenuItem item) {
748 switch (item.getItemId()) {
749 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700750 if (makeItemDefault(item)) {
751 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800752 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700753 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800754 }
755 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700756
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800757 return super.onContextItemSelected(item);
758 }
759
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700760 private boolean makeItemDefault(MenuItem item) {
761 ViewEntry entry = getViewEntryForMenuItem(item);
762 if (entry == null) {
763 return false;
764 }
765
766 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700767 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700768 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700769 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
770 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700771 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700772 return true;
773 }
774
775 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700776 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700777 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700778 public void showJoinAggregateActivity() {
Evan Millar6a61a1a2009-09-29 14:00:43 -0700779 long freshId = getRefreshedContactId();
780 if (freshId > 0) {
781 String displayName = null;
782 if (mCursor.moveToFirst()) {
783 displayName = mCursor.getString(0);
784 }
785 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
786 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, freshId);
787 if (displayName != null) {
788 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_NAME, displayName);
789 }
790 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikova5cfca32009-09-29 18:13:08 -0700791 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700792 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700793
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700794 @Override
795 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700796 if (requestCode == REQUEST_JOIN_CONTACT) {
797 if (resultCode == RESULT_OK && intent != null) {
798 final long contactId = ContentUris.parseId(intent.getData());
799 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700800 }
Evan Millar9cd81242009-09-26 16:02:31 -0700801 } else if (requestCode == REQUEST_EDIT_CONTACT) {
802 mTabsVisible = false;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700803 }
804 }
805
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700806 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700807 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700808
809 // The split operation may have removed the original aggregate contact, so we need
810 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700811 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700812 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700813 }
814
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700815 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700816 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700817 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700818
819 try {
820 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700821 long rawContactId = c.getLong(0);
822 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700823 }
824 } finally {
825 c.close();
826 }
827
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700828 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
829 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700830 }
831
832 /**
833 * Given a contact ID sets an aggregation exception to either join the contact with the
834 * current aggregate or split off.
835 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700836 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700837 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700838 for (long aRawContactId : mRawContactIds) {
839 if (aRawContactId != rawContactId) {
840 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
841 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
842 values.put(AggregationExceptions.TYPE, exceptionType);
843 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
844 }
845 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700846 }
847
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700848 private void showOptionsActivity() {
849 final Intent intent = new Intent(this, ContactOptionsActivity.class);
Evan Millar6a61a1a2009-09-29 14:00:43 -0700850 intent.setData(mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700851 startActivity(intent);
852 }
853
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700854 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
855 AdapterView.AdapterContextMenuInfo info;
856 try {
857 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
858 } catch (ClassCastException e) {
859 Log.e(TAG, "bad menuInfo", e);
860 return null;
861 }
862
863 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
864 }
865
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800866 @Override
867 public boolean onKeyDown(int keyCode, KeyEvent event) {
868 switch (keyCode) {
869 case KeyEvent.KEYCODE_CALL: {
870 try {
871 ITelephony phone = ITelephony.Stub.asInterface(
872 ServiceManager.checkService("phone"));
873 if (phone != null && !phone.isIdle()) {
874 // Skip out and let the key be handled at a higher level
875 break;
876 }
877 } catch (RemoteException re) {
878 // Fall through and try to call the contact
879 }
880
Evan Millar7911ff52009-07-21 15:55:18 -0700881 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800882 if (index != -1) {
883 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700884 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
885 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800886 }
887 } else if (mNumPhoneNumbers != 0) {
888 // There isn't anything selected, call the default number
Evan Millar6a61a1a2009-09-29 14:00:43 -0700889 long freshContactId = getRefreshedContactId();
890 if (freshContactId > 0) {
891 Uri hardContacUri = ContentUris.withAppendedId(
892 Contacts.CONTENT_URI, freshContactId);
893 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, hardContacUri);
894 startActivity(intent);
895 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800896 }
897 return true;
898 }
899
900 case KeyEvent.KEYCODE_DEL: {
Megha Joshid5afd8a2009-09-22 09:38:41 -0700901 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
902 showDialog(DIALOG_CONFIRM_READONLY_DELETE);
903 } else if (mWritableSourcesCnt > 1) {
904 showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE);
905 } else {
906 showDialog(DIALOG_CONFIRM_DELETE);
907 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800908 return true;
909 }
910 }
911
912 return super.onKeyDown(keyCode, event);
913 }
914
Evan Millar7911ff52009-07-21 15:55:18 -0700915 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800916 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
917 if (entry != null) {
918 Intent intent = entry.intent;
Evan Millar2e1b8af2009-09-24 09:39:21 -0700919 if (entry.mimetype == SPLIT_MIMETYPE) {
920 splitContact(entry.id);
921 } else if (intent != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800922 try {
923 startActivity(intent);
924 } catch (ActivityNotFoundException e) {
925 Log.e(TAG, "No activity found for intent: " + intent);
926 signalError();
927 }
928 } else {
929 signalError();
930 }
931 } else {
932 signalError();
933 }
934 }
935
936 /**
937 * Signal an error to the user via a beep, or some other method.
938 */
939 private void signalError() {
940 //TODO: implement this when we have the sonification APIs
941 }
942
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800943 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700944 // 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 -0800945 StringBuilder buf = new StringBuilder("imto://");
946 buf.append(host);
947 buf.append('/');
948 buf.append(data);
949 return Uri.parse(buf.toString());
950 }
951
952 /**
953 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700954 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800955 * @param personCursor the URI for the contact being displayed
956 */
Evan Millar11d628c2009-09-02 08:55:01 -0700957 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800958 // Clear out the old entries
959 final int numSections = mSections.size();
960 for (int i = 0; i < numSections; i++) {
961 mSections.get(i).clear();
962 }
963
Evan Millar7911ff52009-07-21 15:55:18 -0700964 mRawContactIds.clear();
Megha Joshid5afd8a2009-09-22 09:38:41 -0700965 mReadOnlySourcesCnt = 0;
966 mWritableSourcesCnt = 0;
967 mWritableRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700968
Evan Millar11d628c2009-09-02 08:55:01 -0700969 Sources sources = Sources.getInstance(this);
970
Evan Millar66388be2009-05-28 15:41:07 -0700971 // Build up method entries
Evan Millar6a61a1a2009-09-29 14:00:43 -0700972 if (mLookupUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700973 for (Entity entity: mEntities) {
974 final ContentValues entValues = entity.getEntityValues();
975 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700976 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700977
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700978 if (!mRawContactIds.contains(rawContactId)) {
979 mRawContactIds.add(rawContactId);
980 }
981
Evan Millar2e1b8af2009-09-24 09:39:21 -0700982 // This performs the tab filtering
983 if (mSelectedRawContactId != null && mSelectedRawContactId != rawContactId) {
984 continue;
985 }
Evan Millar66210452009-09-21 10:53:43 -0700986
Tadashi G. Takaoka4f56df32009-09-29 16:04:08 -0700987 final ContactsSource source = sources.getInflatedSource(accountType,
988 ContactsSource.LEVEL_SUMMARY);
Megha Joshid5afd8a2009-09-22 09:38:41 -0700989 if (source.readOnly) {
990 mReadOnlySourcesCnt += 1;
991 } else {
992 mWritableSourcesCnt += 1;
993 mWritableRawContactIds.add(rawContactId);
994 }
Tadashi G. Takaokab4a72982009-09-29 13:08:28 -0700995 final String accountName = entValues.getAsString(RawContacts.ACCOUNT_NAME);
Tadashi G. Takaoka4f56df32009-09-29 16:04:08 -0700996 mAccountName.setText(getString(R.string.account_name_format,
997 source.getDisplayLabel(this), accountName));
Tadashi G. Takaoka33aa1c62009-09-27 14:22:28 -0700998
Evan Millar11d628c2009-09-02 08:55:01 -0700999 for (NamedContentValues subValue : entity.getSubValues()) {
1000 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001001
Jeff Sharkeyab066932009-09-21 09:55:30 -07001002 final ContentValues entryValues = subValue.values;
1003 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
1004
Evan Millar11d628c2009-09-02 08:55:01 -07001005 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -07001006 if (mimetype == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -07001007
Jeff Sharkeyab066932009-09-21 09:55:30 -07001008 final DataKind kind = sources.getKindOrFallback(accountType, mimetype, this,
Evan Millar11d628c2009-09-02 08:55:01 -07001009 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -07001010 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001011
Evan Millar11d628c2009-09-02 08:55:01 -07001012 final long id = entryValues.getAsLong(Data._ID);
1013 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
Jeff Sharkeyab066932009-09-21 09:55:30 -07001014 entry.contactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -07001015 entry.id = id;
1016 entry.uri = uri;
1017 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -07001018 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -07001019 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001020 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -07001021 entry.type = entryValues.getAsInteger(kind.typeColumn);
1022 }
1023 if (kind.iconRes > 0) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001024 entry.resPackageName = kind.resPackageName;
Evan Millar11d628c2009-09-02 08:55:01 -07001025 entry.actionIcon = kind.iconRes;
1026 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001027
Evan Millar11d628c2009-09-02 08:55:01 -07001028 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -07001029 if (TextUtils.isEmpty(entry.data)) {
Alex Kennberg87fc3172009-03-28 06:43:06 -07001030 continue;
1031 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001032
Evan Millarff04a272009-09-26 15:23:37 -07001033 final boolean isSuperPrimary = entryValues.getAsInteger(
1034 Data.IS_SUPER_PRIMARY) != 0;
Evan Millar11d628c2009-09-02 08:55:01 -07001035
Evan Millarff04a272009-09-26 15:23:37 -07001036 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
1037 // Build phone entries
1038 mNumPhoneNumbers++;
Evan Millar11d628c2009-09-02 08:55:01 -07001039
Evan Millarff04a272009-09-26 15:23:37 -07001040 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
1041 Uri.fromParts("tel", entry.data, null));
1042 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
1043 Uri.fromParts("sms", entry.data, null));
1044 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -07001045
Evan Millarff04a272009-09-26 15:23:37 -07001046 entry.isPrimary = isSuperPrimary;
1047 mPhoneEntries.add(entry);
1048
1049 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
1050 || mShowSmsLinksForAllPhones) {
1051 // Add an SMS entry
1052 if (kind.iconAltRes > 0) {
1053 entry.secondaryActionIcon = kind.iconAltRes;
Evan Millar49714ee2009-09-02 16:42:47 -07001054 }
Evan Millarff04a272009-09-26 15:23:37 -07001055 }
1056 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
1057 // Build email entries
1058 entry.intent = new Intent(Intent.ACTION_SENDTO,
1059 Uri.fromParts("mailto", entry.data, null));
1060 entry.isPrimary = isSuperPrimary;
1061 mEmailEntries.add(entry);
1062 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
1063 equals(mimetype)) {
1064 // Build postal entries
1065 entry.maxLines = 4;
1066 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
1067 mPostalEntries.add(entry);
1068 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
1069 // Build im entries
1070 Object protocolObj = entryValues.getAsInteger(CommonDataKinds.Im.PROTOCOL);
1071 String host = null;
Evan Millar49714ee2009-09-02 16:42:47 -07001072
Evan Millarff04a272009-09-26 15:23:37 -07001073 if (TextUtils.isEmpty(entry.label)) {
1074 entry.label = getString(R.string.chat).toLowerCase();
1075 }
Evan Millar11d628c2009-09-02 08:55:01 -07001076
Evan Millarff04a272009-09-26 15:23:37 -07001077 if (protocolObj instanceof Number) {
1078 int protocol = ((Number) protocolObj).intValue();
1079 host = ContactsUtils.lookupProviderNameFromId(protocol);
1080 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
1081 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
1082 entry.maxLabelLines = 2;
Evan Millar11d628c2009-09-02 08:55:01 -07001083 }
Evan Millarff04a272009-09-26 15:23:37 -07001084 } else if (protocolObj != null) {
1085 String providerName = (String) protocolObj;
1086 host = providerName.toLowerCase();
1087 }
1088
1089 // Only add the intent if there is a valid host
1090 // host is null for CommonDataKinds.Im.PROTOCOL_CUSTOM
1091 if (!TextUtils.isEmpty(host)) {
Evan Millar11d628c2009-09-02 08:55:01 -07001092 entry.intent = new Intent(Intent.ACTION_SENDTO,
Evan Millarff04a272009-09-26 15:23:37 -07001093 constructImToUrl(host.toLowerCase(), entry.data));
1094 }
1095 //TODO(emillar) Add in presence info
Evan Millar11d628c2009-09-02 08:55:01 -07001096 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
1097 entry.presenceIcon = Presence.getPresenceIconResourceId(
1098 aggCursor.getInt(METHODS_STATUS_COLUMN));
1099 entry.status = ...
1100 }*/
Evan Millarff04a272009-09-26 15:23:37 -07001101 mImEntries.add(entry);
1102 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)
1103 || CommonDataKinds.Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) {
1104 // Build organization and note entries
1105 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -07001106 mOrganizationEntries.add(entry);
1107 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
1108 // Build note entries
Evan Millar11d628c2009-09-02 08:55:01 -07001109 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -07001110 entry.maxLines = 10;
1111 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -07001112 } else {
1113 // Handle showing custom
Evan Millarff04a272009-09-26 15:23:37 -07001114 entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri);
Jeff Sharkeyab066932009-09-21 09:55:30 -07001115 mOtherEntries.add(entry);
Evan Millar11d628c2009-09-02 08:55:01 -07001116 }
1117
Evan Millar2e1b8af2009-09-24 09:39:21 -07001118 }
1119
1120 if (mSelectedRawContactId != null &&
1121 mSelectedRawContactId == rawContactId
1122 && mEntities.size() > 1) {
1123 ViewEntry entry = new ViewEntry();
1124 entry.mimetype = SPLIT_MIMETYPE;
1125 entry.id = rawContactId;
1126 entry.label = getString(R.string.split_label);
1127 entry.data = getString(R.string.split_explanation);
1128 entry.actionIcon = R.drawable.ic_list_split;
1129 mSplitEntry.add(entry);
1130 }
Evan Millar11d628c2009-09-02 08:55:01 -07001131 // TODO(emillar) Add group entries
1132 // // Build the group entries
1133 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1134 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1135 // null, null, Groups.DEFAULT_SORT_ORDER);
1136 // if (groupCursor != null) {
1137 // try {
1138 // StringBuilder sb = new StringBuilder();
1139 //
1140 // while (groupCursor.moveToNext()) {
1141 // String systemId = groupCursor.getString(
1142 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1143 //
1144 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1145 // continue;
1146 // }
1147 //
1148 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1149 // if (!TextUtils.isEmpty(name)) {
1150 // if (sb.length() == 0) {
1151 // sb.append(name);
1152 // } else {
1153 // sb.append(getString(R.string.group_list, name));
1154 // }
1155 // }
1156 // }
1157 //
1158 // if (sb.length() > 0) {
1159 // ViewEntry entry = new ViewEntry();
1160 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1161 // entry.label = getString(R.string.label_groups);
1162 // entry.data = sb.toString();
1163 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1164 //
1165 // // TODO: Add an icon for the groups item.
1166 //
1167 // mGroupEntries.add(entry);
1168 // }
1169 // } finally {
1170 // groupCursor.close();
1171 // }
1172 // }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001173
Evan Millar66388be2009-05-28 15:41:07 -07001174 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001175 }
1176 }
1177
Evan Millar11d628c2009-09-02 08:55:01 -07001178 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1179 if (kind.actionHeader == null) {
1180 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001181 }
Evan Millar11d628c2009-09-02 08:55:01 -07001182 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1183 if (actionHeader == null) {
1184 return null;
1185 }
1186 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1187 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001188
Evan Millar11d628c2009-09-02 08:55:01 -07001189 String buildDataString(DataKind kind, ContentValues values) {
1190 if (kind.actionBody == null) {
1191 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001192 }
Evan Millar11d628c2009-09-02 08:55:01 -07001193 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1194 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001195 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001196
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001197 /**
1198 * A basic structure with the data for a contact entry in the list.
1199 */
Evan Millaradb0f8c2009-09-28 17:20:50 -07001200 class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001201 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001202 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001203 public boolean isPrimary = false;
1204 public int presenceIcon = -1;
1205 public int secondaryActionIcon = -1;
1206 public Intent intent;
1207 public Intent secondaryIntent = null;
1208 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001209 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001210 public ArrayList<Long> ids = new ArrayList<Long>();
1211 public int collapseCount = 0;
1212
1213 public boolean collapseWith(ViewEntry entry) {
1214 // assert equal collapse keys
Evan Millaradb0f8c2009-09-28 17:20:50 -07001215 if (!shouldCollapseWith(entry)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001216 return false;
1217 }
1218
1219 // Choose the label associated with the highest type precedence.
1220 if (TypePrecedence.getTypePrecedence(mimetype, type)
1221 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1222 type = entry.type;
1223 label = entry.label;
1224 }
1225
1226 // Choose the max of the maxLines and maxLabelLines values.
1227 maxLines = Math.max(maxLines, entry.maxLines);
1228 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1229
1230 // Choose the presence with the highest precedence.
1231 if (Presence.getPresencePrecedence(status)
1232 < Presence.getPresencePrecedence(entry.status)) {
1233 status = entry.status;
1234 }
1235
1236 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001237 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001238
1239 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1240
1241 // Keep track of all the ids that have been collapsed with this one.
1242 ids.add(entry.id);
1243 collapseCount++;
1244 return true;
1245 }
1246
Evan Millaradb0f8c2009-09-28 17:20:50 -07001247 public boolean shouldCollapseWith(ViewEntry entry) {
1248 if (entry == null) {
1249 return false;
1250 }
1251
1252 if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)
1253 && Phone.CONTENT_ITEM_TYPE.equals(entry.mimetype)) {
1254 if (!PhoneNumberUtils.compare(ViewContactActivity.this, data, entry.data)) {
1255 return false;
1256 }
1257 } else {
1258 if (!equals(data, entry.data)) {
1259 return false;
1260 }
1261 }
1262
1263 if (!equals(mimetype, entry.mimetype)
1264 || !intentCollapsible(intent, entry.intent)
1265 || !intentCollapsible(secondaryIntent, entry.secondaryIntent)
1266 || actionIcon != entry.actionIcon) {
1267 return false;
1268 }
1269
1270 return true;
1271 }
1272
1273 private boolean equals(Object a, Object b) {
1274 return a==b || (a != null && a.equals(b));
1275 }
1276
1277 private boolean intentCollapsible(Intent a, Intent b) {
1278 if (a == b) {
1279 return true;
1280 } else if ((a != null && b != null) && equals(a.getAction(), b.getAction())) {
1281 return true;
1282 }
1283 return false;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001284 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001285 }
1286
Evan Millar15e514d2009-08-04 10:14:57 -07001287 /** Cache of the children views of a row */
1288 static class ViewCache {
1289 public TextView label;
1290 public TextView data;
1291 public ImageView actionIcon;
1292 public ImageView presenceIcon;
1293 public ImageView primaryIcon;
1294 public ImageView secondaryActionButton;
1295 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001296
Evan Millar15e514d2009-08-04 10:14:57 -07001297 // Need to keep track of this too
1298 ViewEntry entry;
1299 }
1300
1301 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1302 implements View.OnClickListener {
1303
Evan Millar5c22c3b2009-05-29 11:37:54 -07001304
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001305 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1306 super(context, sections, SHOW_SEPARATORS);
1307 }
1308
Evan Millar15e514d2009-08-04 10:14:57 -07001309 public void onClick(View v) {
1310 Intent intent = (Intent) v.getTag();
1311 startActivity(intent);
1312 }
1313
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001314 @Override
1315 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001316 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001317 View v;
1318
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001319 ViewCache views;
1320
1321 // Check to see if we can reuse convertView
1322 if (convertView != null) {
1323 v = convertView;
1324 views = (ViewCache) v.getTag();
1325 } else {
1326 // Create a new view if needed
1327 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1328
1329 // Cache the children
1330 views = new ViewCache();
1331 views.label = (TextView) v.findViewById(android.R.id.text1);
1332 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001333 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1334 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1335 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1336 views.secondaryActionButton = (ImageView) v.findViewById(
1337 R.id.secondary_action_button);
1338 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001339 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001340 v.setTag(views);
1341 }
1342
1343 // Update the entry in the view cache
1344 views.entry = entry;
1345
1346 // Bind the data to the view
1347 bindView(v, entry);
1348 return v;
1349 }
1350
1351 @Override
1352 protected View newView(int position, ViewGroup parent) {
1353 // getView() handles this
1354 throw new UnsupportedOperationException();
1355 }
1356
1357 @Override
1358 protected void bindView(View view, ViewEntry entry) {
1359 final Resources resources = mContext.getResources();
1360 ViewCache views = (ViewCache) view.getTag();
1361
1362 // Set the label
1363 TextView label = views.label;
1364 setMaxLines(label, entry.maxLabelLines);
1365 label.setText(entry.label);
1366
1367 // Set the data
1368 TextView data = views.data;
1369 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001370 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001371 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001372 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1373 } else {
1374 data.setText(entry.data);
1375 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001376 setMaxLines(data, entry.maxLines);
1377 }
1378
Evan Millar15e514d2009-08-04 10:14:57 -07001379 // Set the primary icon
1380 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1381
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001382 // Set the action icon
1383 ImageView action = views.actionIcon;
1384 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001385 Drawable actionIcon;
1386 if (entry.resPackageName != null) {
1387 // Load external resources through PackageManager
1388 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1389 entry.actionIcon, null);
1390 } else {
1391 actionIcon = resources.getDrawable(entry.actionIcon);
1392 }
1393 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001394 action.setVisibility(View.VISIBLE);
1395 } else {
1396 // Things should still line up as if there was an icon, so make it invisible
1397 action.setVisibility(View.INVISIBLE);
1398 }
1399
1400 // Set the presence icon
1401 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001402 if (entry.presenceIcon != -1) {
1403 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001404 } else if (entry.status != -1) {
1405 presenceIcon = resources.getDrawable(
1406 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001407 }
Evan Millar15e514d2009-08-04 10:14:57 -07001408 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001409 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001410 presenceIconView.setImageDrawable(presenceIcon);
1411 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001412 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001413 presenceIconView.setVisibility(View.GONE);
1414 }
1415
1416 // Set the secondary action button
1417 ImageView secondaryActionView = views.secondaryActionButton;
1418 Drawable secondaryActionIcon = null;
1419 if (entry.secondaryActionIcon != -1) {
1420 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1421 }
1422 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1423 secondaryActionView.setImageDrawable(secondaryActionIcon);
1424 secondaryActionView.setTag(entry.secondaryIntent);
1425 secondaryActionView.setVisibility(View.VISIBLE);
1426 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1427 } else {
1428 secondaryActionView.setVisibility(View.GONE);
1429 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001430 }
1431 }
1432
1433 private void setMaxLines(TextView textView, int maxLines) {
1434 if (maxLines == 1) {
1435 textView.setSingleLine(true);
1436 textView.setEllipsize(TextUtils.TruncateAt.END);
1437 } else {
1438 textView.setSingleLine(false);
1439 textView.setMaxLines(maxLines);
1440 textView.setEllipsize(null);
1441 }
1442 }
1443 }
1444}