Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 Google Inc. |
| 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.views.detail; |
| 18 | |
| 19 | import com.android.contacts.Collapser; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 20 | import com.android.contacts.ContactOptionsActivity; |
| 21 | import com.android.contacts.ContactPresenceIconUtil; |
| 22 | import com.android.contacts.ContactsUtils; |
| 23 | import com.android.contacts.R; |
| 24 | import com.android.contacts.TypePrecedence; |
| 25 | import com.android.contacts.Collapser.Collapsible; |
| 26 | import com.android.contacts.model.ContactsSource; |
| 27 | import com.android.contacts.model.Sources; |
| 28 | import com.android.contacts.model.ContactsSource.DataKind; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 29 | import com.android.contacts.util.Constants; |
| 30 | import com.android.contacts.util.DataStatus; |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 31 | import com.android.contacts.views.ContactLoader; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 32 | import com.android.internal.telephony.ITelephony; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 33 | |
Daniel Lehmann | 25a0282 | 2010-05-11 15:52:43 -0700 | [diff] [blame] | 34 | import android.app.Activity; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 35 | import android.app.AlertDialog; |
| 36 | import android.app.Dialog; |
Jeff Hamilton | 3c46291 | 2010-05-15 02:20:01 -0500 | [diff] [blame] | 37 | import android.app.LoaderManagingFragment; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 38 | import android.content.ActivityNotFoundException; |
| 39 | import android.content.ContentUris; |
| 40 | import android.content.ContentValues; |
| 41 | import android.content.Context; |
| 42 | import android.content.DialogInterface; |
| 43 | import android.content.Entity; |
| 44 | import android.content.Intent; |
Jeff Hamilton | 3c46291 | 2010-05-15 02:20:01 -0500 | [diff] [blame] | 45 | import android.content.Loader; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 46 | import android.content.Entity.NamedContentValues; |
| 47 | import android.content.res.Resources; |
| 48 | import android.graphics.drawable.Drawable; |
| 49 | import android.net.ParseException; |
| 50 | import android.net.Uri; |
| 51 | import android.net.WebAddress; |
| 52 | import android.os.Bundle; |
| 53 | import android.os.RemoteException; |
| 54 | import android.os.ServiceManager; |
| 55 | import android.provider.ContactsContract.CommonDataKinds; |
| 56 | import android.provider.ContactsContract.Contacts; |
| 57 | import android.provider.ContactsContract.Data; |
| 58 | import android.provider.ContactsContract.DisplayNameSources; |
| 59 | import android.provider.ContactsContract.RawContacts; |
| 60 | import android.provider.ContactsContract.StatusUpdates; |
| 61 | import android.provider.ContactsContract.CommonDataKinds.Email; |
| 62 | import android.provider.ContactsContract.CommonDataKinds.Im; |
| 63 | import android.provider.ContactsContract.CommonDataKinds.Nickname; |
| 64 | import android.provider.ContactsContract.CommonDataKinds.Note; |
| 65 | import android.provider.ContactsContract.CommonDataKinds.Organization; |
| 66 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 67 | import android.provider.ContactsContract.CommonDataKinds.StructuredName; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 68 | import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; |
| 69 | import android.provider.ContactsContract.CommonDataKinds.Website; |
| 70 | import android.telephony.PhoneNumberUtils; |
| 71 | import android.text.TextUtils; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 72 | import android.util.Log; |
| 73 | import android.view.ContextMenu; |
| 74 | import android.view.KeyEvent; |
| 75 | import android.view.LayoutInflater; |
| 76 | import android.view.Menu; |
| 77 | import android.view.MenuInflater; |
| 78 | import android.view.MenuItem; |
| 79 | import android.view.View; |
| 80 | import android.view.ViewGroup; |
| 81 | import android.view.ContextMenu.ContextMenuInfo; |
Daniel Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 82 | import android.view.View.OnClickListener; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 83 | import android.view.View.OnCreateContextMenuListener; |
| 84 | import android.widget.AdapterView; |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 85 | import android.widget.BaseAdapter; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 86 | import android.widget.ImageView; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 87 | import android.widget.ListView; |
| 88 | import android.widget.TextView; |
| 89 | import android.widget.Toast; |
| 90 | import android.widget.AdapterView.OnItemClickListener; |
| 91 | |
| 92 | import java.util.ArrayList; |
| 93 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 94 | public class ContactDetailFragment extends LoaderManagingFragment<ContactLoader.Result> |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 95 | implements OnCreateContextMenuListener, OnItemClickListener { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 96 | private static final String TAG = "ContactDetailFragment"; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 97 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 98 | private static final int MENU_ITEM_MAKE_DEFAULT = 3; |
| 99 | |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 100 | private static final int LOADER_DETAILS = 1; |
| 101 | |
Daniel Lehmann | 25a0282 | 2010-05-11 15:52:43 -0700 | [diff] [blame] | 102 | private Context mContext; |
| 103 | private Uri mLookupUri; |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 104 | private Listener mListener; |
Daniel Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 105 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 106 | private ContactLoader.Result mContactData; |
| 107 | private ContactDetailHeaderView mHeaderView; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 108 | private ListView mListView; |
| 109 | private boolean mShowSmsLinksForAllPhones; |
| 110 | private ViewAdapter mAdapter; |
| 111 | private Uri mPrimaryPhoneUri = null; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 112 | |
| 113 | private int mReadOnlySourcesCnt; |
| 114 | private int mWritableSourcesCnt; |
| 115 | private boolean mAllRestricted; |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 116 | private final ArrayList<Long> mWritableRawContactIds = new ArrayList<Long>(); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 117 | private int mNumPhoneNumbers = 0; |
| 118 | |
| 119 | /** |
| 120 | * The view shown if the detail list is empty. |
| 121 | * We set this to the list view when first bind the adapter, so that it won't be shown while |
| 122 | * we're loading data. |
| 123 | */ |
| 124 | private View mEmptyView; |
| 125 | |
| 126 | /** |
| 127 | * A list of distinct contact IDs included in the current contact. |
| 128 | */ |
| 129 | private ArrayList<Long> mRawContactIds = new ArrayList<Long>(); |
| 130 | private ArrayList<ViewEntry> mPhoneEntries = new ArrayList<ViewEntry>(); |
| 131 | private ArrayList<ViewEntry> mSmsEntries = new ArrayList<ViewEntry>(); |
| 132 | private ArrayList<ViewEntry> mEmailEntries = new ArrayList<ViewEntry>(); |
| 133 | private ArrayList<ViewEntry> mPostalEntries = new ArrayList<ViewEntry>(); |
| 134 | private ArrayList<ViewEntry> mImEntries = new ArrayList<ViewEntry>(); |
| 135 | private ArrayList<ViewEntry> mNicknameEntries = new ArrayList<ViewEntry>(); |
| 136 | private ArrayList<ViewEntry> mOrganizationEntries = new ArrayList<ViewEntry>(); |
| 137 | private ArrayList<ViewEntry> mGroupEntries = new ArrayList<ViewEntry>(); |
| 138 | private ArrayList<ViewEntry> mOtherEntries = new ArrayList<ViewEntry>(); |
| 139 | private ArrayList<ArrayList<ViewEntry>> mSections = new ArrayList<ArrayList<ViewEntry>>(); |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 140 | private LayoutInflater mInflater; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 141 | |
Daniel Lehmann | 25a0282 | 2010-05-11 15:52:43 -0700 | [diff] [blame] | 142 | public ContactDetailFragment() { |
| 143 | // Explicit constructor for inflation |
Daniel Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 144 | |
| 145 | // Build the list of sections. The order they're added to mSections dictates the |
| 146 | // order they are displayed in the list. |
| 147 | mSections.add(mPhoneEntries); |
| 148 | mSections.add(mSmsEntries); |
| 149 | mSections.add(mEmailEntries); |
| 150 | mSections.add(mImEntries); |
| 151 | mSections.add(mPostalEntries); |
| 152 | mSections.add(mNicknameEntries); |
| 153 | mSections.add(mOrganizationEntries); |
| 154 | mSections.add(mGroupEntries); |
| 155 | mSections.add(mOtherEntries); |
Daniel Lehmann | 25a0282 | 2010-05-11 15:52:43 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void onAttach(Activity activity) { |
| 160 | super.onAttach(activity); |
| 161 | mContext = activity; |
| 162 | } |
| 163 | |
| 164 | @Override |
Dianne Hackborn | 84df2b6 | 2010-05-12 19:04:42 -0700 | [diff] [blame] | 165 | public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 166 | final View view = inflater.inflate(R.layout.contact_detail_fragment, container, false); |
Daniel Lehmann | 25a0282 | 2010-05-11 15:52:43 -0700 | [diff] [blame] | 167 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 168 | mInflater = inflater; |
| 169 | |
| 170 | mHeaderView = (ContactDetailHeaderView) view.findViewById(R.id.contact_header_widget); |
| 171 | mHeaderView.showStar(true); |
| 172 | mHeaderView.setExcludeMimes(new String[] { |
Daniel Lehmann | 25a0282 | 2010-05-11 15:52:43 -0700 | [diff] [blame] | 173 | Contacts.CONTENT_ITEM_TYPE |
| 174 | }); |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 175 | mHeaderView.setListener(mHeaderViewListener); |
Daniel Lehmann | 25a0282 | 2010-05-11 15:52:43 -0700 | [diff] [blame] | 176 | |
| 177 | mListView = (ListView) view.findViewById(android.R.id.list); |
| 178 | mListView.setOnCreateContextMenuListener(this); |
| 179 | mListView.setScrollBarStyle(ListView.SCROLLBARS_OUTSIDE_OVERLAY); |
| 180 | mListView.setOnItemClickListener(this); |
| 181 | // Don't set it to mListView yet. We do so later when we bind the adapter. |
| 182 | mEmptyView = view.findViewById(android.R.id.empty); |
| 183 | |
Daniel Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 184 | //TODO Read this value from a preference |
| 185 | mShowSmsLinksForAllPhones = true; |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 186 | |
Daniel Lehmann | 25a0282 | 2010-05-11 15:52:43 -0700 | [diff] [blame] | 187 | return view; |
| 188 | } |
| 189 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 190 | public void setListener(Listener value) { |
| 191 | mListener = value; |
Daniel Lehmann | 25a0282 | 2010-05-11 15:52:43 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | public void loadUri(Uri lookupUri) { |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 195 | mLookupUri = lookupUri; |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 196 | startLoading(LOADER_DETAILS, null); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 199 | @Override |
| 200 | protected void onInitializeLoaders() { |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 201 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 202 | |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 203 | @Override |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 204 | protected Loader<ContactLoader.Result> onCreateLoader(int id, Bundle args) { |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 205 | switch (id) { |
| 206 | case LOADER_DETAILS: { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 207 | return new ContactLoader(mContext, mLookupUri); |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 208 | } |
| 209 | default: { |
| 210 | Log.wtf(TAG, "Unknown ID in onCreateLoader: " + id); |
| 211 | } |
| 212 | } |
| 213 | return null; |
| 214 | } |
| 215 | |
| 216 | @Override |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 217 | protected void onLoadFinished(Loader<ContactLoader.Result> loader, |
| 218 | ContactLoader.Result data) { |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 219 | final int id = loader.getId(); |
| 220 | switch (id) { |
| 221 | case LOADER_DETAILS: |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 222 | if (data == ContactLoader.Result.NOT_FOUND) { |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 223 | // Item has been deleted |
| 224 | Log.i(TAG, "No contact found. Closing activity"); |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 225 | mListener.onContactNotFound(); |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 226 | return; |
| 227 | } |
| 228 | mContactData = data; |
| 229 | bindData(); |
| 230 | break; |
| 231 | default: { |
| 232 | Log.wtf(TAG, "Unknown ID in onLoadFinished: " + id); |
| 233 | } |
| 234 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | private void bindData() { |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 238 | // Set the header |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 239 | mHeaderView.setContactUri(mContactData.getLookupUri()); |
| 240 | mHeaderView.setDisplayName(mContactData.getDisplayName(), |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 241 | mContactData.getPhoneticName()); |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 242 | mHeaderView.setPhotoId(mContactData.getPhotoId(), mContactData.getLookupUri()); |
| 243 | mHeaderView.setStared(mContactData.getStarred()); |
| 244 | mHeaderView.setPresence(mContactData.getPresence()); |
| 245 | mHeaderView.setStatus( |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 246 | mContactData.getStatus(), mContactData.getStatusTimestamp(), |
| 247 | mContactData.getStatusLabel(), mContactData.getStatusResPackage()); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 248 | |
| 249 | // Build up the contact entries |
| 250 | buildEntries(); |
| 251 | |
| 252 | // Collapse similar data items in select sections. |
| 253 | Collapser.collapseList(mPhoneEntries); |
| 254 | Collapser.collapseList(mSmsEntries); |
| 255 | Collapser.collapseList(mEmailEntries); |
| 256 | Collapser.collapseList(mPostalEntries); |
| 257 | Collapser.collapseList(mImEntries); |
| 258 | |
| 259 | if (mAdapter == null) { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 260 | mAdapter = new ViewAdapter(); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 261 | mListView.setAdapter(mAdapter); |
| 262 | } else { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 263 | mAdapter.notifyDataSetChanged(); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 264 | } |
| 265 | mListView.setEmptyView(mEmptyView); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Build up the entries to display on the screen. |
| 270 | */ |
| 271 | private final void buildEntries() { |
| 272 | // Clear out the old entries |
| 273 | final int numSections = mSections.size(); |
| 274 | for (int i = 0; i < numSections; i++) { |
| 275 | mSections.get(i).clear(); |
| 276 | } |
| 277 | |
| 278 | mRawContactIds.clear(); |
| 279 | |
| 280 | mReadOnlySourcesCnt = 0; |
| 281 | mWritableSourcesCnt = 0; |
| 282 | mAllRestricted = true; |
| 283 | mPrimaryPhoneUri = null; |
| 284 | mNumPhoneNumbers = 0; |
| 285 | |
| 286 | mWritableRawContactIds.clear(); |
| 287 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 288 | // TODO: This should be done in the background thread |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 289 | final Sources sources = Sources.getInstance(mContext); |
| 290 | |
| 291 | // Build up method entries |
| 292 | if (mContactData == null) { |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | for (Entity entity: mContactData.getEntities()) { |
| 297 | final ContentValues entValues = entity.getEntityValues(); |
| 298 | final String accountType = entValues.getAsString(RawContacts.ACCOUNT_TYPE); |
| 299 | final long rawContactId = entValues.getAsLong(RawContacts._ID); |
| 300 | |
| 301 | // Mark when this contact has any unrestricted components |
| 302 | final boolean isRestricted = entValues.getAsInteger(RawContacts.IS_RESTRICTED) != 0; |
| 303 | if (!isRestricted) mAllRestricted = false; |
| 304 | |
| 305 | if (!mRawContactIds.contains(rawContactId)) { |
| 306 | mRawContactIds.add(rawContactId); |
| 307 | } |
| 308 | ContactsSource contactsSource = sources.getInflatedSource(accountType, |
| 309 | ContactsSource.LEVEL_SUMMARY); |
| 310 | if (contactsSource != null && contactsSource.readOnly) { |
| 311 | mReadOnlySourcesCnt += 1; |
| 312 | } else { |
| 313 | mWritableSourcesCnt += 1; |
| 314 | mWritableRawContactIds.add(rawContactId); |
| 315 | } |
| 316 | |
| 317 | |
| 318 | for (NamedContentValues subValue : entity.getSubValues()) { |
| 319 | final ContentValues entryValues = subValue.values; |
| 320 | entryValues.put(Data.RAW_CONTACT_ID, rawContactId); |
| 321 | |
| 322 | final long dataId = entryValues.getAsLong(Data._ID); |
| 323 | final String mimeType = entryValues.getAsString(Data.MIMETYPE); |
| 324 | if (mimeType == null) continue; |
| 325 | |
| 326 | final DataKind kind = sources.getKindOrFallback(accountType, mimeType, mContext, |
| 327 | ContactsSource.LEVEL_MIMETYPES); |
| 328 | if (kind == null) continue; |
| 329 | |
| 330 | final ViewEntry entry = ViewEntry.fromValues(mContext, mimeType, kind, |
| 331 | rawContactId, dataId, entryValues); |
| 332 | |
| 333 | final boolean hasData = !TextUtils.isEmpty(entry.data); |
| 334 | final boolean isSuperPrimary = entryValues.getAsInteger( |
| 335 | Data.IS_SUPER_PRIMARY) != 0; |
| 336 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 337 | if (StructuredName.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 338 | // Always ignore the name. It is shown in the header if set |
| 339 | } else if (Phone.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 340 | // Build phone entries |
| 341 | mNumPhoneNumbers++; |
| 342 | |
| 343 | entry.intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, |
| 344 | Uri.fromParts(Constants.SCHEME_TEL, entry.data, null)); |
| 345 | entry.secondaryIntent = new Intent(Intent.ACTION_SENDTO, |
| 346 | Uri.fromParts(Constants.SCHEME_SMSTO, entry.data, null)); |
| 347 | |
| 348 | // Remember super-primary phone |
| 349 | if (isSuperPrimary) mPrimaryPhoneUri = entry.uri; |
| 350 | |
| 351 | entry.isPrimary = isSuperPrimary; |
| 352 | mPhoneEntries.add(entry); |
| 353 | |
| 354 | if (entry.type == CommonDataKinds.Phone.TYPE_MOBILE |
| 355 | || mShowSmsLinksForAllPhones) { |
| 356 | // Add an SMS entry |
| 357 | if (kind.iconAltRes > 0) { |
| 358 | entry.secondaryActionIcon = kind.iconAltRes; |
| 359 | } |
| 360 | } |
| 361 | } else if (Email.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) { |
| 362 | // Build email entries |
| 363 | entry.intent = new Intent(Intent.ACTION_SENDTO, |
| 364 | Uri.fromParts(Constants.SCHEME_MAILTO, entry.data, null)); |
| 365 | entry.isPrimary = isSuperPrimary; |
| 366 | mEmailEntries.add(entry); |
| 367 | |
| 368 | // When Email rows have status, create additional Im row |
| 369 | final DataStatus status = mContactData.getStatuses().get(entry.id); |
| 370 | if (status != null) { |
| 371 | final String imMime = Im.CONTENT_ITEM_TYPE; |
| 372 | final DataKind imKind = sources.getKindOrFallback(accountType, |
| 373 | imMime, mContext, ContactsSource.LEVEL_MIMETYPES); |
| 374 | final ViewEntry imEntry = ViewEntry.fromValues(mContext, |
| 375 | imMime, imKind, rawContactId, dataId, entryValues); |
| 376 | imEntry.intent = ContactsUtils.buildImIntent(entryValues); |
| 377 | imEntry.applyStatus(status, false); |
| 378 | mImEntries.add(imEntry); |
| 379 | } |
| 380 | } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) { |
| 381 | // Build postal entries |
| 382 | entry.maxLines = 4; |
| 383 | entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri); |
| 384 | mPostalEntries.add(entry); |
| 385 | } else if (Im.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) { |
| 386 | // Build IM entries |
| 387 | entry.intent = ContactsUtils.buildImIntent(entryValues); |
| 388 | if (TextUtils.isEmpty(entry.label)) { |
| 389 | entry.label = mContext.getString(R.string.chat).toLowerCase(); |
| 390 | } |
| 391 | |
| 392 | // Apply presence and status details when available |
| 393 | final DataStatus status = mContactData.getStatuses().get(entry.id); |
| 394 | if (status != null) { |
| 395 | entry.applyStatus(status, false); |
| 396 | } |
| 397 | mImEntries.add(entry); |
| 398 | } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType) && |
| 399 | (hasData || !TextUtils.isEmpty(entry.label))) { |
| 400 | // Build organization entries |
| 401 | final boolean isNameRawContact = |
| 402 | (mContactData.getNameRawContactId() == rawContactId); |
| 403 | |
| 404 | final boolean duplicatesTitle = |
| 405 | isNameRawContact |
| 406 | && mContactData.getDisplayNameSource() |
| 407 | == DisplayNameSources.ORGANIZATION |
| 408 | && (!hasData || TextUtils.isEmpty(entry.label)); |
| 409 | |
| 410 | if (!duplicatesTitle) { |
| 411 | entry.uri = null; |
| 412 | |
| 413 | if (TextUtils.isEmpty(entry.label)) { |
| 414 | entry.label = entry.data; |
| 415 | entry.data = ""; |
| 416 | } |
| 417 | |
| 418 | mOrganizationEntries.add(entry); |
| 419 | } |
| 420 | } else if (Nickname.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) { |
| 421 | // Build nickname entries |
| 422 | final boolean isNameRawContact = |
| 423 | (mContactData.getNameRawContactId() == rawContactId); |
| 424 | |
| 425 | final boolean duplicatesTitle = |
| 426 | isNameRawContact |
| 427 | && mContactData.getDisplayNameSource() == DisplayNameSources.NICKNAME; |
| 428 | |
| 429 | if (!duplicatesTitle) { |
| 430 | entry.uri = null; |
| 431 | mNicknameEntries.add(entry); |
| 432 | } |
| 433 | } else if (Note.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) { |
| 434 | // Build note entries |
| 435 | entry.uri = null; |
| 436 | entry.maxLines = 100; |
| 437 | mOtherEntries.add(entry); |
| 438 | } else if (Website.CONTENT_ITEM_TYPE.equals(mimeType) && hasData) { |
| 439 | // Build note entries |
| 440 | entry.uri = null; |
| 441 | entry.maxLines = 10; |
| 442 | try { |
| 443 | WebAddress webAddress = new WebAddress(entry.data); |
| 444 | entry.intent = new Intent(Intent.ACTION_VIEW, |
| 445 | Uri.parse(webAddress.toString())); |
| 446 | } catch (ParseException e) { |
| 447 | Log.e(TAG, "Couldn't parse website: " + entry.data); |
| 448 | } |
| 449 | mOtherEntries.add(entry); |
| 450 | } else { |
| 451 | // Handle showing custom rows |
| 452 | entry.intent = new Intent(Intent.ACTION_VIEW, entry.uri); |
| 453 | |
| 454 | // Use social summary when requested by external source |
| 455 | final DataStatus status = mContactData.getStatuses().get(entry.id); |
| 456 | final boolean hasSocial = kind.actionBodySocial && status != null; |
| 457 | if (hasSocial) { |
| 458 | entry.applyStatus(status, true); |
| 459 | } |
| 460 | |
| 461 | if (hasSocial || hasData) { |
| 462 | mOtherEntries.add(entry); |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 469 | private static String buildActionString(DataKind kind, ContentValues values, |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 470 | boolean lowerCase, Context context) { |
| 471 | if (kind.actionHeader == null) { |
| 472 | return null; |
| 473 | } |
| 474 | CharSequence actionHeader = kind.actionHeader.inflateUsing(context, values); |
| 475 | if (actionHeader == null) { |
| 476 | return null; |
| 477 | } |
| 478 | return lowerCase ? actionHeader.toString().toLowerCase() : actionHeader.toString(); |
| 479 | } |
| 480 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 481 | private static String buildDataString(DataKind kind, ContentValues values, |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 482 | Context context) { |
| 483 | if (kind.actionBody == null) { |
| 484 | return null; |
| 485 | } |
| 486 | CharSequence actionBody = kind.actionBody.inflateUsing(context, values); |
| 487 | return actionBody == null ? null : actionBody.toString(); |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * A basic structure with the data for a contact entry in the list. |
| 492 | */ |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 493 | private static class ViewEntry implements Collapsible<ViewEntry> { |
| 494 | // Copied from baseclass |
| 495 | public int type = -1; |
| 496 | public String label; |
| 497 | public String data; |
| 498 | public Uri uri; |
| 499 | public long id = 0; |
| 500 | public int maxLines = 1; |
| 501 | public String mimetype; |
| 502 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 503 | public Context context = null; |
| 504 | public String resPackageName = null; |
| 505 | public int actionIcon = -1; |
| 506 | public boolean isPrimary = false; |
| 507 | public int secondaryActionIcon = -1; |
| 508 | public Intent intent; |
| 509 | public Intent secondaryIntent = null; |
| 510 | public int maxLabelLines = 1; |
| 511 | public ArrayList<Long> ids = new ArrayList<Long>(); |
| 512 | public int collapseCount = 0; |
| 513 | |
| 514 | public int presence = -1; |
| 515 | |
| 516 | public CharSequence footerLine = null; |
| 517 | |
| 518 | private ViewEntry() { |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Build new {@link ViewEntry} and populate from the given values. |
| 523 | */ |
| 524 | public static ViewEntry fromValues(Context context, String mimeType, DataKind kind, |
| 525 | long rawContactId, long dataId, ContentValues values) { |
| 526 | final ViewEntry entry = new ViewEntry(); |
| 527 | entry.context = context; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 528 | entry.id = dataId; |
| 529 | entry.uri = ContentUris.withAppendedId(Data.CONTENT_URI, entry.id); |
| 530 | entry.mimetype = mimeType; |
| 531 | entry.label = buildActionString(kind, values, false, context); |
| 532 | entry.data = buildDataString(kind, values, context); |
| 533 | |
| 534 | if (kind.typeColumn != null && values.containsKey(kind.typeColumn)) { |
| 535 | entry.type = values.getAsInteger(kind.typeColumn); |
| 536 | } |
| 537 | if (kind.iconRes > 0) { |
| 538 | entry.resPackageName = kind.resPackageName; |
| 539 | entry.actionIcon = kind.iconRes; |
| 540 | } |
| 541 | |
| 542 | return entry; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * Apply given {@link DataStatus} values over this {@link ViewEntry} |
| 547 | * |
| 548 | * @param fillData When true, the given status replaces {@link #data} |
| 549 | * and {@link #footerLine}. Otherwise only {@link #presence} |
| 550 | * is updated. |
| 551 | */ |
| 552 | public ViewEntry applyStatus(DataStatus status, boolean fillData) { |
| 553 | presence = status.getPresence(); |
| 554 | if (fillData && status.isValid()) { |
| 555 | this.data = status.getStatus().toString(); |
| 556 | this.footerLine = status.getTimestampLabel(context); |
| 557 | } |
| 558 | |
| 559 | return this; |
| 560 | } |
| 561 | |
| 562 | public boolean collapseWith(ViewEntry entry) { |
| 563 | // assert equal collapse keys |
| 564 | if (!shouldCollapseWith(entry)) { |
| 565 | return false; |
| 566 | } |
| 567 | |
| 568 | // Choose the label associated with the highest type precedence. |
| 569 | if (TypePrecedence.getTypePrecedence(mimetype, type) |
| 570 | > TypePrecedence.getTypePrecedence(entry.mimetype, entry.type)) { |
| 571 | type = entry.type; |
| 572 | label = entry.label; |
| 573 | } |
| 574 | |
| 575 | // Choose the max of the maxLines and maxLabelLines values. |
| 576 | maxLines = Math.max(maxLines, entry.maxLines); |
| 577 | maxLabelLines = Math.max(maxLabelLines, entry.maxLabelLines); |
| 578 | |
| 579 | // Choose the presence with the highest precedence. |
| 580 | if (StatusUpdates.getPresencePrecedence(presence) |
| 581 | < StatusUpdates.getPresencePrecedence(entry.presence)) { |
| 582 | presence = entry.presence; |
| 583 | } |
| 584 | |
| 585 | // If any of the collapsed entries are primary make the whole thing primary. |
| 586 | isPrimary = entry.isPrimary ? true : isPrimary; |
| 587 | |
| 588 | // uri, and contactdId, shouldn't make a difference. Just keep the original. |
| 589 | |
| 590 | // Keep track of all the ids that have been collapsed with this one. |
| 591 | ids.add(entry.id); |
| 592 | collapseCount++; |
| 593 | return true; |
| 594 | } |
| 595 | |
| 596 | public boolean shouldCollapseWith(ViewEntry entry) { |
| 597 | if (entry == null) { |
| 598 | return false; |
| 599 | } |
| 600 | |
| 601 | if (!ContactsUtils.shouldCollapse(context, mimetype, data, entry.mimetype, |
| 602 | entry.data)) { |
| 603 | return false; |
| 604 | } |
| 605 | |
| 606 | if (!TextUtils.equals(mimetype, entry.mimetype) |
| 607 | || !ContactsUtils.areIntentActionEqual(intent, entry.intent) |
| 608 | || !ContactsUtils.areIntentActionEqual(secondaryIntent, entry.secondaryIntent) |
| 609 | || actionIcon != entry.actionIcon) { |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | return true; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /** Cache of the children views of a row */ |
| 618 | private static class ViewCache { |
| 619 | public TextView label; |
| 620 | public TextView data; |
| 621 | public TextView footer; |
| 622 | public ImageView actionIcon; |
| 623 | public ImageView presenceIcon; |
| 624 | public ImageView primaryIcon; |
| 625 | public ImageView secondaryActionButton; |
| 626 | public View secondaryActionDivider; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 629 | private final class ViewAdapter extends BaseAdapter { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 630 | public View getView(int position, View convertView, ViewGroup parent) { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 631 | final ViewEntry entry = getEntry(position); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 632 | final View v; |
Daniel Lehmann | c3a0008 | 2010-04-13 13:53:54 -0700 | [diff] [blame] | 633 | final ViewCache viewCache; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 634 | |
| 635 | // Check to see if we can reuse convertView |
| 636 | if (convertView != null) { |
| 637 | v = convertView; |
Daniel Lehmann | c3a0008 | 2010-04-13 13:53:54 -0700 | [diff] [blame] | 638 | viewCache = (ViewCache) v.getTag(); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 639 | } else { |
| 640 | // Create a new view if needed |
| 641 | v = mInflater.inflate(R.layout.list_item_text_icons, parent, false); |
| 642 | |
| 643 | // Cache the children |
Daniel Lehmann | c3a0008 | 2010-04-13 13:53:54 -0700 | [diff] [blame] | 644 | viewCache = new ViewCache(); |
| 645 | viewCache.label = (TextView) v.findViewById(android.R.id.text1); |
| 646 | viewCache.data = (TextView) v.findViewById(android.R.id.text2); |
| 647 | viewCache.footer = (TextView) v.findViewById(R.id.footer); |
| 648 | viewCache.actionIcon = (ImageView) v.findViewById(R.id.action_icon); |
| 649 | viewCache.primaryIcon = (ImageView) v.findViewById(R.id.primary_icon); |
| 650 | viewCache.presenceIcon = (ImageView) v.findViewById(R.id.presence_icon); |
| 651 | viewCache.secondaryActionButton = (ImageView) v.findViewById( |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 652 | R.id.secondary_action_button); |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 653 | viewCache.secondaryActionButton.setOnClickListener(mSecondaryActionClickListener); |
Daniel Lehmann | c3a0008 | 2010-04-13 13:53:54 -0700 | [diff] [blame] | 654 | viewCache.secondaryActionDivider = v.findViewById(R.id.divider); |
| 655 | v.setTag(viewCache); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 658 | // Bind the data to the view |
| 659 | bindView(v, entry); |
| 660 | return v; |
| 661 | } |
| 662 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 663 | protected void bindView(View view, ViewEntry entry) { |
| 664 | final Resources resources = mContext.getResources(); |
| 665 | ViewCache views = (ViewCache) view.getTag(); |
| 666 | |
| 667 | // Set the label |
| 668 | TextView label = views.label; |
| 669 | setMaxLines(label, entry.maxLabelLines); |
| 670 | label.setText(entry.label); |
| 671 | |
| 672 | // Set the data |
| 673 | TextView data = views.data; |
| 674 | if (data != null) { |
| 675 | if (entry.mimetype.equals(Phone.CONTENT_ITEM_TYPE) |
| 676 | || entry.mimetype.equals(Constants.MIME_SMS_ADDRESS)) { |
| 677 | data.setText(PhoneNumberUtils.formatNumber(entry.data)); |
| 678 | } else { |
| 679 | data.setText(entry.data); |
| 680 | } |
| 681 | setMaxLines(data, entry.maxLines); |
| 682 | } |
| 683 | |
| 684 | // Set the footer |
| 685 | if (!TextUtils.isEmpty(entry.footerLine)) { |
| 686 | views.footer.setText(entry.footerLine); |
| 687 | views.footer.setVisibility(View.VISIBLE); |
| 688 | } else { |
| 689 | views.footer.setVisibility(View.GONE); |
| 690 | } |
| 691 | |
| 692 | // Set the primary icon |
| 693 | views.primaryIcon.setVisibility(entry.isPrimary ? View.VISIBLE : View.GONE); |
| 694 | |
| 695 | // Set the action icon |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 696 | final ImageView action = views.actionIcon; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 697 | if (entry.actionIcon != -1) { |
| 698 | Drawable actionIcon; |
| 699 | if (entry.resPackageName != null) { |
| 700 | // Load external resources through PackageManager |
| 701 | actionIcon = mContext.getPackageManager().getDrawable(entry.resPackageName, |
| 702 | entry.actionIcon, null); |
| 703 | } else { |
| 704 | actionIcon = resources.getDrawable(entry.actionIcon); |
| 705 | } |
| 706 | action.setImageDrawable(actionIcon); |
| 707 | action.setVisibility(View.VISIBLE); |
| 708 | } else { |
| 709 | // Things should still line up as if there was an icon, so make it invisible |
| 710 | action.setVisibility(View.INVISIBLE); |
| 711 | } |
| 712 | |
| 713 | // Set the presence icon |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 714 | final Drawable presenceIcon = ContactPresenceIconUtil.getPresenceIcon( |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 715 | mContext, entry.presence); |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 716 | final ImageView presenceIconView = views.presenceIcon; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 717 | if (presenceIcon != null) { |
| 718 | presenceIconView.setImageDrawable(presenceIcon); |
| 719 | presenceIconView.setVisibility(View.VISIBLE); |
| 720 | } else { |
| 721 | presenceIconView.setVisibility(View.GONE); |
| 722 | } |
| 723 | |
| 724 | // Set the secondary action button |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 725 | final ImageView secondaryActionView = views.secondaryActionButton; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 726 | Drawable secondaryActionIcon = null; |
| 727 | if (entry.secondaryActionIcon != -1) { |
| 728 | secondaryActionIcon = resources.getDrawable(entry.secondaryActionIcon); |
| 729 | } |
| 730 | if (entry.secondaryIntent != null && secondaryActionIcon != null) { |
| 731 | secondaryActionView.setImageDrawable(secondaryActionIcon); |
Daniel Lehmann | c3a0008 | 2010-04-13 13:53:54 -0700 | [diff] [blame] | 732 | secondaryActionView.setTag(entry); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 733 | secondaryActionView.setVisibility(View.VISIBLE); |
| 734 | views.secondaryActionDivider.setVisibility(View.VISIBLE); |
| 735 | } else { |
| 736 | secondaryActionView.setVisibility(View.GONE); |
| 737 | views.secondaryActionDivider.setVisibility(View.GONE); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | private void setMaxLines(TextView textView, int maxLines) { |
| 742 | if (maxLines == 1) { |
| 743 | textView.setSingleLine(true); |
| 744 | textView.setEllipsize(TextUtils.TruncateAt.END); |
| 745 | } else { |
| 746 | textView.setSingleLine(false); |
| 747 | textView.setMaxLines(maxLines); |
| 748 | textView.setEllipsize(null); |
| 749 | } |
| 750 | } |
| 751 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 752 | private OnClickListener mSecondaryActionClickListener = new OnClickListener() { |
| 753 | public void onClick(View v) { |
| 754 | if (mListener == null) return; |
| 755 | if (v == null) return; |
| 756 | final ViewEntry entry = (ViewEntry) v.getTag(); |
| 757 | if (entry == null) return; |
| 758 | final Intent intent = entry.secondaryIntent; |
| 759 | if (intent == null) return; |
| 760 | mListener.onItemClicked(intent); |
| 761 | } |
| 762 | }; |
| 763 | |
| 764 | public int getCount() { |
| 765 | int count = 0; |
| 766 | final int numSections = mSections.size(); |
| 767 | for (int i = 0; i < numSections; i++) { |
| 768 | final ArrayList<ViewEntry> section = mSections.get(i); |
| 769 | count += section.size(); |
| 770 | } |
| 771 | return count; |
| 772 | } |
| 773 | |
| 774 | public Object getItem(int position) { |
| 775 | return getEntry(position); |
| 776 | } |
| 777 | |
| 778 | public long getItemId(int position) { |
| 779 | final ViewEntry entry = getEntry(position); |
| 780 | if (entry != null) { |
| 781 | return entry.id; |
| 782 | } else { |
| 783 | return -1; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | private ViewEntry getEntry(int position) { |
| 788 | final int numSections = mSections.size(); |
| 789 | for (int i = 0; i < numSections; i++) { |
| 790 | final ArrayList<ViewEntry> section = mSections.get(i); |
| 791 | final int sectionSize = section.size(); |
| 792 | if (position < sectionSize) { |
| 793 | return section.get(position); |
| 794 | } |
| 795 | position -= sectionSize; |
| 796 | } |
| 797 | return null; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
| 801 | public boolean onCreateOptionsMenu(Menu menu, final MenuInflater inflater) { |
| 802 | inflater.inflate(R.menu.view, menu); |
| 803 | return true; |
| 804 | } |
| 805 | |
| 806 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 807 | // Only allow edit when we have at least one raw_contact id |
| 808 | final boolean hasRawContact = (mRawContactIds.size() > 0); |
| 809 | menu.findItem(R.id.menu_edit).setEnabled(hasRawContact); |
| 810 | |
| 811 | // Only allow share when unrestricted contacts available |
| 812 | menu.findItem(R.id.menu_share).setEnabled(!mAllRestricted); |
| 813 | |
| 814 | return true; |
| 815 | } |
| 816 | |
| 817 | public boolean onOptionsItemSelected(MenuItem item) { |
| 818 | switch (item.getItemId()) { |
| 819 | case R.id.menu_edit: { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 820 | mListener.onEditRequested(mLookupUri); |
| 821 | break; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 822 | } |
| 823 | case R.id.menu_delete: { |
| 824 | showDeleteConfirmationDialog(); |
| 825 | return true; |
| 826 | } |
| 827 | case R.id.menu_options: { |
| 828 | final Intent intent = new Intent(mContext, ContactOptionsActivity.class); |
| 829 | intent.setData(mContactData.getLookupUri()); |
| 830 | mContext.startActivity(intent); |
| 831 | return true; |
| 832 | } |
| 833 | case R.id.menu_share: { |
| 834 | if (mAllRestricted) return false; |
| 835 | |
| 836 | final String lookupKey = mContactData.getLookupKey(); |
| 837 | final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey); |
| 838 | |
| 839 | final Intent intent = new Intent(Intent.ACTION_SEND); |
| 840 | intent.setType(Contacts.CONTENT_VCARD_TYPE); |
| 841 | intent.putExtra(Intent.EXTRA_STREAM, shareUri); |
| 842 | |
| 843 | // Launch chooser to share contact via |
| 844 | final CharSequence chooseTitle = mContext.getText(R.string.share_via); |
| 845 | final Intent chooseIntent = Intent.createChooser(intent, chooseTitle); |
| 846 | |
| 847 | try { |
| 848 | mContext.startActivity(chooseIntent); |
| 849 | } catch (ActivityNotFoundException ex) { |
| 850 | Toast.makeText(mContext, R.string.share_error, Toast.LENGTH_SHORT).show(); |
| 851 | } |
| 852 | return true; |
| 853 | } |
| 854 | } |
| 855 | return false; |
| 856 | } |
| 857 | |
| 858 | private void showDeleteConfirmationDialog() { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 859 | final int id; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 860 | if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 861 | id = R.id.detail_dialog_confirm_readonly_delete; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 862 | } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 863 | id = R.id.detail_dialog_confirm_readonly_hide; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 864 | } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 865 | id = R.id.detail_dialog_confirm_multiple_delete; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 866 | } else { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 867 | id = R.id.detail_dialog_confirm_delete; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 868 | } |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 869 | if (mListener != null) mListener.onDialogRequested(id, null); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { |
| 873 | AdapterView.AdapterContextMenuInfo info; |
| 874 | try { |
| 875 | info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
| 876 | } catch (ClassCastException e) { |
| 877 | Log.e(TAG, "bad menuInfo", e); |
| 878 | return; |
| 879 | } |
| 880 | |
| 881 | // This can be null sometimes, don't crash... |
| 882 | if (info == null) { |
| 883 | Log.e(TAG, "bad menuInfo"); |
| 884 | return; |
| 885 | } |
| 886 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 887 | final ViewEntry entry = mAdapter.getEntry(info.position); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 888 | menu.setHeaderTitle(R.string.contactOptionsTitle); |
| 889 | if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) { |
| 890 | menu.add(0, 0, 0, R.string.menu_call).setIntent(entry.intent); |
| 891 | menu.add(0, 0, 0, R.string.menu_sendSMS).setIntent(entry.secondaryIntent); |
| 892 | if (!entry.isPrimary) { |
| 893 | menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultNumber); |
| 894 | } |
| 895 | } else if (entry.mimetype.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE)) { |
| 896 | menu.add(0, 0, 0, R.string.menu_sendEmail).setIntent(entry.intent); |
| 897 | if (!entry.isPrimary) { |
| 898 | menu.add(0, MENU_ITEM_MAKE_DEFAULT, 0, R.string.menu_makeDefaultEmail); |
| 899 | } |
| 900 | } else if (entry.mimetype.equals(CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)) { |
| 901 | menu.add(0, 0, 0, R.string.menu_viewAddress).setIntent(entry.intent); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 906 | if (mListener == null) return; |
| 907 | final ViewEntry entry = mAdapter.getEntry(position); |
Daniel Lehmann | c3a0008 | 2010-04-13 13:53:54 -0700 | [diff] [blame] | 908 | if (entry == null) return; |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 909 | final Intent intent = entry.intent; |
| 910 | if (intent == null) return; |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 911 | mListener.onItemClicked(intent); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | private final DialogInterface.OnClickListener mDeleteListener = |
| 915 | new DialogInterface.OnClickListener() { |
| 916 | public void onClick(DialogInterface dialog, int which) { |
| 917 | mContext.getContentResolver().delete(mContactData.getLookupUri(), null, null); |
| 918 | } |
| 919 | }; |
| 920 | |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 921 | public Dialog onCreateDialog(int id, Bundle bundle) { |
| 922 | switch (id) { |
| 923 | case R.id.detail_dialog_confirm_delete: |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 924 | return new AlertDialog.Builder(mContext) |
| 925 | .setTitle(R.string.deleteConfirmation_title) |
| 926 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 927 | .setMessage(R.string.deleteConfirmation) |
| 928 | .setNegativeButton(android.R.string.cancel, null) |
| 929 | .setPositiveButton(android.R.string.ok, mDeleteListener) |
| 930 | .setCancelable(false) |
| 931 | .create(); |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 932 | case R.id.detail_dialog_confirm_readonly_delete: |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 933 | return new AlertDialog.Builder(mContext) |
| 934 | .setTitle(R.string.deleteConfirmation_title) |
| 935 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 936 | .setMessage(R.string.readOnlyContactDeleteConfirmation) |
| 937 | .setNegativeButton(android.R.string.cancel, null) |
| 938 | .setPositiveButton(android.R.string.ok, mDeleteListener) |
| 939 | .setCancelable(false) |
| 940 | .create(); |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 941 | case R.id.detail_dialog_confirm_multiple_delete: |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 942 | return new AlertDialog.Builder(mContext) |
| 943 | .setTitle(R.string.deleteConfirmation_title) |
| 944 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 945 | .setMessage(R.string.multipleContactDeleteConfirmation) |
| 946 | .setNegativeButton(android.R.string.cancel, null) |
| 947 | .setPositiveButton(android.R.string.ok, mDeleteListener) |
| 948 | .setCancelable(false) |
| 949 | .create(); |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 950 | case R.id.detail_dialog_confirm_readonly_hide: { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 951 | return new AlertDialog.Builder(mContext) |
| 952 | .setTitle(R.string.deleteConfirmation_title) |
| 953 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 954 | .setMessage(R.string.readOnlyContactWarning) |
| 955 | .setPositiveButton(android.R.string.ok, mDeleteListener) |
| 956 | .create(); |
| 957 | } |
| 958 | default: |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 959 | return null; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 963 | public boolean onContextItemSelected(MenuItem item) { |
| 964 | switch (item.getItemId()) { |
| 965 | case MENU_ITEM_MAKE_DEFAULT: { |
| 966 | if (makeItemDefault(item)) { |
| 967 | return true; |
| 968 | } |
| 969 | break; |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | return false; |
| 974 | } |
| 975 | |
| 976 | private boolean makeItemDefault(MenuItem item) { |
| 977 | ViewEntry entry = getViewEntryForMenuItem(item); |
| 978 | if (entry == null) { |
| 979 | return false; |
| 980 | } |
| 981 | |
| 982 | // Update the primary values in the data record. |
| 983 | ContentValues values = new ContentValues(1); |
| 984 | values.put(Data.IS_SUPER_PRIMARY, 1); |
| 985 | |
| 986 | mContext.getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, entry.id), |
| 987 | values, null, null); |
| 988 | return true; |
| 989 | } |
| 990 | |
| 991 | private ViewEntry getViewEntryForMenuItem(MenuItem item) { |
| 992 | AdapterView.AdapterContextMenuInfo info; |
| 993 | try { |
| 994 | info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); |
| 995 | } catch (ClassCastException e) { |
| 996 | Log.e(TAG, "bad menuInfo", e); |
| 997 | return null; |
| 998 | } |
| 999 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 1000 | return mAdapter.getEntry(info.position); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1003 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 1004 | switch (keyCode) { |
| 1005 | case KeyEvent.KEYCODE_CALL: { |
| 1006 | try { |
| 1007 | ITelephony phone = ITelephony.Stub.asInterface( |
| 1008 | ServiceManager.checkService("phone")); |
| 1009 | if (phone != null && !phone.isIdle()) { |
| 1010 | // Skip out and let the key be handled at a higher level |
| 1011 | break; |
| 1012 | } |
| 1013 | } catch (RemoteException re) { |
| 1014 | // Fall through and try to call the contact |
| 1015 | } |
| 1016 | |
| 1017 | int index = mListView.getSelectedItemPosition(); |
| 1018 | if (index != -1) { |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 1019 | final ViewEntry entry = mAdapter.getEntry(index); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1020 | if (entry != null && |
| 1021 | entry.intent.getAction() == Intent.ACTION_CALL_PRIVILEGED) { |
| 1022 | mContext.startActivity(entry.intent); |
| 1023 | return true; |
| 1024 | } |
| 1025 | } else if (mPrimaryPhoneUri != null) { |
| 1026 | // There isn't anything selected, call the default number |
| 1027 | final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, |
| 1028 | mPrimaryPhoneUri); |
| 1029 | mContext.startActivity(intent); |
| 1030 | return true; |
| 1031 | } |
| 1032 | return false; |
| 1033 | } |
| 1034 | |
| 1035 | case KeyEvent.KEYCODE_DEL: { |
| 1036 | showDeleteConfirmationDialog(); |
| 1037 | return true; |
| 1038 | } |
| 1039 | } |
| 1040 | |
Daniel Lehmann | c2687c3 | 2010-04-19 18:20:44 -0700 | [diff] [blame] | 1041 | return false; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1042 | } |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 1043 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 1044 | private ContactDetailHeaderView.Listener mHeaderViewListener = |
| 1045 | new ContactDetailHeaderView.Listener() { |
| 1046 | public void onDisplayNameClick(View view) { |
| 1047 | } |
| 1048 | |
| 1049 | public void onEditClicked() { |
| 1050 | if (mListener != null) mListener.onEditRequested(mLookupUri); |
| 1051 | } |
| 1052 | |
| 1053 | public void onPhotoClick(View view) { |
| 1054 | } |
| 1055 | }; |
| 1056 | |
| 1057 | |
| 1058 | public static interface Listener { |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 1059 | /** |
| 1060 | * Contact was not found, so somehow close this fragment. |
| 1061 | */ |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 1062 | public void onContactNotFound(); |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 1063 | |
| 1064 | /** |
| 1065 | * User decided to go to Edit-Mode |
| 1066 | */ |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 1067 | public void onEditRequested(Uri lookupUri); |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 1068 | |
| 1069 | /** |
| 1070 | * User clicked a single item (e.g. mail) |
| 1071 | */ |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame^] | 1072 | public void onItemClicked(Intent intent); |
| 1073 | |
| 1074 | /** |
| 1075 | * Show a dialog using the globally unique id |
| 1076 | */ |
| 1077 | public void onDialogRequested(int id, Bundle bundle); |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 1078 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1079 | } |