blob: fe203a21cb76d4c8e4466ec1e56675dfd048aa8c [file] [log] [blame]
Daniel Lehmann4cd94412010-04-08 16:44:36 -07001/*
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
17package com.android.contacts.views.detail;
18
19import com.android.contacts.Collapser;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070020import com.android.contacts.ContactOptionsActivity;
21import com.android.contacts.ContactPresenceIconUtil;
22import com.android.contacts.ContactsUtils;
23import com.android.contacts.R;
24import com.android.contacts.TypePrecedence;
25import com.android.contacts.Collapser.Collapsible;
26import com.android.contacts.model.ContactsSource;
27import com.android.contacts.model.Sources;
28import com.android.contacts.model.ContactsSource.DataKind;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070029import com.android.contacts.util.Constants;
30import com.android.contacts.util.DataStatus;
Daniel Lehmannf77b8e92010-07-16 18:58:36 -070031import com.android.contacts.util.PhoneCapabilityTester;
Daniel Lehmanncdef2b62010-06-06 18:25:49 -070032import com.android.contacts.views.ContactLoader;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070033import com.android.internal.telephony.ITelephony;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070034
Daniel Lehmann25a02822010-05-11 15:52:43 -070035import android.app.Activity;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070036import android.app.AlertDialog;
37import android.app.Dialog;
Daniel Lehmann222744e2010-07-12 16:28:06 -070038import android.app.Fragment;
39import android.app.LoaderManager;
40import android.app.LoaderManager.LoaderCallbacks;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070041import android.content.ActivityNotFoundException;
42import android.content.ContentUris;
43import android.content.ContentValues;
44import android.content.Context;
45import android.content.DialogInterface;
46import android.content.Entity;
47import android.content.Intent;
Jeff Hamilton3c462912010-05-15 02:20:01 -050048import android.content.Loader;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070049import android.content.Entity.NamedContentValues;
50import android.content.res.Resources;
51import android.graphics.drawable.Drawable;
52import android.net.ParseException;
53import android.net.Uri;
54import android.net.WebAddress;
55import android.os.Bundle;
56import android.os.RemoteException;
57import android.os.ServiceManager;
58import android.provider.ContactsContract.CommonDataKinds;
59import android.provider.ContactsContract.Contacts;
60import android.provider.ContactsContract.Data;
61import android.provider.ContactsContract.DisplayNameSources;
62import android.provider.ContactsContract.RawContacts;
63import android.provider.ContactsContract.StatusUpdates;
64import android.provider.ContactsContract.CommonDataKinds.Email;
65import android.provider.ContactsContract.CommonDataKinds.Im;
66import android.provider.ContactsContract.CommonDataKinds.Nickname;
67import android.provider.ContactsContract.CommonDataKinds.Note;
68import android.provider.ContactsContract.CommonDataKinds.Organization;
69import android.provider.ContactsContract.CommonDataKinds.Phone;
Daniel Lehmanncdef2b62010-06-06 18:25:49 -070070import android.provider.ContactsContract.CommonDataKinds.StructuredName;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070071import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
72import android.provider.ContactsContract.CommonDataKinds.Website;
73import android.telephony.PhoneNumberUtils;
74import android.text.TextUtils;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070075import android.util.Log;
76import android.view.ContextMenu;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
80import android.view.MenuInflater;
81import android.view.MenuItem;
82import android.view.View;
83import android.view.ViewGroup;
84import android.view.ContextMenu.ContextMenuInfo;
Daniel Lehmannc2687c32010-04-19 18:20:44 -070085import android.view.View.OnClickListener;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070086import android.view.View.OnCreateContextMenuListener;
87import android.widget.AdapterView;
Daniel Lehmanncdef2b62010-06-06 18:25:49 -070088import android.widget.BaseAdapter;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070089import android.widget.ImageView;
Daniel Lehmann4cd94412010-04-08 16:44:36 -070090import android.widget.ListView;
91import android.widget.TextView;
92import android.widget.Toast;
93import android.widget.AdapterView.OnItemClickListener;
94
95import java.util.ArrayList;
96
Daniel Lehmann222744e2010-07-12 16:28:06 -070097public class ContactDetailFragment extends Fragment
Daniel Lehmann18f104f2010-05-07 15:41:11 -070098 implements OnCreateContextMenuListener, OnItemClickListener {
Daniel Lehmanncdef2b62010-06-06 18:25:49 -070099 private static final String TAG = "ContactDetailFragment";
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700100
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700101 private static final int MENU_ITEM_MAKE_DEFAULT = 3;
102
Daniel Lehmann18f104f2010-05-07 15:41:11 -0700103 private static final int LOADER_DETAILS = 1;
104
Daniel Lehmann25a02822010-05-11 15:52:43 -0700105 private Context mContext;
106 private Uri mLookupUri;
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700107 private Listener mListener;
Daniel Lehmannc2687c32010-04-19 18:20:44 -0700108
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700109 private ContactLoader.Result mContactData;
110 private ContactDetailHeaderView mHeaderView;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700111 private ListView mListView;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700112 private ViewAdapter mAdapter;
113 private Uri mPrimaryPhoneUri = null;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700114
115 private int mReadOnlySourcesCnt;
116 private int mWritableSourcesCnt;
117 private boolean mAllRestricted;
Daniel Lehmann18f104f2010-05-07 15:41:11 -0700118 private final ArrayList<Long> mWritableRawContactIds = new ArrayList<Long>();
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700119 private int mNumPhoneNumbers = 0;
120
121 /**
Daniel Lehmannf77b8e92010-07-16 18:58:36 -0700122 * 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 Lehmann4cd94412010-04-08 16:44:36 -0700132 * 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 Lehmanncdef2b62010-06-06 18:25:49 -0700152 private LayoutInflater mInflater;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700153
Daniel Lehmann25a02822010-05-11 15:52:43 -0700154 public ContactDetailFragment() {
155 // Explicit constructor for inflation
Daniel Lehmannc2687c32010-04-19 18:20:44 -0700156
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 Lehmann25a02822010-05-11 15:52:43 -0700168 }
169
170 @Override
171 public void onAttach(Activity activity) {
172 super.onAttach(activity);
173 mContext = activity;
174 }
175
176 @Override
Dianne Hackborn84df2b62010-05-12 19:04:42 -0700177 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700178 final View view = inflater.inflate(R.layout.contact_detail_fragment, container, false);
Daniel Lehmann25a02822010-05-11 15:52:43 -0700179
Dianne Hackborneb4d1bf2010-06-08 23:12:51 -0700180 setHasOptionsMenu(true);
Daniel Lehmannb2257cf2010-06-15 13:36:07 -0700181
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700182 mInflater = inflater;
183
184 mHeaderView = (ContactDetailHeaderView) view.findViewById(R.id.contact_header_widget);
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700185 mHeaderView.setExcludeMimes(new String[] {
Daniel Lehmann25a02822010-05-11 15:52:43 -0700186 Contacts.CONTENT_ITEM_TYPE
187 });
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700188 mHeaderView.setListener(mHeaderViewListener);
Daniel Lehmann25a02822010-05-11 15:52:43 -0700189
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 Lehmann25a02822010-05-11 15:52:43 -0700197 return view;
198 }
199
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700200 public void setListener(Listener value) {
201 mListener = value;
Daniel Lehmann25a02822010-05-11 15:52:43 -0700202 }
203
Dmitri Plotnikov9cd17082010-07-09 14:10:04 -0700204 public Uri getUri() {
205 return mLookupUri;
206 }
207
Daniel Lehmann222744e2010-07-12 16:28:06 -0700208 @Override
209 public void onActivityCreated(Bundle savedInstanceState) {
210 super.onActivityCreated(savedInstanceState);
211
212 getLoaderManager().initLoader(LOADER_DETAILS, null, mDetailLoaderListener);
213 }
214
Daniel Lehmann25a02822010-05-11 15:52:43 -0700215 public void loadUri(Uri lookupUri) {
Daniel Lehmann18f104f2010-05-07 15:41:11 -0700216 mLookupUri = lookupUri;
Daniel Lehmann222744e2010-07-12 16:28:06 -0700217 if (getActivity() != null) {
218 getLoaderManager().restartLoader(LOADER_DETAILS, null, mDetailLoaderListener);
Daniel Lehmann18f104f2010-05-07 15:41:11 -0700219 }
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700220 }
221
222 private void bindData() {
Daniel Lehmannd3e0cdb2010-04-19 13:45:53 -0700223 // Set the header
Daniel Lehmannb2257cf2010-06-15 13:36:07 -0700224 mHeaderView.loadData(mContactData);
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700225
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 Lehmanncdef2b62010-06-06 18:25:49 -0700237 mAdapter = new ViewAdapter();
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700238 mListView.setAdapter(mAdapter);
239 } else {
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700240 mAdapter.notifyDataSetChanged();
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700241 }
242 mListView.setEmptyView(mEmptyView);
243 }
244
245 /**
246 * Build up the entries to display on the screen.
247 */
248 private final void buildEntries() {
Daniel Lehmannf77b8e92010-07-16 18:58:36 -0700249 mHasPhone = PhoneCapabilityTester.isPhoneCallIntentRegistered(mContext);
250 mHasSms = PhoneCapabilityTester.isSmsIntentRegistered(mContext);
251
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700252 // 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 Lehmanncdef2b62010-06-06 18:25:49 -0700268 // TODO: This should be done in the background thread
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700269 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 Lehmanncdef2b62010-06-06 18:25:49 -0700317 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 Lehmann4cd94412010-04-08 16:44:36 -0700320 // Build phone entries
321 mNumPhoneNumbers++;
322
Daniel Lehmannf77b8e92010-07-16 18:58:36 -0700323 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 Lehmann4cd94412010-04-08 16:44:36 -0700343
344 // Remember super-primary phone
345 if (isSuperPrimary) mPrimaryPhoneUri = entry.uri;
346
347 entry.isPrimary = isSuperPrimary;
348 mPhoneEntries.add(entry);
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700349 } 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 Lehmanncdef2b62010-06-06 18:25:49 -0700457 private static String buildActionString(DataKind kind, ContentValues values,
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700458 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 Lehmanncdef2b62010-06-06 18:25:49 -0700469 private static String buildDataString(DataKind kind, ContentValues values,
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700470 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 Lehmanncdef2b62010-06-06 18:25:49 -0700481 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 Lehmann4cd94412010-04-08 16:44:36 -0700491 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 Lehmann4cd94412010-04-08 16:44:36 -0700516 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 Lehmann4cd94412010-04-08 16:44:36 -0700615 }
616
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700617 private final class ViewAdapter extends BaseAdapter {
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700618 public View getView(int position, View convertView, ViewGroup parent) {
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700619 final ViewEntry entry = getEntry(position);
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700620 final View v;
Daniel Lehmannc3a00082010-04-13 13:53:54 -0700621 final ViewCache viewCache;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700622
623 // Check to see if we can reuse convertView
624 if (convertView != null) {
625 v = convertView;
Daniel Lehmannc3a00082010-04-13 13:53:54 -0700626 viewCache = (ViewCache) v.getTag();
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700627 } 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 Lehmannc3a00082010-04-13 13:53:54 -0700632 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 Lehmann4cd94412010-04-08 16:44:36 -0700640 R.id.secondary_action_button);
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700641 viewCache.secondaryActionButton.setOnClickListener(mSecondaryActionClickListener);
Daniel Lehmannc3a00082010-04-13 13:53:54 -0700642 viewCache.secondaryActionDivider = v.findViewById(R.id.divider);
643 v.setTag(viewCache);
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700644 }
645
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700646 // Bind the data to the view
647 bindView(v, entry);
648 return v;
649 }
650
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700651 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 Lehmanncdef2b62010-06-06 18:25:49 -0700684 final ImageView action = views.actionIcon;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700685 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 Lehmanncdef2b62010-06-06 18:25:49 -0700702 final Drawable presenceIcon = ContactPresenceIconUtil.getPresenceIcon(
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700703 mContext, entry.presence);
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700704 final ImageView presenceIconView = views.presenceIcon;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700705 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 Lehmanncdef2b62010-06-06 18:25:49 -0700713 final ImageView secondaryActionView = views.secondaryActionButton;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700714 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 Lehmannc3a00082010-04-13 13:53:54 -0700720 secondaryActionView.setTag(entry);
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700721 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 Lehmanncdef2b62010-06-06 18:25:49 -0700740 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 Lehmann4cd94412010-04-08 16:44:36 -0700786 }
787 }
788
Daniel Lehmann3648d2e2010-06-30 19:58:31 +0200789 @Override
Dianne Hackborneb4d1bf2010-06-08 23:12:51 -0700790 public void onCreateOptionsMenu(Menu menu, final MenuInflater inflater) {
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700791 inflater.inflate(R.menu.view, menu);
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700792 }
793
Daniel Lehmann3648d2e2010-06-30 19:58:31 +0200794 @Override
Daniel Lehmannf77b8e92010-07-16 18:58:36 -0700795 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 Lehmann4cd94412010-04-08 16:44:36 -0700804 public boolean onOptionsItemSelected(MenuItem item) {
805 switch (item.getItemId()) {
806 case R.id.menu_edit: {
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700807 mListener.onEditRequested(mLookupUri);
808 break;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700809 }
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 Lehmanncdef2b62010-06-06 18:25:49 -0700846 final int id;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700847 if (mReadOnlySourcesCnt > 0 & mWritableSourcesCnt > 0) {
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700848 id = R.id.detail_dialog_confirm_readonly_delete;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700849 } else if (mReadOnlySourcesCnt > 0 && mWritableSourcesCnt == 0) {
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700850 id = R.id.detail_dialog_confirm_readonly_hide;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700851 } else if (mReadOnlySourcesCnt == 0 && mWritableSourcesCnt > 1) {
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700852 id = R.id.detail_dialog_confirm_multiple_delete;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700853 } else {
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700854 id = R.id.detail_dialog_confirm_delete;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700855 }
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700856 if (mListener != null) mListener.onDialogRequested(id, null);
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700857 }
858
Dmitri Plotnikovd3643142010-07-09 13:49:07 -0700859 @Override
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700860 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 Lehmanncdef2b62010-06-06 18:25:49 -0700875 final ViewEntry entry = mAdapter.getEntry(info.position);
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700876 menu.setHeaderTitle(R.string.contactOptionsTitle);
877 if (entry.mimetype.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
Daniel Lehmannf77b8e92010-07-16 18:58:36 -0700878 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 Lehmann4cd94412010-04-08 16:44:36 -0700887 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 Lehmanncdef2b62010-06-06 18:25:49 -0700900 if (mListener == null) return;
901 final ViewEntry entry = mAdapter.getEntry(position);
Daniel Lehmannc3a00082010-04-13 13:53:54 -0700902 if (entry == null) return;
Daniel Lehmann18f104f2010-05-07 15:41:11 -0700903 final Intent intent = entry.intent;
904 if (intent == null) return;
Daniel Lehmanncdef2b62010-06-06 18:25:49 -0700905 mListener.onItemClicked(intent);
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700906 }
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 Lehmann18f104f2010-05-07 15:41:11 -0700915 public Dialog onCreateDialog(int id, Bundle bundle) {
916 switch (id) {
917 case R.id.detail_dialog_confirm_delete:
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700918 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 Lehmann18f104f2010-05-07 15:41:11 -0700926 case R.id.detail_dialog_confirm_readonly_delete:
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700927 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 Lehmann18f104f2010-05-07 15:41:11 -0700935 case R.id.detail_dialog_confirm_multiple_delete:
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700936 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 Lehmann18f104f2010-05-07 15:41:11 -0700944 case R.id.detail_dialog_confirm_readonly_hide: {
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700945 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 Lehmann18f104f2010-05-07 15:41:11 -0700953 return null;
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700954 }
955 }
956
Dmitri Plotnikovd3643142010-07-09 13:49:07 -0700957 @Override
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700958 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 Lehmanncdef2b62010-06-06 18:25:49 -0700995 return mAdapter.getEntry(info.position);
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700996 }
997
Daniel Lehmann4cd94412010-04-08 16:44:36 -0700998 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 Lehmanncdef2b62010-06-06 18:25:49 -07001014 final ViewEntry entry = mAdapter.getEntry(index);
Daniel Lehmann4cd94412010-04-08 16:44:36 -07001015 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 Lehmannc2687c32010-04-19 18:20:44 -07001036 return false;
Daniel Lehmann4cd94412010-04-08 16:44:36 -07001037 }
Daniel Lehmann18f104f2010-05-07 15:41:11 -07001038
Daniel Lehmann222744e2010-07-12 16:28:06 -07001039 /**
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 Lehmanncdef2b62010-06-06 18:25:49 -07001062 private ContactDetailHeaderView.Listener mHeaderViewListener =
1063 new ContactDetailHeaderView.Listener() {
Daniel Lehmannbe82afa2010-07-14 15:31:56 -07001064 @Override
Daniel Lehmanncdef2b62010-06-06 18:25:49 -07001065 public void onDisplayNameClick(View view) {
1066 }
1067
Daniel Lehmannbe82afa2010-07-14 15:31:56 -07001068 @Override
Daniel Lehmanncdef2b62010-06-06 18:25:49 -07001069 public void onPhotoClick(View view) {
1070 }
1071 };
1072
1073
1074 public static interface Listener {
Daniel Lehmann18f104f2010-05-07 15:41:11 -07001075 /**
1076 * Contact was not found, so somehow close this fragment.
1077 */
Daniel Lehmanncdef2b62010-06-06 18:25:49 -07001078 public void onContactNotFound();
Daniel Lehmann18f104f2010-05-07 15:41:11 -07001079
1080 /**
1081 * User decided to go to Edit-Mode
1082 */
Daniel Lehmanncdef2b62010-06-06 18:25:49 -07001083 public void onEditRequested(Uri lookupUri);
Daniel Lehmann18f104f2010-05-07 15:41:11 -07001084
1085 /**
1086 * User clicked a single item (e.g. mail)
1087 */
Daniel Lehmanncdef2b62010-06-06 18:25:49 -07001088 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 Lehmann18f104f2010-05-07 15:41:11 -07001094 }
Daniel Lehmann4cd94412010-04-08 16:44:36 -07001095}