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; |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 25 | import com.android.contacts.util.DataStatus; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 26 | import com.android.contacts.util.NotifyingAsyncQueryHandler; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 27 | import com.android.internal.telephony.ITelephony; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 28 | import com.android.internal.widget.ContactHeaderWidget; |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 29 | import com.google.android.collect.Lists; |
| 30 | import com.google.android.collect.Maps; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 31 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 32 | import android.app.Activity; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 33 | import android.app.AlertDialog; |
| 34 | import android.app.Dialog; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 35 | import android.content.ActivityNotFoundException; |
| 36 | import android.content.ContentResolver; |
| 37 | import android.content.ContentUris; |
| 38 | import android.content.ContentValues; |
| 39 | import android.content.Context; |
| 40 | import android.content.DialogInterface; |
Evan Millar | 5f4af70 | 2009-08-11 11:12:00 -0700 | [diff] [blame] | 41 | import android.content.Entity; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 42 | import android.content.EntityIterator; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 43 | import android.content.Intent; |
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; |
Evan Millar | 951fa9c | 2009-10-26 14:57:43 -0700 | [diff] [blame] | 49 | import android.net.ParseException; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 50 | import android.net.Uri; |
Evan Millar | 951fa9c | 2009-10-26 14:57:43 -0700 | [diff] [blame] | 51 | import android.net.WebAddress; |
Brad Fitzpatrick | 23bc9b7 | 2010-03-23 23:27:34 -0700 | [diff] [blame] | 52 | import android.os.AsyncTask; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 53 | import android.os.Bundle; |
| 54 | import android.os.Handler; |
| 55 | import android.os.RemoteException; |
| 56 | import android.os.ServiceManager; |
Dmitri Plotnikov | 4e3a043 | 2009-09-15 13:54:40 -0700 | [diff] [blame] | 57 | import android.provider.ContactsContract; |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 58 | import android.provider.ContactsContract.AggregationExceptions; |
| 59 | import android.provider.ContactsContract.CommonDataKinds; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 60 | import android.provider.ContactsContract.Contacts; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 61 | import android.provider.ContactsContract.Data; |
Daniel Lehmann | 350ba6f | 2010-03-23 18:10:57 -0700 | [diff] [blame] | 62 | import android.provider.ContactsContract.DisplayNameSources; |
Dmitri Plotnikov | 3946659 | 2009-07-27 11:23:51 -0700 | [diff] [blame] | 63 | import android.provider.ContactsContract.RawContacts; |
Fred Quintana | 8579e25 | 2009-12-07 14:50:12 -0800 | [diff] [blame] | 64 | import android.provider.ContactsContract.RawContactsEntity; |
Dmitri Plotnikov | 160e9da | 2009-09-30 23:48:12 -0700 | [diff] [blame] | 65 | import android.provider.ContactsContract.StatusUpdates; |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 66 | import android.provider.ContactsContract.CommonDataKinds.Email; |
| 67 | import android.provider.ContactsContract.CommonDataKinds.Im; |
| 68 | import android.provider.ContactsContract.CommonDataKinds.Nickname; |
| 69 | import android.provider.ContactsContract.CommonDataKinds.Note; |
| 70 | import android.provider.ContactsContract.CommonDataKinds.Organization; |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 71 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 72 | import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; |
Evan Millar | 951fa9c | 2009-10-26 14:57:43 -0700 | [diff] [blame] | 73 | import android.provider.ContactsContract.CommonDataKinds.Website; |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 74 | import android.telephony.PhoneNumberUtils; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 75 | import android.text.TextUtils; |
| 76 | import android.util.Log; |
| 77 | import android.view.ContextMenu; |
| 78 | import android.view.KeyEvent; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 79 | import android.view.LayoutInflater; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 80 | import android.view.Menu; |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 81 | import android.view.MenuInflater; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 82 | import android.view.MenuItem; |
| 83 | import android.view.View; |
| 84 | import android.view.ViewGroup; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 85 | import android.view.Window; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 86 | import android.view.ContextMenu.ContextMenuInfo; |
| 87 | import android.widget.AdapterView; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 88 | import android.widget.ImageView; |
| 89 | import android.widget.ListView; |
| 90 | import android.widget.TextView; |
| 91 | import android.widget.Toast; |
| 92 | |
| 93 | import java.util.ArrayList; |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 94 | import java.util.HashMap; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 95 | |
| 96 | /** |
| 97 | * Displays the details of a specific contact. |
| 98 | */ |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 99 | public class ViewContactActivity extends Activity |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 100 | implements View.OnCreateContextMenuListener, DialogInterface.OnClickListener, |
Evan Millar | df18120 | 2009-09-30 15:15:51 -0700 | [diff] [blame] | 101 | AdapterView.OnItemClickListener, NotifyingAsyncQueryHandler.AsyncQueryListener { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 102 | private static final String TAG = "ViewContact"; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 103 | |
| 104 | private static final boolean SHOW_SEPARATORS = false; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 105 | |
| 106 | private static final int DIALOG_CONFIRM_DELETE = 1; |
Megha Joshi | d5afd8a | 2009-09-22 09:38:41 -0700 | [diff] [blame] | 107 | private static final int DIALOG_CONFIRM_READONLY_DELETE = 2; |
| 108 | private static final int DIALOG_CONFIRM_MULTIPLE_DELETE = 3; |
Megha Joshi | c508bd8 | 2009-10-01 17:31:49 -0700 | [diff] [blame] | 109 | private static final int DIALOG_CONFIRM_READONLY_HIDE = 4; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 110 | |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame] | 111 | private static final int REQUEST_JOIN_CONTACT = 1; |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 112 | private static final int REQUEST_EDIT_CONTACT = 2; |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 113 | |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame] | 114 | public static final int MENU_ITEM_MAKE_DEFAULT = 3; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 115 | |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 116 | protected Uri mLookupUri; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 117 | private ContentResolver mResolver; |
| 118 | private ViewAdapter mAdapter; |
| 119 | private int mNumPhoneNumbers = 0; |
| 120 | |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 121 | /** |
Dmitri Plotnikov | e1cd679 | 2009-07-27 20:28:17 -0700 | [diff] [blame] | 122 | * A list of distinct contact IDs included in the current contact. |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 123 | */ |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 124 | private ArrayList<Long> mRawContactIds = new ArrayList<Long>(); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 125 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 126 | /* package */ ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>(); |
| 127 | /* package */ ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>(); |
| 128 | /* package */ ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>(); |
| 129 | /* package */ ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>(); |
| 130 | /* package */ ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>(); |
Daniel Lehmann | e12f8a9 | 2010-02-24 18:37:56 -0800 | [diff] [blame] | 131 | /* package */ ArrayList<ViewEntry> mNicknameEntries = new ArrayList<ViewEntry>(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 132 | /* package */ ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>(); |
The Android Open Source Project | cac191e | 2009-03-18 22:20:27 -0700 | [diff] [blame] | 133 | /* package */ ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 134 | /* package */ ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>(); |
| 135 | /* package */ ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>(); |
| 136 | |
| 137 | private Cursor mCursor; |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 138 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 139 | protected ContactHeaderWidget mContactHeaderWidget; |
| 140 | private NotifyingAsyncQueryHandler mHandler; |
| 141 | |
| 142 | protected LayoutInflater mInflater; |
| 143 | |
Megha Joshi | d5afd8a | 2009-09-22 09:38:41 -0700 | [diff] [blame] | 144 | protected int mReadOnlySourcesCnt; |
| 145 | protected int mWritableSourcesCnt; |
Jeff Sharkey | e040876 | 2009-11-03 12:57:16 -0800 | [diff] [blame] | 146 | protected boolean mAllRestricted; |
| 147 | |
Jeff Sharkey | 827762d | 2009-12-23 11:06:08 -0800 | [diff] [blame] | 148 | protected Uri mPrimaryPhoneUri = null; |
| 149 | |
Megha Joshi | d5afd8a | 2009-09-22 09:38:41 -0700 | [diff] [blame] | 150 | protected ArrayList<Long> mWritableRawContactIds = new ArrayList<Long>(); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 151 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 152 | private static final int TOKEN_ENTITIES = 0; |
| 153 | private static final int TOKEN_STATUSES = 1; |
| 154 | |
| 155 | private boolean mHasEntities = false; |
| 156 | private boolean mHasStatuses = false; |
| 157 | |
Daniel Lehmann | 350ba6f | 2010-03-23 18:10:57 -0700 | [diff] [blame] | 158 | private long mNameRawContactId = -1; |
| 159 | private int mDisplayNameSource = DisplayNameSources.UNDEFINED; |
| 160 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 161 | private ArrayList<Entity> mEntities = Lists.newArrayList(); |
| 162 | private HashMap<Long, DataStatus> mStatuses = Maps.newHashMap(); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 163 | |
Makoto Onuki | 7a70738 | 2009-11-19 13:35:55 +0900 | [diff] [blame] | 164 | /** |
| 165 | * The view shown if the detail list is empty. |
| 166 | * We set this to the list view when first bind the adapter, so that it won't be shown while |
| 167 | * we're loading data. |
| 168 | */ |
| 169 | private View mEmptyView; |
| 170 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 171 | private ContentObserver mObserver = new ContentObserver(new Handler()) { |
| 172 | @Override |
| 173 | public boolean deliverSelfNotifications() { |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public void onChange(boolean selfChange) { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 179 | if (mCursor != null && !mCursor.isClosed()) { |
| 180 | startEntityQuery(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | public void onClick(DialogInterface dialog, int which) { |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 186 | closeCursor(); |
Megha Joshi | c508bd8 | 2009-10-01 17:31:49 -0700 | [diff] [blame] | 187 | getContentResolver().delete(mLookupUri, null, null); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 188 | finish(); |
| 189 | } |
| 190 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 191 | private ListView mListView; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 192 | private boolean mShowSmsLinksForAllPhones; |
| 193 | |
| 194 | @Override |
| 195 | protected void onCreate(Bundle icicle) { |
| 196 | super.onCreate(icicle); |
| 197 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 198 | final Intent intent = getIntent(); |
Dmitri Plotnikov | 4e3a043 | 2009-09-15 13:54:40 -0700 | [diff] [blame] | 199 | Uri data = intent.getData(); |
| 200 | String authority = data.getAuthority(); |
| 201 | if (ContactsContract.AUTHORITY.equals(authority)) { |
| 202 | mLookupUri = data; |
| 203 | } else if (android.provider.Contacts.AUTHORITY.equals(authority)) { |
| 204 | final long rawContactId = ContentUris.parseId(data); |
| 205 | mLookupUri = RawContacts.getContactLookupUri(getContentResolver(), |
| 206 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId)); |
Dmitri Plotnikov | 83129f0 | 2009-09-02 19:04:41 -0700 | [diff] [blame] | 207 | |
Dmitri Plotnikov | 4e3a043 | 2009-09-15 13:54:40 -0700 | [diff] [blame] | 208 | } |
Dmitri Plotnikov | 83129f0 | 2009-09-02 19:04:41 -0700 | [diff] [blame] | 209 | mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 210 | |
| 211 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 212 | setContentView(R.layout.contact_card_layout); |
| 213 | |
| 214 | mContactHeaderWidget = (ContactHeaderWidget) findViewById(R.id.contact_header_widget); |
| 215 | mContactHeaderWidget.showStar(true); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 216 | mContactHeaderWidget.setExcludeMimes(new String[] { |
| 217 | Contacts.CONTENT_ITEM_TYPE |
| 218 | }); |
| 219 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 220 | mHandler = new NotifyingAsyncQueryHandler(this, this); |
| 221 | |
Evan Millar | cdeea94 | 2009-10-07 11:47:35 -0700 | [diff] [blame] | 222 | mListView = (ListView) findViewById(R.id.contact_data); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 223 | mListView.setOnCreateContextMenuListener(this); |
| 224 | mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY); |
| 225 | mListView.setOnItemClickListener(this); |
Makoto Onuki | 7a70738 | 2009-11-19 13:35:55 +0900 | [diff] [blame] | 226 | // Don't set it to mListView yet. We do so later when we bind the adapter. |
| 227 | mEmptyView = findViewById(android.R.id.empty); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 228 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 229 | mResolver = getContentResolver(); |
| 230 | |
| 231 | // Build the list of sections. The order they're added to mSections dictates the |
| 232 | // order they are displayed in the list. |
| 233 | mSections.add(mPhoneEntries); |
| 234 | mSections.add(mSmsEntries); |
| 235 | mSections.add(mEmailEntries); |
| 236 | mSections.add(mImEntries); |
| 237 | mSections.add(mPostalEntries); |
Daniel Lehmann | e12f8a9 | 2010-02-24 18:37:56 -0800 | [diff] [blame] | 238 | mSections.add(mNicknameEntries); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 239 | mSections.add(mOrganizationEntries); |
The Android Open Source Project | cac191e | 2009-03-18 22:20:27 -0700 | [diff] [blame] | 240 | mSections.add(mGroupEntries); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 241 | mSections.add(mOtherEntries); |
| 242 | |
| 243 | //TODO Read this value from a preference |
| 244 | mShowSmsLinksForAllPhones = true; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 245 | } |
| 246 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 247 | @Override |
| 248 | protected void onResume() { |
| 249 | super.onResume(); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 250 | startEntityQuery(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Override |
| 254 | protected void onPause() { |
| 255 | super.onPause(); |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 256 | closeCursor(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Override |
| 260 | protected void onDestroy() { |
| 261 | super.onDestroy(); |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 262 | closeCursor(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
| 266 | protected Dialog onCreateDialog(int id) { |
| 267 | switch (id) { |
| 268 | case DIALOG_CONFIRM_DELETE: |
| 269 | return new AlertDialog.Builder(this) |
| 270 | .setTitle(R.string.deleteConfirmation_title) |
| 271 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 272 | .setMessage(R.string.deleteConfirmation) |
| 273 | .setNegativeButton(android.R.string.cancel, null) |
| 274 | .setPositiveButton(android.R.string.ok, this) |
| 275 | .setCancelable(false) |
| 276 | .create(); |
Megha Joshi | d5afd8a | 2009-09-22 09:38:41 -0700 | [diff] [blame] | 277 | case DIALOG_CONFIRM_READONLY_DELETE: |
| 278 | return new AlertDialog.Builder(this) |
| 279 | .setTitle(R.string.deleteConfirmation_title) |
| 280 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 281 | .setMessage(R.string.readOnlyContactDeleteConfirmation) |
| 282 | .setNegativeButton(android.R.string.cancel, null) |
| 283 | .setPositiveButton(android.R.string.ok, this) |
| 284 | .setCancelable(false) |
| 285 | .create(); |
| 286 | case DIALOG_CONFIRM_MULTIPLE_DELETE: |
| 287 | return new AlertDialog.Builder(this) |
| 288 | .setTitle(R.string.deleteConfirmation_title) |
| 289 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 290 | .setMessage(R.string.multipleContactDeleteConfirmation) |
| 291 | .setNegativeButton(android.R.string.cancel, null) |
| 292 | .setPositiveButton(android.R.string.ok, this) |
| 293 | .setCancelable(false) |
| 294 | .create(); |
Evan Millar | cdeea94 | 2009-10-07 11:47:35 -0700 | [diff] [blame] | 295 | case DIALOG_CONFIRM_READONLY_HIDE: { |
Megha Joshi | c508bd8 | 2009-10-01 17:31:49 -0700 | [diff] [blame] | 296 | return new AlertDialog.Builder(this) |
| 297 | .setTitle(R.string.deleteConfirmation_title) |
| 298 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 299 | .setMessage(R.string.readOnlyContactWarning) |
| 300 | .setPositiveButton(android.R.string.ok, this) |
Evan Millar | cdeea94 | 2009-10-07 11:47:35 -0700 | [diff] [blame] | 301 | .create(); |
Megha Joshi | c508bd8 | 2009-10-01 17:31:49 -0700 | [diff] [blame] | 302 | } |
| 303 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 304 | } |
| 305 | return null; |
| 306 | } |
| 307 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 308 | /** {@inheritDoc} */ |
Brad Fitzpatrick | 23bc9b7 | 2010-03-23 23:27:34 -0700 | [diff] [blame] | 309 | public void onQueryComplete(int token, Object cookie, final Cursor cursor) { |
Fred Quintana | 8579e25 | 2009-12-07 14:50:12 -0800 | [diff] [blame] | 310 | if (token == TOKEN_STATUSES) { |
| 311 | try { |
| 312 | // Read available social rows and consider binding |
| 313 | readStatuses(cursor); |
| 314 | } finally { |
| 315 | if (cursor != null) { |
| 316 | cursor.close(); |
| 317 | } |
| 318 | } |
Brad Fitzpatrick | 23bc9b7 | 2010-03-23 23:27:34 -0700 | [diff] [blame] | 319 | considerBindData(); |
| 320 | return; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 321 | } |
Brad Fitzpatrick | 23bc9b7 | 2010-03-23 23:27:34 -0700 | [diff] [blame] | 322 | |
Brad Fitzpatrick | 1b28a0a | 2010-03-24 10:53:51 -0700 | [diff] [blame] | 323 | // One would think we could just iterate over the Cursor |
| 324 | // directly here, as the result set should be small, and we've |
| 325 | // already run the query in an AsyncTask, but a lot of ANRs |
| 326 | // were being reported in this code nonetheless. See bug |
| 327 | // 2539603 for details. The real bug which makes this result |
| 328 | // set huge and CPU-heavy may be elsewhere. |
| 329 | // TODO: if we keep this async, perhaps the entity iteration |
| 330 | // should also be original AsyncTask, rather than ping-ponging |
| 331 | // between threads like this. |
Brad Fitzpatrick | 23bc9b7 | 2010-03-23 23:27:34 -0700 | [diff] [blame] | 332 | final ArrayList<Entity> oldEntities = mEntities; |
| 333 | (new AsyncTask<Void, Void, ArrayList<Entity>>() { |
| 334 | @Override |
| 335 | protected ArrayList<Entity> doInBackground(Void... params) { |
| 336 | ArrayList<Entity> newEntities = Lists.newArrayList(); |
| 337 | EntityIterator iterator = RawContacts.newEntityIterator(cursor); |
| 338 | try { |
| 339 | while (iterator.hasNext()) { |
| 340 | Entity entity = iterator.next(); |
| 341 | newEntities.add(entity); |
| 342 | } |
| 343 | } catch (RemoteException e) { |
| 344 | Log.w(TAG, "Problem reading contact data: " + e.toString()); |
| 345 | return null; |
| 346 | } finally { |
| 347 | iterator.close(); |
| 348 | } |
| 349 | return newEntities; |
| 350 | } |
| 351 | |
| 352 | @Override |
| 353 | protected void onPostExecute(ArrayList<Entity> newEntities) { |
| 354 | if (newEntities == null) { |
| 355 | // There was an error loading. |
| 356 | return; |
| 357 | } |
| 358 | synchronized (ViewContactActivity.this) { |
| 359 | if (mEntities != oldEntities) { |
| 360 | // Multiple async tasks were in flight and we |
| 361 | // lost the race. |
| 362 | return; |
| 363 | } |
| 364 | mEntities = newEntities; |
| 365 | mHasEntities = true; |
| 366 | } |
| 367 | considerBindData(); |
| 368 | } |
| 369 | }).execute(); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Evan Millar | 6a61a1a | 2009-09-29 14:00:43 -0700 | [diff] [blame] | 372 | private long getRefreshedContactId() { |
| 373 | Uri freshContactUri = Contacts.lookupContact(getContentResolver(), mLookupUri); |
| 374 | if (freshContactUri != null) { |
| 375 | return ContentUris.parseId(freshContactUri); |
| 376 | } |
| 377 | return -1; |
| 378 | } |
| 379 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 380 | /** |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 381 | * Read from the given {@link Cursor} and build a set of {@link DataStatus} |
| 382 | * objects to match any valid statuses found. |
| 383 | */ |
| 384 | private synchronized void readStatuses(Cursor cursor) { |
| 385 | mStatuses.clear(); |
| 386 | |
| 387 | // Walk found statuses, creating internal row for each |
| 388 | while (cursor.moveToNext()) { |
| 389 | final DataStatus status = new DataStatus(cursor); |
| 390 | final long dataId = cursor.getLong(StatusQuery._ID); |
| 391 | mStatuses.put(dataId, status); |
| 392 | } |
| 393 | |
| 394 | mHasStatuses = true; |
| 395 | } |
| 396 | |
| 397 | private synchronized void startEntityQuery() { |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 398 | closeCursor(); |
| 399 | |
Evan Millar | 6a61a1a | 2009-09-29 14:00:43 -0700 | [diff] [blame] | 400 | Uri uri = null; |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 401 | if (mLookupUri != null) { |
| 402 | mLookupUri = Contacts.getLookupUri(getContentResolver(), mLookupUri); |
| 403 | if (mLookupUri != null) { |
Evan Millar | 6a61a1a | 2009-09-29 14:00:43 -0700 | [diff] [blame] | 404 | uri = Contacts.lookupContact(getContentResolver(), mLookupUri); |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Evan Millar | 6a61a1a | 2009-09-29 14:00:43 -0700 | [diff] [blame] | 408 | if (uri == null) { |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 409 | |
| 410 | // TODO either figure out a way to prevent a flash of black background or |
| 411 | // use some other UI than a toast |
| 412 | Toast.makeText(this, R.string.invalidContactMessage, Toast.LENGTH_SHORT).show(); |
| 413 | Log.e(TAG, "invalid contact uri: " + mLookupUri); |
| 414 | finish(); |
| 415 | return; |
| 416 | } |
| 417 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 418 | final Uri dataUri = Uri.withAppendedPath(uri, Contacts.Data.CONTENT_DIRECTORY); |
| 419 | |
Daniel Lehmann | 350ba6f | 2010-03-23 18:10:57 -0700 | [diff] [blame] | 420 | // This cursor has two purposes: |
| 421 | // - Fetch NAME_RAW_CONTACT_ID and DISPLAY_NAME_SOURCE |
| 422 | // - Watcher for change events |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 423 | mCursor = mResolver.query(dataUri, |
Daniel Lehmann | 350ba6f | 2010-03-23 18:10:57 -0700 | [diff] [blame] | 424 | new String[] { Contacts.NAME_RAW_CONTACT_ID, Contacts.DISPLAY_NAME_SOURCE }, |
| 425 | null, null, null); |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 426 | |
Daniel Lehmann | 350ba6f | 2010-03-23 18:10:57 -0700 | [diff] [blame] | 427 | if (mCursor.moveToFirst()) { |
| 428 | mNameRawContactId = |
| 429 | mCursor.getLong(mCursor.getColumnIndex(Contacts.NAME_RAW_CONTACT_ID)); |
| 430 | mDisplayNameSource = |
| 431 | mCursor.getInt(mCursor.getColumnIndex(Contacts.DISPLAY_NAME_SOURCE)); |
| 432 | } else { |
| 433 | mNameRawContactId = -1; |
| 434 | mDisplayNameSource = DisplayNameSources.UNDEFINED; |
| 435 | } |
| 436 | |
| 437 | mCursor.registerContentObserver(mObserver); |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 438 | final long contactId = ContentUris.parseId(uri); |
| 439 | |
| 440 | // Clear flags and start queries to data and status |
| 441 | mHasEntities = false; |
| 442 | mHasStatuses = false; |
| 443 | |
Fred Quintana | 8579e25 | 2009-12-07 14:50:12 -0800 | [diff] [blame] | 444 | mHandler.startQuery(TOKEN_ENTITIES, null, RawContactsEntity.CONTENT_URI, null, |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 445 | RawContacts.CONTACT_ID + "=" + contactId, null, null); |
| 446 | mHandler.startQuery(TOKEN_STATUSES, null, dataUri, StatusQuery.PROJECTION, |
| 447 | StatusUpdates.PRESENCE + " IS NOT NULL OR " + StatusUpdates.STATUS |
| 448 | + " IS NOT NULL", null, null); |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 449 | |
Evan Millar | 9b1a124 | 2009-09-16 10:26:10 -0700 | [diff] [blame] | 450 | mContactHeaderWidget.bindFromContactLookupUri(mLookupUri); |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | private void closeCursor() { |
| 454 | if (mCursor != null) { |
| 455 | mCursor.unregisterContentObserver(mObserver); |
| 456 | mCursor.close(); |
| 457 | mCursor = null; |
| 458 | } |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 461 | /** |
| 462 | * Consider binding views after any of several background queries has |
| 463 | * completed. We check internal flags and only bind when all data has |
| 464 | * arrived. |
| 465 | */ |
| 466 | private void considerBindData() { |
| 467 | if (mHasEntities && mHasStatuses) { |
| 468 | bindData(); |
| 469 | } |
| 470 | } |
| 471 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 472 | private void bindData() { |
| 473 | |
| 474 | // Build up the contact entries |
| 475 | buildEntries(); |
| 476 | |
| 477 | // Collapse similar data items in select sections. |
| 478 | Collapser.collapseList(mPhoneEntries); |
| 479 | Collapser.collapseList(mSmsEntries); |
| 480 | Collapser.collapseList(mEmailEntries); |
| 481 | Collapser.collapseList(mPostalEntries); |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 482 | Collapser.collapseList(mImEntries); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 483 | |
| 484 | if (mAdapter == null) { |
| 485 | mAdapter = new ViewAdapter(this, mSections); |
| 486 | mListView.setAdapter(mAdapter); |
| 487 | } else { |
| 488 | mAdapter.setSections(mSections, SHOW_SEPARATORS); |
| 489 | } |
Makoto Onuki | 7a70738 | 2009-11-19 13:35:55 +0900 | [diff] [blame] | 490 | mListView.setEmptyView(mEmptyView); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public boolean onCreateOptionsMenu(Menu menu) { |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 495 | super.onCreateOptionsMenu(menu); |
| 496 | |
| 497 | final MenuInflater inflater = getMenuInflater(); |
| 498 | inflater.inflate(R.menu.view, menu); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 499 | return true; |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 504 | super.onPrepareOptionsMenu(menu); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 505 | |
Evan Millar | 51514db | 2009-09-22 11:42:35 -0700 | [diff] [blame] | 506 | // Only allow edit when we have at least one raw_contact id |
| 507 | final boolean hasRawContact = (mRawContactIds.size() > 0); |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 508 | menu.findItem(R.id.menu_edit).setEnabled(hasRawContact); |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 509 | |
Jeff Sharkey | e040876 | 2009-11-03 12:57:16 -0800 | [diff] [blame] | 510 | // Only allow share when unrestricted contacts available |
| 511 | menu.findItem(R.id.menu_share).setEnabled(!mAllRestricted); |
| 512 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 513 | return true; |
| 514 | } |
| 515 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 516 | @Override |
| 517 | public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { |
| 518 | AdapterView.AdapterContextMenuInfo info; |
| 519 | try { |
| 520 | info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
| 521 | } catch (ClassCastException e) { |
| 522 | Log.e(TAG, "bad menuInfo", e); |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | // This can be null sometimes, don't crash... |
| 527 | if (info == null) { |
| 528 | Log.e(TAG, "bad menuInfo"); |
| 529 | return; |
| 530 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 531 | |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 532 | ViewEntry entry = ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS); |
Neel Parekh | a8fb68a | 2009-09-25 18:05:18 -0700 | [diff] [blame] | 533 | menu.setHeaderTitle(R.string.contactOptionsTitle); |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 534 | if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) { |
| 535 | menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent); |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 536 | menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent); |
| 537 | if (!entry.isPrimary) { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 538 | 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] | 539 | } |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 540 | } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) { |
| 541 | menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent); |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 542 | if (!entry.isPrimary) { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 543 | 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] | 544 | } |
Jeff Sharkey | c6ad3ab | 2009-07-21 19:30:15 -0700 | [diff] [blame] | 545 | } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) { |
Evan Millar | 45e0ed3 | 2009-06-01 16:44:38 -0700 | [diff] [blame] | 546 | menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent); |
| 547 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | @Override |
| 551 | public boolean onOptionsItemSelected(MenuItem item) { |
| 552 | switch (item.getItemId()) { |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 553 | case R.id.menu_edit: { |
Evan Millar | df18120 | 2009-09-30 15:15:51 -0700 | [diff] [blame] | 554 | Long rawContactIdToEdit = null; |
| 555 | if (mRawContactIds.size() > 0) { |
| 556 | rawContactIdToEdit = mRawContactIds.get(0); |
| 557 | } else { |
| 558 | // There is no rawContact to edit. |
| 559 | break; |
Evan Millar | db5d88c | 2009-08-28 09:31:57 -0700 | [diff] [blame] | 560 | } |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 561 | Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, |
| 562 | rawContactIdToEdit); |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 563 | startActivityForResult(new Intent(Intent.ACTION_EDIT, rawContactUri), |
| 564 | REQUEST_EDIT_CONTACT); |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame] | 565 | break; |
| 566 | } |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 567 | case R.id.menu_delete: { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 568 | // Get confirmation |
Evan Millar | cdeea94 | 2009-10-07 11:47:35 -0700 | [diff] [blame] | 569 | if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) { |
Megha Joshi | d5afd8a | 2009-09-22 09:38:41 -0700 | [diff] [blame] | 570 | showDialog(DIALOG_CONFIRM_READONLY_DELETE); |
Evan Millar | cdeea94 | 2009-10-07 11:47:35 -0700 | [diff] [blame] | 571 | } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) { |
Megha Joshi | c508bd8 | 2009-10-01 17:31:49 -0700 | [diff] [blame] | 572 | showDialog(DIALOG_CONFIRM_READONLY_HIDE); |
Evan Millar | cdeea94 | 2009-10-07 11:47:35 -0700 | [diff] [blame] | 573 | } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) { |
| 574 | showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE); |
| 575 | } else { |
| 576 | showDialog(DIALOG_CONFIRM_DELETE); |
| 577 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 578 | return true; |
| 579 | } |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 580 | case R.id.menu_join: { |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 581 | showJoinAggregateActivity(); |
| 582 | return true; |
| 583 | } |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 584 | case R.id.menu_options: { |
Dmitri Plotnikov | ef03872 | 2009-06-24 18:51:47 -0700 | [diff] [blame] | 585 | showOptionsActivity(); |
| 586 | return true; |
| 587 | } |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 588 | case R.id.menu_share: { |
Jeff Sharkey | e040876 | 2009-11-03 12:57:16 -0800 | [diff] [blame] | 589 | if (mAllRestricted) return false; |
| 590 | |
Jeff Sharkey | 9304931 | 2009-09-27 17:28:13 -0700 | [diff] [blame] | 591 | // TODO: Keep around actual LOOKUP_KEY, or formalize method of extracting |
Daniel Lehmann | cbf7595 | 2010-03-12 13:29:07 -0800 | [diff] [blame] | 592 | final String lookupKey = Uri.encode(mLookupUri.getPathSegments().get(2)); |
Jeff Sharkey | 9304931 | 2009-09-27 17:28:13 -0700 | [diff] [blame] | 593 | final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey); |
| 594 | |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 595 | final Intent intent = new Intent(Intent.ACTION_SEND); |
Jeff Sharkey | 9304931 | 2009-09-27 17:28:13 -0700 | [diff] [blame] | 596 | intent.setType(Contacts.CONTENT_VCARD_TYPE); |
| 597 | intent.putExtra(Intent.EXTRA_STREAM, shareUri); |
Dmitri Plotnikov | ef03872 | 2009-06-24 18:51:47 -0700 | [diff] [blame] | 598 | |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 599 | // Launch chooser to share contact via |
| 600 | final CharSequence chooseTitle = getText(R.string.share_via); |
| 601 | final Intent chooseIntent = Intent.createChooser(intent, chooseTitle); |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 602 | |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 603 | try { |
| 604 | startActivity(chooseIntent); |
| 605 | } catch (ActivityNotFoundException ex) { |
| 606 | 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] | 607 | } |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 608 | return true; |
| 609 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 610 | } |
| 611 | return super.onOptionsItemSelected(item); |
| 612 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 613 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 614 | @Override |
| 615 | public boolean onContextItemSelected(MenuItem item) { |
| 616 | switch (item.getItemId()) { |
| 617 | case MENU_ITEM_MAKE_DEFAULT: { |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 618 | if (makeItemDefault(item)) { |
| 619 | return true; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 620 | } |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 621 | break; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 622 | } |
| 623 | } |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 624 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 625 | return super.onContextItemSelected(item); |
| 626 | } |
| 627 | |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 628 | private boolean makeItemDefault(MenuItem item) { |
| 629 | ViewEntry entry = getViewEntryForMenuItem(item); |
| 630 | if (entry == null) { |
| 631 | return false; |
| 632 | } |
| 633 | |
| 634 | // Update the primary values in the data record. |
Evan Millar | 14fecb6 | 2009-09-09 09:23:12 -0700 | [diff] [blame] | 635 | ContentValues values = new ContentValues(1); |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 636 | values.put(Data.IS_SUPER_PRIMARY, 1); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 637 | getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id), |
| 638 | values, null, null); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 639 | startEntityQuery(); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 640 | return true; |
| 641 | } |
| 642 | |
| 643 | /** |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 644 | * 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] | 645 | */ |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 646 | public void showJoinAggregateActivity() { |
Evan Millar | 6a61a1a | 2009-09-29 14:00:43 -0700 | [diff] [blame] | 647 | long freshId = getRefreshedContactId(); |
| 648 | if (freshId > 0) { |
| 649 | String displayName = null; |
| 650 | if (mCursor.moveToFirst()) { |
| 651 | displayName = mCursor.getString(0); |
| 652 | } |
| 653 | Intent intent = new Intent(ContactsListActivity.JOIN_AGGREGATE); |
| 654 | intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_ID, freshId); |
| 655 | if (displayName != null) { |
| 656 | intent.putExtra(ContactsListActivity.EXTRA_AGGREGATE_NAME, displayName); |
| 657 | } |
| 658 | startActivityForResult(intent, REQUEST_JOIN_CONTACT); |
Dmitri Plotnikov | a5cfca3 | 2009-09-29 18:13:08 -0700 | [diff] [blame] | 659 | } |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 660 | } |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 661 | |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 662 | @Override |
| 663 | protected void onActivityResult(int requestCode, int resultCode, Intent intent) { |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 664 | if (requestCode == REQUEST_JOIN_CONTACT) { |
| 665 | if (resultCode == RESULT_OK && intent != null) { |
| 666 | final long contactId = ContentUris.parseId(intent.getData()); |
| 667 | joinAggregate(contactId); |
Evan Millar | 8a79cee | 2009-08-19 17:20:49 -0700 | [diff] [blame] | 668 | } |
Evan Millar | 9cd8124 | 2009-09-26 16:02:31 -0700 | [diff] [blame] | 669 | } else if (requestCode == REQUEST_EDIT_CONTACT) { |
Evan Millar | df18120 | 2009-09-30 15:15:51 -0700 | [diff] [blame] | 670 | if (resultCode == EditContactActivity.RESULT_CLOSE_VIEW_ACTIVITY) { |
| 671 | finish(); |
Jeff Hamilton | 5297c6a | 2009-10-01 02:22:33 -0700 | [diff] [blame] | 672 | } else if (resultCode == Activity.RESULT_OK) { |
Dmitri Plotnikov | 160e9da | 2009-09-30 23:48:12 -0700 | [diff] [blame] | 673 | mLookupUri = intent.getData(); |
| 674 | if (mLookupUri == null) { |
| 675 | finish(); |
| 676 | } |
Evan Millar | df18120 | 2009-09-30 15:15:51 -0700 | [diff] [blame] | 677 | } |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
Dmitri Plotnikov | f40ccdf | 2009-09-04 17:37:15 -0700 | [diff] [blame] | 681 | private void joinAggregate(final long contactId) { |
Dmitri Plotnikov | 3946659 | 2009-07-27 11:23:51 -0700 | [diff] [blame] | 682 | Cursor c = mResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID}, |
Dmitri Plotnikov | f40ccdf | 2009-09-04 17:37:15 -0700 | [diff] [blame] | 683 | RawContacts.CONTACT_ID + "=" + contactId, null, null); |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 684 | |
| 685 | try { |
| 686 | while(c.moveToNext()) { |
Dmitri Plotnikov | f40ccdf | 2009-09-04 17:37:15 -0700 | [diff] [blame] | 687 | long rawContactId = c.getLong(0); |
| 688 | setAggregationException(rawContactId, AggregationExceptions.TYPE_KEEP_TOGETHER); |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 689 | } |
| 690 | } finally { |
| 691 | c.close(); |
| 692 | } |
| 693 | |
Dmitri Plotnikov | 040dc15 | 2009-09-03 15:17:56 -0700 | [diff] [blame] | 694 | Toast.makeText(this, R.string.contactsJoinedMessage, Toast.LENGTH_LONG).show(); |
| 695 | startEntityQuery(); |
Dmitri Plotnikov | 49f705f | 2009-06-17 18:31:56 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Given a contact ID sets an aggregation exception to either join the contact with the |
| 700 | * current aggregate or split off. |
| 701 | */ |
Dmitri Plotnikov | 99eafe7 | 2009-09-03 14:09:45 -0700 | [diff] [blame] | 702 | protected void setAggregationException(long rawContactId, int exceptionType) { |
Dmitri Plotnikov | d09f75c | 2009-06-16 11:59:22 -0700 | [diff] [blame] | 703 | ContentValues values = new ContentValues(3); |
Dmitri Plotnikov | f40ccdf | 2009-09-04 17:37:15 -0700 | [diff] [blame] | 704 | for (long aRawContactId : mRawContactIds) { |
| 705 | if (aRawContactId != rawContactId) { |
| 706 | values.put(AggregationExceptions.RAW_CONTACT_ID1, aRawContactId); |
| 707 | values.put(AggregationExceptions.RAW_CONTACT_ID2, rawContactId); |
| 708 | values.put(AggregationExceptions.TYPE, exceptionType); |
| 709 | mResolver.update(AggregationExceptions.CONTENT_URI, values, null, null); |
| 710 | } |
| 711 | } |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Dmitri Plotnikov | ef03872 | 2009-06-24 18:51:47 -0700 | [diff] [blame] | 714 | private void showOptionsActivity() { |
| 715 | final Intent intent = new Intent(this, ContactOptionsActivity.class); |
Evan Millar | 6a61a1a | 2009-09-29 14:00:43 -0700 | [diff] [blame] | 716 | intent.setData(mLookupUri); |
Dmitri Plotnikov | ef03872 | 2009-06-24 18:51:47 -0700 | [diff] [blame] | 717 | startActivity(intent); |
| 718 | } |
| 719 | |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 720 | private ViewEntry getViewEntryForMenuItem(MenuItem item) { |
| 721 | AdapterView.AdapterContextMenuInfo info; |
| 722 | try { |
| 723 | info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); |
| 724 | } catch (ClassCastException e) { |
| 725 | Log.e(TAG, "bad menuInfo", e); |
| 726 | return null; |
| 727 | } |
| 728 | |
| 729 | return ContactEntryAdapter.getEntry(mSections, info.position, SHOW_SEPARATORS); |
| 730 | } |
| 731 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 732 | @Override |
| 733 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 734 | switch (keyCode) { |
| 735 | case KeyEvent.KEYCODE_CALL: { |
| 736 | try { |
| 737 | ITelephony phone = ITelephony.Stub.asInterface( |
| 738 | ServiceManager.checkService("phone")); |
| 739 | if (phone != null && !phone.isIdle()) { |
| 740 | // Skip out and let the key be handled at a higher level |
| 741 | break; |
| 742 | } |
| 743 | } catch (RemoteException re) { |
| 744 | // Fall through and try to call the contact |
| 745 | } |
| 746 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 747 | int index = mListView.getSelectedItemPosition(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 748 | if (index != -1) { |
| 749 | ViewEntry entry = ViewAdapter.getEntry(mSections, index, SHOW_SEPARATORS); |
Bai Tao | 67205ed | 2010-03-16 08:09:11 +0800 | [diff] [blame] | 750 | if (entry != null && |
| 751 | entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) { |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 752 | startActivity(entry.intent); |
Bai Tao | 67205ed | 2010-03-16 08:09:11 +0800 | [diff] [blame] | 753 | return true; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 754 | } |
Jeff Sharkey | 827762d | 2009-12-23 11:06:08 -0800 | [diff] [blame] | 755 | } else if (mPrimaryPhoneUri != null) { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 756 | // There isn't anything selected, call the default number |
Jeff Sharkey | 827762d | 2009-12-23 11:06:08 -0800 | [diff] [blame] | 757 | final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, |
| 758 | mPrimaryPhoneUri); |
| 759 | startActivity(intent); |
Bai Tao | 67205ed | 2010-03-16 08:09:11 +0800 | [diff] [blame] | 760 | return true; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 761 | } |
Bai Tao | 67205ed | 2010-03-16 08:09:11 +0800 | [diff] [blame] | 762 | return false; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | case KeyEvent.KEYCODE_DEL: { |
Evan Millar | cdeea94 | 2009-10-07 11:47:35 -0700 | [diff] [blame] | 766 | if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) { |
Megha Joshi | d5afd8a | 2009-09-22 09:38:41 -0700 | [diff] [blame] | 767 | showDialog(DIALOG_CONFIRM_READONLY_DELETE); |
Evan Millar | cdeea94 | 2009-10-07 11:47:35 -0700 | [diff] [blame] | 768 | } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) { |
Megha Joshi | c508bd8 | 2009-10-01 17:31:49 -0700 | [diff] [blame] | 769 | showDialog(DIALOG_CONFIRM_READONLY_HIDE); |
Evan Millar | cdeea94 | 2009-10-07 11:47:35 -0700 | [diff] [blame] | 770 | } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) { |
| 771 | showDialog(DIALOG_CONFIRM_MULTIPLE_DELETE); |
| 772 | } else { |
| 773 | showDialog(DIALOG_CONFIRM_DELETE); |
| 774 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 775 | return true; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | return super.onKeyDown(keyCode, event); |
| 780 | } |
| 781 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 782 | 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] | 783 | ViewEntry entry = ViewAdapter.getEntry(mSections, position, SHOW_SEPARATORS); |
| 784 | if (entry != null) { |
| 785 | Intent intent = entry.intent; |
Evan Millar | df18120 | 2009-09-30 15:15:51 -0700 | [diff] [blame] | 786 | if (intent != null) { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 787 | try { |
| 788 | startActivity(intent); |
| 789 | } catch (ActivityNotFoundException e) { |
| 790 | Log.e(TAG, "No activity found for intent: " + intent); |
| 791 | signalError(); |
| 792 | } |
| 793 | } else { |
| 794 | signalError(); |
| 795 | } |
| 796 | } else { |
| 797 | signalError(); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | /** |
| 802 | * Signal an error to the user via a beep, or some other method. |
| 803 | */ |
| 804 | private void signalError() { |
| 805 | //TODO: implement this when we have the sonification APIs |
| 806 | } |
| 807 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 808 | /** |
| 809 | * Build up the entries to display on the screen. |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 810 | * |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 811 | * @param personCursor the URI for the contact being displayed |
| 812 | */ |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 813 | private final void buildEntries() { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 814 | // Clear out the old entries |
| 815 | final int numSections = mSections.size(); |
| 816 | for (int i = 0; i < numSections; i++) { |
| 817 | mSections.get(i).clear(); |
| 818 | } |
| 819 | |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 820 | mRawContactIds.clear(); |
Jeff Sharkey | e040876 | 2009-11-03 12:57:16 -0800 | [diff] [blame] | 821 | |
Megha Joshi | d5afd8a | 2009-09-22 09:38:41 -0700 | [diff] [blame] | 822 | mReadOnlySourcesCnt = 0; |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 823 | mWritableSourcesCnt = 0; |
Jeff Sharkey | e040876 | 2009-11-03 12:57:16 -0800 | [diff] [blame] | 824 | mAllRestricted = true; |
Jeff Sharkey | 827762d | 2009-12-23 11:06:08 -0800 | [diff] [blame] | 825 | mPrimaryPhoneUri = null; |
Jeff Sharkey | e040876 | 2009-11-03 12:57:16 -0800 | [diff] [blame] | 826 | |
Megha Joshi | d5afd8a | 2009-09-22 09:38:41 -0700 | [diff] [blame] | 827 | mWritableRawContactIds.clear(); |
Dmitri Plotnikov | b4491ee | 2009-06-15 09:31:02 -0700 | [diff] [blame] | 828 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 829 | final Context context = this; |
| 830 | final Sources sources = Sources.getInstance(context); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 831 | |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 832 | // Build up method entries |
Evan Millar | 6a61a1a | 2009-09-29 14:00:43 -0700 | [diff] [blame] | 833 | if (mLookupUri != null) { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 834 | for (Entity entity: mEntities) { |
| 835 | final ContentValues entValues = entity.getEntityValues(); |
| 836 | final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE); |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 837 | final long rawContactId = entValues.getAsLong(RawContacts._ID); |
Evan Millar | 7911ff5 | 2009-07-21 15:55:18 -0700 | [diff] [blame] | 838 | |
Jeff Sharkey | e040876 | 2009-11-03 12:57:16 -0800 | [diff] [blame] | 839 | // Mark when this contact has any unrestricted components |
| 840 | final boolean isRestricted = entValues.getAsInteger(RawContacts.IS_RESTRICTED) != 0; |
| 841 | if (!isRestricted) mAllRestricted = false; |
| 842 | |
Dmitri Plotnikov | 2a8cef0 | 2009-09-23 19:01:15 -0700 | [diff] [blame] | 843 | if (!mRawContactIds.contains(rawContactId)) { |
| 844 | mRawContactIds.add(rawContactId); |
| 845 | } |
Megha Joshi | c508bd8 | 2009-10-01 17:31:49 -0700 | [diff] [blame] | 846 | ContactsSource contactsSource = sources.getInflatedSource(accountType, |
| 847 | ContactsSource.LEVEL_SUMMARY); |
| 848 | if (contactsSource != null && contactsSource.readOnly) { |
| 849 | mReadOnlySourcesCnt += 1; |
| 850 | } else { |
| 851 | mWritableSourcesCnt += 1; |
Evan Millar | cdeea94 | 2009-10-07 11:47:35 -0700 | [diff] [blame] | 852 | mWritableRawContactIds.add(rawContactId); |
| 853 | } |
Megha Joshi | c508bd8 | 2009-10-01 17:31:49 -0700 | [diff] [blame] | 854 | |
Dmitri Plotnikov | 2a8cef0 | 2009-09-23 19:01:15 -0700 | [diff] [blame] | 855 | |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 856 | for (NamedContentValues subValue : entity.getSubValues()) { |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 857 | final ContentValues entryValues = subValue.values; |
| 858 | entryValues.put(Data.RAW_CONTACT_ID, rawContactId); |
| 859 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 860 | final long dataId = entryValues.getAsLong(Data._ID); |
| 861 | final String mimeType = entryValues.getAsString(Data.MIMETYPE); |
| 862 | if (mimeType == null) continue; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 863 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 864 | final DataKind kind = sources.getKindOrFallback(accountType, mimeType, this, |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 865 | ContactsSource.LEVEL_MIMETYPES); |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 866 | if (kind == null) continue; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 867 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 868 | final ViewEntry entry = ViewEntry.fromValues(context, mimeType, kind, |
| 869 | rawContactId, dataId, entryValues); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 870 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 871 | final boolean hasData = !TextUtils.isEmpty(entry.data); |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 872 | final boolean isSuperPrimary = entryValues.getAsInteger( |
| 873 | Data.IS_SUPER_PRIMARY) != 0; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 874 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 875 | if (Phone.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) { |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 876 | // Build phone entries |
| 877 | mNumPhoneNumbers++; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 878 | |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 879 | entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 880 | Uri.fromParts(Constants.SCHEME_TEL, entry.data, null)); |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 881 | entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO, |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 882 | Uri.fromParts(Constants.SCHEME_SMSTO, entry.data, null)); |
Evan Millar | 49714ee | 2009-09-02 16:42:47 -0700 | [diff] [blame] | 883 | |
Jeff Sharkey | 827762d | 2009-12-23 11:06:08 -0800 | [diff] [blame] | 884 | // Remember super-primary phone |
| 885 | if (isSuperPrimary) mPrimaryPhoneUri = entry.uri; |
| 886 | |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 887 | entry.isPrimary = isSuperPrimary; |
| 888 | mPhoneEntries.add(entry); |
| 889 | |
| 890 | if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE |
| 891 | || mShowSmsLinksForAllPhones) { |
| 892 | // Add an SMS entry |
| 893 | if (kind.iconAltRes > 0) { |
| 894 | entry.secondaryActionIcon = kind.iconAltRes; |
Evan Millar | 49714ee | 2009-09-02 16:42:47 -0700 | [diff] [blame] | 895 | } |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 896 | } |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 897 | } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) { |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 898 | // Build email entries |
| 899 | entry.intent = new Intent(Intent.ACTION_SENDTO, |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 900 | Uri.fromParts(Constants.SCHEME_MAILTO, entry.data, null)); |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 901 | entry.isPrimary = isSuperPrimary; |
| 902 | mEmailEntries.add(entry); |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 903 | |
| 904 | // When Email rows have status, create additional Im row |
| 905 | final DataStatus status = mStatuses.get(entry.id); |
| 906 | if (status != null) { |
| 907 | final String imMime = Im.CONTENT_ITEM_TYPE; |
| 908 | final DataKind imKind = sources.getKindOrFallback(accountType, |
| 909 | imMime, this, ContactsSource.LEVEL_MIMETYPES); |
| 910 | final ViewEntry imEntry = ViewEntry.fromValues(context, |
| 911 | imMime, imKind, rawContactId, dataId, entryValues); |
| 912 | imEntry.intent = ContactsUtils.buildImIntent(entryValues); |
| 913 | imEntry.applyStatus(status, false); |
| 914 | mImEntries.add(imEntry); |
| 915 | } |
| 916 | } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) { |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 917 | // Build postal entries |
| 918 | entry.maxLines = 4; |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 919 | entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri); |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 920 | mPostalEntries.add(entry); |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 921 | } else if (Im.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) { |
| 922 | // Build IM entries |
| 923 | entry.intent = ContactsUtils.buildImIntent(entryValues); |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 924 | if (TextUtils.isEmpty(entry.label)) { |
| 925 | entry.label = getString(R.string.chat).toLowerCase(); |
| 926 | } |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 927 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 928 | // Apply presence and status details when available |
| 929 | final DataStatus status = mStatuses.get(entry.id); |
| 930 | if (status != null) { |
| 931 | entry.applyStatus(status, false); |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 932 | } |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 933 | mImEntries.add(entry); |
Daniel Lehmann | e12f8a9 | 2010-02-24 18:37:56 -0800 | [diff] [blame] | 934 | } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType) && |
| 935 | (hasData || !TextUtils.isEmpty(entry.label))) { |
| 936 | // Build organization entries |
Daniel Lehmann | 350ba6f | 2010-03-23 18:10:57 -0700 | [diff] [blame] | 937 | final boolean isNameRawContact = (mNameRawContactId == rawContactId); |
| 938 | |
| 939 | final boolean duplicatesTitle = |
| 940 | isNameRawContact |
| 941 | && mDisplayNameSource == DisplayNameSources.ORGANIZATION |
Daniel Lehmann | 15a194a | 2010-03-24 13:45:05 -0700 | [diff] [blame] | 942 | && (!hasData || TextUtils.isEmpty(entry.label)); |
Daniel Lehmann | 350ba6f | 2010-03-23 18:10:57 -0700 | [diff] [blame] | 943 | |
| 944 | if (!duplicatesTitle) { |
| 945 | entry.uri = null; |
Daniel Lehmann | 15a194a | 2010-03-24 13:45:05 -0700 | [diff] [blame] | 946 | |
| 947 | if (TextUtils.isEmpty(entry.label)) { |
| 948 | entry.label = entry.data; |
| 949 | entry.data = ""; |
| 950 | } |
| 951 | |
Daniel Lehmann | 350ba6f | 2010-03-23 18:10:57 -0700 | [diff] [blame] | 952 | mOrganizationEntries.add(entry); |
| 953 | } |
Daniel Lehmann | e12f8a9 | 2010-02-24 18:37:56 -0800 | [diff] [blame] | 954 | } else if (Nickname.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) { |
| 955 | // Build nickname entries |
Daniel Lehmann | 350ba6f | 2010-03-23 18:10:57 -0700 | [diff] [blame] | 956 | final boolean isNameRawContact = (mNameRawContactId == rawContactId); |
| 957 | |
| 958 | final boolean duplicatesTitle = |
| 959 | isNameRawContact |
| 960 | && mDisplayNameSource == DisplayNameSources.NICKNAME; |
| 961 | |
| 962 | if (!duplicatesTitle) { |
| 963 | entry.uri = null; |
| 964 | mNicknameEntries.add(entry); |
| 965 | } |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 966 | } else if (Note.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 967 | // Build note entries |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 968 | entry.uri = null; |
Daniel Lehmann | 0e32201 | 2010-02-25 16:09:46 -0800 | [diff] [blame] | 969 | entry.maxLines = 100; |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 970 | mOtherEntries.add(entry); |
Evan Millar | 951fa9c | 2009-10-26 14:57:43 -0700 | [diff] [blame] | 971 | } else if (Website.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) { |
| 972 | // Build note entries |
| 973 | entry.uri = null; |
| 974 | entry.maxLines = 10; |
| 975 | try { |
| 976 | WebAddress webAddress = new WebAddress(entry.data); |
Makoto Onuki | 7a70738 | 2009-11-19 13:35:55 +0900 | [diff] [blame] | 977 | entry.intent = new Intent(Intent.ACTION_VIEW, |
Evan Millar | 951fa9c | 2009-10-26 14:57:43 -0700 | [diff] [blame] | 978 | Uri.parse(webAddress.toString())); |
| 979 | } catch (ParseException e) { |
| 980 | Log.e(TAG, "Couldn't parse website: " + entry.data); |
| 981 | } |
| 982 | mOtherEntries.add(entry); |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 983 | } else { |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 984 | // Handle showing custom rows |
Evan Millar | ff04a27 | 2009-09-26 15:23:37 -0700 | [diff] [blame] | 985 | entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri); |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 986 | |
| 987 | // Use social summary when requested by external source |
| 988 | final DataStatus status = mStatuses.get(entry.id); |
| 989 | final boolean hasSocial = kind.actionBodySocial && status != null; |
| 990 | if (hasSocial) { |
| 991 | entry.applyStatus(status, true); |
| 992 | } |
| 993 | |
| 994 | if (hasSocial || hasData) { |
| 995 | mOtherEntries.add(entry); |
| 996 | } |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 997 | } |
Evan Millar | 2e1b8af | 2009-09-24 09:39:21 -0700 | [diff] [blame] | 998 | } |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 999 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1000 | } |
| 1001 | } |
| 1002 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 1003 | static String buildActionString(DataKind kind, ContentValues values, boolean lowerCase, |
| 1004 | Context context) { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1005 | if (kind.actionHeader == null) { |
| 1006 | return null; |
Jeff Hamilton | 8350e5b | 2009-03-24 21:01:34 -0700 | [diff] [blame] | 1007 | } |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 1008 | CharSequence actionHeader = kind.actionHeader.inflateUsing(context, values); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1009 | if (actionHeader == null) { |
| 1010 | return null; |
| 1011 | } |
| 1012 | return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString(); |
| 1013 | } |
Jeff Hamilton | 8350e5b | 2009-03-24 21:01:34 -0700 | [diff] [blame] | 1014 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 1015 | static String buildDataString(DataKind kind, ContentValues values, Context context) { |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1016 | if (kind.actionBody == null) { |
| 1017 | return null; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1018 | } |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 1019 | CharSequence actionBody = kind.actionBody.inflateUsing(context, values); |
Evan Millar | 11d628c | 2009-09-02 08:55:01 -0700 | [diff] [blame] | 1020 | return actionBody == null ? null : actionBody.toString(); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1021 | } |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 1022 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1023 | /** |
| 1024 | * A basic structure with the data for a contact entry in the list. |
| 1025 | */ |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 1026 | static class ViewEntry extends ContactEntryAdapter.Entry implements Collapsible<ViewEntry> { |
| 1027 | public Context context = null; |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 1028 | public String resPackageName = null; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1029 | public int actionIcon = -1; |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1030 | public boolean isPrimary = false; |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1031 | public int secondaryActionIcon = -1; |
| 1032 | public Intent intent; |
| 1033 | public Intent secondaryIntent = null; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1034 | public int maxLabelLines = 1; |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1035 | public ArrayList<Long> ids = new ArrayList<Long>(); |
| 1036 | public int collapseCount = 0; |
| 1037 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 1038 | public int presence = -1; |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 1039 | |
| 1040 | public CharSequence footerLine = null; |
| 1041 | |
| 1042 | private ViewEntry() { |
| 1043 | } |
| 1044 | |
| 1045 | /** |
| 1046 | * Build new {@link ViewEntry} and populate from the given values. |
| 1047 | */ |
| 1048 | public static ViewEntry fromValues(Context context, String mimeType, DataKind kind, |
| 1049 | long rawContactId, long dataId, ContentValues values) { |
| 1050 | final ViewEntry entry = new ViewEntry(); |
| 1051 | entry.context = context; |
| 1052 | entry.contactId = rawContactId; |
| 1053 | entry.id = dataId; |
| 1054 | entry.uri = ContentUris.withAppendedId(Data.CONTENT_URI, entry.id); |
| 1055 | entry.mimetype = mimeType; |
| 1056 | entry.label = buildActionString(kind, values, false, context); |
| 1057 | entry.data = buildDataString(kind, values, context); |
| 1058 | |
| 1059 | if (kind.typeColumn != null && values.containsKey(kind.typeColumn)) { |
| 1060 | entry.type = values.getAsInteger(kind.typeColumn); |
| 1061 | } |
| 1062 | if (kind.iconRes > 0) { |
| 1063 | entry.resPackageName = kind.resPackageName; |
| 1064 | entry.actionIcon = kind.iconRes; |
| 1065 | } |
| 1066 | |
| 1067 | return entry; |
| 1068 | } |
| 1069 | |
| 1070 | /** |
| 1071 | * Apply given {@link DataStatus} values over this {@link ViewEntry} |
| 1072 | * |
| 1073 | * @param fillData When true, the given status replaces {@link #data} |
| 1074 | * and {@link #footerLine}. Otherwise only {@link #presence} |
| 1075 | * is updated. |
| 1076 | */ |
| 1077 | public ViewEntry applyStatus(DataStatus status, boolean fillData) { |
| 1078 | presence = status.getPresence(); |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 1079 | if (fillData && status.isValid()) { |
| 1080 | this.data = status.getStatus().toString(); |
| 1081 | this.footerLine = status.getTimestampLabel(context); |
| 1082 | } |
| 1083 | |
| 1084 | return this; |
| 1085 | } |
| 1086 | |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1087 | public boolean collapseWith(ViewEntry entry) { |
| 1088 | // assert equal collapse keys |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 1089 | if (!shouldCollapseWith(entry)) { |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1090 | return false; |
| 1091 | } |
| 1092 | |
| 1093 | // Choose the label associated with the highest type precedence. |
| 1094 | if (TypePrecedence.getTypePrecedence(mimetype, type) |
| 1095 | > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) { |
| 1096 | type = entry.type; |
| 1097 | label = entry.label; |
| 1098 | } |
| 1099 | |
| 1100 | // Choose the max of the maxLines and maxLabelLines values. |
| 1101 | maxLines = Math.max(maxLines, entry.maxLines); |
| 1102 | maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines); |
| 1103 | |
| 1104 | // Choose the presence with the highest precedence. |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 1105 | if (StatusUpdates.getPresencePrecedence(presence) |
| 1106 | < StatusUpdates.getPresencePrecedence(entry.presence)) { |
| 1107 | presence = entry.presence; |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | // 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] | 1111 | isPrimary = entry.isPrimary ? true : isPrimary; |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1112 | |
| 1113 | // uri, and contactdId, shouldn't make a difference. Just keep the original. |
| 1114 | |
| 1115 | // Keep track of all the ids that have been collapsed with this one. |
| 1116 | ids.add(entry.id); |
| 1117 | collapseCount++; |
| 1118 | return true; |
| 1119 | } |
| 1120 | |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 1121 | public boolean shouldCollapseWith(ViewEntry entry) { |
| 1122 | if (entry == null) { |
| 1123 | return false; |
| 1124 | } |
| 1125 | |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 1126 | if (!ContactsUtils.areDataEqual(context, mimetype, data, entry.mimetype, entry.data)) { |
| 1127 | return false; |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Makoto Onuki | c710b0e | 2009-10-13 15:43:24 -0700 | [diff] [blame] | 1130 | if (!TextUtils.equals(mimetype, entry.mimetype) |
| 1131 | || !ContactsUtils.areIntentActionEqual(intent, entry.intent) |
| 1132 | || !ContactsUtils.areIntentActionEqual(secondaryIntent, entry.secondaryIntent) |
Evan Millar | adb0f8c | 2009-09-28 17:20:50 -0700 | [diff] [blame] | 1133 | || actionIcon != entry.actionIcon) { |
| 1134 | return false; |
| 1135 | } |
| 1136 | |
| 1137 | return true; |
| 1138 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1139 | } |
| 1140 | |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1141 | /** Cache of the children views of a row */ |
| 1142 | static class ViewCache { |
| 1143 | public TextView label; |
| 1144 | public TextView data; |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 1145 | public TextView footer; |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1146 | public ImageView actionIcon; |
| 1147 | public ImageView presenceIcon; |
| 1148 | public ImageView primaryIcon; |
| 1149 | public ImageView secondaryActionButton; |
| 1150 | public View secondaryActionDivider; |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 1151 | |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1152 | // Need to keep track of this too |
| 1153 | ViewEntry entry; |
| 1154 | } |
| 1155 | |
| 1156 | private final class ViewAdapter extends ContactEntryAdapter<ViewEntry> |
| 1157 | implements View.OnClickListener { |
| 1158 | |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 1159 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1160 | ViewAdapter(Context context, ArrayList<ArrayList<ViewEntry>> sections) { |
| 1161 | super(context, sections, SHOW_SEPARATORS); |
| 1162 | } |
| 1163 | |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1164 | public void onClick(View v) { |
| 1165 | Intent intent = (Intent) v.getTag(); |
| 1166 | startActivity(intent); |
| 1167 | } |
| 1168 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1169 | @Override |
| 1170 | public View getView(int position, View convertView, ViewGroup parent) { |
Evan Millar | 5c22c3b | 2009-05-29 11:37:54 -0700 | [diff] [blame] | 1171 | ViewEntry entry = getEntry(mSections, position, false); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1172 | View v; |
| 1173 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1174 | ViewCache views; |
| 1175 | |
| 1176 | // Check to see if we can reuse convertView |
| 1177 | if (convertView != null) { |
| 1178 | v = convertView; |
| 1179 | views = (ViewCache) v.getTag(); |
| 1180 | } else { |
| 1181 | // Create a new view if needed |
| 1182 | v = mInflater.inflate(R.layout.list_item_text_icons, parent, false); |
| 1183 | |
| 1184 | // Cache the children |
| 1185 | views = new ViewCache(); |
| 1186 | views.label = (TextView) v.findViewById(android.R.id.text1); |
| 1187 | views.data = (TextView) v.findViewById(android.R.id.text2); |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 1188 | views.footer = (TextView) v.findViewById(R.id.footer); |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1189 | views.actionIcon = (ImageView) v.findViewById(R.id.action_icon); |
| 1190 | views.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon); |
| 1191 | views.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon); |
| 1192 | views.secondaryActionButton = (ImageView) v.findViewById( |
| 1193 | R.id.secondary_action_button); |
| 1194 | views.secondaryActionButton.setOnClickListener(this); |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1195 | views.secondaryActionDivider = v.findViewById(R.id.divider); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1196 | v.setTag(views); |
| 1197 | } |
| 1198 | |
| 1199 | // Update the entry in the view cache |
| 1200 | views.entry = entry; |
| 1201 | |
| 1202 | // Bind the data to the view |
| 1203 | bindView(v, entry); |
| 1204 | return v; |
| 1205 | } |
| 1206 | |
| 1207 | @Override |
| 1208 | protected View newView(int position, ViewGroup parent) { |
| 1209 | // getView() handles this |
| 1210 | throw new UnsupportedOperationException(); |
| 1211 | } |
| 1212 | |
| 1213 | @Override |
| 1214 | protected void bindView(View view, ViewEntry entry) { |
| 1215 | final Resources resources = mContext.getResources(); |
| 1216 | ViewCache views = (ViewCache) view.getTag(); |
| 1217 | |
| 1218 | // Set the label |
| 1219 | TextView label = views.label; |
| 1220 | setMaxLines(label, entry.maxLabelLines); |
| 1221 | label.setText(entry.label); |
| 1222 | |
| 1223 | // Set the data |
| 1224 | TextView data = views.data; |
| 1225 | if (data != null) { |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1226 | if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE) |
Jeff Sharkey | 49d17b3 | 2009-09-07 02:14:21 -0700 | [diff] [blame] | 1227 | || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) { |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame] | 1228 | data.setText(PhoneNumberUtils.formatNumber(entry.data)); |
| 1229 | } else { |
| 1230 | data.setText(entry.data); |
| 1231 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1232 | setMaxLines(data, entry.maxLines); |
| 1233 | } |
| 1234 | |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 1235 | // Set the footer |
| 1236 | if (!TextUtils.isEmpty(entry.footerLine)) { |
| 1237 | views.footer.setText(entry.footerLine); |
| 1238 | views.footer.setVisibility(View.VISIBLE); |
| 1239 | } else { |
| 1240 | views.footer.setVisibility(View.GONE); |
| 1241 | } |
| 1242 | |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1243 | // Set the primary icon |
| 1244 | views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE); |
| 1245 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1246 | // Set the action icon |
| 1247 | ImageView action = views.actionIcon; |
| 1248 | if (entry.actionIcon != -1) { |
Jeff Sharkey | ab06693 | 2009-09-21 09:55:30 -0700 | [diff] [blame] | 1249 | Drawable actionIcon; |
| 1250 | if (entry.resPackageName != null) { |
| 1251 | // Load external resources through PackageManager |
| 1252 | actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName, |
| 1253 | entry.actionIcon, null); |
| 1254 | } else { |
| 1255 | actionIcon = resources.getDrawable(entry.actionIcon); |
| 1256 | } |
| 1257 | action.setImageDrawable(actionIcon); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1258 | action.setVisibility(View.VISIBLE); |
| 1259 | } else { |
| 1260 | // Things should still line up as if there was an icon, so make it invisible |
| 1261 | action.setVisibility(View.INVISIBLE); |
| 1262 | } |
| 1263 | |
| 1264 | // Set the presence icon |
Bai Tao | 107736c | 2010-03-12 08:00:42 +0800 | [diff] [blame] | 1265 | Drawable presenceIcon = ContactPresenceIconUtil.getPresenceIcon( |
| 1266 | mContext, entry.presence); |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1267 | ImageView presenceIconView = views.presenceIcon; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1268 | if (presenceIcon != null) { |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1269 | presenceIconView.setImageDrawable(presenceIcon); |
| 1270 | presenceIconView.setVisibility(View.VISIBLE); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1271 | } else { |
Evan Millar | 15e514d | 2009-08-04 10:14:57 -0700 | [diff] [blame] | 1272 | presenceIconView.setVisibility(View.GONE); |
| 1273 | } |
| 1274 | |
| 1275 | // Set the secondary action button |
| 1276 | ImageView secondaryActionView = views.secondaryActionButton; |
| 1277 | Drawable secondaryActionIcon = null; |
| 1278 | if (entry.secondaryActionIcon != -1) { |
| 1279 | secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon); |
| 1280 | } |
| 1281 | if (entry.secondaryIntent != null && secondaryActionIcon != null) { |
| 1282 | secondaryActionView.setImageDrawable(secondaryActionIcon); |
| 1283 | secondaryActionView.setTag(entry.secondaryIntent); |
| 1284 | secondaryActionView.setVisibility(View.VISIBLE); |
| 1285 | views.secondaryActionDivider.setVisibility(View.VISIBLE); |
| 1286 | } else { |
| 1287 | secondaryActionView.setVisibility(View.GONE); |
| 1288 | views.secondaryActionDivider.setVisibility(View.GONE); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | private void setMaxLines(TextView textView, int maxLines) { |
| 1293 | if (maxLines == 1) { |
| 1294 | textView.setSingleLine(true); |
| 1295 | textView.setEllipsize(TextUtils.TruncateAt.END); |
| 1296 | } else { |
| 1297 | textView.setSingleLine(false); |
| 1298 | textView.setMaxLines(maxLines); |
| 1299 | textView.setEllipsize(null); |
| 1300 | } |
| 1301 | } |
| 1302 | } |
Jeff Sharkey | 624ddc3 | 2009-10-01 21:32:19 -0700 | [diff] [blame] | 1303 | |
| 1304 | private interface StatusQuery { |
| 1305 | final String[] PROJECTION = new String[] { |
| 1306 | Data._ID, |
| 1307 | Data.STATUS, |
| 1308 | Data.STATUS_RES_PACKAGE, |
| 1309 | Data.STATUS_ICON, |
| 1310 | Data.STATUS_LABEL, |
| 1311 | Data.STATUS_TIMESTAMP, |
| 1312 | Data.PRESENCE, |
| 1313 | }; |
| 1314 | |
| 1315 | final int _ID = 0; |
| 1316 | } |
Dmitri Plotnikov | 8e86b75 | 2010-02-22 17:47:57 -0800 | [diff] [blame] | 1317 | |
| 1318 | @Override |
| 1319 | public void startSearch(String initialQuery, boolean selectInitialQuery, Bundle appSearchData, |
| 1320 | boolean globalSearch) { |
| 1321 | if (globalSearch) { |
| 1322 | super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch); |
| 1323 | } else { |
| 1324 | ContactsSearchManager.startSearch(this, initialQuery); |
| 1325 | } |
| 1326 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1327 | } |