blob: 87e6718d8df6cba2cc9d5880aff418673dadb368 [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
101 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800102
103 private static final int DIALOG_CONFIRM_DELETE = 1;
104
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700105 private static final int REQUEST_JOIN_AGGREGATE = 1;
106
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800107 public static final int MENU_ITEM_DELETE = 1;
108 public static final int MENU_ITEM_MAKE_DEFAULT = 2;
109 public static final int MENU_ITEM_SHOW_BARCODE = 3;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700110 public static final int MENU_ITEM_SPLIT_AGGREGATE = 4;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700111 public static final int MENU_ITEM_JOIN_AGGREGATE = 5;
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700112 public static final int MENU_ITEM_OPTIONS = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800113
114 private Uri mUri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700115 private Uri mAggDataUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800116 private ContentResolver mResolver;
117 private ViewAdapter mAdapter;
118 private int mNumPhoneNumbers = 0;
119
Evan Millar7911ff52009-07-21 15:55:18 -0700120 private static final long ALL_CONTACTS_ID = -1;
121 private long mRawContactId = ALL_CONTACTS_ID;
122
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700123 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700124 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700125 */
Evan Millar7911ff52009-07-21 15:55:18 -0700126 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700127
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800128 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
129 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
130 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
131 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
132 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
133 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700134 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800135 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
136 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
137
138 private Cursor mCursor;
139 private boolean mObserverRegistered;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700140
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800141 private ContentObserver mObserver = new ContentObserver(new Handler()) {
142 @Override
143 public boolean deliverSelfNotifications() {
144 return true;
145 }
146
147 @Override
148 public void onChange(boolean selfChange) {
149 if (mCursor != null && !mCursor.isClosed()){
150 dataChanged();
151 }
152 }
153 };
154
155 public void onClick(DialogInterface dialog, int which) {
156 if (mCursor != null) {
157 if (mObserverRegistered) {
158 mCursor.unregisterContentObserver(mObserver);
159 mObserverRegistered = false;
160 }
161 mCursor.close();
162 mCursor = null;
163 }
164 getContentResolver().delete(mUri, null, null);
165 finish();
166 }
167
Evan Millar7911ff52009-07-21 15:55:18 -0700168 private FrameLayout mTabContentLayout;
169 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800170 private boolean mShowSmsLinksForAllPhones;
171
172 @Override
173 protected void onCreate(Bundle icicle) {
174 super.onCreate(icicle);
175
Evan Millar7911ff52009-07-21 15:55:18 -0700176 mListView = new ListView(this);
177 mListView.setOnCreateContextMenuListener(this);
178 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
179 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800180
Evan Millar7911ff52009-07-21 15:55:18 -0700181 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
182 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800183
Evan Millar45e0ed32009-06-01 16:44:38 -0700184 mUri = getIntent().getData();
185 mAggDataUri = Uri.withAppendedPath(mUri, "data");
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800186 mResolver = getContentResolver();
187
188 // Build the list of sections. The order they're added to mSections dictates the
189 // order they are displayed in the list.
190 mSections.add(mPhoneEntries);
191 mSections.add(mSmsEntries);
192 mSections.add(mEmailEntries);
193 mSections.add(mImEntries);
194 mSections.add(mPostalEntries);
195 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700196 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800197 mSections.add(mOtherEntries);
198
199 //TODO Read this value from a preference
200 mShowSmsLinksForAllPhones = true;
201
Evan Millar45e0ed32009-06-01 16:44:38 -0700202 mCursor = mResolver.query(mAggDataUri,
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700203 CONTACT_PROJECTION, null, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800204 }
205
206 @Override
207 protected void onResume() {
208 super.onResume();
209 mObserverRegistered = true;
210 mCursor.registerContentObserver(mObserver);
211 dataChanged();
212 }
213
214 @Override
215 protected void onPause() {
216 super.onPause();
217 if (mCursor != null) {
218 if (mObserverRegistered) {
219 mObserverRegistered = false;
220 mCursor.unregisterContentObserver(mObserver);
221 }
222 mCursor.deactivate();
223 }
224 }
225
226 @Override
227 protected void onDestroy() {
228 super.onDestroy();
229
230 if (mCursor != null) {
231 if (mObserverRegistered) {
232 mCursor.unregisterContentObserver(mObserver);
233 mObserverRegistered = false;
234 }
235 mCursor.close();
236 }
237 }
238
239 @Override
240 protected Dialog onCreateDialog(int id) {
241 switch (id) {
242 case DIALOG_CONFIRM_DELETE:
243 return new AlertDialog.Builder(this)
244 .setTitle(R.string.deleteConfirmation_title)
245 .setIcon(android.R.drawable.ic_dialog_alert)
246 .setMessage(R.string.deleteConfirmation)
247 .setNegativeButton(android.R.string.cancel, null)
248 .setPositiveButton(android.R.string.ok, this)
249 .setCancelable(false)
250 .create();
251 }
252 return null;
253 }
254
Evan Millar7911ff52009-07-21 15:55:18 -0700255 @Override
Evan Millar5f4af702009-08-11 11:12:00 -0700256 protected void bindTabs(ArrayList<Entity> entities) {
257 if (entities.size() > 1) {
Evan Millar7911ff52009-07-21 15:55:18 -0700258 addAllTab();
259 }
Evan Millar5f4af702009-08-11 11:12:00 -0700260 super.bindTabs(entities);
Evan Millar7911ff52009-07-21 15:55:18 -0700261 }
262
263 private void addAllTab() {
264 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator, mTabWidget, false);
265 addTab(ALL_CONTACTS_ID, allTabIndicator);
266 }
267
268 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
269 long rawContactId = getTabRawContactId(tabIndex);
270 mRawContactId = rawContactId;
271 dataChanged();
272 }
273
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800274 private void dataChanged() {
275 mCursor.requery();
276 if (mCursor.moveToFirst()) {
Evan Millar2c1cc832009-07-13 11:08:06 -0700277
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800278 // Build up the contact entries
279 buildEntries(mCursor);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700280
281 // Collapse similar data items in select sections.
282 Collapser.collapseList(mPhoneEntries);
283 Collapser.collapseList(mSmsEntries);
284 Collapser.collapseList(mEmailEntries);
285 Collapser.collapseList(mPostalEntries);
286
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800287 if (mAdapter == null) {
288 mAdapter = new ViewAdapter(this, mSections);
Evan Millar7911ff52009-07-21 15:55:18 -0700289 mListView.setAdapter(mAdapter);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800290 } else {
291 mAdapter.setSections(mSections, SHOW_SEPARATORS);
292 }
293 } else {
294 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
295 Log.e(TAG, "invalid contact uri: " + mUri);
296 finish();
297 }
298 }
299
300 @Override
301 public boolean onCreateOptionsMenu(Menu menu) {
302 menu.add(0, 0, 0, R.string.menu_editContact)
303 .setIcon(android.R.drawable.ic_menu_edit)
304 .setIntent(new Intent(Intent.ACTION_EDIT, mUri))
305 .setAlphabeticShortcut('e');
306 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
307 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700308 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
309 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700310 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
311 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700312 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
313 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800314 return true;
315 }
316
317 @Override
318 public boolean onPrepareOptionsMenu(Menu menu) {
319 super.onPrepareOptionsMenu(menu);
320 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
321 // could be installed or uninstalled at any time.
322 if (isBarcodeScannerInstalled()) {
323 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
324 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
325 .setIcon(R.drawable.ic_menu_show_barcode);
326 }
327 } else {
328 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
329 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700330
Evan Millar7911ff52009-07-21 15:55:18 -0700331 boolean isAggregate = mRawContactIds.size() > 1;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700332 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800333 return true;
334 }
335
336 private boolean isBarcodeScannerInstalled() {
337 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
338 ResolveInfo ri = getPackageManager().resolveActivity(intent,
339 PackageManager.MATCH_DEFAULT_ONLY);
340 return ri != null;
341 }
342
343 @Override
344 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
345 AdapterView.AdapterContextMenuInfo info;
346 try {
347 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
348 } catch (ClassCastException e) {
349 Log.e(TAG, "bad menuInfo", e);
350 return;
351 }
352
353 // This can be null sometimes, don't crash...
354 if (info == null) {
355 Log.e(TAG, "bad menuInfo");
356 return;
357 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700358
Evan Millar45e0ed32009-06-01 16:44:38 -0700359 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
360 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
361 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700362 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
363 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700364 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800365 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700366 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
367 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700368 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700369 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800370 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700371 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700372 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
373 }
374 // TODO(emillar): add back with group support.
375 /* else if (entry.mimetype.equals()) {
376 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
377 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800378 }
379
380 @Override
381 public boolean onOptionsItemSelected(MenuItem item) {
382 switch (item.getItemId()) {
383 case MENU_ITEM_DELETE: {
384 // Get confirmation
385 showDialog(DIALOG_CONFIRM_DELETE);
386 return true;
387 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700388
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700389 case MENU_ITEM_SPLIT_AGGREGATE: {
390 showSplitAggregateDialog();
391 return true;
392 }
393
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700394 case MENU_ITEM_JOIN_AGGREGATE: {
395 showJoinAggregateActivity();
396 return true;
397 }
398
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700399 case MENU_ITEM_OPTIONS: {
400 showOptionsActivity();
401 return true;
402 }
403
Evan Millar66388be2009-05-28 15:41:07 -0700404 // TODO(emillar) Bring this back.
405 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800406 if (mCursor.moveToFirst()) {
407 Intent intent = new Intent(SHOW_BARCODE_INTENT);
408 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
409 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700410 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800411 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700412 // Correctly handle when section headers are hidden
413 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700414
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800415 bundle.putString(Contacts.Intents.Insert.NAME, name);
416 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700417 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800418 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700419 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800420 bundle.putString(PHONE_KEYS[x], entry.data);
421 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700422 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800423 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700424 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800425 bundle.putString(EMAIL_KEYS[x], entry.data);
426 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700427 if (mPostalEntries.size() >= 1 + sepAdjust) {
428 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800429 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
430 }
431 intent.putExtra("ENCODE_DATA", bundle);
432 try {
433 startActivity(intent);
434 } catch (ActivityNotFoundException e) {
435 // The check in onPrepareOptionsMenu() should make this impossible, but
436 // for safety I'm catching the exception rather than crashing. Ideally
437 // I'd call Menu.removeItem() here too, but I don't see a way to get
438 // the options menu.
439 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
440 "was not installed.");
441 }
442 return true;
443 }
444 }
Evan Millar66388be2009-05-28 15:41:07 -0700445 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800446 }
447 return super.onOptionsItemSelected(item);
448 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700449
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800450 @Override
451 public boolean onContextItemSelected(MenuItem item) {
452 switch (item.getItemId()) {
453 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700454 if (makeItemDefault(item)) {
455 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800456 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700457 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800458 }
459 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700460
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800461 return super.onContextItemSelected(item);
462 }
463
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700464 private boolean makeItemDefault(MenuItem item) {
465 ViewEntry entry = getViewEntryForMenuItem(item);
466 if (entry == null) {
467 return false;
468 }
469
470 // Update the primary values in the data record.
471 ContentValues values = new ContentValues(2);
472 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700473
Evan Millar54a5c9f2009-06-23 17:41:09 -0700474 if (entry.ids.size() > 0) {
475 for (int i = 0; i < entry.ids.size(); i++) {
476 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
477 entry.ids.get(i)),
478 values, null, null);
479 }
480 }
481
482 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700483 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
484 values, null, null);
485 dataChanged();
486 return true;
487 }
488
489 /**
490 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
491 * The user picks a contact to be split into its own aggregate or clicks Cancel.
492 */
493 private void showSplitAggregateDialog() {
494
495 // Wrap this dialog in a specific theme so that list items have correct text color.
496 final ContextThemeWrapper dialogContext =
497 new ContextThemeWrapper(this, android.R.style.Theme_Light);
498 AlertDialog.Builder builder =
499 new AlertDialog.Builder(dialogContext);
500 builder.setTitle(getString(R.string.splitAggregate_title));
501
502 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
503 builder.setView(view);
504
505 builder.setInverseBackgroundForced(true);
506 builder.setCancelable(true);
507 builder.setNegativeButton(android.R.string.cancel,
508 new OnClickListener() {
509 public void onClick(DialogInterface dialog, int which) {
510 dialog.dismiss();
511 }
512 });
513 final AlertDialog dialog = builder.create();
514
515 view.setOnContactSelectedListener(new OnContactSelectedListener() {
516 public void onContactSelected(long contactId) {
517 dialog.dismiss();
518 splitContact(contactId);
519 }
520 });
521
522 dialog.show();
523 }
524
525 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700526 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700527 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700528 public void showJoinAggregateActivity() {
529 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
530 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
531 startActivityForResult(intent, REQUEST_JOIN_AGGREGATE);
532 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700533
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700534 @Override
535 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
536 if (requestCode == REQUEST_JOIN_AGGREGATE && resultCode == RESULT_OK && intent != null) {
537 final long aggregateId = ContentUris.parseId(intent.getData());
538 joinAggregate(aggregateId);
539 }
540 }
541
542 private void splitContact(long contactId) {
543 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_OUT);
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700544 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700545 mAdapter.notifyDataSetChanged();
546 }
547
548 private void joinAggregate(final long aggregateId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700549 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700550 RawContacts.CONTACT_ID + "=" + aggregateId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700551
552 try {
553 while(c.moveToNext()) {
554 long contactId = c.getLong(0);
555 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
556 }
557 } finally {
558 c.close();
559 }
560
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700561 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700562 mAdapter.notifyDataSetChanged();
563 }
564
565 /**
566 * Given a contact ID sets an aggregation exception to either join the contact with the
567 * current aggregate or split off.
568 */
569 protected void setAggregationException(long contactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700570 ContentValues values = new ContentValues(3);
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700571 values.put(AggregationExceptions.CONTACT_ID, ContentUris.parseId(mUri));
Jeff Sharkey14f61ab2009-08-05 21:02:37 -0700572 values.put(AggregationExceptions.RAW_CONTACT_ID, contactId);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700573 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700574 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700575 }
576
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700577 private void showOptionsActivity() {
578 final Intent intent = new Intent(this, ContactOptionsActivity.class);
579 intent.setData(mUri);
580 startActivity(intent);
581 }
582
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700583 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
584 AdapterView.AdapterContextMenuInfo info;
585 try {
586 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
587 } catch (ClassCastException e) {
588 Log.e(TAG, "bad menuInfo", e);
589 return null;
590 }
591
592 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
593 }
594
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800595 @Override
596 public boolean onKeyDown(int keyCode, KeyEvent event) {
597 switch (keyCode) {
598 case KeyEvent.KEYCODE_CALL: {
599 try {
600 ITelephony phone = ITelephony.Stub.asInterface(
601 ServiceManager.checkService("phone"));
602 if (phone != null && !phone.isIdle()) {
603 // Skip out and let the key be handled at a higher level
604 break;
605 }
606 } catch (RemoteException re) {
607 // Fall through and try to call the contact
608 }
609
Evan Millar7911ff52009-07-21 15:55:18 -0700610 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800611 if (index != -1) {
612 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700613 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
614 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800615 }
616 } else if (mNumPhoneNumbers != 0) {
617 // There isn't anything selected, call the default number
618 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
619 startActivity(intent);
620 }
621 return true;
622 }
623
624 case KeyEvent.KEYCODE_DEL: {
625 showDialog(DIALOG_CONFIRM_DELETE);
626 return true;
627 }
628 }
629
630 return super.onKeyDown(keyCode, event);
631 }
632
Evan Millar7911ff52009-07-21 15:55:18 -0700633 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800634 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
635 if (entry != null) {
636 Intent intent = entry.intent;
637 if (intent != null) {
638 try {
639 startActivity(intent);
640 } catch (ActivityNotFoundException e) {
641 Log.e(TAG, "No activity found for intent: " + intent);
642 signalError();
643 }
644 } else {
645 signalError();
646 }
647 } else {
648 signalError();
649 }
650 }
651
652 /**
653 * Signal an error to the user via a beep, or some other method.
654 */
655 private void signalError() {
656 //TODO: implement this when we have the sonification APIs
657 }
658
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800659 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700660 // 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 -0800661 StringBuilder buf = new StringBuilder("imto://");
662 buf.append(host);
663 buf.append('/');
664 buf.append(data);
665 return Uri.parse(buf.toString());
666 }
667
668 /**
669 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700670 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800671 * @param personCursor the URI for the contact being displayed
672 */
Evan Millar66388be2009-05-28 15:41:07 -0700673 private final void buildEntries(Cursor aggCursor) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800674 // Clear out the old entries
675 final int numSections = mSections.size();
676 for (int i = 0; i < numSections; i++) {
677 mSections.get(i).clear();
678 }
679
Evan Millar7911ff52009-07-21 15:55:18 -0700680 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700681
Evan Millar66388be2009-05-28 15:41:07 -0700682 // Build up method entries
683 if (mUri != null) {
Jeff Hamilton119b6ca2009-07-30 12:57:49 -0500684 aggCursor.moveToPosition(-1);
Evan Millar66388be2009-05-28 15:41:07 -0700685 while (aggCursor.moveToNext()) {
686 final String mimetype = aggCursor.getString(DATA_MIMETYPE_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700687
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800688 ViewEntry entry = new ViewEntry();
Evan Millar66388be2009-05-28 15:41:07 -0700689
690 final long id = aggCursor.getLong(DATA_ID_COLUMN);
691 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800692 entry.id = id;
693 entry.uri = uri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700694 entry.mimetype = mimetype;
Evan Millar7911ff52009-07-21 15:55:18 -0700695 // TODO: entry.contactId should be renamed to entry.rawContactId
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700696 entry.contactId = aggCursor.getLong(DATA_CONTACT_ID_COLUMN);
Evan Millar7911ff52009-07-21 15:55:18 -0700697 if (!mRawContactIds.contains(entry.contactId)) {
698 mRawContactIds.add(entry.contactId);
699 }
700
701 // This performs the tab filtering
702 if (mRawContactId != entry.contactId && mRawContactId != ALL_CONTACTS_ID) {
703 continue;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700704 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800705
Evan Millar66388be2009-05-28 15:41:07 -0700706 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
Evan Millar5c22c3b2009-05-29 11:37:54 -0700707 || mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700708 || mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
Evan Millar66388be2009-05-28 15:41:07 -0700709 || mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
710 final int type = aggCursor.getInt(DATA_1_COLUMN);
Evan Millar45e0ed32009-06-01 16:44:38 -0700711 final String label = aggCursor.getString(DATA_3_COLUMN);
712 final String data = aggCursor.getString(DATA_2_COLUMN);
713 final boolean isSuperPrimary = "1".equals(
714 aggCursor.getString(DATA_IS_SUPER_PRIMARY_COLUMN));
Evan Millar66388be2009-05-28 15:41:07 -0700715
Evan Millar54a5c9f2009-06-23 17:41:09 -0700716 entry.type = type;
717
Evan Millar66388be2009-05-28 15:41:07 -0700718 // Don't crash if the data is bogus
719 if (TextUtils.isEmpty(data)) {
720 Log.w(TAG, "empty data for contact method " + id);
721 continue;
722 }
723
724 // Build phone entries
725 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
726 mNumPhoneNumbers++;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700727
Evan Millar66388be2009-05-28 15:41:07 -0700728 final CharSequence displayLabel = ContactsUtils.getDisplayLabel(
729 this, mimetype, type, label);
730 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700731 entry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar296758b2009-08-07 15:40:08 -0700732 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
733 Uri.fromParts("tel", data, null));
Evan Millar15e514d2009-08-04 10:14:57 -0700734 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
Evan Millar66388be2009-05-28 15:41:07 -0700735 Uri.fromParts("sms", data, null));
Evan Millar15e514d2009-08-04 10:14:57 -0700736 entry.isPrimary = isSuperPrimary;
Evan Millar66388be2009-05-28 15:41:07 -0700737 entry.actionIcon = android.R.drawable.sym_action_call;
738 mPhoneEntries.add(entry);
739
Evan Millar5c22c3b2009-05-29 11:37:54 -0700740 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700741 || mShowSmsLinksForAllPhones) {
742 // Add an SMS entry
Evan Millar15e514d2009-08-04 10:14:57 -0700743 entry.secondaryActionIcon = R.drawable.sym_action_sms;
Evan Millar66388be2009-05-28 15:41:07 -0700744 }
745 // Build email entries
746 } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800747 entry.label = buildActionString(R.string.actionEmail,
Evan Millar66388be2009-05-28 15:41:07 -0700748 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800749 entry.data = data;
750 entry.intent = new Intent(Intent.ACTION_SENDTO,
751 Uri.fromParts("mailto", data, null));
752 entry.actionIcon = android.R.drawable.sym_action_email;
Evan Millar15e514d2009-08-04 10:14:57 -0700753 entry.isPrimary = isSuperPrimary;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800754 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700755 // Build postal entries
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700756 } else if (mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800757 entry.label = buildActionString(R.string.actionMap,
Evan Millar66388be2009-05-28 15:41:07 -0700758 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800759 entry.data = data;
760 entry.maxLines = 4;
761 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
762 entry.actionIcon = R.drawable.sym_action_map;
763 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700764 // Build im entries
765 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
766 String[] protocolStrings = getResources().getStringArray(
767 android.R.array.imProtocols);
Jeff Sharkeye731d422009-07-28 21:12:43 -0700768 Object protocolObj = aggCursor.getString(DATA_5_COLUMN);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700769 String host = null;
Jeff Sharkeye731d422009-07-28 21:12:43 -0700770 // TODO: fix by moving to contactssource-based rendering rules
771// Object protocolObj = ContactsUtils.decodeImProtocol(
772// aggCursor.getString(DATA_5_COLUMN));
773// if (protocolObj instanceof Number) {
774// int protocol = ((Number) protocolObj).intValue();
775// entry.label = buildActionString(R.string.actionChat,
776// protocolStrings[protocol], false);
777// host = ContactsUtils.lookupProviderNameFromId(
778// protocol).toLowerCase();
779// if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
780// || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
781// entry.maxLabelLines = 2;
782// }
Neel Parekh064fa802009-08-12 12:57:12 -0700783 if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800784 String providerName = (String) protocolObj;
785 entry.label = buildActionString(R.string.actionChat,
786 providerName, false);
787 host = providerName.toLowerCase();
Neel Parekh064fa802009-08-12 12:57:12 -0700788 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800789
790 // Only add the intent if there is a valid host
791 if (!TextUtils.isEmpty(host)) {
792 entry.intent = new Intent(Intent.ACTION_SENDTO,
793 constructImToUrl(host, data));
794 }
795 entry.data = data;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700796 //TODO(emillar) Add in presence info
Evan Millar66388be2009-05-28 15:41:07 -0700797 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800798 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700799 aggCursor.getInt(METHODS_STATUS_COLUMN));
Evan Millar54a5c9f2009-06-23 17:41:09 -0700800 entry.status = ...
Evan Millar66388be2009-05-28 15:41:07 -0700801 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800802 entry.actionIcon = android.R.drawable.sym_action_chat;
803 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700804 }
Evan Millar66388be2009-05-28 15:41:07 -0700805 // Build organization entries
806 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
Evan Millar66388be2009-05-28 15:41:07 -0700807 final String company = aggCursor.getString(DATA_3_COLUMN);
808 final String title = aggCursor.getString(DATA_4_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700809
Evan Millar66388be2009-05-28 15:41:07 -0700810 // Don't crash if the data is bogus
811 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
812 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800813 continue;
814 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700815
Evan Millar66388be2009-05-28 15:41:07 -0700816 entry.data = title;
817 entry.actionIcon = R.drawable.sym_action_organization;
818 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800819
Evan Millar66388be2009-05-28 15:41:07 -0700820 mOrganizationEntries.add(entry);
821 // Build note entries
822 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
823 entry.label = getString(R.string.label_notes);
824 entry.data = aggCursor.getString(DATA_1_COLUMN);
825 entry.id = 0;
826 entry.uri = null;
827 entry.intent = null;
828 entry.maxLines = 10;
829 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800830
Evan Millar66388be2009-05-28 15:41:07 -0700831 if (TextUtils.isEmpty(entry.data)) {
832 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700833 continue;
834 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700835
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800836 mOtherEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700837 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700838
Evan Millar45e0ed32009-06-01 16:44:38 -0700839
Evan Millar66388be2009-05-28 15:41:07 -0700840 // TODO(emillar) Add group entries
841// // Build the group entries
842// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
843// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
844// null, null, Groups.DEFAULT_SORT_ORDER);
845// if (groupCursor != null) {
846// try {
847// StringBuilder sb = new StringBuilder();
848//
849// while (groupCursor.moveToNext()) {
850// String systemId = groupCursor.getString(
851// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
852//
853// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
854// continue;
855// }
856//
857// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
858// if (!TextUtils.isEmpty(name)) {
859// if (sb.length() == 0) {
860// sb.append(name);
861// } else {
862// sb.append(getString(R.string.group_list, name));
863// }
864// }
865// }
866//
867// if (sb.length() > 0) {
868// ViewEntry entry = new ViewEntry();
869// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
870// entry.label = getString(R.string.label_groups);
871// entry.data = sb.toString();
872// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
873//
874// // TODO: Add an icon for the groups item.
875//
876// mGroupEntries.add(entry);
877// }
878// } finally {
879// groupCursor.close();
880// }
881// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700882
Evan Millar66388be2009-05-28 15:41:07 -0700883 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800884 }
885 }
886
887 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700888 // If there is no type just display an empty string
889 if (type == null) {
890 type = "";
891 }
892
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800893 if (lowerCase) {
894 return getString(actionResId, type.toString().toLowerCase());
895 } else {
896 return getString(actionResId, type.toString());
897 }
898 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700899
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800900 /**
901 * A basic structure with the data for a contact entry in the list.
902 */
Evan Millar54a5c9f2009-06-23 17:41:09 -0700903 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800904 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -0700905 public boolean isPrimary = false;
906 public int presenceIcon = -1;
907 public int secondaryActionIcon = -1;
908 public Intent intent;
909 public Intent secondaryIntent = null;
910 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800911 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700912 public ArrayList<Long> ids = new ArrayList<Long>();
913 public int collapseCount = 0;
914
915 public boolean collapseWith(ViewEntry entry) {
916 // assert equal collapse keys
917 if (!getCollapseKey().equals(entry.getCollapseKey())) {
918 return false;
919 }
920
921 // Choose the label associated with the highest type precedence.
922 if (TypePrecedence.getTypePrecedence(mimetype, type)
923 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
924 type = entry.type;
925 label = entry.label;
926 }
927
928 // Choose the max of the maxLines and maxLabelLines values.
929 maxLines = Math.max(maxLines, entry.maxLines);
930 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
931
932 // Choose the presence with the highest precedence.
933 if (Presence.getPresencePrecedence(status)
934 < Presence.getPresencePrecedence(entry.status)) {
935 status = entry.status;
936 }
937
938 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -0700939 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700940
941 // uri, and contactdId, shouldn't make a difference. Just keep the original.
942
943 // Keep track of all the ids that have been collapsed with this one.
944 ids.add(entry.id);
945 collapseCount++;
946 return true;
947 }
948
949 public String getCollapseKey() {
950 StringBuilder hashSb = new StringBuilder();
951 hashSb.append(data);
952 hashSb.append(mimetype);
953 hashSb.append((intent != null && intent.getAction() != null)
954 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -0700955 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
956 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -0700957 hashSb.append(actionIcon);
958 return hashSb.toString();
959 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800960 }
961
Evan Millar15e514d2009-08-04 10:14:57 -0700962 /** Cache of the children views of a row */
963 static class ViewCache {
964 public TextView label;
965 public TextView data;
966 public ImageView actionIcon;
967 public ImageView presenceIcon;
968 public ImageView primaryIcon;
969 public ImageView secondaryActionButton;
970 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700971
Evan Millar15e514d2009-08-04 10:14:57 -0700972 // Need to keep track of this too
973 ViewEntry entry;
974 }
975
976 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
977 implements View.OnClickListener {
978
Evan Millar5c22c3b2009-05-29 11:37:54 -0700979
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800980 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
981 super(context, sections, SHOW_SEPARATORS);
982 }
983
Evan Millar15e514d2009-08-04 10:14:57 -0700984 public void onClick(View v) {
985 Intent intent = (Intent) v.getTag();
986 startActivity(intent);
987 }
988
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800989 @Override
990 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -0700991 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800992 View v;
993
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800994 ViewCache views;
995
996 // Check to see if we can reuse convertView
997 if (convertView != null) {
998 v = convertView;
999 views = (ViewCache) v.getTag();
1000 } else {
1001 // Create a new view if needed
1002 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1003
1004 // Cache the children
1005 views = new ViewCache();
1006 views.label = (TextView) v.findViewById(android.R.id.text1);
1007 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001008 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1009 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1010 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1011 views.secondaryActionButton = (ImageView) v.findViewById(
1012 R.id.secondary_action_button);
1013 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001014 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001015 v.setTag(views);
1016 }
1017
1018 // Update the entry in the view cache
1019 views.entry = entry;
1020
1021 // Bind the data to the view
1022 bindView(v, entry);
1023 return v;
1024 }
1025
1026 @Override
1027 protected View newView(int position, ViewGroup parent) {
1028 // getView() handles this
1029 throw new UnsupportedOperationException();
1030 }
1031
1032 @Override
1033 protected void bindView(View view, ViewEntry entry) {
1034 final Resources resources = mContext.getResources();
1035 ViewCache views = (ViewCache) view.getTag();
1036
1037 // Set the label
1038 TextView label = views.label;
1039 setMaxLines(label, entry.maxLabelLines);
1040 label.setText(entry.label);
1041
1042 // Set the data
1043 TextView data = views.data;
1044 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001045 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1046 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1047 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1048 } else {
1049 data.setText(entry.data);
1050 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001051 setMaxLines(data, entry.maxLines);
1052 }
1053
Evan Millar15e514d2009-08-04 10:14:57 -07001054 // Set the primary icon
1055 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1056
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001057 // Set the action icon
1058 ImageView action = views.actionIcon;
1059 if (entry.actionIcon != -1) {
1060 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1061 action.setVisibility(View.VISIBLE);
1062 } else {
1063 // Things should still line up as if there was an icon, so make it invisible
1064 action.setVisibility(View.INVISIBLE);
1065 }
1066
1067 // Set the presence icon
1068 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001069 if (entry.presenceIcon != -1) {
1070 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001071 } else if (entry.status != -1) {
1072 presenceIcon = resources.getDrawable(
1073 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001074 }
Evan Millar15e514d2009-08-04 10:14:57 -07001075 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001076 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001077 presenceIconView.setImageDrawable(presenceIcon);
1078 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001079 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001080 presenceIconView.setVisibility(View.GONE);
1081 }
1082
1083 // Set the secondary action button
1084 ImageView secondaryActionView = views.secondaryActionButton;
1085 Drawable secondaryActionIcon = null;
1086 if (entry.secondaryActionIcon != -1) {
1087 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1088 }
1089 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1090 secondaryActionView.setImageDrawable(secondaryActionIcon);
1091 secondaryActionView.setTag(entry.secondaryIntent);
1092 secondaryActionView.setVisibility(View.VISIBLE);
1093 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1094 } else {
1095 secondaryActionView.setVisibility(View.GONE);
1096 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001097 }
1098 }
1099
1100 private void setMaxLines(TextView textView, int maxLines) {
1101 if (maxLines == 1) {
1102 textView.setSingleLine(true);
1103 textView.setEllipsize(TextUtils.TruncateAt.END);
1104 } else {
1105 textView.setSingleLine(false);
1106 textView.setMaxLines(maxLines);
1107 textView.setEllipsize(null);
1108 }
1109 }
1110 }
1111}