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