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