blob: 921d5a790f6aff12f67d941dc562477757c15662 [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;
Evan Millar11d628c2009-09-02 08:55:01 -0700111 private Uri mUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800112 private ContentResolver mResolver;
113 private ViewAdapter mAdapter;
114 private int mNumPhoneNumbers = 0;
115
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700116 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700117 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700118 */
Evan Millar7911ff52009-07-21 15:55:18 -0700119 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700120
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800121 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
122 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
123 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
124 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
125 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
126 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700127 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800128 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
Evan Millar2e1b8af2009-09-24 09:39:21 -0700129 /* package */ ArrayList<ViewEntry> mSplitEntry = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800130 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
131
132 private Cursor mCursor;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700133
Evan Millar11d628c2009-09-02 08:55:01 -0700134 private SparseArray<Long> mTabRawContactIdMap;
135 protected ScrollingTabWidget mTabWidget;
136 protected ContactHeaderWidget mContactHeaderWidget;
Evan Millar2e1b8af2009-09-24 09:39:21 -0700137 protected View mBelowHeader;
Tadashi G. Takaoka33aa1c62009-09-27 14:22:28 -0700138 protected TextView mAccountName;
Evan Millar2e1b8af2009-09-24 09:39:21 -0700139 protected View mBufferView;
Evan Millar11d628c2009-09-02 08:55:01 -0700140 private NotifyingAsyncQueryHandler mHandler;
141
142 protected LayoutInflater mInflater;
143
144 //Projection used for the query that determines which tabs to add.
145 protected static final String[] TAB_PROJECTION = new String[] {
146 RawContacts._ID,
147 RawContacts.ACCOUNT_NAME,
148 RawContacts.ACCOUNT_TYPE
149 };
150 protected static final int TAB_CONTACT_ID_COLUMN_INDEX = 0;
151 protected static final int TAB_ACCOUNT_NAME_COLUMN_INDEX = 1;
152 protected static final int TAB_ACCOUNT_TYPE_COLUMN_INDEX = 2;
153
Evan Millar9cd81242009-09-26 16:02:31 -0700154 private static final String SAVED_STATE_SELECTED_RAW_CONTACT_ID_KEY = "selectedRawContactKey";
155 private static final String SAVED_STATE_TABS_VISIBLE_KEY = "tabsVisibleKey";
156
Evan Millar11d628c2009-09-02 08:55:01 -0700157 protected Long mSelectedRawContactId = null;
158
159 private static final int TOKEN_QUERY = 0;
160
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800161 private ContentObserver mObserver = new ContentObserver(new Handler()) {
162 @Override
163 public boolean deliverSelfNotifications() {
164 return true;
165 }
166
167 @Override
168 public void onChange(boolean selfChange) {
Evan Millar11d628c2009-09-02 08:55:01 -0700169 if (mCursor != null && !mCursor.isClosed()) {
170 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800171 }
172 }
173 };
174
175 public void onClick(DialogInterface dialog, int which) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700176 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800177 getContentResolver().delete(mUri, null, null);
178 finish();
179 }
180
Evan Millar7911ff52009-07-21 15:55:18 -0700181 private FrameLayout mTabContentLayout;
182 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800183 private boolean mShowSmsLinksForAllPhones;
Evan Millar11d628c2009-09-02 08:55:01 -0700184 private ArrayList<Entity> mEntities = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800185
Evan Millar2e1b8af2009-09-24 09:39:21 -0700186 private boolean mTabsVisible;
187
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800188 @Override
189 protected void onCreate(Bundle icicle) {
190 super.onCreate(icicle);
191
Evan Millar11d628c2009-09-02 08:55:01 -0700192 final Intent intent = getIntent();
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700193 Uri data = intent.getData();
194 String authority = data.getAuthority();
195 if (ContactsContract.AUTHORITY.equals(authority)) {
196 mLookupUri = data;
197 } else if (android.provider.Contacts.AUTHORITY.equals(authority)) {
198 final long rawContactId = ContentUris.parseId(data);
199 mLookupUri = RawContacts.getContactLookupUri(getContentResolver(),
200 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700201
Dmitri Plotnikov4e3a0432009-09-15 13:54:40 -0700202 }
Dmitri Plotnikov83129f02009-09-02 19:04:41 -0700203 mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Evan Millar11d628c2009-09-02 08:55:01 -0700204
205 requestWindowFeature(Window.FEATURE_NO_TITLE);
206 setContentView(R.layout.contact_card_layout);
207
208 mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget);
209 mContactHeaderWidget.showStar(true);
Evan Millar11d628c2009-09-02 08:55:01 -0700210 mContactHeaderWidget.setExcludeMimes(new String[] {
211 Contacts.CONTENT_ITEM_TYPE
212 });
213
214 mTabWidget = (ScrollingTabWidget) findViewById(R.id.tab_widget);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700215 mTabWidget.setTabSelectionListener(this);
Evan Millar85863112009-09-21 14:35:18 -0700216 mTabWidget.setVisibility(View.GONE);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700217 mTabsVisible = false;
218
219 mBelowHeader = findViewById(R.id.below_header);
Tadashi G. Takaoka33aa1c62009-09-27 14:22:28 -0700220 mAccountName = (TextView) findViewById(R.id.account_name);
Evan Millar11d628c2009-09-02 08:55:01 -0700221
222 mTabRawContactIdMap = new SparseArray<Long>();
223
224 mHandler = new NotifyingAsyncQueryHandler(this, this);
225
Evan Millar7911ff52009-07-21 15:55:18 -0700226 mListView = new ListView(this);
227 mListView.setOnCreateContextMenuListener(this);
228 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
229 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800230
Evan Millar7911ff52009-07-21 15:55:18 -0700231 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
232 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800233
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800234 mResolver = getContentResolver();
235
236 // Build the list of sections. The order they're added to mSections dictates the
237 // order they are displayed in the list.
238 mSections.add(mPhoneEntries);
239 mSections.add(mSmsEntries);
240 mSections.add(mEmailEntries);
241 mSections.add(mImEntries);
242 mSections.add(mPostalEntries);
243 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700244 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800245 mSections.add(mOtherEntries);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700246 mSections.add(mSplitEntry);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800247
248 //TODO Read this value from a preference
249 mShowSmsLinksForAllPhones = true;
Evan Millar11d628c2009-09-02 08:55:01 -0700250 }
251
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800252 @Override
Evan Millar9cd81242009-09-26 16:02:31 -0700253 protected void onRestoreInstanceState(Bundle savedInstanceState) {
254 super.onRestoreInstanceState(savedInstanceState);
255 long restoredRawContactId = savedInstanceState.getLong(
256 SAVED_STATE_SELECTED_RAW_CONTACT_ID_KEY, -1);
257 mSelectedRawContactId = restoredRawContactId != -1 ? restoredRawContactId : null;
258 mTabsVisible = savedInstanceState.getBoolean(SAVED_STATE_TABS_VISIBLE_KEY);
259 }
260
261 @Override
262 protected void onSaveInstanceState(Bundle outState) {
263 super.onSaveInstanceState(outState);
264 if (mSelectedRawContactId != null) {
265 outState.putLong(SAVED_STATE_SELECTED_RAW_CONTACT_ID_KEY, mSelectedRawContactId);
266
267 }
268 outState.putBoolean(SAVED_STATE_TABS_VISIBLE_KEY, mTabsVisible);
269 }
270
271 @Override
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800272 protected void onResume() {
273 super.onResume();
Evan Millar11d628c2009-09-02 08:55:01 -0700274 startEntityQuery();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800275 }
276
277 @Override
278 protected void onPause() {
279 super.onPause();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700280 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800281 }
282
283 @Override
284 protected void onDestroy() {
285 super.onDestroy();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700286 closeCursor();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800287 }
288
289 @Override
290 protected Dialog onCreateDialog(int id) {
291 switch (id) {
292 case DIALOG_CONFIRM_DELETE:
293 return new AlertDialog.Builder(this)
294 .setTitle(R.string.deleteConfirmation_title)
295 .setIcon(android.R.drawable.ic_dialog_alert)
296 .setMessage(R.string.deleteConfirmation)
297 .setNegativeButton(android.R.string.cancel, null)
298 .setPositiveButton(android.R.string.ok, this)
299 .setCancelable(false)
300 .create();
301 }
302 return null;
303 }
304
Evan Millar11d628c2009-09-02 08:55:01 -0700305
306 // TAB CODE //
307 /**
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700308 * Adds a tab for each {@link RawContacts} associated with this contact.
Evan Millar11d628c2009-09-02 08:55:01 -0700309 * Override this method if you want to additional tabs and/or different
310 * tabs for your activity.
Evan Millar11d628c2009-09-02 08:55:01 -0700311 */
Evan Millar2e1b8af2009-09-24 09:39:21 -0700312 protected void bindTabs() {
313 final Sources sources = Sources.getInstance(this);
314
315 for (Entity entity : mEntities) {
316 final String accountType = entity.getEntityValues().
317 getAsString(RawContacts.ACCOUNT_TYPE);
318 final Long rawContactId = entity.getEntityValues().
319 getAsLong(RawContacts._ID);
320
321 // TODO: ensure inflation on background task so we don't block UI thread here
322 final ContactsSource source = sources.getInflatedSource(accountType,
323 ContactsSource.LEVEL_SUMMARY);
324 addTab(rawContactId, ContactsUtils.createTabIndicatorView(mTabWidget.getTabParent(),
325 source));
326 }
327 }
328
329 /**
330 * Add a tab to be displayed in the {@link ScrollingTabWidget}.
331 *
332 * @param rawContactId The contact id associated with the tab.
333 * @param view A view to use as the tab indicator.
334 */
335 protected void addTab(long rawContactId, View view) {
336 mTabRawContactIdMap.put(mTabWidget.getTabCount(), rawContactId);
337 mTabWidget.addTab(view);
338 }
339
340
341 protected void clearCurrentTabs() {
342 mTabRawContactIdMap.clear();
343 mTabWidget.removeAllTabs();
344 }
345
346 protected void selectInitialTab() {
347 int selectedTabIndex = 0;
348
349 if (mSelectedRawContactId != null) {
350 selectedTabIndex = getTabIndexForRawContactId(mSelectedRawContactId);
351 if (selectedTabIndex == -1) {
352 // If there was no matching tab, just select the first;
353 selectedTabIndex = 0;
354 }
355 }
356
357 mTabWidget.setCurrentTab(selectedTabIndex);
358 onTabSelectionChanged(selectedTabIndex, false);
359 }
360
361 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
362 Long rawContactId = getTabRawContactId(tabIndex);
363 if (rawContactId != null) {
364 mSelectedRawContactId = rawContactId;
365 bindData();
366 }
367 }
368
369 /**
370 * Return the RawContact id associated with the tab at an index.
371 *
372 * @param index The index of the tab in question.
373 * @return The contactId associated with the tab at the specified index.
374 */
375 protected Long getTabRawContactId(int index) {
376 return mTabRawContactIdMap.get(index);
377 }
378
379 /**
380 * Return the tab index associated with the RawContact id.
381 *
382 * @param index The index of the tab in question.
383 * @return The contactId associated with the tab at the specified index.
384 */
385 protected int getTabIndexForRawContactId(long rawContactId) {
386 int numTabs = mTabRawContactIdMap.size();
387 for (int i=0; i < numTabs; i++) {
388 if (mTabRawContactIdMap.get(i) == rawContactId) {
389 return i;
390 }
391 }
392 return -1;
393 }
394
Evan Millar9cd81242009-09-26 16:02:31 -0700395 protected void showTabs(boolean show) {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700396 if (mTabsVisible == show) {
397 return;
398 }
399
400 float tabHeight = getResources().getDimension(R.dimen.tab_height);
401 if (show) {
Evan Millar9cd81242009-09-26 16:02:31 -0700402 TranslateAnimation showAnimation = new TranslateAnimation(
403 Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
404 Animation.ABSOLUTE, -tabHeight, Animation.ABSOLUTE, 0);
405 showAnimation.setDuration(getResources().getInteger(
406 android.R.integer.config_longAnimTime));
Evan Millar2e1b8af2009-09-24 09:39:21 -0700407
Evan Millar9cd81242009-09-26 16:02:31 -0700408 showAnimation.setAnimationListener(new AnimationListener() {
409 public void onAnimationEnd(Animation animation) {
410 selectInitialTab();
411 bindData();
412 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700413
Evan Millar9cd81242009-09-26 16:02:31 -0700414 public void onAnimationRepeat(Animation animation) {
415 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700416
Evan Millar9cd81242009-09-26 16:02:31 -0700417 public void onAnimationStart(Animation animation) {
418 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700419
Evan Millar9cd81242009-09-26 16:02:31 -0700420 });
Evan Millar2e1b8af2009-09-24 09:39:21 -0700421
Evan Millar9cd81242009-09-26 16:02:31 -0700422 mBelowHeader.startAnimation(showAnimation);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700423 mTabWidget.setVisibility(View.VISIBLE);
424 mTabsVisible = true;
425 clearCurrentTabs();
426 bindTabs();
Evan Millar2e1b8af2009-09-24 09:39:21 -0700427 } else {
Evan Millar9cd81242009-09-26 16:02:31 -0700428 TranslateAnimation hideTabsAnimation = new TranslateAnimation(
429 Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
430 Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -tabHeight);
431 hideTabsAnimation.setDuration(getResources().getInteger(
432 android.R.integer.config_longAnimTime));
433 hideTabsAnimation.setAnimationListener(new AnimationListener() {
434 public void onAnimationEnd(Animation animation) {
435 bindData();
436 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700437
Evan Millar9cd81242009-09-26 16:02:31 -0700438 public void onAnimationRepeat(Animation animation) {
439 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700440
Evan Millar9cd81242009-09-26 16:02:31 -0700441 public void onAnimationStart(Animation animation) {
442 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700443
Evan Millar9cd81242009-09-26 16:02:31 -0700444 });
Evan Millar2e1b8af2009-09-24 09:39:21 -0700445
Evan Millar9cd81242009-09-26 16:02:31 -0700446 TranslateAnimation hideListAnimation = new TranslateAnimation(
447 Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
448 Animation.ABSOLUTE, tabHeight, Animation.ABSOLUTE, 0);
449 hideListAnimation.setDuration(getResources().getInteger(
450 android.R.integer.config_longAnimTime));
Evan Millar2e1b8af2009-09-24 09:39:21 -0700451
452
Evan Millar9cd81242009-09-26 16:02:31 -0700453 mTabWidget.startAnimation(hideTabsAnimation);
454 mTabContentLayout.startAnimation(hideListAnimation);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700455 mTabWidget.setVisibility(View.GONE);
456 mTabsVisible = false;
457 mSelectedRawContactId = null;
Evan Millar2e1b8af2009-09-24 09:39:21 -0700458 }
459 }
Evan Millar11d628c2009-09-02 08:55:01 -0700460
461
462 // QUERY CODE //
463 /** {@inheritDoc} */
464 public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
465 try{
466 if (token == TOKEN_QUERY) {
Evan Millar11d628c2009-09-02 08:55:01 -0700467 mEntities = readEntities(iterator);
Evan Millar85863112009-09-21 14:35:18 -0700468 // Show the aggregate badge if this contact is aggregated.
Evan Millarcc87fb92009-09-22 15:27:47 -0700469 boolean isAggregate = mEntities.size() > 1;
470 mContactHeaderWidget.showAggregateBadge(isAggregate);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700471 if (mTabsVisible) {
Evan Millar9cd81242009-09-26 16:02:31 -0700472 mTabWidget.setVisibility(View.VISIBLE);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700473 clearCurrentTabs();
474 bindTabs();
475 selectInitialTab();
Evan Millar9cd81242009-09-26 16:02:31 -0700476 } else {
477 mTabWidget.setVisibility(View.GONE);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700478 }
Evan Millar85863112009-09-21 14:35:18 -0700479 bindData();
Evan Millar11d628c2009-09-02 08:55:01 -0700480 }
481 } finally {
482 if (iterator != null) {
483 iterator.close();
484 }
485 }
486 }
487
488 /** {@inheritDoc} */
489 public void onQueryComplete(int token, Object cookie, Cursor cursor) {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700490 // Empty
Evan Millar11d628c2009-09-02 08:55:01 -0700491 }
492
493 private ArrayList<Entity> readEntities(EntityIterator iterator) {
494 ArrayList<Entity> entities = new ArrayList<Entity>();
495 try {
496 while (iterator.hasNext()) {
497 entities.add(iterator.next());
498 }
499 } catch (RemoteException e) {
500 }
501
502 return entities;
503 }
504
505 private void startEntityQuery() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700506 closeCursor();
507
Dmitri Plotnikov0306bbd2009-09-03 14:37:15 -0700508 mUri = null;
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700509 if (mLookupUri != null) {
510 mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri);
511 if (mLookupUri != null) {
512 mUri = Contacts.lookupContact(getContentResolver(), mLookupUri);
513 }
514 }
515
516 if (mUri == null) {
517
518 // TODO either figure out a way to prevent a flash of black background or
519 // use some other UI than a toast
520 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
521 Log.e(TAG, "invalid contact uri: " + mLookupUri);
522 finish();
523 return;
524 }
525
526 mCursor = mResolver.query(Uri.withAppendedPath(mUri, Contacts.Data.CONTENT_DIRECTORY),
527 new String[] {Contacts.DISPLAY_NAME}, null, null, null);
528 mCursor.registerContentObserver(mObserver);
529
Evan Millar11d628c2009-09-02 08:55:01 -0700530 long contactId = ContentUris.parseId(mUri);
531 mHandler.startQueryEntities(TOKEN_QUERY, null,
532 RawContacts.CONTENT_URI, RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700533
Evan Millar9b1a1242009-09-16 10:26:10 -0700534 mContactHeaderWidget.bindFromContactLookupUri(mLookupUri);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700535 }
536
537 private void closeCursor() {
538 if (mCursor != null) {
539 mCursor.unregisterContentObserver(mObserver);
540 mCursor.close();
541 mCursor = null;
542 }
Evan Millar11d628c2009-09-02 08:55:01 -0700543 }
544
545 private void bindData() {
546
547 // Build up the contact entries
548 buildEntries();
549
550 // Collapse similar data items in select sections.
551 Collapser.collapseList(mPhoneEntries);
552 Collapser.collapseList(mSmsEntries);
553 Collapser.collapseList(mEmailEntries);
554 Collapser.collapseList(mPostalEntries);
555
556 if (mAdapter == null) {
557 mAdapter = new ViewAdapter(this, mSections);
558 mListView.setAdapter(mAdapter);
559 } else {
560 mAdapter.setSections(mSections, SHOW_SEPARATORS);
561 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800562 }
563
564 @Override
565 public boolean onCreateOptionsMenu(Menu menu) {
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700566 super.onCreateOptionsMenu(menu);
567
568 final MenuInflater inflater = getMenuInflater();
569 inflater.inflate(R.menu.view, menu);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800570 return true;
571 }
572
573 @Override
574 public boolean onPrepareOptionsMenu(Menu menu) {
575 super.onPrepareOptionsMenu(menu);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700576
Evan Millar2e1b8af2009-09-24 09:39:21 -0700577 // If tabWidget is not shown enable "show sources", otherwise
578 // enable "hide sources"
579 if (mTabsVisible) {
580 menu.findItem(R.id.menu_show_sources).setVisible(false);
581 menu.findItem(R.id.menu_hide_sources).setVisible(true);
582 } else {
583 menu.findItem(R.id.menu_show_sources).setVisible(true);
584 menu.findItem(R.id.menu_hide_sources).setVisible(false);
585 }
586
Evan Millar51514db2009-09-22 11:42:35 -0700587 // Only allow edit when we have at least one raw_contact id
588 final boolean hasRawContact = (mRawContactIds.size() > 0);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700589 menu.findItem(R.id.menu_edit).setEnabled(hasRawContact);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700590
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800591 return true;
592 }
593
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800594 @Override
595 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
596 AdapterView.AdapterContextMenuInfo info;
597 try {
598 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
599 } catch (ClassCastException e) {
600 Log.e(TAG, "bad menuInfo", e);
601 return;
602 }
603
604 // This can be null sometimes, don't crash...
605 if (info == null) {
606 Log.e(TAG, "bad menuInfo");
607 return;
608 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700609
Evan Millar45e0ed32009-06-01 16:44:38 -0700610 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
Neel Parekha8fb68a2009-09-25 18:05:18 -0700611 menu.setHeaderTitle(R.string.contactOptionsTitle);
Evan Millar45e0ed32009-06-01 16:44:38 -0700612 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
613 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700614 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
615 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700616 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800617 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700618 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
619 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700620 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700621 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800622 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700623 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700624 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
625 }
626 // TODO(emillar): add back with group support.
627 /* else if (entry.mimetype.equals()) {
628 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
629 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800630 }
631
632 @Override
633 public boolean onOptionsItemSelected(MenuItem item) {
634 switch (item.getItemId()) {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700635 case R.id.menu_show_sources: {
Evan Millar9cd81242009-09-26 16:02:31 -0700636 showTabs(true);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700637 break;
638 }
639 case R.id.menu_hide_sources: {
Evan Millar9cd81242009-09-26 16:02:31 -0700640 showTabs(false);
Evan Millar2e1b8af2009-09-24 09:39:21 -0700641 break;
642 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700643 case R.id.menu_edit: {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700644 Long rawContactIdToEdit = mSelectedRawContactId;
645 if (rawContactIdToEdit == null) {
646 if (mRawContactIds.size() > 0) {
647 rawContactIdToEdit = mRawContactIds.get(0);
648 } else {
649 // There is no rawContact to edit.
650 break;
651 }
Evan Millardb5d88c2009-08-28 09:31:57 -0700652 }
Evan Millar2e1b8af2009-09-24 09:39:21 -0700653 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
654 rawContactIdToEdit);
Evan Millar9cd81242009-09-26 16:02:31 -0700655 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
656 REQUEST_EDIT_CONTACT);
Evan Millar8a79cee2009-08-19 17:20:49 -0700657 break;
658 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700659 case R.id.menu_delete: {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800660 // Get confirmation
661 showDialog(DIALOG_CONFIRM_DELETE);
662 return true;
663 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700664 case R.id.menu_join: {
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700665 showJoinAggregateActivity();
666 return true;
667 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700668 case R.id.menu_options: {
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700669 showOptionsActivity();
670 return true;
671 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700672 case R.id.menu_share: {
673 final Intent intent = new Intent(Intent.ACTION_SEND);
674 intent.setType(Contacts.CONTENT_ITEM_TYPE);
675 intent.putExtra(Intent.EXTRA_STREAM, mLookupUri);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700676
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700677 // Launch chooser to share contact via
678 final CharSequence chooseTitle = getText(R.string.share_via);
679 final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700680
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700681 try {
682 startActivity(chooseIntent);
683 } catch (ActivityNotFoundException ex) {
684 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800685 }
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700686 return true;
687 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800688 }
689 return super.onOptionsItemSelected(item);
690 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700691
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800692 @Override
693 public boolean onContextItemSelected(MenuItem item) {
694 switch (item.getItemId()) {
695 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700696 if (makeItemDefault(item)) {
697 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800698 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700699 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800700 }
701 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700702
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800703 return super.onContextItemSelected(item);
704 }
705
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700706 private boolean makeItemDefault(MenuItem item) {
707 ViewEntry entry = getViewEntryForMenuItem(item);
708 if (entry == null) {
709 return false;
710 }
711
712 // Update the primary values in the data record.
Evan Millar14fecb62009-09-09 09:23:12 -0700713 ContentValues values = new ContentValues(1);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700714 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700715 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
716 values, null, null);
Evan Millar11d628c2009-09-02 08:55:01 -0700717 startEntityQuery();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700718 return true;
719 }
720
721 /**
722 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
723 * The user picks a contact to be split into its own aggregate or clicks Cancel.
724 */
725 private void showSplitAggregateDialog() {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700726 // Wrap this dialog in a specific theme so that list items have correct text color.
727 final ContextThemeWrapper dialogContext =
728 new ContextThemeWrapper(this, android.R.style.Theme_Light);
729 AlertDialog.Builder builder =
730 new AlertDialog.Builder(dialogContext);
731 builder.setTitle(getString(R.string.splitAggregate_title));
732
733 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
734 builder.setView(view);
735
736 builder.setInverseBackgroundForced(true);
737 builder.setCancelable(true);
738 builder.setNegativeButton(android.R.string.cancel,
739 new OnClickListener() {
740 public void onClick(DialogInterface dialog, int which) {
741 dialog.dismiss();
742 }
743 });
744 final AlertDialog dialog = builder.create();
745
746 view.setOnContactSelectedListener(new OnContactSelectedListener() {
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700747 public void onContactSelected(long rawContactId) {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700748 dialog.dismiss();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700749 splitContact(rawContactId);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700750 }
751 });
752
753 dialog.show();
754 }
755
756 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700757 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700758 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700759 public void showJoinAggregateActivity() {
760 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
761 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700762 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700763 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700764
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700765 @Override
766 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar2e1b8af2009-09-24 09:39:21 -0700767 if (requestCode == REQUEST_JOIN_CONTACT) {
768 if (resultCode == RESULT_OK && intent != null) {
769 final long contactId = ContentUris.parseId(intent.getData());
770 joinAggregate(contactId);
Evan Millar8a79cee2009-08-19 17:20:49 -0700771 }
Evan Millar9cd81242009-09-26 16:02:31 -0700772 } else if (requestCode == REQUEST_EDIT_CONTACT) {
773 mTabsVisible = false;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700774 }
775 }
776
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700777 private void splitContact(long rawContactId) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700778 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_SEPARATE);
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700779
780 // The split operation may have removed the original aggregate contact, so we need
781 // to requery everything
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700782 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_LONG).show();
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700783 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700784 }
785
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700786 private void joinAggregate(final long contactId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700787 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700788 RawContacts.CONTACT_ID + "=" + contactId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700789
790 try {
791 while(c.moveToNext()) {
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700792 long rawContactId = c.getLong(0);
793 setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700794 }
795 } finally {
796 c.close();
797 }
798
Dmitri Plotnikov040dc152009-09-03 15:17:56 -0700799 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show();
800 startEntityQuery();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700801 }
802
803 /**
804 * Given a contact ID sets an aggregation exception to either join the contact with the
805 * current aggregate or split off.
806 */
Dmitri Plotnikov99eafe72009-09-03 14:09:45 -0700807 protected void setAggregationException(long rawContactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700808 ContentValues values = new ContentValues(3);
Dmitri Plotnikovf40ccdf2009-09-04 17:37:15 -0700809 for (long aRawContactId : mRawContactIds) {
810 if (aRawContactId != rawContactId) {
811 values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId);
812 values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId);
813 values.put(AggregationExceptions.TYPE, exceptionType);
814 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
815 }
816 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700817 }
818
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700819 private void showOptionsActivity() {
820 final Intent intent = new Intent(this, ContactOptionsActivity.class);
821 intent.setData(mUri);
822 startActivity(intent);
823 }
824
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700825 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
826 AdapterView.AdapterContextMenuInfo info;
827 try {
828 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
829 } catch (ClassCastException e) {
830 Log.e(TAG, "bad menuInfo", e);
831 return null;
832 }
833
834 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
835 }
836
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800837 @Override
838 public boolean onKeyDown(int keyCode, KeyEvent event) {
839 switch (keyCode) {
840 case KeyEvent.KEYCODE_CALL: {
841 try {
842 ITelephony phone = ITelephony.Stub.asInterface(
843 ServiceManager.checkService("phone"));
844 if (phone != null && !phone.isIdle()) {
845 // Skip out and let the key be handled at a higher level
846 break;
847 }
848 } catch (RemoteException re) {
849 // Fall through and try to call the contact
850 }
851
Evan Millar7911ff52009-07-21 15:55:18 -0700852 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800853 if (index != -1) {
854 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700855 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
856 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800857 }
858 } else if (mNumPhoneNumbers != 0) {
859 // There isn't anything selected, call the default number
860 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
861 startActivity(intent);
862 }
863 return true;
864 }
865
866 case KeyEvent.KEYCODE_DEL: {
867 showDialog(DIALOG_CONFIRM_DELETE);
868 return true;
869 }
870 }
871
872 return super.onKeyDown(keyCode, event);
873 }
874
Evan Millar7911ff52009-07-21 15:55:18 -0700875 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800876 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
877 if (entry != null) {
878 Intent intent = entry.intent;
Evan Millar2e1b8af2009-09-24 09:39:21 -0700879 if (entry.mimetype == SPLIT_MIMETYPE) {
880 splitContact(entry.id);
881 } else if (intent != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800882 try {
883 startActivity(intent);
884 } catch (ActivityNotFoundException e) {
885 Log.e(TAG, "No activity found for intent: " + intent);
886 signalError();
887 }
888 } else {
889 signalError();
890 }
891 } else {
892 signalError();
893 }
894 }
895
896 /**
897 * Signal an error to the user via a beep, or some other method.
898 */
899 private void signalError() {
900 //TODO: implement this when we have the sonification APIs
901 }
902
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800903 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700904 // 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 -0800905 StringBuilder buf = new StringBuilder("imto://");
906 buf.append(host);
907 buf.append('/');
908 buf.append(data);
909 return Uri.parse(buf.toString());
910 }
911
912 /**
913 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700914 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800915 * @param personCursor the URI for the contact being displayed
916 */
Evan Millar11d628c2009-09-02 08:55:01 -0700917 private final void buildEntries() {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800918 // Clear out the old entries
919 final int numSections = mSections.size();
920 for (int i = 0; i < numSections; i++) {
921 mSections.get(i).clear();
922 }
923
Evan Millar7911ff52009-07-21 15:55:18 -0700924 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700925
Evan Millar11d628c2009-09-02 08:55:01 -0700926 Sources sources = Sources.getInstance(this);
927
Evan Millar66388be2009-05-28 15:41:07 -0700928 // Build up method entries
929 if (mUri != null) {
Evan Millar11d628c2009-09-02 08:55:01 -0700930 for (Entity entity: mEntities) {
931 final ContentValues entValues = entity.getEntityValues();
932 final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700933 final long rawContactId = entValues.getAsLong(RawContacts._ID);
Evan Millar7911ff52009-07-21 15:55:18 -0700934
Dmitri Plotnikov2a8cef02009-09-23 19:01:15 -0700935 if (!mRawContactIds.contains(rawContactId)) {
936 mRawContactIds.add(rawContactId);
937 }
938
Evan Millar2e1b8af2009-09-24 09:39:21 -0700939 // This performs the tab filtering
940 if (mSelectedRawContactId != null && mSelectedRawContactId != rawContactId) {
941 continue;
942 }
Evan Millar66210452009-09-21 10:53:43 -0700943
Tadashi G. Takaoka33aa1c62009-09-27 14:22:28 -0700944 if (mTabsVisible) {
945 final String accountName = entValues.getAsString(RawContacts.ACCOUNT_NAME);
946 mAccountName.setText(getString(R.string.account_name_format, accountName));
947 mAccountName.setVisibility(View.VISIBLE);
948 } else {
949 mAccountName.setVisibility(View.GONE);
950 }
951
Evan Millar11d628c2009-09-02 08:55:01 -0700952 for (NamedContentValues subValue : entity.getSubValues()) {
953 ViewEntry entry = new ViewEntry();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800954
Jeff Sharkeyab066932009-09-21 09:55:30 -0700955 final ContentValues entryValues = subValue.values;
956 entryValues.put(Data.RAW_CONTACT_ID, rawContactId);
957
Evan Millar11d628c2009-09-02 08:55:01 -0700958 final String mimetype = entryValues.getAsString(Data.MIMETYPE);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700959 if (mimetype == null) continue;
Evan Millar66388be2009-05-28 15:41:07 -0700960
Jeff Sharkeyab066932009-09-21 09:55:30 -0700961 final DataKind kind = sources.getKindOrFallback(accountType, mimetype, this,
Evan Millar11d628c2009-09-02 08:55:01 -0700962 ContactsSource.LEVEL_MIMETYPES);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700963 if (kind == null) continue;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800964
Evan Millar11d628c2009-09-02 08:55:01 -0700965 final long id = entryValues.getAsLong(Data._ID);
966 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
Jeff Sharkeyab066932009-09-21 09:55:30 -0700967 entry.contactId = rawContactId;
Evan Millar11d628c2009-09-02 08:55:01 -0700968 entry.id = id;
969 entry.uri = uri;
970 entry.mimetype = mimetype;
Daisuke Miyakawa999db912009-09-17 15:55:15 -0700971 entry.label = buildActionString(kind, entryValues, false);
Evan Millar11d628c2009-09-02 08:55:01 -0700972 entry.data = buildDataString(kind, entryValues);
Jeff Sharkey49d17b32009-09-07 02:14:21 -0700973 if (kind.typeColumn != null && entryValues.containsKey(kind.typeColumn)) {
Evan Millar11d628c2009-09-02 08:55:01 -0700974 entry.type = entryValues.getAsInteger(kind.typeColumn);
975 }
976 if (kind.iconRes > 0) {
Jeff Sharkeyab066932009-09-21 09:55:30 -0700977 entry.resPackageName = kind.resPackageName;
Evan Millar11d628c2009-09-02 08:55:01 -0700978 entry.actionIcon = kind.iconRes;
979 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800980
Evan Millar11d628c2009-09-02 08:55:01 -0700981 // Don't crash if the data is bogus
Evan Millar66388be2009-05-28 15:41:07 -0700982 if (TextUtils.isEmpty(entry.data)) {
Alex Kennberg87fc3172009-03-28 06:43:06 -0700983 continue;
984 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700985
Evan Millarff04a272009-09-26 15:23:37 -0700986 final boolean isSuperPrimary = entryValues.getAsInteger(
987 Data.IS_SUPER_PRIMARY) != 0;
Evan Millar11d628c2009-09-02 08:55:01 -0700988
Evan Millarff04a272009-09-26 15:23:37 -0700989 if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimetype)) {
990 // Build phone entries
991 mNumPhoneNumbers++;
Evan Millar11d628c2009-09-02 08:55:01 -0700992
Evan Millarff04a272009-09-26 15:23:37 -0700993 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
994 Uri.fromParts("tel", entry.data, null));
995 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
996 Uri.fromParts("sms", entry.data, null));
997 entry.data = PhoneNumberUtils.stripSeparators(entry.data);
Evan Millar49714ee2009-09-02 16:42:47 -0700998
Evan Millarff04a272009-09-26 15:23:37 -0700999 entry.isPrimary = isSuperPrimary;
1000 mPhoneEntries.add(entry);
1001
1002 if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE
1003 || mShowSmsLinksForAllPhones) {
1004 // Add an SMS entry
1005 if (kind.iconAltRes > 0) {
1006 entry.secondaryActionIcon = kind.iconAltRes;
Evan Millar49714ee2009-09-02 16:42:47 -07001007 }
Evan Millarff04a272009-09-26 15:23:37 -07001008 }
1009 } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimetype)) {
1010 // Build email entries
1011 entry.intent = new Intent(Intent.ACTION_SENDTO,
1012 Uri.fromParts("mailto", entry.data, null));
1013 entry.isPrimary = isSuperPrimary;
1014 mEmailEntries.add(entry);
1015 } else if (CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE.
1016 equals(mimetype)) {
1017 // Build postal entries
1018 entry.maxLines = 4;
1019 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
1020 mPostalEntries.add(entry);
1021 } else if (CommonDataKinds.Im.CONTENT_ITEM_TYPE.equals(mimetype)) {
1022 // Build im entries
1023 Object protocolObj = entryValues.getAsInteger(CommonDataKinds.Im.PROTOCOL);
1024 String host = null;
Evan Millar49714ee2009-09-02 16:42:47 -07001025
Evan Millarff04a272009-09-26 15:23:37 -07001026 if (TextUtils.isEmpty(entry.label)) {
1027 entry.label = getString(R.string.chat).toLowerCase();
1028 }
Evan Millar11d628c2009-09-02 08:55:01 -07001029
Evan Millarff04a272009-09-26 15:23:37 -07001030 if (protocolObj instanceof Number) {
1031 int protocol = ((Number) protocolObj).intValue();
1032 host = ContactsUtils.lookupProviderNameFromId(protocol);
1033 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
1034 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
1035 entry.maxLabelLines = 2;
Evan Millar11d628c2009-09-02 08:55:01 -07001036 }
Evan Millarff04a272009-09-26 15:23:37 -07001037 } else if (protocolObj != null) {
1038 String providerName = (String) protocolObj;
1039 host = providerName.toLowerCase();
1040 }
1041
1042 // Only add the intent if there is a valid host
1043 // host is null for CommonDataKinds.Im.PROTOCOL_CUSTOM
1044 if (!TextUtils.isEmpty(host)) {
Evan Millar11d628c2009-09-02 08:55:01 -07001045 entry.intent = new Intent(Intent.ACTION_SENDTO,
Evan Millarff04a272009-09-26 15:23:37 -07001046 constructImToUrl(host.toLowerCase(), entry.data));
1047 }
1048 //TODO(emillar) Add in presence info
Evan Millar11d628c2009-09-02 08:55:01 -07001049 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
1050 entry.presenceIcon = Presence.getPresenceIconResourceId(
1051 aggCursor.getInt(METHODS_STATUS_COLUMN));
1052 entry.status = ...
1053 }*/
Evan Millarff04a272009-09-26 15:23:37 -07001054 mImEntries.add(entry);
1055 } else if (CommonDataKinds.Organization.CONTENT_ITEM_TYPE.equals(mimetype)
1056 || CommonDataKinds.Nickname.CONTENT_ITEM_TYPE.equals(mimetype)) {
1057 // Build organization and note entries
1058 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -07001059 mOrganizationEntries.add(entry);
1060 } else if (CommonDataKinds.Note.CONTENT_ITEM_TYPE.equals(mimetype)) {
1061 // Build note entries
Evan Millar11d628c2009-09-02 08:55:01 -07001062 entry.uri = null;
Evan Millar11d628c2009-09-02 08:55:01 -07001063 entry.maxLines = 10;
1064 mOtherEntries.add(entry);
Jeff Sharkeyab066932009-09-21 09:55:30 -07001065 } else {
1066 // Handle showing custom
Evan Millarff04a272009-09-26 15:23:37 -07001067 entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri);
Jeff Sharkeyab066932009-09-21 09:55:30 -07001068 mOtherEntries.add(entry);
Evan Millar11d628c2009-09-02 08:55:01 -07001069 }
1070
Evan Millar2e1b8af2009-09-24 09:39:21 -07001071 }
1072
1073 if (mSelectedRawContactId != null &&
1074 mSelectedRawContactId == rawContactId
1075 && mEntities.size() > 1) {
1076 ViewEntry entry = new ViewEntry();
1077 entry.mimetype = SPLIT_MIMETYPE;
1078 entry.id = rawContactId;
1079 entry.label = getString(R.string.split_label);
1080 entry.data = getString(R.string.split_explanation);
1081 entry.actionIcon = R.drawable.ic_list_split;
1082 mSplitEntry.add(entry);
1083 }
Evan Millar11d628c2009-09-02 08:55:01 -07001084 // TODO(emillar) Add group entries
1085 // // Build the group entries
1086 // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
1087 // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
1088 // null, null, Groups.DEFAULT_SORT_ORDER);
1089 // if (groupCursor != null) {
1090 // try {
1091 // StringBuilder sb = new StringBuilder();
1092 //
1093 // while (groupCursor.moveToNext()) {
1094 // String systemId = groupCursor.getString(
1095 // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
1096 //
1097 // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
1098 // continue;
1099 // }
1100 //
1101 // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
1102 // if (!TextUtils.isEmpty(name)) {
1103 // if (sb.length() == 0) {
1104 // sb.append(name);
1105 // } else {
1106 // sb.append(getString(R.string.group_list, name));
1107 // }
1108 // }
1109 // }
1110 //
1111 // if (sb.length() > 0) {
1112 // ViewEntry entry = new ViewEntry();
1113 // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
1114 // entry.label = getString(R.string.label_groups);
1115 // entry.data = sb.toString();
1116 // entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
1117 //
1118 // // TODO: Add an icon for the groups item.
1119 //
1120 // mGroupEntries.add(entry);
1121 // }
1122 // } finally {
1123 // groupCursor.close();
1124 // }
1125 // }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001126
Evan Millar66388be2009-05-28 15:41:07 -07001127 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001128 }
1129 }
1130
Evan Millar11d628c2009-09-02 08:55:01 -07001131 String buildActionString(DataKind kind, ContentValues values, boolean lowerCase) {
1132 if (kind.actionHeader == null) {
1133 return null;
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001134 }
Evan Millar11d628c2009-09-02 08:55:01 -07001135 CharSequence actionHeader = kind.actionHeader.inflateUsing(this, values);
1136 if (actionHeader == null) {
1137 return null;
1138 }
1139 return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString();
1140 }
Jeff Hamilton8350e5b2009-03-24 21:01:34 -07001141
Evan Millar11d628c2009-09-02 08:55:01 -07001142 String buildDataString(DataKind kind, ContentValues values) {
1143 if (kind.actionBody == null) {
1144 return null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001145 }
Evan Millar11d628c2009-09-02 08:55:01 -07001146 CharSequence actionBody = kind.actionBody.inflateUsing(this, values);
1147 return actionBody == null ? null : actionBody.toString();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001148 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001149
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001150 /**
1151 * A basic structure with the data for a contact entry in the list.
1152 */
Evan Millar54a5c9f2009-06-23 17:41:09 -07001153 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001154 public String resPackageName = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001155 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -07001156 public boolean isPrimary = false;
1157 public int presenceIcon = -1;
1158 public int secondaryActionIcon = -1;
1159 public Intent intent;
1160 public Intent secondaryIntent = null;
1161 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001162 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001163 public ArrayList<Long> ids = new ArrayList<Long>();
1164 public int collapseCount = 0;
1165
1166 public boolean collapseWith(ViewEntry entry) {
1167 // assert equal collapse keys
1168 if (!getCollapseKey().equals(entry.getCollapseKey())) {
1169 return false;
1170 }
1171
1172 // Choose the label associated with the highest type precedence.
1173 if (TypePrecedence.getTypePrecedence(mimetype, type)
1174 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
1175 type = entry.type;
1176 label = entry.label;
1177 }
1178
1179 // Choose the max of the maxLines and maxLabelLines values.
1180 maxLines = Math.max(maxLines, entry.maxLines);
1181 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
1182
1183 // Choose the presence with the highest precedence.
1184 if (Presence.getPresencePrecedence(status)
1185 < Presence.getPresencePrecedence(entry.status)) {
1186 status = entry.status;
1187 }
1188
1189 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -07001190 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -07001191
1192 // uri, and contactdId, shouldn't make a difference. Just keep the original.
1193
1194 // Keep track of all the ids that have been collapsed with this one.
1195 ids.add(entry.id);
1196 collapseCount++;
1197 return true;
1198 }
1199
1200 public String getCollapseKey() {
1201 StringBuilder hashSb = new StringBuilder();
1202 hashSb.append(data);
1203 hashSb.append(mimetype);
1204 hashSb.append((intent != null && intent.getAction() != null)
1205 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -07001206 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
1207 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -07001208 hashSb.append(actionIcon);
1209 return hashSb.toString();
1210 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001211 }
1212
Evan Millar15e514d2009-08-04 10:14:57 -07001213 /** Cache of the children views of a row */
1214 static class ViewCache {
1215 public TextView label;
1216 public TextView data;
1217 public ImageView actionIcon;
1218 public ImageView presenceIcon;
1219 public ImageView primaryIcon;
1220 public ImageView secondaryActionButton;
1221 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001222
Evan Millar15e514d2009-08-04 10:14:57 -07001223 // Need to keep track of this too
1224 ViewEntry entry;
1225 }
1226
1227 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1228 implements View.OnClickListener {
1229
Evan Millar5c22c3b2009-05-29 11:37:54 -07001230
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001231 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1232 super(context, sections, SHOW_SEPARATORS);
1233 }
1234
Evan Millar15e514d2009-08-04 10:14:57 -07001235 public void onClick(View v) {
1236 Intent intent = (Intent) v.getTag();
1237 startActivity(intent);
1238 }
1239
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001240 @Override
1241 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001242 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001243 View v;
1244
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001245 ViewCache views;
1246
1247 // Check to see if we can reuse convertView
1248 if (convertView != null) {
1249 v = convertView;
1250 views = (ViewCache) v.getTag();
1251 } else {
1252 // Create a new view if needed
1253 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1254
1255 // Cache the children
1256 views = new ViewCache();
1257 views.label = (TextView) v.findViewById(android.R.id.text1);
1258 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001259 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1260 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1261 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1262 views.secondaryActionButton = (ImageView) v.findViewById(
1263 R.id.secondary_action_button);
1264 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001265 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001266 v.setTag(views);
1267 }
1268
1269 // Update the entry in the view cache
1270 views.entry = entry;
1271
1272 // Bind the data to the view
1273 bindView(v, entry);
1274 return v;
1275 }
1276
1277 @Override
1278 protected View newView(int position, ViewGroup parent) {
1279 // getView() handles this
1280 throw new UnsupportedOperationException();
1281 }
1282
1283 @Override
1284 protected void bindView(View view, ViewEntry entry) {
1285 final Resources resources = mContext.getResources();
1286 ViewCache views = (ViewCache) view.getTag();
1287
1288 // Set the label
1289 TextView label = views.label;
1290 setMaxLines(label, entry.maxLabelLines);
1291 label.setText(entry.label);
1292
1293 // Set the data
1294 TextView data = views.data;
1295 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001296 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
Jeff Sharkey49d17b32009-09-07 02:14:21 -07001297 || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001298 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1299 } else {
1300 data.setText(entry.data);
1301 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001302 setMaxLines(data, entry.maxLines);
1303 }
1304
Evan Millar15e514d2009-08-04 10:14:57 -07001305 // Set the primary icon
1306 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1307
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001308 // Set the action icon
1309 ImageView action = views.actionIcon;
1310 if (entry.actionIcon != -1) {
Jeff Sharkeyab066932009-09-21 09:55:30 -07001311 Drawable actionIcon;
1312 if (entry.resPackageName != null) {
1313 // Load external resources through PackageManager
1314 actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName,
1315 entry.actionIcon, null);
1316 } else {
1317 actionIcon = resources.getDrawable(entry.actionIcon);
1318 }
1319 action.setImageDrawable(actionIcon);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001320 action.setVisibility(View.VISIBLE);
1321 } else {
1322 // Things should still line up as if there was an icon, so make it invisible
1323 action.setVisibility(View.INVISIBLE);
1324 }
1325
1326 // Set the presence icon
1327 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001328 if (entry.presenceIcon != -1) {
1329 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001330 } else if (entry.status != -1) {
1331 presenceIcon = resources.getDrawable(
1332 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001333 }
Evan Millar15e514d2009-08-04 10:14:57 -07001334 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001335 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001336 presenceIconView.setImageDrawable(presenceIcon);
1337 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001338 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001339 presenceIconView.setVisibility(View.GONE);
1340 }
1341
1342 // Set the secondary action button
1343 ImageView secondaryActionView = views.secondaryActionButton;
1344 Drawable secondaryActionIcon = null;
1345 if (entry.secondaryActionIcon != -1) {
1346 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1347 }
1348 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1349 secondaryActionView.setImageDrawable(secondaryActionIcon);
1350 secondaryActionView.setTag(entry.secondaryIntent);
1351 secondaryActionView.setVisibility(View.VISIBLE);
1352 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1353 } else {
1354 secondaryActionView.setVisibility(View.GONE);
1355 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001356 }
1357 }
1358
1359 private void setMaxLines(TextView textView, int maxLines) {
1360 if (maxLines == 1) {
1361 textView.setSingleLine(true);
1362 textView.setEllipsize(TextUtils.TruncateAt.END);
1363 } else {
1364 textView.setSingleLine(false);
1365 textView.setMaxLines(maxLines);
1366 textView.setEllipsize(null);
1367 }
1368 }
1369 }
1370}