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