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