blob: d00df4ad69d91550cd077cae395bdadd22cdc7ba [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));
Dmitri Plotnikov49f705f2009-06-17 18:31:56 -0700571 values.put(AggregationExceptions.CONTACT_ID, contactId);
572 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 Millar66388be2009-05-28 15:41:07 -0700731 entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, entry.uri);
Evan Millar15e514d2009-08-04 10:14:57 -0700732 entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO,
Evan Millar66388be2009-05-28 15:41:07 -0700733 Uri.fromParts("sms", data, null));
Evan Millar15e514d2009-08-04 10:14:57 -0700734 entry.isPrimary = isSuperPrimary;
Evan Millar66388be2009-05-28 15:41:07 -0700735 entry.actionIcon = android.R.drawable.sym_action_call;
736 mPhoneEntries.add(entry);
737
Evan Millar5c22c3b2009-05-29 11:37:54 -0700738 if (type == CommonDataKinds.Phone.TYPE_MOBILE
Evan Millar66388be2009-05-28 15:41:07 -0700739 || mShowSmsLinksForAllPhones) {
740 // Add an SMS entry
Evan Millar15e514d2009-08-04 10:14:57 -0700741 entry.secondaryActionIcon = R.drawable.sym_action_sms;
742
743// ViewEntry smsEntry = new ViewEntry();
744// smsEntry.label = buildActionString(
745// R.string.actionText, displayLabel, true);
746// smsEntry.data = PhoneNumberUtils.stripSeparators(data);
747// smsEntry.id = id;
748// smsEntry.uri = uri;
749// smsEntry.intent = entry.secondaryIntent;
750// smsEntry.mimetype = FastTrackWindow.MIME_SMS_ADDRESS;
751// smsEntry.type = type;
752// smsEntry.actionIcon = R.drawable.sym_action_sms;
753// mSmsEntries.add(smsEntry);
Evan Millar66388be2009-05-28 15:41:07 -0700754 }
755 // Build email entries
756 } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800757 entry.label = buildActionString(R.string.actionEmail,
Evan Millar66388be2009-05-28 15:41:07 -0700758 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800759 entry.data = data;
760 entry.intent = new Intent(Intent.ACTION_SENDTO,
761 Uri.fromParts("mailto", data, null));
762 entry.actionIcon = android.R.drawable.sym_action_email;
Evan Millar15e514d2009-08-04 10:14:57 -0700763 entry.isPrimary = isSuperPrimary;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800764 mEmailEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700765 // Build postal entries
Jeff Sharkeyc6ad3ab2009-07-21 19:30:15 -0700766 } else if (mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800767 entry.label = buildActionString(R.string.actionMap,
Evan Millar66388be2009-05-28 15:41:07 -0700768 ContactsUtils.getDisplayLabel(this, mimetype, type, label), true);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800769 entry.data = data;
770 entry.maxLines = 4;
771 entry.intent = new Intent(Intent.ACTION_VIEW, uri);
772 entry.actionIcon = R.drawable.sym_action_map;
773 mPostalEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700774 // Build im entries
775 } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) {
776 String[] protocolStrings = getResources().getStringArray(
777 android.R.array.imProtocols);
Jeff Sharkeye731d422009-07-28 21:12:43 -0700778 Object protocolObj = aggCursor.getString(DATA_5_COLUMN);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700779 String host = null;
Jeff Sharkeye731d422009-07-28 21:12:43 -0700780 // TODO: fix by moving to contactssource-based rendering rules
781// Object protocolObj = ContactsUtils.decodeImProtocol(
782// aggCursor.getString(DATA_5_COLUMN));
783// if (protocolObj instanceof Number) {
784// int protocol = ((Number) protocolObj).intValue();
785// entry.label = buildActionString(R.string.actionChat,
786// protocolStrings[protocol], false);
787// host = ContactsUtils.lookupProviderNameFromId(
788// protocol).toLowerCase();
789// if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK
790// || protocol == CommonDataKinds.Im.PROTOCOL_MSN) {
791// entry.maxLabelLines = 2;
792// }
793// } else if (protocolObj != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800794 String providerName = (String) protocolObj;
795 entry.label = buildActionString(R.string.actionChat,
796 providerName, false);
797 host = providerName.toLowerCase();
Jeff Sharkeye731d422009-07-28 21:12:43 -0700798// }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800799
800 // Only add the intent if there is a valid host
801 if (!TextUtils.isEmpty(host)) {
802 entry.intent = new Intent(Intent.ACTION_SENDTO,
803 constructImToUrl(host, data));
804 }
805 entry.data = data;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700806 //TODO(emillar) Add in presence info
Evan Millar66388be2009-05-28 15:41:07 -0700807 /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800808 entry.presenceIcon = Presence.getPresenceIconResourceId(
Evan Millar66388be2009-05-28 15:41:07 -0700809 aggCursor.getInt(METHODS_STATUS_COLUMN));
Evan Millar54a5c9f2009-06-23 17:41:09 -0700810 entry.status = ...
Evan Millar66388be2009-05-28 15:41:07 -0700811 }*/
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800812 entry.actionIcon = android.R.drawable.sym_action_chat;
813 mImEntries.add(entry);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700814 }
Evan Millar66388be2009-05-28 15:41:07 -0700815 // Build organization entries
816 } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) {
Evan Millar66388be2009-05-28 15:41:07 -0700817 final String company = aggCursor.getString(DATA_3_COLUMN);
818 final String title = aggCursor.getString(DATA_4_COLUMN);
Evan Millar5c22c3b2009-05-29 11:37:54 -0700819
Evan Millar66388be2009-05-28 15:41:07 -0700820 // Don't crash if the data is bogus
821 if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) {
822 Log.w(TAG, "empty data for contact method " + id);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800823 continue;
824 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700825
Evan Millar66388be2009-05-28 15:41:07 -0700826 entry.data = title;
827 entry.actionIcon = R.drawable.sym_action_organization;
828 entry.label = company;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800829
Evan Millar66388be2009-05-28 15:41:07 -0700830 mOrganizationEntries.add(entry);
831 // Build note entries
832 } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) {
833 entry.label = getString(R.string.label_notes);
834 entry.data = aggCursor.getString(DATA_1_COLUMN);
835 entry.id = 0;
836 entry.uri = null;
837 entry.intent = null;
838 entry.maxLines = 10;
839 entry.actionIcon = R.drawable.sym_note;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800840
Evan Millar66388be2009-05-28 15:41:07 -0700841 if (TextUtils.isEmpty(entry.data)) {
842 Log.w(TAG, "empty data for contact method " + id);
Alex Kennberg87fc3172009-03-28 06:43:06 -0700843 continue;
844 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700845
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800846 mOtherEntries.add(entry);
Evan Millar66388be2009-05-28 15:41:07 -0700847 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700848
Evan Millar45e0ed32009-06-01 16:44:38 -0700849
Evan Millar66388be2009-05-28 15:41:07 -0700850 // TODO(emillar) Add group entries
851// // Build the group entries
852// final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY);
853// Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION,
854// null, null, Groups.DEFAULT_SORT_ORDER);
855// if (groupCursor != null) {
856// try {
857// StringBuilder sb = new StringBuilder();
858//
859// while (groupCursor.moveToNext()) {
860// String systemId = groupCursor.getString(
861// ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID);
862//
863// if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) {
864// continue;
865// }
866//
867// String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME);
868// if (!TextUtils.isEmpty(name)) {
869// if (sb.length() == 0) {
870// sb.append(name);
871// } else {
872// sb.append(getString(R.string.group_list, name));
873// }
874// }
875// }
876//
877// if (sb.length() > 0) {
878// ViewEntry entry = new ViewEntry();
879// entry.kind = ContactEntryAdapter.Entry.KIND_GROUP;
880// entry.label = getString(R.string.label_groups);
881// entry.data = sb.toString();
882// entry.intent = new Intent(Intent.ACTION_EDIT, mUri);
883//
884// // TODO: Add an icon for the groups item.
885//
886// mGroupEntries.add(entry);
887// }
888// } finally {
889// groupCursor.close();
890// }
891// }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700892
Evan Millar66388be2009-05-28 15:41:07 -0700893 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800894 }
895 }
896
897 String buildActionString(int actionResId, CharSequence type, boolean lowerCase) {
Jeff Hamilton8350e5b2009-03-24 21:01:34 -0700898 // If there is no type just display an empty string
899 if (type == null) {
900 type = "";
901 }
902
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800903 if (lowerCase) {
904 return getString(actionResId, type.toString().toLowerCase());
905 } else {
906 return getString(actionResId, type.toString());
907 }
908 }
Evan Millar5c22c3b2009-05-29 11:37:54 -0700909
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800910 /**
911 * A basic structure with the data for a contact entry in the list.
912 */
Evan Millar54a5c9f2009-06-23 17:41:09 -0700913 static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800914 public int actionIcon = -1;
Evan Millar15e514d2009-08-04 10:14:57 -0700915 public boolean isPrimary = false;
916 public int presenceIcon = -1;
917 public int secondaryActionIcon = -1;
918 public Intent intent;
919 public Intent secondaryIntent = null;
920 public int status = -1;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800921 public int maxLabelLines = 1;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700922 public ArrayList<Long> ids = new ArrayList<Long>();
923 public int collapseCount = 0;
924
925 public boolean collapseWith(ViewEntry entry) {
926 // assert equal collapse keys
927 if (!getCollapseKey().equals(entry.getCollapseKey())) {
928 return false;
929 }
930
931 // Choose the label associated with the highest type precedence.
932 if (TypePrecedence.getTypePrecedence(mimetype, type)
933 > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) {
934 type = entry.type;
935 label = entry.label;
936 }
937
938 // Choose the max of the maxLines and maxLabelLines values.
939 maxLines = Math.max(maxLines, entry.maxLines);
940 maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines);
941
942 // Choose the presence with the highest precedence.
943 if (Presence.getPresencePrecedence(status)
944 < Presence.getPresencePrecedence(entry.status)) {
945 status = entry.status;
946 }
947
948 // If any of the collapsed entries are primary make the whole thing primary.
Evan Millar15e514d2009-08-04 10:14:57 -0700949 isPrimary = entry.isPrimary ? true : isPrimary;
Evan Millar54a5c9f2009-06-23 17:41:09 -0700950
951 // uri, and contactdId, shouldn't make a difference. Just keep the original.
952
953 // Keep track of all the ids that have been collapsed with this one.
954 ids.add(entry.id);
955 collapseCount++;
956 return true;
957 }
958
959 public String getCollapseKey() {
960 StringBuilder hashSb = new StringBuilder();
961 hashSb.append(data);
962 hashSb.append(mimetype);
963 hashSb.append((intent != null && intent.getAction() != null)
964 ? intent.getAction() : "");
Evan Millar15e514d2009-08-04 10:14:57 -0700965 hashSb.append((secondaryIntent != null && secondaryIntent.getAction() != null)
966 ? secondaryIntent.getAction() : "");
Evan Millar54a5c9f2009-06-23 17:41:09 -0700967 hashSb.append(actionIcon);
968 return hashSb.toString();
969 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800970 }
971
Evan Millar15e514d2009-08-04 10:14:57 -0700972 /** Cache of the children views of a row */
973 static class ViewCache {
974 public TextView label;
975 public TextView data;
976 public ImageView actionIcon;
977 public ImageView presenceIcon;
978 public ImageView primaryIcon;
979 public ImageView secondaryActionButton;
980 public View secondaryActionDivider;
Evan Millar5c22c3b2009-05-29 11:37:54 -0700981
Evan Millar15e514d2009-08-04 10:14:57 -0700982 // Need to keep track of this too
983 ViewEntry entry;
984 }
985
986 private final class ViewAdapter extends ContactEntryAdapter<ViewEntry>
987 implements View.OnClickListener {
988
Evan Millar5c22c3b2009-05-29 11:37:54 -0700989
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800990 ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) {
991 super(context, sections, SHOW_SEPARATORS);
992 }
993
Evan Millar15e514d2009-08-04 10:14:57 -0700994 public void onClick(View v) {
995 Intent intent = (Intent) v.getTag();
996 startActivity(intent);
997 }
998
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800999 @Override
1000 public View getView(int position, View convertView, ViewGroup parent) {
Evan Millar5c22c3b2009-05-29 11:37:54 -07001001 ViewEntry entry = getEntry(mSections, position, false);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001002 View v;
1003
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001004 ViewCache views;
1005
1006 // Check to see if we can reuse convertView
1007 if (convertView != null) {
1008 v = convertView;
1009 views = (ViewCache) v.getTag();
1010 } else {
1011 // Create a new view if needed
1012 v = mInflater.inflate(R.layout.list_item_text_icons, parent, false);
1013
1014 // Cache the children
1015 views = new ViewCache();
1016 views.label = (TextView) v.findViewById(android.R.id.text1);
1017 views.data = (TextView) v.findViewById(android.R.id.text2);
Evan Millar15e514d2009-08-04 10:14:57 -07001018 views.actionIcon = (ImageView) v.findViewById(R.id.action_icon);
1019 views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon);
1020 views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon);
1021 views.secondaryActionButton = (ImageView) v.findViewById(
1022 R.id.secondary_action_button);
1023 views.secondaryActionButton.setOnClickListener(this);
1024 views.secondaryActionButton.setFocusable(true);
1025 views.secondaryActionDivider = v.findViewById(R.id.divider);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001026 v.setTag(views);
1027 }
1028
1029 // Update the entry in the view cache
1030 views.entry = entry;
1031
1032 // Bind the data to the view
1033 bindView(v, entry);
1034 return v;
1035 }
1036
1037 @Override
1038 protected View newView(int position, ViewGroup parent) {
1039 // getView() handles this
1040 throw new UnsupportedOperationException();
1041 }
1042
1043 @Override
1044 protected void bindView(View view, ViewEntry entry) {
1045 final Resources resources = mContext.getResources();
1046 ViewCache views = (ViewCache) view.getTag();
1047
1048 // Set the label
1049 TextView label = views.label;
1050 setMaxLines(label, entry.maxLabelLines);
1051 label.setText(entry.label);
1052
1053 // Set the data
1054 TextView data = views.data;
1055 if (data != null) {
Evan Millar54a5c9f2009-06-23 17:41:09 -07001056 if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE)
1057 || entry.mimetype.equals(FastTrackWindow.MIME_SMS_ADDRESS)) {
1058 data.setText(PhoneNumberUtils.formatNumber(entry.data));
1059 } else {
1060 data.setText(entry.data);
1061 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001062 setMaxLines(data, entry.maxLines);
1063 }
1064
Evan Millar15e514d2009-08-04 10:14:57 -07001065 // Set the primary icon
1066 views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE);
1067
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001068 // Set the action icon
1069 ImageView action = views.actionIcon;
1070 if (entry.actionIcon != -1) {
1071 action.setImageDrawable(resources.getDrawable(entry.actionIcon));
1072 action.setVisibility(View.VISIBLE);
1073 } else {
1074 // Things should still line up as if there was an icon, so make it invisible
1075 action.setVisibility(View.INVISIBLE);
1076 }
1077
1078 // Set the presence icon
1079 Drawable presenceIcon = null;
Evan Millar15e514d2009-08-04 10:14:57 -07001080 if (entry.presenceIcon != -1) {
1081 presenceIcon = resources.getDrawable(entry.presenceIcon);
Evan Millar54a5c9f2009-06-23 17:41:09 -07001082 } else if (entry.status != -1) {
1083 presenceIcon = resources.getDrawable(
1084 Presence.getPresenceIconResourceId(entry.status));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001085 }
Evan Millar15e514d2009-08-04 10:14:57 -07001086 ImageView presenceIconView = views.presenceIcon;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001087 if (presenceIcon != null) {
Evan Millar15e514d2009-08-04 10:14:57 -07001088 presenceIconView.setImageDrawable(presenceIcon);
1089 presenceIconView.setVisibility(View.VISIBLE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001090 } else {
Evan Millar15e514d2009-08-04 10:14:57 -07001091 presenceIconView.setVisibility(View.GONE);
1092 }
1093
1094 // Set the secondary action button
1095 ImageView secondaryActionView = views.secondaryActionButton;
1096 Drawable secondaryActionIcon = null;
1097 if (entry.secondaryActionIcon != -1) {
1098 secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon);
1099 }
1100 if (entry.secondaryIntent != null && secondaryActionIcon != null) {
1101 secondaryActionView.setImageDrawable(secondaryActionIcon);
1102 secondaryActionView.setTag(entry.secondaryIntent);
1103 secondaryActionView.setVisibility(View.VISIBLE);
1104 views.secondaryActionDivider.setVisibility(View.VISIBLE);
1105 } else {
1106 secondaryActionView.setVisibility(View.GONE);
1107 views.secondaryActionDivider.setVisibility(View.GONE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001108 }
1109 }
1110
1111 private void setMaxLines(TextView textView, int maxLines) {
1112 if (maxLines == 1) {
1113 textView.setSingleLine(true);
1114 textView.setEllipsize(TextUtils.TruncateAt.END);
1115 } else {
1116 textView.setSingleLine(false);
1117 textView.setMaxLines(maxLines);
1118 textView.setEllipsize(null);
1119 }
1120 }
1121 }
1122}