blob: 9dfa551c25c4bc8139b78dc069acedb4549cd64e [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;
47import android.content.Intent;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070048import android.content.DialogInterface.OnClickListener;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080049import android.content.pm.PackageManager;
50import android.content.pm.ResolveInfo;
51import android.content.res.Resources;
52import android.database.ContentObserver;
53import android.database.Cursor;
Evan Millar45e0ed32009-06-01 16:44:38 -070054import android.graphics.Bitmap;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080055import android.graphics.drawable.Drawable;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080056import android.net.Uri;
57import android.os.Bundle;
58import android.os.Handler;
59import android.os.RemoteException;
60import android.os.ServiceManager;
61import android.os.SystemClock;
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -070062import android.provider.ContactsContract.Contacts;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070063import android.provider.ContactsContract.AggregationExceptions;
64import android.provider.ContactsContract.CommonDataKinds;
Evan Millar66388be2009-05-28 15:41:07 -070065import android.provider.ContactsContract.Data;
Evan Millar54a5c9f2009-06-23 17:41:09 -070066import android.provider.ContactsContract.Presence;
Dmitri Plotnikov39466592009-07-27 11:23:51 -070067import android.provider.ContactsContract.RawContacts;
Evan Millar54a5c9f2009-06-23 17:41:09 -070068import android.provider.ContactsContract.CommonDataKinds.Phone;
69import android.telephony.PhoneNumberUtils;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080070import android.text.TextUtils;
71import android.util.Log;
72import android.view.ContextMenu;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -070073import android.view.ContextThemeWrapper;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080074import android.view.KeyEvent;
Evan Millar7911ff52009-07-21 15:55:18 -070075import android.view.LayoutInflater;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080076import android.view.Menu;
77import android.view.MenuItem;
78import android.view.View;
79import android.view.ViewGroup;
80import android.view.ContextMenu.ContextMenuInfo;
81import android.widget.AdapterView;
82import android.widget.CheckBox;
Evan Millar7911ff52009-07-21 15:55:18 -070083import android.widget.FrameLayout;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080084import android.widget.ImageView;
85import android.widget.ListView;
86import android.widget.TextView;
87import android.widget.Toast;
88
89import java.util.ArrayList;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080090
91/**
92 * Displays the details of a specific contact.
93 */
Evan Millar7911ff52009-07-21 15:55:18 -070094public class ViewContactActivity extends BaseContactCardActivity
95 implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener,
96 AdapterView.OnItemClickListener {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080097 private static final String TAG = "ViewContact";
98 private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE";
99
100 private static final boolean SHOW_SEPARATORS = false;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800101
102 private static final int DIALOG_CONFIRM_DELETE = 1;
103
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700104 private static final int REQUEST_JOIN_AGGREGATE = 1;
105
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800106 public static final int MENU_ITEM_DELETE = 1;
107 public static final int MENU_ITEM_MAKE_DEFAULT = 2;
108 public static final int MENU_ITEM_SHOW_BARCODE = 3;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700109 public static final int MENU_ITEM_SPLIT_AGGREGATE = 4;
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700110 public static final int MENU_ITEM_JOIN_AGGREGATE = 5;
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700111 public static final int MENU_ITEM_OPTIONS = 6;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800112
113 private Uri mUri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700114 private Uri mAggDataUri;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800115 private ContentResolver mResolver;
116 private ViewAdapter mAdapter;
117 private int mNumPhoneNumbers = 0;
118
Evan Millar7911ff52009-07-21 15:55:18 -0700119 private static final long ALL_CONTACTS_ID = -1;
120 private long mRawContactId = ALL_CONTACTS_ID;
121
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700122 /**
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700123 * A list of distinct contact IDs included in the current contact.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700124 */
Evan Millar7911ff52009-07-21 15:55:18 -0700125 private ArrayList<Long> mRawContactIds = new ArrayList<Long>();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700126
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800127 /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>();
128 /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>();
129 /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>();
130 /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>();
131 /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>();
132 /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>();
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700133 /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800134 /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>();
135 /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>();
136
137 private Cursor mCursor;
138 private boolean mObserverRegistered;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700139
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800140 private ContentObserver mObserver = new ContentObserver(new Handler()) {
141 @Override
142 public boolean deliverSelfNotifications() {
143 return true;
144 }
145
146 @Override
147 public void onChange(boolean selfChange) {
148 if (mCursor != null && !mCursor.isClosed()){
149 dataChanged();
150 }
151 }
152 };
153
154 public void onClick(DialogInterface dialog, int which) {
155 if (mCursor != null) {
156 if (mObserverRegistered) {
157 mCursor.unregisterContentObserver(mObserver);
158 mObserverRegistered = false;
159 }
160 mCursor.close();
161 mCursor = null;
162 }
163 getContentResolver().delete(mUri, null, null);
164 finish();
165 }
166
Evan Millar7911ff52009-07-21 15:55:18 -0700167 private FrameLayout mTabContentLayout;
168 private ListView mListView;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800169 private boolean mShowSmsLinksForAllPhones;
170
171 @Override
172 protected void onCreate(Bundle icicle) {
173 super.onCreate(icicle);
174
Evan Millar7911ff52009-07-21 15:55:18 -0700175 mListView = new ListView(this);
176 mListView.setOnCreateContextMenuListener(this);
177 mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY);
178 mListView.setOnItemClickListener(this);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800179
Evan Millar7911ff52009-07-21 15:55:18 -0700180 mTabContentLayout = (FrameLayout) findViewById(android.R.id.tabcontent);
181 mTabContentLayout.addView(mListView);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800182
Evan Millar45e0ed32009-06-01 16:44:38 -0700183 mUri = getIntent().getData();
184 mAggDataUri = Uri.withAppendedPath(mUri, "data");
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800185 mResolver = getContentResolver();
186
187 // Build the list of sections. The order they're added to mSections dictates the
188 // order they are displayed in the list.
189 mSections.add(mPhoneEntries);
190 mSections.add(mSmsEntries);
191 mSections.add(mEmailEntries);
192 mSections.add(mImEntries);
193 mSections.add(mPostalEntries);
194 mSections.add(mOrganizationEntries);
The Android Open Source Projectcac191e2009-03-18 22:20:27 -0700195 mSections.add(mGroupEntries);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800196 mSections.add(mOtherEntries);
197
198 //TODO Read this value from a preference
199 mShowSmsLinksForAllPhones = true;
200
Evan Millar45e0ed32009-06-01 16:44:38 -0700201 mCursor = mResolver.query(mAggDataUri,
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700202 CONTACT_PROJECTION, null, null, null);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800203 }
204
205 @Override
206 protected void onResume() {
207 super.onResume();
208 mObserverRegistered = true;
209 mCursor.registerContentObserver(mObserver);
210 dataChanged();
211 }
212
213 @Override
214 protected void onPause() {
215 super.onPause();
216 if (mCursor != null) {
217 if (mObserverRegistered) {
218 mObserverRegistered = false;
219 mCursor.unregisterContentObserver(mObserver);
220 }
221 mCursor.deactivate();
222 }
223 }
224
225 @Override
226 protected void onDestroy() {
227 super.onDestroy();
228
229 if (mCursor != null) {
230 if (mObserverRegistered) {
231 mCursor.unregisterContentObserver(mObserver);
232 mObserverRegistered = false;
233 }
234 mCursor.close();
235 }
236 }
237
238 @Override
239 protected Dialog onCreateDialog(int id) {
240 switch (id) {
241 case DIALOG_CONFIRM_DELETE:
242 return new AlertDialog.Builder(this)
243 .setTitle(R.string.deleteConfirmation_title)
244 .setIcon(android.R.drawable.ic_dialog_alert)
245 .setMessage(R.string.deleteConfirmation)
246 .setNegativeButton(android.R.string.cancel, null)
247 .setPositiveButton(android.R.string.ok, this)
248 .setCancelable(false)
249 .create();
250 }
251 return null;
252 }
253
Evan Millar7911ff52009-07-21 15:55:18 -0700254 @Override
255 protected void bindTabs(Cursor tabsCursor) {
256 if (tabsCursor.getCount() > 1) {
257 addAllTab();
258 }
259 super.bindTabs(tabsCursor);
260 }
261
262 private void addAllTab() {
263 View allTabIndicator = mInflater.inflate(R.layout.all_tab_indicator, mTabWidget, false);
264 addTab(ALL_CONTACTS_ID, allTabIndicator);
265 }
266
267 public void onTabSelectionChanged(int tabIndex, boolean clicked) {
268 long rawContactId = getTabRawContactId(tabIndex);
269 mRawContactId = rawContactId;
270 dataChanged();
271 }
272
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800273 private void dataChanged() {
274 mCursor.requery();
275 if (mCursor.moveToFirst()) {
Evan Millar2c1cc832009-07-13 11:08:06 -0700276
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800277 // Build up the contact entries
278 buildEntries(mCursor);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700279
280 // Collapse similar data items in select sections.
281 Collapser.collapseList(mPhoneEntries);
282 Collapser.collapseList(mSmsEntries);
283 Collapser.collapseList(mEmailEntries);
284 Collapser.collapseList(mPostalEntries);
285
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800286 if (mAdapter == null) {
287 mAdapter = new ViewAdapter(this, mSections);
Evan Millar7911ff52009-07-21 15:55:18 -0700288 mListView.setAdapter(mAdapter);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800289 } else {
290 mAdapter.setSections(mSections, SHOW_SEPARATORS);
291 }
292 } else {
293 Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show();
294 Log.e(TAG, "invalid contact uri: " + mUri);
295 finish();
296 }
297 }
298
299 @Override
300 public boolean onCreateOptionsMenu(Menu menu) {
301 menu.add(0, 0, 0, R.string.menu_editContact)
302 .setIcon(android.R.drawable.ic_menu_edit)
303 .setIntent(new Intent(Intent.ACTION_EDIT, mUri))
304 .setAlphabeticShortcut('e');
305 menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact)
306 .setIcon(android.R.drawable.ic_menu_delete);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700307 menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate)
308 .setIcon(android.R.drawable.ic_menu_share);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700309 menu.add(0, MENU_ITEM_JOIN_AGGREGATE, 0, R.string.menu_joinAggregate)
310 .setIcon(android.R.drawable.ic_menu_add);
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700311 menu.add(0, MENU_ITEM_OPTIONS, 0, R.string.menu_contactOptions)
312 .setIcon(R.drawable.ic_menu_mark);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800313 return true;
314 }
315
316 @Override
317 public boolean onPrepareOptionsMenu(Menu menu) {
318 super.onPrepareOptionsMenu(menu);
319 // Perform this check each time the menu is about to be shown, because the Barcode Scanner
320 // could be installed or uninstalled at any time.
321 if (isBarcodeScannerInstalled()) {
322 if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) {
323 menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode)
324 .setIcon(R.drawable.ic_menu_show_barcode);
325 }
326 } else {
327 menu.removeItem(MENU_ITEM_SHOW_BARCODE);
328 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700329
Evan Millar7911ff52009-07-21 15:55:18 -0700330 boolean isAggregate = mRawContactIds.size() > 1;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700331 menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800332 return true;
333 }
334
335 private boolean isBarcodeScannerInstalled() {
336 final Intent intent = new Intent(SHOW_BARCODE_INTENT);
337 ResolveInfo ri = getPackageManager().resolveActivity(intent,
338 PackageManager.MATCH_DEFAULT_ONLY);
339 return ri != null;
340 }
341
342 @Override
343 public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
344 AdapterView.AdapterContextMenuInfo info;
345 try {
346 info = (AdapterView.AdapterContextMenuInfo) menuInfo;
347 } catch (ClassCastException e) {
348 Log.e(TAG, "bad menuInfo", e);
349 return;
350 }
351
352 // This can be null sometimes, don't crash...
353 if (info == null) {
354 Log.e(TAG, "bad menuInfo");
355 return;
356 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700357
Evan Millar45e0ed32009-06-01 16:44:38 -0700358 ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
359 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
360 menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700361 menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent);
362 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700363 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800364 }
Evan Millar45e0ed32009-06-01 16:44:38 -0700365 } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
366 menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent);
Evan Millar15e514d2009-08-04 10:14:57 -0700367 if (!entry.isPrimary) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700368 menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800369 }
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700370 } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700371 menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent);
372 }
373 // TODO(emillar): add back with group support.
374 /* else if (entry.mimetype.equals()) {
375 menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent);
376 } */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800377 }
378
379 @Override
380 public boolean onOptionsItemSelected(MenuItem item) {
381 switch (item.getItemId()) {
382 case MENU_ITEM_DELETE: {
383 // Get confirmation
384 showDialog(DIALOG_CONFIRM_DELETE);
385 return true;
386 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700387
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700388 case MENU_ITEM_SPLIT_AGGREGATE: {
389 showSplitAggregateDialog();
390 return true;
391 }
392
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700393 case MENU_ITEM_JOIN_AGGREGATE: {
394 showJoinAggregateActivity();
395 return true;
396 }
397
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700398 case MENU_ITEM_OPTIONS: {
399 showOptionsActivity();
400 return true;
401 }
402
Evan Millar66388be2009-05-28 15:41:07 -0700403 // TODO(emillar) Bring this back.
404 /*case MENU_ITEM_SHOW_BARCODE:
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800405 if (mCursor.moveToFirst()) {
406 Intent intent = new Intent(SHOW_BARCODE_INTENT);
407 intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE");
408 Bundle bundle = new Bundle();
Evan Millar66388be2009-05-28 15:41:07 -0700409 String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800410 if (!TextUtils.isEmpty(name)) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700411 // Correctly handle when section headers are hidden
412 int sepAdjust = SHOW_SEPARATORS ? 1 : 0;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700413
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800414 bundle.putString(Contacts.Intents.Insert.NAME, name);
415 // The 0th ViewEntry in each ArrayList below is a separator item
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700416 int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800417 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700418 ViewEntry entry = mPhoneEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800419 bundle.putString(PHONE_KEYS[x], entry.data);
420 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700421 entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800422 for (int x = 0; x < entriesToAdd; x++) {
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700423 ViewEntry entry = mEmailEntries.get(x + sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800424 bundle.putString(EMAIL_KEYS[x], entry.data);
425 }
Jeffrey Sharkey715b32a2009-03-27 18:56:05 -0700426 if (mPostalEntries.size() >= 1 + sepAdjust) {
427 ViewEntry entry = mPostalEntries.get(sepAdjust);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800428 bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data);
429 }
430 intent.putExtra("ENCODE_DATA", bundle);
431 try {
432 startActivity(intent);
433 } catch (ActivityNotFoundException e) {
434 // The check in onPrepareOptionsMenu() should make this impossible, but
435 // for safety I'm catching the exception rather than crashing. Ideally
436 // I'd call Menu.removeItem() here too, but I don't see a way to get
437 // the options menu.
438 Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " +
439 "was not installed.");
440 }
441 return true;
442 }
443 }
Evan Millar66388be2009-05-28 15:41:07 -0700444 break; */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800445 }
446 return super.onOptionsItemSelected(item);
447 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700448
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800449 @Override
450 public boolean onContextItemSelected(MenuItem item) {
451 switch (item.getItemId()) {
452 case MENU_ITEM_MAKE_DEFAULT: {
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700453 if (makeItemDefault(item)) {
454 return true;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800455 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700456 break;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800457 }
458 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700459
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800460 return super.onContextItemSelected(item);
461 }
462
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700463 private boolean makeItemDefault(MenuItem item) {
464 ViewEntry entry = getViewEntryForMenuItem(item);
465 if (entry == null) {
466 return false;
467 }
468
469 // Update the primary values in the data record.
470 ContentValues values = new ContentValues(2);
471 values.put(Data.IS_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700472
Evan Millar54a5c9f2009-06-23 17:41:09 -0700473 if (entry.ids.size() > 0) {
474 for (int i = 0; i < entry.ids.size(); i++) {
475 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI,
476 entry.ids.get(i)),
477 values, null, null);
478 }
479 }
480
481 values.put(Data.IS_SUPER_PRIMARY, 1);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700482 getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id),
483 values, null, null);
484 dataChanged();
485 return true;
486 }
487
488 /**
489 * Shows a dialog that contains a list of all constituent contacts in this aggregate.
490 * The user picks a contact to be split into its own aggregate or clicks Cancel.
491 */
492 private void showSplitAggregateDialog() {
493
494 // Wrap this dialog in a specific theme so that list items have correct text color.
495 final ContextThemeWrapper dialogContext =
496 new ContextThemeWrapper(this, android.R.style.Theme_Light);
497 AlertDialog.Builder builder =
498 new AlertDialog.Builder(dialogContext);
499 builder.setTitle(getString(R.string.splitAggregate_title));
500
501 final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri);
502 builder.setView(view);
503
504 builder.setInverseBackgroundForced(true);
505 builder.setCancelable(true);
506 builder.setNegativeButton(android.R.string.cancel,
507 new OnClickListener() {
508 public void onClick(DialogInterface dialog, int which) {
509 dialog.dismiss();
510 }
511 });
512 final AlertDialog dialog = builder.create();
513
514 view.setOnContactSelectedListener(new OnContactSelectedListener() {
515 public void onContactSelected(long contactId) {
516 dialog.dismiss();
517 splitContact(contactId);
518 }
519 });
520
521 dialog.show();
522 }
523
524 /**
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700525 * Shows a list of aggregates that can be joined into the currently viewed aggregate.
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700526 */
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700527 public void showJoinAggregateActivity() {
528 Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE);
529 intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, ContentUris.parseId(mUri));
530 startActivityForResult(intent, REQUEST_JOIN_AGGREGATE);
531 }
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700532
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700533 @Override
534 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
535 if (requestCode == REQUEST_JOIN_AGGREGATE && resultCode == RESULT_OK && intent != null) {
536 final long aggregateId = ContentUris.parseId(intent.getData());
537 joinAggregate(aggregateId);
538 }
539 }
540
541 private void splitContact(long contactId) {
542 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_OUT);
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700543 Toast.makeText(this, R.string.contactsSplitMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700544 mAdapter.notifyDataSetChanged();
545 }
546
547 private void joinAggregate(final long aggregateId) {
Dmitri Plotnikov39466592009-07-27 11:23:51 -0700548 Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID},
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700549 RawContacts.CONTACT_ID + "=" + aggregateId, null, null);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700550
551 try {
552 while(c.moveToNext()) {
553 long contactId = c.getLong(0);
554 setAggregationException(contactId, AggregationExceptions.TYPE_KEEP_IN);
555 }
556 } finally {
557 c.close();
558 }
559
Dmitri Plotnikovd7c4af22009-06-19 18:31:00 -0700560 Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_SHORT).show();
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700561 mAdapter.notifyDataSetChanged();
562 }
563
564 /**
565 * Given a contact ID sets an aggregation exception to either join the contact with the
566 * current aggregate or split off.
567 */
568 protected void setAggregationException(long contactId, int exceptionType) {
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700569 ContentValues values = new ContentValues(3);
Dmitri Plotnikove1cd6792009-07-27 20:28:17 -0700570 values.put(AggregationExceptions.CONTACT_ID, ContentUris.parseId(mUri));
Jeff Sharkey14f61ab2009-08-05 21:02:37 -0700571 values.put(AggregationExceptions.RAW_CONTACT_ID, contactId);
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700572 values.put(AggregationExceptions.TYPE, exceptionType);
Dmitri Plotnikovd09f75c2009-06-16 11:59:22 -0700573 mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null);
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700574 }
575
Dmitri Plotnikovef038722009-06-24 18:51:47 -0700576 private void showOptionsActivity() {
577 final Intent intent = new Intent(this, ContactOptionsActivity.class);
578 intent.setData(mUri);
579 startActivity(intent);
580 }
581
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700582 private ViewEntry getViewEntryForMenuItem(MenuItem item) {
583 AdapterView.AdapterContextMenuInfo info;
584 try {
585 info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
586 } catch (ClassCastException e) {
587 Log.e(TAG, "bad menuInfo", e);
588 return null;
589 }
590
591 return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS);
592 }
593
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800594 @Override
595 public boolean onKeyDown(int keyCode, KeyEvent event) {
596 switch (keyCode) {
597 case KeyEvent.KEYCODE_CALL: {
598 try {
599 ITelephony phone = ITelephony.Stub.asInterface(
600 ServiceManager.checkService("phone"));
601 if (phone != null && !phone.isIdle()) {
602 // Skip out and let the key be handled at a higher level
603 break;
604 }
605 } catch (RemoteException re) {
606 // Fall through and try to call the contact
607 }
608
Evan Millar7911ff52009-07-21 15:55:18 -0700609 int index = mListView.getSelectedItemPosition();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800610 if (index != -1) {
611 ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS);
Evan Millar66388be2009-05-28 15:41:07 -0700612 if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) {
613 startActivity(entry.intent);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800614 }
615 } else if (mNumPhoneNumbers != 0) {
616 // There isn't anything selected, call the default number
617 Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri);
618 startActivity(intent);
619 }
620 return true;
621 }
622
623 case KeyEvent.KEYCODE_DEL: {
624 showDialog(DIALOG_CONFIRM_DELETE);
625 return true;
626 }
627 }
628
629 return super.onKeyDown(keyCode, event);
630 }
631
Evan Millar7911ff52009-07-21 15:55:18 -0700632 public void onItemClick(AdapterView parent, View v, int position, long id) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800633 ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS);
634 if (entry != null) {
635 Intent intent = entry.intent;
636 if (intent != null) {
637 try {
638 startActivity(intent);
639 } catch (ActivityNotFoundException e) {
640 Log.e(TAG, "No activity found for intent: " + intent);
641 signalError();
642 }
643 } else {
644 signalError();
645 }
646 } else {
647 signalError();
648 }
649 }
650
651 /**
652 * Signal an error to the user via a beep, or some other method.
653 */
654 private void signalError() {
655 //TODO: implement this when we have the sonification APIs
656 }
657
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800658 private Uri constructImToUrl(String host, String data) {
Evan Millar45e0ed32009-06-01 16:44:38 -0700659 // 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 -0800660 StringBuilder buf = new StringBuilder("imto://");
661 buf.append(host);
662 buf.append('/');
663 buf.append(data);
664 return Uri.parse(buf.toString());
665 }
666
667 /**
668 * Build up the entries to display on the screen.
Evan Millar5c22c3b2009-05-29 11:37:54 -0700669 *
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800670 * @param personCursor the URI for the contact being displayed
671 */
Evan Millar66388be2009-05-28 15:41:07 -0700672 private final void buildEntries(Cursor aggCursor) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800673 // Clear out the old entries
674 final int numSections = mSections.size();
675 for (int i = 0; i < numSections; i++) {
676 mSections.get(i).clear();
677 }
678
Evan Millar7911ff52009-07-21 15:55:18 -0700679 mRawContactIds.clear();
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700680
Evan Millar66388be2009-05-28 15:41:07 -0700681 // Build up method entries
682 if (mUri != null) {
Jeff Hamilton119b6ca2009-07-30 12:57:49 -0500683 aggCursor.moveToPosition(-1);
Evan Millar66388be2009-05-28 15:41:07 -0700684 while (aggCursor.moveToNext()) {
685 final String mimetype = aggCursor.getString(DATA_MIMETYPE_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700686
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800687 ViewEntry entry = new ViewEntry();
Evan Millar66388be2009-05-28 15:41:07 -0700688
689 final long id = aggCursor.getLong(DATA_ID_COLUMN);
690 final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800691 entry.id = id;
692 entry.uri = uri;
Evan Millar45e0ed32009-06-01 16:44:38 -0700693 entry.mimetype = mimetype;
Evan Millar7911ff52009-07-21 15:55:18 -0700694 // TODO: entry.contactId should be renamed to entry.rawContactId
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700695 entry.contactId = aggCursor.getLong(DATA_CONTACT_ID_COLUMN);
Evan Millar7911ff52009-07-21 15:55:18 -0700696 if (!mRawContactIds.contains(entry.contactId)) {
697 mRawContactIds.add(entry.contactId);
698 }
699
700 // This performs the tab filtering
701 if (mRawContactId != entry.contactId && mRawContactId != ALL_CONTACTS_ID) {
702 continue;
Dmitri Plotnikovb4491ee2009-06-15 09:31:02 -0700703 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800704
Evan Millar66388be2009-05-28 15:41:07 -0700705 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
Evan Millar5c22c3b2009-05-29 11:37:54 -0700706 || mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700707 || mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
Evan Millar66388be2009-05-28 15:41:07 -0700708 || mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
709 final int type = aggCursor.getInt(DATA_1_COLUMN);
Evan Millar45e0ed32009-06-01 16:44:38 -0700710 final String label = aggCursor.getString(DATA_3_COLUMN);
711 final String data = aggCursor.getString(DATA_2_COLUMN);
712 final boolean isSuperPrimary = "1".equals(
713 aggCursor.getString(DATA_IS_SUPER_PRIMARY_COLUMN));
Evan Millar66388be2009-05-28 15:41:07 -0700714
Evan Millar54a5c9f2009-06-23 17:41:09 -0700715 entry.type = type;
716
Evan Millar66388be2009-05-28 15:41:07 -0700717 // Don't crash if the data is bogus
718 if (TextUtils.isEmpty(data)) {
719 Log.w(TAG, "empty data for contact method " + id);
720 continue;
721 }
722
723 // Build phone entries
724 if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
725 mNumPhoneNumbers++;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700726
Evan Millar66388be2009-05-28 15:41:07 -0700727 final CharSequence displayLabel = ContactsUtils.getDisplayLabel(
728 this, mimetype, type, label);
729 entry.label = buildActionString(R.string.actionCall, displayLabel, true);
Evan Millar54a5c9f2009-06-23 17:41:09 -0700730 entry.data = PhoneNumberUtils.stripSeparators(data);
Evan Millar296758b2009-08-07 15:40:08 -0700731 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
732 Uri.fromParts("tel", data, null));
Evan Millar15e514d2009-08-04 10:14:57 -0700733 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
Evan Millar66388be2009-05-28 15:41:07 -0700734 Uri.fromParts("sms", data, null));
Evan Millar15e514d2009-08-04 10:14:57 -0700735 entry.isPrimary = isSuperPrimary;
Evan Millar66388be2009-05-28 15:41:07 -0700736 entry.actionIcon = android.R.drawable.sym_action_call;
737 mPhoneEntries.add(entry);
738
Evan Millar5c22c3b2009-05-29 11:37:54 -0700739 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700740 || mShowSmsLinksForAllPhones) {
741 // Add an SMS entry
Evan Millar15e514d2009-08-04 10:14:57 -0700742 entry.secondaryActionIcon = R.drawable.sym_action_sms;
Evan Millar66388be2009-05-28 15:41:07 -0700743 }
744 // Build email entries
745 } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800746 entry.label = buildActionString(R.string.actionEmail,
Evan Millar66388be2009-05-28 15:41:07 -0700747 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800748 entry.data = data;
749 entry.intent = new Intent(Intent.ACTION_SENDTO,
750 Uri.fromParts("mailto", data, null));
751 entry.actionIcon = android.R.drawable.sym_action_email;
Evan Millar15e514d2009-08-04 10:14:57 -0700752 entry.isPrimary = isSuperPrimary;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800753 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700754 // Build postal entries
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700755 } else if (mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800756 entry.label = buildActionString(R.string.actionMap,
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.maxLines = 4;
760 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
761 entry.actionIcon = R.drawable.sym_action_map;
762 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700763 // Build im entries
764 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
765 String[] protocolStrings = getResources().getStringArray(
766 android.R.array.imProtocols);
Jeff Sharkeye731d422009-07-28 21:12:43 -0700767 Object protocolObj = aggCursor.getString(DATA_5_COLUMN);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700768 String host = null;
Jeff Sharkeye731d422009-07-28 21:12:43 -0700769 // TODO: fix by moving to contactssource-based rendering rules
770// Object protocolObj = ContactsUtils.decodeImProtocol(
771// aggCursor.getString(DATA_5_COLUMN));
772// if (protocolObj instanceof Number) {
773// int protocol = ((Number) protocolObj).intValue();
774// entry.label = buildActionString(R.string.actionChat,
775// protocolStrings[protocol], false);
776// host = ContactsUtils.lookupProviderNameFromId(
777// protocol).toLowerCase();
778// if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
779// || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
780// entry.maxLabelLines = 2;
781// }
782// } else if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800783 String providerName = (String) protocolObj;
784 entry.label = buildActionString(R.string.actionChat,
785 providerName, false);
786 host = providerName.toLowerCase();
Jeff Sharkeye731d422009-07-28 21:12:43 -0700787// }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800788
789 // Only add the intent if there is a valid host
790 if (!TextUtils.isEmpty(host)) {
791 entry.intent = new Intent(Intent.ACTION_SENDTO,
792 constructImToUrl(host, data));
793 }
794 entry.data = data;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700795 //TODO(emillar) Add in presence info
Evan Millar66388be2009-05-28 15:41:07 -0700796 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800797 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700798 aggCursor.getInt(METHODS_STATUS_COLUMN));
Evan Millar54a5c9f2009-06-23 17:41:09 -0700799 entry.status = ...
Evan Millar66388be2009-05-28 15:41:07 -0700800 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800801 entry.actionIcon = android.R.drawable.sym_action_chat;
802 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700803 }
Evan Millar66388be2009-05-28 15:41:07 -0700804 // Build organization entries
805 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
Evan Millar66388be2009-05-28 15:41:07 -0700806 final String company = aggCursor.getString(DATA_3_COLUMN);
807 final String title = aggCursor.getString(DATA_4_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700808
Evan Millar66388be2009-05-28 15:41:07 -0700809 // Don't crash if the data is bogus
810 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
811 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800812 continue;
813 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700814
Evan Millar66388be2009-05-28 15:41:07 -0700815 entry.data = title;
816 entry.actionIcon = R.drawable.sym_action_organization;
817 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800818
Evan Millar66388be2009-05-28 15:41:07 -0700819 mOrganizationEntries.add(entry);
820 // Build note entries
821 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
822 entry.label = getString(R.string.label_notes);
823 entry.data = aggCursor.getString(DATA_1_COLUMN);
824 entry.id = 0;
825 entry.uri = null;
826 entry.intent = null;
827 entry.maxLines = 10;
828 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800829
Evan Millar66388be2009-05-28 15:41:07 -0700830 if (TextUtils.isEmpty(entry.data)) {
831 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700832 continue;
833 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700834
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800835 mOtherEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700836 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700837
Evan Millar45e0ed32009-06-01 16:44:38 -0700838
Evan Millar66388be2009-05-28 15:41:07 -0700839 // TODO(emillar) Add group entries
840// // Build the group entries
841// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
842// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
843// null, null, Groups.DEFAULT_SORT_ORDER);
844// if (groupCursor != null) {
845// try {
846// StringBuilder sb = new StringBuilder();
847//
848// while (groupCursor.moveToNext()) {
849// String systemId = groupCursor.getString(
850// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
851//
852// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
853// continue;
854// }
855//
856// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
857// if (!TextUtils.isEmpty(name)) {
858// if (sb.length() == 0) {
859// sb.append(name);
860// } else {
861// sb.append(getString(R.string.group_list, name));
862// }
863// }
864// }
865//
866// if (sb.length() > 0) {
867// ViewEntry entry = new ViewEntry();
868// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
869// entry.label = getString(R.string.label_groups);
870// entry.data = sb.toString();
871// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
872//
873// // TODO: Add an icon for the groups item.
874//
875// mGroupEntries.add(entry);
876// }
877// } finally {
878// groupCursor.close();
879// }
880// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700881
Evan Millar66388be2009-05-28 15:41:07 -0700882 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800883 }
884 }
885
886 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700887 // If there is no type just display an empty string
888 if (type == null) {
889 type = "";
890 }
891
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800892 if (lowerCase) {
893 return getString(actionResId, type.toString().toLowerCase());
894 } else {
895 return getString(actionResId, type.toString());
896 }
897 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700898
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800899 /**
900 * A basic structure with the data for a contact entry in the list.
901 */
Evan Millar54a5c9f2009-06-23 17:41:09 -0700902 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800903 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -0700904 public boolean isPrimary = false;
905 public int presenceIcon = -1;
906 public int secondaryActionIcon = -1;
907 public Intent intent;
908 public Intent secondaryIntent = null;
909 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800910 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700911 public ArrayList<Long> ids = new ArrayList<Long>();
912 public int collapseCount = 0;
913
914 public boolean collapseWith(ViewEntry entry) {
915 // assert equal collapse keys
916 if (!getCollapseKey().equals(entry.getCollapseKey())) {
917 return false;
918 }
919
920 // Choose the label associated with the highest type precedence.
921 if (TypePrecedence.getTypePrecedence(mimetype, type)
922 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
923 type = entry.type;
924 label = entry.label;
925 }
926
927 // Choose the max of the maxLines and maxLabelLines values.
928 maxLines = Math.max(maxLines, entry.maxLines);
929 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
930
931 // Choose the presence with the highest precedence.
932 if (Presence.getPresencePrecedence(status)
933 < Presence.getPresencePrecedence(entry.status)) {
934 status = entry.status;
935 }
936
937 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -0700938 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700939
940 // uri, and contactdId, shouldn't make a difference. Just keep the original.
941
942 // Keep track of all the ids that have been collapsed with this one.
943 ids.add(entry.id);
944 collapseCount++;
945 return true;
946 }
947
948 public String getCollapseKey() {
949 StringBuilder hashSb = new StringBuilder();
950 hashSb.append(data);
951 hashSb.append(mimetype);
952 hashSb.append((intent != null && intent.getAction() != null)
953 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -0700954 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
955 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -0700956 hashSb.append(actionIcon);
957 return hashSb.toString();
958 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800959 }
960
Evan Millar15e514d2009-08-04 10:14:57 -0700961 /** Cache of the children views of a row */
962 static class ViewCache {
963 public TextView label;
964 public TextView data;
965 public ImageView actionIcon;
966 public ImageView presenceIcon;
967 public ImageView primaryIcon;
968 public ImageView secondaryActionButton;
969 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700970
Evan Millar15e514d2009-08-04 10:14:57 -0700971 // Need to keep track of this too
972 ViewEntry entry;
973 }
974
975 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
976 implements View.OnClickListener {
977
Evan Millar5c22c3b2009-05-29 11:37:54 -0700978
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800979 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
980 super(context, sections, SHOW_SEPARATORS);
981 }
982
Evan Millar15e514d2009-08-04 10:14:57 -0700983 public void onClick(View v) {
984 Intent intent = (Intent) v.getTag();
985 startActivity(intent);
986 }
987
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800988 @Override
989 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -0700990 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800991 View v;
992
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800993 ViewCache views;
994
995 // Check to see if we can reuse convertView
996 if (convertView != null) {
997 v = convertView;
998 views = (ViewCache) v.getTag();
999 } else {
1000 // Create a new view if needed
1001 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1002
1003 // Cache the children
1004 views = new ViewCache();
1005 views.label = (TextView) v.findViewById(android.R.id.text1);
1006 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001007 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1008 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1009 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1010 views.secondaryActionButton = (ImageView) v.findViewById(
1011 R.id.secondary_action_button);
1012 views.secondaryActionButton.setOnClickListener(this);
Evan Millar15e514d2009-08-04 10:14:57 -07001013 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001014 v.setTag(views);
1015 }
1016
1017 // Update the entry in the view cache
1018 views.entry = entry;
1019
1020 // Bind the data to the view
1021 bindView(v, entry);
1022 return v;
1023 }
1024
1025 @Override
1026 protected View newView(int position, ViewGroup parent) {
1027 // getView() handles this
1028 throw new UnsupportedOperationException();
1029 }
1030
1031 @Override
1032 protected void bindView(View view, ViewEntry entry) {
1033 final Resources resources = mContext.getResources();
1034 ViewCache views = (ViewCache) view.getTag();
1035
1036 // Set the label
1037 TextView label = views.label;
1038 setMaxLines(label, entry.maxLabelLines);
1039 label.setText(entry.label);
1040
1041 // Set the data
1042 TextView data = views.data;
1043 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001044 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1045 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1046 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1047 } else {
1048 data.setText(entry.data);
1049 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001050 setMaxLines(data, entry.maxLines);
1051 }
1052
Evan Millar15e514d2009-08-04 10:14:57 -07001053 // Set the primary icon
1054 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1055
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001056 // Set the action icon
1057 ImageView action = views.actionIcon;
1058 if (entry.actionIcon != -1) {
1059 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1060 action.setVisibility(View.VISIBLE);
1061 } else {
1062 // Things should still line up as if there was an icon, so make it invisible
1063 action.setVisibility(View.INVISIBLE);
1064 }
1065
1066 // Set the presence icon
1067 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001068 if (entry.presenceIcon != -1) {
1069 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001070 } else if (entry.status != -1) {
1071 presenceIcon = resources.getDrawable(
1072 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001073 }
Evan Millar15e514d2009-08-04 10:14:57 -07001074 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001075 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001076 presenceIconView.setImageDrawable(presenceIcon);
1077 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001078 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001079 presenceIconView.setVisibility(View.GONE);
1080 }
1081
1082 // Set the secondary action button
1083 ImageView secondaryActionView = views.secondaryActionButton;
1084 Drawable secondaryActionIcon = null;
1085 if (entry.secondaryActionIcon != -1) {
1086 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1087 }
1088 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1089 secondaryActionView.setImageDrawable(secondaryActionIcon);
1090 secondaryActionView.setTag(entry.secondaryIntent);
1091 secondaryActionView.setVisibility(View.VISIBLE);
1092 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1093 } else {
1094 secondaryActionView.setVisibility(View.GONE);
1095 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001096 }
1097 }
1098
1099 private void setMaxLines(TextView textView, int maxLines) {
1100 if (maxLines == 1) {
1101 textView.setSingleLine(true);
1102 textView.setEllipsize(TextUtils.TruncateAt.END);
1103 } else {
1104 textView.setSingleLine(false);
1105 textView.setMaxLines(maxLines);
1106 textView.setEllipsize(null);
1107 }
1108 }
1109 }
1110}