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