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