blob: bec871ff60ff830f05c0a07f0aca29f0ce4aa69c [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;
102
Evan Millar8a79cee2009-08-19 17:20:49 -0700103 private static final int REQUEST_JOIN_CONTACT = 1;
Evan Millar9cd81242009-09-26 16:02:31 -0700104 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700105
Evan Millar8a79cee2009-08-19 17:20:49 -0700106 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800107
Evan Millar2e1b8af2009-09-24 09:39:21 -0700108 private static final String SPLIT_MIMETYPE = "split_mimetype";
109
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700110 protected Uri mLookupUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800111 private ContentResolver mResolver;
112 private ViewAdapter mAdapter;
113 private int mNumPhoneNumbers = 0;
114
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700115 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700116 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700117 */
Evan Millar7911ff52009-07-21 15:55:18 -0700118 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700119
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800120 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
121 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
122 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
123 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
124 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
125 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700126 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800127 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
Evan Millar2e1b8af2009-09-24 09:39:21 -0700128 /* package */ ArrayList<ViewEntry> mSplitEntry = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800129 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
130
131 private Cursor mCursor;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700132
Evan Millar11d628c2009-09-02 08:55:01 -0700133 private SparseArray<Long> mTabRawContactIdMap;
134 protected ScrollingTabWidget mTabWidget;
135 protected ContactHeaderWidget mContactHeaderWidget;
Evan Millar2e1b8af2009-09-24 09:39:21 -0700136 protected View mBelowHeader;
Tadashi G. Takaoka33aa1c62009-09-27 14:22:28 -0700137 protected TextView mAccountName;
Evan Millar2e1b8af2009-09-24 09:39:21 -0700138 protected View mBufferView;
Evan Millar11d628c2009-09-02 08:55:01 -0700139 private NotifyingAsyncQueryHandler mHandler;
140
141 protected LayoutInflater mInflater;
142
143 //Projection used for the query that determines which tabs to add.
144 protected static final String[] TAB_PROJECTION = new String[] {
145 RawContacts._ID,
146 RawContacts.ACCOUNT_NAME,
147 RawContacts.ACCOUNT_TYPE
148 };
149 protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0;
150 protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1;
151 protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2;
152
Evan Millar9cd81242009-09-26 16:02:31 -0700153 private static final String SAVED_STATE_SELECTED_RAW_CONTACT_ID_KEY = "selectedRawContactKey";
154 private static final String SAVED_STATE_TABS_VISIBLE_KEY = "tabsVisibleKey";
155
Evan Millar11d628c2009-09-02 08:55:01 -0700156 protected Long mSelectedRawContactId = null;
157
158 private static final int TOKEN_QUERY = 0;
159
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800160 private ContentObserver mObserver = new ContentObserver(new Handler()) {
161 @Override
162 public boolean deliverSelfNotifications() {
163 return true;
164 }
165
166 @Override
167 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700168 if (mCursor != null && !mCursor.isClosed()) {
169 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800170 }
171 }
172 };
173
174 public void onClick(DialogInterface dialog, int which) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700175 closeCursor();
Evan Millar6a61a1a2009-09-29 14:00:43 -0700176 getContentResolver().delete(mLookupUri, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800177 finish();
178 }
179
Evan Millar7911ff52009-07-21 15:55:18 -0700180 private FrameLayout mTabContentLayout;
181 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800182 private boolean mShowSmsLinksForAllPhones;
Evan Millar11d628c2009-09-02 08:55:01 -0700183 private ArrayList<Entity> mEntities = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800184
Evan Millar2e1b8af2009-09-24 09:39:21 -0700185 private boolean mTabsVisible;
186
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800187 @Override
188 protected void onCreate(Bundle icicle) {
189 super.onCreate(icicle);
190
Evan Millar11d628c2009-09-02 08:55:01 -0700191 final Intent intent = getIntent();
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700192 Uri data = intent.getData();
193 String authority = data.getAuthority();
194 if (ContactsContract.AUTHORITY.equals(authority)) {
195 mLookupUri = data;
196 } else if (android.provider.Contacts.AUTHORITY.equals(authority)) {
197 final long rawContactId = ContentUris.parseId(data);
198 mLookupUri = RawContacts.getContactLookupUri(getContentResolver(),
199 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700200
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700201 }
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700202 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700203
204 requestWindowFeature(Window.FEATURE_NO_TITLE);
205 setContentView(R.layout.contact_card_layout);
206
207 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
208 mContactHeaderWidget.showStar(true);
Evan Millar11d628c2009-09-02 08:55:01 -0700209 mContactHeaderWidget.setExcludeMimes(new String[] {
210 Contacts.CONTENT_ITEM_TYPE
211 });
212
213 mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700214 mTabWidget.setTabSelectionListener(this);
Evan Millar85863112009-09-21 14:35:18 -0700215 mTabWidget.setVisibility(View.GONE);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700216 mTabsVisible = false;
Tadashi G. Takaokab4a72982009-09-29 13:08:28 -0700217 mAccountName = (TextView) mTabWidget.findViewById(R.id.account_name);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700218
219 mBelowHeader = findViewById(R.id.below_header);
Evan Millar11d628c2009-09-02 08:55:01 -0700220
221 mTabRawContactIdMap = new SparseArray<Long>();
222
223 mHandler = new NotifyingAsyncQueryHandler(this, this);
224
Evan Millar7911ff52009-07-21 15:55:18 -0700225 mListView = new ListView(this);
226 mListView.setOnCreateContextMenuListener(this);
227 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
228 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800229
Evan Millar7911ff52009-07-21 15:55:18 -0700230 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
231 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800232
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800233 mResolver = getContentResolver();
234
235 // Build the list of sections. The order they're added to mSections dictates the
236 // order they are displayed in the list.
237 mSections.add(mPhoneEntries);
238 mSections.add(mSmsEntries);
239 mSections.add(mEmailEntries);
240 mSections.add(mImEntries);
241 mSections.add(mPostalEntries);
242 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700243 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800244 mSections.add(mOtherEntries);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700245 mSections.add(mSplitEntry);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800246
247 //TODO Read this value from a preference
248 mShowSmsLinksForAllPhones = true;
Evan Millar11d628c2009-09-02 08:55:01 -0700249 }
250
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800251 @Override
Evan Millar9cd81242009-09-26 16:02:31 -0700252 protected void onRestoreInstanceState(Bundle savedInstanceState) {
253 super.onRestoreInstanceState(savedInstanceState);
254 long restoredRawContactId = savedInstanceState.getLong(
255 SAVED_STATE_SELECTED_RAW_CONTACT_ID_KEY, -1);
256 mSelectedRawContactId = restoredRawContactId != -1 ? restoredRawContactId : null;
257 mTabsVisible = savedInstanceState.getBoolean(SAVED_STATE_TABS_VISIBLE_KEY);
258 }
259
260 @Override
261 protected void onSaveInstanceState(Bundle outState) {
262 super.onSaveInstanceState(outState);
263 if (mSelectedRawContactId != null) {
264 outState.putLong(SAVED_STATE_SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
265
266 }
267 outState.putBoolean(SAVED_STATE_TABS_VISIBLE_KEY, mTabsVisible);
268 }
269
270 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800271 protected void onResume() {
272 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700273 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800274 }
275
276 @Override
277 protected void onPause() {
278 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700279 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800280 }
281
282 @Override
283 protected void onDestroy() {
284 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700285 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800286 }
287
288 @Override
289 protected Dialog onCreateDialog(int id) {
290 switch (id) {
291 case DIALOG_CONFIRM_DELETE:
292 return new AlertDialog.Builder(this)
293 .setTitle(R.string.deleteConfirmation_title)
294 .setIcon(android.R.drawable.ic_dialog_alert)
295 .setMessage(R.string.deleteConfirmation)
296 .setNegativeButton(android.R.string.cancel, null)
297 .setPositiveButton(android.R.string.ok, this)
298 .setCancelable(false)
299 .create();
300 }
301 return null;
302 }
303
Evan Millar11d628c2009-09-02 08:55:01 -0700304
305 // TAB CODE //
306 /**
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700307 * Adds a tab for each {@link RawContacts} associated with this contact.
Evan Millar11d628c2009-09-02 08:55:01 -0700308 * Override this method if you want to additional tabs and/or different
309 * tabs for your activity.
Evan Millar11d628c2009-09-02 08:55:01 -0700310 */
Evan Millar2e1b8af2009-09-24 09:39:21 -0700311 protected void bindTabs() {
312 final Sources sources = Sources.getInstance(this);
313
314 for (Entity entity : mEntities) {
315 final String accountType = entity.getEntityValues().
316 getAsString(RawContacts.ACCOUNT_TYPE);
317 final Long rawContactId = entity.getEntityValues().
318 getAsLong(RawContacts._ID);
319
320 // TODO: ensure inflation on background task so we don't block UI thread here
321 final ContactsSource source = sources.getInflatedSource(accountType,
322 ContactsSource.LEVEL_SUMMARY);
323 addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(),
324 source));
325 }
326 }
327
328 /**
329 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
330 *
331 * @param rawContactId The contact id associated with the tab.
332 * @param view A view to use as the tab indicator.
333 */
334 protected void addTab(long rawContactId, View view) {
335 mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId);
336 mTabWidget.addTab(view);
337 }
338
339
340 protected void clearCurrentTabs() {
341 mTabRawContactIdMap.clear();
342 mTabWidget.removeAllTabs();
343 }
344
345 protected void selectInitialTab() {
346 int selectedTabIndex = 0;
347
348 if (mSelectedRawContactId != null) {
349 selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
350 if (selectedTabIndex == -1) {
351 // If there was no matching tab, just select the first;
352 selectedTabIndex = 0;
353 }
354 }
355
356 mTabWidget.setCurrentTab(selectedTabIndex);
357 onTabSelectionChanged(selectedTabIndex, false);
358 }
359
360 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
361 Long rawContactId = getTabRawContactId(tabIndex);
362 if (rawContactId != null) {
363 mSelectedRawContactId = rawContactId;
364 bindData();
365 }
366 }
367
368 /**
369 * Return the RawContact id associated with the tab at an index.
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 Long getTabRawContactId(int index) {
375 return mTabRawContactIdMap.get(index);
376 }
377
378 /**
379 * Return the tab index associated with the RawContact id.
380 *
381 * @param index The index of the tab in question.
382 * @return The contactId associated with the tab at the specified index.
383 */
384 protected int getTabIndexForRawContactId(long rawContactId) {
385 int numTabs = mTabRawContactIdMap.size();
386 for (int i=0; i < numTabs; i++) {
387 if (mTabRawContactIdMap.get(i) == rawContactId) {
388 return i;
389 }
390 }
391 return -1;
392 }
393
Evan Millar9cd81242009-09-26 16:02:31 -0700394 protected void showTabs(boolean show) {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700395 if (mTabsVisible == show) {
396 return;
397 }
398
Tadashi G. Takaokab4a72982009-09-29 13:08:28 -0700399 final Resources resources = getResources();
400 final float tabHeight = resources.getDimension(R.dimen.tab_height)
401 + resources.getDimension(R.dimen.account_name_height);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700402 if (show) {
Evan Millar9cd81242009-09-26 16:02:31 -0700403 TranslateAnimation showAnimation = new TranslateAnimation(
404 Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
405 Animation.ABSOLUTE, -tabHeight, Animation.ABSOLUTE, 0);
Tadashi G. Takaokab4a72982009-09-29 13:08:28 -0700406 showAnimation.setDuration(resources.getInteger(
Evan Millar9cd81242009-09-26 16:02:31 -0700407 android.R.integer.config_longAnimTime));
Evan Millar2e1b8af2009-09-24 09:39:21 -0700408
Evan Millar9cd81242009-09-26 16:02:31 -0700409 showAnimation.setAnimationListener(new AnimationListener() {
410 public void onAnimationEnd(Animation animation) {
411 selectInitialTab();
412 bindData();
413 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700414
Evan Millar9cd81242009-09-26 16:02:31 -0700415 public void onAnimationRepeat(Animation animation) {
416 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700417
Evan Millar9cd81242009-09-26 16:02:31 -0700418 public void onAnimationStart(Animation animation) {
419 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700420
Evan Millar9cd81242009-09-26 16:02:31 -0700421 });
Evan Millar2e1b8af2009-09-24 09:39:21 -0700422
Evan Millar9cd81242009-09-26 16:02:31 -0700423 mBelowHeader.startAnimation(showAnimation);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700424 mTabWidget.setVisibility(View.VISIBLE);
425 mTabsVisible = true;
426 clearCurrentTabs();
427 bindTabs();
Evan Millar2e1b8af2009-09-24 09:39:21 -0700428 } else {
Evan Millar9cd81242009-09-26 16:02:31 -0700429 TranslateAnimation hideTabsAnimation = new TranslateAnimation(
430 Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
431 Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -tabHeight);
Tadashi G. Takaokab4a72982009-09-29 13:08:28 -0700432 hideTabsAnimation.setDuration(resources.getInteger(
Evan Millar9cd81242009-09-26 16:02:31 -0700433 android.R.integer.config_longAnimTime));
434 hideTabsAnimation.setAnimationListener(new AnimationListener() {
435 public void onAnimationEnd(Animation animation) {
436 bindData();
437 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700438
Evan Millar9cd81242009-09-26 16:02:31 -0700439 public void onAnimationRepeat(Animation animation) {
440 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700441
Evan Millar9cd81242009-09-26 16:02:31 -0700442 public void onAnimationStart(Animation animation) {
443 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700444
Evan Millar9cd81242009-09-26 16:02:31 -0700445 });
Evan Millar2e1b8af2009-09-24 09:39:21 -0700446
Evan Millar9cd81242009-09-26 16:02:31 -0700447 TranslateAnimation hideListAnimation = new TranslateAnimation(
448 Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
449 Animation.ABSOLUTE, tabHeight, Animation.ABSOLUTE, 0);
Tadashi G. Takaokab4a72982009-09-29 13:08:28 -0700450 hideListAnimation.setDuration(resources.getInteger(
Evan Millar9cd81242009-09-26 16:02:31 -0700451 android.R.integer.config_longAnimTime));
Evan Millar2e1b8af2009-09-24 09:39:21 -0700452
453
Evan Millar9cd81242009-09-26 16:02:31 -0700454 mTabWidget.startAnimation(hideTabsAnimation);
455 mTabContentLayout.startAnimation(hideListAnimation);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700456 mTabWidget.setVisibility(View.GONE);
457 mTabsVisible = false;
458 mSelectedRawContactId = null;
Evan Millar2e1b8af2009-09-24 09:39:21 -0700459 }
460 }
Evan Millar11d628c2009-09-02 08:55:01 -0700461
462
463 // QUERY CODE //
464 /** {@inheritDoc} */
465 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
466 try{
467 if (token == TOKEN_QUERY) {
Evan Millar11d628c2009-09-02 08:55:01 -0700468 mEntities = readEntities(iterator);
Evan Millar85863112009-09-21 14:35:18 -0700469 // Show the aggregate badge if this contact is aggregated.
Evan Millarcc87fb92009-09-22 15:27:47 -0700470 boolean isAggregate = mEntities.size() > 1;
471 mContactHeaderWidget.showAggregateBadge(isAggregate);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700472 if (mTabsVisible) {
Evan Millar9cd81242009-09-26 16:02:31 -0700473 mTabWidget.setVisibility(View.VISIBLE);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700474 clearCurrentTabs();
475 bindTabs();
476 selectInitialTab();
Evan Millar9cd81242009-09-26 16:02:31 -0700477 } else {
478 mTabWidget.setVisibility(View.GONE);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700479 }
Evan Millar85863112009-09-21 14:35:18 -0700480 bindData();
Evan Millar11d628c2009-09-02 08:55:01 -0700481 }
482 } finally {
483 if (iterator != null) {
484 iterator.close();
485 }
486 }
487 }
488
489 /** {@inheritDoc} */
490 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700491 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700492 }
493
Evan Millar6a61a1a2009-09-29 14:00:43 -0700494 private long getRefreshedContactId() {
495 Uri freshContactUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
496 if (freshContactUri != null) {
497 return ContentUris.parseId(freshContactUri);
498 }
499 return -1;
500 }
501
Evan Millar11d628c2009-09-02 08:55:01 -0700502 private ArrayList<Entity> readEntities(EntityIterator iterator) {
503 ArrayList<Entity> entities = new ArrayList<Entity>();
504 try {
505 while (iterator.hasNext()) {
506 entities.add(iterator.next());
507 }
508 } catch (RemoteException e) {
509 }
510
511 return entities;
512 }
513
514 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700515 closeCursor();
516
Evan Millar6a61a1a2009-09-29 14:00:43 -0700517 Uri uri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700518 if (mLookupUri != null) {
519 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
520 if (mLookupUri != null) {
Evan Millar6a61a1a2009-09-29 14:00:43 -0700521 uri = Contacts.lookupContact(getContentResolver(), mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700522 }
523 }
524
Evan Millar6a61a1a2009-09-29 14:00:43 -0700525 if (uri == null) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700526
527 // TODO either figure out a way to prevent a flash of black background or
528 // use some other UI than a toast
529 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
530 Log.e(TAG, "invalid contact uri: " + mLookupUri);
531 finish();
532 return;
533 }
534
Evan Millar6a61a1a2009-09-29 14:00:43 -0700535 mCursor = mResolver.query(Uri.withAppendedPath(uri, Contacts.Data.CONTENT_DIRECTORY),
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700536 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
537 mCursor.registerContentObserver(mObserver);
538
Evan Millar6a61a1a2009-09-29 14:00:43 -0700539 long contactId = ContentUris.parseId(uri);
Evan Millar11d628c2009-09-02 08:55:01 -0700540 mHandler.startQueryEntities(TOKEN_QUERY, null,
541 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700542
Evan Millar9b1a1242009-09-16 10:26:10 -0700543 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700544 }
545
546 private void closeCursor() {
547 if (mCursor != null) {
548 mCursor.unregisterContentObserver(mObserver);
549 mCursor.close();
550 mCursor = null;
551 }
Evan Millar11d628c2009-09-02 08:55:01 -0700552 }
553
554 private void bindData() {
555
556 // Build up the contact entries
557 buildEntries();
558
559 // Collapse similar data items in select sections.
560 Collapser.collapseList(mPhoneEntries);
561 Collapser.collapseList(mSmsEntries);
562 Collapser.collapseList(mEmailEntries);
563 Collapser.collapseList(mPostalEntries);
564
565 if (mAdapter == null) {
566 mAdapter = new ViewAdapter(this, mSections);
567 mListView.setAdapter(mAdapter);
568 } else {
569 mAdapter.setSections(mSections, SHOW_SEPARATORS);
570 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800571 }
572
573 @Override
574 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700575 super.onCreateOptionsMenu(menu);
576
577 final MenuInflater inflater = getMenuInflater();
578 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800579 return true;
580 }
581
582 @Override
583 public boolean onPrepareOptionsMenu(Menu menu) {
584 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700585
Evan Millar2e1b8af2009-09-24 09:39:21 -0700586 // If tabWidget is not shown enable "show sources", otherwise
587 // enable "hide sources"
588 if (mTabsVisible) {
589 menu.findItem(R.id.menu_show_sources).setVisible(false);
590 menu.findItem(R.id.menu_hide_sources).setVisible(true);
591 } else {
592 menu.findItem(R.id.menu_show_sources).setVisible(true);
593 menu.findItem(R.id.menu_hide_sources).setVisible(false);
594 }
595
Evan Millar51514db2009-09-22 11:42:35 -0700596 // Only allow edit when we have at least one raw_contact id
597 final boolean hasRawContact = (mRawContactIds.size() > 0);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700598 menu.findItem(R.id.menu_edit).setEnabled(hasRawContact);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700599
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800600 return true;
601 }
602
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800603 @Override
604 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
605 AdapterView.AdapterContextMenuInfo info;
606 try {
607 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
608 } catch (ClassCastException e) {
609 Log.e(TAG, "bad menuInfo", e);
610 return;
611 }
612
613 // This can be null sometimes, don't crash...
614 if (info == null) {
615 Log.e(TAG, "bad menuInfo");
616 return;
617 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700618
Evan Millar45e0ed32009-06-01 16:44:38 -0700619 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
Neel Parekha8fb68a2009-09-25 18:05:18 -0700620 menu.setHeaderTitle(R.string.contactOptionsTitle);
Evan Millar45e0ed32009-06-01 16:44:38 -0700621 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
622 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700623 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
624 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700625 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800626 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700627 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
628 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700629 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700630 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800631 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700632 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700633 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
634 }
635 // TODO(emillar): add back with group support.
636 /* else if (entry.mimetype.equals()) {
637 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
638 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800639 }
640
641 @Override
642 public boolean onOptionsItemSelected(MenuItem item) {
643 switch (item.getItemId()) {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700644 case R.id.menu_show_sources: {
Evan Millar9cd81242009-09-26 16:02:31 -0700645 showTabs(true);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700646 break;
647 }
648 case R.id.menu_hide_sources: {
Evan Millar9cd81242009-09-26 16:02:31 -0700649 showTabs(false);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700650 break;
651 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700652 case R.id.menu_edit: {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700653 Long rawContactIdToEdit = mSelectedRawContactId;
654 if (rawContactIdToEdit == null) {
655 if (mRawContactIds.size() > 0) {
656 rawContactIdToEdit = mRawContactIds.get(0);
657 } else {
658 // There is no rawContact to edit.
659 break;
660 }
Evan Millardb5d88c2009-08-28 09:31:57 -0700661 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700662 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
663 rawContactIdToEdit);
Evan Millar9cd81242009-09-26 16:02:31 -0700664 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
665 REQUEST_EDIT_CONTACT);
Evan Millar8a79cee2009-08-19 17:20:49 -0700666 break;
667 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700668 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800669 // Get confirmation
670 showDialog(DIALOG_CONFIRM_DELETE);
671 return true;
672 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700673 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700674 showJoinAggregateActivity();
675 return true;
676 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700677 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700678 showOptionsActivity();
679 return true;
680 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700681 case R.id.menu_share: {
Jeff Sharkey93049312009-09-27 17:28:13 -0700682 // TODO: Keep around actual LOOKUP_KEY, or formalize method of extracting
683 final String lookupKey = mLookupUri.getPathSegments().get(2);
684 final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
685
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700686 final Intent intent = new Intent(Intent.ACTION_SEND);
Jeff Sharkey93049312009-09-27 17:28:13 -0700687 intent.setType(Contacts.CONTENT_VCARD_TYPE);
688 intent.putExtra(Intent.EXTRA_STREAM, shareUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700689
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700690 // Launch chooser to share contact via
691 final CharSequence chooseTitle = getText(R.string.share_via);
692 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700693
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700694 try {
695 startActivity(chooseIntent);
696 } catch (ActivityNotFoundException ex) {
697 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800698 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700699 return true;
700 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800701 }
702 return super.onOptionsItemSelected(item);
703 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700704
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800705 @Override
706 public boolean onContextItemSelected(MenuItem item) {
707 switch (item.getItemId()) {
708 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700709 if (makeItemDefault(item)) {
710 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800711 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700712 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800713 }
714 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700715
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800716 return super.onContextItemSelected(item);
717 }
718
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700719 private boolean makeItemDefault(MenuItem item) {
720 ViewEntry entry = getViewEntryForMenuItem(item);
721 if (entry == null) {
722 return false;
723 }
724
725 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700726 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700727 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700728 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
729 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700730 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700731 return true;
732 }
733
734 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700735 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700736 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700737 public void showJoinAggregateActivity() {
Evan Millar6a61a1a2009-09-29 14:00:43 -0700738 long freshId = getRefreshedContactId();
739 if (freshId > 0) {
740 String displayName = null;
741 if (mCursor.moveToFirst()) {
742 displayName = mCursor.getString(0);
743 }
744 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
745 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, freshId);
746 if (displayName != null) {
747 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_NAME, displayName);
748 }
749 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikova5cfca32009-09-29 18:13:08 -0700750 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700751 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700752
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700753 @Override
754 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700755 if (requestCode == REQUEST_JOIN_CONTACT) {
756 if (resultCode == RESULT_OK && intent != null) {
757 final long contactId = ContentUris.parseId(intent.getData());
758 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700759 }
Evan Millar9cd81242009-09-26 16:02:31 -0700760 } else if (requestCode == REQUEST_EDIT_CONTACT) {
761 mTabsVisible = false;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700762 }
763 }
764
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700765 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700766 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700767
768 // The split operation may have removed the original aggregate contact, so we need
769 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700770 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700771 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700772 }
773
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700774 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700775 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700776 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700777
778 try {
779 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700780 long rawContactId = c.getLong(0);
781 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700782 }
783 } finally {
784 c.close();
785 }
786
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700787 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
788 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700789 }
790
791 /**
792 * Given a contact ID sets an aggregation exception to either join the contact with the
793 * current aggregate or split off.
794 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700795 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700796 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700797 for (long aRawContactId : mRawContactIds) {
798 if (aRawContactId != rawContactId) {
799 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
800 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
801 values.put(AggregationExceptions.TYPE, exceptionType);
802 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
803 }
804 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700805 }
806
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700807 private void showOptionsActivity() {
808 final Intent intent = new Intent(this, ContactOptionsActivity.class);
Evan Millar6a61a1a2009-09-29 14:00:43 -0700809 intent.setData(mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700810 startActivity(intent);
811 }
812
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700813 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
814 AdapterView.AdapterContextMenuInfo info;
815 try {
816 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
817 } catch (ClassCastException e) {
818 Log.e(TAG, "bad menuInfo", e);
819 return null;
820 }
821
822 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
823 }
824
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800825 @Override
826 public boolean onKeyDown(int keyCode, KeyEvent event) {
827 switch (keyCode) {
828 case KeyEvent.KEYCODE_CALL: {
829 try {
830 ITelephony phone = ITelephony.Stub.asInterface(
831 ServiceManager.checkService("phone"));
832 if (phone != null && !phone.isIdle()) {
833 // Skip out and let the key be handled at a higher level
834 break;
835 }
836 } catch (RemoteException re) {
837 // Fall through and try to call the contact
838 }
839
Evan Millar7911ff52009-07-21 15:55:18 -0700840 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800841 if (index != -1) {
842 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700843 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
844 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800845 }
846 } else if (mNumPhoneNumbers != 0) {
847 // There isn't anything selected, call the default number
Evan Millar6a61a1a2009-09-29 14:00:43 -0700848 long freshContactId = getRefreshedContactId();
849 if (freshContactId > 0) {
850 Uri hardContacUri = ContentUris.withAppendedId(
851 Contacts.CONTENT_URI, freshContactId);
852 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, hardContacUri);
853 startActivity(intent);
854 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800855 }
856 return true;
857 }
858
859 case KeyEvent.KEYCODE_DEL: {
860 showDialog(DIALOG_CONFIRM_DELETE);
861 return true;
862 }
863 }
864
865 return super.onKeyDown(keyCode, event);
866 }
867
Evan Millar7911ff52009-07-21 15:55:18 -0700868 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800869 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
870 if (entry != null) {
871 Intent intent = entry.intent;
Evan Millar2e1b8af2009-09-24 09:39:21 -0700872 if (entry.mimetype == SPLIT_MIMETYPE) {
873 splitContact(entry.id);
874 } else if (intent != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800875 try {
876 startActivity(intent);
877 } catch (ActivityNotFoundException e) {
878 Log.e(TAG, "No activity found for intent: " + intent);
879 signalError();
880 }
881 } else {
882 signalError();
883 }
884 } else {
885 signalError();
886 }
887 }
888
889 /**
890 * Signal an error to the user via a beep, or some other method.
891 */
892 private void signalError() {
893 //TODO: implement this when we have the sonification APIs
894 }
895
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800896 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700897 // 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 -0800898 StringBuilder buf = new StringBuilder("imto://");
899 buf.append(host);
900 buf.append('/');
901 buf.append(data);
902 return Uri.parse(buf.toString());
903 }
904
905 /**
906 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700907 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800908 * @param personCursor the URI for the contact being displayed
909 */
Evan Millar11d628c2009-09-02 08:55:01 -0700910 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800911 // Clear out the old entries
912 final int numSections = mSections.size();
913 for (int i = 0; i < numSections; i++) {
914 mSections.get(i).clear();
915 }
916
Evan Millar7911ff52009-07-21 15:55:18 -0700917 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700918
Evan Millar11d628c2009-09-02 08:55:01 -0700919 Sources sources = Sources.getInstance(this);
920
Evan Millar66388be2009-05-28 15:41:07 -0700921 // Build up method entries
Evan Millar6a61a1a2009-09-29 14:00:43 -0700922 if (mLookupUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700923 for (Entity entity: mEntities) {
924 final ContentValues entValues = entity.getEntityValues();
925 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700926 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700927
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700928 if (!mRawContactIds.contains(rawContactId)) {
929 mRawContactIds.add(rawContactId);
930 }
931
Evan Millar2e1b8af2009-09-24 09:39:21 -0700932 // This performs the tab filtering
933 if (mSelectedRawContactId != null && mSelectedRawContactId != rawContactId) {
934 continue;
935 }
Evan Millar66210452009-09-21 10:53:43 -0700936
Tadashi G. Takaoka4f56df32009-09-29 16:04:08 -0700937 final ContactsSource source = sources.getInflatedSource(accountType,
938 ContactsSource.LEVEL_SUMMARY);
Tadashi G. Takaokab4a72982009-09-29 13:08:28 -0700939 final String accountName = entValues.getAsString(RawContacts.ACCOUNT_NAME);
Tadashi G. Takaoka4f56df32009-09-29 16:04:08 -0700940 mAccountName.setText(getString(R.string.account_name_format,
941 source.getDisplayLabel(this), accountName));
Tadashi G. Takaoka33aa1c62009-09-27 14:22:28 -0700942
Evan Millar11d628c2009-09-02 08:55:01 -0700943 for (NamedContentValues subValue : entity.getSubValues()) {
944 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800945
Jeff Sharkeyab066932009-09-21 09:55:30 -0700946 final ContentValues entryValues = subValue.values;
947 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
948
Evan Millar11d628c2009-09-02 08:55:01 -0700949 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700950 if (mimetype == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -0700951
Jeff Sharkeyab066932009-09-21 09:55:30 -0700952 final DataKind kind = sources.getKindOrFallback(accountType, mimetype, this,
Evan Millar11d628c2009-09-02 08:55:01 -0700953 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700954 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800955
Evan Millar11d628c2009-09-02 08:55:01 -0700956 final long id = entryValues.getAsLong(Data._ID);
957 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700958 entry.contactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700959 entry.id = id;
960 entry.uri = uri;
961 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -0700962 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -0700963 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700964 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700965 entry.type = entryValues.getAsInteger(kind.typeColumn);
966 }
967 if (kind.iconRes > 0) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700968 entry.resPackageName = kind.resPackageName;
Evan Millar11d628c2009-09-02 08:55:01 -0700969 entry.actionIcon = kind.iconRes;
970 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800971
Evan Millar11d628c2009-09-02 08:55:01 -0700972 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700973 if (TextUtils.isEmpty(entry.data)) {
Alex Kennberg87fc3172009-03-28 06:43:06 -0700974 continue;
975 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700976
Evan Millarff04a272009-09-26 15:23:37 -0700977 final boolean isSuperPrimary = entryValues.getAsInteger(
978 Data.IS_SUPER_PRIMARY) != 0;
Evan Millar11d628c2009-09-02 08:55:01 -0700979
Evan Millarff04a272009-09-26 15:23:37 -0700980 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
981 // Build phone entries
982 mNumPhoneNumbers++;
Evan Millar11d628c2009-09-02 08:55:01 -0700983
Evan Millarff04a272009-09-26 15:23:37 -0700984 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
985 Uri.fromParts("tel", entry.data, null));
986 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
987 Uri.fromParts("sms", entry.data, null));
988 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700989
Evan Millarff04a272009-09-26 15:23:37 -0700990 entry.isPrimary = isSuperPrimary;
991 mPhoneEntries.add(entry);
992
993 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
994 || mShowSmsLinksForAllPhones) {
995 // Add an SMS entry
996 if (kind.iconAltRes > 0) {
997 entry.secondaryActionIcon = kind.iconAltRes;
Evan Millar49714ee2009-09-02 16:42:47 -0700998 }
Evan Millarff04a272009-09-26 15:23:37 -0700999 }
1000 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
1001 // Build email entries
1002 entry.intent = new Intent(Intent.ACTION_SENDTO,
1003 Uri.fromParts("mailto", entry.data, null));
1004 entry.isPrimary = isSuperPrimary;
1005 mEmailEntries.add(entry);
1006 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
1007 equals(mimetype)) {
1008 // Build postal entries
1009 entry.maxLines = 4;
1010 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
1011 mPostalEntries.add(entry);
1012 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
1013 // Build im entries
1014 Object protocolObj = entryValues.getAsInteger(CommonDataKinds.Im.PROTOCOL);
1015 String host = null;
Evan Millar49714ee2009-09-02 16:42:47 -07001016
Evan Millarff04a272009-09-26 15:23:37 -07001017 if (TextUtils.isEmpty(entry.label)) {
1018 entry.label = getString(R.string.chat).toLowerCase();
1019 }
Evan Millar11d628c2009-09-02 08:55:01 -07001020
Evan Millarff04a272009-09-26 15:23:37 -07001021 if (protocolObj instanceof Number) {
1022 int protocol = ((Number) protocolObj).intValue();
1023 host = ContactsUtils.lookupProviderNameFromId(protocol);
1024 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
1025 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
1026 entry.maxLabelLines = 2;
Evan Millar11d628c2009-09-02 08:55:01 -07001027 }
Evan Millarff04a272009-09-26 15:23:37 -07001028 } else if (protocolObj != null) {
1029 String providerName = (String) protocolObj;
1030 host = providerName.toLowerCase();
1031 }
1032
1033 // Only add the intent if there is a valid host
1034 // host is null for CommonDataKinds.Im.PROTOCOL_CUSTOM
1035 if (!TextUtils.isEmpty(host)) {
Evan Millar11d628c2009-09-02 08:55:01 -07001036 entry.intent = new Intent(Intent.ACTION_SENDTO,
Evan Millarff04a272009-09-26 15:23:37 -07001037 constructImToUrl(host.toLowerCase(), entry.data));
1038 }
1039 //TODO(emillar) Add in presence info
Evan Millar11d628c2009-09-02 08:55:01 -07001040 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
1041 entry.presenceIcon = Presence.getPresenceIconResourceId(
1042 aggCursor.getInt(METHODS_STATUS_COLUMN));
1043 entry.status = ...
1044 }*/
Evan Millarff04a272009-09-26 15:23:37 -07001045 mImEntries.add(entry);
1046 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)
1047 || CommonDataKinds.Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) {
1048 // Build organization and note entries
1049 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -07001050 mOrganizationEntries.add(entry);
1051 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
1052 // Build note entries
Evan Millar11d628c2009-09-02 08:55:01 -07001053 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -07001054 entry.maxLines = 10;
1055 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -07001056 } else {
1057 // Handle showing custom
Evan Millarff04a272009-09-26 15:23:37 -07001058 entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri);
Jeff Sharkeyab066932009-09-21 09:55:30 -07001059 mOtherEntries.add(entry);
Evan Millar11d628c2009-09-02 08:55:01 -07001060 }
1061
Evan Millar2e1b8af2009-09-24 09:39:21 -07001062 }
1063
1064 if (mSelectedRawContactId != null &&
1065 mSelectedRawContactId == rawContactId
1066 && mEntities.size() > 1) {
1067 ViewEntry entry = new ViewEntry();
1068 entry.mimetype = SPLIT_MIMETYPE;
1069 entry.id = rawContactId;
1070 entry.label = getString(R.string.split_label);
1071 entry.data = getString(R.string.split_explanation);
1072 entry.actionIcon = R.drawable.ic_list_split;
1073 mSplitEntry.add(entry);
1074 }
Evan Millar11d628c2009-09-02 08:55:01 -07001075 // TODO(emillar) Add group entries
1076 // // Build the group entries
1077 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1078 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1079 // null, null, Groups.DEFAULT_SORT_ORDER);
1080 // if (groupCursor != null) {
1081 // try {
1082 // StringBuilder sb = new StringBuilder();
1083 //
1084 // while (groupCursor.moveToNext()) {
1085 // String systemId = groupCursor.getString(
1086 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1087 //
1088 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1089 // continue;
1090 // }
1091 //
1092 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1093 // if (!TextUtils.isEmpty(name)) {
1094 // if (sb.length() == 0) {
1095 // sb.append(name);
1096 // } else {
1097 // sb.append(getString(R.string.group_list, name));
1098 // }
1099 // }
1100 // }
1101 //
1102 // if (sb.length() > 0) {
1103 // ViewEntry entry = new ViewEntry();
1104 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1105 // entry.label = getString(R.string.label_groups);
1106 // entry.data = sb.toString();
1107 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1108 //
1109 // // TODO: Add an icon for the groups item.
1110 //
1111 // mGroupEntries.add(entry);
1112 // }
1113 // } finally {
1114 // groupCursor.close();
1115 // }
1116 // }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001117
Evan Millar66388be2009-05-28 15:41:07 -07001118 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001119 }
1120 }
1121
Evan Millar11d628c2009-09-02 08:55:01 -07001122 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1123 if (kind.actionHeader == null) {
1124 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001125 }
Evan Millar11d628c2009-09-02 08:55:01 -07001126 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1127 if (actionHeader == null) {
1128 return null;
1129 }
1130 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1131 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001132
Evan Millar11d628c2009-09-02 08:55:01 -07001133 String buildDataString(DataKind kind, ContentValues values) {
1134 if (kind.actionBody == null) {
1135 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001136 }
Evan Millar11d628c2009-09-02 08:55:01 -07001137 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1138 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001139 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001140
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001141 /**
1142 * A basic structure with the data for a contact entry in the list.
1143 */
Evan Millaradb0f8c2009-09-28 17:20:50 -07001144 class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001145 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001146 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001147 public boolean isPrimary = false;
1148 public int presenceIcon = -1;
1149 public int secondaryActionIcon = -1;
1150 public Intent intent;
1151 public Intent secondaryIntent = null;
1152 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001153 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001154 public ArrayList<Long> ids = new ArrayList<Long>();
1155 public int collapseCount = 0;
1156
1157 public boolean collapseWith(ViewEntry entry) {
1158 // assert equal collapse keys
Evan Millaradb0f8c2009-09-28 17:20:50 -07001159 if (!shouldCollapseWith(entry)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001160 return false;
1161 }
1162
1163 // Choose the label associated with the highest type precedence.
1164 if (TypePrecedence.getTypePrecedence(mimetype, type)
1165 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1166 type = entry.type;
1167 label = entry.label;
1168 }
1169
1170 // Choose the max of the maxLines and maxLabelLines values.
1171 maxLines = Math.max(maxLines, entry.maxLines);
1172 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1173
1174 // Choose the presence with the highest precedence.
1175 if (Presence.getPresencePrecedence(status)
1176 < Presence.getPresencePrecedence(entry.status)) {
1177 status = entry.status;
1178 }
1179
1180 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001181 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001182
1183 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1184
1185 // Keep track of all the ids that have been collapsed with this one.
1186 ids.add(entry.id);
1187 collapseCount++;
1188 return true;
1189 }
1190
Evan Millaradb0f8c2009-09-28 17:20:50 -07001191 public boolean shouldCollapseWith(ViewEntry entry) {
1192 if (entry == null) {
1193 return false;
1194 }
1195
1196 if (Phone.CONTENT_ITEM_TYPE.equals(mimetype)
1197 && Phone.CONTENT_ITEM_TYPE.equals(entry.mimetype)) {
1198 if (!PhoneNumberUtils.compare(ViewContactActivity.this, data, entry.data)) {
1199 return false;
1200 }
1201 } else {
1202 if (!equals(data, entry.data)) {
1203 return false;
1204 }
1205 }
1206
1207 if (!equals(mimetype, entry.mimetype)
1208 || !intentCollapsible(intent, entry.intent)
1209 || !intentCollapsible(secondaryIntent, entry.secondaryIntent)
1210 || actionIcon != entry.actionIcon) {
1211 return false;
1212 }
1213
1214 return true;
1215 }
1216
1217 private boolean equals(Object a, Object b) {
1218 return a==b || (a != null && a.equals(b));
1219 }
1220
1221 private boolean intentCollapsible(Intent a, Intent b) {
1222 if (a == b) {
1223 return true;
1224 } else if ((a != null && b != null) && equals(a.getAction(), b.getAction())) {
1225 return true;
1226 }
1227 return false;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001228 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001229 }
1230
Evan Millar15e514d2009-08-04 10:14:57 -07001231 /** Cache of the children views of a row */
1232 static class ViewCache {
1233 public TextView label;
1234 public TextView data;
1235 public ImageView actionIcon;
1236 public ImageView presenceIcon;
1237 public ImageView primaryIcon;
1238 public ImageView secondaryActionButton;
1239 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001240
Evan Millar15e514d2009-08-04 10:14:57 -07001241 // Need to keep track of this too
1242 ViewEntry entry;
1243 }
1244
1245 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1246 implements View.OnClickListener {
1247
Evan Millar5c22c3b2009-05-29 11:37:54 -07001248
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001249 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1250 super(context, sections, SHOW_SEPARATORS);
1251 }
1252
Evan Millar15e514d2009-08-04 10:14:57 -07001253 public void onClick(View v) {
1254 Intent intent = (Intent) v.getTag();
1255 startActivity(intent);
1256 }
1257
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001258 @Override
1259 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001260 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001261 View v;
1262
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001263 ViewCache views;
1264
1265 // Check to see if we can reuse convertView
1266 if (convertView != null) {
1267 v = convertView;
1268 views = (ViewCache) v.getTag();
1269 } else {
1270 // Create a new view if needed
1271 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1272
1273 // Cache the children
1274 views = new ViewCache();
1275 views.label = (TextView) v.findViewById(android.R.id.text1);
1276 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001277 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1278 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1279 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1280 views.secondaryActionButton = (ImageView) v.findViewById(
1281 R.id.secondary_action_button);
1282 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001283 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001284 v.setTag(views);
1285 }
1286
1287 // Update the entry in the view cache
1288 views.entry = entry;
1289
1290 // Bind the data to the view
1291 bindView(v, entry);
1292 return v;
1293 }
1294
1295 @Override
1296 protected View newView(int position, ViewGroup parent) {
1297 // getView() handles this
1298 throw new UnsupportedOperationException();
1299 }
1300
1301 @Override
1302 protected void bindView(View view, ViewEntry entry) {
1303 final Resources resources = mContext.getResources();
1304 ViewCache views = (ViewCache) view.getTag();
1305
1306 // Set the label
1307 TextView label = views.label;
1308 setMaxLines(label, entry.maxLabelLines);
1309 label.setText(entry.label);
1310
1311 // Set the data
1312 TextView data = views.data;
1313 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001314 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001315 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001316 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1317 } else {
1318 data.setText(entry.data);
1319 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001320 setMaxLines(data, entry.maxLines);
1321 }
1322
Evan Millar15e514d2009-08-04 10:14:57 -07001323 // Set the primary icon
1324 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1325
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001326 // Set the action icon
1327 ImageView action = views.actionIcon;
1328 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001329 Drawable actionIcon;
1330 if (entry.resPackageName != null) {
1331 // Load external resources through PackageManager
1332 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1333 entry.actionIcon, null);
1334 } else {
1335 actionIcon = resources.getDrawable(entry.actionIcon);
1336 }
1337 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001338 action.setVisibility(View.VISIBLE);
1339 } else {
1340 // Things should still line up as if there was an icon, so make it invisible
1341 action.setVisibility(View.INVISIBLE);
1342 }
1343
1344 // Set the presence icon
1345 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001346 if (entry.presenceIcon != -1) {
1347 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001348 } else if (entry.status != -1) {
1349 presenceIcon = resources.getDrawable(
1350 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001351 }
Evan Millar15e514d2009-08-04 10:14:57 -07001352 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001353 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001354 presenceIconView.setImageDrawable(presenceIcon);
1355 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001356 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001357 presenceIconView.setVisibility(View.GONE);
1358 }
1359
1360 // Set the secondary action button
1361 ImageView secondaryActionView = views.secondaryActionButton;
1362 Drawable secondaryActionIcon = null;
1363 if (entry.secondaryActionIcon != -1) {
1364 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1365 }
1366 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1367 secondaryActionView.setImageDrawable(secondaryActionIcon);
1368 secondaryActionView.setTag(entry.secondaryIntent);
1369 secondaryActionView.setVisibility(View.VISIBLE);
1370 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1371 } else {
1372 secondaryActionView.setVisibility(View.GONE);
1373 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001374 }
1375 }
1376
1377 private void setMaxLines(TextView textView, int maxLines) {
1378 if (maxLines == 1) {
1379 textView.setSingleLine(true);
1380 textView.setEllipsize(TextUtils.TruncateAt.END);
1381 } else {
1382 textView.setSingleLine(false);
1383 textView.setMaxLines(maxLines);
1384 textView.setEllipsize(null);
1385 }
1386 }
1387 }
1388}