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