QuickContactActivity.java revision a3859ed9ff9f315d492e199620a08ae35ab8569f
1edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann/*
2edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Copyright (C) 2009 The Android Open Source Project
3edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann *
4edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Licensed under the Apache License, Version 2.0 (the "License");
5edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * you may not use this file except in compliance with the License.
6edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * You may obtain a copy of the License at
7edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann *
8edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann *      http://www.apache.org/licenses/LICENSE-2.0
9edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann *
10edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Unless required by applicable law or agreed to in writing, software
11edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * distributed under the License is distributed on an "AS IS" BASIS,
12edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * See the License for the specific language governing permissions and
14edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * limitations under the License.
15edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann */
16edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
17edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannpackage com.android.contacts.quickcontact;
18edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
198477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwellimport android.animation.Animator;
208477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwellimport android.animation.Animator.AnimatorListener;
218477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwellimport android.animation.AnimatorListenerAdapter;
228a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.animation.ArgbEvaluator;
23b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport android.animation.ObjectAnimator;
24edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.app.Activity;
25d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.app.Fragment;
26cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmannimport android.app.LoaderManager.LoaderCallbacks;
27eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.app.SearchManager;
28752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.content.ActivityNotFoundException;
29eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.content.ComponentName;
30edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.content.ContentUris;
31ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulosimport android.content.ContentValues;
32edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.content.Intent;
33cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmannimport android.content.Loader;
34dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulosimport android.content.pm.PackageManager;
35dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulosimport android.content.pm.ResolveInfo;
3631b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.Bitmap;
378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.graphics.Color;
38eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.graphics.PorterDuff;
39eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.graphics.PorterDuffColorFilter;
4031b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.drawable.BitmapDrawable;
418a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.graphics.drawable.ColorDrawable;
4231b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.graphics.drawable.Drawable;
43eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.net.ParseException;
44edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.net.Uri;
45eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.net.WebAddress;
4631b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.os.AsyncTask;
47edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.os.Bundle;
488a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport android.os.Trace;
49eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.CalendarContract;
50752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.provider.ContactsContract;
51edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Email;
52eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Event;
53eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.GroupMembership;
54eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Identity;
55eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Im;
56eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Nickname;
57eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Note;
58eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Organization;
59edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Phone;
60eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.CommonDataKinds.Relation;
61edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.SipAddress;
62edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
63edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.CommonDataKinds.Website;
64752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport android.provider.ContactsContract.Contacts;
6560e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulosimport android.provider.ContactsContract.Data;
66eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.provider.ContactsContract.DisplayNameSources;
67ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulosimport android.provider.ContactsContract.DataUsageFeedback;
68edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.QuickContact;
69edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.provider.ContactsContract.RawContacts;
7031b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport android.support.v7.graphics.Palette;
71edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.text.TextUtils;
72edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.util.Log;
73eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.util.Pair;
74d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.Menu;
75d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.view.MenuInflater;
76eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport android.view.MenuItem;
77edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View;
78edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.view.View.OnClickListener;
7910d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawaimport android.view.WindowManager;
80edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.widget.ImageView;
81edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport android.widget.Toast;
82d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport android.widget.Toolbar;
83edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
8472b3ab1d8a62f228a540b05f4ed6373e494c7d72Christine Chenimport com.android.contacts.ContactSaveService;
85d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.ContactsActivity;
862d150da246632b1649999cfabed776133b097775Brian Attwellimport com.android.contacts.NfcHandler;
87e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.R;
88eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.CallUtil;
89eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.Collapser;
90eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.ContactsUtils;
91d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.editor.SelectAccountDialogFragment;
9231b2d42fb0889e61515d27314aa5a245147100daBrian Attwellimport com.android.contacts.common.lettertiles.LetterTileDrawable;
93752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport com.android.contacts.common.list.ShortcutIntentBuilder;
94752cccf954bd6070c5fb623875314951c8e9849dBrian Attwellimport com.android.contacts.common.list.ShortcutIntentBuilder.OnShortcutIntentCreatedListener;
950d5588da244d0992c3ff8f25d0875fdf95a8c644Chiao Chengimport com.android.contacts.common.model.AccountTypeManager;
96cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.Contact;
97cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.ContactLoader;
98cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.RawContact;
99428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.account.AccountType;
100d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.common.model.account.AccountWithDataSet;
101cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.DataItem;
102428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.dataitem.DataKind;
103cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.EmailDataItem;
104eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.EventDataItem;
105cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.dataitem.ImDataItem;
106eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.NicknameDataItem;
107eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.NoteDataItem;
108eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.OrganizationDataItem;
109b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.common.model.dataitem.PhoneDataItem;
110eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.RelationDataItem;
111eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.SipAddressDataItem;
112eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.StructuredNameDataItem;
113eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.StructuredPostalDataItem;
114eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.model.dataitem.WebsiteDataItem;
115eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.common.util.DateUtils;
116faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils;
117faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwellimport com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette;
118d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellimport com.android.contacts.detail.ContactDetailDisplayUtils;
119899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport com.android.contacts.interactions.CalendarInteractionsLoader;
120ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulosimport com.android.contacts.interactions.CallLogInteractionsLoader;
1218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwellimport com.android.contacts.interactions.ContactDeletionInteraction;
122b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.ContactInteraction;
123b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport com.android.contacts.interactions.SmsInteractionsLoader;
1242d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport com.android.contacts.quickcontact.ExpandingEntryCardView.Entry;
125e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwellimport com.android.contacts.quickcontact.ExpandingEntryCardView.ExpandingEntryCardViewListener;
126e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.util.ImageViewDrawableSetter;
127eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.PhoneCapabilityTester;
128b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.util.SchedulingUtils;
129eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport com.android.contacts.util.StructuredPostalUtils;
130b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller;
131b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwellimport com.android.contacts.widget.MultiShrinkScroller.MultiShrinkScrollerListener;
132e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.google.common.base.Preconditions;
133e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.google.common.collect.Lists;
134e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Cheng
1352d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulosimport java.util.ArrayList;
136899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulosimport java.util.Arrays;
137eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Calendar;
138b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Collections;
139b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Comparator;
140eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulosimport java.util.Date;
141edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.HashMap;
142edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmannimport java.util.List;
143b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulosimport java.util.Map;
144edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
145edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann/**
146edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * Mostly translucent {@link Activity} that shows QuickContact dialog. It loads
147edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * data asynchronously, and then shows a popup with details centered around
148edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann * {@link Intent#getSourceBounds()}.
149edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann */
150d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwellpublic class QuickContactActivity extends ContactsActivity {
1518a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1528a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    /**
1538a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * QuickContacts immediately takes up the full screen. All possible information is shown.
1548a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * This value for {@link android.provider.ContactsContract.QuickContact#EXTRA_MODE}
1558a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     * should only be used by the Contacts app.
1568a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell     */
1578a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public static final int MODE_FULLY_EXPANDED = 4;
1588a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
159edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String TAG = "QuickContact";
160edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1619b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final String KEY_THEME_COLOR = "theme_color";
1629b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
1639b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final int ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION = 150;
1648a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private static final int REQUEST_CODE_CONTACT_EDITOR_ACTIVITY = 1;
1659b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private static final int SCRIM_COLOR = Color.argb(0xB2, 0, 0, 0);
166ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos    private static final String MIMETYPE_SMS = "vnd.android-dir/mms-sms";
167dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
168752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /** This is the Intent action to install a shortcut in the launcher. */
169752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private static final String ACTION_INSTALL_SHORTCUT =
170752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            "com.android.launcher.action.INSTALL_SHORTCUT";
171edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
172edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @SuppressWarnings("deprecation")
173edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final String LEGACY_AUTHORITY = android.provider.Contacts.AUTHORITY;
174edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
175e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_GPLUS_PROFILE =
176e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile";
177e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String INTENT_DATA_GPLUS_PROFILE_ADD_TO_CIRCLE = "Add to circle";
178e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String MIMETYPE_HANGOUTS =
179e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            "vnd.android.cursor.item/vnd.googleplus.profile.comm";
180e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos    private static final String INTENT_DATA_HANGOUTS_VIDEO = "Start video call";
181e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
182edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private Uri mLookupUri;
183edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private String[] mExcludeMimes;
1848a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mExtraMode;
1858a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int mStatusBarColor;
1868a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private boolean mHasAlreadyBeenOpened;
187edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1886219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee    private ImageView mPhotoView;
1890d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell    private View mTransparentView;
190eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mContactCard;
1916095369885edcca566a812b551886e29c7ff8039Brian Attwell    private ExpandingEntryCardView mNoContactDetailsCard;
192b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private ExpandingEntryCardView mRecentCard;
193eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private ExpandingEntryCardView mAboutCard;
194eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
195eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * This list contains all the {@link DataItem}s. Each nested list contains all data items of a
196eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * specific mimetype in sorted order, using mWithinMimeTypeDataItemComparator. The mimetype
197eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * lists are sorted using mAmongstMimeTypeDataItemComparator.
198eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
199eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private List<List<DataItem>> mDataItemsList;
200eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
201eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * A map between a mimetype string and the corresponding list of data items. The data items
202eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * are in sorted order using mWithinMimeTypeDataItemComparator.
203eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
204eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private Map<String, List<DataItem>> mDataItemsMap;
205b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private MultiShrinkScroller mScroller;
206d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private SelectAccountDialogFragmentListener mSelectAccountFragmentListener;
207eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private AsyncTask<Void, Void, Pair<List<List<DataItem>>, Map<String, List<DataItem>>>>
208eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mEntriesAndActionsTask;
2099b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private ColorDrawable mWindowScrim;
210a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell    private MaterialColorMapUtils mMaterialColorMapUtils;
2118477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    private boolean mIsWaitingForOtherPieceOfExitAnimation;
2128477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    private boolean mIsExitAnimationInProgress;
2138571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell    private boolean mHasComputedThemeColor;
214edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
2156cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee    private Contact mContactData;
2169815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann    private ContactLoader mContactLoader;
2179b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    private PorterDuffColorFilter mColorFilter;
2189815d7f98baf80ce51b0cf1f01f48a3dbb9a9db9Daniel Lehmann
2199758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus    private final ImageViewDrawableSetter mPhotoSetter = new ImageViewDrawableSetter();
2209758a92fac3e9f64892d893c992f6020d7fe3bfdJosh Gargus
221edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
222cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * {@link #LEADING_MIMETYPES} is used to sort MIME-types.
223edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     *
224edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * <p>The MIME-types in {@link #LEADING_MIMETYPES} appear in the front of the dialog,
225edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * in the order specified here.</p>
226edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
227edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private static final List<String> LEADING_MIMETYPES = Lists.newArrayList(
228cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos            Phone.CONTENT_ITEM_TYPE, SipAddress.CONTENT_ITEM_TYPE, Email.CONTENT_ITEM_TYPE,
229405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            StructuredPostal.CONTENT_ITEM_TYPE);
230edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
231eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private static final List<String> ABOUT_CARD_MIMETYPES = Lists.newArrayList(
232eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Event.CONTENT_ITEM_TYPE, GroupMembership.CONTENT_ITEM_TYPE, Identity.CONTENT_ITEM_TYPE,
233eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Im.CONTENT_ITEM_TYPE, Nickname.CONTENT_ITEM_TYPE, Note.CONTENT_ITEM_TYPE,
234eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Organization.CONTENT_ITEM_TYPE, Relation.CONTENT_ITEM_TYPE, Website.CONTENT_ITEM_TYPE);
235eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
236b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background contact loader */
237b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_CONTACT_ID = 0;
238b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
239ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_PHONES =
240ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_PHONES";
241ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
242b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    /** Id for the background Sms Loader */
243b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int LOADER_SMS_ID = 1;
244b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private static final int MAX_SMS_RETRIEVE = 3;
245ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
246ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the back Calendar Loader */
247899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int LOADER_CALENDAR_ID = 2;
248ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final String KEY_LOADER_EXTRA_EMAILS =
249ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos            QuickContactActivity.class.getCanonicalName() + ".KEY_LOADER_EXTRA_EMAILS";
250899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_PAST_CALENDAR_RETRIEVE = 3;
251899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final int MAX_FUTURE_CALENDAR_RETRIEVE = 3;
252899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
253899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            180L * 24L * 60L * 60L * 1000L /* 180 days */;
254899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos    private static final long FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR =
255899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            36L * 60L * 60L * 1000L /* 36 hours */;
256899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
257ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    /** Id for the background Call Log Loader */
258ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int LOADER_CALL_LOG_ID = 3;
259ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int MAX_CALL_LOG_RETRIEVE = 3;
2606095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_CONTACT_ENTRIES_SHOWN = 3;
2616095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN = 3;
2626095369885edcca566a812b551886e29c7ff8039Brian Attwell    private static final int CARD_ENTRY_ID_EDIT_CONTACT = -2;
263ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
264ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
265ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos    private static final int[] mRecentLoaderIds = new int[]{
266ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_SMS_ID,
267ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALENDAR_ID,
268ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        LOADER_CALL_LOG_ID};
269a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos    private Map<Integer, List<ContactInteraction>> mRecentLoaderResults = new HashMap<>();
270b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
271d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private static final String FRAGMENT_TAG_SELECT_ACCOUNT = "select_account_fragment";
2726cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee
2732d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    final OnClickListener mEntryClickHandler = new OnClickListener() {
2742d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        @Override
2752d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        public void onClick(View v) {
276ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Data Id is stored as the entry view id
277ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            final int dataId = v.getId();
2786095369885edcca566a812b551886e29c7ff8039Brian Attwell            if (dataId == CARD_ENTRY_ID_EDIT_CONTACT) {
2796095369885edcca566a812b551886e29c7ff8039Brian Attwell                editContact();
2806095369885edcca566a812b551886e29c7ff8039Brian Attwell                return;
2816095369885edcca566a812b551886e29c7ff8039Brian Attwell            }
2826095369885edcca566a812b551886e29c7ff8039Brian Attwell            final Object intentObject = v.getTag();
283ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            if (intentObject == null || !(intentObject instanceof Intent)) {
284ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                Log.w(TAG, "Intent tag was not used correctly");
2852d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos                return;
2862d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos            }
287ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            final Intent intent = (Intent) intentObject;
288ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
289ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Default to USAGE_TYPE_CALL. Usage is summed among all types for sorting each data id
290ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // so the exact usage type is not necessary in all cases
291ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            String usageType = DataUsageFeedback.USAGE_TYPE_CALL;
292ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
29332777801796c1b1793491d44161da41327e07f53Paul Soulos            final String scheme = intent.getData().getScheme();
294dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            if ((scheme != null && scheme.equals(CallUtil.SCHEME_SMSTO)) ||
295ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    (intent.getType() != null && intent.getType().equals(MIMETYPE_SMS))) {
296ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                usageType = DataUsageFeedback.USAGE_TYPE_SHORT_TEXT;
297ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
298ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
299ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            // Data IDs start at 1 so anything less is invalid
300ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            if (dataId > 0) {
301ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                final Uri uri = DataUsageFeedback.FEEDBACK_URI.buildUpon()
302ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendPath(String.valueOf(dataId))
303ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .appendQueryParameter(DataUsageFeedback.USAGE_TYPE, usageType)
304ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        .build();
305ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                final boolean successful = getContentResolver().update(
306ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                        uri, new ContentValues(), null, null) > 0;
307ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                if (!successful) {
308ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    Log.w(TAG, "DataUsageFeedback increment failed");
309ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                }
310ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            } else {
311ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                Log.w(TAG, "Invalid Data ID");
312ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            }
313ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
314ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            startActivity(intent);
3152d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos        }
3162d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos    };
3172d48b5ae6664a7ae0ed9941f55fae9dc327bd640Paul Soulos
318e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    final ExpandingEntryCardViewListener mExpandingEntryCardViewListener
319e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            = new ExpandingEntryCardViewListener() {
320e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        @Override
321e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        public void onCollapse(int heightDelta) {
322e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell            mScroller.prepareForShrinkingScrollChild(heightDelta);
323e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell        }
324e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell    };
325e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell
326d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    /**
327d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Headless fragment used to handle account selection callbacks invoked from
328d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * {@link DirectoryContactUtil}.
329d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
330d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public static class SelectAccountDialogFragmentListener extends Fragment
331d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            implements SelectAccountDialogFragment.Listener {
332d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
333d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        private QuickContactActivity mQuickContactActivity;
334d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
335d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public SelectAccountDialogFragmentListener() {}
336d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
337d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
338d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
339d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            DirectoryContactUtil.createCopy(mQuickContactActivity.mContactData.getContentValues(),
340d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    account, mQuickContactActivity);
341d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
342d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
343d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        @Override
344d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void onAccountSelectorCancelled() {}
345d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
346d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        /**
347d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * Set the parent activity. Since rotation can cause this fragment to be used across
348d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * more than one activity instance, we need to explicitly set this value instead
349d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         * of making this class non-static.
350d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell         */
351d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        public void setQuickContactActivity(QuickContactActivity quickContactActivity) {
352d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mQuickContactActivity = quickContactActivity;
353d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
354d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
355d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
356b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    final MultiShrinkScrollerListener mMultiShrinkScrollerListener
357b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            = new MultiShrinkScrollerListener() {
358b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        @Override
359b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        public void onScrolledOffBottom() {
3608477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsWaitingForOtherPieceOfExitAnimation) {
3618477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                finish();
3628477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                return;
3638477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
3648477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsWaitingForOtherPieceOfExitAnimation = false;
365b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
3668a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
3678a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
3688a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onEnterFullscreen() {
3698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
3708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
3718a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
3728a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        @Override
3738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        public void onExitFullscreen() {
3748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            updateStatusBarColor();
3758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
3768477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
3778477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
3788477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        public void onStartScrollOffBottom() {
3798477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            // Remove the window shim now that we are starting an Activity exit animation.
3808477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            final int duration = getResources().getInteger(android.R.integer.config_shortAnimTime);
3819b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            final ObjectAnimator animator = ObjectAnimator.ofInt(mWindowScrim, "alpha", 0xFF, 0);
3828477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            animator.addListener(mExitWindowShimAnimationListener);
3838477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            animator.setDuration(duration).start();
3848477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsWaitingForOtherPieceOfExitAnimation = true;
3858477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsExitAnimationInProgress = true;
3868477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
3878477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    };
3888477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell
3898477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell    final AnimatorListener mExitWindowShimAnimationListener = new AnimatorListenerAdapter() {
3908477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        @Override
3918477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        public void onAnimationEnd(Animator animation) {
3928477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsWaitingForOtherPieceOfExitAnimation) {
3938477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                finish();
3948477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                return;
3958477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
3968477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            mIsWaitingForOtherPieceOfExitAnimation = false;
3978477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell        }
398b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    };
399b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
400eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
401eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
402eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Data items are compared to the same mimetype based off of three qualities:
403eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 1. Super primary
404eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 2. Primary
405eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * 3. Times used
406eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
407eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<DataItem> mWithinMimeTypeDataItemComparator =
408eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<DataItem>() {
409eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
410eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(DataItem lhs, DataItem rhs) {
411eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!lhs.getMimeType().equals(rhs.getMimeType())) {
412eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Log.wtf(TAG, "Comparing DataItems with different mimetypes lhs.getMimeType(): " +
413eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getMimeType() + " rhs.getMimeType(): " + rhs.getMimeType());
414eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 0;
415eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
416eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
417eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lhs.isSuperPrimary()) {
418eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
419eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (rhs.isSuperPrimary()) {
420eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
421eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lhs.isPrimary() && !rhs.isPrimary()) {
422eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
423eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (!lhs.isPrimary() && rhs.isPrimary()) {
424eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
425eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
426eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int lhsTimesUsed =
427eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
428eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final int rhsTimesUsed =
429eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
430eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
431eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return rhsTimesUsed - lhsTimesUsed;
432eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
433eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
434eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
435eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
436cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos    /**
437cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * Sorts among different mimetypes based off:
438cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 1. Times used
439cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 2. Last time used
440cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     * 3. Statically defined
441cb4fcc7ef75c33411d2b900db7933c5bc4336527Paul Soulos     */
442eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final Comparator<List<DataItem>> mAmongstMimeTypeDataItemComparator =
443eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            new Comparator<List<DataItem>> () {
444eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        @Override
445eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        public int compare(List<DataItem> lhsList, List<DataItem> rhsList) {
446eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            DataItem lhs = lhsList.get(0);
447eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            DataItem rhs = rhsList.get(0);
448eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int lhsTimesUsed = lhs.getTimesUsed() == null ? 0 : lhs.getTimesUsed();
449eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int rhsTimesUsed = rhs.getTimesUsed() == null ? 0 : rhs.getTimesUsed();
450eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int timesUsedDifference = rhsTimesUsed - lhsTimesUsed;
451eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (timesUsedDifference != 0) {
452eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return timesUsedDifference;
453eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
454eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
455eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lhsLastTimeUsed =
456eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    lhs.getLastTimeUsed() == null ? 0 : lhs.getLastTimeUsed();
457eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long rhsLastTimeUsed =
458eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    rhs.getLastTimeUsed() == null ? 0 : rhs.getLastTimeUsed();
459eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final long lastTimeUsedDifference = rhsLastTimeUsed - lhsLastTimeUsed;
460eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (lastTimeUsedDifference > 0) {
461eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return 1;
462eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else if (lastTimeUsedDifference < 0) {
463eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return -1;
464eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
465eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
466eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Times used and last time used are the same. Resort to statically defined.
467eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String lhsMimeType = lhs.getMimeType();
468eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String rhsMimeType = rhs.getMimeType();
469eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (String mimeType : LEADING_MIMETYPES) {
470eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (lhsMimeType.equals(mimeType)) {
471eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    return -1;
472eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                } else if (rhsMimeType.equals(mimeType)) {
473eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    return 1;
474eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
475eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
476eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return 0;
477eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
478eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    };
479eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
480edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    @Override
4818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onCreate(Bundle savedInstanceState) {
4828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("onCreate()");
4838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onCreate(savedInstanceState);
484edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
4858a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getWindow().setStatusBarColor(Color.TRANSPARENT);
4862426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann
487d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(getIntent());
488dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
48910d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        // Show QuickContact in front of soft input
49010d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa        getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
49110d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
49210d7df7bab8c332744fad64b9167c2ef061070e4Daisuke Miyakawa
493edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        setContentView(R.layout.quickcontact_activity);
494edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
495a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell        mMaterialColorMapUtils = new MaterialColorMapUtils(getResources());
496a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell
497eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard = (ExpandingEntryCardView) findViewById(R.id.communication_card);
4986095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard = (ExpandingEntryCardView) findViewById(R.id.no_contact_data_card);
499b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard = (ExpandingEntryCardView) findViewById(R.id.recent_card);
500eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard = (ExpandingEntryCardView) findViewById(R.id.about_card);
501b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        mScroller = (MultiShrinkScroller) findViewById(R.id.multiscroller);
502edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
5036095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setOnClickListener(mEntryClickHandler);
504eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setOnClickListener(mEntryClickHandler);
505eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mContactCard.setExpandButtonText(
50623889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos        getResources().getString(R.string.expanding_entry_card_view_see_all));
507b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
508b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setOnClickListener(mEntryClickHandler);
509b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        mRecentCard.setTitle(getResources().getString(R.string.recent_card_title));
510edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
511eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard.setOnClickListener(mEntryClickHandler);
512eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
513d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mPhotoView = (ImageView) findViewById(R.id.photo);
5140d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        mTransparentView = findViewById(R.id.transparent_view);
5150d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        if (mScroller != null) {
5160d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell            mTransparentView.setOnClickListener(new OnClickListener() {
5170d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                @Override
5180d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                public void onClick(View v) {
5190d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                    mScroller.scrollOffBottom();
5200d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell                }
5210d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell            });
5220d90afea92dc37245d5c2cad4d6b8040f2faed5cBrian Attwell        }
523edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
524d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
525d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        setActionBar(toolbar);
5269b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getActionBar().setTitle(null);
5279b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // Put a TextView with a known resource id into the ActionBar. This allows us to easily
5289b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // find the correct TextView location & size later.
5299b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        toolbar.addView(getLayoutInflater().inflate(R.layout.quickcontact_title_placeholder, null));
5306219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee
5318a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = savedInstanceState != null;
5328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
5339b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mWindowScrim = new ColorDrawable(SCRIM_COLOR);
5349b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        getWindow().setBackgroundDrawable(mWindowScrim);
5358a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (!mHasAlreadyBeenOpened) {
5368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            final int duration = getResources().getInteger(android.R.integer.config_shortAnimTime);
5379b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            ObjectAnimator.ofInt(mWindowScrim, "alpha", 0, 0xFF).setDuration(duration).start();
5388a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
539edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
540c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.initialize(mMultiShrinkScrollerListener, mExtraMode == MODE_FULLY_EXPANDED);
541ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // mScroller needs to perform asynchronous measurements after initalize(), therefore
542ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        // we can't mark this as GONE.
543ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell        mScroller.setVisibility(View.INVISIBLE);
5448a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
5459b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        setHeaderNameText(R.string.missing_name);
5469b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
547d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener= (SelectAccountDialogFragmentListener) getFragmentManager()
548d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                .findFragmentByTag(FRAGMENT_TAG_SELECT_ACCOUNT);
549d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mSelectAccountFragmentListener == null) {
550d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener = new SelectAccountDialogFragmentListener();
551d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            getFragmentManager().beginTransaction().add(0, mSelectAccountFragmentListener,
552d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    FRAGMENT_TAG_SELECT_ACCOUNT).commit();
553d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mSelectAccountFragmentListener.setRetainInstance(true);
554d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
555d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mSelectAccountFragmentListener.setQuickContactActivity(this);
55623889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
5579b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (savedInstanceState != null) {
5589b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            final int color = savedInstanceState.getInt(KEY_THEME_COLOR, 0);
559ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
560ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    new Runnable() {
561ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        @Override
562ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        public void run() {
563ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for the pre draw before setting the initial scroll
564ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // value. Prior to pre draw all scroll values are invalid.
565ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (mHasAlreadyBeenOpened) {
566ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setVisibility(View.VISIBLE);
567ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                                mScroller.setScroll(mScroller.getScrollNeededToBeFullScreen());
568ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            }
569ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // Need to wait for pre draw for setting the theme color. Setting the
570ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // header tint before the MultiShrinkScroller has been measured will
571ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            // cause incorrect tinting calculations.
572ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                            if (color != 0) {
573a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                setThemeColor(mMaterialColorMapUtils
574a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                                        .calculatePrimaryAndSecondaryColor(color));
5759b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell                            }
576ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                        }
577ac3ed8e48cc474448aa5a7cfed5d6b95073b84f9Brian Attwell                    });
5789b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
5799b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
5808a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
5818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
5828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
583405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    @Override
584405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
5858a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (requestCode == REQUEST_CODE_CONTACT_EDITOR_ACTIVITY &&
5868a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                resultCode == ContactDeletionInteraction.RESULT_CODE_DELETED) {
5878a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // The contact that we were showing has been deleted.
5888a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            finish();
589b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
5908a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
591dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
5928a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
5938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onNewIntent(Intent intent) {
5948a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onNewIntent(intent);
5958a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
5968571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell        mHasComputedThemeColor = false;
597d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        processIntent(intent);
598d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
599d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
6009b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    @Override
6019b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    public void onSaveInstanceState(Bundle savedInstanceState) {
6029b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        super.onSaveInstanceState(savedInstanceState);
6039b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mColorFilter != null) {
6049b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            savedInstanceState.putInt(KEY_THEME_COLOR, mColorFilter.getColor());
6059b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
6069b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
6079b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
608d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void processIntent(Intent intent) {
609d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        Uri lookupUri = intent.getData();
610d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
611d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Check to see whether it comes from the old version.
612d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (lookupUri != null && LEGACY_AUTHORITY.equals(lookupUri.getAuthority())) {
613d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final long rawContactId = ContentUris.parseId(lookupUri);
614d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            lookupUri = RawContacts.getContactLookupUri(getContentResolver(),
615d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId));
616d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
617d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExtraMode = getIntent().getIntExtra(QuickContact.EXTRA_MODE,
618d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                QuickContact.MODE_LARGE);
619d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Uri oldLookupUri = mLookupUri;
620d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
621d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mLookupUri = Preconditions.checkNotNull(lookupUri, "missing lookupUri");
622d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mExcludeMimes = intent.getStringArrayExtra(QuickContact.EXTRA_EXCLUDE_MIMES);
623d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (oldLookupUri == null) {
624d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            mContactLoader = (ContactLoader) getLoaderManager().initLoader(
625d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
626d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        } else if (oldLookupUri != mLookupUri) {
627d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // After copying a directory contact, the contact URI changes. Therefore,
628d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // we need to restart the loader and reload the new contact.
629899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            for (int interactionLoaderId : mRecentLoaderIds) {
630899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                getLoaderManager().destroyLoader(interactionLoaderId);
631899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos            }
632405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactLoader = (ContactLoader) getLoaderManager().restartLoader(
633405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos                    LOADER_CONTACT_ID, null, mLoaderContactCallbacks);
634d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
6352d150da246632b1649999cfabed776133b097775Brian Attwell
6362d150da246632b1649999cfabed776133b097775Brian Attwell        NfcHandler.register(this, mLookupUri);
6372426cb015ba4cb679a525bd1ffe223527375468cDaniel Lehmann    }
638edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
639b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    private void runEntranceAnimation() {
6408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mHasAlreadyBeenOpened) {
6418a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
6428a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
6438a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mHasAlreadyBeenOpened = true;
644c33ae17c9816b770041ee1f9ccde10e7c7eee491Brian Attwell        mScroller.scrollUpForEntranceAnimation(mExtraMode != MODE_FULLY_EXPANDED);
645b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
646b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell
64781281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
648d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void setHeaderNameText(int resId) {
6499b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        if (mScroller != null) {
650f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell            mScroller.setTitle(getText(resId) == null ? null : getText(resId).toString());
6519b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        }
65281281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
65381281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
65481281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /** Assign this string to the view if it is not empty. */
655f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell    private void setHeaderNameText(String value) {
656d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (!TextUtils.isEmpty(value)) {
6579b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            if (mScroller != null) {
658f43f573340fd9de5d30b43d7c96cac1ec9021e58Brian Attwell                mScroller.setTitle(value);
6599b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell            }
66081281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan        }
66181281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    }
66281281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan
66381281eefd5aaf339cbfa080ddfa6c6f63caca71aKatherine Kuan    /**
664edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * Check if the given MIME-type appears in the list of excluded MIME-types
665edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     * that the most-recent caller requested.
666edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
667edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    private boolean isMimeExcluded(String mimeType) {
668edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        if (mExcludeMimes == null) return false;
669edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        for (String excludedMime : mExcludeMimes) {
670edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            if (TextUtils.equals(excludedMime, mimeType)) {
671edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                return true;
672edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
673edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
674edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        return false;
675edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
676edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
677edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
678cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann     * Handle the result from the ContactLoader
679edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann     */
6808a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void bindContactData(final Contact data) {
6818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("bindContactData");
6826cb6f6f8304d4b42fc9db4bbb0cc4058ab53afe3Yorke Lee        mContactData = data;
683d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        invalidateOptionsMenu();
684edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
6858a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
6868a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.beginSection("Set display photo & name");
687dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
6886219668d0612f7522bd15a9d34c6232cb15b9a6cYorke Lee        mPhotoSetter.setupContactPhoto(data, mPhotoView);
68931b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        extractAndApplyTintFromPhotoViewAsynchronously();
6904936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        analyzeWhitenessOfPhotoAsynchronously();
691d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        setHeaderNameText(data.getDisplayName());
692edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
6938a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
694dfe8cc82e8aa31929bb85eb68b7de3492b875d57Makoto Onuki
695eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mEntriesAndActionsTask = new AsyncTask<Void, Void,
696eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Pair<List<List<DataItem>>, Map<String, List<DataItem>>>>() {
6978a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
6988a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
699eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            protected Pair<List<List<DataItem>>, Map<String, List<DataItem>>> doInBackground(
700eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    Void... params) {
701eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                return generateDataModelFromContact(data);
7028a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
7038a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            @Override
705eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            protected void onPostExecute(Pair<List<List<DataItem>>,
706eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    Map<String, List<DataItem>>> dataItemsPair) {
707eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                super.onPostExecute(dataItemsPair);
708eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mDataItemsList = dataItemsPair.first;
709eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mDataItemsMap = dataItemsPair.second;
7108a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // Check that original AsyncTask parameters are still valid and the activity
7118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // is still running before binding to UI. A new intent could invalidate
7128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                // the results, for example.
7138a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (data == mContactData && !isCancelled()) {
714eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    bindDataToCards();
7158a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    showActivity();
7168a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
7178a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            }
7188a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        };
7198a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        mEntriesAndActionsTask.execute();
7208a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
7218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
722eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private void bindDataToCards() {
723eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        startInteractionLoaders();
724eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        populateContactAndAboutCard();
725eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
726eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
727eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private void startInteractionLoaders() {
728eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<DataItem> phoneDataItems = mDataItemsMap.get(Phone.CONTENT_ITEM_TYPE);
729eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] phoneNumbers = null;
730eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (phoneDataItems != null) {
731eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            phoneNumbers = new String[phoneDataItems.size()];
732eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < phoneDataItems.size(); ++i) {
733eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                phoneNumbers[i] = ((PhoneDataItem) phoneDataItems.get(i)).getNumber();
734eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
735eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
736ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle phonesExtraBundle = new Bundle();
737eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        phonesExtraBundle.putStringArray(KEY_LOADER_EXTRA_PHONES, phoneNumbers);
738eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
739eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("start sms loader");
7408a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        getLoaderManager().initLoader(
7418a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                LOADER_SMS_ID,
742ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
743ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                mLoaderInteractionsCallbacks);
744ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.endSection();
745ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos
746ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        Trace.beginSection("start call log loader");
747ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        getLoaderManager().initLoader(
748ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                LOADER_CALL_LOG_ID,
749ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                phonesExtraBundle,
7508a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                mLoaderInteractionsCallbacks);
751899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.endSection();
7528a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
753eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
754899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        Trace.beginSection("start calendar loader");
755eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<DataItem> emailDataItems = mDataItemsMap.get(Email.CONTENT_ITEM_TYPE);
756eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String[] emailAddresses = null;
757eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (emailDataItems != null) {
758eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            emailAddresses = new String[emailDataItems.size()];
759eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            for (int i = 0; i < emailDataItems.size(); ++i) {
760eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                emailAddresses[i] = ((EmailDataItem) emailDataItems.get(i)).getAddress();
761eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
762eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
763ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos        final Bundle emailsExtraBundle = new Bundle();
764eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        emailsExtraBundle.putStringArray(KEY_LOADER_EXTRA_EMAILS, emailAddresses);
765899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos        getLoaderManager().initLoader(
766899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                LOADER_CALENDAR_ID,
767ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                emailsExtraBundle,
768899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                mLoaderInteractionsCallbacks);
7698a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
7708a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
7718a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
7728a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void showActivity() {
7738a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller != null) {
7748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mScroller.setVisibility(View.VISIBLE);
7758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            SchedulingUtils.doOnPreDraw(mScroller, /* drawNextFrame = */ false,
7768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    new Runnable() {
7778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        @Override
7788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        public void run() {
7798a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            runEntranceAnimation();
7808a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                        }
7818a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    });
7828a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
7838a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
7848a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
785eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private void populateContactAndAboutCard() {
786eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind contact card");
787eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
78860e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos        final List<List<Entry>> contactCardEntries = new ArrayList<>();
78960e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos        final List<List<Entry>> aboutCardEntries = new ArrayList<>();
790eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
791eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (int i = 0; i < mDataItemsList.size(); ++i) {
792eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final List<DataItem> dataItemsByMimeType = mDataItemsList.get(i);
793eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final DataItem topDataItem = dataItemsByMimeType.get(0);
794eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (ABOUT_CARD_MIMETYPES.contains(topDataItem.getMimeType())) {
79560e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                List<Entry> aboutEntries = dataItemsToEntries(mDataItemsList.get(i));
79660e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                if (aboutEntries.size() > 0) {
79760e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                    aboutCardEntries.add(aboutEntries);
798eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
79960e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            } else {
80060e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                List<Entry> contactEntries = dataItemsToEntries(mDataItemsList.get(i));
80160e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                if (contactEntries.size() > 0) {
80260e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos                    contactCardEntries.add(contactEntries);
803eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
804eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
805eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
806eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
807eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (contactCardEntries.size() > 0) {
808eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.initialize(contactCardEntries,
809eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_CONTACT_ENTRIES_SHOWN,
810eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    /* isExpanded = */ false,
811eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    mExpandingEntryCardViewListener);
812eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.VISIBLE);
813eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
814eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mContactCard.setVisibility(View.GONE);
815eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
816eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
8178a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
818eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("bind about card");
819eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        mAboutCard.initialize(aboutCardEntries,
820eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                /* numInitialVisibleEntries = */ 1,
821eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                /* isExpanded = */ true,
822eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mExpandingEntryCardViewListener);
8236095369885edcca566a812b551886e29c7ff8039Brian Attwell
8246095369885edcca566a812b551886e29c7ff8039Brian Attwell        if (contactCardEntries.size() == 0 && aboutCardEntries.size() == 0) {
8256095369885edcca566a812b551886e29c7ff8039Brian Attwell            initializeNoContactDetailCard();
8266095369885edcca566a812b551886e29c7ff8039Brian Attwell        } else {
8276095369885edcca566a812b551886e29c7ff8039Brian Attwell            mNoContactDetailsCard.setVisibility(View.GONE);
8286095369885edcca566a812b551886e29c7ff8039Brian Attwell        }
8296095369885edcca566a812b551886e29c7ff8039Brian Attwell
830a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // If the Recent card is already initialized (all recent data is loaded), show the About
831a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        // card if it has entries. Otherwise About card visibility will be set in bindRecentData()
832a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        if (isAllRecentDataLoaded() && aboutCardEntries.size() > 0) {
833a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos            mAboutCard.setVisibility(View.VISIBLE);
834a8fce966f48d84af82f5ad767aa7986740ca7b26Paul Soulos        }
835eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
836eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
837eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
838eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
8396095369885edcca566a812b551886e29c7ff8039Brian Attwell     * Create a card that shows "Add email" and "Add phone number" entries in grey.
8406095369885edcca566a812b551886e29c7ff8039Brian Attwell     */
8416095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void initializeNoContactDetailCard() {
8426095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable phoneIcon = getResources().getDrawable(
8436095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_phone_24dp).mutate();
8446095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry phonePromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
8456095369885edcca566a812b551886e29c7ff8039Brian Attwell                phoneIcon, getString(R.string.quickcontact_add_phone_number),
846dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* subHeader = */ null, /* text = */ null, getEditContactIntent(),
847dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIcon = */ null, /* alternateIntent = */ null,
848dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateContentDescription = */ null, /* shouldApplyColor = */ false,
849dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* isEditable = */ false);
8506095369885edcca566a812b551886e29c7ff8039Brian Attwell
8516095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Drawable emailIcon = getResources().getDrawable(
8526095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.drawable.ic_email_24dp).mutate();
8536095369885edcca566a812b551886e29c7ff8039Brian Attwell        final Entry emailPromptEntry = new Entry(CARD_ENTRY_ID_EDIT_CONTACT,
8546095369885edcca566a812b551886e29c7ff8039Brian Attwell                emailIcon, getString(R.string.quickcontact_add_email), /* subHeader = */ null,
855dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* text = */ null, getEditContactIntent(), /* alternateIcon = */ null,
856dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* alternateIntent = */ null, /* alternateContentDescription = */ null,
857dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                /* shouldApplyColor = */ false, /* isEditable = */ false);
8586095369885edcca566a812b551886e29c7ff8039Brian Attwell
8596095369885edcca566a812b551886e29c7ff8039Brian Attwell        final List<List<Entry>> promptEntries = new ArrayList<>();
8606095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
8616095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.add(new ArrayList<Entry>(1));
8626095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(0).add(phonePromptEntry);
8636095369885edcca566a812b551886e29c7ff8039Brian Attwell        promptEntries.get(1).add(emailPromptEntry);
8646095369885edcca566a812b551886e29c7ff8039Brian Attwell
8656095369885edcca566a812b551886e29c7ff8039Brian Attwell        final int subHeaderTextColor = getResources().getColor(
8666095369885edcca566a812b551886e29c7ff8039Brian Attwell                R.color.quickcontact_entry_sub_header_text_color);
8676095369885edcca566a812b551886e29c7ff8039Brian Attwell        final PorterDuffColorFilter greyColorFilter =
8686095369885edcca566a812b551886e29c7ff8039Brian Attwell                new PorterDuffColorFilter(subHeaderTextColor, PorterDuff.Mode.SRC_ATOP);
8696095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.initialize(promptEntries, 2, /* isExpanded = */ false,
8706095369885edcca566a812b551886e29c7ff8039Brian Attwell                mExpandingEntryCardViewListener);
8716095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setVisibility(View.VISIBLE);
8726095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setEntryHeaderColor(subHeaderTextColor);
8736095369885edcca566a812b551886e29c7ff8039Brian Attwell        mNoContactDetailsCard.setColorAndFilter(subHeaderTextColor, greyColorFilter);
8746095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
8756095369885edcca566a812b551886e29c7ff8039Brian Attwell
8766095369885edcca566a812b551886e29c7ff8039Brian Attwell    /**
877eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Builds the {@link DataItem}s Map out of the Contact.
878eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param data The contact to build the data from.
879eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return A pair containing a list of data items sorted within mimetype and sorted
880eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  amongst mimetype. The map goes from mimetype string to the sorted list of data items within
881eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     *  mimetype
882eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
883eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private Pair<List<List<DataItem>>, Map<String, List<DataItem>>> generateDataModelFromContact(
884eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Contact data) {
885eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("Build data items map");
886eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
887eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final Map<String, List<DataItem>> dataItemsMap = new HashMap<>();
8888bf96e78497ea9c8c893bcb357fc1e3175fb2e9bBrian Attwell
8898a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final ResolveCache cache = ResolveCache.getInstance(this);
890851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        for (RawContact rawContact : data.getRawContacts()) {
891851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            for (DataItem dataItem : rawContact.getDataItems()) {
892eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setRawContactId(rawContact.getId());
893eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
894851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu                final String mimeType = dataItem.getMimeType();
895eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (mimeType == null) continue;
896eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
89747b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final AccountType accountType = rawContact.getAccountType(this);
89847b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                final DataKind dataKind = AccountTypeManager.getInstance(this)
89947b6f70eadb118d815b4aaf5426c070bd75a38fbChiao Cheng                        .getKindOrFallback(accountType, mimeType);
900eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataKind == null) continue;
901cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
902eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItem.setDataKind(dataKind);
903b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
904eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final boolean hasData = !TextUtils.isEmpty(dataItem.buildDataString(this,
905eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        dataKind));
906899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos
907eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (isMimeExcluded(mimeType) || !hasData) continue;
908edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
909eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                List<DataItem> dataItemListByType = dataItemsMap.get(mimeType);
910eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (dataItemListByType == null) {
911eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemListByType = new ArrayList<>();
912eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    dataItemsMap.put(mimeType, dataItemListByType);
913edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann                }
914eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                dataItemListByType.add(dataItem);
915edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
916edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
9178a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        Trace.endSection();
918edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
919eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort within mimetypes");
92016339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos        /*
92116339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         * Sorting is a multi part step. The end result is to a have a sorted list of the most
922eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * used data items, one per mimetype. Then, within each mimetype, the list of data items
923eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * for that type is also sorted, based off of {super primary, primary, times used} in that
924eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos         * order.
92516339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos         */
926eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<List<DataItem>> dataItemsList = new ArrayList<>();
927eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (List<DataItem> mimeTypeDataItems : dataItemsMap.values()) {
928eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Remove duplicate data items
929eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collapser.collapseList(mimeTypeDataItems, this);
930eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Sort within mimetype
931eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            Collections.sort(mimeTypeDataItems, mWithinMimeTypeDataItemComparator);
932eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Add to the list of data item lists
933eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            dataItemsList.add(mimeTypeDataItems);
934edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann        }
935eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
936edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
937eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.beginSection("sort amongst mimetypes");
938eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // Sort amongst mimetypes to bubble up the top data items for the contact card
939eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Collections.sort(dataItemsList, mAmongstMimeTypeDataItemComparator);
940eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Trace.endSection();
94116339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos
942eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        return new Pair<>(dataItemsList, dataItemsMap);
943eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
944edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
945eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    /**
946eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * Converts a {@link DataItem} into an {@link ExpandingEntryCardView.Entry} for display.
947eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * If the {@link ExpandingEntryCardView.Entry} has no visual elements, null is returned.
948eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @param dataItem The {@link DataItem} to convert.
949eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     * @return The {@link ExpandingEntryCardView.Entry}, or null if no visual elements are present.
950eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos     */
951eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private Entry dataItemToEntry(DataItem dataItem) {
952eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable icon = null;
953eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String header = null;
954eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String subHeader = null;
955eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable subHeaderIcon = null;
956eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        String text = null;
957eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Drawable textIcon = null;
958eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        Intent intent = null;
95948ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos        boolean shouldApplyColor = true;
960dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Drawable alternateIcon = null;
961dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        Intent alternateIntent = null;
962dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        String alternateContentDescription = null;
963eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final boolean isEditable = false;
964eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
965eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        DataKind kind = dataItem.getDataKind();
966eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
967eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (dataItem instanceof ImDataItem) {
968eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final ImDataItem im = (ImDataItem) dataItem;
969eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            intent = ContactsUtils.buildImIntent(this, im).first;
970eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_im_entry);
971eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isEmail = im.isCreatedFromEmail();
972eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final int protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : im.getProtocol();
973eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = Im.getProtocolLabel(getResources(), protocol,
974eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    im.getCustomProtocol()).toString();
975eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof OrganizationDataItem) {
976eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final OrganizationDataItem organization = (OrganizationDataItem) dataItem;
977eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_organization_entry);
978eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = organization.getCompany();
979eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = organization.getTitle();
980eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NicknameDataItem) {
981eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NicknameDataItem nickname = (NicknameDataItem) dataItem;
982eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Build nickname entries
983eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean isNameRawContact =
984eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                (mContactData.getNameRawContactId() == dataItem.getRawContactId());
985eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
986eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final boolean duplicatesTitle =
987eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                isNameRawContact
988eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                && mContactData.getDisplayNameSource() == DisplayNameSources.NICKNAME;
989eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
990eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!duplicatesTitle) {
991eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = getResources().getString(R.string.header_nickname_entry);
992eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeader = nickname.getName();
993eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
994eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof NoteDataItem) {
995eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final NoteDataItem note = (NoteDataItem) dataItem;
996eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_note_entry);
997eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = note.getNote();
998eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof WebsiteDataItem) {
999eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final WebsiteDataItem website = (WebsiteDataItem) dataItem;
1000eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_website_entry);
1001eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = website.getUrl();
1002eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            try {
1003eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final WebAddress webAddress = new WebAddress(website.buildDataString(this, kind));
1004eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webAddress.toString()));
1005eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } catch (final ParseException e) {
1006eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                Log.e(TAG, "Couldn't parse website: " + website.buildDataString(this, kind));
1007eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1008eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EventDataItem) {
1009eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EventDataItem event = (EventDataItem) dataItem;
1010eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String dataString = event.buildDataString(this, kind);
1011eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Calendar cal = DateUtils.parseDate(dataString, false);
1012eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (cal != null) {
1013eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Date nextAnniversary =
1014eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        DateUtils.getNextAnnualDate(cal);
1015eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
1016eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                builder.appendPath("time");
1017eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                ContentUris.appendId(builder, nextAnniversary.getTime());
1018eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
1019eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1020eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_event_entry);
1021f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (event.hasKindTypeColumn(kind)) {
1022f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                subHeader = getResources().getString(Event.getTypeResource(
1023f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                        event.getKindTypeColumn(kind)));
1024f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
1025eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = DateUtils.formatDate(this, dataString);
1026eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof RelationDataItem) {
1027eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final RelationDataItem relation = (RelationDataItem) dataItem;
1028eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String dataString = relation.buildDataString(this, kind);
1029eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(dataString)) {
1030eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SEARCH);
1031eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.putExtra(SearchManager.QUERY, dataString);
1032eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent.setType(Contacts.CONTENT_TYPE);
1033eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1034eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = getResources().getString(R.string.header_relation_entry);
1035eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            subHeader = relation.getName();
1036f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            if (relation.hasKindTypeColumn(kind)) {
1037f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                text = Relation.getTypeLabel(getResources(), relation.getKindTypeColumn(kind),
1038f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                        relation.getLabel()).toString();
1039f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos            }
1040eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof PhoneDataItem) {
1041eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final PhoneDataItem phone = (PhoneDataItem) dataItem;
1042eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(phone.getNumber())) {
1043eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = phone.buildDataString(this, kind);
1044f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (phone.hasKindTypeColumn(kind)) {
1045f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                    text = Phone.getTypeLabel(getResources(), phone.getKindTypeColumn(kind),
1046f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            phone.getLabel()).toString();
1047f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
1048eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                icon = getResources().getDrawable(R.drawable.ic_phone_24dp);
1049eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (PhoneCapabilityTester.isPhone(this)) {
1050eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(phone.getNumber());
105116339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1052dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = new Intent(Intent.ACTION_SENDTO,
1053dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                        Uri.fromParts(CallUtil.SCHEME_SMSTO, phone.getNumber(), null));
1054dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIcon = getResources().getDrawable(R.drawable.ic_message_24dp);
1055dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateContentDescription = getResources().getString(R.string.sms_other);
1056eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1057eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof EmailDataItem) {
1058eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final EmailDataItem email = (EmailDataItem) dataItem;
1059eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String address = email.getData();
1060eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(address)) {
1061eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final Uri mailUri = Uri.fromParts(CallUtil.SCHEME_MAILTO, address, null);
1062eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = new Intent(Intent.ACTION_SENDTO, mailUri);
1063eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = email.getAddress();
1064f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (email.hasKindTypeColumn(kind)) {
1065f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                    text = Email.getTypeLabel(getResources(), email.getKindTypeColumn(kind),
1066f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            email.getLabel()).toString();
1067f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
1068eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                icon = getResources().getDrawable(R.drawable.ic_email_24dp);
1069eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1070eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredPostalDataItem) {
1071eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            StructuredPostalDataItem postal = (StructuredPostalDataItem) dataItem;
1072eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String postalAddress = postal.getFormattedAddress();
1073eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(postalAddress)) {
1074eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = StructuredPostalUtils.getViewPostalAddressIntent(postalAddress);
1075eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                header = postal.getFormattedAddress();
1076f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                if (postal.hasKindTypeColumn(kind)) {
1077f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                    text = StructuredPostal.getTypeLabel(getResources(),
1078f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                            postal.getKindTypeColumn(kind), postal.getLabel()).toString();
1079f9d5c0e24d1b11527475227549f9530127e9dc07Paul Soulos                }
1080eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                icon = getResources().getDrawable(R.drawable.ic_place_24dp);
1081eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1082eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof SipAddressDataItem) {
1083eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (PhoneCapabilityTester.isSipPhone(this)) {
1084eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final SipAddressDataItem sip = (SipAddressDataItem) dataItem;
1085eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                final String address = sip.getSipAddress();
1086eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (!TextUtils.isEmpty(address)) {
1087eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    final Uri callUri = Uri.fromParts(CallUtil.SCHEME_SIP, address, null);
1088eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    intent = CallUtil.getCallIntent(callUri);
1089eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // Note that this item will get a SIP-specific variant
1090eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // of the "call phone" icon, rather than the standard
1091eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // app icon for the Phone app (which we show for
1092eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // regular phone numbers.)  That's because the phone
1093eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // app explicitly specifies an android:icon attribute
1094eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    // for the SIP-related intent-filters in its manifest.
1095eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                }
1096eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                icon = ResolveCache.getInstance(this).getIcon(sip.getMimeType(), intent);
1097eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                // Call mutate to create a new Drawable.ConstantState for color filtering
1098eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                if (icon != null) {
1099eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                    icon.mutate();
110016339336653fa028a2e02e1eee92851fe5084d1cPaul Soulos                }
1101edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann            }
1102eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else if (dataItem instanceof StructuredNameDataItem) {
1103eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final String givenName = ((StructuredNameDataItem) dataItem).getGivenName();
1104eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!TextUtils.isEmpty(givenName)) {
1105eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mAboutCard.setTitle(getResources().getString(R.string.about_card_title) +
1106eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                        " " + givenName);
1107eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            } else {
1108eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                mAboutCard.setTitle(getResources().getString(R.string.about_card_title));
1109eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1110eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1111eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Custom DataItem
1112eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            header = dataItem.buildDataStringForDisplay(this, kind);
1113eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            text = kind.typeColumn;
1114eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            intent = new Intent(Intent.ACTION_VIEW);
111560e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, dataItem.getId());
111660e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            intent.setDataAndType(uri, dataItem.getMimeType());
1117e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
1118e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            if (intent != null) {
1119e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                final String mimetype = intent.getType();
1120e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos
1121e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                // Attempt to use known icons for known 3p types. Otherwise default to ResolveCache
1122e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                switch (mimetype) {
1123e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_GPLUS_PROFILE:
1124e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (INTENT_DATA_GPLUS_PROFILE_ADD_TO_CIRCLE.equals(
1125e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                intent.getDataString())) {
1126e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(
1127e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                    R.drawable.ic_add_to_circles_black_24);
1128e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        } else {
1129e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(R.drawable.ic_google_plus_24dp);
1130e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
1131e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
1132e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    case MIMETYPE_HANGOUTS:
1133e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (INTENT_DATA_HANGOUTS_VIDEO.equals(intent.getDataString())) {
1134e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(R.drawable.ic_hangout_video_24dp);
1135e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        } else {
1136e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon = getResources().getDrawable(R.drawable.ic_hangout_24dp);
1137e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
1138e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        break;
1139e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                    default:
1140e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        icon = ResolveCache.getInstance(this).getIcon(
1141e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                                dataItem.getMimeType(), intent);
1142e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        // Call mutate to create a new Drawable.ConstantState for color filtering
1143e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        if (icon != null) {
1144e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                            icon.mutate();
1145e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                        }
114648ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                        shouldApplyColor = false;
1147e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos                }
1148e005566a1616d6797fb7e204d0d41bd341b37543Paul Soulos            }
1149eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1150b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1151eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (intent != null) {
1152eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            // Do not set the intent is there are no resolves
1153eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (!PhoneCapabilityTester.isIntentRegistered(this, intent)) {
1154eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                intent = null;
1155eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1156eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1157eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1158dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (alternateIntent != null) {
1159dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            // Do not set the alternate intent is there are no resolves
1160dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            if (!PhoneCapabilityTester.isIntentRegistered(this, alternateIntent)) {
1161dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIntent = null;
1162dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            }
1163dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1164dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            // Attempt to use package manager to find a suitable content description if needed
1165dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            if (TextUtils.isEmpty(alternateContentDescription)) {
1166dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateContentDescription = getIntentResolveLabel(alternateIntent);
1167dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            }
1168dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1169dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1170eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // If the Entry has no visual elements, return null
1171eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (icon == null && TextUtils.isEmpty(header) && TextUtils.isEmpty(subHeader) &&
1172eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                subHeaderIcon == null && TextUtils.isEmpty(text) && textIcon == null) {
1173eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            return null;
1174eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1175eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1176ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos        final int dataId = dataItem.getId() > Integer.MAX_VALUE ?
1177ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                -1 : (int) dataItem.getId();
1178ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos
1179dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        return new Entry(dataId, icon, header, subHeader, subHeaderIcon, text, textIcon, intent,
1180dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                alternateIcon, alternateIntent, alternateContentDescription, shouldApplyColor,
1181dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                isEditable);
1182eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    }
1183eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1184eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private List<Entry> dataItemsToEntries(List<DataItem> dataItems) {
1185eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
1186eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        for (DataItem dataItem : dataItems) {
1187eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Entry entry = dataItemToEntry(dataItem);
1188eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            if (entry != null) {
1189eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos                entries.add(entry);
1190eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            }
1191eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1192eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        return entries;
1193edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    }
1194edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann
1195dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos    private String getIntentResolveLabel(Intent intent) {
1196dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        final List<ResolveInfo> matches = getPackageManager().queryIntentActivities(intent,
1197dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                PackageManager.MATCH_DEFAULT_ONLY);
1198dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1199dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        // Pick first match, otherwise best found
1200dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        ResolveInfo bestResolve = null;
1201dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        final int size = matches.size();
1202dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (size == 1) {
1203dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            bestResolve = matches.get(0);
1204dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        } else if (size > 1) {
1205dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            bestResolve = ResolveCache.getInstance(this).getBestResolve(intent, matches);
1206dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1207dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1208dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        if (bestResolve == null) {
1209dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos            return null;
1210dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        }
1211dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1212dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos        return String.valueOf(bestResolve.loadLabel(getPackageManager()));
1213dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos    }
1214dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos
1215edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann    /**
121631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * Asynchronously extract the most vibrant color from the PhotoView. Once extracted,
121731b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * apply this tint to {@link MultiShrinkScroller}. This operation takes about 20-30ms
121831b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     * on a Nexus 5.
121931b2d42fb0889e61515d27314aa5a245147100daBrian Attwell     */
122031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    private void extractAndApplyTintFromPhotoViewAsynchronously() {
122131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        if (mScroller == null) {
122231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            return;
122331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }
122431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        final Drawable imageViewDrawable = mPhotoView.getDrawable();
1225faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        new AsyncTask<Void, Void, MaterialPalette>() {
122631b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
1227faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected MaterialPalette doInBackground(Void... params) {
1228faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell
122931b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                if (imageViewDrawable instanceof BitmapDrawable) {
12308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    final Bitmap bitmap = ((BitmapDrawable) imageViewDrawable).getBitmap();
1231faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    final int primaryColor = colorFromBitmap(bitmap);
1232faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    if (primaryColor != 0) {
1233a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                        return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(
1234faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                                primaryColor);
1235faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    }
12368a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
12378a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (imageViewDrawable instanceof LetterTileDrawable) {
1238faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    final int primaryColor = ((LetterTileDrawable) imageViewDrawable).getColor();
1239a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                    return mMaterialColorMapUtils.calculatePrimaryAndSecondaryColor(primaryColor);
124031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
1241a3859ed9ff9f315d492e199620a08ae35ab8569fBrian Attwell                return MaterialColorMapUtils.getDefaultPrimaryAndSecondaryColors(getResources());
124231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
124331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
124431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            @Override
1245faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell            protected void onPostExecute(MaterialPalette palette) {
1246faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                super.onPostExecute(palette);
12478571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (mHasComputedThemeColor) {
12488571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // If we had previously computed a theme color from the contact photo,
12498571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // then do not update the theme color. Changing the theme color several
12508571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // seconds after QC has started, as a result of an updated/upgraded photo,
12518571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // is a jarring experience. On the other hand, changing the theme color after
12528571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    // a rotation or onNewIntent() is perfectly fine.
12538571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    return;
12548571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                }
12558571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // Check that the Photo has not changed. If it has changed, the new tint
12568571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                // color needs to be extracted
12578571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                if (imageViewDrawable == mPhotoView.getDrawable()) {
12588571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    mHasComputedThemeColor = true;
1259faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                    setThemeColor(palette);
126031b2d42fb0889e61515d27314aa5a245147100daBrian Attwell                }
126131b2d42fb0889e61515d27314aa5a245147100daBrian Attwell            }
126231b2d42fb0889e61515d27314aa5a245147100daBrian Attwell        }.execute();
126331b2d42fb0889e61515d27314aa5a245147100daBrian Attwell    }
126431b2d42fb0889e61515d27314aa5a245147100daBrian Attwell
12654936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    /**
12664936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     * Examine how many white pixels are in the bitmap in order to determine whether or not
12674936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     * we need gradient overlays on top of the image.
12684936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell     */
12694936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    private void analyzeWhitenessOfPhotoAsynchronously() {
12704936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        final Drawable imageViewDrawable = mPhotoView.getDrawable();
12714936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        new AsyncTask<Void, Void, Boolean>() {
12724936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            @Override
12734936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            protected Boolean doInBackground(Void... params) {
12744936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                if (imageViewDrawable instanceof BitmapDrawable) {
12754936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                    final Bitmap bitmap = ((BitmapDrawable) imageViewDrawable).getBitmap();
12764936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                    return WhitenessUtils.isBitmapWhiteAtTopOrBottom(bitmap);
12774936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                }
12784936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                return !(imageViewDrawable instanceof LetterTileDrawable);
12794936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            }
12804936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell
12814936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            @Override
12824936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            protected void onPostExecute(Boolean isWhite) {
12834936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                super.onPostExecute(isWhite);
12844936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell                mScroller.setUseGradient(isWhite);
12854936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell            }
12864936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell        }.execute();
12874936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell    }
12884936a92aeef739ae6b57dad00f385307d1b1c5b8Brian Attwell
1289faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell    private void setThemeColor(MaterialPalette palette) {
12909b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        // If the color is invalid, use the predefined default
1291faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        final int primaryColor = palette.mPrimaryColor;
1292faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mScroller.setHeaderTintColor(primaryColor);
1293faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mStatusBarColor = palette.mSecondaryColor;
12949b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        updateStatusBarColor();
12958571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell
12969b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell        mColorFilter =
1297faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell                new PorterDuffColorFilter(primaryColor, PorterDuff.Mode.SRC_ATOP);
1298faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mContactCard.setColorAndFilter(primaryColor, mColorFilter);
1299faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mRecentCard.setColorAndFilter(primaryColor, mColorFilter);
1300faf973989af7ac3b735c2aee1bec22ef6608b123Brian Attwell        mAboutCard.setColorAndFilter(primaryColor, mColorFilter);
13019b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell    }
13029b889e6ef4534db6925e3f915bed281733ea8bc3Brian Attwell
13038a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private void updateStatusBarColor() {
13048a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller == null) {
13058a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            return;
13068a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
13078a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int desiredStatusBarColor;
13088a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Only use a custom status bar color if QuickContacts touches the top of the viewport.
13098a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mScroller.getScrollNeededToBeFullScreen() <= 0) {
13108a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = mStatusBarColor;
13118a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        } else {
13128a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            desiredStatusBarColor = Color.TRANSPARENT;
13138a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
13148a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Animate to the new color.
13158a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (desiredStatusBarColor != getWindow().getStatusBarColor()) {
13168a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            final ObjectAnimator animation = ObjectAnimator.ofInt(getWindow(), "statusBarColor",
13178a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    getWindow().getStatusBarColor(), desiredStatusBarColor);
13188a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            animation.setDuration(ANIMATION_STATUS_BAR_COLOR_CHANGE_DURATION);
13198a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            animation.setEvaluator(new ArgbEvaluator());
13208a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            animation.start();
13218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
13228a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
13238a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
13248a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    private int colorFromBitmap(Bitmap bitmap) {
13258a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // Author of Palette recommends using 24 colors when analyzing profile photos.
13268a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final int NUMBER_OF_PALETTE_COLORS = 24;
13278a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        final Palette palette = Palette.generate(bitmap, NUMBER_OF_PALETTE_COLORS);
1328a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell        if (palette != null && palette.getVibrantSwatch() != null) {
1329a0f20f77e1b4f6cde5934d8b3348d93b58fd6362Brian Attwell            return palette.getVibrantSwatch().getRgb();
13308a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
13318a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        return 0;
13328a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
13338a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1334b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private List<Entry> contactInteractionsToEntries(List<ContactInteraction> interactions) {
1335eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<Entry> entries = new ArrayList<>();
1336b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        for (ContactInteraction interaction : interactions) {
1337ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos            entries.add(new Entry(/* id = */ -1,
1338ea5e0b715030f1b0e8015defd10842127a8a4121Paul Soulos                    interaction.getIcon(this),
1339b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewHeader(this),
1340b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewBody(this),
1341b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getBodyIcon(this),
1342b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getViewFooter(this),
1343b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getFooterIcon(this),
1344b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    interaction.getIntent(),
1345dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIcon = */ null,
1346dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateIntent = */ null,
1347dd7419d90cda5c52e81491bea3cf3c3f4e0535c7Paul Soulos                    /* alternateContentDescription = */ null,
134848ebbaafcf467c072e4477c98ef2faba1c65af7ePaul Soulos                    /* shouldApplyColor = */ true,
1349b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    /* isEditable = */ false));
1350b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1351b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return entries;
1352b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
1353b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1354eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<Contact> mLoaderContactCallbacks =
1355851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            new LoaderCallbacks<Contact>() {
1356cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1357851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoaderReset(Loader<Contact> loader) {
1358405ae406379ad9c47770783afc76a660f6c55fcaPaul Soulos            mContactData = null;
1359cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1360cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1361cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1362851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public void onLoadFinished(Loader<Contact> loader, Contact data) {
13638a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            Trace.beginSection("onLoadFinished()");
13648a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1365cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (isFinishing()) {
1366cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                return;
1367cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1368cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (data.isError()) {
1369cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                // This shouldn't ever happen, so throw an exception. The {@link ContactLoader}
1370cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                // should log the actual exception.
1371cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                throw new IllegalStateException("Failed to load contact", data.getException());
1372cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1373cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (data.isNotFound()) {
13748a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                if (mHasAlreadyBeenOpened) {
13758a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    finish();
13768a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                } else {
13778a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Log.i(TAG, "No contact found: " + ((ContactLoader)loader).getLookupUri());
13788a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                    Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage,
13798a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                            Toast.LENGTH_LONG).show();
13808a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell                }
1381cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                return;
1382cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1383cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1384b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            bindContactData(data);
1385cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
13868a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            Trace.endSection();
1387cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1388cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann
1389cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        @Override
1390851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public Loader<Contact> onCreateLoader(int id, Bundle args) {
1391cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            if (mLookupUri == null) {
1392cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann                Log.wtf(TAG, "Lookup uri wasn't initialized. Loader was started too early");
1393cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann            }
1394d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Load all contact data. We need loadGroupMetaData=true to determine whether the
1395d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // contact is invisible. If it is, we need to display an "Add to Contacts" MenuItem.
1396b2b435a944947fbf1965c3bb7c202a97f0273259Yorke Lee            return new ContactLoader(getApplicationContext(), mLookupUri,
1397d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    true /*loadGroupMetaData*/, false /*loadInvitableAccountTypes*/,
13988571dd3953ddf2cf8442335657a1945487d665a1Brian Attwell                    true /*postViewNotification*/, true /*computeFormattedPhoneNumber*/);
1399cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann        }
1400cb8d73fc8ea538d0b63dd77210c05c5b8de32f03Daniel Lehmann    };
1401b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1402b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    @Override
1403b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    public void onBackPressed() {
1404b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        if (mScroller != null) {
14058477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            if (!mIsExitAnimationInProgress) {
14068477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell                mScroller.scrollOffBottom();
14078477eafe57bca7b6b72da6e2e419ebeaf4de9674Brian Attwell            }
1408b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        } else {
1409b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell            super.onBackPressed();
1410b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell        }
1411b7e4364d6536449271ac4534dbeb7430134c096bBrian Attwell    }
1412b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
14138a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
14148a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    public void finish() {
14158a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.finish();
14168a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
14178a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        // override transitions to skip the standard window animations
14188a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        overridePendingTransition(0, 0);
14198a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
14208a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
1421eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos    private final LoaderCallbacks<List<ContactInteraction>> mLoaderInteractionsCallbacks =
1422b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            new LoaderCallbacks<List<ContactInteraction>>() {
1423b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1424b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1425b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public Loader<List<ContactInteraction>> onCreateLoader(int id, Bundle args) {
1426b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            Log.v(TAG, "onCreateLoader");
1427b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            Loader<List<ContactInteraction>> loader = null;
1428b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            switch (id) {
1429b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                case LOADER_SMS_ID:
1430b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    Log.v(TAG, "LOADER_SMS_ID");
1431b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    loader = new SmsInteractionsLoader(
1432b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            QuickContactActivity.this,
1433ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
1434b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                            MAX_SMS_RETRIEVE);
1435b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    break;
1436899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                case LOADER_CALENDAR_ID:
1437899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    Log.v(TAG, "LOADER_CALENDAR_ID");
1438ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    final String[] emailsArray = args.getStringArray(KEY_LOADER_EXTRA_EMAILS);
1439ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    List<String> emailsList = null;
1440ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    if (emailsArray != null) {
1441ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                        emailsList = Arrays.asList(args.getStringArray(KEY_LOADER_EXTRA_EMAILS));
1442ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                    }
1443899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    loader = new CalendarInteractionsLoader(
1444899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            QuickContactActivity.this,
1445ae4cafed699b7cd6c5d4dad855bc0d4948452983Paul Soulos                            emailsList,
1446899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_FUTURE_CALENDAR_RETRIEVE,
1447899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            MAX_PAST_CALENDAR_RETRIEVE,
1448899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            FUTURE_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR,
1449899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                            PAST_MILLISECOND_TO_SEARCH_LOCAL_CALENDAR);
1450899aa21e911ee7170beab228d44d7fed68c414e4Paul Soulos                    break;
1451ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                case LOADER_CALL_LOG_ID:
1452ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                    Log.v(TAG, "LOADER_CALL_LOG_ID");
1453ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                    loader = new CallLogInteractionsLoader(
1454ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            QuickContactActivity.this,
1455ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            args.getStringArray(KEY_LOADER_EXTRA_PHONES),
1456ab840448dd00ce3f02ec7317df69b24613ade8a9Paul Soulos                            MAX_CALL_LOG_RETRIEVE);
1457b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1458b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            return loader;
1459b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1460b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1461b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1462b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoadFinished(Loader<List<ContactInteraction>> loader,
1463b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                List<ContactInteraction> data) {
1464b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.put(loader.getId(), data);
1465b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1466b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            if (isAllRecentDataLoaded()) {
1467b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                bindRecentData();
1468b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1469b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1470b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1471b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        @Override
1472b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        public void onLoaderReset(Loader<List<ContactInteraction>> loader) {
1473b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentLoaderResults.remove(loader.getId());
1474b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1475b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    };
1476b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1477b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private boolean isAllRecentDataLoaded() {
1478b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        return mRecentLoaderResults.size() == mRecentLoaderIds.length;
1479b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
1480b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1481b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    private void bindRecentData() {
1482eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final List<ContactInteraction> allInteractions = new ArrayList<>();
1483b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        for (List<ContactInteraction> loaderInteractions : mRecentLoaderResults.values()) {
1484b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            allInteractions.addAll(loaderInteractions);
1485b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1486b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
1487b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        // Sort the interactions by most recent
1488b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        Collections.sort(allInteractions, new Comparator<ContactInteraction>() {
1489b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            @Override
1490b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            public int compare(ContactInteraction a, ContactInteraction b) {
1491b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                return a.getInteractionDate() >= b.getInteractionDate() ? -1 : 1;
1492b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            }
1493b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        });
1494b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos
149560e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos
149660e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos        List<List<Entry>> interactionsWrapper = new ArrayList<>();
149760e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos        interactionsWrapper.add(contactInteractionsToEntries(allInteractions));
1498b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        if (allInteractions.size() > 0) {
149960e5108e39e54d0a7e4c6924b33597d338be3fb9Paul Soulos            mRecentCard.initialize(interactionsWrapper,
1500b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos                    /* numInitialVisibleEntries = */ MIN_NUM_COLLAPSED_RECENT_ENTRIES_SHOWN,
1501e8ce6ee1c5ab9f4f3dd11526337c70a8867647d9Brian Attwell                    /* isExpanded = */ false, mExpandingEntryCardViewListener);
1502b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos            mRecentCard.setVisibility(View.VISIBLE);
1503b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos        }
1504eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos
1505eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // About card is initialized along with the contact card, but since it appears after
1506eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // the recent card in the UI, we hold off until making it visible until the recent card
1507eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        // is also ready to avoid stuttering.
1508eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        if (mAboutCard.shouldShow()) {
1509eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mAboutCard.setVisibility(View.VISIBLE);
1510eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } else {
1511eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            mAboutCard.setVisibility(View.GONE);
1512eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        }
1513b3054e551173887029c55cb10b83f1afb7f8a6fePaul Soulos    }
15148a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
15158a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    @Override
15168a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    protected void onStop() {
15178a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        super.onStop();
15188a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell
15198a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        if (mEntriesAndActionsTask != null) {
15208a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // Once the activity is stopped, we will no longer want to bind mEntriesAndActionsTask's
15218a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // results on the UI thread. In some circumstances Activities are killed without
15228a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // onStop() being called. This is not a problem, because in these circumstances
15238a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            // the entire process will be killed.
15248a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell            mEntriesAndActionsTask.cancel(/* mayInterruptIfRunning = */ false);
15258a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell        }
15268a6f4ade05e5a8a89d91078ef9c22944450ac8baBrian Attwell    }
152723889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos
152823889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    /**
1529d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     * Returns true if it is possible to edit the current contact.
1530d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell     */
1531d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private boolean isContactEditable() {
1532d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return mContactData != null && !mContactData.isDirectoryEntry();
1533d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1534d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
15356095369885edcca566a812b551886e29c7ff8039Brian Attwell    private Intent getEditContactIntent() {
1536d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        final Intent intent = new Intent(Intent.ACTION_EDIT, mLookupUri);
1537d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        mContactLoader.cacheResult();
1538d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
15396095369885edcca566a812b551886e29c7ff8039Brian Attwell        return intent;
15406095369885edcca566a812b551886e29c7ff8039Brian Attwell    }
15416095369885edcca566a812b551886e29c7ff8039Brian Attwell
15426095369885edcca566a812b551886e29c7ff8039Brian Attwell    private void editContact() {
15436095369885edcca566a812b551886e29c7ff8039Brian Attwell        startActivityForResult(getEditContactIntent(), REQUEST_CODE_CONTACT_EDITOR_ACTIVITY);
1544d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1545d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1546d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    private void toggleStar(MenuItem starredMenuItem) {
1547d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        // Make sure there is a contact
1548d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mLookupUri != null) {
1549d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Read the current starred value from the UI instead of using the last
1550d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // loaded state. This allows rapid tapping without writing the same
1551d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // value several times
1552d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final boolean isStarred = starredMenuItem.isChecked();
1553d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1554d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // To improve responsiveness, swap out the picture (and tag) in the UI already
1555d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            ContactDetailDisplayUtils.configureStarredMenuItem(starredMenuItem,
1556d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
1557d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    !isStarred);
1558d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1559d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Now perform the real save
1560eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos            final Intent intent = ContactSaveService.createSetStarredIntent(
1561d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    QuickContactActivity.this, mLookupUri, !isStarred);
1562d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            startService(intent);
1563d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
1564d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1565d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1566752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
1567752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Calls into the contacts provider to get a pre-authorized version of the given URI.
1568752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
1569752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private Uri getPreAuthorizedUri(Uri uri) {
1570752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Bundle uriBundle = new Bundle();
1571752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        uriBundle.putParcelable(ContactsContract.Authorization.KEY_URI_TO_AUTHORIZE, uri);
1572752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Bundle authResponse = getContentResolver().call(
1573752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                ContactsContract.AUTHORITY_URI,
1574752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                ContactsContract.Authorization.AUTHORIZATION_METHOD,
1575752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                null,
1576752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                uriBundle);
1577752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        if (authResponse != null) {
1578752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            return (Uri) authResponse.getParcelable(
1579752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    ContactsContract.Authorization.KEY_AUTHORIZED_URI);
1580752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        } else {
1581752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            return uri;
1582752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1583752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
15842d150da246632b1649999cfabed776133b097775Brian Attwell
1585752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void shareContact() {
1586752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final String lookupKey = mContactData.getLookupKey();
1587752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey);
1588752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        if (mContactData.isUserProfile()) {
1589752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // User is sharing the profile.  We don't want to force the receiver to have
1590752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // the highly-privileged READ_PROFILE permission, so we need to request a
1591752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            // pre-authorized URI from the provider.
1592752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            shareUri = getPreAuthorizedUri(shareUri);
1593752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1594752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1595752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent intent = new Intent(Intent.ACTION_SEND);
1596752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        intent.setType(Contacts.CONTENT_VCARD_TYPE);
1597752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        intent.putExtra(Intent.EXTRA_STREAM, shareUri);
1598752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1599752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        // Launch chooser to share contact via
1600752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final CharSequence chooseTitle = getText(R.string.share_via);
1601752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final Intent chooseIntent = Intent.createChooser(intent, chooseTitle);
1602752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1603752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        try {
1604752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            this.startActivity(chooseIntent);
1605eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        } catch (final ActivityNotFoundException ex) {
1606752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
1607752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        }
1608752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
1609752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1610752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    /**
1611752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     * Creates a launcher shortcut with the current contact.
1612752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell     */
1613752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    private void createLauncherShortcutWithContact() {
1614752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        final ShortcutIntentBuilder builder = new ShortcutIntentBuilder(this,
1615752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                new OnShortcutIntentCreatedListener() {
1616752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1617752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    @Override
1618752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    public void onShortcutIntentCreated(Uri uri, Intent shortcutIntent) {
1619752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Broadcast the shortcutIntent to the launcher to create a
1620752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // shortcut to this contact
1621752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        shortcutIntent.setAction(ACTION_INSTALL_SHORTCUT);
1622752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        QuickContactActivity.this.sendBroadcast(shortcutIntent);
1623752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1624752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // Send a toast to give feedback to the user that a shortcut to this
1625752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        // contact was added to the launcher.
1626752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                        Toast.makeText(QuickContactActivity.this,
1627752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                R.string.createContactShortcutSuccessful,
1628752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                                Toast.LENGTH_SHORT).show();
1629752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                    }
1630752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1631752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                });
1632752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell        builder.createContactShortcutIntent(mLookupUri);
1633752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell    }
1634752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell
1635d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1636d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onCreateOptionsMenu(Menu menu) {
1637eb64a4b5c51b39fe56ba4ef97dfff73fdcdf8c75Paul Soulos        final MenuInflater inflater = getMenuInflater();
1638d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        inflater.inflate(R.menu.quickcontact, menu);
1639d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        return true;
1640d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1641d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1642d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1643d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onPrepareOptionsMenu(Menu menu) {
1644d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        if (mContactData != null) {
1645d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem starredMenuItem = menu.findItem(R.id.menu_star);
1646d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            ContactDetailDisplayUtils.configureStarredMenuItem(starredMenuItem,
1647d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.isDirectoryEntry(), mContactData.isUserProfile(),
1648d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    mContactData.getStarred());
1649d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            // Configure edit MenuItem
1650d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            final MenuItem editMenuItem = menu.findItem(R.id.menu_edit);
1651d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            editMenuItem.setVisible(true);
1652d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            if (DirectoryContactUtil.isDirectoryContact(mContactData) || InvisibleContactUtil
1653d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    .isInvisibleAndAddable(mContactData, this)) {
165430cfd121ad8c8adb83cf417ff1d40a8ba1e3761dBrian Attwell                editMenuItem.setIcon(R.drawable.ic_person_add_tinted_24dp);
16552e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_add_contact);
1656d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else if (isContactEditable()) {
1657d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setIcon(R.drawable.ic_create_24dp);
16582e4214c79170cdb6c1b8b6ff0408925d3f512becBrian Attwell                editMenuItem.setTitle(R.string.menu_editContact);
1659d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            } else {
1660d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                editMenuItem.setVisible(false);
1661d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            }
16628a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos            return true;
1663d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
16648a6d0022b07640d4a1fb8b264c8822bbab2981adPaul Soulos        return false;
1665d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    }
1666d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell
1667d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    @Override
1668d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell    public boolean onOptionsItemSelected(MenuItem item) {
1669d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        switch (item.getItemId()) {
1670d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_star:
1671d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                toggleStar(item);
1672d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
1673d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            case R.id.menu_edit:
1674d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                if (DirectoryContactUtil.isDirectoryContact(mContactData)) {
1675d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    DirectoryContactUtil.addToMyContacts(mContactData, this, getFragmentManager(),
1676d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                            mSelectAccountFragmentListener);
1677d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (InvisibleContactUtil.isInvisibleAndAddable(mContactData, this)) {
1678d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    InvisibleContactUtil.addToDefaultGroup(mContactData, this);
1679d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                } else if (isContactEditable()) {
1680d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                    editContact();
1681d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                }
1682d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return true;
1683752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_share:
1684752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                shareContact();
1685752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
1686752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell            case R.id.menu_create_contact_shortcut:
1687752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                createLauncherShortcutWithContact();
1688752cccf954bd6070c5fb623875314951c8e9849dBrian Attwell                return true;
1689d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell            default:
1690d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell                return super.onOptionsItemSelected(item);
1691d28851f436c39a83f02d3b405fd91f0fb4833b2aBrian Attwell        }
169223889ba7ca18ed4ea56248355438a734511bc0b7Paul Soulos    }
1693edb576aab33efff623691a89ace3c76cb2ff12d1Daniel Lehmann}
1694