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