Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.contacts.quickcontact; |
| 18 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.app.Fragment; |
| 21 | import android.app.FragmentManager; |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 22 | import android.app.LoaderManager.LoaderCallbacks; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 23 | import android.content.ActivityNotFoundException; |
| 24 | import android.content.ContentUris; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 27 | import android.content.Loader; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageManager; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 29 | import android.graphics.Rect; |
| 30 | import android.graphics.drawable.Drawable; |
| 31 | import android.net.Uri; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 32 | import android.os.Bundle; |
| 33 | import android.os.Handler; |
| 34 | import android.provider.ContactsContract.CommonDataKinds.Email; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 35 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 36 | import android.provider.ContactsContract.CommonDataKinds.SipAddress; |
| 37 | import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; |
| 38 | import android.provider.ContactsContract.CommonDataKinds.Website; |
| 39 | import android.provider.ContactsContract.Contacts; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 40 | import android.provider.ContactsContract.QuickContact; |
| 41 | import android.provider.ContactsContract.RawContacts; |
| 42 | import android.support.v13.app.FragmentPagerAdapter; |
Chiao Cheng | 35ad0c3 | 2013-02-12 12:25:45 -0800 | [diff] [blame] | 43 | import android.support.v4.view.PagerAdapter; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 44 | import android.support.v4.view.ViewPager; |
| 45 | import android.support.v4.view.ViewPager.SimpleOnPageChangeListener; |
| 46 | import android.text.TextUtils; |
| 47 | import android.util.Log; |
| 48 | import android.view.MotionEvent; |
| 49 | import android.view.View; |
| 50 | import android.view.View.OnClickListener; |
| 51 | import android.view.ViewGroup; |
Daisuke Miyakawa | 10d7df7 | 2011-07-29 16:11:05 -0700 | [diff] [blame] | 52 | import android.view.WindowManager; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 53 | import android.widget.HorizontalScrollView; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 54 | import android.widget.ImageView; |
| 55 | import android.widget.RelativeLayout; |
| 56 | import android.widget.TextView; |
| 57 | import android.widget.Toast; |
| 58 | |
Christine Chen | 72b3ab1 | 2013-08-13 23:22:34 +0200 | [diff] [blame] | 59 | import com.android.contacts.ContactSaveService; |
Chiao Cheng | d7ca03e | 2012-10-24 15:14:08 -0700 | [diff] [blame] | 60 | import com.android.contacts.common.Collapser; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 61 | import com.android.contacts.R; |
Chiao Cheng | 0d5588d | 2012-11-26 15:34:14 -0800 | [diff] [blame] | 62 | import com.android.contacts.common.model.AccountTypeManager; |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 63 | import com.android.contacts.model.Contact; |
| 64 | import com.android.contacts.model.ContactLoader; |
| 65 | import com.android.contacts.model.RawContact; |
Chiao Cheng | 428f008 | 2012-11-13 18:38:56 -0800 | [diff] [blame] | 66 | import com.android.contacts.common.model.account.AccountType; |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 67 | import com.android.contacts.model.dataitem.DataItem; |
Chiao Cheng | 428f008 | 2012-11-13 18:38:56 -0800 | [diff] [blame] | 68 | import com.android.contacts.common.model.dataitem.DataKind; |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 69 | import com.android.contacts.model.dataitem.EmailDataItem; |
| 70 | import com.android.contacts.model.dataitem.ImDataItem; |
Chiao Cheng | d9eab4d | 2012-11-14 18:24:25 -0800 | [diff] [blame] | 71 | import com.android.contacts.common.util.Constants; |
Yorke Lee | 2e38583 | 2013-09-09 14:42:01 -0700 | [diff] [blame^] | 72 | import com.android.contacts.common.util.UriUtils; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 73 | import com.android.contacts.util.DataStatus; |
| 74 | import com.android.contacts.util.ImageViewDrawableSetter; |
| 75 | import com.android.contacts.util.SchedulingUtils; |
Chiao Cheng | 9f2a5e7 | 2012-10-30 09:34:32 -0700 | [diff] [blame] | 76 | import com.android.contacts.common.util.StopWatch; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 77 | import com.google.common.base.Preconditions; |
| 78 | import com.google.common.collect.Lists; |
| 79 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 80 | import java.util.HashMap; |
| 81 | import java.util.HashSet; |
| 82 | import java.util.List; |
| 83 | import java.util.Set; |
| 84 | |
| 85 | // TODO: Save selected tab index during rotation |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 86 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Mostly translucent {@link Activity} that shows QuickContact dialog. It loads |
| 89 | * data asynchronously, and then shows a popup with details centered around |
| 90 | * {@link Intent#getSourceBounds()}. |
| 91 | */ |
| 92 | public class QuickContactActivity extends Activity { |
| 93 | private static final String TAG = "QuickContact"; |
| 94 | |
| 95 | private static final boolean TRACE_LAUNCH = false; |
| 96 | private static final String TRACE_TAG = "quickcontact"; |
Josh Gargus | ee287e9 | 2012-04-06 20:11:33 -0700 | [diff] [blame] | 97 | private static final int POST_DRAW_WAIT_DURATION = 60; |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 98 | private static final boolean ENABLE_STOPWATCH = false; |
| 99 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 100 | |
| 101 | @SuppressWarnings("deprecation") |
| 102 | private static final String LEGACY_AUTHORITY = android.provider.Contacts.AUTHORITY; |
| 103 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 104 | private Uri mLookupUri; |
| 105 | private String[] mExcludeMimes; |
| 106 | private List<String> mSortedActionMimeTypes = Lists.newArrayList(); |
| 107 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 108 | private FloatingChildLayout mFloatingLayout; |
| 109 | |
| 110 | private View mPhotoContainer; |
| 111 | private ViewGroup mTrack; |
| 112 | private HorizontalScrollView mTrackScroller; |
| 113 | private View mSelectedTabRectangle; |
Daniel Lehmann | 1792b08 | 2011-08-23 18:48:22 -0700 | [diff] [blame] | 114 | private View mLineAfterTrack; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 115 | |
Yorke Lee | 6219668 | 2013-09-06 18:34:17 -0700 | [diff] [blame] | 116 | private ImageView mPhotoView; |
Yorke Lee | 9d85a2a | 2012-11-07 17:03:08 -0800 | [diff] [blame] | 117 | private ImageView mOpenDetailsImage; |
Christine Chen | 72b3ab1 | 2013-08-13 23:22:34 +0200 | [diff] [blame] | 118 | private ImageView mStarImage; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 119 | private ViewPager mListPager; |
Chiao Cheng | 35ad0c3 | 2013-02-12 12:25:45 -0800 | [diff] [blame] | 120 | private ViewPagerAdapter mPagerAdapter; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 121 | |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 122 | private ContactLoader mContactLoader; |
| 123 | |
Josh Gargus | 9758a92 | 2012-03-08 17:12:42 -0800 | [diff] [blame] | 124 | private final ImageViewDrawableSetter mPhotoSetter = new ImageViewDrawableSetter(); |
| 125 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Keeps the default action per mimetype. Empty if no default actions are set |
| 128 | */ |
| 129 | private HashMap<String, Action> mDefaultsMap = new HashMap<String, Action>(); |
| 130 | |
| 131 | /** |
| 132 | * Set of {@link Action} that are associated with the aggregate currently |
| 133 | * displayed by this dialog, represented as a map from {@link String} |
| 134 | * MIME-type to a list of {@link Action}. |
| 135 | */ |
| 136 | private ActionMultiMap mActions = new ActionMultiMap(); |
| 137 | |
| 138 | /** |
| 139 | * {@link #LEADING_MIMETYPES} and {@link #TRAILING_MIMETYPES} are used to sort MIME-types. |
| 140 | * |
| 141 | * <p>The MIME-types in {@link #LEADING_MIMETYPES} appear in the front of the dialog, |
| 142 | * in the order specified here.</p> |
| 143 | * |
| 144 | * <p>The ones in {@link #TRAILING_MIMETYPES} appear in the end of the dialog, in the order |
| 145 | * specified here.</p> |
| 146 | * |
| 147 | * <p>The rest go between them, in the order in the array.</p> |
| 148 | */ |
| 149 | private static final List<String> LEADING_MIMETYPES = Lists.newArrayList( |
| 150 | Phone.CONTENT_ITEM_TYPE, SipAddress.CONTENT_ITEM_TYPE, Email.CONTENT_ITEM_TYPE); |
| 151 | |
| 152 | /** See {@link #LEADING_MIMETYPES}. */ |
| 153 | private static final List<String> TRAILING_MIMETYPES = Lists.newArrayList( |
| 154 | StructuredPostal.CONTENT_ITEM_TYPE, Website.CONTENT_ITEM_TYPE); |
| 155 | |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 156 | /** Id for the background loader */ |
| 157 | private static final int LOADER_ID = 0; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 158 | |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 159 | private StopWatch mStopWatch = ENABLE_STOPWATCH |
| 160 | ? StopWatch.start("QuickContact") : StopWatch.getNullStopWatch(); |
| 161 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 162 | @Override |
| 163 | protected void onCreate(Bundle icicle) { |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 164 | mStopWatch.lap("c"); // create start |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 165 | super.onCreate(icicle); |
| 166 | |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 167 | mStopWatch.lap("sc"); // super.onCreate |
| 168 | |
Daniel Lehmann | 2426cb0 | 2012-05-10 18:41:21 -0700 | [diff] [blame] | 169 | if (TRACE_LAUNCH) android.os.Debug.startMethodTracing(TRACE_TAG); |
| 170 | |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 171 | // Parse intent |
| 172 | final Intent intent = getIntent(); |
| 173 | |
| 174 | Uri lookupUri = intent.getData(); |
| 175 | |
| 176 | // Check to see whether it comes from the old version. |
| 177 | if (lookupUri != null && LEGACY_AUTHORITY.equals(lookupUri.getAuthority())) { |
| 178 | final long rawContactId = ContentUris.parseId(lookupUri); |
| 179 | lookupUri = RawContacts.getContactLookupUri(getContentResolver(), |
| 180 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId)); |
| 181 | } |
| 182 | |
| 183 | mLookupUri = Preconditions.checkNotNull(lookupUri, "missing lookupUri"); |
| 184 | |
| 185 | mExcludeMimes = intent.getStringArrayExtra(QuickContact.EXTRA_EXCLUDE_MIMES); |
| 186 | |
| 187 | mStopWatch.lap("i"); // intent parsed |
| 188 | |
| 189 | mContactLoader = (ContactLoader) getLoaderManager().initLoader( |
| 190 | LOADER_ID, null, mLoaderCallbacks); |
| 191 | |
| 192 | mStopWatch.lap("ld"); // loader started |
| 193 | |
Daisuke Miyakawa | 10d7df7 | 2011-07-29 16:11:05 -0700 | [diff] [blame] | 194 | // Show QuickContact in front of soft input |
| 195 | getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, |
| 196 | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); |
| 197 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 198 | setContentView(R.layout.quickcontact_activity); |
| 199 | |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 200 | mStopWatch.lap("l"); // layout inflated |
| 201 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 202 | mFloatingLayout = (FloatingChildLayout) findViewById(R.id.floating_layout); |
| 203 | mTrack = (ViewGroup) findViewById(R.id.track); |
| 204 | mTrackScroller = (HorizontalScrollView) findViewById(R.id.track_scroller); |
Yorke Lee | 9d85a2a | 2012-11-07 17:03:08 -0800 | [diff] [blame] | 205 | mOpenDetailsImage = (ImageView) findViewById(R.id.contact_details_image); |
Christine Chen | 72b3ab1 | 2013-08-13 23:22:34 +0200 | [diff] [blame] | 206 | mStarImage = (ImageView) findViewById(R.id.quickcontact_star_button); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 207 | mListPager = (ViewPager) findViewById(R.id.item_list_pager); |
| 208 | mSelectedTabRectangle = findViewById(R.id.selected_tab_rectangle); |
Daniel Lehmann | 1792b08 | 2011-08-23 18:48:22 -0700 | [diff] [blame] | 209 | mLineAfterTrack = findViewById(R.id.line_after_track); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 210 | |
| 211 | mFloatingLayout.setOnOutsideTouchListener(new View.OnTouchListener() { |
| 212 | @Override |
| 213 | public boolean onTouch(View v, MotionEvent event) { |
Daniel Lehmann | 2426cb0 | 2012-05-10 18:41:21 -0700 | [diff] [blame] | 214 | handleOutsideTouch(); |
| 215 | return true; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 216 | } |
| 217 | }); |
| 218 | |
| 219 | final OnClickListener openDetailsClickHandler = new OnClickListener() { |
| 220 | @Override |
| 221 | public void onClick(View v) { |
| 222 | final Intent intent = new Intent(Intent.ACTION_VIEW, mLookupUri); |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 223 | mContactLoader.cacheResult(); |
Adam Powell | 8ca93ed | 2012-04-23 13:28:28 -0700 | [diff] [blame] | 224 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 225 | startActivity(intent); |
Daniel Lehmann | 2426cb0 | 2012-05-10 18:41:21 -0700 | [diff] [blame] | 226 | close(false); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 227 | } |
| 228 | }; |
Christine Chen | 72b3ab1 | 2013-08-13 23:22:34 +0200 | [diff] [blame] | 229 | mOpenDetailsImage.setOnClickListener(openDetailsClickHandler); |
| 230 | |
Chiao Cheng | 35ad0c3 | 2013-02-12 12:25:45 -0800 | [diff] [blame] | 231 | mPagerAdapter = new ViewPagerAdapter(getFragmentManager()); |
| 232 | mListPager.setAdapter(mPagerAdapter); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 233 | mListPager.setOnPageChangeListener(new PageChangeListener()); |
| 234 | |
Daniel Lehmann | d1e5011 | 2012-05-06 16:41:15 -0700 | [diff] [blame] | 235 | final Rect sourceBounds = intent.getSourceBounds(); |
Makoto Onuki | 802fa62 | 2012-05-15 15:24:00 -0700 | [diff] [blame] | 236 | if (sourceBounds != null) { |
| 237 | mFloatingLayout.setChildTargetScreen(sourceBounds); |
| 238 | } |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 239 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 240 | // find and prepare correct header view |
| 241 | mPhotoContainer = findViewById(R.id.photo_container); |
Yorke Lee | 6219668 | 2013-09-06 18:34:17 -0700 | [diff] [blame] | 242 | |
Katherine Kuan | 81281ee | 2011-07-28 16:20:59 -0700 | [diff] [blame] | 243 | setHeaderNameText(R.id.name, R.string.missing_name); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 244 | |
Yorke Lee | 6219668 | 2013-09-06 18:34:17 -0700 | [diff] [blame] | 245 | mPhotoView = (ImageView) mPhotoContainer.findViewById(R.id.photo); |
| 246 | mPhotoView.setOnClickListener(openDetailsClickHandler); |
| 247 | |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 248 | mStopWatch.lap("v"); // view initialized |
Daniel Lehmann | 2426cb0 | 2012-05-10 18:41:21 -0700 | [diff] [blame] | 249 | |
Daniel Lehmann | 3a53c73 | 2012-05-17 19:18:58 -0700 | [diff] [blame] | 250 | SchedulingUtils.doAfterLayout(mFloatingLayout, new Runnable() { |
| 251 | @Override |
| 252 | public void run() { |
| 253 | mFloatingLayout.fadeInBackground(); |
| 254 | } |
| 255 | }); |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 256 | |
| 257 | mStopWatch.lap("cf"); // onCreate finished |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Daniel Lehmann | 2426cb0 | 2012-05-10 18:41:21 -0700 | [diff] [blame] | 260 | private void handleOutsideTouch() { |
| 261 | if (mFloatingLayout.isContentFullyVisible()) { |
| 262 | close(true); |
| 263 | } |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Daniel Lehmann | 2426cb0 | 2012-05-10 18:41:21 -0700 | [diff] [blame] | 266 | private void close(boolean withAnimation) { |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 267 | // cancel any pending queries |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 268 | getLoaderManager().destroyLoader(LOADER_ID); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 269 | |
| 270 | if (withAnimation) { |
Daniel Lehmann | 2426cb0 | 2012-05-10 18:41:21 -0700 | [diff] [blame] | 271 | mFloatingLayout.fadeOutBackground(); |
| 272 | final boolean animated = mFloatingLayout.hideContent(new Runnable() { |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 273 | @Override |
| 274 | public void run() { |
Josh Gargus | ee287e9 | 2012-04-06 20:11:33 -0700 | [diff] [blame] | 275 | // Wait until the final animation frame has been drawn, otherwise |
| 276 | // there is jank as the framework transitions to the next Activity. |
| 277 | SchedulingUtils.doAfterDraw(mFloatingLayout, new Runnable() { |
| 278 | @Override |
| 279 | public void run() { |
| 280 | // Unfortunately, we need to also use postDelayed() to wait a moment |
| 281 | // for the frame to be drawn, else the framework's activity-transition |
| 282 | // animation will kick in before the final frame is available to it. |
| 283 | // This seems unavoidable. The problem isn't merely that there is no |
| 284 | // post-draw listener API; if that were so, it would be sufficient to |
| 285 | // call post() instead of postDelayed(). |
| 286 | new Handler().postDelayed(new Runnable() { |
| 287 | @Override |
| 288 | public void run() { |
| 289 | finish(); |
| 290 | } |
| 291 | }, POST_DRAW_WAIT_DURATION); |
| 292 | } |
| 293 | }); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 294 | } |
| 295 | }); |
Daniel Lehmann | 2426cb0 | 2012-05-10 18:41:21 -0700 | [diff] [blame] | 296 | if (!animated) { |
| 297 | // If we were in the wrong state, simply quit (this can happen for example |
| 298 | // if the user pushes BACK before anything has loaded) |
| 299 | finish(); |
| 300 | } |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 301 | } else { |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 302 | finish(); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public void onBackPressed() { |
Daniel Lehmann | 2426cb0 | 2012-05-10 18:41:21 -0700 | [diff] [blame] | 308 | close(true); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Katherine Kuan | 81281ee | 2011-07-28 16:20:59 -0700 | [diff] [blame] | 311 | /** Assign this string to the view if it is not empty. */ |
| 312 | private void setHeaderNameText(int id, int resId) { |
| 313 | setHeaderNameText(id, getText(resId)); |
| 314 | } |
| 315 | |
| 316 | /** Assign this string to the view if it is not empty. */ |
| 317 | private void setHeaderNameText(int id, CharSequence value) { |
| 318 | final View view = mPhotoContainer.findViewById(id); |
| 319 | if (view instanceof TextView) { |
| 320 | if (!TextUtils.isEmpty(value)) { |
| 321 | ((TextView)view).setText(value); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 327 | * Check if the given MIME-type appears in the list of excluded MIME-types |
| 328 | * that the most-recent caller requested. |
| 329 | */ |
| 330 | private boolean isMimeExcluded(String mimeType) { |
| 331 | if (mExcludeMimes == null) return false; |
| 332 | for (String excludedMime : mExcludeMimes) { |
| 333 | if (TextUtils.equals(excludedMime, mimeType)) { |
| 334 | return true; |
| 335 | } |
| 336 | } |
| 337 | return false; |
| 338 | } |
| 339 | |
| 340 | /** |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 341 | * Handle the result from the ContactLoader |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 342 | */ |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 343 | private void bindData(Contact data) { |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 344 | final ResolveCache cache = ResolveCache.getInstance(this); |
| 345 | final Context context = this; |
| 346 | |
Yorke Lee | 9d85a2a | 2012-11-07 17:03:08 -0800 | [diff] [blame] | 347 | mOpenDetailsImage.setVisibility(isMimeExcluded(Contacts.CONTENT_ITEM_TYPE) ? View.GONE |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 348 | : View.VISIBLE); |
Christine Chen | 72b3ab1 | 2013-08-13 23:22:34 +0200 | [diff] [blame] | 349 | final boolean isStarred = data.getStarred(); |
| 350 | if (isStarred) { |
| 351 | mStarImage.setImageResource(R.drawable.ic_favorite_on_lt); |
| 352 | } else { |
| 353 | mStarImage.setImageResource(R.drawable.ic_favorite_off_lt); |
| 354 | } |
| 355 | final Uri lookupUri = data.getLookupUri(); |
Christine Chen | 72b3ab1 | 2013-08-13 23:22:34 +0200 | [diff] [blame] | 356 | |
Yorke Lee | 2e38583 | 2013-09-09 14:42:01 -0700 | [diff] [blame^] | 357 | // If this is a json encoded URI, there is no local contact to star |
| 358 | if (UriUtils.isEncodedContactUri(lookupUri)) { |
| 359 | mStarImage.setVisibility(View.GONE); |
| 360 | } else { |
| 361 | mStarImage.setVisibility(View.VISIBLE); |
| 362 | mStarImage.setOnClickListener(new OnClickListener() { |
| 363 | @Override |
| 364 | public void onClick(View view) { |
| 365 | // Toggle "starred" state |
| 366 | // Make sure there is a contact |
| 367 | if (lookupUri != null) { |
| 368 | // Changes the state of the image already before sending updates to the |
| 369 | // database |
| 370 | if (isStarred) { |
| 371 | mStarImage.setImageResource(R.drawable.ic_favorite_off_lt); |
| 372 | } else { |
| 373 | mStarImage.setImageResource(R.drawable.ic_favorite_on_lt); |
| 374 | } |
| 375 | |
| 376 | // Now perform the real save |
| 377 | final Intent intent = ContactSaveService.createSetStarredIntent(context, |
| 378 | lookupUri, !isStarred); |
| 379 | context.startService(intent); |
| 380 | } |
Christine Chen | 72b3ab1 | 2013-08-13 23:22:34 +0200 | [diff] [blame] | 381 | } |
Yorke Lee | 2e38583 | 2013-09-09 14:42:01 -0700 | [diff] [blame^] | 382 | }); |
| 383 | } |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 384 | |
| 385 | mDefaultsMap.clear(); |
| 386 | |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 387 | mStopWatch.lap("sph"); // Start photo setting |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 388 | |
Yorke Lee | 6219668 | 2013-09-06 18:34:17 -0700 | [diff] [blame] | 389 | mPhotoSetter.setupContactPhoto(data, mPhotoView); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 390 | |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 391 | mStopWatch.lap("ph"); // Photo set |
| 392 | |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 393 | for (RawContact rawContact : data.getRawContacts()) { |
| 394 | for (DataItem dataItem : rawContact.getDataItems()) { |
| 395 | final String mimeType = dataItem.getMimeType(); |
Chiao Cheng | 47b6f70 | 2012-09-07 17:28:17 -0700 | [diff] [blame] | 396 | final AccountType accountType = rawContact.getAccountType(this); |
| 397 | final DataKind dataKind = AccountTypeManager.getInstance(this) |
| 398 | .getKindOrFallback(accountType, mimeType); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 399 | |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 400 | // Skip this data item if MIME-type excluded |
| 401 | if (isMimeExcluded(mimeType)) continue; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 402 | |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 403 | final long dataId = dataItem.getId(); |
| 404 | final boolean isPrimary = dataItem.isPrimary(); |
| 405 | final boolean isSuperPrimary = dataItem.isSuperPrimary(); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 406 | |
Chiao Cheng | 47b6f70 | 2012-09-07 17:28:17 -0700 | [diff] [blame] | 407 | if (dataKind != null) { |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 408 | // Build an action for this data entry, find a mapping to a UI |
| 409 | // element, build its summary from the cursor, and collect it |
| 410 | // along with all others of this MIME-type. |
Chiao Cheng | 47b6f70 | 2012-09-07 17:28:17 -0700 | [diff] [blame] | 411 | final Action action = new DataAction(context, dataItem, dataKind); |
Maurice Chu | 8a0dd0f | 2012-04-12 16:16:32 -0700 | [diff] [blame] | 412 | final boolean wasAdded = considerAdd(action, cache, isSuperPrimary); |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 413 | if (wasAdded) { |
| 414 | // Remember the default |
| 415 | if (isSuperPrimary || (isPrimary && (mDefaultsMap.get(mimeType) == null))) { |
| 416 | mDefaultsMap.put(mimeType, action); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 417 | } |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 418 | } |
| 419 | } |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 420 | |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 421 | // Handle Email rows with presence data as Im entry |
Daniel Lehmann | 9daca14 | 2011-12-20 11:41:22 +0200 | [diff] [blame] | 422 | final DataStatus status = data.getStatuses().get(dataId); |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 423 | if (status != null && dataItem instanceof EmailDataItem) { |
| 424 | final EmailDataItem email = (EmailDataItem) dataItem; |
| 425 | final ImDataItem im = ImDataItem.createFromEmail(email); |
Chiao Cheng | 47b6f70 | 2012-09-07 17:28:17 -0700 | [diff] [blame] | 426 | if (dataKind != null) { |
| 427 | final DataAction action = new DataAction(context, im, dataKind); |
Daniel Lehmann | 9daca14 | 2011-12-20 11:41:22 +0200 | [diff] [blame] | 428 | action.setPresence(status.getPresence()); |
Maurice Chu | 8a0dd0f | 2012-04-12 16:16:32 -0700 | [diff] [blame] | 429 | considerAdd(action, cache, isSuperPrimary); |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 430 | } |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | } |
| 434 | |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 435 | mStopWatch.lap("e"); // Entities inflated |
| 436 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 437 | // Collapse Action Lists (remove e.g. duplicate e-mail addresses from different sources) |
| 438 | for (List<Action> actionChildren : mActions.values()) { |
| 439 | Collapser.collapseList(actionChildren); |
| 440 | } |
| 441 | |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 442 | mStopWatch.lap("c"); // List collapsed |
| 443 | |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 444 | setHeaderNameText(R.id.name, data.getDisplayName()); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 445 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 446 | // All the mime-types to add. |
| 447 | final Set<String> containedTypes = new HashSet<String>(mActions.keySet()); |
| 448 | mSortedActionMimeTypes.clear(); |
| 449 | // First, add LEADING_MIMETYPES, which are most common. |
| 450 | for (String mimeType : LEADING_MIMETYPES) { |
| 451 | if (containedTypes.contains(mimeType)) { |
| 452 | mSortedActionMimeTypes.add(mimeType); |
| 453 | containedTypes.remove(mimeType); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // Add all the remaining ones that are not TRAILING |
| 458 | for (String mimeType : containedTypes.toArray(new String[containedTypes.size()])) { |
| 459 | if (!TRAILING_MIMETYPES.contains(mimeType)) { |
| 460 | mSortedActionMimeTypes.add(mimeType); |
| 461 | containedTypes.remove(mimeType); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // Then, add TRAILING_MIMETYPES, which are least common. |
| 466 | for (String mimeType : TRAILING_MIMETYPES) { |
| 467 | if (containedTypes.contains(mimeType)) { |
| 468 | containedTypes.remove(mimeType); |
| 469 | mSortedActionMimeTypes.add(mimeType); |
| 470 | } |
| 471 | } |
Chiao Cheng | 35ad0c3 | 2013-02-12 12:25:45 -0800 | [diff] [blame] | 472 | mPagerAdapter.notifyDataSetChanged(); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 473 | |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 474 | mStopWatch.lap("mt"); // Mime types initialized |
| 475 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 476 | // Add buttons for each mimetype |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 477 | mTrack.removeAllViews(); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 478 | for (String mimeType : mSortedActionMimeTypes) { |
Jay Shrauner | d683b54 | 2013-01-11 14:55:16 -0800 | [diff] [blame] | 479 | final View actionView = inflateAction(mimeType, cache, mTrack, data.getDisplayName()); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 480 | mTrack.addView(actionView); |
| 481 | } |
| 482 | |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 483 | mStopWatch.lap("mt"); // Buttons added |
| 484 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 485 | final boolean hasData = !mSortedActionMimeTypes.isEmpty(); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 486 | mTrackScroller.setVisibility(hasData ? View.VISIBLE : View.GONE); |
| 487 | mSelectedTabRectangle.setVisibility(hasData ? View.VISIBLE : View.GONE); |
Daniel Lehmann | 1792b08 | 2011-08-23 18:48:22 -0700 | [diff] [blame] | 488 | mLineAfterTrack.setVisibility(hasData ? View.VISIBLE : View.GONE); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 489 | mListPager.setVisibility(hasData ? View.VISIBLE : View.GONE); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Consider adding the given {@link Action}, which will only happen if |
| 494 | * {@link PackageManager} finds an application to handle |
| 495 | * {@link Action#getIntent()}. |
Maurice Chu | 8a0dd0f | 2012-04-12 16:16:32 -0700 | [diff] [blame] | 496 | * @param action the action to handle |
| 497 | * @param resolveCache cache of applications that can handle actions |
| 498 | * @param front indicates whether to add the action to the front of the list |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 499 | * @return true if action has been added |
| 500 | */ |
Maurice Chu | 8a0dd0f | 2012-04-12 16:16:32 -0700 | [diff] [blame] | 501 | private boolean considerAdd(Action action, ResolveCache resolveCache, boolean front) { |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 502 | if (resolveCache.hasResolve(action)) { |
Maurice Chu | 8a0dd0f | 2012-04-12 16:16:32 -0700 | [diff] [blame] | 503 | mActions.put(action.getMimeType(), action, front); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 504 | return true; |
| 505 | } |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Inflate the in-track view for the action of the given MIME-type, collapsing duplicate values. |
| 511 | * Will use the icon provided by the {@link DataKind}. |
| 512 | */ |
Jay Shrauner | d683b54 | 2013-01-11 14:55:16 -0800 | [diff] [blame] | 513 | private View inflateAction(String mimeType, ResolveCache resolveCache, |
| 514 | ViewGroup root, String name) { |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 515 | final CheckableImageView typeView = (CheckableImageView) getLayoutInflater().inflate( |
| 516 | R.layout.quickcontact_track_button, root, false); |
| 517 | |
| 518 | List<Action> children = mActions.get(mimeType); |
| 519 | typeView.setTag(mimeType); |
| 520 | final Action firstInfo = children.get(0); |
| 521 | |
| 522 | // Set icon and listen for clicks |
Jay Shrauner | d683b54 | 2013-01-11 14:55:16 -0800 | [diff] [blame] | 523 | final CharSequence descrip = resolveCache.getDescription(firstInfo, name); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 524 | final Drawable icon = resolveCache.getIcon(firstInfo); |
| 525 | typeView.setChecked(false); |
| 526 | typeView.setContentDescription(descrip); |
| 527 | typeView.setImageDrawable(icon); |
| 528 | typeView.setOnClickListener(mTypeViewClickListener); |
| 529 | |
| 530 | return typeView; |
| 531 | } |
| 532 | |
| 533 | private CheckableImageView getActionViewAt(int position) { |
| 534 | return (CheckableImageView) mTrack.getChildAt(position); |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public void onAttachFragment(Fragment fragment) { |
| 539 | final QuickContactListFragment listFragment = (QuickContactListFragment) fragment; |
| 540 | listFragment.setListener(mListFragmentListener); |
| 541 | } |
| 542 | |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 543 | private LoaderCallbacks<Contact> mLoaderCallbacks = |
| 544 | new LoaderCallbacks<Contact>() { |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 545 | @Override |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 546 | public void onLoaderReset(Loader<Contact> loader) { |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | @Override |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 550 | public void onLoadFinished(Loader<Contact> loader, Contact data) { |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 551 | mStopWatch.lap("lf"); // onLoadFinished |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 552 | if (isFinishing()) { |
Daniel Lehmann | 2426cb0 | 2012-05-10 18:41:21 -0700 | [diff] [blame] | 553 | close(false); |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 554 | return; |
| 555 | } |
| 556 | if (data.isError()) { |
| 557 | // This shouldn't ever happen, so throw an exception. The {@link ContactLoader} |
| 558 | // should log the actual exception. |
| 559 | throw new IllegalStateException("Failed to load contact", data.getException()); |
| 560 | } |
| 561 | if (data.isNotFound()) { |
| 562 | Log.i(TAG, "No contact found: " + ((ContactLoader)loader).getLookupUri()); |
| 563 | Toast.makeText(QuickContactActivity.this, R.string.invalidContactMessage, |
| 564 | Toast.LENGTH_LONG).show(); |
Daniel Lehmann | 2426cb0 | 2012-05-10 18:41:21 -0700 | [diff] [blame] | 565 | close(false); |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 566 | return; |
| 567 | } |
| 568 | |
| 569 | bindData(data); |
| 570 | |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 571 | mStopWatch.lap("bd"); // bindData finished |
| 572 | |
Daniel Lehmann | 2426cb0 | 2012-05-10 18:41:21 -0700 | [diff] [blame] | 573 | if (TRACE_LAUNCH) android.os.Debug.stopMethodTracing(); |
Makoto Onuki | fc0a89f | 2012-05-14 17:37:34 -0700 | [diff] [blame] | 574 | if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) { |
| 575 | Log.d(Constants.PERFORMANCE_TAG, "QuickContact shown"); |
| 576 | } |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 577 | |
| 578 | // Data bound and ready, pull curtain to show. Put this on the Handler to ensure |
| 579 | // that the layout passes are completed |
Josh Gargus | 6f5557e | 2012-03-21 10:45:08 -0700 | [diff] [blame] | 580 | SchedulingUtils.doAfterLayout(mFloatingLayout, new Runnable() { |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 581 | @Override |
| 582 | public void run() { |
Daniel Lehmann | 2426cb0 | 2012-05-10 18:41:21 -0700 | [diff] [blame] | 583 | mFloatingLayout.showContent(new Runnable() { |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 584 | @Override |
| 585 | public void run() { |
Daniel Lehmann | 9815d7f | 2012-04-16 18:28:03 -0700 | [diff] [blame] | 586 | mContactLoader.upgradeToFullContact(); |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 587 | } |
| 588 | }); |
| 589 | } |
| 590 | }); |
Makoto Onuki | dfe8cc8 | 2012-05-17 18:03:44 -0700 | [diff] [blame] | 591 | mStopWatch.stopAndLog(TAG, 0); |
| 592 | mStopWatch = StopWatch.getNullStopWatch(); // We're done with it. |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | @Override |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 596 | public Loader<Contact> onCreateLoader(int id, Bundle args) { |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 597 | if (mLookupUri == null) { |
| 598 | Log.wtf(TAG, "Lookup uri wasn't initialized. Loader was started too early"); |
| 599 | } |
Yorke Lee | b2b435a | 2012-11-12 16:47:06 -0800 | [diff] [blame] | 600 | return new ContactLoader(getApplicationContext(), mLookupUri, |
Paul Sliwowski | 45f23d4 | 2013-08-27 15:29:40 -0700 | [diff] [blame] | 601 | false /*loadGroupMetaData*/, false /*loadInvitableAccountTypes*/, |
| 602 | false /*postViewNotification*/, true /*computeFormattedPhoneNumber*/); |
Daniel Lehmann | cb8d73f | 2011-12-16 17:39:50 +0200 | [diff] [blame] | 603 | } |
| 604 | }; |
| 605 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 606 | /** A type (e.g. Call/Addresses was clicked) */ |
| 607 | private final OnClickListener mTypeViewClickListener = new OnClickListener() { |
| 608 | @Override |
| 609 | public void onClick(View view) { |
| 610 | final CheckableImageView actionView = (CheckableImageView)view; |
| 611 | final String mimeType = (String) actionView.getTag(); |
| 612 | int index = mSortedActionMimeTypes.indexOf(mimeType); |
| 613 | mListPager.setCurrentItem(index, true); |
| 614 | } |
| 615 | }; |
| 616 | |
| 617 | private class ViewPagerAdapter extends FragmentPagerAdapter { |
| 618 | public ViewPagerAdapter(FragmentManager fragmentManager) { |
| 619 | super(fragmentManager); |
| 620 | } |
| 621 | |
| 622 | @Override |
| 623 | public Fragment getItem(int position) { |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 624 | final String mimeType = mSortedActionMimeTypes.get(position); |
Chiao Cheng | 35ad0c3 | 2013-02-12 12:25:45 -0800 | [diff] [blame] | 625 | QuickContactListFragment fragment = new QuickContactListFragment(mimeType); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 626 | final List<Action> actions = mActions.get(mimeType); |
| 627 | fragment.setActions(actions); |
| 628 | return fragment; |
| 629 | } |
| 630 | |
| 631 | @Override |
| 632 | public int getCount() { |
| 633 | return mSortedActionMimeTypes.size(); |
| 634 | } |
Chiao Cheng | 35ad0c3 | 2013-02-12 12:25:45 -0800 | [diff] [blame] | 635 | |
| 636 | @Override |
| 637 | public int getItemPosition(Object object) { |
| 638 | final QuickContactListFragment fragment = (QuickContactListFragment) object; |
| 639 | final String mimeType = fragment.getMimeType(); |
| 640 | for (int i = 0; i < mSortedActionMimeTypes.size(); i++) { |
| 641 | if (mimeType.equals(mSortedActionMimeTypes.get(i))) { |
| 642 | return i; |
| 643 | } |
| 644 | } |
| 645 | return PagerAdapter.POSITION_NONE; |
| 646 | } |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | private class PageChangeListener extends SimpleOnPageChangeListener { |
Yorke Lee | 421c938 | 2012-12-12 18:36:35 -0800 | [diff] [blame] | 650 | private int mScrollingState = ViewPager.SCROLL_STATE_IDLE; |
| 651 | |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 652 | @Override |
| 653 | public void onPageSelected(int position) { |
| 654 | final CheckableImageView actionView = getActionViewAt(position); |
| 655 | mTrackScroller.requestChildRectangleOnScreen(actionView, |
| 656 | new Rect(0, 0, actionView.getWidth(), actionView.getHeight()), false); |
Yorke Lee | 421c938 | 2012-12-12 18:36:35 -0800 | [diff] [blame] | 657 | // Don't render rectangle if we are currently scrolling to prevent it from flickering |
| 658 | if (mScrollingState == ViewPager.SCROLL_STATE_IDLE) { |
| 659 | renderSelectedRectangle(position, 0); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | @Override |
| 664 | public void onPageScrollStateChanged(int state) { |
| 665 | super.onPageScrollStateChanged(state); |
| 666 | mScrollingState = state; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | @Override |
| 670 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { |
Chiao Cheng | 4c29090 | 2012-11-12 15:53:48 -0800 | [diff] [blame] | 671 | renderSelectedRectangle(position, positionOffset); |
| 672 | } |
| 673 | |
| 674 | private void renderSelectedRectangle(int position, float positionOffset) { |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 675 | final RelativeLayout.LayoutParams layoutParams = |
| 676 | (RelativeLayout.LayoutParams) mSelectedTabRectangle.getLayoutParams(); |
Chiao Cheng | 4c29090 | 2012-11-12 15:53:48 -0800 | [diff] [blame] | 677 | final int width = layoutParams.width; |
Yorke Lee | 9e4b797 | 2013-04-08 11:29:48 -0700 | [diff] [blame] | 678 | layoutParams.setMarginStart((int) ((position + positionOffset) * width)); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 679 | mSelectedTabRectangle.setLayoutParams(layoutParams); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | private final QuickContactListFragment.Listener mListFragmentListener = |
| 684 | new QuickContactListFragment.Listener() { |
| 685 | @Override |
| 686 | public void onOutsideClick() { |
| 687 | // If there is no background, we want to dismiss, because to the user it seems |
| 688 | // like he had touched outside. If the ViewPager is solid however, those taps |
| 689 | // must be ignored |
| 690 | final boolean isTransparent = mListPager.getBackground() == null; |
| 691 | if (isTransparent) handleOutsideTouch(); |
| 692 | } |
| 693 | |
| 694 | @Override |
| 695 | public void onItemClicked(final Action action, final boolean alternate) { |
| 696 | final Runnable startAppRunnable = new Runnable() { |
| 697 | @Override |
| 698 | public void run() { |
| 699 | try { |
| 700 | startActivity(alternate ? action.getAlternateIntent() : action.getIntent()); |
| 701 | } catch (ActivityNotFoundException e) { |
| 702 | Toast.makeText(QuickContactActivity.this, R.string.quickcontact_missing_app, |
| 703 | Toast.LENGTH_SHORT).show(); |
| 704 | } |
| 705 | |
Daniel Lehmann | 2426cb0 | 2012-05-10 18:41:21 -0700 | [diff] [blame] | 706 | close(false); |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 707 | } |
| 708 | }; |
| 709 | // Defer the action to make the window properly repaint |
| 710 | new Handler().post(startAppRunnable); |
| 711 | } |
| 712 | }; |
Daniel Lehmann | edb576a | 2011-07-27 16:45:13 -0700 | [diff] [blame] | 713 | } |