blob: beaea44ced253cd7e1d5dd2acdb9ed63264d5037 [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts;
18
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070019import static com.android.contacts.ContactEntryAdapter.CONTACT_PHOTO_ID;
20import static com.android.contacts.ContactEntryAdapter.CONTACT_PROJECTION;
21import static com.android.contacts.ContactEntryAdapter.CONTACT_STARRED_COLUMN;
Evan Millar66388be2009-05-28 15:41:07 -070022import static com.android.contacts.ContactEntryAdapter.DATA_1_COLUMN;
23import static com.android.contacts.ContactEntryAdapter.DATA_2_COLUMN;
24import static com.android.contacts.ContactEntryAdapter.DATA_3_COLUMN;
25import static com.android.contacts.ContactEntryAdapter.DATA_4_COLUMN;
26import static com.android.contacts.ContactEntryAdapter.DATA_5_COLUMN;
Evan Millar66388be2009-05-28 15:41:07 -070027import static com.android.contacts.ContactEntryAdapter.DATA_9_COLUMN;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070028import static com.android.contacts.ContactEntryAdapter.DATA_CONTACT_ID_COLUMN;
29import static com.android.contacts.ContactEntryAdapter.DATA_ID_COLUMN;
30import static com.android.contacts.ContactEntryAdapter.DATA_IS_SUPER_PRIMARY_COLUMN;
31import static com.android.contacts.ContactEntryAdapter.DATA_MIMETYPE_COLUMN;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080032
Evan Millar54a5c9f2009-06-23 17:41:09 -070033import com.android.contacts.Collapser.Collapsible;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070034import com.android.contacts.SplitAggregateView.OnContactSelectedListener;
Jeff Sharkey802b2052009-08-04 14:21:06 -070035import com.android.contacts.ui.FastTrackWindow;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080036import com.android.internal.telephony.ITelephony;
37
38import android.app.AlertDialog;
39import android.app.Dialog;
40import android.app.ListActivity;
41import android.content.ActivityNotFoundException;
42import android.content.ContentResolver;
43import android.content.ContentUris;
44import android.content.ContentValues;
45import android.content.Context;
46import android.content.DialogInterface;
Evan Millar5f4af702009-08-11 11:12:00 -070047import android.content.Entity;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080048import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070049import android.content.DialogInterface.OnClickListener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080050import android.content.pm.PackageManager;
51import android.content.pm.ResolveInfo;
52import android.content.res.Resources;
53import android.database.ContentObserver;
54import android.database.Cursor;
Evan Millar45e0ed32009-06-01 16:44:38 -070055import android.graphics.Bitmap;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080056import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080057import android.net.Uri;
58import android.os.Bundle;
59import android.os.Handler;
60import android.os.RemoteException;
61import android.os.ServiceManager;
62import android.os.SystemClock;
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070063import android.provider.ContactsContract.Contacts;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070064import android.provider.ContactsContract.AggregationExceptions;
65import android.provider.ContactsContract.CommonDataKinds;
Evan Millar66388be2009-05-28 15:41:07 -070066import android.provider.ContactsContract.Data;
Evan Millar54a5c9f2009-06-23 17:41:09 -070067import android.provider.ContactsContract.Presence;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070068import android.provider.ContactsContract.RawContacts;
Evan Millar54a5c9f2009-06-23 17:41:09 -070069import android.provider.ContactsContract.CommonDataKinds.Phone;
70import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080071import android.text.TextUtils;
72import android.util.Log;
73import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070074import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080075import android.view.KeyEvent;
Evan Millar7911ff52009-07-21 15:55:18 -070076import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080077import android.view.Menu;
78import android.view.MenuItem;
79import android.view.View;
80import android.view.ViewGroup;
81import android.view.ContextMenu.ContextMenuInfo;
82import android.widget.AdapterView;
83import android.widget.CheckBox;
Evan Millar7911ff52009-07-21 15:55:18 -070084import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080085import android.widget.ImageView;
86import android.widget.ListView;
87import android.widget.TextView;
88import android.widget.Toast;
89
90import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080091
92/**
93 * Displays the details of a specific contact.
94 */
Evan Millar7911ff52009-07-21 15:55:18 -070095public class ViewContactActivity extends BaseContactCardActivity
96 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
97 AdapterView.OnItemClickListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080098 private static final String TAG = "ViewContact";
99 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
100
101 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800102
103 private static final int DIALOG_CONFIRM_DELETE = 1;
104
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700105 private static final int REQUEST_JOIN_AGGREGATE = 1;
106
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800107 public static final int MENU_ITEM_DELETE = 1;
108 public static final int MENU_ITEM_MAKE_DEFAULT = 2;
109 public static final int MENU_ITEM_SHOW_BARCODE = 3;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700110 public static final int MENU_ITEM_SPLIT_AGGREGATE = 4;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700111 public static final int MENU_ITEM_JOIN_AGGREGATE = 5;
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700112 public static final int MENU_ITEM_OPTIONS = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800113
114 private Uri mUri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700115 private Uri mAggDataUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800116 private ContentResolver mResolver;
117 private ViewAdapter mAdapter;
118 private int mNumPhoneNumbers = 0;
119
Evan Millar7911ff52009-07-21 15:55:18 -0700120 private static final long ALL_CONTACTS_ID = -1;
121 private long mRawContactId = ALL_CONTACTS_ID;
122
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700123 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700124 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700125 */
Evan Millar7911ff52009-07-21 15:55:18 -0700126 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700127
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800128 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
129 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
130 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
131 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
132 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
133 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700134 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800135 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
136 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
137
138 private Cursor mCursor;
139 private boolean mObserverRegistered;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700140
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800141 private ContentObserver mObserver = new ContentObserver(new Handler()) {
142 @Override
143 public boolean deliverSelfNotifications() {
144 return true;
145 }
146
147 @Override
148 public void onChange(boolean selfChange) {
149 if (mCursor != null && !mCursor.isClosed()){
150 dataChanged();
151 }
152 }
153 };
154
155 public void onClick(DialogInterface dialog, int which) {
156 if (mCursor != null) {
157 if (mObserverRegistered) {
158 mCursor.unregisterContentObserver(mObserver);
159 mObserverRegistered = false;
160 }
161 mCursor.close();
162 mCursor = null;
163 }
164 getContentResolver().delete(mUri, null, null);
165 finish();
166 }
167
Evan Millar7911ff52009-07-21 15:55:18 -0700168 private FrameLayout mTabContentLayout;
169 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800170 private boolean mShowSmsLinksForAllPhones;
171
172 @Override
173 protected void onCreate(Bundle icicle) {
174 super.onCreate(icicle);
175
Evan Millar7911ff52009-07-21 15:55:18 -0700176 mListView = new ListView(this);
177 mListView.setOnCreateContextMenuListener(this);
178 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
179 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800180
Evan Millar7911ff52009-07-21 15:55:18 -0700181 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
182 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800183
Evan Millar45e0ed32009-06-01 16:44:38 -0700184 mUri = getIntent().getData();
185 mAggDataUri = Uri.withAppendedPath(mUri, "data");
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800186 mResolver = getContentResolver();
187
188 // Build the list of sections. The order they're added to mSections dictates the
189 // order they are displayed in the list.
190 mSections.add(mPhoneEntries);
191 mSections.add(mSmsEntries);
192 mSections.add(mEmailEntries);
193 mSections.add(mImEntries);
194 mSections.add(mPostalEntries);
195 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700196 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800197 mSections.add(mOtherEntries);
198
199 //TODO Read this value from a preference
200 mShowSmsLinksForAllPhones = true;
201
Evan Millar45e0ed32009-06-01 16:44:38 -0700202 mCursor = mResolver.query(mAggDataUri,
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700203 CONTACT_PROJECTION, null, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800204 }
205
206 @Override
207 protected void onResume() {
208 super.onResume();
209 mObserverRegistered = true;
210 mCursor.registerContentObserver(mObserver);
211 dataChanged();
212 }
213
214 @Override
215 protected void onPause() {
216 super.onPause();
217 if (mCursor != null) {
218 if (mObserverRegistered) {
219 mObserverRegistered = false;
220 mCursor.unregisterContentObserver(mObserver);
221 }
222 mCursor.deactivate();
223 }
224 }
225
226 @Override
227 protected void onDestroy() {
228 super.onDestroy();
229
230 if (mCursor != null) {
231 if (mObserverRegistered) {
232 mCursor.unregisterContentObserver(mObserver);
233 mObserverRegistered = false;
234 }
235 mCursor.close();
236 }
237 }
238
239 @Override
240 protected Dialog onCreateDialog(int id) {
241 switch (id) {
242 case DIALOG_CONFIRM_DELETE:
243 return new AlertDialog.Builder(this)
244 .setTitle(R.string.deleteConfirmation_title)
245 .setIcon(android.R.drawable.ic_dialog_alert)
246 .setMessage(R.string.deleteConfirmation)
247 .setNegativeButton(android.R.string.cancel, null)
248 .setPositiveButton(android.R.string.ok, this)
249 .setCancelable(false)
250 .create();
251 }
252 return null;
253 }
254
Evan Millar7911ff52009-07-21 15:55:18 -0700255 @Override
Evan Millar5f4af702009-08-11 11:12:00 -0700256 protected void bindTabs(ArrayList<Entity> entities) {
257 if (entities.size() > 1) {
Evan Millar7911ff52009-07-21 15:55:18 -0700258 addAllTab();
259 }
Evan Millar5f4af702009-08-11 11:12:00 -0700260 super.bindTabs(entities);
Evan Millar7911ff52009-07-21 15:55:18 -0700261 }
262
263 private void addAllTab() {
264 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator, mTabWidget, false);
Evan Millar74660912009-08-19 17:36:33 -0700265 allTabIndicator.getBackground().setDither(true);
Evan Millar7911ff52009-07-21 15:55:18 -0700266 addTab(ALL_CONTACTS_ID, allTabIndicator);
267 }
268
269 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
270 long rawContactId = getTabRawContactId(tabIndex);
271 mRawContactId = rawContactId;
272 dataChanged();
273 }
274
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800275 private void dataChanged() {
276 mCursor.requery();
277 if (mCursor.moveToFirst()) {
Evan Millar2c1cc832009-07-13 11:08:06 -0700278
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800279 // Build up the contact entries
280 buildEntries(mCursor);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700281
282 // Collapse similar data items in select sections.
283 Collapser.collapseList(mPhoneEntries);
284 Collapser.collapseList(mSmsEntries);
285 Collapser.collapseList(mEmailEntries);
286 Collapser.collapseList(mPostalEntries);
287
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800288 if (mAdapter == null) {
289 mAdapter = new ViewAdapter(this, mSections);
Evan Millar7911ff52009-07-21 15:55:18 -0700290 mListView.setAdapter(mAdapter);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800291 } else {
292 mAdapter.setSections(mSections, SHOW_SEPARATORS);
293 }
294 } else {
295 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
296 Log.e(TAG, "invalid contact uri: " + mUri);
297 finish();
298 }
299 }
300
301 @Override
302 public boolean onCreateOptionsMenu(Menu menu) {
303 menu.add(0, 0, 0, R.string.menu_editContact)
304 .setIcon(android.R.drawable.ic_menu_edit)
305 .setIntent(new Intent(Intent.ACTION_EDIT, mUri))
306 .setAlphabeticShortcut('e');
307 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
308 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700309 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
310 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700311 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
312 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700313 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
314 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800315 return true;
316 }
317
318 @Override
319 public boolean onPrepareOptionsMenu(Menu menu) {
320 super.onPrepareOptionsMenu(menu);
321 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
322 // could be installed or uninstalled at any time.
323 if (isBarcodeScannerInstalled()) {
324 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
325 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
326 .setIcon(R.drawable.ic_menu_show_barcode);
327 }
328 } else {
329 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
330 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700331
Evan Millar7911ff52009-07-21 15:55:18 -0700332 boolean isAggregate = mRawContactIds.size() > 1;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700333 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800334 return true;
335 }
336
337 private boolean isBarcodeScannerInstalled() {
338 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
339 ResolveInfo ri = getPackageManager().resolveActivity(intent,
340 PackageManager.MATCH_DEFAULT_ONLY);
341 return ri != null;
342 }
343
344 @Override
345 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
346 AdapterView.AdapterContextMenuInfo info;
347 try {
348 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
349 } catch (ClassCastException e) {
350 Log.e(TAG, "bad menuInfo", e);
351 return;
352 }
353
354 // This can be null sometimes, don't crash...
355 if (info == null) {
356 Log.e(TAG, "bad menuInfo");
357 return;
358 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700359
Evan Millar45e0ed32009-06-01 16:44:38 -0700360 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
361 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
362 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700363 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
364 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700365 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800366 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700367 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
368 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700369 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700370 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800371 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700372 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700373 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
374 }
375 // TODO(emillar): add back with group support.
376 /* else if (entry.mimetype.equals()) {
377 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
378 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800379 }
380
381 @Override
382 public boolean onOptionsItemSelected(MenuItem item) {
383 switch (item.getItemId()) {
384 case MENU_ITEM_DELETE: {
385 // Get confirmation
386 showDialog(DIALOG_CONFIRM_DELETE);
387 return true;
388 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700389
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700390 case MENU_ITEM_SPLIT_AGGREGATE: {
391 showSplitAggregateDialog();
392 return true;
393 }
394
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700395 case MENU_ITEM_JOIN_AGGREGATE: {
396 showJoinAggregateActivity();
397 return true;
398 }
399
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700400 case MENU_ITEM_OPTIONS: {
401 showOptionsActivity();
402 return true;
403 }
404
Evan Millar66388be2009-05-28 15:41:07 -0700405 // TODO(emillar) Bring this back.
406 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800407 if (mCursor.moveToFirst()) {
408 Intent intent = new Intent(SHOW_BARCODE_INTENT);
409 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
410 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700411 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800412 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700413 // Correctly handle when section headers are hidden
414 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700415
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800416 bundle.putString(Contacts.Intents.Insert.NAME, name);
417 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700418 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800419 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700420 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800421 bundle.putString(PHONE_KEYS[x], entry.data);
422 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700423 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800424 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700425 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800426 bundle.putString(EMAIL_KEYS[x], entry.data);
427 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700428 if (mPostalEntries.size() >= 1 + sepAdjust) {
429 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800430 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
431 }
432 intent.putExtra("ENCODE_DATA", bundle);
433 try {
434 startActivity(intent);
435 } catch (ActivityNotFoundException e) {
436 // The check in onPrepareOptionsMenu() should make this impossible, but
437 // for safety I'm catching the exception rather than crashing. Ideally
438 // I'd call Menu.removeItem() here too, but I don't see a way to get
439 // the options menu.
440 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
441 "was not installed.");
442 }
443 return true;
444 }
445 }
Evan Millar66388be2009-05-28 15:41:07 -0700446 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800447 }
448 return super.onOptionsItemSelected(item);
449 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700450
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800451 @Override
452 public boolean onContextItemSelected(MenuItem item) {
453 switch (item.getItemId()) {
454 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700455 if (makeItemDefault(item)) {
456 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800457 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700458 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800459 }
460 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700461
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800462 return super.onContextItemSelected(item);
463 }
464
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700465 private boolean makeItemDefault(MenuItem item) {
466 ViewEntry entry = getViewEntryForMenuItem(item);
467 if (entry == null) {
468 return false;
469 }
470
471 // Update the primary values in the data record.
472 ContentValues values = new ContentValues(2);
473 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700474
Evan Millar54a5c9f2009-06-23 17:41:09 -0700475 if (entry.ids.size() > 0) {
476 for (int i = 0; i < entry.ids.size(); i++) {
477 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
478 entry.ids.get(i)),
479 values, null, null);
480 }
481 }
482
483 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700484 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
485 values, null, null);
486 dataChanged();
487 return true;
488 }
489
490 /**
491 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
492 * The user picks a contact to be split into its own aggregate or clicks Cancel.
493 */
494 private void showSplitAggregateDialog() {
495
496 // Wrap this dialog in a specific theme so that list items have correct text color.
497 final ContextThemeWrapper dialogContext =
498 new ContextThemeWrapper(this, android.R.style.Theme_Light);
499 AlertDialog.Builder builder =
500 new AlertDialog.Builder(dialogContext);
501 builder.setTitle(getString(R.string.splitAggregate_title));
502
503 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
504 builder.setView(view);
505
506 builder.setInverseBackgroundForced(true);
507 builder.setCancelable(true);
508 builder.setNegativeButton(android.R.string.cancel,
509 new OnClickListener() {
510 public void onClick(DialogInterface dialog, int which) {
511 dialog.dismiss();
512 }
513 });
514 final AlertDialog dialog = builder.create();
515
516 view.setOnContactSelectedListener(new OnContactSelectedListener() {
517 public void onContactSelected(long contactId) {
518 dialog.dismiss();
519 splitContact(contactId);
520 }
521 });
522
523 dialog.show();
524 }
525
526 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700527 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700528 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700529 public void showJoinAggregateActivity() {
530 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
531 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
532 startActivityForResult(intent, REQUEST_JOIN_AGGREGATE);
533 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700534
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700535 @Override
536 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
537 if (requestCode == REQUEST_JOIN_AGGREGATE && resultCode == RESULT_OK && intent != null) {
538 final long aggregateId = ContentUris.parseId(intent.getData());
539 joinAggregate(aggregateId);
540 }
541 }
542
543 private void splitContact(long contactId) {
544 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_OUT);
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700545 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700546 mAdapter.notifyDataSetChanged();
547 }
548
549 private void joinAggregate(final long aggregateId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700550 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700551 RawContacts.CONTACT_ID + "=" + aggregateId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700552
553 try {
554 while(c.moveToNext()) {
555 long contactId = c.getLong(0);
556 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
557 }
558 } finally {
559 c.close();
560 }
561
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700562 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700563 mAdapter.notifyDataSetChanged();
564 }
565
566 /**
567 * Given a contact ID sets an aggregation exception to either join the contact with the
568 * current aggregate or split off.
569 */
570 protected void setAggregationException(long contactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700571 ContentValues values = new ContentValues(3);
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700572 values.put(AggregationExceptions.CONTACT_ID, ContentUris.parseId(mUri));
Jeff Sharkey14f61ab2009-08-05 21:02:37 -0700573 values.put(AggregationExceptions.RAW_CONTACT_ID, contactId);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700574 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700575 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700576 }
577
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700578 private void showOptionsActivity() {
579 final Intent intent = new Intent(this, ContactOptionsActivity.class);
580 intent.setData(mUri);
581 startActivity(intent);
582 }
583
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700584 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
585 AdapterView.AdapterContextMenuInfo info;
586 try {
587 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
588 } catch (ClassCastException e) {
589 Log.e(TAG, "bad menuInfo", e);
590 return null;
591 }
592
593 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
594 }
595
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800596 @Override
597 public boolean onKeyDown(int keyCode, KeyEvent event) {
598 switch (keyCode) {
599 case KeyEvent.KEYCODE_CALL: {
600 try {
601 ITelephony phone = ITelephony.Stub.asInterface(
602 ServiceManager.checkService("phone"));
603 if (phone != null && !phone.isIdle()) {
604 // Skip out and let the key be handled at a higher level
605 break;
606 }
607 } catch (RemoteException re) {
608 // Fall through and try to call the contact
609 }
610
Evan Millar7911ff52009-07-21 15:55:18 -0700611 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800612 if (index != -1) {
613 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700614 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
615 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800616 }
617 } else if (mNumPhoneNumbers != 0) {
618 // There isn't anything selected, call the default number
619 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
620 startActivity(intent);
621 }
622 return true;
623 }
624
625 case KeyEvent.KEYCODE_DEL: {
626 showDialog(DIALOG_CONFIRM_DELETE);
627 return true;
628 }
629 }
630
631 return super.onKeyDown(keyCode, event);
632 }
633
Evan Millar7911ff52009-07-21 15:55:18 -0700634 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800635 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
636 if (entry != null) {
637 Intent intent = entry.intent;
638 if (intent != null) {
639 try {
640 startActivity(intent);
641 } catch (ActivityNotFoundException e) {
642 Log.e(TAG, "No activity found for intent: " + intent);
643 signalError();
644 }
645 } else {
646 signalError();
647 }
648 } else {
649 signalError();
650 }
651 }
652
653 /**
654 * Signal an error to the user via a beep, or some other method.
655 */
656 private void signalError() {
657 //TODO: implement this when we have the sonification APIs
658 }
659
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800660 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700661 // 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 -0800662 StringBuilder buf = new StringBuilder("imto://");
663 buf.append(host);
664 buf.append('/');
665 buf.append(data);
666 return Uri.parse(buf.toString());
667 }
668
669 /**
670 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700671 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800672 * @param personCursor the URI for the contact being displayed
673 */
Evan Millar66388be2009-05-28 15:41:07 -0700674 private final void buildEntries(Cursor aggCursor) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800675 // Clear out the old entries
676 final int numSections = mSections.size();
677 for (int i = 0; i < numSections; i++) {
678 mSections.get(i).clear();
679 }
680
Evan Millar7911ff52009-07-21 15:55:18 -0700681 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700682
Evan Millar66388be2009-05-28 15:41:07 -0700683 // Build up method entries
684 if (mUri != null) {
Jeff Hamilton119b6ca2009-07-30 12:57:49 -0500685 aggCursor.moveToPosition(-1);
Evan Millar66388be2009-05-28 15:41:07 -0700686 while (aggCursor.moveToNext()) {
687 final String mimetype = aggCursor.getString(DATA_MIMETYPE_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700688
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800689 ViewEntry entry = new ViewEntry();
Evan Millar66388be2009-05-28 15:41:07 -0700690
691 final long id = aggCursor.getLong(DATA_ID_COLUMN);
692 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800693 entry.id = id;
694 entry.uri = uri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700695 entry.mimetype = mimetype;
Evan Millar7911ff52009-07-21 15:55:18 -0700696 // TODO: entry.contactId should be renamed to entry.rawContactId
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700697 entry.contactId = aggCursor.getLong(DATA_CONTACT_ID_COLUMN);
Evan Millar7911ff52009-07-21 15:55:18 -0700698 if (!mRawContactIds.contains(entry.contactId)) {
699 mRawContactIds.add(entry.contactId);
700 }
701
702 // This performs the tab filtering
703 if (mRawContactId != entry.contactId && mRawContactId != ALL_CONTACTS_ID) {
704 continue;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700705 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800706
Evan Millar66388be2009-05-28 15:41:07 -0700707 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
Evan Millar5c22c3b2009-05-29 11:37:54 -0700708 || mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700709 || mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
Evan Millar66388be2009-05-28 15:41:07 -0700710 || mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
711 final int type = aggCursor.getInt(DATA_1_COLUMN);
Evan Millar45e0ed32009-06-01 16:44:38 -0700712 final String label = aggCursor.getString(DATA_3_COLUMN);
713 final String data = aggCursor.getString(DATA_2_COLUMN);
714 final boolean isSuperPrimary = "1".equals(
715 aggCursor.getString(DATA_IS_SUPER_PRIMARY_COLUMN));
Evan Millar66388be2009-05-28 15:41:07 -0700716
Evan Millar54a5c9f2009-06-23 17:41:09 -0700717 entry.type = type;
718
Evan Millar66388be2009-05-28 15:41:07 -0700719 // Don't crash if the data is bogus
720 if (TextUtils.isEmpty(data)) {
721 Log.w(TAG, "empty data for contact method " + id);
722 continue;
723 }
724
725 // Build phone entries
726 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
727 mNumPhoneNumbers++;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700728
Evan Millar66388be2009-05-28 15:41:07 -0700729 final CharSequence displayLabel = ContactsUtils.getDisplayLabel(
730 this, mimetype, type, label);
731 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700732 entry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar296758b2009-08-07 15:40:08 -0700733 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
734 Uri.fromParts("tel", data, null));
Evan Millar15e514d2009-08-04 10:14:57 -0700735 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
Evan Millar66388be2009-05-28 15:41:07 -0700736 Uri.fromParts("sms", data, null));
Evan Millar15e514d2009-08-04 10:14:57 -0700737 entry.isPrimary = isSuperPrimary;
Evan Millar66388be2009-05-28 15:41:07 -0700738 entry.actionIcon = android.R.drawable.sym_action_call;
739 mPhoneEntries.add(entry);
740
Evan Millar5c22c3b2009-05-29 11:37:54 -0700741 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700742 || mShowSmsLinksForAllPhones) {
743 // Add an SMS entry
Evan Millar15e514d2009-08-04 10:14:57 -0700744 entry.secondaryActionIcon = R.drawable.sym_action_sms;
Evan Millar66388be2009-05-28 15:41:07 -0700745 }
746 // Build email entries
747 } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800748 entry.label = buildActionString(R.string.actionEmail,
Evan Millar66388be2009-05-28 15:41:07 -0700749 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800750 entry.data = data;
751 entry.intent = new Intent(Intent.ACTION_SENDTO,
752 Uri.fromParts("mailto", data, null));
753 entry.actionIcon = android.R.drawable.sym_action_email;
Evan Millar15e514d2009-08-04 10:14:57 -0700754 entry.isPrimary = isSuperPrimary;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800755 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700756 // Build postal entries
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700757 } else if (mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800758 entry.label = buildActionString(R.string.actionMap,
Evan Millar66388be2009-05-28 15:41:07 -0700759 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800760 entry.data = data;
761 entry.maxLines = 4;
762 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
763 entry.actionIcon = R.drawable.sym_action_map;
764 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700765 // Build im entries
766 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
767 String[] protocolStrings = getResources().getStringArray(
768 android.R.array.imProtocols);
Jeff Sharkeye731d422009-07-28 21:12:43 -0700769 Object protocolObj = aggCursor.getString(DATA_5_COLUMN);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700770 String host = null;
Jeff Sharkeye731d422009-07-28 21:12:43 -0700771 // TODO: fix by moving to contactssource-based rendering rules
772// Object protocolObj = ContactsUtils.decodeImProtocol(
773// aggCursor.getString(DATA_5_COLUMN));
774// if (protocolObj instanceof Number) {
775// int protocol = ((Number) protocolObj).intValue();
776// entry.label = buildActionString(R.string.actionChat,
777// protocolStrings[protocol], false);
778// host = ContactsUtils.lookupProviderNameFromId(
779// protocol).toLowerCase();
780// if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
781// || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
782// entry.maxLabelLines = 2;
783// }
Neel Parekh064fa802009-08-12 12:57:12 -0700784 if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800785 String providerName = (String) protocolObj;
786 entry.label = buildActionString(R.string.actionChat,
787 providerName, false);
788 host = providerName.toLowerCase();
Neel Parekh064fa802009-08-12 12:57:12 -0700789 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800790
791 // Only add the intent if there is a valid host
792 if (!TextUtils.isEmpty(host)) {
793 entry.intent = new Intent(Intent.ACTION_SENDTO,
794 constructImToUrl(host, data));
795 }
796 entry.data = data;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700797 //TODO(emillar) Add in presence info
Evan Millar66388be2009-05-28 15:41:07 -0700798 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800799 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700800 aggCursor.getInt(METHODS_STATUS_COLUMN));
Evan Millar54a5c9f2009-06-23 17:41:09 -0700801 entry.status = ...
Evan Millar66388be2009-05-28 15:41:07 -0700802 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800803 entry.actionIcon = android.R.drawable.sym_action_chat;
804 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700805 }
Evan Millar66388be2009-05-28 15:41:07 -0700806 // Build organization entries
807 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
Evan Millar66388be2009-05-28 15:41:07 -0700808 final String company = aggCursor.getString(DATA_3_COLUMN);
809 final String title = aggCursor.getString(DATA_4_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700810
Evan Millar66388be2009-05-28 15:41:07 -0700811 // Don't crash if the data is bogus
812 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
813 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800814 continue;
815 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700816
Evan Millar66388be2009-05-28 15:41:07 -0700817 entry.data = title;
818 entry.actionIcon = R.drawable.sym_action_organization;
819 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800820
Evan Millar66388be2009-05-28 15:41:07 -0700821 mOrganizationEntries.add(entry);
822 // Build note entries
823 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
824 entry.label = getString(R.string.label_notes);
825 entry.data = aggCursor.getString(DATA_1_COLUMN);
826 entry.id = 0;
827 entry.uri = null;
828 entry.intent = null;
829 entry.maxLines = 10;
830 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800831
Evan Millar66388be2009-05-28 15:41:07 -0700832 if (TextUtils.isEmpty(entry.data)) {
833 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700834 continue;
835 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700836
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800837 mOtherEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700838 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700839
Evan Millar45e0ed32009-06-01 16:44:38 -0700840
Evan Millar66388be2009-05-28 15:41:07 -0700841 // TODO(emillar) Add group entries
842// // Build the group entries
843// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
844// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
845// null, null, Groups.DEFAULT_SORT_ORDER);
846// if (groupCursor != null) {
847// try {
848// StringBuilder sb = new StringBuilder();
849//
850// while (groupCursor.moveToNext()) {
851// String systemId = groupCursor.getString(
852// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
853//
854// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
855// continue;
856// }
857//
858// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
859// if (!TextUtils.isEmpty(name)) {
860// if (sb.length() == 0) {
861// sb.append(name);
862// } else {
863// sb.append(getString(R.string.group_list, name));
864// }
865// }
866// }
867//
868// if (sb.length() > 0) {
869// ViewEntry entry = new ViewEntry();
870// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
871// entry.label = getString(R.string.label_groups);
872// entry.data = sb.toString();
873// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
874//
875// // TODO: Add an icon for the groups item.
876//
877// mGroupEntries.add(entry);
878// }
879// } finally {
880// groupCursor.close();
881// }
882// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700883
Evan Millar66388be2009-05-28 15:41:07 -0700884 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800885 }
886 }
887
888 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700889 // If there is no type just display an empty string
890 if (type == null) {
891 type = "";
892 }
893
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800894 if (lowerCase) {
895 return getString(actionResId, type.toString().toLowerCase());
896 } else {
897 return getString(actionResId, type.toString());
898 }
899 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700900
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800901 /**
902 * A basic structure with the data for a contact entry in the list.
903 */
Evan Millar54a5c9f2009-06-23 17:41:09 -0700904 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800905 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -0700906 public boolean isPrimary = false;
907 public int presenceIcon = -1;
908 public int secondaryActionIcon = -1;
909 public Intent intent;
910 public Intent secondaryIntent = null;
911 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800912 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700913 public ArrayList<Long> ids = new ArrayList<Long>();
914 public int collapseCount = 0;
915
916 public boolean collapseWith(ViewEntry entry) {
917 // assert equal collapse keys
918 if (!getCollapseKey().equals(entry.getCollapseKey())) {
919 return false;
920 }
921
922 // Choose the label associated with the highest type precedence.
923 if (TypePrecedence.getTypePrecedence(mimetype, type)
924 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
925 type = entry.type;
926 label = entry.label;
927 }
928
929 // Choose the max of the maxLines and maxLabelLines values.
930 maxLines = Math.max(maxLines, entry.maxLines);
931 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
932
933 // Choose the presence with the highest precedence.
934 if (Presence.getPresencePrecedence(status)
935 < Presence.getPresencePrecedence(entry.status)) {
936 status = entry.status;
937 }
938
939 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -0700940 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700941
942 // uri, and contactdId, shouldn't make a difference. Just keep the original.
943
944 // Keep track of all the ids that have been collapsed with this one.
945 ids.add(entry.id);
946 collapseCount++;
947 return true;
948 }
949
950 public String getCollapseKey() {
951 StringBuilder hashSb = new StringBuilder();
952 hashSb.append(data);
953 hashSb.append(mimetype);
954 hashSb.append((intent != null && intent.getAction() != null)
955 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -0700956 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
957 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -0700958 hashSb.append(actionIcon);
959 return hashSb.toString();
960 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800961 }
962
Evan Millar15e514d2009-08-04 10:14:57 -0700963 /** Cache of the children views of a row */
964 static class ViewCache {
965 public TextView label;
966 public TextView data;
967 public ImageView actionIcon;
968 public ImageView presenceIcon;
969 public ImageView primaryIcon;
970 public ImageView secondaryActionButton;
971 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700972
Evan Millar15e514d2009-08-04 10:14:57 -0700973 // Need to keep track of this too
974 ViewEntry entry;
975 }
976
977 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
978 implements View.OnClickListener {
979
Evan Millar5c22c3b2009-05-29 11:37:54 -0700980
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800981 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
982 super(context, sections, SHOW_SEPARATORS);
983 }
984
Evan Millar15e514d2009-08-04 10:14:57 -0700985 public void onClick(View v) {
986 Intent intent = (Intent) v.getTag();
987 startActivity(intent);
988 }
989
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800990 @Override
991 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -0700992 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800993 View v;
994
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800995 ViewCache views;
996
997 // Check to see if we can reuse convertView
998 if (convertView != null) {
999 v = convertView;
1000 views = (ViewCache) v.getTag();
1001 } else {
1002 // Create a new view if needed
1003 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1004
1005 // Cache the children
1006 views = new ViewCache();
1007 views.label = (TextView) v.findViewById(android.R.id.text1);
1008 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001009 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1010 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1011 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1012 views.secondaryActionButton = (ImageView) v.findViewById(
1013 R.id.secondary_action_button);
1014 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001015 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001016 v.setTag(views);
1017 }
1018
1019 // Update the entry in the view cache
1020 views.entry = entry;
1021
1022 // Bind the data to the view
1023 bindView(v, entry);
1024 return v;
1025 }
1026
1027 @Override
1028 protected View newView(int position, ViewGroup parent) {
1029 // getView() handles this
1030 throw new UnsupportedOperationException();
1031 }
1032
1033 @Override
1034 protected void bindView(View view, ViewEntry entry) {
1035 final Resources resources = mContext.getResources();
1036 ViewCache views = (ViewCache) view.getTag();
1037
1038 // Set the label
1039 TextView label = views.label;
1040 setMaxLines(label, entry.maxLabelLines);
1041 label.setText(entry.label);
1042
1043 // Set the data
1044 TextView data = views.data;
1045 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001046 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1047 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1048 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1049 } else {
1050 data.setText(entry.data);
1051 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001052 setMaxLines(data, entry.maxLines);
1053 }
1054
Evan Millar15e514d2009-08-04 10:14:57 -07001055 // Set the primary icon
1056 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1057
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001058 // Set the action icon
1059 ImageView action = views.actionIcon;
1060 if (entry.actionIcon != -1) {
1061 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1062 action.setVisibility(View.VISIBLE);
1063 } else {
1064 // Things should still line up as if there was an icon, so make it invisible
1065 action.setVisibility(View.INVISIBLE);
1066 }
1067
1068 // Set the presence icon
1069 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001070 if (entry.presenceIcon != -1) {
1071 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001072 } else if (entry.status != -1) {
1073 presenceIcon = resources.getDrawable(
1074 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001075 }
Evan Millar15e514d2009-08-04 10:14:57 -07001076 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001077 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001078 presenceIconView.setImageDrawable(presenceIcon);
1079 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001080 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001081 presenceIconView.setVisibility(View.GONE);
1082 }
1083
1084 // Set the secondary action button
1085 ImageView secondaryActionView = views.secondaryActionButton;
1086 Drawable secondaryActionIcon = null;
1087 if (entry.secondaryActionIcon != -1) {
1088 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1089 }
1090 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1091 secondaryActionView.setImageDrawable(secondaryActionIcon);
1092 secondaryActionView.setTag(entry.secondaryIntent);
1093 secondaryActionView.setVisibility(View.VISIBLE);
1094 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1095 } else {
1096 secondaryActionView.setVisibility(View.GONE);
1097 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001098 }
1099 }
1100
1101 private void setMaxLines(TextView textView, int maxLines) {
1102 if (maxLines == 1) {
1103 textView.setSingleLine(true);
1104 textView.setEllipsize(TextUtils.TruncateAt.END);
1105 } else {
1106 textView.setSingleLine(false);
1107 textView.setMaxLines(maxLines);
1108 textView.setEllipsize(null);
1109 }
1110 }
1111 }
1112}