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