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