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