blob: 789e7a7c8ea10feda27f22a4e9bc8183ec75ad7d [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;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080035import com.android.internal.telephony.ITelephony;
36
37import android.app.AlertDialog;
38import android.app.Dialog;
39import android.app.ListActivity;
40import android.content.ActivityNotFoundException;
41import android.content.ContentResolver;
42import android.content.ContentUris;
43import android.content.ContentValues;
44import android.content.Context;
45import android.content.DialogInterface;
46import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070047import android.content.DialogInterface.OnClickListener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080048import android.content.pm.PackageManager;
49import android.content.pm.ResolveInfo;
50import android.content.res.Resources;
51import android.database.ContentObserver;
52import android.database.Cursor;
Evan Millar45e0ed32009-06-01 16:44:38 -070053import android.graphics.Bitmap;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080054import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080055import android.net.Uri;
56import android.os.Bundle;
57import android.os.Handler;
58import android.os.RemoteException;
59import android.os.ServiceManager;
60import android.os.SystemClock;
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070061import android.provider.ContactsContract.Contacts;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070062import android.provider.ContactsContract.AggregationExceptions;
63import android.provider.ContactsContract.CommonDataKinds;
Evan Millar66388be2009-05-28 15:41:07 -070064import android.provider.ContactsContract.Data;
Evan Millar54a5c9f2009-06-23 17:41:09 -070065import android.provider.ContactsContract.Presence;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070066import android.provider.ContactsContract.RawContacts;
Evan Millar54a5c9f2009-06-23 17:41:09 -070067import android.provider.ContactsContract.CommonDataKinds.Phone;
68import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080069import android.text.TextUtils;
70import android.util.Log;
71import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070072import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080073import android.view.KeyEvent;
74import android.view.Menu;
75import android.view.MenuItem;
76import android.view.View;
77import android.view.ViewGroup;
78import android.view.ContextMenu.ContextMenuInfo;
79import android.widget.AdapterView;
80import android.widget.CheckBox;
81import android.widget.ImageView;
82import android.widget.ListView;
83import android.widget.TextView;
84import android.widget.Toast;
85
86import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087
88/**
89 * Displays the details of a specific contact.
90 */
Evan Millar5c22c3b2009-05-29 11:37:54 -070091public class ViewContactActivity extends ListActivity
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080092 implements View.OnCreateContextMenuListener, View.OnClickListener,
93 DialogInterface.OnClickListener {
94 private static final String TAG = "ViewContact";
95 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
96
97 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080098
99 private static final int DIALOG_CONFIRM_DELETE = 1;
100
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700101 private static final int REQUEST_JOIN_AGGREGATE = 1;
102
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800103 public static final int MENU_ITEM_DELETE = 1;
104 public static final int MENU_ITEM_MAKE_DEFAULT = 2;
105 public static final int MENU_ITEM_SHOW_BARCODE = 3;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700106 public static final int MENU_ITEM_SPLIT_AGGREGATE = 4;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700107 public static final int MENU_ITEM_JOIN_AGGREGATE = 5;
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700108 public static final int MENU_ITEM_OPTIONS = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800109
110 private Uri mUri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700111 private Uri mAggDataUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800112 private ContentResolver mResolver;
113 private ViewAdapter mAdapter;
114 private int mNumPhoneNumbers = 0;
115
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700116 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700117 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700118 */
119 private ArrayList<Long> mContactIds = new ArrayList<Long>();
120
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800121 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
122 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
123 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
124 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
125 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
126 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700127 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800128 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
129 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
130
131 private Cursor mCursor;
132 private boolean mObserverRegistered;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700133
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800134 private ContentObserver mObserver = new ContentObserver(new Handler()) {
135 @Override
136 public boolean deliverSelfNotifications() {
137 return true;
138 }
139
140 @Override
141 public void onChange(boolean selfChange) {
142 if (mCursor != null && !mCursor.isClosed()){
143 dataChanged();
144 }
145 }
146 };
147
148 public void onClick(DialogInterface dialog, int which) {
149 if (mCursor != null) {
150 if (mObserverRegistered) {
151 mCursor.unregisterContentObserver(mObserver);
152 mObserverRegistered = false;
153 }
154 mCursor.close();
155 mCursor = null;
156 }
157 getContentResolver().delete(mUri, null, null);
158 finish();
159 }
160
161 public void onClick(View view) {
162 if (!mObserverRegistered) {
163 return;
164 }
165 switch (view.getId()) {
166 case R.id.star: {
Evan Millar7e4accf2009-06-08 10:43:26 -0700167 mCursor.moveToFirst();
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700168 int oldStarredState = mCursor.getInt(CONTACT_STARRED_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800169 ContentValues values = new ContentValues(1);
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700170 values.put(Contacts.STARRED, oldStarredState == 1 ? 0 : 1);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800171 getContentResolver().update(mUri, values, null, null);
172 break;
173 }
174 }
175 }
176
177 private TextView mNameView;
178 private TextView mPhoneticNameView; // may be null in some locales
179 private ImageView mPhotoView;
180 private int mNoPhotoResource;
181 private CheckBox mStarView;
182 private boolean mShowSmsLinksForAllPhones;
183
184 @Override
185 protected void onCreate(Bundle icicle) {
186 super.onCreate(icicle);
187
188 setContentView(R.layout.view_contact);
189 getListView().setOnCreateContextMenuListener(this);
190
191 mNameView = (TextView) findViewById(R.id.name);
192 mPhoneticNameView = (TextView) findViewById(R.id.phonetic_name);
193 mPhotoView = (ImageView) findViewById(R.id.photo);
194 mStarView = (CheckBox) findViewById(R.id.star);
195 mStarView.setOnClickListener(this);
196
197 // Set the photo with a random "no contact" image
198 long now = SystemClock.elapsedRealtime();
199 int num = (int) now & 0xf;
200 if (num < 9) {
201 // Leaning in from right, common
202 mNoPhotoResource = R.drawable.ic_contact_picture;
203 } else if (num < 14) {
204 // Leaning in from left uncommon
205 mNoPhotoResource = R.drawable.ic_contact_picture_2;
206 } else {
207 // Coming in from the top, rare
208 mNoPhotoResource = R.drawable.ic_contact_picture_3;
209 }
210
Evan Millar45e0ed32009-06-01 16:44:38 -0700211 mUri = getIntent().getData();
212 mAggDataUri = Uri.withAppendedPath(mUri, "data");
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800213 mResolver = getContentResolver();
214
215 // Build the list of sections. The order they're added to mSections dictates the
216 // order they are displayed in the list.
217 mSections.add(mPhoneEntries);
218 mSections.add(mSmsEntries);
219 mSections.add(mEmailEntries);
220 mSections.add(mImEntries);
221 mSections.add(mPostalEntries);
222 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700223 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800224 mSections.add(mOtherEntries);
225
226 //TODO Read this value from a preference
227 mShowSmsLinksForAllPhones = true;
228
Evan Millar45e0ed32009-06-01 16:44:38 -0700229 mCursor = mResolver.query(mAggDataUri,
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700230 CONTACT_PROJECTION, null, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800231 }
232
233 @Override
234 protected void onResume() {
235 super.onResume();
236 mObserverRegistered = true;
237 mCursor.registerContentObserver(mObserver);
238 dataChanged();
239 }
240
241 @Override
242 protected void onPause() {
243 super.onPause();
244 if (mCursor != null) {
245 if (mObserverRegistered) {
246 mObserverRegistered = false;
247 mCursor.unregisterContentObserver(mObserver);
248 }
249 mCursor.deactivate();
250 }
251 }
252
253 @Override
254 protected void onDestroy() {
255 super.onDestroy();
256
257 if (mCursor != null) {
258 if (mObserverRegistered) {
259 mCursor.unregisterContentObserver(mObserver);
260 mObserverRegistered = false;
261 }
262 mCursor.close();
263 }
264 }
265
266 @Override
267 protected Dialog onCreateDialog(int id) {
268 switch (id) {
269 case DIALOG_CONFIRM_DELETE:
270 return new AlertDialog.Builder(this)
271 .setTitle(R.string.deleteConfirmation_title)
272 .setIcon(android.R.drawable.ic_dialog_alert)
273 .setMessage(R.string.deleteConfirmation)
274 .setNegativeButton(android.R.string.cancel, null)
275 .setPositiveButton(android.R.string.ok, this)
276 .setCancelable(false)
277 .create();
278 }
279 return null;
280 }
281
282 private void dataChanged() {
283 mCursor.requery();
284 if (mCursor.moveToFirst()) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800285 // Set the star
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700286 mStarView.setChecked(mCursor.getInt(CONTACT_STARRED_COLUMN) == 1 ? true : false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800287
Evan Millar2c1cc832009-07-13 11:08:06 -0700288 //Set the photo
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700289 int photoId = mCursor.getInt(CONTACT_PHOTO_ID);
Evan Millar2c1cc832009-07-13 11:08:06 -0700290 Bitmap photoBitmap = ContactsUtils.loadContactPhoto(
291 this, photoId, null);
292 if (photoBitmap == null) {
293 photoBitmap = ContactsUtils.loadPlaceholderPhoto(mNoPhotoResource, this, null);
294 }
295 mPhotoView.setImageBitmap(photoBitmap);
296
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800297 // Build up the contact entries
298 buildEntries(mCursor);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700299
300 // Collapse similar data items in select sections.
301 Collapser.collapseList(mPhoneEntries);
302 Collapser.collapseList(mSmsEntries);
303 Collapser.collapseList(mEmailEntries);
304 Collapser.collapseList(mPostalEntries);
305
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800306 if (mAdapter == null) {
307 mAdapter = new ViewAdapter(this, mSections);
308 setListAdapter(mAdapter);
309 } else {
310 mAdapter.setSections(mSections, SHOW_SEPARATORS);
311 }
312 } else {
313 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
314 Log.e(TAG, "invalid contact uri: " + mUri);
315 finish();
316 }
317 }
318
319 @Override
320 public boolean onCreateOptionsMenu(Menu menu) {
321 menu.add(0, 0, 0, R.string.menu_editContact)
322 .setIcon(android.R.drawable.ic_menu_edit)
323 .setIntent(new Intent(Intent.ACTION_EDIT, mUri))
324 .setAlphabeticShortcut('e');
325 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
326 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700327 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
328 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700329 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
330 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700331 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
332 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800333 return true;
334 }
335
336 @Override
337 public boolean onPrepareOptionsMenu(Menu menu) {
338 super.onPrepareOptionsMenu(menu);
339 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
340 // could be installed or uninstalled at any time.
341 if (isBarcodeScannerInstalled()) {
342 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
343 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
344 .setIcon(R.drawable.ic_menu_show_barcode);
345 }
346 } else {
347 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
348 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700349
350 boolean isAggregate = mContactIds.size() > 1;
351 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800352 return true;
353 }
354
355 private boolean isBarcodeScannerInstalled() {
356 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
357 ResolveInfo ri = getPackageManager().resolveActivity(intent,
358 PackageManager.MATCH_DEFAULT_ONLY);
359 return ri != null;
360 }
361
362 @Override
363 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
364 AdapterView.AdapterContextMenuInfo info;
365 try {
366 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
367 } catch (ClassCastException e) {
368 Log.e(TAG, "bad menuInfo", e);
369 return;
370 }
371
372 // This can be null sometimes, don't crash...
373 if (info == null) {
374 Log.e(TAG, "bad menuInfo");
375 return;
376 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700377
Evan Millar45e0ed32009-06-01 16:44:38 -0700378 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
379 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
380 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
381 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.auxIntent);
382 if (entry.primaryIcon == -1) {
383 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800384 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700385 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
386 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
387 if (entry.primaryIcon == -1) {
388 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800389 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700390 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700391 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
392 }
393 // TODO(emillar): add back with group support.
394 /* else if (entry.mimetype.equals()) {
395 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
396 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800397 }
398
399 @Override
400 public boolean onOptionsItemSelected(MenuItem item) {
401 switch (item.getItemId()) {
402 case MENU_ITEM_DELETE: {
403 // Get confirmation
404 showDialog(DIALOG_CONFIRM_DELETE);
405 return true;
406 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700407
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700408 case MENU_ITEM_SPLIT_AGGREGATE: {
409 showSplitAggregateDialog();
410 return true;
411 }
412
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700413 case MENU_ITEM_JOIN_AGGREGATE: {
414 showJoinAggregateActivity();
415 return true;
416 }
417
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700418 case MENU_ITEM_OPTIONS: {
419 showOptionsActivity();
420 return true;
421 }
422
Evan Millar66388be2009-05-28 15:41:07 -0700423 // TODO(emillar) Bring this back.
424 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800425 if (mCursor.moveToFirst()) {
426 Intent intent = new Intent(SHOW_BARCODE_INTENT);
427 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
428 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700429 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800430 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700431 // Correctly handle when section headers are hidden
432 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700433
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800434 bundle.putString(Contacts.Intents.Insert.NAME, name);
435 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700436 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800437 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700438 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800439 bundle.putString(PHONE_KEYS[x], entry.data);
440 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700441 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800442 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700443 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800444 bundle.putString(EMAIL_KEYS[x], entry.data);
445 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700446 if (mPostalEntries.size() >= 1 + sepAdjust) {
447 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800448 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
449 }
450 intent.putExtra("ENCODE_DATA", bundle);
451 try {
452 startActivity(intent);
453 } catch (ActivityNotFoundException e) {
454 // The check in onPrepareOptionsMenu() should make this impossible, but
455 // for safety I'm catching the exception rather than crashing. Ideally
456 // I'd call Menu.removeItem() here too, but I don't see a way to get
457 // the options menu.
458 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
459 "was not installed.");
460 }
461 return true;
462 }
463 }
Evan Millar66388be2009-05-28 15:41:07 -0700464 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800465 }
466 return super.onOptionsItemSelected(item);
467 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700468
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800469 @Override
470 public boolean onContextItemSelected(MenuItem item) {
471 switch (item.getItemId()) {
472 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700473 if (makeItemDefault(item)) {
474 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800475 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700476 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800477 }
478 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700479
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800480 return super.onContextItemSelected(item);
481 }
482
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700483 private boolean makeItemDefault(MenuItem item) {
484 ViewEntry entry = getViewEntryForMenuItem(item);
485 if (entry == null) {
486 return false;
487 }
488
489 // Update the primary values in the data record.
490 ContentValues values = new ContentValues(2);
491 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700492
Evan Millar54a5c9f2009-06-23 17:41:09 -0700493 if (entry.ids.size() > 0) {
494 for (int i = 0; i < entry.ids.size(); i++) {
495 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
496 entry.ids.get(i)),
497 values, null, null);
498 }
499 }
500
501 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700502 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
503 values, null, null);
504 dataChanged();
505 return true;
506 }
507
508 /**
509 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
510 * The user picks a contact to be split into its own aggregate or clicks Cancel.
511 */
512 private void showSplitAggregateDialog() {
513
514 // Wrap this dialog in a specific theme so that list items have correct text color.
515 final ContextThemeWrapper dialogContext =
516 new ContextThemeWrapper(this, android.R.style.Theme_Light);
517 AlertDialog.Builder builder =
518 new AlertDialog.Builder(dialogContext);
519 builder.setTitle(getString(R.string.splitAggregate_title));
520
521 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
522 builder.setView(view);
523
524 builder.setInverseBackgroundForced(true);
525 builder.setCancelable(true);
526 builder.setNegativeButton(android.R.string.cancel,
527 new OnClickListener() {
528 public void onClick(DialogInterface dialog, int which) {
529 dialog.dismiss();
530 }
531 });
532 final AlertDialog dialog = builder.create();
533
534 view.setOnContactSelectedListener(new OnContactSelectedListener() {
535 public void onContactSelected(long contactId) {
536 dialog.dismiss();
537 splitContact(contactId);
538 }
539 });
540
541 dialog.show();
542 }
543
544 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700545 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700546 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700547 public void showJoinAggregateActivity() {
548 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
549 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
550 startActivityForResult(intent, REQUEST_JOIN_AGGREGATE);
551 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700552
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700553 @Override
554 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
555 if (requestCode == REQUEST_JOIN_AGGREGATE && resultCode == RESULT_OK && intent != null) {
556 final long aggregateId = ContentUris.parseId(intent.getData());
557 joinAggregate(aggregateId);
558 }
559 }
560
561 private void splitContact(long contactId) {
562 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_OUT);
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700563 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700564 mAdapter.notifyDataSetChanged();
565 }
566
567 private void joinAggregate(final long aggregateId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700568 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700569 RawContacts.CONTACT_ID + "=" + aggregateId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700570
571 try {
572 while(c.moveToNext()) {
573 long contactId = c.getLong(0);
574 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
575 }
576 } finally {
577 c.close();
578 }
579
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700580 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700581 mAdapter.notifyDataSetChanged();
582 }
583
584 /**
585 * Given a contact ID sets an aggregation exception to either join the contact with the
586 * current aggregate or split off.
587 */
588 protected void setAggregationException(long contactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700589 ContentValues values = new ContentValues(3);
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700590 values.put(AggregationExceptions.CONTACT_ID, ContentUris.parseId(mUri));
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700591 values.put(AggregationExceptions.CONTACT_ID, contactId);
592 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700593 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700594 }
595
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700596 private void showOptionsActivity() {
597 final Intent intent = new Intent(this, ContactOptionsActivity.class);
598 intent.setData(mUri);
599 startActivity(intent);
600 }
601
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700602 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
603 AdapterView.AdapterContextMenuInfo info;
604 try {
605 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
606 } catch (ClassCastException e) {
607 Log.e(TAG, "bad menuInfo", e);
608 return null;
609 }
610
611 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
612 }
613
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800614 @Override
615 public boolean onKeyDown(int keyCode, KeyEvent event) {
616 switch (keyCode) {
617 case KeyEvent.KEYCODE_CALL: {
618 try {
619 ITelephony phone = ITelephony.Stub.asInterface(
620 ServiceManager.checkService("phone"));
621 if (phone != null && !phone.isIdle()) {
622 // Skip out and let the key be handled at a higher level
623 break;
624 }
625 } catch (RemoteException re) {
626 // Fall through and try to call the contact
627 }
628
629 int index = getListView().getSelectedItemPosition();
630 if (index != -1) {
631 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700632 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
633 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800634 }
635 } else if (mNumPhoneNumbers != 0) {
636 // There isn't anything selected, call the default number
637 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
638 startActivity(intent);
639 }
640 return true;
641 }
642
643 case KeyEvent.KEYCODE_DEL: {
644 showDialog(DIALOG_CONFIRM_DELETE);
645 return true;
646 }
647 }
648
649 return super.onKeyDown(keyCode, event);
650 }
651
652 @Override
653 protected void onListItemClick(ListView l, View v, int position, long id) {
654 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
655 if (entry != null) {
656 Intent intent = entry.intent;
657 if (intent != null) {
658 try {
659 startActivity(intent);
660 } catch (ActivityNotFoundException e) {
661 Log.e(TAG, "No activity found for intent: " + intent);
662 signalError();
663 }
664 } else {
665 signalError();
666 }
667 } else {
668 signalError();
669 }
670 }
671
672 /**
673 * Signal an error to the user via a beep, or some other method.
674 */
675 private void signalError() {
676 //TODO: implement this when we have the sonification APIs
677 }
678
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800679 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700680 // 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 -0800681 StringBuilder buf = new StringBuilder("imto://");
682 buf.append(host);
683 buf.append('/');
684 buf.append(data);
685 return Uri.parse(buf.toString());
686 }
687
688 /**
689 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700690 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800691 * @param personCursor the URI for the contact being displayed
692 */
Evan Millar66388be2009-05-28 15:41:07 -0700693 private final void buildEntries(Cursor aggCursor) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800694 // Clear out the old entries
695 final int numSections = mSections.size();
696 for (int i = 0; i < numSections; i++) {
697 mSections.get(i).clear();
698 }
699
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700700 mContactIds.clear();
701
Evan Millar66388be2009-05-28 15:41:07 -0700702 // Build up method entries
703 if (mUri != null) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700704 Bitmap photoBitmap = null;
Jeff Hamilton119b6ca2009-07-30 12:57:49 -0500705 aggCursor.moveToPosition(-1);
Evan Millar66388be2009-05-28 15:41:07 -0700706 while (aggCursor.moveToNext()) {
707 final String mimetype = aggCursor.getString(DATA_MIMETYPE_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700708
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800709 ViewEntry entry = new ViewEntry();
Evan Millar66388be2009-05-28 15:41:07 -0700710
711 final long id = aggCursor.getLong(DATA_ID_COLUMN);
712 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800713 entry.id = id;
714 entry.uri = uri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700715 entry.mimetype = mimetype;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700716 entry.contactId = aggCursor.getLong(DATA_CONTACT_ID_COLUMN);
717 if (!mContactIds.contains(entry.contactId)) {
718 mContactIds.add(entry.contactId);
719 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800720
Evan Millar66388be2009-05-28 15:41:07 -0700721 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
Evan Millar5c22c3b2009-05-29 11:37:54 -0700722 || mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700723 || mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
Evan Millar66388be2009-05-28 15:41:07 -0700724 || mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
725 final int type = aggCursor.getInt(DATA_1_COLUMN);
Evan Millar45e0ed32009-06-01 16:44:38 -0700726 final String label = aggCursor.getString(DATA_3_COLUMN);
727 final String data = aggCursor.getString(DATA_2_COLUMN);
728 final boolean isSuperPrimary = "1".equals(
729 aggCursor.getString(DATA_IS_SUPER_PRIMARY_COLUMN));
Evan Millar66388be2009-05-28 15:41:07 -0700730
Evan Millar54a5c9f2009-06-23 17:41:09 -0700731 entry.type = type;
732
Evan Millar66388be2009-05-28 15:41:07 -0700733 // Don't crash if the data is bogus
734 if (TextUtils.isEmpty(data)) {
735 Log.w(TAG, "empty data for contact method " + id);
736 continue;
737 }
738
739 // Build phone entries
740 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
741 mNumPhoneNumbers++;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700742
Evan Millar66388be2009-05-28 15:41:07 -0700743 final CharSequence displayLabel = ContactsUtils.getDisplayLabel(
744 this, mimetype, type, label);
745 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700746 entry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar66388be2009-05-28 15:41:07 -0700747 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, entry.uri);
748 entry.auxIntent = new Intent(Intent.ACTION_SENDTO,
749 Uri.fromParts("sms", data, null));
Evan Millar45e0ed32009-06-01 16:44:38 -0700750 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700751 entry.primaryIcon = R.drawable.ic_default_number;
752 }
753 entry.actionIcon = android.R.drawable.sym_action_call;
754 mPhoneEntries.add(entry);
755
Evan Millar5c22c3b2009-05-29 11:37:54 -0700756 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700757 || mShowSmsLinksForAllPhones) {
758 // Add an SMS entry
759 ViewEntry smsEntry = new ViewEntry();
760 smsEntry.label = buildActionString(
761 R.string.actionText, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700762 smsEntry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar66388be2009-05-28 15:41:07 -0700763 smsEntry.id = id;
764 smsEntry.uri = uri;
765 smsEntry.intent = entry.auxIntent;
Evan Millar45e0ed32009-06-01 16:44:38 -0700766 smsEntry.mimetype = FastTrackWindow.MIME_SMS_ADDRESS;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700767 smsEntry.type = type;
Evan Millar66388be2009-05-28 15:41:07 -0700768 smsEntry.actionIcon = R.drawable.sym_action_sms;
769 mSmsEntries.add(smsEntry);
770 }
771 // Build email entries
772 } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800773 entry.label = buildActionString(R.string.actionEmail,
Evan Millar66388be2009-05-28 15:41:07 -0700774 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800775 entry.data = data;
776 entry.intent = new Intent(Intent.ACTION_SENDTO,
777 Uri.fromParts("mailto", data, null));
778 entry.actionIcon = android.R.drawable.sym_action_email;
Evan Millar45e0ed32009-06-01 16:44:38 -0700779 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700780 entry.primaryIcon = R.drawable.ic_default_number;
781 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800782 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700783 // Build postal entries
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700784 } else if (mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800785 entry.label = buildActionString(R.string.actionMap,
Evan Millar66388be2009-05-28 15:41:07 -0700786 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800787 entry.data = data;
788 entry.maxLines = 4;
789 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
790 entry.actionIcon = R.drawable.sym_action_map;
791 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700792 // Build im entries
793 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
794 String[] protocolStrings = getResources().getStringArray(
795 android.R.array.imProtocols);
796 Object protocolObj = ContactsUtils.decodeImProtocol(
797 aggCursor.getString(DATA_5_COLUMN));
Alex Kennberg87fc3172009-03-28 06:43:06 -0700798 String host = null;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800799 if (protocolObj instanceof Number) {
800 int protocol = ((Number) protocolObj).intValue();
801 entry.label = buildActionString(R.string.actionChat,
802 protocolStrings[protocol], false);
Evan Millar66388be2009-05-28 15:41:07 -0700803 host = ContactsUtils.lookupProviderNameFromId(
804 protocol).toLowerCase();
805 if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
806 || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800807 entry.maxLabelLines = 2;
808 }
Alex Kennberg87fc3172009-03-28 06:43:06 -0700809 } else if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800810 String providerName = (String) protocolObj;
811 entry.label = buildActionString(R.string.actionChat,
812 providerName, false);
813 host = providerName.toLowerCase();
814 }
815
816 // Only add the intent if there is a valid host
817 if (!TextUtils.isEmpty(host)) {
818 entry.intent = new Intent(Intent.ACTION_SENDTO,
819 constructImToUrl(host, data));
820 }
821 entry.data = data;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700822 //TODO(emillar) Add in presence info
Evan Millar66388be2009-05-28 15:41:07 -0700823 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800824 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700825 aggCursor.getInt(METHODS_STATUS_COLUMN));
Evan Millar54a5c9f2009-06-23 17:41:09 -0700826 entry.status = ...
Evan Millar66388be2009-05-28 15:41:07 -0700827 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800828 entry.actionIcon = android.R.drawable.sym_action_chat;
829 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700830 }
Evan Millar7e4accf2009-06-08 10:43:26 -0700831 // Set the name
832 } else if (mimetype.equals(CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) {
833 String name = mCursor.getString(DATA_9_COLUMN);
834 if (TextUtils.isEmpty(name)) {
835 mNameView.setText(getText(android.R.string.unknownName));
836 } else {
837 mNameView.setText(name);
838 }
839 /*
840 if (mPhoneticNameView != null) {
841 String phoneticName = mCursor.getString(CONTACT_PHONETIC_NAME_COLUMN);
842 mPhoneticNameView.setText(phoneticName);
843 }
844 */
Evan Millar66388be2009-05-28 15:41:07 -0700845 // Build organization entries
846 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
Evan Millar66388be2009-05-28 15:41:07 -0700847 final String company = aggCursor.getString(DATA_3_COLUMN);
848 final String title = aggCursor.getString(DATA_4_COLUMN);
849 final boolean isPrimary = "1".equals(aggCursor.getString(DATA_5_COLUMN));
850
851 if (isPrimary) {
852 entry.primaryIcon = R.drawable.ic_default_number;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800853 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700854
Evan Millar66388be2009-05-28 15:41:07 -0700855 // Don't crash if the data is bogus
856 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
857 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800858 continue;
859 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700860
Evan Millar66388be2009-05-28 15:41:07 -0700861 entry.data = title;
862 entry.actionIcon = R.drawable.sym_action_organization;
863 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800864
Evan Millar66388be2009-05-28 15:41:07 -0700865 mOrganizationEntries.add(entry);
866 // Build note entries
867 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
868 entry.label = getString(R.string.label_notes);
869 entry.data = aggCursor.getString(DATA_1_COLUMN);
870 entry.id = 0;
871 entry.uri = null;
872 entry.intent = null;
873 entry.maxLines = 10;
874 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800875
Evan Millar66388be2009-05-28 15:41:07 -0700876 if (TextUtils.isEmpty(entry.data)) {
877 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700878 continue;
879 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700880
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800881 mOtherEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700882 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700883
Evan Millar45e0ed32009-06-01 16:44:38 -0700884
Evan Millar66388be2009-05-28 15:41:07 -0700885 // TODO(emillar) Add group entries
886// // Build the group entries
887// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
888// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
889// null, null, Groups.DEFAULT_SORT_ORDER);
890// if (groupCursor != null) {
891// try {
892// StringBuilder sb = new StringBuilder();
893//
894// while (groupCursor.moveToNext()) {
895// String systemId = groupCursor.getString(
896// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
897//
898// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
899// continue;
900// }
901//
902// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
903// if (!TextUtils.isEmpty(name)) {
904// if (sb.length() == 0) {
905// sb.append(name);
906// } else {
907// sb.append(getString(R.string.group_list, name));
908// }
909// }
910// }
911//
912// if (sb.length() > 0) {
913// ViewEntry entry = new ViewEntry();
914// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
915// entry.label = getString(R.string.label_groups);
916// entry.data = sb.toString();
917// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
918//
919// // TODO: Add an icon for the groups item.
920//
921// mGroupEntries.add(entry);
922// }
923// } finally {
924// groupCursor.close();
925// }
926// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700927
Evan Millar66388be2009-05-28 15:41:07 -0700928 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800929 }
930 }
931
932 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700933 // If there is no type just display an empty string
934 if (type == null) {
935 type = "";
936 }
937
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800938 if (lowerCase) {
939 return getString(actionResId, type.toString().toLowerCase());
940 } else {
941 return getString(actionResId, type.toString());
942 }
943 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700944
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800945 /**
946 * A basic structure with the data for a contact entry in the list.
947 */
Evan Millar54a5c9f2009-06-23 17:41:09 -0700948 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800949 public int primaryIcon = -1;
950 public Intent intent;
951 public Intent auxIntent = null;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700952 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800953 public int actionIcon = -1;
954 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700955 public ArrayList<Long> ids = new ArrayList<Long>();
956 public int collapseCount = 0;
957
958 public boolean collapseWith(ViewEntry entry) {
959 // assert equal collapse keys
960 if (!getCollapseKey().equals(entry.getCollapseKey())) {
961 return false;
962 }
963
964 // Choose the label associated with the highest type precedence.
965 if (TypePrecedence.getTypePrecedence(mimetype, type)
966 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
967 type = entry.type;
968 label = entry.label;
969 }
970
971 // Choose the max of the maxLines and maxLabelLines values.
972 maxLines = Math.max(maxLines, entry.maxLines);
973 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
974
975 // Choose the presence with the highest precedence.
976 if (Presence.getPresencePrecedence(status)
977 < Presence.getPresencePrecedence(entry.status)) {
978 status = entry.status;
979 }
980
981 // If any of the collapsed entries are primary make the whole thing primary.
982 if (primaryIcon != entry.primaryIcon && primaryIcon == -1) {
983 primaryIcon = entry.primaryIcon;
984 }
985
986 // uri, and contactdId, shouldn't make a difference. Just keep the original.
987
988 // Keep track of all the ids that have been collapsed with this one.
989 ids.add(entry.id);
990 collapseCount++;
991 return true;
992 }
993
994 public String getCollapseKey() {
995 StringBuilder hashSb = new StringBuilder();
996 hashSb.append(data);
997 hashSb.append(mimetype);
998 hashSb.append((intent != null && intent.getAction() != null)
999 ? intent.getAction() : "");
1000 hashSb.append((auxIntent != null && auxIntent.getAction() != null)
1001 ? auxIntent.getAction() : "");
1002 hashSb.append(actionIcon);
1003 return hashSb.toString();
1004 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001005 }
1006
1007 private static final class ViewAdapter extends ContactEntryAdapter<ViewEntry> {
1008 /** Cache of the children views of a row */
1009 static class ViewCache {
1010 public TextView label;
1011 public TextView data;
1012 public ImageView actionIcon;
1013 public ImageView presenceIcon;
Evan Millar5c22c3b2009-05-29 11:37:54 -07001014
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001015 // Need to keep track of this too
1016 ViewEntry entry;
1017 }
Evan Millar5c22c3b2009-05-29 11:37:54 -07001018
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001019 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
1020 super(context, sections, SHOW_SEPARATORS);
1021 }
1022
1023 @Override
1024 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001025 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001026 View v;
1027
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001028 ViewCache views;
1029
1030 // Check to see if we can reuse convertView
1031 if (convertView != null) {
1032 v = convertView;
1033 views = (ViewCache) v.getTag();
1034 } else {
1035 // Create a new view if needed
1036 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1037
1038 // Cache the children
1039 views = new ViewCache();
1040 views.label = (TextView) v.findViewById(android.R.id.text1);
1041 views.data = (TextView) v.findViewById(android.R.id.text2);
1042 views.actionIcon = (ImageView) v.findViewById(R.id.icon1);
1043 views.presenceIcon = (ImageView) v.findViewById(R.id.icon2);
1044 v.setTag(views);
1045 }
1046
1047 // Update the entry in the view cache
1048 views.entry = entry;
1049
1050 // Bind the data to the view
1051 bindView(v, entry);
1052 return v;
1053 }
1054
1055 @Override
1056 protected View newView(int position, ViewGroup parent) {
1057 // getView() handles this
1058 throw new UnsupportedOperationException();
1059 }
1060
1061 @Override
1062 protected void bindView(View view, ViewEntry entry) {
1063 final Resources resources = mContext.getResources();
1064 ViewCache views = (ViewCache) view.getTag();
1065
1066 // Set the label
1067 TextView label = views.label;
1068 setMaxLines(label, entry.maxLabelLines);
1069 label.setText(entry.label);
1070
1071 // Set the data
1072 TextView data = views.data;
1073 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001074 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1075 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1076 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1077 } else {
1078 data.setText(entry.data);
1079 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001080 setMaxLines(data, entry.maxLines);
1081 }
1082
1083 // Set the action icon
1084 ImageView action = views.actionIcon;
1085 if (entry.actionIcon != -1) {
1086 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1087 action.setVisibility(View.VISIBLE);
1088 } else {
1089 // Things should still line up as if there was an icon, so make it invisible
1090 action.setVisibility(View.INVISIBLE);
1091 }
1092
1093 // Set the presence icon
1094 Drawable presenceIcon = null;
1095 if (entry.primaryIcon != -1) {
1096 presenceIcon = resources.getDrawable(entry.primaryIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001097 } else if (entry.status != -1) {
1098 presenceIcon = resources.getDrawable(
1099 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001100 }
1101
1102 ImageView presence = views.presenceIcon;
1103 if (presenceIcon != null) {
1104 presence.setImageDrawable(presenceIcon);
1105 presence.setVisibility(View.VISIBLE);
1106 } else {
1107 presence.setVisibility(View.GONE);
1108 }
1109 }
1110
1111 private void setMaxLines(TextView textView, int maxLines) {
1112 if (maxLines == 1) {
1113 textView.setSingleLine(true);
1114 textView.setEllipsize(TextUtils.TruncateAt.END);
1115 } else {
1116 textView.setSingleLine(false);
1117 textView.setMaxLines(maxLines);
1118 textView.setEllipsize(null);
1119 }
1120 }
1121 }
1122}