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