blob: 549c8ab145343545c6d7063348e585b25a8c3d25 [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_PROJECTION;
Evan Millar66388be2009-05-28 15:41:07 -070020import static com.android.contacts.ContactEntryAdapter.DATA_1_COLUMN;
21import static com.android.contacts.ContactEntryAdapter.DATA_2_COLUMN;
22import static com.android.contacts.ContactEntryAdapter.DATA_3_COLUMN;
23import static com.android.contacts.ContactEntryAdapter.DATA_4_COLUMN;
24import static com.android.contacts.ContactEntryAdapter.DATA_5_COLUMN;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070025import static com.android.contacts.ContactEntryAdapter.DATA_CONTACT_ID_COLUMN;
26import static com.android.contacts.ContactEntryAdapter.DATA_ID_COLUMN;
27import static com.android.contacts.ContactEntryAdapter.DATA_IS_SUPER_PRIMARY_COLUMN;
28import static com.android.contacts.ContactEntryAdapter.DATA_MIMETYPE_COLUMN;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080029
Evan Millar54a5c9f2009-06-23 17:41:09 -070030import com.android.contacts.Collapser.Collapsible;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070031import com.android.contacts.SplitAggregateView.OnContactSelectedListener;
Jeff Sharkey802b2052009-08-04 14:21:06 -070032import com.android.contacts.ui.FastTrackWindow;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080033import com.android.internal.telephony.ITelephony;
34
35import android.app.AlertDialog;
36import android.app.Dialog;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080037import android.content.ActivityNotFoundException;
38import android.content.ContentResolver;
39import android.content.ContentUris;
40import android.content.ContentValues;
41import android.content.Context;
42import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070043import android.content.Entity;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080044import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070045import android.content.DialogInterface.OnClickListener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080046import android.content.pm.PackageManager;
47import android.content.pm.ResolveInfo;
48import android.content.res.Resources;
49import android.database.ContentObserver;
50import android.database.Cursor;
Dmitri Plotnikov3d53ce22009-09-02 08:44:32 -070051import android.database.DatabaseUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080052import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080053import android.net.Uri;
54import android.os.Bundle;
55import android.os.Handler;
56import android.os.RemoteException;
57import android.os.ServiceManager;
Dmitri Plotnikov3d53ce22009-09-02 08:44:32 -070058import android.provider.ContactsContract;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070059import android.provider.ContactsContract.AggregationExceptions;
60import android.provider.ContactsContract.CommonDataKinds;
Evan Millar66388be2009-05-28 15:41:07 -070061import android.provider.ContactsContract.Data;
Evan Millar54a5c9f2009-06-23 17:41:09 -070062import android.provider.ContactsContract.Presence;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070063import android.provider.ContactsContract.RawContacts;
Evan Millar54a5c9f2009-06-23 17:41:09 -070064import android.provider.ContactsContract.CommonDataKinds.Phone;
65import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080066import android.text.TextUtils;
67import android.util.Log;
68import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070069import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080070import android.view.KeyEvent;
71import android.view.Menu;
72import android.view.MenuItem;
73import android.view.View;
74import android.view.ViewGroup;
75import android.view.ContextMenu.ContextMenuInfo;
76import android.widget.AdapterView;
Evan Millar7911ff52009-07-21 15:55:18 -070077import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080078import android.widget.ImageView;
79import android.widget.ListView;
80import android.widget.TextView;
81import android.widget.Toast;
82
83import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080084
85/**
86 * Displays the details of a specific contact.
87 */
Evan Millar7911ff52009-07-21 15:55:18 -070088public class ViewContactActivity extends BaseContactCardActivity
89 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
90 AdapterView.OnItemClickListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080091 private static final String TAG = "ViewContact";
92 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
93
Evan Millar8a79cee2009-08-19 17:20:49 -070094 public static final String RAW_CONTACT_ID_EXTRA = "rawContactIdExtra";
95
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080096 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080097
98 private static final int DIALOG_CONFIRM_DELETE = 1;
99
Evan Millar8a79cee2009-08-19 17:20:49 -0700100 private static final int REQUEST_JOIN_CONTACT = 1;
101 private static final int REQUEST_EDIT_CONTACT = 2;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700102
Evan Millar8a79cee2009-08-19 17:20:49 -0700103 public static final int MENU_ITEM_EDIT = 1;
104 public static final int MENU_ITEM_DELETE = 2;
105 public static final int MENU_ITEM_MAKE_DEFAULT = 3;
106 public static final int MENU_ITEM_SHOW_BARCODE = 4;
107 public static final int MENU_ITEM_SPLIT_AGGREGATE = 5;
108 public static final int MENU_ITEM_JOIN_AGGREGATE = 6;
109 public static final int MENU_ITEM_OPTIONS = 7;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800110
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800111 private ContentResolver mResolver;
112 private ViewAdapter mAdapter;
113 private int mNumPhoneNumbers = 0;
114
Evan Millar8a79cee2009-08-19 17:20:49 -0700115 private static final long ALL_CONTACTS_ID = -100;
Evan Millar7911ff52009-07-21 15:55:18 -0700116
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700117 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700118 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700119 */
Evan Millar7911ff52009-07-21 15:55:18 -0700120 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700121
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800122 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
123 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
124 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
125 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
126 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
127 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700128 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800129 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
130 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
131
132 private Cursor mCursor;
133 private boolean mObserverRegistered;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700134
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800135 private ContentObserver mObserver = new ContentObserver(new Handler()) {
136 @Override
137 public boolean deliverSelfNotifications() {
138 return true;
139 }
140
141 @Override
142 public void onChange(boolean selfChange) {
143 if (mCursor != null && !mCursor.isClosed()){
144 dataChanged();
145 }
146 }
147 };
148
149 public void onClick(DialogInterface dialog, int which) {
150 if (mCursor != null) {
151 if (mObserverRegistered) {
152 mCursor.unregisterContentObserver(mObserver);
153 mObserverRegistered = false;
154 }
155 mCursor.close();
156 mCursor = null;
157 }
158 getContentResolver().delete(mUri, null, null);
159 finish();
160 }
161
Evan Millar7911ff52009-07-21 15:55:18 -0700162 private FrameLayout mTabContentLayout;
163 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800164 private boolean mShowSmsLinksForAllPhones;
165
166 @Override
167 protected void onCreate(Bundle icicle) {
168 super.onCreate(icicle);
169
Evan Millar7911ff52009-07-21 15:55:18 -0700170 mListView = new ListView(this);
171 mListView.setOnCreateContextMenuListener(this);
172 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
173 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800174
Evan Millar7911ff52009-07-21 15:55:18 -0700175 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
176 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800177
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800178 mResolver = getContentResolver();
179
180 // Build the list of sections. The order they're added to mSections dictates the
181 // order they are displayed in the list.
182 mSections.add(mPhoneEntries);
183 mSections.add(mSmsEntries);
184 mSections.add(mEmailEntries);
185 mSections.add(mImEntries);
186 mSections.add(mPostalEntries);
187 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700188 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800189 mSections.add(mOtherEntries);
190
191 //TODO Read this value from a preference
192 mShowSmsLinksForAllPhones = true;
193
Evan Millar8a79cee2009-08-19 17:20:49 -0700194 mCursor = mResolver.query(Uri.withAppendedPath(mUri, "data"),
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700195 CONTACT_PROJECTION, null, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800196 }
197
198 @Override
199 protected void onResume() {
200 super.onResume();
201 mObserverRegistered = true;
202 mCursor.registerContentObserver(mObserver);
203 dataChanged();
204 }
205
206 @Override
207 protected void onPause() {
208 super.onPause();
209 if (mCursor != null) {
210 if (mObserverRegistered) {
211 mObserverRegistered = false;
212 mCursor.unregisterContentObserver(mObserver);
213 }
214 mCursor.deactivate();
215 }
216 }
217
218 @Override
219 protected void onDestroy() {
220 super.onDestroy();
221
222 if (mCursor != null) {
223 if (mObserverRegistered) {
224 mCursor.unregisterContentObserver(mObserver);
225 mObserverRegistered = false;
226 }
227 mCursor.close();
228 }
229 }
230
231 @Override
232 protected Dialog onCreateDialog(int id) {
233 switch (id) {
234 case DIALOG_CONFIRM_DELETE:
235 return new AlertDialog.Builder(this)
236 .setTitle(R.string.deleteConfirmation_title)
237 .setIcon(android.R.drawable.ic_dialog_alert)
238 .setMessage(R.string.deleteConfirmation)
239 .setNegativeButton(android.R.string.cancel, null)
240 .setPositiveButton(android.R.string.ok, this)
241 .setCancelable(false)
242 .create();
243 }
244 return null;
245 }
246
Evan Millar7911ff52009-07-21 15:55:18 -0700247 @Override
Evan Millar5f4af702009-08-11 11:12:00 -0700248 protected void bindTabs(ArrayList<Entity> entities) {
249 if (entities.size() > 1) {
Evan Millar7911ff52009-07-21 15:55:18 -0700250 addAllTab();
251 }
Evan Millar5f4af702009-08-11 11:12:00 -0700252 super.bindTabs(entities);
Evan Millar7911ff52009-07-21 15:55:18 -0700253 }
254
255 private void addAllTab() {
Evan Millar56d2caa2009-08-20 20:30:12 -0700256 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator,
257 mTabWidget.getTabParent(), false);
Evan Millar74660912009-08-19 17:36:33 -0700258 allTabIndicator.getBackground().setDither(true);
Evan Millar7911ff52009-07-21 15:55:18 -0700259 addTab(ALL_CONTACTS_ID, allTabIndicator);
260 }
261
262 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
263 long rawContactId = getTabRawContactId(tabIndex);
Evan Millar8a79cee2009-08-19 17:20:49 -0700264 mSelectedRawContactId = rawContactId;
Evan Millar7911ff52009-07-21 15:55:18 -0700265 dataChanged();
266 }
267
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800268 private void dataChanged() {
269 mCursor.requery();
270 if (mCursor.moveToFirst()) {
Evan Millar2c1cc832009-07-13 11:08:06 -0700271
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800272 // Build up the contact entries
273 buildEntries(mCursor);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700274
275 // Collapse similar data items in select sections.
276 Collapser.collapseList(mPhoneEntries);
277 Collapser.collapseList(mSmsEntries);
278 Collapser.collapseList(mEmailEntries);
279 Collapser.collapseList(mPostalEntries);
280
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800281 if (mAdapter == null) {
282 mAdapter = new ViewAdapter(this, mSections);
Evan Millar7911ff52009-07-21 15:55:18 -0700283 mListView.setAdapter(mAdapter);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800284 } else {
285 mAdapter.setSections(mSections, SHOW_SEPARATORS);
286 }
287 } else {
288 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov3d53ce22009-09-02 08:44:32 -0700289 Log.e(TAG, "invalid contact uri: " + mOriginalUri);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800290 finish();
291 }
292 }
293
294 @Override
295 public boolean onCreateOptionsMenu(Menu menu) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800296 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
297 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700298 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
299 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700300 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
301 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700302 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
303 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800304 return true;
305 }
306
307 @Override
308 public boolean onPrepareOptionsMenu(Menu menu) {
309 super.onPrepareOptionsMenu(menu);
310 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
311 // could be installed or uninstalled at any time.
312 if (isBarcodeScannerInstalled()) {
313 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
314 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
315 .setIcon(R.drawable.ic_menu_show_barcode);
316 }
317 } else {
318 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
319 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700320
Evan Millardb5d88c2009-08-28 09:31:57 -0700321 // Only show the edit option if we have a selected tab.
322 if (mSelectedRawContactId != null) {
323 if (menu.findItem(MENU_ITEM_EDIT) == null) {
324 menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_editContact)
325 .setIcon(android.R.drawable.ic_menu_edit)
326 .setAlphabeticShortcut('e');
327 }
328 } else {
329 menu.removeItem(MENU_ITEM_EDIT);
330 }
331
Evan Millar7911ff52009-07-21 15:55:18 -0700332 boolean isAggregate = mRawContactIds.size() > 1;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700333 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800334 return true;
335 }
336
337 private boolean isBarcodeScannerInstalled() {
338 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
339 ResolveInfo ri = getPackageManager().resolveActivity(intent,
340 PackageManager.MATCH_DEFAULT_ONLY);
341 return ri != null;
342 }
343
344 @Override
345 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
346 AdapterView.AdapterContextMenuInfo info;
347 try {
348 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
349 } catch (ClassCastException e) {
350 Log.e(TAG, "bad menuInfo", e);
351 return;
352 }
353
354 // This can be null sometimes, don't crash...
355 if (info == null) {
356 Log.e(TAG, "bad menuInfo");
357 return;
358 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700359
Evan Millar45e0ed32009-06-01 16:44:38 -0700360 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
361 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
362 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700363 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
364 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700365 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800366 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700367 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
368 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700369 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700370 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800371 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700372 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700373 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
374 }
375 // TODO(emillar): add back with group support.
376 /* else if (entry.mimetype.equals()) {
377 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
378 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800379 }
380
381 @Override
382 public boolean onOptionsItemSelected(MenuItem item) {
383 switch (item.getItemId()) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700384 case MENU_ITEM_EDIT: {
Evan Millardb5d88c2009-08-28 09:31:57 -0700385 Long rawContactIdToEdit = mSelectedRawContactId;
386 if (rawContactIdToEdit == null) {
387 // This shouldn't be possible. We only show the edit option if
388 // this value is non-null.
389 break;
390 }
Evan Millar8a79cee2009-08-19 17:20:49 -0700391 if (rawContactIdToEdit == ALL_CONTACTS_ID) {
392 // If the "all" tab is selected, edit the next tab.
Evan Millardb5d88c2009-08-28 09:31:57 -0700393 rawContactIdToEdit = getTabRawContactId(mTabWidget.getCurrentTab() + 1);
Evan Millar8a79cee2009-08-19 17:20:49 -0700394 }
395 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
396 rawContactIdToEdit);
397 startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri),
398 REQUEST_EDIT_CONTACT);
399 break;
400 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800401 case MENU_ITEM_DELETE: {
402 // Get confirmation
403 showDialog(DIALOG_CONFIRM_DELETE);
404 return true;
405 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700406
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700407 case MENU_ITEM_SPLIT_AGGREGATE: {
408 showSplitAggregateDialog();
409 return true;
410 }
411
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700412 case MENU_ITEM_JOIN_AGGREGATE: {
413 showJoinAggregateActivity();
414 return true;
415 }
416
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700417 case MENU_ITEM_OPTIONS: {
418 showOptionsActivity();
419 return true;
420 }
421
Evan Millar66388be2009-05-28 15:41:07 -0700422 // TODO(emillar) Bring this back.
423 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800424 if (mCursor.moveToFirst()) {
425 Intent intent = new Intent(SHOW_BARCODE_INTENT);
426 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
427 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700428 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800429 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700430 // Correctly handle when section headers are hidden
431 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700432
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800433 bundle.putString(Contacts.Intents.Insert.NAME, name);
434 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700435 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800436 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700437 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800438 bundle.putString(PHONE_KEYS[x], entry.data);
439 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700440 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800441 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700442 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800443 bundle.putString(EMAIL_KEYS[x], entry.data);
444 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700445 if (mPostalEntries.size() >= 1 + sepAdjust) {
446 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800447 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
448 }
449 intent.putExtra("ENCODE_DATA", bundle);
450 try {
451 startActivity(intent);
452 } catch (ActivityNotFoundException e) {
453 // The check in onPrepareOptionsMenu() should make this impossible, but
454 // for safety I'm catching the exception rather than crashing. Ideally
455 // I'd call Menu.removeItem() here too, but I don't see a way to get
456 // the options menu.
457 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
458 "was not installed.");
459 }
460 return true;
461 }
462 }
Evan Millar66388be2009-05-28 15:41:07 -0700463 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800464 }
465 return super.onOptionsItemSelected(item);
466 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700467
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800468 @Override
469 public boolean onContextItemSelected(MenuItem item) {
470 switch (item.getItemId()) {
471 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700472 if (makeItemDefault(item)) {
473 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800474 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700475 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800476 }
477 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700478
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800479 return super.onContextItemSelected(item);
480 }
481
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700482 private boolean makeItemDefault(MenuItem item) {
483 ViewEntry entry = getViewEntryForMenuItem(item);
484 if (entry == null) {
485 return false;
486 }
487
488 // Update the primary values in the data record.
489 ContentValues values = new ContentValues(2);
490 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700491
Evan Millar54a5c9f2009-06-23 17:41:09 -0700492 if (entry.ids.size() > 0) {
493 for (int i = 0; i < entry.ids.size(); i++) {
494 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
495 entry.ids.get(i)),
496 values, null, null);
497 }
498 }
499
500 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700501 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
502 values, null, null);
503 dataChanged();
504 return true;
505 }
506
507 /**
508 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
509 * The user picks a contact to be split into its own aggregate or clicks Cancel.
510 */
511 private void showSplitAggregateDialog() {
512
513 // Wrap this dialog in a specific theme so that list items have correct text color.
514 final ContextThemeWrapper dialogContext =
515 new ContextThemeWrapper(this, android.R.style.Theme_Light);
516 AlertDialog.Builder builder =
517 new AlertDialog.Builder(dialogContext);
518 builder.setTitle(getString(R.string.splitAggregate_title));
519
520 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
521 builder.setView(view);
522
523 builder.setInverseBackgroundForced(true);
524 builder.setCancelable(true);
525 builder.setNegativeButton(android.R.string.cancel,
526 new OnClickListener() {
527 public void onClick(DialogInterface dialog, int which) {
528 dialog.dismiss();
529 }
530 });
531 final AlertDialog dialog = builder.create();
532
533 view.setOnContactSelectedListener(new OnContactSelectedListener() {
534 public void onContactSelected(long contactId) {
535 dialog.dismiss();
536 splitContact(contactId);
537 }
538 });
539
540 dialog.show();
541 }
542
543 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700544 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700545 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700546 public void showJoinAggregateActivity() {
547 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
548 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
Evan Millar8a79cee2009-08-19 17:20:49 -0700549 startActivityForResult(intent, REQUEST_JOIN_CONTACT);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700550 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700551
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700552 @Override
553 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
Evan Millar8a79cee2009-08-19 17:20:49 -0700554 switch (requestCode) {
555 case REQUEST_JOIN_CONTACT: {
556 if (resultCode == RESULT_OK && intent != null) {
557 final long aggregateId = ContentUris.parseId(intent.getData());
558 joinAggregate(aggregateId);
559 }
560 break;
561 }
562 case REQUEST_EDIT_CONTACT: {
563 if (resultCode == RESULT_OK && intent != null) {
564 long newInitialSelectedRawContactId = intent.getLongExtra(
565 RAW_CONTACT_ID_EXTRA, ALL_CONTACTS_ID);
566 if (newInitialSelectedRawContactId != mSelectedRawContactId) {
567 mSelectedRawContactId = newInitialSelectedRawContactId;
568 selectInitialTab();
569 }
570 }
571 }
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700572 }
573 }
574
575 private void splitContact(long contactId) {
576 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_OUT);
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700577 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700578 mAdapter.notifyDataSetChanged();
579 }
580
581 private void joinAggregate(final long aggregateId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700582 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700583 RawContacts.CONTACT_ID + "=" + aggregateId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700584
585 try {
586 while(c.moveToNext()) {
587 long contactId = c.getLong(0);
588 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
589 }
590 } finally {
591 c.close();
592 }
593
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700594 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700595 mAdapter.notifyDataSetChanged();
596 }
597
598 /**
599 * Given a contact ID sets an aggregation exception to either join the contact with the
600 * current aggregate or split off.
601 */
602 protected void setAggregationException(long contactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700603 ContentValues values = new ContentValues(3);
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700604 values.put(AggregationExceptions.CONTACT_ID, ContentUris.parseId(mUri));
Jeff Sharkey14f61ab2009-08-05 21:02:37 -0700605 values.put(AggregationExceptions.RAW_CONTACT_ID, contactId);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700606 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700607 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700608 }
609
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700610 private void showOptionsActivity() {
611 final Intent intent = new Intent(this, ContactOptionsActivity.class);
612 intent.setData(mUri);
613 startActivity(intent);
614 }
615
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700616 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
617 AdapterView.AdapterContextMenuInfo info;
618 try {
619 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
620 } catch (ClassCastException e) {
621 Log.e(TAG, "bad menuInfo", e);
622 return null;
623 }
624
625 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
626 }
627
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800628 @Override
629 public boolean onKeyDown(int keyCode, KeyEvent event) {
630 switch (keyCode) {
631 case KeyEvent.KEYCODE_CALL: {
632 try {
633 ITelephony phone = ITelephony.Stub.asInterface(
634 ServiceManager.checkService("phone"));
635 if (phone != null && !phone.isIdle()) {
636 // Skip out and let the key be handled at a higher level
637 break;
638 }
639 } catch (RemoteException re) {
640 // Fall through and try to call the contact
641 }
642
Evan Millar7911ff52009-07-21 15:55:18 -0700643 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800644 if (index != -1) {
645 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700646 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
647 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800648 }
649 } else if (mNumPhoneNumbers != 0) {
650 // There isn't anything selected, call the default number
651 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
652 startActivity(intent);
653 }
654 return true;
655 }
656
657 case KeyEvent.KEYCODE_DEL: {
658 showDialog(DIALOG_CONFIRM_DELETE);
659 return true;
660 }
661 }
662
663 return super.onKeyDown(keyCode, event);
664 }
665
Evan Millar7911ff52009-07-21 15:55:18 -0700666 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800667 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
668 if (entry != null) {
669 Intent intent = entry.intent;
670 if (intent != null) {
671 try {
672 startActivity(intent);
673 } catch (ActivityNotFoundException e) {
674 Log.e(TAG, "No activity found for intent: " + intent);
675 signalError();
676 }
677 } else {
678 signalError();
679 }
680 } else {
681 signalError();
682 }
683 }
684
685 /**
686 * Signal an error to the user via a beep, or some other method.
687 */
688 private void signalError() {
689 //TODO: implement this when we have the sonification APIs
690 }
691
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800692 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700693 // 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 -0800694 StringBuilder buf = new StringBuilder("imto://");
695 buf.append(host);
696 buf.append('/');
697 buf.append(data);
698 return Uri.parse(buf.toString());
699 }
700
701 /**
702 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700703 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800704 * @param personCursor the URI for the contact being displayed
705 */
Evan Millar66388be2009-05-28 15:41:07 -0700706 private final void buildEntries(Cursor aggCursor) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800707 // Clear out the old entries
708 final int numSections = mSections.size();
709 for (int i = 0; i < numSections; i++) {
710 mSections.get(i).clear();
711 }
712
Evan Millar7911ff52009-07-21 15:55:18 -0700713 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700714
Evan Millar66388be2009-05-28 15:41:07 -0700715 // Build up method entries
716 if (mUri != null) {
Jeff Hamilton119b6ca2009-07-30 12:57:49 -0500717 aggCursor.moveToPosition(-1);
Evan Millar66388be2009-05-28 15:41:07 -0700718 while (aggCursor.moveToNext()) {
719 final String mimetype = aggCursor.getString(DATA_MIMETYPE_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700720
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800721 ViewEntry entry = new ViewEntry();
Evan Millar66388be2009-05-28 15:41:07 -0700722
723 final long id = aggCursor.getLong(DATA_ID_COLUMN);
724 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800725 entry.id = id;
726 entry.uri = uri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700727 entry.mimetype = mimetype;
Evan Millar7911ff52009-07-21 15:55:18 -0700728 // TODO: entry.contactId should be renamed to entry.rawContactId
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700729 entry.contactId = aggCursor.getLong(DATA_CONTACT_ID_COLUMN);
Evan Millar7911ff52009-07-21 15:55:18 -0700730 if (!mRawContactIds.contains(entry.contactId)) {
731 mRawContactIds.add(entry.contactId);
732 }
733
734 // This performs the tab filtering
Evan Millardb5d88c2009-08-28 09:31:57 -0700735 if (mSelectedRawContactId != null
736 && mSelectedRawContactId != entry.contactId
Evan Millar8a79cee2009-08-19 17:20:49 -0700737 && mSelectedRawContactId != ALL_CONTACTS_ID) {
Evan Millar7911ff52009-07-21 15:55:18 -0700738 continue;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700739 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800740
Evan Millar66388be2009-05-28 15:41:07 -0700741 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
Evan Millar5c22c3b2009-05-29 11:37:54 -0700742 || mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700743 || mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
Evan Millar66388be2009-05-28 15:41:07 -0700744 || mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
745 final int type = aggCursor.getInt(DATA_1_COLUMN);
Evan Millar45e0ed32009-06-01 16:44:38 -0700746 final String label = aggCursor.getString(DATA_3_COLUMN);
747 final String data = aggCursor.getString(DATA_2_COLUMN);
748 final boolean isSuperPrimary = "1".equals(
749 aggCursor.getString(DATA_IS_SUPER_PRIMARY_COLUMN));
Evan Millar66388be2009-05-28 15:41:07 -0700750
Evan Millar54a5c9f2009-06-23 17:41:09 -0700751 entry.type = type;
752
Evan Millar66388be2009-05-28 15:41:07 -0700753 // Don't crash if the data is bogus
754 if (TextUtils.isEmpty(data)) {
755 Log.w(TAG, "empty data for contact method " + id);
756 continue;
757 }
758
759 // Build phone entries
760 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
761 mNumPhoneNumbers++;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700762
Evan Millar66388be2009-05-28 15:41:07 -0700763 final CharSequence displayLabel = ContactsUtils.getDisplayLabel(
764 this, mimetype, type, label);
765 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700766 entry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar296758b2009-08-07 15:40:08 -0700767 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
768 Uri.fromParts("tel", data, null));
Evan Millar15e514d2009-08-04 10:14:57 -0700769 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
Evan Millar66388be2009-05-28 15:41:07 -0700770 Uri.fromParts("sms", data, null));
Evan Millar15e514d2009-08-04 10:14:57 -0700771 entry.isPrimary = isSuperPrimary;
Evan Millar66388be2009-05-28 15:41:07 -0700772 entry.actionIcon = android.R.drawable.sym_action_call;
773 mPhoneEntries.add(entry);
774
Evan Millar5c22c3b2009-05-29 11:37:54 -0700775 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700776 || mShowSmsLinksForAllPhones) {
777 // Add an SMS entry
Evan Millar15e514d2009-08-04 10:14:57 -0700778 entry.secondaryActionIcon = R.drawable.sym_action_sms;
Evan Millar66388be2009-05-28 15:41:07 -0700779 }
780 // Build email entries
781 } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800782 entry.label = buildActionString(R.string.actionEmail,
Evan Millar66388be2009-05-28 15:41:07 -0700783 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800784 entry.data = data;
785 entry.intent = new Intent(Intent.ACTION_SENDTO,
786 Uri.fromParts("mailto", data, null));
787 entry.actionIcon = android.R.drawable.sym_action_email;
Evan Millar15e514d2009-08-04 10:14:57 -0700788 entry.isPrimary = isSuperPrimary;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800789 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700790 // Build postal entries
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700791 } else if (mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800792 entry.label = buildActionString(R.string.actionMap,
Evan Millar66388be2009-05-28 15:41:07 -0700793 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800794 entry.data = data;
795 entry.maxLines = 4;
796 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
797 entry.actionIcon = R.drawable.sym_action_map;
798 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700799 // Build im entries
800 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
801 String[] protocolStrings = getResources().getStringArray(
802 android.R.array.imProtocols);
Jeff Sharkeye731d422009-07-28 21:12:43 -0700803 Object protocolObj = aggCursor.getString(DATA_5_COLUMN);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700804 String host = null;
Jeff Sharkeye731d422009-07-28 21:12:43 -0700805 // TODO: fix by moving to contactssource-based rendering rules
806// Object protocolObj = ContactsUtils.decodeImProtocol(
807// aggCursor.getString(DATA_5_COLUMN));
808// if (protocolObj instanceof Number) {
809// int protocol = ((Number) protocolObj).intValue();
810// entry.label = buildActionString(R.string.actionChat,
811// protocolStrings[protocol], false);
812// host = ContactsUtils.lookupProviderNameFromId(
813// protocol).toLowerCase();
814// if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
815// || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
816// entry.maxLabelLines = 2;
817// }
Neel Parekh064fa802009-08-12 12:57:12 -0700818 if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800819 String providerName = (String) protocolObj;
820 entry.label = buildActionString(R.string.actionChat,
821 providerName, false);
822 host = providerName.toLowerCase();
Neel Parekh064fa802009-08-12 12:57:12 -0700823 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800824
825 // Only add the intent if there is a valid host
826 if (!TextUtils.isEmpty(host)) {
827 entry.intent = new Intent(Intent.ACTION_SENDTO,
828 constructImToUrl(host, data));
829 }
830 entry.data = data;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700831 //TODO(emillar) Add in presence info
Evan Millar66388be2009-05-28 15:41:07 -0700832 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800833 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700834 aggCursor.getInt(METHODS_STATUS_COLUMN));
Evan Millar54a5c9f2009-06-23 17:41:09 -0700835 entry.status = ...
Evan Millar66388be2009-05-28 15:41:07 -0700836 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800837 entry.actionIcon = android.R.drawable.sym_action_chat;
838 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700839 }
Evan Millar66388be2009-05-28 15:41:07 -0700840 // Build organization entries
841 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
Evan Millar66388be2009-05-28 15:41:07 -0700842 final String company = aggCursor.getString(DATA_3_COLUMN);
843 final String title = aggCursor.getString(DATA_4_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700844
Evan Millar66388be2009-05-28 15:41:07 -0700845 // Don't crash if the data is bogus
846 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
847 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800848 continue;
849 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700850
Evan Millar66388be2009-05-28 15:41:07 -0700851 entry.data = title;
852 entry.actionIcon = R.drawable.sym_action_organization;
853 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800854
Evan Millar66388be2009-05-28 15:41:07 -0700855 mOrganizationEntries.add(entry);
856 // Build note entries
857 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
858 entry.label = getString(R.string.label_notes);
859 entry.data = aggCursor.getString(DATA_1_COLUMN);
860 entry.id = 0;
861 entry.uri = null;
862 entry.intent = null;
863 entry.maxLines = 10;
864 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800865
Evan Millar66388be2009-05-28 15:41:07 -0700866 if (TextUtils.isEmpty(entry.data)) {
867 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700868 continue;
869 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700870
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800871 mOtherEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700872 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700873
Evan Millar45e0ed32009-06-01 16:44:38 -0700874
Evan Millar66388be2009-05-28 15:41:07 -0700875 // TODO(emillar) Add group entries
876// // Build the group entries
877// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
878// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
879// null, null, Groups.DEFAULT_SORT_ORDER);
880// if (groupCursor != null) {
881// try {
882// StringBuilder sb = new StringBuilder();
883//
884// while (groupCursor.moveToNext()) {
885// String systemId = groupCursor.getString(
886// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
887//
888// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
889// continue;
890// }
891//
892// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
893// if (!TextUtils.isEmpty(name)) {
894// if (sb.length() == 0) {
895// sb.append(name);
896// } else {
897// sb.append(getString(R.string.group_list, name));
898// }
899// }
900// }
901//
902// if (sb.length() > 0) {
903// ViewEntry entry = new ViewEntry();
904// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
905// entry.label = getString(R.string.label_groups);
906// entry.data = sb.toString();
907// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
908//
909// // TODO: Add an icon for the groups item.
910//
911// mGroupEntries.add(entry);
912// }
913// } finally {
914// groupCursor.close();
915// }
916// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700917
Evan Millar66388be2009-05-28 15:41:07 -0700918 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800919 }
920 }
921
922 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700923 // If there is no type just display an empty string
924 if (type == null) {
925 type = "";
926 }
927
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800928 if (lowerCase) {
929 return getString(actionResId, type.toString().toLowerCase());
930 } else {
931 return getString(actionResId, type.toString());
932 }
933 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700934
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800935 /**
936 * A basic structure with the data for a contact entry in the list.
937 */
Evan Millar54a5c9f2009-06-23 17:41:09 -0700938 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800939 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -0700940 public boolean isPrimary = false;
941 public int presenceIcon = -1;
942 public int secondaryActionIcon = -1;
943 public Intent intent;
944 public Intent secondaryIntent = null;
945 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800946 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700947 public ArrayList<Long> ids = new ArrayList<Long>();
948 public int collapseCount = 0;
949
950 public boolean collapseWith(ViewEntry entry) {
951 // assert equal collapse keys
952 if (!getCollapseKey().equals(entry.getCollapseKey())) {
953 return false;
954 }
955
956 // Choose the label associated with the highest type precedence.
957 if (TypePrecedence.getTypePrecedence(mimetype, type)
958 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
959 type = entry.type;
960 label = entry.label;
961 }
962
963 // Choose the max of the maxLines and maxLabelLines values.
964 maxLines = Math.max(maxLines, entry.maxLines);
965 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
966
967 // Choose the presence with the highest precedence.
968 if (Presence.getPresencePrecedence(status)
969 < Presence.getPresencePrecedence(entry.status)) {
970 status = entry.status;
971 }
972
973 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -0700974 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700975
976 // uri, and contactdId, shouldn't make a difference. Just keep the original.
977
978 // Keep track of all the ids that have been collapsed with this one.
979 ids.add(entry.id);
980 collapseCount++;
981 return true;
982 }
983
984 public String getCollapseKey() {
985 StringBuilder hashSb = new StringBuilder();
986 hashSb.append(data);
987 hashSb.append(mimetype);
988 hashSb.append((intent != null && intent.getAction() != null)
989 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -0700990 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
991 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -0700992 hashSb.append(actionIcon);
993 return hashSb.toString();
994 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800995 }
996
Evan Millar15e514d2009-08-04 10:14:57 -0700997 /** Cache of the children views of a row */
998 static class ViewCache {
999 public TextView label;
1000 public TextView data;
1001 public ImageView actionIcon;
1002 public ImageView presenceIcon;
1003 public ImageView primaryIcon;
1004 public ImageView secondaryActionButton;
1005 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001006
Evan Millar15e514d2009-08-04 10:14:57 -07001007 // Need to keep track of this too
1008 ViewEntry entry;
1009 }
1010
1011 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
1012 implements View.OnClickListener {
1013
Evan Millar5c22c3b2009-05-29 11:37:54 -07001014
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001015 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1016 super(context, sections, SHOW_SEPARATORS);
1017 }
1018
Evan Millar15e514d2009-08-04 10:14:57 -07001019 public void onClick(View v) {
1020 Intent intent = (Intent) v.getTag();
1021 startActivity(intent);
1022 }
1023
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001024 @Override
1025 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001026 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001027 View v;
1028
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001029 ViewCache views;
1030
1031 // Check to see if we can reuse convertView
1032 if (convertView != null) {
1033 v = convertView;
1034 views = (ViewCache) v.getTag();
1035 } else {
1036 // Create a new view if needed
1037 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1038
1039 // Cache the children
1040 views = new ViewCache();
1041 views.label = (TextView) v.findViewById(android.R.id.text1);
1042 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001043 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1044 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1045 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1046 views.secondaryActionButton = (ImageView) v.findViewById(
1047 R.id.secondary_action_button);
1048 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001049 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001050 v.setTag(views);
1051 }
1052
1053 // Update the entry in the view cache
1054 views.entry = entry;
1055
1056 // Bind the data to the view
1057 bindView(v, entry);
1058 return v;
1059 }
1060
1061 @Override
1062 protected View newView(int position, ViewGroup parent) {
1063 // getView() handles this
1064 throw new UnsupportedOperationException();
1065 }
1066
1067 @Override
1068 protected void bindView(View view, ViewEntry entry) {
1069 final Resources resources = mContext.getResources();
1070 ViewCache views = (ViewCache) view.getTag();
1071
1072 // Set the label
1073 TextView label = views.label;
1074 setMaxLines(label, entry.maxLabelLines);
1075 label.setText(entry.label);
1076
1077 // Set the data
1078 TextView data = views.data;
1079 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001080 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1081 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1082 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1083 } else {
1084 data.setText(entry.data);
1085 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001086 setMaxLines(data, entry.maxLines);
1087 }
1088
Evan Millar15e514d2009-08-04 10:14:57 -07001089 // Set the primary icon
1090 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1091
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001092 // Set the action icon
1093 ImageView action = views.actionIcon;
1094 if (entry.actionIcon != -1) {
1095 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1096 action.setVisibility(View.VISIBLE);
1097 } else {
1098 // Things should still line up as if there was an icon, so make it invisible
1099 action.setVisibility(View.INVISIBLE);
1100 }
1101
1102 // Set the presence icon
1103 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001104 if (entry.presenceIcon != -1) {
1105 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001106 } else if (entry.status != -1) {
1107 presenceIcon = resources.getDrawable(
1108 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001109 }
Evan Millar15e514d2009-08-04 10:14:57 -07001110 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001111 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001112 presenceIconView.setImageDrawable(presenceIcon);
1113 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001114 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001115 presenceIconView.setVisibility(View.GONE);
1116 }
1117
1118 // Set the secondary action button
1119 ImageView secondaryActionView = views.secondaryActionButton;
1120 Drawable secondaryActionIcon = null;
1121 if (entry.secondaryActionIcon != -1) {
1122 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1123 }
1124 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1125 secondaryActionView.setImageDrawable(secondaryActionIcon);
1126 secondaryActionView.setTag(entry.secondaryIntent);
1127 secondaryActionView.setVisibility(View.VISIBLE);
1128 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1129 } else {
1130 secondaryActionView.setVisibility(View.GONE);
1131 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001132 }
1133 }
1134
1135 private void setMaxLines(TextView textView, int maxLines) {
1136 if (maxLines == 1) {
1137 textView.setSingleLine(true);
1138 textView.setEllipsize(TextUtils.TruncateAt.END);
1139 } else {
1140 textView.setSingleLine(false);
1141 textView.setMaxLines(maxLines);
1142 textView.setEllipsize(null);
1143 }
1144 }
1145 }
1146}