blob: da49188d0e41001e888b23c5bd069cca1b8a57ac [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
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070019import static com.android.contacts.ContactEntryAdapter.CONTACT_PHOTO_ID;
20import static com.android.contacts.ContactEntryAdapter.CONTACT_PROJECTION;
21import static com.android.contacts.ContactEntryAdapter.CONTACT_STARRED_COLUMN;
Evan Millar66388be2009-05-28 15:41:07 -070022import static com.android.contacts.ContactEntryAdapter.DATA_1_COLUMN;
23import static com.android.contacts.ContactEntryAdapter.DATA_2_COLUMN;
24import static com.android.contacts.ContactEntryAdapter.DATA_3_COLUMN;
25import static com.android.contacts.ContactEntryAdapter.DATA_4_COLUMN;
26import static com.android.contacts.ContactEntryAdapter.DATA_5_COLUMN;
Evan Millar66388be2009-05-28 15:41:07 -070027import static com.android.contacts.ContactEntryAdapter.DATA_9_COLUMN;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070028import static com.android.contacts.ContactEntryAdapter.DATA_CONTACT_ID_COLUMN;
29import static com.android.contacts.ContactEntryAdapter.DATA_ID_COLUMN;
30import static com.android.contacts.ContactEntryAdapter.DATA_IS_SUPER_PRIMARY_COLUMN;
31import static com.android.contacts.ContactEntryAdapter.DATA_MIMETYPE_COLUMN;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080032
Evan Millar54a5c9f2009-06-23 17:41:09 -070033import com.android.contacts.Collapser.Collapsible;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070034import com.android.contacts.SplitAggregateView.OnContactSelectedListener;
Jeff Sharkey802b2052009-08-04 14:21:06 -070035import com.android.contacts.ui.FastTrackWindow;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080036import com.android.internal.telephony.ITelephony;
37
38import android.app.AlertDialog;
39import android.app.Dialog;
40import android.app.ListActivity;
41import android.content.ActivityNotFoundException;
42import android.content.ContentResolver;
43import android.content.ContentUris;
44import android.content.ContentValues;
45import android.content.Context;
46import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070047import android.content.Entity;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080048import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070049import android.content.DialogInterface.OnClickListener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080050import android.content.pm.PackageManager;
51import android.content.pm.ResolveInfo;
52import android.content.res.Resources;
53import android.database.ContentObserver;
54import android.database.Cursor;
Evan Millar45e0ed32009-06-01 16:44:38 -070055import android.graphics.Bitmap;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080056import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080057import android.net.Uri;
58import android.os.Bundle;
59import android.os.Handler;
60import android.os.RemoteException;
61import android.os.ServiceManager;
62import android.os.SystemClock;
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070063import android.provider.ContactsContract.Contacts;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070064import android.provider.ContactsContract.AggregationExceptions;
65import android.provider.ContactsContract.CommonDataKinds;
Evan Millar66388be2009-05-28 15:41:07 -070066import android.provider.ContactsContract.Data;
Evan Millar54a5c9f2009-06-23 17:41:09 -070067import android.provider.ContactsContract.Presence;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070068import android.provider.ContactsContract.RawContacts;
Evan Millar54a5c9f2009-06-23 17:41:09 -070069import android.provider.ContactsContract.CommonDataKinds.Phone;
70import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080071import android.text.TextUtils;
72import android.util.Log;
73import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070074import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080075import android.view.KeyEvent;
Evan Millar7911ff52009-07-21 15:55:18 -070076import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080077import android.view.Menu;
78import android.view.MenuItem;
79import android.view.View;
80import android.view.ViewGroup;
81import android.view.ContextMenu.ContextMenuInfo;
82import android.widget.AdapterView;
83import android.widget.CheckBox;
Evan Millar7911ff52009-07-21 15:55:18 -070084import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080085import android.widget.ImageView;
86import android.widget.ListView;
87import android.widget.TextView;
88import android.widget.Toast;
89
90import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080091
92/**
93 * Displays the details of a specific contact.
94 */
Evan Millar7911ff52009-07-21 15:55:18 -070095public class ViewContactActivity extends BaseContactCardActivity
96 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
97 AdapterView.OnItemClickListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080098 private static final String TAG = "ViewContact";
99 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
100
Evan Millar8a79cee2009-08-19 17:20:49 -0700101 public static final String RAW_CONTACT_ID_EXTRA = "rawContactIdExtra";
102
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800103 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800104
105 private static final int DIALOG_CONFIRM_DELETE = 1;
106
Evan Millar8a79cee2009-08-19 17:20:49 -0700107 private static final int REQUEST_JOIN_CONTACT = 1;
108 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700109
Evan Millar8a79cee2009-08-19 17:20:49 -0700110 public static final int MENU_ITEM_EDIT = 1;
111 public static final int MENU_ITEM_DELETE = 2;
112 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
113 public static final int MENU_ITEM_SHOW_BARCODE = 4;
114 public static final int MENU_ITEM_SPLIT_AGGREGATE = 5;
115 public static final int MENU_ITEM_JOIN_AGGREGATE = 6;
116 public static final int MENU_ITEM_OPTIONS = 7;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800117
118 private Uri mUri;
119 private ContentResolver mResolver;
120 private ViewAdapter mAdapter;
121 private int mNumPhoneNumbers = 0;
122
Evan Millar8a79cee2009-08-19 17:20:49 -0700123 private static final long ALL_CONTACTS_ID = -100;
Evan Millar7911ff52009-07-21 15:55:18 -0700124
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700125 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700126 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700127 */
Evan Millar7911ff52009-07-21 15:55:18 -0700128 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700129
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800130 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
131 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
132 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
133 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
134 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
135 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700136 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800137 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
138 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
139
140 private Cursor mCursor;
141 private boolean mObserverRegistered;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700142
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800143 private ContentObserver mObserver = new ContentObserver(new Handler()) {
144 @Override
145 public boolean deliverSelfNotifications() {
146 return true;
147 }
148
149 @Override
150 public void onChange(boolean selfChange) {
151 if (mCursor != null && !mCursor.isClosed()){
152 dataChanged();
153 }
154 }
155 };
156
157 public void onClick(DialogInterface dialog, int which) {
158 if (mCursor != null) {
159 if (mObserverRegistered) {
160 mCursor.unregisterContentObserver(mObserver);
161 mObserverRegistered = false;
162 }
163 mCursor.close();
164 mCursor = null;
165 }
166 getContentResolver().delete(mUri, null, null);
167 finish();
168 }
169
Evan Millar7911ff52009-07-21 15:55:18 -0700170 private FrameLayout mTabContentLayout;
171 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800172 private boolean mShowSmsLinksForAllPhones;
173
174 @Override
175 protected void onCreate(Bundle icicle) {
176 super.onCreate(icicle);
177
Evan Millar7911ff52009-07-21 15:55:18 -0700178 mListView = new ListView(this);
179 mListView.setOnCreateContextMenuListener(this);
180 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
181 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800182
Evan Millar7911ff52009-07-21 15:55:18 -0700183 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
184 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800185
Evan Millar45e0ed32009-06-01 16:44:38 -0700186 mUri = getIntent().getData();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800187 mResolver = getContentResolver();
188
189 // Build the list of sections. The order they're added to mSections dictates the
190 // order they are displayed in the list.
191 mSections.add(mPhoneEntries);
192 mSections.add(mSmsEntries);
193 mSections.add(mEmailEntries);
194 mSections.add(mImEntries);
195 mSections.add(mPostalEntries);
196 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700197 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800198 mSections.add(mOtherEntries);
199
200 //TODO Read this value from a preference
201 mShowSmsLinksForAllPhones = true;
202
Evan Millar8a79cee2009-08-19 17:20:49 -0700203 //Select the all tab to start.
204 mSelectedRawContactId = ALL_CONTACTS_ID;
205
206 mCursor = mResolver.query(Uri.withAppendedPath(mUri, "data"),
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700207 CONTACT_PROJECTION, null, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800208 }
209
210 @Override
211 protected void onResume() {
212 super.onResume();
213 mObserverRegistered = true;
214 mCursor.registerContentObserver(mObserver);
215 dataChanged();
216 }
217
218 @Override
219 protected void onPause() {
220 super.onPause();
221 if (mCursor != null) {
222 if (mObserverRegistered) {
223 mObserverRegistered = false;
224 mCursor.unregisterContentObserver(mObserver);
225 }
226 mCursor.deactivate();
227 }
228 }
229
230 @Override
231 protected void onDestroy() {
232 super.onDestroy();
233
234 if (mCursor != null) {
235 if (mObserverRegistered) {
236 mCursor.unregisterContentObserver(mObserver);
237 mObserverRegistered = false;
238 }
239 mCursor.close();
240 }
241 }
242
243 @Override
244 protected Dialog onCreateDialog(int id) {
245 switch (id) {
246 case DIALOG_CONFIRM_DELETE:
247 return new AlertDialog.Builder(this)
248 .setTitle(R.string.deleteConfirmation_title)
249 .setIcon(android.R.drawable.ic_dialog_alert)
250 .setMessage(R.string.deleteConfirmation)
251 .setNegativeButton(android.R.string.cancel, null)
252 .setPositiveButton(android.R.string.ok, this)
253 .setCancelable(false)
254 .create();
255 }
256 return null;
257 }
258
Evan Millar7911ff52009-07-21 15:55:18 -0700259 @Override
Evan Millar5f4af702009-08-11 11:12:00 -0700260 protected void bindTabs(ArrayList<Entity> entities) {
261 if (entities.size() > 1) {
Evan Millar7911ff52009-07-21 15:55:18 -0700262 addAllTab();
263 }
Evan Millar5f4af702009-08-11 11:12:00 -0700264 super.bindTabs(entities);
Evan Millar7911ff52009-07-21 15:55:18 -0700265 }
266
267 private void addAllTab() {
268 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator, mTabWidget, false);
Evan Millar74660912009-08-19 17:36:33 -0700269 allTabIndicator.getBackground().setDither(true);
Evan Millar7911ff52009-07-21 15:55:18 -0700270 addTab(ALL_CONTACTS_ID, allTabIndicator);
271 }
272
273 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
274 long rawContactId = getTabRawContactId(tabIndex);
Evan Millar8a79cee2009-08-19 17:20:49 -0700275 mSelectedRawContactId = rawContactId;
Evan Millar7911ff52009-07-21 15:55:18 -0700276 dataChanged();
277 }
278
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800279 private void dataChanged() {
280 mCursor.requery();
281 if (mCursor.moveToFirst()) {
Evan Millar2c1cc832009-07-13 11:08:06 -0700282
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800283 // Build up the contact entries
284 buildEntries(mCursor);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700285
286 // Collapse similar data items in select sections.
287 Collapser.collapseList(mPhoneEntries);
288 Collapser.collapseList(mSmsEntries);
289 Collapser.collapseList(mEmailEntries);
290 Collapser.collapseList(mPostalEntries);
291
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800292 if (mAdapter == null) {
293 mAdapter = new ViewAdapter(this, mSections);
Evan Millar7911ff52009-07-21 15:55:18 -0700294 mListView.setAdapter(mAdapter);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800295 } else {
296 mAdapter.setSections(mSections, SHOW_SEPARATORS);
297 }
298 } else {
299 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
300 Log.e(TAG, "invalid contact uri: " + mUri);
301 finish();
302 }
303 }
304
305 @Override
306 public boolean onCreateOptionsMenu(Menu menu) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700307 menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_editContact)
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800308 .setIcon(android.R.drawable.ic_menu_edit)
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800309 .setAlphabeticShortcut('e');
310 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
311 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700312 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
313 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700314 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
315 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700316 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
317 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800318 return true;
319 }
320
321 @Override
322 public boolean onPrepareOptionsMenu(Menu menu) {
323 super.onPrepareOptionsMenu(menu);
324 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
325 // could be installed or uninstalled at any time.
326 if (isBarcodeScannerInstalled()) {
327 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
328 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
329 .setIcon(R.drawable.ic_menu_show_barcode);
330 }
331 } else {
332 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
333 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700334
Evan Millar7911ff52009-07-21 15:55:18 -0700335 boolean isAggregate = mRawContactIds.size() > 1;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700336 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800337 return true;
338 }
339
340 private boolean isBarcodeScannerInstalled() {
341 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
342 ResolveInfo ri = getPackageManager().resolveActivity(intent,
343 PackageManager.MATCH_DEFAULT_ONLY);
344 return ri != null;
345 }
346
347 @Override
348 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
349 AdapterView.AdapterContextMenuInfo info;
350 try {
351 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
352 } catch (ClassCastException e) {
353 Log.e(TAG, "bad menuInfo", e);
354 return;
355 }
356
357 // This can be null sometimes, don't crash...
358 if (info == null) {
359 Log.e(TAG, "bad menuInfo");
360 return;
361 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700362
Evan Millar45e0ed32009-06-01 16:44:38 -0700363 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
364 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
365 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700366 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
367 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700368 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800369 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700370 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
371 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700372 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700373 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800374 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700375 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700376 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
377 }
378 // TODO(emillar): add back with group support.
379 /* else if (entry.mimetype.equals()) {
380 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
381 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800382 }
383
384 @Override
385 public boolean onOptionsItemSelected(MenuItem item) {
386 switch (item.getItemId()) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700387 case MENU_ITEM_EDIT: {
Jeff Sharkey1cac9ff2009-08-22 23:49:02 -0700388 final int tabIndex = mTabWidget.getCurrentTab();
389 long rawContactIdToEdit = getTabRawContactId(tabIndex);
Evan Millar8a79cee2009-08-19 17:20:49 -0700390 if (rawContactIdToEdit == ALL_CONTACTS_ID) {
391 // If the "all" tab is selected, edit the next tab.
Jeff Sharkey1cac9ff2009-08-22 23:49:02 -0700392 rawContactIdToEdit = getTabRawContactId(tabIndex + 1);
Evan Millar8a79cee2009-08-19 17:20:49 -0700393 }
394 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
395 rawContactIdToEdit);
396 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
397 REQUEST_EDIT_CONTACT);
398 break;
399 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800400 case MENU_ITEM_DELETE: {
401 // Get confirmation
402 showDialog(DIALOG_CONFIRM_DELETE);
403 return true;
404 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700405
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700406 case MENU_ITEM_SPLIT_AGGREGATE: {
407 showSplitAggregateDialog();
408 return true;
409 }
410
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700411 case MENU_ITEM_JOIN_AGGREGATE: {
412 showJoinAggregateActivity();
413 return true;
414 }
415
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700416 case MENU_ITEM_OPTIONS: {
417 showOptionsActivity();
418 return true;
419 }
420
Evan Millar66388be2009-05-28 15:41:07 -0700421 // TODO(emillar) Bring this back.
422 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800423 if (mCursor.moveToFirst()) {
424 Intent intent = new Intent(SHOW_BARCODE_INTENT);
425 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
426 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700427 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800428 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700429 // Correctly handle when section headers are hidden
430 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700431
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800432 bundle.putString(Contacts.Intents.Insert.NAME, name);
433 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700434 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800435 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700436 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800437 bundle.putString(PHONE_KEYS[x], entry.data);
438 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700439 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800440 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700441 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800442 bundle.putString(EMAIL_KEYS[x], entry.data);
443 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700444 if (mPostalEntries.size() >= 1 + sepAdjust) {
445 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800446 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
447 }
448 intent.putExtra("ENCODE_DATA", bundle);
449 try {
450 startActivity(intent);
451 } catch (ActivityNotFoundException e) {
452 // The check in onPrepareOptionsMenu() should make this impossible, but
453 // for safety I'm catching the exception rather than crashing. Ideally
454 // I'd call Menu.removeItem() here too, but I don't see a way to get
455 // the options menu.
456 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
457 "was not installed.");
458 }
459 return true;
460 }
461 }
Evan Millar66388be2009-05-28 15:41:07 -0700462 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800463 }
464 return super.onOptionsItemSelected(item);
465 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700466
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800467 @Override
468 public boolean onContextItemSelected(MenuItem item) {
469 switch (item.getItemId()) {
470 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700471 if (makeItemDefault(item)) {
472 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800473 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700474 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800475 }
476 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700477
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800478 return super.onContextItemSelected(item);
479 }
480
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700481 private boolean makeItemDefault(MenuItem item) {
482 ViewEntry entry = getViewEntryForMenuItem(item);
483 if (entry == null) {
484 return false;
485 }
486
487 // Update the primary values in the data record.
488 ContentValues values = new ContentValues(2);
489 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700490
Evan Millar54a5c9f2009-06-23 17:41:09 -0700491 if (entry.ids.size() > 0) {
492 for (int i = 0; i < entry.ids.size(); i++) {
493 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
494 entry.ids.get(i)),
495 values, null, null);
496 }
497 }
498
499 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700500 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
501 values, null, null);
502 dataChanged();
503 return true;
504 }
505
506 /**
507 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
508 * The user picks a contact to be split into its own aggregate or clicks Cancel.
509 */
510 private void showSplitAggregateDialog() {
511
512 // Wrap this dialog in a specific theme so that list items have correct text color.
513 final ContextThemeWrapper dialogContext =
514 new ContextThemeWrapper(this, android.R.style.Theme_Light);
515 AlertDialog.Builder builder =
516 new AlertDialog.Builder(dialogContext);
517 builder.setTitle(getString(R.string.splitAggregate_title));
518
519 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
520 builder.setView(view);
521
522 builder.setInverseBackgroundForced(true);
523 builder.setCancelable(true);
524 builder.setNegativeButton(android.R.string.cancel,
525 new OnClickListener() {
526 public void onClick(DialogInterface dialog, int which) {
527 dialog.dismiss();
528 }
529 });
530 final AlertDialog dialog = builder.create();
531
532 view.setOnContactSelectedListener(new OnContactSelectedListener() {
533 public void onContactSelected(long contactId) {
534 dialog.dismiss();
535 splitContact(contactId);
536 }
537 });
538
539 dialog.show();
540 }
541
542 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700543 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700544 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700545 public void showJoinAggregateActivity() {
546 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
547 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700548 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700549 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700550
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700551 @Override
552 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700553 switch (requestCode) {
554 case REQUEST_JOIN_CONTACT: {
555 if (resultCode == RESULT_OK && intent != null) {
556 final long aggregateId = ContentUris.parseId(intent.getData());
557 joinAggregate(aggregateId);
558 }
559 break;
560 }
561 case REQUEST_EDIT_CONTACT: {
562 if (resultCode == RESULT_OK && intent != null) {
563 long newInitialSelectedRawContactId = intent.getLongExtra(
564 RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
565 if (newInitialSelectedRawContactId != mSelectedRawContactId) {
566 mSelectedRawContactId = newInitialSelectedRawContactId;
567 selectInitialTab();
568 }
569 }
570 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700571 }
572 }
573
574 private void splitContact(long contactId) {
575 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_OUT);
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700576 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700577 mAdapter.notifyDataSetChanged();
578 }
579
580 private void joinAggregate(final long aggregateId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700581 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700582 RawContacts.CONTACT_ID + "=" + aggregateId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700583
584 try {
585 while(c.moveToNext()) {
586 long contactId = c.getLong(0);
587 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
588 }
589 } finally {
590 c.close();
591 }
592
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700593 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700594 mAdapter.notifyDataSetChanged();
595 }
596
597 /**
598 * Given a contact ID sets an aggregation exception to either join the contact with the
599 * current aggregate or split off.
600 */
601 protected void setAggregationException(long contactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700602 ContentValues values = new ContentValues(3);
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700603 values.put(AggregationExceptions.CONTACT_ID, ContentUris.parseId(mUri));
Jeff Sharkey14f61ab2009-08-05 21:02:37 -0700604 values.put(AggregationExceptions.RAW_CONTACT_ID, contactId);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700605 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700606 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700607 }
608
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700609 private void showOptionsActivity() {
610 final Intent intent = new Intent(this, ContactOptionsActivity.class);
611 intent.setData(mUri);
612 startActivity(intent);
613 }
614
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700615 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
616 AdapterView.AdapterContextMenuInfo info;
617 try {
618 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
619 } catch (ClassCastException e) {
620 Log.e(TAG, "bad menuInfo", e);
621 return null;
622 }
623
624 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
625 }
626
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800627 @Override
628 public boolean onKeyDown(int keyCode, KeyEvent event) {
629 switch (keyCode) {
630 case KeyEvent.KEYCODE_CALL: {
631 try {
632 ITelephony phone = ITelephony.Stub.asInterface(
633 ServiceManager.checkService("phone"));
634 if (phone != null && !phone.isIdle()) {
635 // Skip out and let the key be handled at a higher level
636 break;
637 }
638 } catch (RemoteException re) {
639 // Fall through and try to call the contact
640 }
641
Evan Millar7911ff52009-07-21 15:55:18 -0700642 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800643 if (index != -1) {
644 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700645 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
646 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800647 }
648 } else if (mNumPhoneNumbers != 0) {
649 // There isn't anything selected, call the default number
650 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
651 startActivity(intent);
652 }
653 return true;
654 }
655
656 case KeyEvent.KEYCODE_DEL: {
657 showDialog(DIALOG_CONFIRM_DELETE);
658 return true;
659 }
660 }
661
662 return super.onKeyDown(keyCode, event);
663 }
664
Evan Millar7911ff52009-07-21 15:55:18 -0700665 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800666 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
667 if (entry != null) {
668 Intent intent = entry.intent;
669 if (intent != null) {
670 try {
671 startActivity(intent);
672 } catch (ActivityNotFoundException e) {
673 Log.e(TAG, "No activity found for intent: " + intent);
674 signalError();
675 }
676 } else {
677 signalError();
678 }
679 } else {
680 signalError();
681 }
682 }
683
684 /**
685 * Signal an error to the user via a beep, or some other method.
686 */
687 private void signalError() {
688 //TODO: implement this when we have the sonification APIs
689 }
690
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800691 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700692 // 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 -0800693 StringBuilder buf = new StringBuilder("imto://");
694 buf.append(host);
695 buf.append('/');
696 buf.append(data);
697 return Uri.parse(buf.toString());
698 }
699
700 /**
701 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700702 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800703 * @param personCursor the URI for the contact being displayed
704 */
Evan Millar66388be2009-05-28 15:41:07 -0700705 private final void buildEntries(Cursor aggCursor) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800706 // Clear out the old entries
707 final int numSections = mSections.size();
708 for (int i = 0; i < numSections; i++) {
709 mSections.get(i).clear();
710 }
711
Evan Millar7911ff52009-07-21 15:55:18 -0700712 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700713
Evan Millar66388be2009-05-28 15:41:07 -0700714 // Build up method entries
715 if (mUri != null) {
Jeff Hamilton119b6ca2009-07-30 12:57:49 -0500716 aggCursor.moveToPosition(-1);
Evan Millar66388be2009-05-28 15:41:07 -0700717 while (aggCursor.moveToNext()) {
718 final String mimetype = aggCursor.getString(DATA_MIMETYPE_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700719
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800720 ViewEntry entry = new ViewEntry();
Evan Millar66388be2009-05-28 15:41:07 -0700721
722 final long id = aggCursor.getLong(DATA_ID_COLUMN);
723 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800724 entry.id = id;
725 entry.uri = uri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700726 entry.mimetype = mimetype;
Evan Millar7911ff52009-07-21 15:55:18 -0700727 // TODO: entry.contactId should be renamed to entry.rawContactId
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700728 entry.contactId = aggCursor.getLong(DATA_CONTACT_ID_COLUMN);
Evan Millar7911ff52009-07-21 15:55:18 -0700729 if (!mRawContactIds.contains(entry.contactId)) {
730 mRawContactIds.add(entry.contactId);
731 }
732
733 // This performs the tab filtering
Evan Millar8a79cee2009-08-19 17:20:49 -0700734 if (mSelectedRawContactId != entry.contactId
735 && mSelectedRawContactId != ALL_CONTACTS_ID) {
Evan Millar7911ff52009-07-21 15:55:18 -0700736 continue;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700737 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800738
Evan Millar66388be2009-05-28 15:41:07 -0700739 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
Evan Millar5c22c3b2009-05-29 11:37:54 -0700740 || mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700741 || mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
Evan Millar66388be2009-05-28 15:41:07 -0700742 || mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
743 final int type = aggCursor.getInt(DATA_1_COLUMN);
Evan Millar45e0ed32009-06-01 16:44:38 -0700744 final String label = aggCursor.getString(DATA_3_COLUMN);
745 final String data = aggCursor.getString(DATA_2_COLUMN);
746 final boolean isSuperPrimary = "1".equals(
747 aggCursor.getString(DATA_IS_SUPER_PRIMARY_COLUMN));
Evan Millar66388be2009-05-28 15:41:07 -0700748
Evan Millar54a5c9f2009-06-23 17:41:09 -0700749 entry.type = type;
750
Evan Millar66388be2009-05-28 15:41:07 -0700751 // Don't crash if the data is bogus
752 if (TextUtils.isEmpty(data)) {
753 Log.w(TAG, "empty data for contact method " + id);
754 continue;
755 }
756
757 // Build phone entries
758 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
759 mNumPhoneNumbers++;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700760
Evan Millar66388be2009-05-28 15:41:07 -0700761 final CharSequence displayLabel = ContactsUtils.getDisplayLabel(
762 this, mimetype, type, label);
763 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700764 entry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar296758b2009-08-07 15:40:08 -0700765 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
766 Uri.fromParts("tel", data, null));
Evan Millar15e514d2009-08-04 10:14:57 -0700767 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
Evan Millar66388be2009-05-28 15:41:07 -0700768 Uri.fromParts("sms", data, null));
Evan Millar15e514d2009-08-04 10:14:57 -0700769 entry.isPrimary = isSuperPrimary;
Evan Millar66388be2009-05-28 15:41:07 -0700770 entry.actionIcon = android.R.drawable.sym_action_call;
771 mPhoneEntries.add(entry);
772
Evan Millar5c22c3b2009-05-29 11:37:54 -0700773 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700774 || mShowSmsLinksForAllPhones) {
775 // Add an SMS entry
Evan Millar15e514d2009-08-04 10:14:57 -0700776 entry.secondaryActionIcon = R.drawable.sym_action_sms;
Evan Millar66388be2009-05-28 15:41:07 -0700777 }
778 // Build email entries
779 } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800780 entry.label = buildActionString(R.string.actionEmail,
Evan Millar66388be2009-05-28 15:41:07 -0700781 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800782 entry.data = data;
783 entry.intent = new Intent(Intent.ACTION_SENDTO,
784 Uri.fromParts("mailto", data, null));
785 entry.actionIcon = android.R.drawable.sym_action_email;
Evan Millar15e514d2009-08-04 10:14:57 -0700786 entry.isPrimary = isSuperPrimary;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800787 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700788 // Build postal entries
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700789 } else if (mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800790 entry.label = buildActionString(R.string.actionMap,
Evan Millar66388be2009-05-28 15:41:07 -0700791 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800792 entry.data = data;
793 entry.maxLines = 4;
794 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
795 entry.actionIcon = R.drawable.sym_action_map;
796 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700797 // Build im entries
798 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
799 String[] protocolStrings = getResources().getStringArray(
800 android.R.array.imProtocols);
Jeff Sharkeye731d422009-07-28 21:12:43 -0700801 Object protocolObj = aggCursor.getString(DATA_5_COLUMN);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700802 String host = null;
Jeff Sharkeye731d422009-07-28 21:12:43 -0700803 // TODO: fix by moving to contactssource-based rendering rules
804// Object protocolObj = ContactsUtils.decodeImProtocol(
805// aggCursor.getString(DATA_5_COLUMN));
806// if (protocolObj instanceof Number) {
807// int protocol = ((Number) protocolObj).intValue();
808// entry.label = buildActionString(R.string.actionChat,
809// protocolStrings[protocol], false);
810// host = ContactsUtils.lookupProviderNameFromId(
811// protocol).toLowerCase();
812// if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
813// || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
814// entry.maxLabelLines = 2;
815// }
Neel Parekh064fa802009-08-12 12:57:12 -0700816 if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800817 String providerName = (String) protocolObj;
818 entry.label = buildActionString(R.string.actionChat,
819 providerName, false);
820 host = providerName.toLowerCase();
Neel Parekh064fa802009-08-12 12:57:12 -0700821 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800822
823 // Only add the intent if there is a valid host
824 if (!TextUtils.isEmpty(host)) {
825 entry.intent = new Intent(Intent.ACTION_SENDTO,
826 constructImToUrl(host, data));
827 }
828 entry.data = data;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700829 //TODO(emillar) Add in presence info
Evan Millar66388be2009-05-28 15:41:07 -0700830 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800831 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700832 aggCursor.getInt(METHODS_STATUS_COLUMN));
Evan Millar54a5c9f2009-06-23 17:41:09 -0700833 entry.status = ...
Evan Millar66388be2009-05-28 15:41:07 -0700834 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800835 entry.actionIcon = android.R.drawable.sym_action_chat;
836 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700837 }
Evan Millar66388be2009-05-28 15:41:07 -0700838 // Build organization entries
839 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
Evan Millar66388be2009-05-28 15:41:07 -0700840 final String company = aggCursor.getString(DATA_3_COLUMN);
841 final String title = aggCursor.getString(DATA_4_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700842
Evan Millar66388be2009-05-28 15:41:07 -0700843 // Don't crash if the data is bogus
844 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
845 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800846 continue;
847 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700848
Evan Millar66388be2009-05-28 15:41:07 -0700849 entry.data = title;
850 entry.actionIcon = R.drawable.sym_action_organization;
851 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800852
Evan Millar66388be2009-05-28 15:41:07 -0700853 mOrganizationEntries.add(entry);
854 // Build note entries
855 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
856 entry.label = getString(R.string.label_notes);
857 entry.data = aggCursor.getString(DATA_1_COLUMN);
858 entry.id = 0;
859 entry.uri = null;
860 entry.intent = null;
861 entry.maxLines = 10;
862 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800863
Evan Millar66388be2009-05-28 15:41:07 -0700864 if (TextUtils.isEmpty(entry.data)) {
865 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700866 continue;
867 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700868
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800869 mOtherEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700870 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700871
Evan Millar45e0ed32009-06-01 16:44:38 -0700872
Evan Millar66388be2009-05-28 15:41:07 -0700873 // TODO(emillar) Add group entries
874// // Build the group entries
875// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
876// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
877// null, null, Groups.DEFAULT_SORT_ORDER);
878// if (groupCursor != null) {
879// try {
880// StringBuilder sb = new StringBuilder();
881//
882// while (groupCursor.moveToNext()) {
883// String systemId = groupCursor.getString(
884// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
885//
886// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
887// continue;
888// }
889//
890// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
891// if (!TextUtils.isEmpty(name)) {
892// if (sb.length() == 0) {
893// sb.append(name);
894// } else {
895// sb.append(getString(R.string.group_list, name));
896// }
897// }
898// }
899//
900// if (sb.length() > 0) {
901// ViewEntry entry = new ViewEntry();
902// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
903// entry.label = getString(R.string.label_groups);
904// entry.data = sb.toString();
905// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
906//
907// // TODO: Add an icon for the groups item.
908//
909// mGroupEntries.add(entry);
910// }
911// } finally {
912// groupCursor.close();
913// }
914// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700915
Evan Millar66388be2009-05-28 15:41:07 -0700916 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800917 }
918 }
919
920 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700921 // If there is no type just display an empty string
922 if (type == null) {
923 type = "";
924 }
925
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800926 if (lowerCase) {
927 return getString(actionResId, type.toString().toLowerCase());
928 } else {
929 return getString(actionResId, type.toString());
930 }
931 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700932
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800933 /**
934 * A basic structure with the data for a contact entry in the list.
935 */
Evan Millar54a5c9f2009-06-23 17:41:09 -0700936 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800937 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -0700938 public boolean isPrimary = false;
939 public int presenceIcon = -1;
940 public int secondaryActionIcon = -1;
941 public Intent intent;
942 public Intent secondaryIntent = null;
943 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800944 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700945 public ArrayList<Long> ids = new ArrayList<Long>();
946 public int collapseCount = 0;
947
948 public boolean collapseWith(ViewEntry entry) {
949 // assert equal collapse keys
950 if (!getCollapseKey().equals(entry.getCollapseKey())) {
951 return false;
952 }
953
954 // Choose the label associated with the highest type precedence.
955 if (TypePrecedence.getTypePrecedence(mimetype, type)
956 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
957 type = entry.type;
958 label = entry.label;
959 }
960
961 // Choose the max of the maxLines and maxLabelLines values.
962 maxLines = Math.max(maxLines, entry.maxLines);
963 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
964
965 // Choose the presence with the highest precedence.
966 if (Presence.getPresencePrecedence(status)
967 < Presence.getPresencePrecedence(entry.status)) {
968 status = entry.status;
969 }
970
971 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -0700972 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700973
974 // uri, and contactdId, shouldn't make a difference. Just keep the original.
975
976 // Keep track of all the ids that have been collapsed with this one.
977 ids.add(entry.id);
978 collapseCount++;
979 return true;
980 }
981
982 public String getCollapseKey() {
983 StringBuilder hashSb = new StringBuilder();
984 hashSb.append(data);
985 hashSb.append(mimetype);
986 hashSb.append((intent != null && intent.getAction() != null)
987 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -0700988 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
989 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -0700990 hashSb.append(actionIcon);
991 return hashSb.toString();
992 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800993 }
994
Evan Millar15e514d2009-08-04 10:14:57 -0700995 /** Cache of the children views of a row */
996 static class ViewCache {
997 public TextView label;
998 public TextView data;
999 public ImageView actionIcon;
1000 public ImageView presenceIcon;
1001 public ImageView primaryIcon;
1002 public ImageView secondaryActionButton;
1003 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001004
Evan Millar15e514d2009-08-04 10:14:57 -07001005 // Need to keep track of this too
1006 ViewEntry entry;
1007 }
1008
1009 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1010 implements View.OnClickListener {
1011
Evan Millar5c22c3b2009-05-29 11:37:54 -07001012
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001013 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1014 super(context, sections, SHOW_SEPARATORS);
1015 }
1016
Evan Millar15e514d2009-08-04 10:14:57 -07001017 public void onClick(View v) {
1018 Intent intent = (Intent) v.getTag();
1019 startActivity(intent);
1020 }
1021
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001022 @Override
1023 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001024 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001025 View v;
1026
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001027 ViewCache views;
1028
1029 // Check to see if we can reuse convertView
1030 if (convertView != null) {
1031 v = convertView;
1032 views = (ViewCache) v.getTag();
1033 } else {
1034 // Create a new view if needed
1035 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1036
1037 // Cache the children
1038 views = new ViewCache();
1039 views.label = (TextView) v.findViewById(android.R.id.text1);
1040 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001041 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1042 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1043 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1044 views.secondaryActionButton = (ImageView) v.findViewById(
1045 R.id.secondary_action_button);
1046 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001047 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001048 v.setTag(views);
1049 }
1050
1051 // Update the entry in the view cache
1052 views.entry = entry;
1053
1054 // Bind the data to the view
1055 bindView(v, entry);
1056 return v;
1057 }
1058
1059 @Override
1060 protected View newView(int position, ViewGroup parent) {
1061 // getView() handles this
1062 throw new UnsupportedOperationException();
1063 }
1064
1065 @Override
1066 protected void bindView(View view, ViewEntry entry) {
1067 final Resources resources = mContext.getResources();
1068 ViewCache views = (ViewCache) view.getTag();
1069
1070 // Set the label
1071 TextView label = views.label;
1072 setMaxLines(label, entry.maxLabelLines);
1073 label.setText(entry.label);
1074
1075 // Set the data
1076 TextView data = views.data;
1077 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001078 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1079 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1080 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1081 } else {
1082 data.setText(entry.data);
1083 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001084 setMaxLines(data, entry.maxLines);
1085 }
1086
Evan Millar15e514d2009-08-04 10:14:57 -07001087 // Set the primary icon
1088 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1089
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001090 // Set the action icon
1091 ImageView action = views.actionIcon;
1092 if (entry.actionIcon != -1) {
1093 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1094 action.setVisibility(View.VISIBLE);
1095 } else {
1096 // Things should still line up as if there was an icon, so make it invisible
1097 action.setVisibility(View.INVISIBLE);
1098 }
1099
1100 // Set the presence icon
1101 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001102 if (entry.presenceIcon != -1) {
1103 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001104 } else if (entry.status != -1) {
1105 presenceIcon = resources.getDrawable(
1106 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001107 }
Evan Millar15e514d2009-08-04 10:14:57 -07001108 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001109 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001110 presenceIconView.setImageDrawable(presenceIcon);
1111 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001112 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001113 presenceIconView.setVisibility(View.GONE);
1114 }
1115
1116 // Set the secondary action button
1117 ImageView secondaryActionView = views.secondaryActionButton;
1118 Drawable secondaryActionIcon = null;
1119 if (entry.secondaryActionIcon != -1) {
1120 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1121 }
1122 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1123 secondaryActionView.setImageDrawable(secondaryActionIcon);
1124 secondaryActionView.setTag(entry.secondaryIntent);
1125 secondaryActionView.setVisibility(View.VISIBLE);
1126 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1127 } else {
1128 secondaryActionView.setVisibility(View.GONE);
1129 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001130 }
1131 }
1132
1133 private void setMaxLines(TextView textView, int maxLines) {
1134 if (maxLines == 1) {
1135 textView.setSingleLine(true);
1136 textView.setEllipsize(TextUtils.TruncateAt.END);
1137 } else {
1138 textView.setSingleLine(false);
1139 textView.setMaxLines(maxLines);
1140 textView.setEllipsize(null);
1141 }
1142 }
1143 }
1144}