blob: f678632270d8d61e55b5f97189fb8e67006a25dd [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);
Evan Millar15e514d2009-08-04 10:14:57 -0700360 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
361 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700362 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);
Evan Millar15e514d2009-08-04 10:14:57 -0700366 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700367 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);
Evan Millar15e514d2009-08-04 10:14:57 -0700731 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
Evan Millar66388be2009-05-28 15:41:07 -0700732 Uri.fromParts("sms", data, null));
Evan Millar15e514d2009-08-04 10:14:57 -0700733 entry.isPrimary = isSuperPrimary;
Evan Millar66388be2009-05-28 15:41:07 -0700734 entry.actionIcon = android.R.drawable.sym_action_call;
735 mPhoneEntries.add(entry);
736
Evan Millar5c22c3b2009-05-29 11:37:54 -0700737 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700738 || mShowSmsLinksForAllPhones) {
739 // Add an SMS entry
Evan Millar15e514d2009-08-04 10:14:57 -0700740 entry.secondaryActionIcon = R.drawable.sym_action_sms;
741
742// ViewEntry smsEntry = new ViewEntry();
743// smsEntry.label = buildActionString(
744// R.string.actionText, displayLabel, true);
745// smsEntry.data = PhoneNumberUtils.stripSeparators(data);
746// smsEntry.id = id;
747// smsEntry.uri = uri;
748// smsEntry.intent = entry.secondaryIntent;
749// smsEntry.mimetype = FastTrackWindow.MIME_SMS_ADDRESS;
750// smsEntry.type = type;
751// smsEntry.actionIcon = R.drawable.sym_action_sms;
752// mSmsEntries.add(smsEntry);
Evan Millar66388be2009-05-28 15:41:07 -0700753 }
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 Millar15e514d2009-08-04 10:14:57 -0700762 entry.isPrimary = isSuperPrimary;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800763 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700764 // Build postal entries
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700765 } else if (mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800766 entry.label = buildActionString(R.string.actionMap,
Evan Millar66388be2009-05-28 15:41:07 -0700767 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800768 entry.data = data;
769 entry.maxLines = 4;
770 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
771 entry.actionIcon = R.drawable.sym_action_map;
772 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700773 // Build im entries
774 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
775 String[] protocolStrings = getResources().getStringArray(
776 android.R.array.imProtocols);
Jeff Sharkeye731d422009-07-28 21:12:43 -0700777 Object protocolObj = aggCursor.getString(DATA_5_COLUMN);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700778 String host = null;
Jeff Sharkeye731d422009-07-28 21:12:43 -0700779 // TODO: fix by moving to contactssource-based rendering rules
780// Object protocolObj = ContactsUtils.decodeImProtocol(
781// aggCursor.getString(DATA_5_COLUMN));
782// if (protocolObj instanceof Number) {
783// int protocol = ((Number) protocolObj).intValue();
784// entry.label = buildActionString(R.string.actionChat,
785// protocolStrings[protocol], false);
786// host = ContactsUtils.lookupProviderNameFromId(
787// protocol).toLowerCase();
788// if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
789// || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
790// entry.maxLabelLines = 2;
791// }
792// } else if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800793 String providerName = (String) protocolObj;
794 entry.label = buildActionString(R.string.actionChat,
795 providerName, false);
796 host = providerName.toLowerCase();
Jeff Sharkeye731d422009-07-28 21:12:43 -0700797// }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800798
799 // Only add the intent if there is a valid host
800 if (!TextUtils.isEmpty(host)) {
801 entry.intent = new Intent(Intent.ACTION_SENDTO,
802 constructImToUrl(host, data));
803 }
804 entry.data = data;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700805 //TODO(emillar) Add in presence info
Evan Millar66388be2009-05-28 15:41:07 -0700806 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800807 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700808 aggCursor.getInt(METHODS_STATUS_COLUMN));
Evan Millar54a5c9f2009-06-23 17:41:09 -0700809 entry.status = ...
Evan Millar66388be2009-05-28 15:41:07 -0700810 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800811 entry.actionIcon = android.R.drawable.sym_action_chat;
812 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700813 }
Evan Millar66388be2009-05-28 15:41:07 -0700814 // Build organization entries
815 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
Evan Millar66388be2009-05-28 15:41:07 -0700816 final String company = aggCursor.getString(DATA_3_COLUMN);
817 final String title = aggCursor.getString(DATA_4_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700818
Evan Millar66388be2009-05-28 15:41:07 -0700819 // Don't crash if the data is bogus
820 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
821 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800822 continue;
823 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700824
Evan Millar66388be2009-05-28 15:41:07 -0700825 entry.data = title;
826 entry.actionIcon = R.drawable.sym_action_organization;
827 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800828
Evan Millar66388be2009-05-28 15:41:07 -0700829 mOrganizationEntries.add(entry);
830 // Build note entries
831 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
832 entry.label = getString(R.string.label_notes);
833 entry.data = aggCursor.getString(DATA_1_COLUMN);
834 entry.id = 0;
835 entry.uri = null;
836 entry.intent = null;
837 entry.maxLines = 10;
838 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800839
Evan Millar66388be2009-05-28 15:41:07 -0700840 if (TextUtils.isEmpty(entry.data)) {
841 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700842 continue;
843 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700844
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800845 mOtherEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700846 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700847
Evan Millar45e0ed32009-06-01 16:44:38 -0700848
Evan Millar66388be2009-05-28 15:41:07 -0700849 // TODO(emillar) Add group entries
850// // Build the group entries
851// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
852// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
853// null, null, Groups.DEFAULT_SORT_ORDER);
854// if (groupCursor != null) {
855// try {
856// StringBuilder sb = new StringBuilder();
857//
858// while (groupCursor.moveToNext()) {
859// String systemId = groupCursor.getString(
860// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
861//
862// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
863// continue;
864// }
865//
866// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
867// if (!TextUtils.isEmpty(name)) {
868// if (sb.length() == 0) {
869// sb.append(name);
870// } else {
871// sb.append(getString(R.string.group_list, name));
872// }
873// }
874// }
875//
876// if (sb.length() > 0) {
877// ViewEntry entry = new ViewEntry();
878// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
879// entry.label = getString(R.string.label_groups);
880// entry.data = sb.toString();
881// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
882//
883// // TODO: Add an icon for the groups item.
884//
885// mGroupEntries.add(entry);
886// }
887// } finally {
888// groupCursor.close();
889// }
890// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700891
Evan Millar66388be2009-05-28 15:41:07 -0700892 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800893 }
894 }
895
896 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700897 // If there is no type just display an empty string
898 if (type == null) {
899 type = "";
900 }
901
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800902 if (lowerCase) {
903 return getString(actionResId, type.toString().toLowerCase());
904 } else {
905 return getString(actionResId, type.toString());
906 }
907 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700908
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800909 /**
910 * A basic structure with the data for a contact entry in the list.
911 */
Evan Millar54a5c9f2009-06-23 17:41:09 -0700912 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800913 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -0700914 public boolean isPrimary = false;
915 public int presenceIcon = -1;
916 public int secondaryActionIcon = -1;
917 public Intent intent;
918 public Intent secondaryIntent = null;
919 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800920 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700921 public ArrayList<Long> ids = new ArrayList<Long>();
922 public int collapseCount = 0;
923
924 public boolean collapseWith(ViewEntry entry) {
925 // assert equal collapse keys
926 if (!getCollapseKey().equals(entry.getCollapseKey())) {
927 return false;
928 }
929
930 // Choose the label associated with the highest type precedence.
931 if (TypePrecedence.getTypePrecedence(mimetype, type)
932 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
933 type = entry.type;
934 label = entry.label;
935 }
936
937 // Choose the max of the maxLines and maxLabelLines values.
938 maxLines = Math.max(maxLines, entry.maxLines);
939 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
940
941 // Choose the presence with the highest precedence.
942 if (Presence.getPresencePrecedence(status)
943 < Presence.getPresencePrecedence(entry.status)) {
944 status = entry.status;
945 }
946
947 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -0700948 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700949
950 // uri, and contactdId, shouldn't make a difference. Just keep the original.
951
952 // Keep track of all the ids that have been collapsed with this one.
953 ids.add(entry.id);
954 collapseCount++;
955 return true;
956 }
957
958 public String getCollapseKey() {
959 StringBuilder hashSb = new StringBuilder();
960 hashSb.append(data);
961 hashSb.append(mimetype);
962 hashSb.append((intent != null && intent.getAction() != null)
963 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -0700964 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
965 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -0700966 hashSb.append(actionIcon);
967 return hashSb.toString();
968 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800969 }
970
Evan Millar15e514d2009-08-04 10:14:57 -0700971 /** Cache of the children views of a row */
972 static class ViewCache {
973 public TextView label;
974 public TextView data;
975 public ImageView actionIcon;
976 public ImageView presenceIcon;
977 public ImageView primaryIcon;
978 public ImageView secondaryActionButton;
979 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700980
Evan Millar15e514d2009-08-04 10:14:57 -0700981 // Need to keep track of this too
982 ViewEntry entry;
983 }
984
985 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
986 implements View.OnClickListener {
987
Evan Millar5c22c3b2009-05-29 11:37:54 -0700988
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800989 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
990 super(context, sections, SHOW_SEPARATORS);
991 }
992
Evan Millar15e514d2009-08-04 10:14:57 -0700993 public void onClick(View v) {
994 Intent intent = (Intent) v.getTag();
995 startActivity(intent);
996 }
997
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800998 @Override
999 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001000 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001001 View v;
1002
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001003 ViewCache views;
1004
1005 // Check to see if we can reuse convertView
1006 if (convertView != null) {
1007 v = convertView;
1008 views = (ViewCache) v.getTag();
1009 } else {
1010 // Create a new view if needed
1011 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1012
1013 // Cache the children
1014 views = new ViewCache();
1015 views.label = (TextView) v.findViewById(android.R.id.text1);
1016 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001017 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1018 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1019 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1020 views.secondaryActionButton = (ImageView) v.findViewById(
1021 R.id.secondary_action_button);
1022 views.secondaryActionButton.setOnClickListener(this);
1023 views.secondaryActionButton.setFocusable(true);
1024 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001025 v.setTag(views);
1026 }
1027
1028 // Update the entry in the view cache
1029 views.entry = entry;
1030
1031 // Bind the data to the view
1032 bindView(v, entry);
1033 return v;
1034 }
1035
1036 @Override
1037 protected View newView(int position, ViewGroup parent) {
1038 // getView() handles this
1039 throw new UnsupportedOperationException();
1040 }
1041
1042 @Override
1043 protected void bindView(View view, ViewEntry entry) {
1044 final Resources resources = mContext.getResources();
1045 ViewCache views = (ViewCache) view.getTag();
1046
1047 // Set the label
1048 TextView label = views.label;
1049 setMaxLines(label, entry.maxLabelLines);
1050 label.setText(entry.label);
1051
1052 // Set the data
1053 TextView data = views.data;
1054 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001055 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1056 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1057 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1058 } else {
1059 data.setText(entry.data);
1060 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001061 setMaxLines(data, entry.maxLines);
1062 }
1063
Evan Millar15e514d2009-08-04 10:14:57 -07001064 // Set the primary icon
1065 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1066
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001067 // Set the action icon
1068 ImageView action = views.actionIcon;
1069 if (entry.actionIcon != -1) {
1070 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1071 action.setVisibility(View.VISIBLE);
1072 } else {
1073 // Things should still line up as if there was an icon, so make it invisible
1074 action.setVisibility(View.INVISIBLE);
1075 }
1076
1077 // Set the presence icon
1078 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001079 if (entry.presenceIcon != -1) {
1080 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001081 } else if (entry.status != -1) {
1082 presenceIcon = resources.getDrawable(
1083 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001084 }
Evan Millar15e514d2009-08-04 10:14:57 -07001085 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001086 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001087 presenceIconView.setImageDrawable(presenceIcon);
1088 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001089 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001090 presenceIconView.setVisibility(View.GONE);
1091 }
1092
1093 // Set the secondary action button
1094 ImageView secondaryActionView = views.secondaryActionButton;
1095 Drawable secondaryActionIcon = null;
1096 if (entry.secondaryActionIcon != -1) {
1097 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1098 }
1099 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1100 secondaryActionView.setImageDrawable(secondaryActionIcon);
1101 secondaryActionView.setTag(entry.secondaryIntent);
1102 secondaryActionView.setVisibility(View.VISIBLE);
1103 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1104 } else {
1105 secondaryActionView.setVisibility(View.GONE);
1106 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001107 }
1108 }
1109
1110 private void setMaxLines(TextView textView, int maxLines) {
1111 if (maxLines == 1) {
1112 textView.setSingleLine(true);
1113 textView.setEllipsize(TextUtils.TruncateAt.END);
1114 } else {
1115 textView.setSingleLine(false);
1116 textView.setMaxLines(maxLines);
1117 textView.setEllipsize(null);
1118 }
1119 }
1120 }
1121}