blob: 751ef75e289b1caa2e8e4f118efe0c7c814596ad [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;
Evan Millar7911ff52009-07-21 15:55:18 -070074import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080075import android.view.Menu;
76import android.view.MenuItem;
77import android.view.View;
78import android.view.ViewGroup;
79import android.view.ContextMenu.ContextMenuInfo;
80import android.widget.AdapterView;
81import android.widget.CheckBox;
Evan Millar7911ff52009-07-21 15:55:18 -070082import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080083import android.widget.ImageView;
84import android.widget.ListView;
85import android.widget.TextView;
86import android.widget.Toast;
87
88import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080089
90/**
91 * Displays the details of a specific contact.
92 */
Evan Millar7911ff52009-07-21 15:55:18 -070093public class ViewContactActivity extends BaseContactCardActivity
94 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
95 AdapterView.OnItemClickListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080096 private static final String TAG = "ViewContact";
97 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
98
99 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800100
101 private static final int DIALOG_CONFIRM_DELETE = 1;
102
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700103 private static final int REQUEST_JOIN_AGGREGATE = 1;
104
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800105 public static final int MENU_ITEM_DELETE = 1;
106 public static final int MENU_ITEM_MAKE_DEFAULT = 2;
107 public static final int MENU_ITEM_SHOW_BARCODE = 3;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700108 public static final int MENU_ITEM_SPLIT_AGGREGATE = 4;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700109 public static final int MENU_ITEM_JOIN_AGGREGATE = 5;
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700110 public static final int MENU_ITEM_OPTIONS = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800111
112 private Uri mUri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700113 private Uri mAggDataUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800114 private ContentResolver mResolver;
115 private ViewAdapter mAdapter;
116 private int mNumPhoneNumbers = 0;
117
Evan Millar7911ff52009-07-21 15:55:18 -0700118 private static final long ALL_CONTACTS_ID = -1;
119 private long mRawContactId = ALL_CONTACTS_ID;
120
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700121 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700122 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700123 */
Evan Millar7911ff52009-07-21 15:55:18 -0700124 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700125
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800126 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
127 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
128 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
129 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
130 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
131 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700132 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800133 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
134 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
135
136 private Cursor mCursor;
137 private boolean mObserverRegistered;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700138
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800139 private ContentObserver mObserver = new ContentObserver(new Handler()) {
140 @Override
141 public boolean deliverSelfNotifications() {
142 return true;
143 }
144
145 @Override
146 public void onChange(boolean selfChange) {
147 if (mCursor != null && !mCursor.isClosed()){
148 dataChanged();
149 }
150 }
151 };
152
153 public void onClick(DialogInterface dialog, int which) {
154 if (mCursor != null) {
155 if (mObserverRegistered) {
156 mCursor.unregisterContentObserver(mObserver);
157 mObserverRegistered = false;
158 }
159 mCursor.close();
160 mCursor = null;
161 }
162 getContentResolver().delete(mUri, null, null);
163 finish();
164 }
165
Evan Millar7911ff52009-07-21 15:55:18 -0700166 private FrameLayout mTabContentLayout;
167 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800168 private boolean mShowSmsLinksForAllPhones;
169
170 @Override
171 protected void onCreate(Bundle icicle) {
172 super.onCreate(icicle);
173
Evan Millar7911ff52009-07-21 15:55:18 -0700174 mListView = new ListView(this);
175 mListView.setOnCreateContextMenuListener(this);
176 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
177 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800178
Evan Millar7911ff52009-07-21 15:55:18 -0700179 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
180 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800181
Evan Millar45e0ed32009-06-01 16:44:38 -0700182 mUri = getIntent().getData();
183 mAggDataUri = Uri.withAppendedPath(mUri, "data");
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800184 mResolver = getContentResolver();
185
186 // Build the list of sections. The order they're added to mSections dictates the
187 // order they are displayed in the list.
188 mSections.add(mPhoneEntries);
189 mSections.add(mSmsEntries);
190 mSections.add(mEmailEntries);
191 mSections.add(mImEntries);
192 mSections.add(mPostalEntries);
193 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700194 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800195 mSections.add(mOtherEntries);
196
197 //TODO Read this value from a preference
198 mShowSmsLinksForAllPhones = true;
199
Evan Millar45e0ed32009-06-01 16:44:38 -0700200 mCursor = mResolver.query(mAggDataUri,
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700201 CONTACT_PROJECTION, null, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800202 }
203
204 @Override
205 protected void onResume() {
206 super.onResume();
207 mObserverRegistered = true;
208 mCursor.registerContentObserver(mObserver);
209 dataChanged();
210 }
211
212 @Override
213 protected void onPause() {
214 super.onPause();
215 if (mCursor != null) {
216 if (mObserverRegistered) {
217 mObserverRegistered = false;
218 mCursor.unregisterContentObserver(mObserver);
219 }
220 mCursor.deactivate();
221 }
222 }
223
224 @Override
225 protected void onDestroy() {
226 super.onDestroy();
227
228 if (mCursor != null) {
229 if (mObserverRegistered) {
230 mCursor.unregisterContentObserver(mObserver);
231 mObserverRegistered = false;
232 }
233 mCursor.close();
234 }
235 }
236
237 @Override
238 protected Dialog onCreateDialog(int id) {
239 switch (id) {
240 case DIALOG_CONFIRM_DELETE:
241 return new AlertDialog.Builder(this)
242 .setTitle(R.string.deleteConfirmation_title)
243 .setIcon(android.R.drawable.ic_dialog_alert)
244 .setMessage(R.string.deleteConfirmation)
245 .setNegativeButton(android.R.string.cancel, null)
246 .setPositiveButton(android.R.string.ok, this)
247 .setCancelable(false)
248 .create();
249 }
250 return null;
251 }
252
Evan Millar7911ff52009-07-21 15:55:18 -0700253 @Override
254 protected void bindTabs(Cursor tabsCursor) {
255 if (tabsCursor.getCount() > 1) {
256 addAllTab();
257 }
258 super.bindTabs(tabsCursor);
259 }
260
261 private void addAllTab() {
262 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator, mTabWidget, false);
263 addTab(ALL_CONTACTS_ID, allTabIndicator);
264 }
265
266 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
267 long rawContactId = getTabRawContactId(tabIndex);
268 mRawContactId = rawContactId;
269 dataChanged();
270 }
271
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800272 private void dataChanged() {
273 mCursor.requery();
274 if (mCursor.moveToFirst()) {
Evan Millar2c1cc832009-07-13 11:08:06 -0700275
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800276 // Build up the contact entries
277 buildEntries(mCursor);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700278
279 // Collapse similar data items in select sections.
280 Collapser.collapseList(mPhoneEntries);
281 Collapser.collapseList(mSmsEntries);
282 Collapser.collapseList(mEmailEntries);
283 Collapser.collapseList(mPostalEntries);
284
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800285 if (mAdapter == null) {
286 mAdapter = new ViewAdapter(this, mSections);
Evan Millar7911ff52009-07-21 15:55:18 -0700287 mListView.setAdapter(mAdapter);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800288 } else {
289 mAdapter.setSections(mSections, SHOW_SEPARATORS);
290 }
291 } else {
292 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
293 Log.e(TAG, "invalid contact uri: " + mUri);
294 finish();
295 }
296 }
297
298 @Override
299 public boolean onCreateOptionsMenu(Menu menu) {
300 menu.add(0, 0, 0, R.string.menu_editContact)
301 .setIcon(android.R.drawable.ic_menu_edit)
302 .setIntent(new Intent(Intent.ACTION_EDIT, mUri))
303 .setAlphabeticShortcut('e');
304 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
305 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700306 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
307 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700308 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
309 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700310 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
311 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800312 return true;
313 }
314
315 @Override
316 public boolean onPrepareOptionsMenu(Menu menu) {
317 super.onPrepareOptionsMenu(menu);
318 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
319 // could be installed or uninstalled at any time.
320 if (isBarcodeScannerInstalled()) {
321 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
322 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
323 .setIcon(R.drawable.ic_menu_show_barcode);
324 }
325 } else {
326 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
327 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700328
Evan Millar7911ff52009-07-21 15:55:18 -0700329 boolean isAggregate = mRawContactIds.size() > 1;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700330 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800331 return true;
332 }
333
334 private boolean isBarcodeScannerInstalled() {
335 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
336 ResolveInfo ri = getPackageManager().resolveActivity(intent,
337 PackageManager.MATCH_DEFAULT_ONLY);
338 return ri != null;
339 }
340
341 @Override
342 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
343 AdapterView.AdapterContextMenuInfo info;
344 try {
345 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
346 } catch (ClassCastException e) {
347 Log.e(TAG, "bad menuInfo", e);
348 return;
349 }
350
351 // This can be null sometimes, don't crash...
352 if (info == null) {
353 Log.e(TAG, "bad menuInfo");
354 return;
355 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700356
Evan Millar45e0ed32009-06-01 16:44:38 -0700357 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
358 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
359 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
360 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.auxIntent);
361 if (entry.primaryIcon == -1) {
362 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800363 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700364 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
365 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
366 if (entry.primaryIcon == -1) {
367 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800368 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700369 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700370 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
371 }
372 // TODO(emillar): add back with group support.
373 /* else if (entry.mimetype.equals()) {
374 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
375 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800376 }
377
378 @Override
379 public boolean onOptionsItemSelected(MenuItem item) {
380 switch (item.getItemId()) {
381 case MENU_ITEM_DELETE: {
382 // Get confirmation
383 showDialog(DIALOG_CONFIRM_DELETE);
384 return true;
385 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700386
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700387 case MENU_ITEM_SPLIT_AGGREGATE: {
388 showSplitAggregateDialog();
389 return true;
390 }
391
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700392 case MENU_ITEM_JOIN_AGGREGATE: {
393 showJoinAggregateActivity();
394 return true;
395 }
396
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700397 case MENU_ITEM_OPTIONS: {
398 showOptionsActivity();
399 return true;
400 }
401
Evan Millar66388be2009-05-28 15:41:07 -0700402 // TODO(emillar) Bring this back.
403 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800404 if (mCursor.moveToFirst()) {
405 Intent intent = new Intent(SHOW_BARCODE_INTENT);
406 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
407 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700408 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800409 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700410 // Correctly handle when section headers are hidden
411 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700412
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800413 bundle.putString(Contacts.Intents.Insert.NAME, name);
414 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700415 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800416 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700417 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800418 bundle.putString(PHONE_KEYS[x], entry.data);
419 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700420 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800421 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700422 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800423 bundle.putString(EMAIL_KEYS[x], entry.data);
424 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700425 if (mPostalEntries.size() >= 1 + sepAdjust) {
426 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800427 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
428 }
429 intent.putExtra("ENCODE_DATA", bundle);
430 try {
431 startActivity(intent);
432 } catch (ActivityNotFoundException e) {
433 // The check in onPrepareOptionsMenu() should make this impossible, but
434 // for safety I'm catching the exception rather than crashing. Ideally
435 // I'd call Menu.removeItem() here too, but I don't see a way to get
436 // the options menu.
437 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
438 "was not installed.");
439 }
440 return true;
441 }
442 }
Evan Millar66388be2009-05-28 15:41:07 -0700443 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800444 }
445 return super.onOptionsItemSelected(item);
446 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700447
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800448 @Override
449 public boolean onContextItemSelected(MenuItem item) {
450 switch (item.getItemId()) {
451 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700452 if (makeItemDefault(item)) {
453 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800454 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700455 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800456 }
457 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700458
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800459 return super.onContextItemSelected(item);
460 }
461
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700462 private boolean makeItemDefault(MenuItem item) {
463 ViewEntry entry = getViewEntryForMenuItem(item);
464 if (entry == null) {
465 return false;
466 }
467
468 // Update the primary values in the data record.
469 ContentValues values = new ContentValues(2);
470 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700471
Evan Millar54a5c9f2009-06-23 17:41:09 -0700472 if (entry.ids.size() > 0) {
473 for (int i = 0; i < entry.ids.size(); i++) {
474 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
475 entry.ids.get(i)),
476 values, null, null);
477 }
478 }
479
480 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700481 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
482 values, null, null);
483 dataChanged();
484 return true;
485 }
486
487 /**
488 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
489 * The user picks a contact to be split into its own aggregate or clicks Cancel.
490 */
491 private void showSplitAggregateDialog() {
492
493 // Wrap this dialog in a specific theme so that list items have correct text color.
494 final ContextThemeWrapper dialogContext =
495 new ContextThemeWrapper(this, android.R.style.Theme_Light);
496 AlertDialog.Builder builder =
497 new AlertDialog.Builder(dialogContext);
498 builder.setTitle(getString(R.string.splitAggregate_title));
499
500 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
501 builder.setView(view);
502
503 builder.setInverseBackgroundForced(true);
504 builder.setCancelable(true);
505 builder.setNegativeButton(android.R.string.cancel,
506 new OnClickListener() {
507 public void onClick(DialogInterface dialog, int which) {
508 dialog.dismiss();
509 }
510 });
511 final AlertDialog dialog = builder.create();
512
513 view.setOnContactSelectedListener(new OnContactSelectedListener() {
514 public void onContactSelected(long contactId) {
515 dialog.dismiss();
516 splitContact(contactId);
517 }
518 });
519
520 dialog.show();
521 }
522
523 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700524 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700525 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700526 public void showJoinAggregateActivity() {
527 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
528 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
529 startActivityForResult(intent, REQUEST_JOIN_AGGREGATE);
530 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700531
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700532 @Override
533 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
534 if (requestCode == REQUEST_JOIN_AGGREGATE && resultCode == RESULT_OK && intent != null) {
535 final long aggregateId = ContentUris.parseId(intent.getData());
536 joinAggregate(aggregateId);
537 }
538 }
539
540 private void splitContact(long contactId) {
541 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_OUT);
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700542 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700543 mAdapter.notifyDataSetChanged();
544 }
545
546 private void joinAggregate(final long aggregateId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700547 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700548 RawContacts.CONTACT_ID + "=" + aggregateId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700549
550 try {
551 while(c.moveToNext()) {
552 long contactId = c.getLong(0);
553 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
554 }
555 } finally {
556 c.close();
557 }
558
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700559 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700560 mAdapter.notifyDataSetChanged();
561 }
562
563 /**
564 * Given a contact ID sets an aggregation exception to either join the contact with the
565 * current aggregate or split off.
566 */
567 protected void setAggregationException(long contactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700568 ContentValues values = new ContentValues(3);
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700569 values.put(AggregationExceptions.CONTACT_ID, ContentUris.parseId(mUri));
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700570 values.put(AggregationExceptions.CONTACT_ID, contactId);
571 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700572 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700573 }
574
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700575 private void showOptionsActivity() {
576 final Intent intent = new Intent(this, ContactOptionsActivity.class);
577 intent.setData(mUri);
578 startActivity(intent);
579 }
580
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700581 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
582 AdapterView.AdapterContextMenuInfo info;
583 try {
584 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
585 } catch (ClassCastException e) {
586 Log.e(TAG, "bad menuInfo", e);
587 return null;
588 }
589
590 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
591 }
592
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800593 @Override
594 public boolean onKeyDown(int keyCode, KeyEvent event) {
595 switch (keyCode) {
596 case KeyEvent.KEYCODE_CALL: {
597 try {
598 ITelephony phone = ITelephony.Stub.asInterface(
599 ServiceManager.checkService("phone"));
600 if (phone != null && !phone.isIdle()) {
601 // Skip out and let the key be handled at a higher level
602 break;
603 }
604 } catch (RemoteException re) {
605 // Fall through and try to call the contact
606 }
607
Evan Millar7911ff52009-07-21 15:55:18 -0700608 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800609 if (index != -1) {
610 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700611 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
612 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800613 }
614 } else if (mNumPhoneNumbers != 0) {
615 // There isn't anything selected, call the default number
616 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
617 startActivity(intent);
618 }
619 return true;
620 }
621
622 case KeyEvent.KEYCODE_DEL: {
623 showDialog(DIALOG_CONFIRM_DELETE);
624 return true;
625 }
626 }
627
628 return super.onKeyDown(keyCode, event);
629 }
630
Evan Millar7911ff52009-07-21 15:55:18 -0700631 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800632 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
633 if (entry != null) {
634 Intent intent = entry.intent;
635 if (intent != null) {
636 try {
637 startActivity(intent);
638 } catch (ActivityNotFoundException e) {
639 Log.e(TAG, "No activity found for intent: " + intent);
640 signalError();
641 }
642 } else {
643 signalError();
644 }
645 } else {
646 signalError();
647 }
648 }
649
650 /**
651 * Signal an error to the user via a beep, or some other method.
652 */
653 private void signalError() {
654 //TODO: implement this when we have the sonification APIs
655 }
656
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800657 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700658 // 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 -0800659 StringBuilder buf = new StringBuilder("imto://");
660 buf.append(host);
661 buf.append('/');
662 buf.append(data);
663 return Uri.parse(buf.toString());
664 }
665
666 /**
667 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700668 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800669 * @param personCursor the URI for the contact being displayed
670 */
Evan Millar66388be2009-05-28 15:41:07 -0700671 private final void buildEntries(Cursor aggCursor) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800672 // Clear out the old entries
673 final int numSections = mSections.size();
674 for (int i = 0; i < numSections; i++) {
675 mSections.get(i).clear();
676 }
677
Evan Millar7911ff52009-07-21 15:55:18 -0700678 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700679
Evan Millar66388be2009-05-28 15:41:07 -0700680 // Build up method entries
681 if (mUri != null) {
Jeff Hamilton119b6ca2009-07-30 12:57:49 -0500682 aggCursor.moveToPosition(-1);
Evan Millar66388be2009-05-28 15:41:07 -0700683 while (aggCursor.moveToNext()) {
684 final String mimetype = aggCursor.getString(DATA_MIMETYPE_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700685
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800686 ViewEntry entry = new ViewEntry();
Evan Millar66388be2009-05-28 15:41:07 -0700687
688 final long id = aggCursor.getLong(DATA_ID_COLUMN);
689 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800690 entry.id = id;
691 entry.uri = uri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700692 entry.mimetype = mimetype;
Evan Millar7911ff52009-07-21 15:55:18 -0700693 // TODO: entry.contactId should be renamed to entry.rawContactId
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700694 entry.contactId = aggCursor.getLong(DATA_CONTACT_ID_COLUMN);
Evan Millar7911ff52009-07-21 15:55:18 -0700695 if (!mRawContactIds.contains(entry.contactId)) {
696 mRawContactIds.add(entry.contactId);
697 }
698
699 // This performs the tab filtering
700 if (mRawContactId != entry.contactId && mRawContactId != ALL_CONTACTS_ID) {
701 continue;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700702 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800703
Evan Millar66388be2009-05-28 15:41:07 -0700704 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
Evan Millar5c22c3b2009-05-29 11:37:54 -0700705 || mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700706 || mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
Evan Millar66388be2009-05-28 15:41:07 -0700707 || mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
708 final int type = aggCursor.getInt(DATA_1_COLUMN);
Evan Millar45e0ed32009-06-01 16:44:38 -0700709 final String label = aggCursor.getString(DATA_3_COLUMN);
710 final String data = aggCursor.getString(DATA_2_COLUMN);
711 final boolean isSuperPrimary = "1".equals(
712 aggCursor.getString(DATA_IS_SUPER_PRIMARY_COLUMN));
Evan Millar66388be2009-05-28 15:41:07 -0700713
Evan Millar54a5c9f2009-06-23 17:41:09 -0700714 entry.type = type;
715
Evan Millar66388be2009-05-28 15:41:07 -0700716 // Don't crash if the data is bogus
717 if (TextUtils.isEmpty(data)) {
718 Log.w(TAG, "empty data for contact method " + id);
719 continue;
720 }
721
722 // Build phone entries
723 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
724 mNumPhoneNumbers++;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700725
Evan Millar66388be2009-05-28 15:41:07 -0700726 final CharSequence displayLabel = ContactsUtils.getDisplayLabel(
727 this, mimetype, type, label);
728 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700729 entry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar66388be2009-05-28 15:41:07 -0700730 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, entry.uri);
731 entry.auxIntent = new Intent(Intent.ACTION_SENDTO,
732 Uri.fromParts("sms", data, null));
Evan Millar45e0ed32009-06-01 16:44:38 -0700733 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700734 entry.primaryIcon = R.drawable.ic_default_number;
735 }
736 entry.actionIcon = android.R.drawable.sym_action_call;
737 mPhoneEntries.add(entry);
738
Evan Millar5c22c3b2009-05-29 11:37:54 -0700739 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700740 || mShowSmsLinksForAllPhones) {
741 // Add an SMS entry
742 ViewEntry smsEntry = new ViewEntry();
743 smsEntry.label = buildActionString(
744 R.string.actionText, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700745 smsEntry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar66388be2009-05-28 15:41:07 -0700746 smsEntry.id = id;
747 smsEntry.uri = uri;
748 smsEntry.intent = entry.auxIntent;
Evan Millar45e0ed32009-06-01 16:44:38 -0700749 smsEntry.mimetype = FastTrackWindow.MIME_SMS_ADDRESS;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700750 smsEntry.type = type;
Evan Millar66388be2009-05-28 15:41:07 -0700751 smsEntry.actionIcon = R.drawable.sym_action_sms;
752 mSmsEntries.add(smsEntry);
753 }
754 // Build email entries
755 } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800756 entry.label = buildActionString(R.string.actionEmail,
Evan Millar66388be2009-05-28 15:41:07 -0700757 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800758 entry.data = data;
759 entry.intent = new Intent(Intent.ACTION_SENDTO,
760 Uri.fromParts("mailto", data, null));
761 entry.actionIcon = android.R.drawable.sym_action_email;
Evan Millar45e0ed32009-06-01 16:44:38 -0700762 if (isSuperPrimary) {
Evan Millar66388be2009-05-28 15:41:07 -0700763 entry.primaryIcon = R.drawable.ic_default_number;
764 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800765 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700766 // Build postal entries
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700767 } else if (mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800768 entry.label = buildActionString(R.string.actionMap,
Evan Millar66388be2009-05-28 15:41:07 -0700769 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800770 entry.data = data;
771 entry.maxLines = 4;
772 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
773 entry.actionIcon = R.drawable.sym_action_map;
774 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700775 // Build im entries
776 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
777 String[] protocolStrings = getResources().getStringArray(
778 android.R.array.imProtocols);
Jeff Sharkeye731d422009-07-28 21:12:43 -0700779 Object protocolObj = aggCursor.getString(DATA_5_COLUMN);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700780 String host = null;
Jeff Sharkeye731d422009-07-28 21:12:43 -0700781 // TODO: fix by moving to contactssource-based rendering rules
782// Object protocolObj = ContactsUtils.decodeImProtocol(
783// aggCursor.getString(DATA_5_COLUMN));
784// if (protocolObj instanceof Number) {
785// int protocol = ((Number) protocolObj).intValue();
786// entry.label = buildActionString(R.string.actionChat,
787// protocolStrings[protocol], false);
788// host = ContactsUtils.lookupProviderNameFromId(
789// protocol).toLowerCase();
790// if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
791// || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
792// entry.maxLabelLines = 2;
793// }
794// } else if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800795 String providerName = (String) protocolObj;
796 entry.label = buildActionString(R.string.actionChat,
797 providerName, false);
798 host = providerName.toLowerCase();
Jeff Sharkeye731d422009-07-28 21:12:43 -0700799// }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800800
801 // Only add the intent if there is a valid host
802 if (!TextUtils.isEmpty(host)) {
803 entry.intent = new Intent(Intent.ACTION_SENDTO,
804 constructImToUrl(host, data));
805 }
806 entry.data = data;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700807 //TODO(emillar) Add in presence info
Evan Millar66388be2009-05-28 15:41:07 -0700808 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800809 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700810 aggCursor.getInt(METHODS_STATUS_COLUMN));
Evan Millar54a5c9f2009-06-23 17:41:09 -0700811 entry.status = ...
Evan Millar66388be2009-05-28 15:41:07 -0700812 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800813 entry.actionIcon = android.R.drawable.sym_action_chat;
814 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700815 }
Evan Millar66388be2009-05-28 15:41:07 -0700816 // Build organization entries
817 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
Evan Millar66388be2009-05-28 15:41:07 -0700818 final String company = aggCursor.getString(DATA_3_COLUMN);
819 final String title = aggCursor.getString(DATA_4_COLUMN);
820 final boolean isPrimary = "1".equals(aggCursor.getString(DATA_5_COLUMN));
821
822 if (isPrimary) {
823 entry.primaryIcon = R.drawable.ic_default_number;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800824 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700825
Evan Millar66388be2009-05-28 15:41:07 -0700826 // Don't crash if the data is bogus
827 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
828 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800829 continue;
830 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700831
Evan Millar66388be2009-05-28 15:41:07 -0700832 entry.data = title;
833 entry.actionIcon = R.drawable.sym_action_organization;
834 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800835
Evan Millar66388be2009-05-28 15:41:07 -0700836 mOrganizationEntries.add(entry);
837 // Build note entries
838 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
839 entry.label = getString(R.string.label_notes);
840 entry.data = aggCursor.getString(DATA_1_COLUMN);
841 entry.id = 0;
842 entry.uri = null;
843 entry.intent = null;
844 entry.maxLines = 10;
845 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800846
Evan Millar66388be2009-05-28 15:41:07 -0700847 if (TextUtils.isEmpty(entry.data)) {
848 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700849 continue;
850 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700851
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800852 mOtherEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700853 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700854
Evan Millar45e0ed32009-06-01 16:44:38 -0700855
Evan Millar66388be2009-05-28 15:41:07 -0700856 // TODO(emillar) Add group entries
857// // Build the group entries
858// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
859// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
860// null, null, Groups.DEFAULT_SORT_ORDER);
861// if (groupCursor != null) {
862// try {
863// StringBuilder sb = new StringBuilder();
864//
865// while (groupCursor.moveToNext()) {
866// String systemId = groupCursor.getString(
867// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
868//
869// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
870// continue;
871// }
872//
873// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
874// if (!TextUtils.isEmpty(name)) {
875// if (sb.length() == 0) {
876// sb.append(name);
877// } else {
878// sb.append(getString(R.string.group_list, name));
879// }
880// }
881// }
882//
883// if (sb.length() > 0) {
884// ViewEntry entry = new ViewEntry();
885// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
886// entry.label = getString(R.string.label_groups);
887// entry.data = sb.toString();
888// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
889//
890// // TODO: Add an icon for the groups item.
891//
892// mGroupEntries.add(entry);
893// }
894// } finally {
895// groupCursor.close();
896// }
897// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700898
Evan Millar66388be2009-05-28 15:41:07 -0700899 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800900 }
901 }
902
903 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700904 // If there is no type just display an empty string
905 if (type == null) {
906 type = "";
907 }
908
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800909 if (lowerCase) {
910 return getString(actionResId, type.toString().toLowerCase());
911 } else {
912 return getString(actionResId, type.toString());
913 }
914 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700915
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800916 /**
917 * A basic structure with the data for a contact entry in the list.
918 */
Evan Millar54a5c9f2009-06-23 17:41:09 -0700919 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800920 public int primaryIcon = -1;
921 public Intent intent;
922 public Intent auxIntent = null;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700923 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800924 public int actionIcon = -1;
925 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700926 public ArrayList<Long> ids = new ArrayList<Long>();
927 public int collapseCount = 0;
928
929 public boolean collapseWith(ViewEntry entry) {
930 // assert equal collapse keys
931 if (!getCollapseKey().equals(entry.getCollapseKey())) {
932 return false;
933 }
934
935 // Choose the label associated with the highest type precedence.
936 if (TypePrecedence.getTypePrecedence(mimetype, type)
937 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
938 type = entry.type;
939 label = entry.label;
940 }
941
942 // Choose the max of the maxLines and maxLabelLines values.
943 maxLines = Math.max(maxLines, entry.maxLines);
944 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
945
946 // Choose the presence with the highest precedence.
947 if (Presence.getPresencePrecedence(status)
948 < Presence.getPresencePrecedence(entry.status)) {
949 status = entry.status;
950 }
951
952 // If any of the collapsed entries are primary make the whole thing primary.
953 if (primaryIcon != entry.primaryIcon && primaryIcon == -1) {
954 primaryIcon = entry.primaryIcon;
955 }
956
957 // uri, and contactdId, shouldn't make a difference. Just keep the original.
958
959 // Keep track of all the ids that have been collapsed with this one.
960 ids.add(entry.id);
961 collapseCount++;
962 return true;
963 }
964
965 public String getCollapseKey() {
966 StringBuilder hashSb = new StringBuilder();
967 hashSb.append(data);
968 hashSb.append(mimetype);
969 hashSb.append((intent != null && intent.getAction() != null)
970 ? intent.getAction() : "");
971 hashSb.append((auxIntent != null && auxIntent.getAction() != null)
972 ? auxIntent.getAction() : "");
973 hashSb.append(actionIcon);
974 return hashSb.toString();
975 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800976 }
977
978 private static final class ViewAdapter extends ContactEntryAdapter<ViewEntry> {
979 /** Cache of the children views of a row */
980 static class ViewCache {
981 public TextView label;
982 public TextView data;
983 public ImageView actionIcon;
984 public ImageView presenceIcon;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700985
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800986 // Need to keep track of this too
987 ViewEntry entry;
988 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700989
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800990 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
991 super(context, sections, SHOW_SEPARATORS);
992 }
993
994 @Override
995 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -0700996 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800997 View v;
998
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800999 ViewCache views;
1000
1001 // Check to see if we can reuse convertView
1002 if (convertView != null) {
1003 v = convertView;
1004 views = (ViewCache) v.getTag();
1005 } else {
1006 // Create a new view if needed
1007 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1008
1009 // Cache the children
1010 views = new ViewCache();
1011 views.label = (TextView) v.findViewById(android.R.id.text1);
1012 views.data = (TextView) v.findViewById(android.R.id.text2);
1013 views.actionIcon = (ImageView) v.findViewById(R.id.icon1);
1014 views.presenceIcon = (ImageView) v.findViewById(R.id.icon2);
1015 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
1054 // Set the action icon
1055 ImageView action = views.actionIcon;
1056 if (entry.actionIcon != -1) {
1057 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1058 action.setVisibility(View.VISIBLE);
1059 } else {
1060 // Things should still line up as if there was an icon, so make it invisible
1061 action.setVisibility(View.INVISIBLE);
1062 }
1063
1064 // Set the presence icon
1065 Drawable presenceIcon = null;
1066 if (entry.primaryIcon != -1) {
1067 presenceIcon = resources.getDrawable(entry.primaryIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001068 } else if (entry.status != -1) {
1069 presenceIcon = resources.getDrawable(
1070 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001071 }
1072
1073 ImageView presence = views.presenceIcon;
1074 if (presenceIcon != null) {
1075 presence.setImageDrawable(presenceIcon);
1076 presence.setVisibility(View.VISIBLE);
1077 } else {
1078 presence.setVisibility(View.GONE);
1079 }
1080 }
1081
1082 private void setMaxLines(TextView textView, int maxLines) {
1083 if (maxLines == 1) {
1084 textView.setSingleLine(true);
1085 textView.setEllipsize(TextUtils.TruncateAt.END);
1086 } else {
1087 textView.setSingleLine(false);
1088 textView.setMaxLines(maxLines);
1089 textView.setEllipsize(null);
1090 }
1091 }
1092 }
1093}