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