The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.contacts; |
| 18 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 19 | import static com.android.contacts.ContactEntryAdapter.AGGREGATE_PROJECTION; |
| 20 | import static com.android.contacts.ContactEntryAdapter.AGGREGATE_STARRED_COLUMN; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 21 | import static com.android.contacts.ContactEntryAdapter.DATA_1_COLUMN; |
| 22 | import static com.android.contacts.ContactEntryAdapter.DATA_2_COLUMN; |
| 23 | import static com.android.contacts.ContactEntryAdapter.DATA_3_COLUMN; |
| 24 | import static com.android.contacts.ContactEntryAdapter.DATA_4_COLUMN; |
| 25 | import static com.android.contacts.ContactEntryAdapter.DATA_5_COLUMN; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 26 | import static com.android.contacts.ContactEntryAdapter.DATA_9_COLUMN; |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 27 | import static com.android.contacts.ContactEntryAdapter.DATA_CONTACT_ID_COLUMN; |
| 28 | import static com.android.contacts.ContactEntryAdapter.DATA_ID_COLUMN; |
| 29 | import static com.android.contacts.ContactEntryAdapter.DATA_IS_SUPER_PRIMARY_COLUMN; |
| 30 | import static com.android.contacts.ContactEntryAdapter.DATA_MIMETYPE_COLUMN; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 31 | |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 32 | import com.android.contacts.SplitAggregateView.OnContactSelectedListener; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 33 | import com.android.internal.telephony.ITelephony; |
| 34 | |
| 35 | import android.app.AlertDialog; |
| 36 | import android.app.Dialog; |
| 37 | import android.app.ListActivity; |
| 38 | import android.content.ActivityNotFoundException; |
| 39 | import android.content.ContentResolver; |
| 40 | import android.content.ContentUris; |
| 41 | import android.content.ContentValues; |
| 42 | import android.content.Context; |
| 43 | import android.content.DialogInterface; |
| 44 | import android.content.Intent; |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 45 | import android.content.DialogInterface.OnClickListener; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 46 | import android.content.pm.PackageManager; |
| 47 | import android.content.pm.ResolveInfo; |
| 48 | import android.content.res.Resources; |
| 49 | import android.database.ContentObserver; |
| 50 | import android.database.Cursor; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 51 | import android.graphics.Bitmap; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 52 | import android.graphics.drawable.Drawable; |
| 53 | import android.media.Ringtone; |
| 54 | import android.media.RingtoneManager; |
| 55 | import android.net.Uri; |
| 56 | import android.os.Bundle; |
| 57 | import android.os.Handler; |
| 58 | import android.os.RemoteException; |
| 59 | import android.os.ServiceManager; |
| 60 | import android.os.SystemClock; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 61 | import android.provider.ContactsContract.Aggregates; |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 62 | import android.provider.ContactsContract.AggregationExceptions; |
| 63 | import android.provider.ContactsContract.CommonDataKinds; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 64 | import android.provider.ContactsContract.Data; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 65 | import android.text.TextUtils; |
| 66 | import android.util.Log; |
| 67 | import android.view.ContextMenu; |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 68 | import android.view.ContextThemeWrapper; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 69 | import android.view.KeyEvent; |
| 70 | import android.view.Menu; |
| 71 | import android.view.MenuItem; |
| 72 | import android.view.View; |
| 73 | import android.view.ViewGroup; |
| 74 | import android.view.ContextMenu.ContextMenuInfo; |
| 75 | import android.widget.AdapterView; |
| 76 | import android.widget.CheckBox; |
| 77 | import android.widget.ImageView; |
| 78 | import android.widget.ListView; |
| 79 | import android.widget.TextView; |
| 80 | import android.widget.Toast; |
| 81 | |
| 82 | import java.util.ArrayList; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Displays the details of a specific contact. |
| 86 | */ |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 87 | public class ViewContactActivity extends ListActivity |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 88 | implements View.OnCreateContextMenuListener, View.OnClickListener, |
| 89 | DialogInterface.OnClickListener { |
| 90 | private static final String TAG = "ViewContact"; |
| 91 | private static final String SHOW_BARCODE_INTENT = "com.google.zxing.client.android.ENCODE"; |
| 92 | |
| 93 | private static final boolean SHOW_SEPARATORS = false; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 94 | |
| 95 | private static final int DIALOG_CONFIRM_DELETE = 1; |
| 96 | |
| 97 | public static final int MENU_ITEM_DELETE = 1; |
| 98 | public static final int MENU_ITEM_MAKE_DEFAULT = 2; |
| 99 | public static final int MENU_ITEM_SHOW_BARCODE = 3; |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 100 | public static final int MENU_ITEM_SPLIT_AGGREGATE = 4; |
| 101 | |
| 102 | private static final String[] AGGREGATION_EXCEPTIONS_PROJECTION = |
| 103 | new String[] { AggregationExceptions._ID}; |
| 104 | |
| 105 | private static final int AGGREGATION_EXCEPTIONS_COL_ID = 0; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 106 | |
| 107 | private Uri mUri; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 108 | private Uri mAggDataUri; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 109 | private ContentResolver mResolver; |
| 110 | private ViewAdapter mAdapter; |
| 111 | private int mNumPhoneNumbers = 0; |
| 112 | |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 113 | /** |
| 114 | * A list of distinct contact IDs included in the current aggregate. |
| 115 | */ |
| 116 | private ArrayList<Long> mContactIds = new ArrayList<Long>(); |
| 117 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 118 | /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>(); |
| 119 | /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>(); |
| 120 | /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>(); |
| 121 | /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>(); |
| 122 | /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>(); |
| 123 | /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>(); |
The Android Open Source Project | cac191e | 2009-03-18 22:20:27 -0700 | [diff] [blame] | 124 | /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 125 | /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>(); |
| 126 | /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>(); |
| 127 | |
| 128 | private Cursor mCursor; |
| 129 | private boolean mObserverRegistered; |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 130 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 131 | private ContentObserver mObserver = new ContentObserver(new Handler()) { |
| 132 | @Override |
| 133 | public boolean deliverSelfNotifications() { |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void onChange(boolean selfChange) { |
| 139 | if (mCursor != null && !mCursor.isClosed()){ |
| 140 | dataChanged(); |
| 141 | } |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | public void onClick(DialogInterface dialog, int which) { |
| 146 | if (mCursor != null) { |
| 147 | if (mObserverRegistered) { |
| 148 | mCursor.unregisterContentObserver(mObserver); |
| 149 | mObserverRegistered = false; |
| 150 | } |
| 151 | mCursor.close(); |
| 152 | mCursor = null; |
| 153 | } |
| 154 | getContentResolver().delete(mUri, null, null); |
| 155 | finish(); |
| 156 | } |
| 157 | |
| 158 | public void onClick(View view) { |
| 159 | if (!mObserverRegistered) { |
| 160 | return; |
| 161 | } |
| 162 | switch (view.getId()) { |
| 163 | case R.id.star: { |
Evan Millar | 7e4accf | 2009-06-08 10:43:26 -0700 | [diff] [blame] | 164 | mCursor.moveToFirst(); |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 165 | int oldStarredState = mCursor.getInt(AGGREGATE_STARRED_COLUMN); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 166 | ContentValues values = new ContentValues(1); |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 167 | values.put(Aggregates.STARRED, oldStarredState == 1 ? 0 : 1); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 168 | getContentResolver().update(mUri, values, null, null); |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | private TextView mNameView; |
| 175 | private TextView mPhoneticNameView; // may be null in some locales |
| 176 | private ImageView mPhotoView; |
| 177 | private int mNoPhotoResource; |
| 178 | private CheckBox mStarView; |
| 179 | private boolean mShowSmsLinksForAllPhones; |
| 180 | |
| 181 | @Override |
| 182 | protected void onCreate(Bundle icicle) { |
| 183 | super.onCreate(icicle); |
| 184 | |
| 185 | setContentView(R.layout.view_contact); |
| 186 | getListView().setOnCreateContextMenuListener(this); |
| 187 | |
| 188 | mNameView = (TextView) findViewById(R.id.name); |
| 189 | mPhoneticNameView = (TextView) findViewById(R.id.phonetic_name); |
| 190 | mPhotoView = (ImageView) findViewById(R.id.photo); |
| 191 | mStarView = (CheckBox) findViewById(R.id.star); |
| 192 | mStarView.setOnClickListener(this); |
| 193 | |
| 194 | // Set the photo with a random "no contact" image |
| 195 | long now = SystemClock.elapsedRealtime(); |
| 196 | int num = (int) now & 0xf; |
| 197 | if (num < 9) { |
| 198 | // Leaning in from right, common |
| 199 | mNoPhotoResource = R.drawable.ic_contact_picture; |
| 200 | } else if (num < 14) { |
| 201 | // Leaning in from left uncommon |
| 202 | mNoPhotoResource = R.drawable.ic_contact_picture_2; |
| 203 | } else { |
| 204 | // Coming in from the top, rare |
| 205 | mNoPhotoResource = R.drawable.ic_contact_picture_3; |
| 206 | } |
| 207 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 208 | mUri = getIntent().getData(); |
| 209 | mAggDataUri = Uri.withAppendedPath(mUri, "data"); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 210 | mResolver = getContentResolver(); |
| 211 | |
| 212 | // Build the list of sections. The order they're added to mSections dictates the |
| 213 | // order they are displayed in the list. |
| 214 | mSections.add(mPhoneEntries); |
| 215 | mSections.add(mSmsEntries); |
| 216 | mSections.add(mEmailEntries); |
| 217 | mSections.add(mImEntries); |
| 218 | mSections.add(mPostalEntries); |
| 219 | mSections.add(mOrganizationEntries); |
The Android Open Source Project | cac191e | 2009-03-18 22:20:27 -0700 | [diff] [blame] | 220 | mSections.add(mGroupEntries); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 221 | mSections.add(mOtherEntries); |
| 222 | |
| 223 | //TODO Read this value from a preference |
| 224 | mShowSmsLinksForAllPhones = true; |
| 225 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 226 | mCursor = mResolver.query(mAggDataUri, |
| 227 | AGGREGATE_PROJECTION, null, null, null); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
| 231 | protected void onResume() { |
| 232 | super.onResume(); |
| 233 | mObserverRegistered = true; |
| 234 | mCursor.registerContentObserver(mObserver); |
| 235 | dataChanged(); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | protected void onPause() { |
| 240 | super.onPause(); |
| 241 | if (mCursor != null) { |
| 242 | if (mObserverRegistered) { |
| 243 | mObserverRegistered = false; |
| 244 | mCursor.unregisterContentObserver(mObserver); |
| 245 | } |
| 246 | mCursor.deactivate(); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | protected void onDestroy() { |
| 252 | super.onDestroy(); |
| 253 | |
| 254 | if (mCursor != null) { |
| 255 | if (mObserverRegistered) { |
| 256 | mCursor.unregisterContentObserver(mObserver); |
| 257 | mObserverRegistered = false; |
| 258 | } |
| 259 | mCursor.close(); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | protected Dialog onCreateDialog(int id) { |
| 265 | switch (id) { |
| 266 | case DIALOG_CONFIRM_DELETE: |
| 267 | return new AlertDialog.Builder(this) |
| 268 | .setTitle(R.string.deleteConfirmation_title) |
| 269 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 270 | .setMessage(R.string.deleteConfirmation) |
| 271 | .setNegativeButton(android.R.string.cancel, null) |
| 272 | .setPositiveButton(android.R.string.ok, this) |
| 273 | .setCancelable(false) |
| 274 | .create(); |
| 275 | } |
| 276 | return null; |
| 277 | } |
| 278 | |
| 279 | private void dataChanged() { |
| 280 | mCursor.requery(); |
| 281 | if (mCursor.moveToFirst()) { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 282 | // Set the star |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 283 | mStarView.setChecked(mCursor.getInt(AGGREGATE_STARRED_COLUMN) == 1 ? true : false); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 284 | |
| 285 | // Build up the contact entries |
| 286 | buildEntries(mCursor); |
| 287 | if (mAdapter == null) { |
| 288 | mAdapter = new ViewAdapter(this, mSections); |
| 289 | setListAdapter(mAdapter); |
| 290 | } else { |
| 291 | mAdapter.setSections(mSections, SHOW_SEPARATORS); |
| 292 | } |
| 293 | } else { |
| 294 | Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show(); |
| 295 | Log.e(TAG, "invalid contact uri: " + mUri); |
| 296 | finish(); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public boolean onCreateOptionsMenu(Menu menu) { |
| 302 | menu.add(0, 0, 0, R.string.menu_editContact) |
| 303 | .setIcon(android.R.drawable.ic_menu_edit) |
| 304 | .setIntent(new Intent(Intent.ACTION_EDIT, mUri)) |
| 305 | .setAlphabeticShortcut('e'); |
| 306 | menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact) |
| 307 | .setIcon(android.R.drawable.ic_menu_delete); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 308 | menu.add(0, MENU_ITEM_SPLIT_AGGREGATE, 0, R.string.menu_splitAggregate) |
| 309 | .setIcon(android.R.drawable.ic_menu_share); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 310 | return true; |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 315 | super.onPrepareOptionsMenu(menu); |
| 316 | // Perform this check each time the menu is about to be shown, because the Barcode Scanner |
| 317 | // could be installed or uninstalled at any time. |
| 318 | if (isBarcodeScannerInstalled()) { |
| 319 | if (menu.findItem(MENU_ITEM_SHOW_BARCODE) == null) { |
| 320 | menu.add(0, MENU_ITEM_SHOW_BARCODE, 0, R.string.menu_showBarcode) |
| 321 | .setIcon(R.drawable.ic_menu_show_barcode); |
| 322 | } |
| 323 | } else { |
| 324 | menu.removeItem(MENU_ITEM_SHOW_BARCODE); |
| 325 | } |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 326 | |
| 327 | boolean isAggregate = mContactIds.size() > 1; |
| 328 | menu.findItem(MENU_ITEM_SPLIT_AGGREGATE).setEnabled(isAggregate); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 329 | return true; |
| 330 | } |
| 331 | |
| 332 | private boolean isBarcodeScannerInstalled() { |
| 333 | final Intent intent = new Intent(SHOW_BARCODE_INTENT); |
| 334 | ResolveInfo ri = getPackageManager().resolveActivity(intent, |
| 335 | PackageManager.MATCH_DEFAULT_ONLY); |
| 336 | return ri != null; |
| 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { |
| 341 | AdapterView.AdapterContextMenuInfo info; |
| 342 | try { |
| 343 | info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
| 344 | } catch (ClassCastException e) { |
| 345 | Log.e(TAG, "bad menuInfo", e); |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | // This can be null sometimes, don't crash... |
| 350 | if (info == null) { |
| 351 | Log.e(TAG, "bad menuInfo"); |
| 352 | return; |
| 353 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 354 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 355 | ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS); |
| 356 | if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) { |
| 357 | menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent); |
| 358 | menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.auxIntent); |
| 359 | if (entry.primaryIcon == -1) { |
| 360 | menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 361 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 362 | } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) { |
| 363 | menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent); |
| 364 | if (entry.primaryIcon == -1) { |
| 365 | menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 366 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 367 | } else if (entry.mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)) { |
| 368 | menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent); |
| 369 | } |
| 370 | // TODO(emillar): add back with group support. |
| 371 | /* else if (entry.mimetype.equals()) { |
| 372 | menu.add(0, 0, 0, R.string.menu_viewGroup).setIntent(entry.intent); |
| 373 | } */ |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public boolean onOptionsItemSelected(MenuItem item) { |
| 378 | switch (item.getItemId()) { |
| 379 | case MENU_ITEM_DELETE: { |
| 380 | // Get confirmation |
| 381 | showDialog(DIALOG_CONFIRM_DELETE); |
| 382 | return true; |
| 383 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 384 | |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 385 | case MENU_ITEM_SPLIT_AGGREGATE: { |
| 386 | showSplitAggregateDialog(); |
| 387 | return true; |
| 388 | } |
| 389 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 390 | // TODO(emillar) Bring this back. |
| 391 | /*case MENU_ITEM_SHOW_BARCODE: |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 392 | if (mCursor.moveToFirst()) { |
| 393 | Intent intent = new Intent(SHOW_BARCODE_INTENT); |
| 394 | intent.putExtra("ENCODE_TYPE", "CONTACT_TYPE"); |
| 395 | Bundle bundle = new Bundle(); |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 396 | String name = mCursor.getString(AGGREGATE_DISPLAY_NAME_COLUMN); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 397 | if (!TextUtils.isEmpty(name)) { |
Jeffrey Sharkey | 715b32a | 2009-03-27 18:56:05 -0700 | [diff] [blame] | 398 | // Correctly handle when section headers are hidden |
| 399 | int sepAdjust = SHOW_SEPARATORS ? 1 : 0; |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 400 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 401 | bundle.putString(Contacts.Intents.Insert.NAME, name); |
| 402 | // The 0th ViewEntry in each ArrayList below is a separator item |
Jeffrey Sharkey | 715b32a | 2009-03-27 18:56:05 -0700 | [diff] [blame] | 403 | int entriesToAdd = Math.min(mPhoneEntries.size() - sepAdjust, PHONE_KEYS.length); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 404 | for (int x = 0; x < entriesToAdd; x++) { |
Jeffrey Sharkey | 715b32a | 2009-03-27 18:56:05 -0700 | [diff] [blame] | 405 | ViewEntry entry = mPhoneEntries.get(x + sepAdjust); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 406 | bundle.putString(PHONE_KEYS[x], entry.data); |
| 407 | } |
Jeffrey Sharkey | 715b32a | 2009-03-27 18:56:05 -0700 | [diff] [blame] | 408 | entriesToAdd = Math.min(mEmailEntries.size() - sepAdjust, EMAIL_KEYS.length); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 409 | for (int x = 0; x < entriesToAdd; x++) { |
Jeffrey Sharkey | 715b32a | 2009-03-27 18:56:05 -0700 | [diff] [blame] | 410 | ViewEntry entry = mEmailEntries.get(x + sepAdjust); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 411 | bundle.putString(EMAIL_KEYS[x], entry.data); |
| 412 | } |
Jeffrey Sharkey | 715b32a | 2009-03-27 18:56:05 -0700 | [diff] [blame] | 413 | if (mPostalEntries.size() >= 1 + sepAdjust) { |
| 414 | ViewEntry entry = mPostalEntries.get(sepAdjust); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 415 | bundle.putString(Contacts.Intents.Insert.POSTAL, entry.data); |
| 416 | } |
| 417 | intent.putExtra("ENCODE_DATA", bundle); |
| 418 | try { |
| 419 | startActivity(intent); |
| 420 | } catch (ActivityNotFoundException e) { |
| 421 | // The check in onPrepareOptionsMenu() should make this impossible, but |
| 422 | // for safety I'm catching the exception rather than crashing. Ideally |
| 423 | // I'd call Menu.removeItem() here too, but I don't see a way to get |
| 424 | // the options menu. |
| 425 | Log.e(TAG, "Show barcode menu item was clicked but Barcode Scanner " + |
| 426 | "was not installed."); |
| 427 | } |
| 428 | return true; |
| 429 | } |
| 430 | } |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 431 | break; */ |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 432 | } |
| 433 | return super.onOptionsItemSelected(item); |
| 434 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 435 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 436 | @Override |
| 437 | public boolean onContextItemSelected(MenuItem item) { |
| 438 | switch (item.getItemId()) { |
| 439 | case MENU_ITEM_MAKE_DEFAULT: { |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 440 | if (makeItemDefault(item)) { |
| 441 | return true; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 442 | } |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 443 | break; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 444 | } |
| 445 | } |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 446 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 447 | return super.onContextItemSelected(item); |
| 448 | } |
| 449 | |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 450 | private boolean makeItemDefault(MenuItem item) { |
| 451 | ViewEntry entry = getViewEntryForMenuItem(item); |
| 452 | if (entry == null) { |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | // Update the primary values in the data record. |
| 457 | ContentValues values = new ContentValues(2); |
| 458 | values.put(Data.IS_PRIMARY, 1); |
| 459 | values.put(Data.IS_SUPER_PRIMARY, 1); |
| 460 | |
| 461 | Log.i(TAG, mUri.toString()); |
| 462 | getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id), |
| 463 | values, null, null); |
| 464 | dataChanged(); |
| 465 | return true; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Shows a dialog that contains a list of all constituent contacts in this aggregate. |
| 470 | * The user picks a contact to be split into its own aggregate or clicks Cancel. |
| 471 | */ |
| 472 | private void showSplitAggregateDialog() { |
| 473 | |
| 474 | // Wrap this dialog in a specific theme so that list items have correct text color. |
| 475 | final ContextThemeWrapper dialogContext = |
| 476 | new ContextThemeWrapper(this, android.R.style.Theme_Light); |
| 477 | AlertDialog.Builder builder = |
| 478 | new AlertDialog.Builder(dialogContext); |
| 479 | builder.setTitle(getString(R.string.splitAggregate_title)); |
| 480 | |
| 481 | final SplitAggregateView view = new SplitAggregateView(dialogContext, mUri); |
| 482 | builder.setView(view); |
| 483 | |
| 484 | builder.setInverseBackgroundForced(true); |
| 485 | builder.setCancelable(true); |
| 486 | builder.setNegativeButton(android.R.string.cancel, |
| 487 | new OnClickListener() { |
| 488 | public void onClick(DialogInterface dialog, int which) { |
| 489 | dialog.dismiss(); |
| 490 | } |
| 491 | }); |
| 492 | final AlertDialog dialog = builder.create(); |
| 493 | |
| 494 | view.setOnContactSelectedListener(new OnContactSelectedListener() { |
| 495 | public void onContactSelected(long contactId) { |
| 496 | dialog.dismiss(); |
| 497 | splitContact(contactId); |
| 498 | } |
| 499 | }); |
| 500 | |
| 501 | dialog.show(); |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Given an ID of a constituent contact, splits it off into a separate aggregate. |
| 506 | */ |
| 507 | protected void splitContact(long contactToSplit) { |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 508 | |
Dmitri Plotnikov | d09f75c | 2009-06-16 11:59:22 -0700 | [diff] [blame^] | 509 | ContentValues values = new ContentValues(3); |
| 510 | values.put(AggregationExceptions.AGGREGATE_ID, ContentUris.parseId(mUri)); |
| 511 | values.put(AggregationExceptions.CONTACT_ID, contactToSplit); |
| 512 | values.put(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_OUT); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 513 | |
Dmitri Plotnikov | d09f75c | 2009-06-16 11:59:22 -0700 | [diff] [blame^] | 514 | mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 515 | |
Dmitri Plotnikov | d09f75c | 2009-06-16 11:59:22 -0700 | [diff] [blame^] | 516 | mAdapter.notifyDataSetChanged(); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | private ViewEntry getViewEntryForMenuItem(MenuItem item) { |
| 520 | AdapterView.AdapterContextMenuInfo info; |
| 521 | try { |
| 522 | info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); |
| 523 | } catch (ClassCastException e) { |
| 524 | Log.e(TAG, "bad menuInfo", e); |
| 525 | return null; |
| 526 | } |
| 527 | |
| 528 | return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS); |
| 529 | } |
| 530 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 531 | @Override |
| 532 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 533 | switch (keyCode) { |
| 534 | case KeyEvent.KEYCODE_CALL: { |
| 535 | try { |
| 536 | ITelephony phone = ITelephony.Stub.asInterface( |
| 537 | ServiceManager.checkService("phone")); |
| 538 | if (phone != null && !phone.isIdle()) { |
| 539 | // Skip out and let the key be handled at a higher level |
| 540 | break; |
| 541 | } |
| 542 | } catch (RemoteException re) { |
| 543 | // Fall through and try to call the contact |
| 544 | } |
| 545 | |
| 546 | int index = getListView().getSelectedItemPosition(); |
| 547 | if (index != -1) { |
| 548 | ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS); |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 549 | if (entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) { |
| 550 | startActivity(entry.intent); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 551 | } |
| 552 | } else if (mNumPhoneNumbers != 0) { |
| 553 | // There isn't anything selected, call the default number |
| 554 | Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, mUri); |
| 555 | startActivity(intent); |
| 556 | } |
| 557 | return true; |
| 558 | } |
| 559 | |
| 560 | case KeyEvent.KEYCODE_DEL: { |
| 561 | showDialog(DIALOG_CONFIRM_DELETE); |
| 562 | return true; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | return super.onKeyDown(keyCode, event); |
| 567 | } |
| 568 | |
| 569 | @Override |
| 570 | protected void onListItemClick(ListView l, View v, int position, long id) { |
| 571 | ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS); |
| 572 | if (entry != null) { |
| 573 | Intent intent = entry.intent; |
| 574 | if (intent != null) { |
| 575 | try { |
| 576 | startActivity(intent); |
| 577 | } catch (ActivityNotFoundException e) { |
| 578 | Log.e(TAG, "No activity found for intent: " + intent); |
| 579 | signalError(); |
| 580 | } |
| 581 | } else { |
| 582 | signalError(); |
| 583 | } |
| 584 | } else { |
| 585 | signalError(); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Signal an error to the user via a beep, or some other method. |
| 591 | */ |
| 592 | private void signalError() { |
| 593 | //TODO: implement this when we have the sonification APIs |
| 594 | } |
| 595 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 596 | private Uri constructImToUrl(String host, String data) { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 597 | // don't encode the url, because the Activity Manager can't find using the encoded url |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 598 | StringBuilder buf = new StringBuilder("imto://"); |
| 599 | buf.append(host); |
| 600 | buf.append('/'); |
| 601 | buf.append(data); |
| 602 | return Uri.parse(buf.toString()); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Build up the entries to display on the screen. |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 607 | * |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 608 | * @param personCursor the URI for the contact being displayed |
| 609 | */ |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 610 | private final void buildEntries(Cursor aggCursor) { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 611 | // Clear out the old entries |
| 612 | final int numSections = mSections.size(); |
| 613 | for (int i = 0; i < numSections; i++) { |
| 614 | mSections.get(i).clear(); |
| 615 | } |
| 616 | |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 617 | mContactIds.clear(); |
| 618 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 619 | // Build up method entries |
| 620 | if (mUri != null) { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 621 | Bitmap photoBitmap = null; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 622 | while (aggCursor.moveToNext()) { |
| 623 | final String mimetype = aggCursor.getString(DATA_MIMETYPE_COLUMN); |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 624 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 625 | ViewEntry entry = new ViewEntry(); |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 626 | |
| 627 | final long id = aggCursor.getLong(DATA_ID_COLUMN); |
| 628 | final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 629 | entry.id = id; |
| 630 | entry.uri = uri; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 631 | entry.mimetype = mimetype; |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 632 | entry.contactId = aggCursor.getLong(DATA_CONTACT_ID_COLUMN); |
| 633 | if (!mContactIds.contains(entry.contactId)) { |
| 634 | mContactIds.add(entry.contactId); |
| 635 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 636 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 637 | if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE) |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 638 | || mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE) |
| 639 | || mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE) |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 640 | || mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) { |
| 641 | final int type = aggCursor.getInt(DATA_1_COLUMN); |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 642 | final String label = aggCursor.getString(DATA_3_COLUMN); |
| 643 | final String data = aggCursor.getString(DATA_2_COLUMN); |
| 644 | final boolean isSuperPrimary = "1".equals( |
| 645 | aggCursor.getString(DATA_IS_SUPER_PRIMARY_COLUMN)); |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 646 | |
| 647 | // Don't crash if the data is bogus |
| 648 | if (TextUtils.isEmpty(data)) { |
| 649 | Log.w(TAG, "empty data for contact method " + id); |
| 650 | continue; |
| 651 | } |
| 652 | |
| 653 | // Build phone entries |
| 654 | if (mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) { |
| 655 | mNumPhoneNumbers++; |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 656 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 657 | final CharSequence displayLabel = ContactsUtils.getDisplayLabel( |
| 658 | this, mimetype, type, label); |
| 659 | entry.label = buildActionString(R.string.actionCall, displayLabel, true); |
| 660 | entry.data = data; |
| 661 | entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, entry.uri); |
| 662 | entry.auxIntent = new Intent(Intent.ACTION_SENDTO, |
| 663 | Uri.fromParts("sms", data, null)); |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 664 | if (isSuperPrimary) { |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 665 | entry.primaryIcon = R.drawable.ic_default_number; |
| 666 | } |
| 667 | entry.actionIcon = android.R.drawable.sym_action_call; |
| 668 | mPhoneEntries.add(entry); |
| 669 | |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 670 | if (type == CommonDataKinds.Phone.TYPE_MOBILE |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 671 | || mShowSmsLinksForAllPhones) { |
| 672 | // Add an SMS entry |
| 673 | ViewEntry smsEntry = new ViewEntry(); |
| 674 | smsEntry.label = buildActionString( |
| 675 | R.string.actionText, displayLabel, true); |
| 676 | smsEntry.data = data; |
| 677 | smsEntry.id = id; |
| 678 | smsEntry.uri = uri; |
| 679 | smsEntry.intent = entry.auxIntent; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 680 | smsEntry.mimetype = FastTrackWindow.MIME_SMS_ADDRESS; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 681 | smsEntry.actionIcon = R.drawable.sym_action_sms; |
| 682 | mSmsEntries.add(smsEntry); |
| 683 | } |
| 684 | // Build email entries |
| 685 | } else if (mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 686 | entry.label = buildActionString(R.string.actionEmail, |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 687 | ContactsUtils.getDisplayLabel(this, mimetype, type, label), true); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 688 | entry.data = data; |
| 689 | entry.intent = new Intent(Intent.ACTION_SENDTO, |
| 690 | Uri.fromParts("mailto", data, null)); |
| 691 | entry.actionIcon = android.R.drawable.sym_action_email; |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 692 | if (isSuperPrimary) { |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 693 | entry.primaryIcon = R.drawable.ic_default_number; |
| 694 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 695 | mEmailEntries.add(entry); |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 696 | // Build postal entries |
| 697 | } else if (mimetype.equals(CommonDataKinds.Postal.CONTENT_ITEM_TYPE)) { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 698 | entry.label = buildActionString(R.string.actionMap, |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 699 | ContactsUtils.getDisplayLabel(this, mimetype, type, label), true); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 700 | entry.data = data; |
| 701 | entry.maxLines = 4; |
| 702 | entry.intent = new Intent(Intent.ACTION_VIEW, uri); |
| 703 | entry.actionIcon = R.drawable.sym_action_map; |
| 704 | mPostalEntries.add(entry); |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 705 | // Build im entries |
| 706 | } else if (mimetype.equals(CommonDataKinds.Im.CONTENT_ITEM_TYPE)) { |
| 707 | String[] protocolStrings = getResources().getStringArray( |
| 708 | android.R.array.imProtocols); |
| 709 | Object protocolObj = ContactsUtils.decodeImProtocol( |
| 710 | aggCursor.getString(DATA_5_COLUMN)); |
Alex Kennberg | 87fc317 | 2009-03-28 06:43:06 -0700 | [diff] [blame] | 711 | String host = null; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 712 | if (protocolObj instanceof Number) { |
| 713 | int protocol = ((Number) protocolObj).intValue(); |
| 714 | entry.label = buildActionString(R.string.actionChat, |
| 715 | protocolStrings[protocol], false); |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 716 | host = ContactsUtils.lookupProviderNameFromId( |
| 717 | protocol).toLowerCase(); |
| 718 | if (protocol == CommonDataKinds.Im.PROTOCOL_GOOGLE_TALK |
| 719 | || protocol == CommonDataKinds.Im.PROTOCOL_MSN) { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 720 | entry.maxLabelLines = 2; |
| 721 | } |
Alex Kennberg | 87fc317 | 2009-03-28 06:43:06 -0700 | [diff] [blame] | 722 | } else if (protocolObj != null) { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 723 | String providerName = (String) protocolObj; |
| 724 | entry.label = buildActionString(R.string.actionChat, |
| 725 | providerName, false); |
| 726 | host = providerName.toLowerCase(); |
| 727 | } |
| 728 | |
| 729 | // Only add the intent if there is a valid host |
| 730 | if (!TextUtils.isEmpty(host)) { |
| 731 | entry.intent = new Intent(Intent.ACTION_SENDTO, |
| 732 | constructImToUrl(host, data)); |
| 733 | } |
| 734 | entry.data = data; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 735 | //TODO(emillar) Do we want presence info? |
| 736 | /*if (!aggCursor.isNull(METHODS_STATUS_COLUMN)) { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 737 | entry.presenceIcon = Presence.getPresenceIconResourceId( |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 738 | aggCursor.getInt(METHODS_STATUS_COLUMN)); |
| 739 | }*/ |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 740 | entry.actionIcon = android.R.drawable.sym_action_chat; |
| 741 | mImEntries.add(entry); |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 742 | } |
Evan Millar | 7e4accf | 2009-06-08 10:43:26 -0700 | [diff] [blame] | 743 | // Set the name |
| 744 | } else if (mimetype.equals(CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)) { |
| 745 | String name = mCursor.getString(DATA_9_COLUMN); |
| 746 | if (TextUtils.isEmpty(name)) { |
| 747 | mNameView.setText(getText(android.R.string.unknownName)); |
| 748 | } else { |
| 749 | mNameView.setText(name); |
| 750 | } |
| 751 | /* |
| 752 | if (mPhoneticNameView != null) { |
| 753 | String phoneticName = mCursor.getString(CONTACT_PHONETIC_NAME_COLUMN); |
| 754 | mPhoneticNameView.setText(phoneticName); |
| 755 | } |
| 756 | */ |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 757 | // Build organization entries |
| 758 | } else if (mimetype.equals(CommonDataKinds.Organization.CONTENT_ITEM_TYPE)) { |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 759 | final String company = aggCursor.getString(DATA_3_COLUMN); |
| 760 | final String title = aggCursor.getString(DATA_4_COLUMN); |
| 761 | final boolean isPrimary = "1".equals(aggCursor.getString(DATA_5_COLUMN)); |
| 762 | |
| 763 | if (isPrimary) { |
| 764 | entry.primaryIcon = R.drawable.ic_default_number; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 765 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 766 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 767 | // Don't crash if the data is bogus |
| 768 | if (TextUtils.isEmpty(company) && TextUtils.isEmpty(title)) { |
| 769 | Log.w(TAG, "empty data for contact method " + id); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 770 | continue; |
| 771 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 772 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 773 | entry.data = title; |
| 774 | entry.actionIcon = R.drawable.sym_action_organization; |
| 775 | entry.label = company; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 776 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 777 | mOrganizationEntries.add(entry); |
| 778 | // Build note entries |
| 779 | } else if (mimetype.equals(CommonDataKinds.Note.CONTENT_ITEM_TYPE)) { |
| 780 | entry.label = getString(R.string.label_notes); |
| 781 | entry.data = aggCursor.getString(DATA_1_COLUMN); |
| 782 | entry.id = 0; |
| 783 | entry.uri = null; |
| 784 | entry.intent = null; |
| 785 | entry.maxLines = 10; |
| 786 | entry.actionIcon = R.drawable.sym_note; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 787 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 788 | if (TextUtils.isEmpty(entry.data)) { |
| 789 | Log.w(TAG, "empty data for contact method " + id); |
Alex Kennberg | 87fc317 | 2009-03-28 06:43:06 -0700 | [diff] [blame] | 790 | continue; |
| 791 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 792 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 793 | mOtherEntries.add(entry); |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 794 | // Build the ringtone and send to voicemail entries |
| 795 | } else if (mimetype.equals(CommonDataKinds.CustomRingtone.CONTENT_ITEM_TYPE)) { |
| 796 | final Boolean sendToVoicemail = "1".equals(aggCursor.getString(DATA_1_COLUMN)); |
| 797 | final String ringtoneStr = aggCursor.getString(DATA_2_COLUMN); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 798 | |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 799 | // TODO(emillar) we need to enforce uniqueness of custom ringtone entries on a |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 800 | // single aggregate. This could be done by checking against the reference ID stored |
| 801 | // in the aggregate. |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 802 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 803 | // Build the send directly to voice mail entry |
| 804 | if (sendToVoicemail) { |
| 805 | entry.label = getString(R.string.actionIncomingCall); |
| 806 | entry.data = getString(R.string.detailIncomingCallsGoToVoicemail); |
| 807 | entry.actionIcon = R.drawable.sym_send_to_voicemail; |
| 808 | mOtherEntries.add(entry); |
| 809 | } else if (!TextUtils.isEmpty(ringtoneStr)) { |
| 810 | // Get the URI |
| 811 | Uri ringtoneUri = Uri.parse(ringtoneStr); |
| 812 | if (ringtoneUri != null) { |
| 813 | Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri); |
| 814 | if (ringtone != null) { |
| 815 | entry.label = getString(R.string.label_ringtone); |
| 816 | entry.data = ringtone.getTitle(this); |
| 817 | entry.uri = ringtoneUri; |
| 818 | entry.actionIcon = R.drawable.sym_ringtone; |
| 819 | mOtherEntries.add(entry); |
| 820 | } |
| 821 | } |
| 822 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 823 | // Load the photo |
| 824 | } else if (mimetype.equals(CommonDataKinds.Photo.CONTENT_ITEM_TYPE)) { |
| 825 | photoBitmap = ContactsUtils.loadContactPhoto(aggCursor, DATA_1_COLUMN, null); |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 826 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 827 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 828 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 829 | // TODO(emillar) Add group entries |
| 830 | // // Build the group entries |
| 831 | // final Uri groupsUri = Uri.withAppendedPath(mUri, GroupMembership.CONTENT_DIRECTORY); |
| 832 | // Cursor groupCursor = mResolver.query(groupsUri, ContactsListActivity.GROUPS_PROJECTION, |
| 833 | // null, null, Groups.DEFAULT_SORT_ORDER); |
| 834 | // if (groupCursor != null) { |
| 835 | // try { |
| 836 | // StringBuilder sb = new StringBuilder(); |
| 837 | // |
| 838 | // while (groupCursor.moveToNext()) { |
| 839 | // String systemId = groupCursor.getString( |
| 840 | // ContactsListActivity.GROUPS_COLUMN_INDEX_SYSTEM_ID); |
| 841 | // |
| 842 | // if (systemId != null || Groups.GROUP_MY_CONTACTS.equals(systemId)) { |
| 843 | // continue; |
| 844 | // } |
| 845 | // |
| 846 | // String name = groupCursor.getString(ContactsListActivity.GROUPS_COLUMN_INDEX_NAME); |
| 847 | // if (!TextUtils.isEmpty(name)) { |
| 848 | // if (sb.length() == 0) { |
| 849 | // sb.append(name); |
| 850 | // } else { |
| 851 | // sb.append(getString(R.string.group_list, name)); |
| 852 | // } |
| 853 | // } |
| 854 | // } |
| 855 | // |
| 856 | // if (sb.length() > 0) { |
| 857 | // ViewEntry entry = new ViewEntry(); |
| 858 | // entry.kind = ContactEntryAdapter.Entry.KIND_GROUP; |
| 859 | // entry.label = getString(R.string.label_groups); |
| 860 | // entry.data = sb.toString(); |
| 861 | // entry.intent = new Intent(Intent.ACTION_EDIT, mUri); |
| 862 | // |
| 863 | // // TODO: Add an icon for the groups item. |
| 864 | // |
| 865 | // mGroupEntries.add(entry); |
| 866 | // } |
| 867 | // } finally { |
| 868 | // groupCursor.close(); |
| 869 | // } |
| 870 | // } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 871 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 872 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 873 | |
| 874 | if (photoBitmap == null) { |
| 875 | photoBitmap = ContactsUtils.loadPlaceholderPhoto(mNoPhotoResource, this, null); |
| 876 | } |
| 877 | mPhotoView.setImageBitmap(photoBitmap); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 881 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 882 | String buildActionString(int actionResId, CharSequence type, boolean lowerCase) { |
Jeff Hamilton | 8350e5b | 2009-03-24 21:01:34 -0700 | [diff] [blame] | 883 | // If there is no type just display an empty string |
| 884 | if (type == null) { |
| 885 | type = ""; |
| 886 | } |
| 887 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 888 | if (lowerCase) { |
| 889 | return getString(actionResId, type.toString().toLowerCase()); |
| 890 | } else { |
| 891 | return getString(actionResId, type.toString()); |
| 892 | } |
| 893 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 894 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 895 | /** |
| 896 | * A basic structure with the data for a contact entry in the list. |
| 897 | */ |
| 898 | final static class ViewEntry extends ContactEntryAdapter.Entry { |
| 899 | public int primaryIcon = -1; |
| 900 | public Intent intent; |
| 901 | public Intent auxIntent = null; |
| 902 | public int presenceIcon = -1; |
| 903 | public int actionIcon = -1; |
| 904 | public int maxLabelLines = 1; |
| 905 | } |
| 906 | |
| 907 | private static final class ViewAdapter extends ContactEntryAdapter<ViewEntry> { |
| 908 | /** Cache of the children views of a row */ |
| 909 | static class ViewCache { |
| 910 | public TextView label; |
| 911 | public TextView data; |
| 912 | public ImageView actionIcon; |
| 913 | public ImageView presenceIcon; |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 914 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 915 | // Need to keep track of this too |
| 916 | ViewEntry entry; |
| 917 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 918 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 919 | ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) { |
| 920 | super(context, sections, SHOW_SEPARATORS); |
| 921 | } |
| 922 | |
| 923 | @Override |
| 924 | public View getView(int position, View convertView, ViewGroup parent) { |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 925 | ViewEntry entry = getEntry(mSections, position, false); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 926 | View v; |
| 927 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 928 | ViewCache views; |
| 929 | |
| 930 | // Check to see if we can reuse convertView |
| 931 | if (convertView != null) { |
| 932 | v = convertView; |
| 933 | views = (ViewCache) v.getTag(); |
| 934 | } else { |
| 935 | // Create a new view if needed |
| 936 | v = mInflater.inflate(R.layout.list_item_text_icons, parent, false); |
| 937 | |
| 938 | // Cache the children |
| 939 | views = new ViewCache(); |
| 940 | views.label = (TextView) v.findViewById(android.R.id.text1); |
| 941 | views.data = (TextView) v.findViewById(android.R.id.text2); |
| 942 | views.actionIcon = (ImageView) v.findViewById(R.id.icon1); |
| 943 | views.presenceIcon = (ImageView) v.findViewById(R.id.icon2); |
| 944 | v.setTag(views); |
| 945 | } |
| 946 | |
| 947 | // Update the entry in the view cache |
| 948 | views.entry = entry; |
| 949 | |
| 950 | // Bind the data to the view |
| 951 | bindView(v, entry); |
| 952 | return v; |
| 953 | } |
| 954 | |
| 955 | @Override |
| 956 | protected View newView(int position, ViewGroup parent) { |
| 957 | // getView() handles this |
| 958 | throw new UnsupportedOperationException(); |
| 959 | } |
| 960 | |
| 961 | @Override |
| 962 | protected void bindView(View view, ViewEntry entry) { |
| 963 | final Resources resources = mContext.getResources(); |
| 964 | ViewCache views = (ViewCache) view.getTag(); |
| 965 | |
| 966 | // Set the label |
| 967 | TextView label = views.label; |
| 968 | setMaxLines(label, entry.maxLabelLines); |
| 969 | label.setText(entry.label); |
| 970 | |
| 971 | // Set the data |
| 972 | TextView data = views.data; |
| 973 | if (data != null) { |
| 974 | data.setText(entry.data); |
| 975 | setMaxLines(data, entry.maxLines); |
| 976 | } |
| 977 | |
| 978 | // Set the action icon |
| 979 | ImageView action = views.actionIcon; |
| 980 | if (entry.actionIcon != -1) { |
| 981 | action.setImageDrawable(resources.getDrawable(entry.actionIcon)); |
| 982 | action.setVisibility(View.VISIBLE); |
| 983 | } else { |
| 984 | // Things should still line up as if there was an icon, so make it invisible |
| 985 | action.setVisibility(View.INVISIBLE); |
| 986 | } |
| 987 | |
| 988 | // Set the presence icon |
| 989 | Drawable presenceIcon = null; |
| 990 | if (entry.primaryIcon != -1) { |
| 991 | presenceIcon = resources.getDrawable(entry.primaryIcon); |
| 992 | } else if (entry.presenceIcon != -1) { |
| 993 | presenceIcon = resources.getDrawable(entry.presenceIcon); |
| 994 | } |
| 995 | |
| 996 | ImageView presence = views.presenceIcon; |
| 997 | if (presenceIcon != null) { |
| 998 | presence.setImageDrawable(presenceIcon); |
| 999 | presence.setVisibility(View.VISIBLE); |
| 1000 | } else { |
| 1001 | presence.setVisibility(View.GONE); |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | private void setMaxLines(TextView textView, int maxLines) { |
| 1006 | if (maxLines == 1) { |
| 1007 | textView.setSingleLine(true); |
| 1008 | textView.setEllipsize(TextUtils.TruncateAt.END); |
| 1009 | } else { |
| 1010 | textView.setSingleLine(false); |
| 1011 | textView.setMaxLines(maxLines); |
| 1012 | textView.setEllipsize(null); |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | } |