ChooserActivity.java revision a2c7774d218a5c42ea9154a3df122d5275589911
1/* 2 * Copyright (C) 2008 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 17package com.android.internal.app; 18 19import android.animation.ObjectAnimator; 20import android.annotation.NonNull; 21import android.app.Activity; 22import android.app.usage.UsageStatsManager; 23import android.content.ComponentName; 24import android.content.Context; 25import android.content.Intent; 26import android.content.IntentSender; 27import android.content.IntentSender.SendIntentException; 28import android.content.ServiceConnection; 29import android.content.SharedPreferences; 30import android.content.pm.ActivityInfo; 31import android.content.pm.LabeledIntent; 32import android.content.pm.PackageManager; 33import android.content.pm.PackageManager.NameNotFoundException; 34import android.content.pm.ResolveInfo; 35import android.database.DataSetObserver; 36import android.graphics.Color; 37import android.graphics.drawable.Drawable; 38import android.graphics.drawable.Icon; 39import android.os.Bundle; 40import android.os.Environment; 41import android.os.Handler; 42import android.os.IBinder; 43import android.os.Message; 44import android.os.Parcelable; 45import android.os.Process; 46import android.os.RemoteException; 47import android.os.ResultReceiver; 48import android.os.UserHandle; 49import android.os.UserManager; 50import android.os.storage.StorageManager; 51import android.service.chooser.ChooserTarget; 52import android.service.chooser.ChooserTargetService; 53import android.service.chooser.IChooserTargetResult; 54import android.service.chooser.IChooserTargetService; 55import android.text.TextUtils; 56import android.util.FloatProperty; 57import android.util.Log; 58import android.util.Slog; 59import android.view.LayoutInflater; 60import android.view.View; 61import android.view.View.MeasureSpec; 62import android.view.View.OnClickListener; 63import android.view.View.OnLongClickListener; 64import android.view.ViewGroup; 65import android.view.ViewGroup.LayoutParams; 66import android.view.animation.AnimationUtils; 67import android.view.animation.Interpolator; 68import android.widget.AbsListView; 69import android.widget.BaseAdapter; 70import android.widget.ListView; 71import com.android.internal.R; 72import com.android.internal.app.ResolverActivity.TargetInfo; 73import com.android.internal.logging.MetricsLogger; 74import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 75import com.google.android.collect.Lists; 76 77import java.io.File; 78import java.util.ArrayList; 79import java.util.Collections; 80import java.util.Comparator; 81import java.util.List; 82 83public class ChooserActivity extends ResolverActivity { 84 private static final String TAG = "ChooserActivity"; 85 86 private static final boolean DEBUG = false; 87 88 private static final int QUERY_TARGET_SERVICE_LIMIT = 5; 89 private static final int WATCHDOG_TIMEOUT_MILLIS = 5000; 90 91 private Bundle mReplacementExtras; 92 private IntentSender mChosenComponentSender; 93 private IntentSender mRefinementIntentSender; 94 private RefinementResultReceiver mRefinementResultReceiver; 95 private ChooserTarget[] mCallerChooserTargets; 96 97 private Intent mReferrerFillInIntent; 98 99 private ChooserListAdapter mChooserListAdapter; 100 private ChooserRowAdapter mChooserRowAdapter; 101 102 private SharedPreferences mPinnedSharedPrefs; 103 private static final float PINNED_TARGET_SCORE_BOOST = 1000.f; 104 private static final float CALLER_TARGET_SCORE_BOOST = 900.f; 105 private static final String PINNED_SHARED_PREFS_NAME = "chooser_pin_settings"; 106 private static final String TARGET_DETAILS_FRAGMENT_TAG = "targetDetailsFragment"; 107 108 private final List<ChooserTargetServiceConnection> mServiceConnections = new ArrayList<>(); 109 110 private static final int CHOOSER_TARGET_SERVICE_RESULT = 1; 111 private static final int CHOOSER_TARGET_SERVICE_WATCHDOG_TIMEOUT = 2; 112 113 private final Handler mChooserHandler = new Handler() { 114 @Override 115 public void handleMessage(Message msg) { 116 switch (msg.what) { 117 case CHOOSER_TARGET_SERVICE_RESULT: 118 if (DEBUG) Log.d(TAG, "CHOOSER_TARGET_SERVICE_RESULT"); 119 if (isDestroyed()) break; 120 final ServiceResultInfo sri = (ServiceResultInfo) msg.obj; 121 if (!mServiceConnections.contains(sri.connection)) { 122 Log.w(TAG, "ChooserTargetServiceConnection " + sri.connection 123 + " returned after being removed from active connections." 124 + " Have you considered returning results faster?"); 125 break; 126 } 127 if (sri.resultTargets != null) { 128 mChooserListAdapter.addServiceResults(sri.originalTarget, 129 sri.resultTargets); 130 } 131 unbindService(sri.connection); 132 sri.connection.destroy(); 133 mServiceConnections.remove(sri.connection); 134 if (mServiceConnections.isEmpty()) { 135 mChooserHandler.removeMessages(CHOOSER_TARGET_SERVICE_WATCHDOG_TIMEOUT); 136 sendVoiceChoicesIfNeeded(); 137 mChooserListAdapter.setShowServiceTargets(true); 138 } 139 break; 140 141 case CHOOSER_TARGET_SERVICE_WATCHDOG_TIMEOUT: 142 if (DEBUG) { 143 Log.d(TAG, "CHOOSER_TARGET_SERVICE_WATCHDOG_TIMEOUT; unbinding services"); 144 } 145 unbindRemainingServices(); 146 sendVoiceChoicesIfNeeded(); 147 mChooserListAdapter.setShowServiceTargets(true); 148 break; 149 150 default: 151 super.handleMessage(msg); 152 } 153 } 154 }; 155 156 @Override 157 protected void onCreate(Bundle savedInstanceState) { 158 Intent intent = getIntent(); 159 Parcelable targetParcelable = intent.getParcelableExtra(Intent.EXTRA_INTENT); 160 if (!(targetParcelable instanceof Intent)) { 161 Log.w("ChooserActivity", "Target is not an intent: " + targetParcelable); 162 finish(); 163 super.onCreate(null); 164 return; 165 } 166 Intent target = (Intent) targetParcelable; 167 if (target != null) { 168 modifyTargetIntent(target); 169 } 170 Parcelable[] targetsParcelable 171 = intent.getParcelableArrayExtra(Intent.EXTRA_ALTERNATE_INTENTS); 172 if (targetsParcelable != null) { 173 final boolean offset = target == null; 174 Intent[] additionalTargets = 175 new Intent[offset ? targetsParcelable.length - 1 : targetsParcelable.length]; 176 for (int i = 0; i < targetsParcelable.length; i++) { 177 if (!(targetsParcelable[i] instanceof Intent)) { 178 Log.w(TAG, "EXTRA_ALTERNATE_INTENTS array entry #" + i + " is not an Intent: " 179 + targetsParcelable[i]); 180 finish(); 181 super.onCreate(null); 182 return; 183 } 184 final Intent additionalTarget = (Intent) targetsParcelable[i]; 185 if (i == 0 && target == null) { 186 target = additionalTarget; 187 modifyTargetIntent(target); 188 } else { 189 additionalTargets[offset ? i - 1 : i] = additionalTarget; 190 modifyTargetIntent(additionalTarget); 191 } 192 } 193 setAdditionalTargets(additionalTargets); 194 } 195 196 mReplacementExtras = intent.getBundleExtra(Intent.EXTRA_REPLACEMENT_EXTRAS); 197 CharSequence title = intent.getCharSequenceExtra(Intent.EXTRA_TITLE); 198 int defaultTitleRes = 0; 199 if (title == null) { 200 defaultTitleRes = com.android.internal.R.string.chooseActivity; 201 } 202 Parcelable[] pa = intent.getParcelableArrayExtra(Intent.EXTRA_INITIAL_INTENTS); 203 Intent[] initialIntents = null; 204 if (pa != null) { 205 initialIntents = new Intent[pa.length]; 206 for (int i=0; i<pa.length; i++) { 207 if (!(pa[i] instanceof Intent)) { 208 Log.w(TAG, "Initial intent #" + i + " not an Intent: " + pa[i]); 209 finish(); 210 super.onCreate(null); 211 return; 212 } 213 final Intent in = (Intent) pa[i]; 214 modifyTargetIntent(in); 215 initialIntents[i] = in; 216 } 217 } 218 219 mReferrerFillInIntent = new Intent().putExtra(Intent.EXTRA_REFERRER, getReferrer()); 220 221 mChosenComponentSender = intent.getParcelableExtra( 222 Intent.EXTRA_CHOSEN_COMPONENT_INTENT_SENDER); 223 mRefinementIntentSender = intent.getParcelableExtra( 224 Intent.EXTRA_CHOOSER_REFINEMENT_INTENT_SENDER); 225 setSafeForwardingMode(true); 226 227 pa = intent.getParcelableArrayExtra(Intent.EXTRA_EXCLUDE_COMPONENTS); 228 if (pa != null) { 229 ComponentName[] names = new ComponentName[pa.length]; 230 for (int i = 0; i < pa.length; i++) { 231 if (!(pa[i] instanceof ComponentName)) { 232 Log.w(TAG, "Filtered component #" + i + " not a ComponentName: " + pa[i]); 233 names = null; 234 break; 235 } 236 names[i] = (ComponentName) pa[i]; 237 } 238 setFilteredComponents(names); 239 } 240 241 pa = intent.getParcelableArrayExtra(Intent.EXTRA_CHOOSER_TARGETS); 242 if (pa != null) { 243 ChooserTarget[] targets = new ChooserTarget[pa.length]; 244 for (int i = 0; i < pa.length; i++) { 245 if (!(pa[i] instanceof ChooserTarget)) { 246 Log.w(TAG, "Chooser target #" + i + " not a ChooserTarget: " + pa[i]); 247 targets = null; 248 break; 249 } 250 targets[i] = (ChooserTarget) pa[i]; 251 } 252 mCallerChooserTargets = targets; 253 } 254 255 mPinnedSharedPrefs = getPinnedSharedPrefs(this); 256 super.onCreate(savedInstanceState, target, title, defaultTitleRes, initialIntents, 257 null, false); 258 259 MetricsLogger.action(this, MetricsEvent.ACTION_ACTIVITY_CHOOSER_SHOWN); 260 } 261 262 static SharedPreferences getPinnedSharedPrefs(Context context) { 263 // The code below is because in the android:ui process, no one can hear you scream. 264 // The package info in the context isn't initialized in the way it is for normal apps, 265 // so the standard, name-based context.getSharedPreferences doesn't work. Instead, we 266 // build the path manually below using the same policy that appears in ContextImpl. 267 // This fails silently under the hood if there's a problem, so if we find ourselves in 268 // the case where we don't have access to credential encrypted storage we just won't 269 // have our pinned target info. 270 final File prefsFile = new File(new File( 271 Environment.getDataUserCePackageDirectory(StorageManager.UUID_PRIVATE_INTERNAL, 272 context.getUserId(), context.getPackageName()), 273 "shared_prefs"), 274 PINNED_SHARED_PREFS_NAME + ".xml"); 275 return context.getSharedPreferences(prefsFile, MODE_PRIVATE); 276 } 277 278 @Override 279 protected void onDestroy() { 280 super.onDestroy(); 281 if (mRefinementResultReceiver != null) { 282 mRefinementResultReceiver.destroy(); 283 mRefinementResultReceiver = null; 284 } 285 unbindRemainingServices(); 286 mChooserHandler.removeMessages(CHOOSER_TARGET_SERVICE_RESULT); 287 } 288 289 @Override 290 public Intent getReplacementIntent(ActivityInfo aInfo, Intent defIntent) { 291 Intent result = defIntent; 292 if (mReplacementExtras != null) { 293 final Bundle replExtras = mReplacementExtras.getBundle(aInfo.packageName); 294 if (replExtras != null) { 295 result = new Intent(defIntent); 296 result.putExtras(replExtras); 297 } 298 } 299 if (aInfo.name.equals(IntentForwarderActivity.FORWARD_INTENT_TO_PARENT) 300 || aInfo.name.equals(IntentForwarderActivity.FORWARD_INTENT_TO_MANAGED_PROFILE)) { 301 result = Intent.createChooser(result, 302 getIntent().getCharSequenceExtra(Intent.EXTRA_TITLE)); 303 304 // Don't auto-launch single intents if the intent is being forwarded. This is done 305 // because automatically launching a resolving application as a response to the user 306 // action of switching accounts is pretty unexpected. 307 result.putExtra(Intent.EXTRA_AUTO_LAUNCH_SINGLE_CHOICE, false); 308 } 309 return result; 310 } 311 312 @Override 313 public void onActivityStarted(TargetInfo cti) { 314 if (mChosenComponentSender != null) { 315 final ComponentName target = cti.getResolvedComponentName(); 316 if (target != null) { 317 final Intent fillIn = new Intent().putExtra(Intent.EXTRA_CHOSEN_COMPONENT, target); 318 try { 319 mChosenComponentSender.sendIntent(this, Activity.RESULT_OK, fillIn, null, null); 320 } catch (IntentSender.SendIntentException e) { 321 Slog.e(TAG, "Unable to launch supplied IntentSender to report " 322 + "the chosen component: " + e); 323 } 324 } 325 } 326 } 327 328 @Override 329 public void onPrepareAdapterView(AbsListView adapterView, ResolveListAdapter adapter, 330 boolean alwaysUseOption) { 331 final ListView listView = adapterView instanceof ListView ? (ListView) adapterView : null; 332 mChooserListAdapter = (ChooserListAdapter) adapter; 333 if (mCallerChooserTargets != null && mCallerChooserTargets.length > 0) { 334 mChooserListAdapter.addServiceResults(null, Lists.newArrayList(mCallerChooserTargets)); 335 } 336 mChooserRowAdapter = new ChooserRowAdapter(mChooserListAdapter); 337 mChooserRowAdapter.registerDataSetObserver(new OffsetDataSetObserver(adapterView)); 338 adapterView.setAdapter(mChooserRowAdapter); 339 if (listView != null) { 340 listView.setItemsCanFocus(true); 341 } 342 } 343 344 @Override 345 public int getLayoutResource() { 346 return R.layout.chooser_grid; 347 } 348 349 @Override 350 public boolean shouldGetActivityMetadata() { 351 return true; 352 } 353 354 @Override 355 public boolean shouldAutoLaunchSingleChoice(TargetInfo target) { 356 return getIntent().getBooleanExtra(Intent.EXTRA_AUTO_LAUNCH_SINGLE_CHOICE, 357 super.shouldAutoLaunchSingleChoice(target)); 358 } 359 360 @Override 361 public void showTargetDetails(ResolveInfo ri) { 362 ComponentName name = ri.activityInfo.getComponentName(); 363 boolean pinned = mPinnedSharedPrefs.getBoolean(name.flattenToString(), false); 364 ResolverTargetActionsDialogFragment f = 365 new ResolverTargetActionsDialogFragment(ri.loadLabel(getPackageManager()), 366 name, pinned); 367 f.show(getFragmentManager(), TARGET_DETAILS_FRAGMENT_TAG); 368 } 369 370 private void modifyTargetIntent(Intent in) { 371 final String action = in.getAction(); 372 if (Intent.ACTION_SEND.equals(action) || 373 Intent.ACTION_SEND_MULTIPLE.equals(action)) { 374 in.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | 375 Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 376 } 377 } 378 379 @Override 380 protected boolean onTargetSelected(TargetInfo target, boolean alwaysCheck) { 381 if (mRefinementIntentSender != null) { 382 final Intent fillIn = new Intent(); 383 final List<Intent> sourceIntents = target.getAllSourceIntents(); 384 if (!sourceIntents.isEmpty()) { 385 fillIn.putExtra(Intent.EXTRA_INTENT, sourceIntents.get(0)); 386 if (sourceIntents.size() > 1) { 387 final Intent[] alts = new Intent[sourceIntents.size() - 1]; 388 for (int i = 1, N = sourceIntents.size(); i < N; i++) { 389 alts[i - 1] = sourceIntents.get(i); 390 } 391 fillIn.putExtra(Intent.EXTRA_ALTERNATE_INTENTS, alts); 392 } 393 if (mRefinementResultReceiver != null) { 394 mRefinementResultReceiver.destroy(); 395 } 396 mRefinementResultReceiver = new RefinementResultReceiver(this, target, null); 397 fillIn.putExtra(Intent.EXTRA_RESULT_RECEIVER, 398 mRefinementResultReceiver); 399 try { 400 mRefinementIntentSender.sendIntent(this, 0, fillIn, null, null); 401 return false; 402 } catch (SendIntentException e) { 403 Log.e(TAG, "Refinement IntentSender failed to send", e); 404 } 405 } 406 } 407 updateChooserCounts(target, mContentType); 408 return super.onTargetSelected(target, alwaysCheck); 409 } 410 411 @Override 412 public void startSelected(int which, boolean always, boolean filtered) { 413 super.startSelected(which, always, filtered); 414 415 if (mChooserListAdapter != null) { 416 // Log the index of which type of target the user picked. 417 // Lower values mean the ranking was better. 418 int cat = 0; 419 int value = which; 420 switch (mChooserListAdapter.getPositionTargetType(which)) { 421 case ChooserListAdapter.TARGET_CALLER: 422 cat = MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_APP_TARGET; 423 break; 424 case ChooserListAdapter.TARGET_SERVICE: 425 cat = MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_SERVICE_TARGET; 426 value -= mChooserListAdapter.getCallerTargetCount(); 427 break; 428 case ChooserListAdapter.TARGET_STANDARD: 429 cat = MetricsEvent.ACTION_ACTIVITY_CHOOSER_PICKED_STANDARD_TARGET; 430 value -= mChooserListAdapter.getCallerTargetCount() 431 + mChooserListAdapter.getServiceTargetCount(); 432 break; 433 } 434 435 if (cat != 0) { 436 MetricsLogger.action(this, cat, value); 437 } 438 } 439 } 440 441 void queryTargetServices(ChooserListAdapter adapter) { 442 final PackageManager pm = getPackageManager(); 443 int targetsToQuery = 0; 444 for (int i = 0, N = adapter.getDisplayResolveInfoCount(); i < N; i++) { 445 final DisplayResolveInfo dri = adapter.getDisplayResolveInfo(i); 446 if (adapter.getScore(dri) == 0) { 447 // A score of 0 means the app hasn't been used in some time; 448 // don't query it as it's not likely to be relevant. 449 continue; 450 } 451 final ActivityInfo ai = dri.getResolveInfo().activityInfo; 452 final Bundle md = ai.metaData; 453 final String serviceName = md != null ? convertServiceName(ai.packageName, 454 md.getString(ChooserTargetService.META_DATA_NAME)) : null; 455 if (serviceName != null) { 456 final ComponentName serviceComponent = new ComponentName( 457 ai.packageName, serviceName); 458 final Intent serviceIntent = new Intent(ChooserTargetService.SERVICE_INTERFACE) 459 .setComponent(serviceComponent); 460 461 if (DEBUG) { 462 Log.d(TAG, "queryTargets found target with service " + serviceComponent); 463 } 464 465 try { 466 final String perm = pm.getServiceInfo(serviceComponent, 0).permission; 467 if (!ChooserTargetService.BIND_PERMISSION.equals(perm)) { 468 Log.w(TAG, "ChooserTargetService " + serviceComponent + " does not require" 469 + " permission " + ChooserTargetService.BIND_PERMISSION 470 + " - this service will not be queried for ChooserTargets." 471 + " add android:permission=\"" 472 + ChooserTargetService.BIND_PERMISSION + "\"" 473 + " to the <service> tag for " + serviceComponent 474 + " in the manifest."); 475 continue; 476 } 477 } catch (NameNotFoundException e) { 478 Log.e(TAG, "Could not look up service " + serviceComponent 479 + "; component name not found"); 480 continue; 481 } 482 483 final ChooserTargetServiceConnection conn = 484 new ChooserTargetServiceConnection(this, dri); 485 486 // Explicitly specify Process.myUserHandle instead of calling bindService 487 // to avoid the warning from calling from the system process without an explicit 488 // user handle 489 if (bindServiceAsUser(serviceIntent, conn, BIND_AUTO_CREATE | BIND_NOT_FOREGROUND, 490 Process.myUserHandle())) { 491 if (DEBUG) { 492 Log.d(TAG, "Binding service connection for target " + dri 493 + " intent " + serviceIntent); 494 } 495 mServiceConnections.add(conn); 496 targetsToQuery++; 497 } 498 } 499 if (targetsToQuery >= QUERY_TARGET_SERVICE_LIMIT) { 500 if (DEBUG) Log.d(TAG, "queryTargets hit query target limit " 501 + QUERY_TARGET_SERVICE_LIMIT); 502 break; 503 } 504 } 505 506 if (!mServiceConnections.isEmpty()) { 507 if (DEBUG) Log.d(TAG, "queryTargets setting watchdog timer for " 508 + WATCHDOG_TIMEOUT_MILLIS + "ms"); 509 mChooserHandler.sendEmptyMessageDelayed(CHOOSER_TARGET_SERVICE_WATCHDOG_TIMEOUT, 510 WATCHDOG_TIMEOUT_MILLIS); 511 } else { 512 sendVoiceChoicesIfNeeded(); 513 } 514 } 515 516 private String convertServiceName(String packageName, String serviceName) { 517 if (TextUtils.isEmpty(serviceName)) { 518 return null; 519 } 520 521 final String fullName; 522 if (serviceName.startsWith(".")) { 523 // Relative to the app package. Prepend the app package name. 524 fullName = packageName + serviceName; 525 } else if (serviceName.indexOf('.') >= 0) { 526 // Fully qualified package name. 527 fullName = serviceName; 528 } else { 529 fullName = null; 530 } 531 return fullName; 532 } 533 534 void unbindRemainingServices() { 535 if (DEBUG) { 536 Log.d(TAG, "unbindRemainingServices, " + mServiceConnections.size() + " left"); 537 } 538 for (int i = 0, N = mServiceConnections.size(); i < N; i++) { 539 final ChooserTargetServiceConnection conn = mServiceConnections.get(i); 540 if (DEBUG) Log.d(TAG, "unbinding " + conn); 541 unbindService(conn); 542 conn.destroy(); 543 } 544 mServiceConnections.clear(); 545 mChooserHandler.removeMessages(CHOOSER_TARGET_SERVICE_WATCHDOG_TIMEOUT); 546 } 547 548 public void onSetupVoiceInteraction() { 549 // Do nothing. We'll send the voice stuff ourselves. 550 } 551 552 void updateChooserCounts(TargetInfo info, String annotation) { 553 if (info != null) { 554 UsageStatsManager usageStatsManager = 555 (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE); 556 if (usageStatsManager == null) { 557 if (DEBUG) { 558 Log.d(TAG, "Can not start UsageStatsManager"); 559 } 560 return; 561 } 562 final ResolveInfo ri = info.getResolveInfo(); 563 if (ri != null && ri.activityInfo != null) { 564 usageStatsManager.reportChooserSelection(ri.activityInfo.packageName, getUserId(), 565 annotation, null, info.getResolvedIntent().getAction()); 566 mResolverComparator.updateModel(info.getResolvedComponentName()); 567 if (DEBUG) { 568 Log.d(TAG, "ResolveInfo Package is" + ri.activityInfo.packageName); 569 } 570 } else if(DEBUG) { 571 Log.d(TAG, "Can not log Chooser Counts of null ResovleInfo"); 572 } 573 } 574 } 575 576 void onRefinementResult(TargetInfo selectedTarget, Intent matchingIntent) { 577 if (mRefinementResultReceiver != null) { 578 mRefinementResultReceiver.destroy(); 579 mRefinementResultReceiver = null; 580 } 581 if (selectedTarget == null) { 582 Log.e(TAG, "Refinement result intent did not match any known targets; canceling"); 583 } else if (!checkTargetSourceIntent(selectedTarget, matchingIntent)) { 584 Log.e(TAG, "onRefinementResult: Selected target " + selectedTarget 585 + " cannot match refined source intent " + matchingIntent); 586 } else { 587 TargetInfo clonedTarget = selectedTarget.cloneFilledIn(matchingIntent, 0); 588 if (super.onTargetSelected(clonedTarget, false)) { 589 updateChooserCounts(clonedTarget, mContentType); 590 finish(); 591 return; 592 } 593 } 594 onRefinementCanceled(); 595 } 596 597 void onRefinementCanceled() { 598 if (mRefinementResultReceiver != null) { 599 mRefinementResultReceiver.destroy(); 600 mRefinementResultReceiver = null; 601 } 602 finish(); 603 } 604 605 boolean checkTargetSourceIntent(TargetInfo target, Intent matchingIntent) { 606 final List<Intent> targetIntents = target.getAllSourceIntents(); 607 for (int i = 0, N = targetIntents.size(); i < N; i++) { 608 final Intent targetIntent = targetIntents.get(i); 609 if (targetIntent.filterEquals(matchingIntent)) { 610 return true; 611 } 612 } 613 return false; 614 } 615 616 void filterServiceTargets(String packageName, List<ChooserTarget> targets) { 617 if (targets == null) { 618 return; 619 } 620 621 final PackageManager pm = getPackageManager(); 622 for (int i = targets.size() - 1; i >= 0; i--) { 623 final ChooserTarget target = targets.get(i); 624 final ComponentName targetName = target.getComponentName(); 625 if (packageName != null && packageName.equals(targetName.getPackageName())) { 626 // Anything from the original target's package is fine. 627 continue; 628 } 629 630 boolean remove; 631 try { 632 final ActivityInfo ai = pm.getActivityInfo(targetName, 0); 633 remove = !ai.exported || ai.permission != null; 634 } catch (NameNotFoundException e) { 635 Log.e(TAG, "Target " + target + " returned by " + packageName 636 + " component not found"); 637 remove = true; 638 } 639 640 if (remove) { 641 targets.remove(i); 642 } 643 } 644 } 645 646 @Override 647 public ResolveListAdapter createAdapter(Context context, List<Intent> payloadIntents, 648 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid, 649 boolean filterLastUsed) { 650 final ChooserListAdapter adapter = new ChooserListAdapter(context, payloadIntents, 651 initialIntents, rList, launchedFromUid, filterLastUsed); 652 if (DEBUG) Log.d(TAG, "Adapter created; querying services"); 653 queryTargetServices(adapter); 654 return adapter; 655 } 656 657 final class ChooserTargetInfo implements TargetInfo { 658 private final DisplayResolveInfo mSourceInfo; 659 private final ResolveInfo mBackupResolveInfo; 660 private final ChooserTarget mChooserTarget; 661 private Drawable mBadgeIcon = null; 662 private CharSequence mBadgeContentDescription; 663 private Drawable mDisplayIcon; 664 private final Intent mFillInIntent; 665 private final int mFillInFlags; 666 private final float mModifiedScore; 667 668 public ChooserTargetInfo(DisplayResolveInfo sourceInfo, ChooserTarget chooserTarget, 669 float modifiedScore) { 670 mSourceInfo = sourceInfo; 671 mChooserTarget = chooserTarget; 672 mModifiedScore = modifiedScore; 673 if (sourceInfo != null) { 674 final ResolveInfo ri = sourceInfo.getResolveInfo(); 675 if (ri != null) { 676 final ActivityInfo ai = ri.activityInfo; 677 if (ai != null && ai.applicationInfo != null) { 678 final PackageManager pm = getPackageManager(); 679 mBadgeIcon = pm.getApplicationIcon(ai.applicationInfo); 680 mBadgeContentDescription = pm.getApplicationLabel(ai.applicationInfo); 681 } 682 } 683 } 684 final Icon icon = chooserTarget.getIcon(); 685 // TODO do this in the background 686 mDisplayIcon = icon != null ? icon.loadDrawable(ChooserActivity.this) : null; 687 688 if (sourceInfo != null) { 689 mBackupResolveInfo = null; 690 } else { 691 mBackupResolveInfo = getPackageManager().resolveActivity(getResolvedIntent(), 0); 692 } 693 694 mFillInIntent = null; 695 mFillInFlags = 0; 696 } 697 698 private ChooserTargetInfo(ChooserTargetInfo other, Intent fillInIntent, int flags) { 699 mSourceInfo = other.mSourceInfo; 700 mBackupResolveInfo = other.mBackupResolveInfo; 701 mChooserTarget = other.mChooserTarget; 702 mBadgeIcon = other.mBadgeIcon; 703 mBadgeContentDescription = other.mBadgeContentDescription; 704 mDisplayIcon = other.mDisplayIcon; 705 mFillInIntent = fillInIntent; 706 mFillInFlags = flags; 707 mModifiedScore = other.mModifiedScore; 708 } 709 710 public float getModifiedScore() { 711 return mModifiedScore; 712 } 713 714 @Override 715 public Intent getResolvedIntent() { 716 if (mSourceInfo != null) { 717 return mSourceInfo.getResolvedIntent(); 718 } 719 720 final Intent targetIntent = new Intent(getTargetIntent()); 721 targetIntent.setComponent(mChooserTarget.getComponentName()); 722 targetIntent.putExtras(mChooserTarget.getIntentExtras()); 723 return targetIntent; 724 } 725 726 @Override 727 public ComponentName getResolvedComponentName() { 728 if (mSourceInfo != null) { 729 return mSourceInfo.getResolvedComponentName(); 730 } else if (mBackupResolveInfo != null) { 731 return new ComponentName(mBackupResolveInfo.activityInfo.packageName, 732 mBackupResolveInfo.activityInfo.name); 733 } 734 return null; 735 } 736 737 private Intent getBaseIntentToSend() { 738 Intent result = getResolvedIntent(); 739 if (result == null) { 740 Log.e(TAG, "ChooserTargetInfo: no base intent available to send"); 741 } else { 742 result = new Intent(result); 743 if (mFillInIntent != null) { 744 result.fillIn(mFillInIntent, mFillInFlags); 745 } 746 result.fillIn(mReferrerFillInIntent, 0); 747 } 748 return result; 749 } 750 751 @Override 752 public boolean start(Activity activity, Bundle options) { 753 throw new RuntimeException("ChooserTargets should be started as caller."); 754 } 755 756 @Override 757 public boolean startAsCaller(Activity activity, Bundle options, int userId) { 758 final Intent intent = getBaseIntentToSend(); 759 if (intent == null) { 760 return false; 761 } 762 intent.setComponent(mChooserTarget.getComponentName()); 763 intent.putExtras(mChooserTarget.getIntentExtras()); 764 765 // Important: we will ignore the target security checks in ActivityManager 766 // if and only if the ChooserTarget's target package is the same package 767 // where we got the ChooserTargetService that provided it. This lets a 768 // ChooserTargetService provide a non-exported or permission-guarded target 769 // to the chooser for the user to pick. 770 // 771 // If mSourceInfo is null, we got this ChooserTarget from the caller or elsewhere 772 // so we'll obey the caller's normal security checks. 773 final boolean ignoreTargetSecurity = mSourceInfo != null 774 && mSourceInfo.getResolvedComponentName().getPackageName() 775 .equals(mChooserTarget.getComponentName().getPackageName()); 776 activity.startActivityAsCaller(intent, options, ignoreTargetSecurity, userId); 777 return true; 778 } 779 780 @Override 781 public boolean startAsUser(Activity activity, Bundle options, UserHandle user) { 782 throw new RuntimeException("ChooserTargets should be started as caller."); 783 } 784 785 @Override 786 public ResolveInfo getResolveInfo() { 787 return mSourceInfo != null ? mSourceInfo.getResolveInfo() : mBackupResolveInfo; 788 } 789 790 @Override 791 public CharSequence getDisplayLabel() { 792 return mChooserTarget.getTitle(); 793 } 794 795 @Override 796 public CharSequence getExtendedInfo() { 797 // ChooserTargets have badge icons, so we won't show the extended info to disambiguate. 798 return null; 799 } 800 801 @Override 802 public Drawable getDisplayIcon() { 803 return mDisplayIcon; 804 } 805 806 @Override 807 public Drawable getBadgeIcon() { 808 return mBadgeIcon; 809 } 810 811 @Override 812 public CharSequence getBadgeContentDescription() { 813 return mBadgeContentDescription; 814 } 815 816 @Override 817 public TargetInfo cloneFilledIn(Intent fillInIntent, int flags) { 818 return new ChooserTargetInfo(this, fillInIntent, flags); 819 } 820 821 @Override 822 public List<Intent> getAllSourceIntents() { 823 final List<Intent> results = new ArrayList<>(); 824 if (mSourceInfo != null) { 825 // We only queried the service for the first one in our sourceinfo. 826 results.add(mSourceInfo.getAllSourceIntents().get(0)); 827 } 828 return results; 829 } 830 831 @Override 832 public boolean isPinned() { 833 return mSourceInfo != null ? mSourceInfo.isPinned() : false; 834 } 835 } 836 837 public class ChooserListAdapter extends ResolveListAdapter { 838 public static final int TARGET_BAD = -1; 839 public static final int TARGET_CALLER = 0; 840 public static final int TARGET_SERVICE = 1; 841 public static final int TARGET_STANDARD = 2; 842 843 private static final int MAX_SERVICE_TARGETS = 8; 844 private static final int MAX_TARGETS_PER_SERVICE = 4; 845 846 private final List<ChooserTargetInfo> mServiceTargets = new ArrayList<>(); 847 private final List<TargetInfo> mCallerTargets = new ArrayList<>(); 848 private boolean mShowServiceTargets; 849 850 private float mLateFee = 1.f; 851 852 private final BaseChooserTargetComparator mBaseTargetComparator 853 = new BaseChooserTargetComparator(); 854 855 public ChooserListAdapter(Context context, List<Intent> payloadIntents, 856 Intent[] initialIntents, List<ResolveInfo> rList, int launchedFromUid, 857 boolean filterLastUsed) { 858 // Don't send the initial intents through the shared ResolverActivity path, 859 // we want to separate them into a different section. 860 super(context, payloadIntents, null, rList, launchedFromUid, filterLastUsed); 861 862 if (initialIntents != null) { 863 final PackageManager pm = getPackageManager(); 864 for (int i = 0; i < initialIntents.length; i++) { 865 final Intent ii = initialIntents[i]; 866 if (ii == null) { 867 continue; 868 } 869 870 // We reimplement Intent#resolveActivityInfo here because if we have an 871 // implicit intent, we want the ResolveInfo returned by PackageManager 872 // instead of one we reconstruct ourselves. The ResolveInfo returned might 873 // have extra metadata and resolvePackageName set and we want to respect that. 874 ResolveInfo ri = null; 875 ActivityInfo ai = null; 876 final ComponentName cn = ii.getComponent(); 877 if (cn != null) { 878 try { 879 ai = pm.getActivityInfo(ii.getComponent(), 0); 880 ri = new ResolveInfo(); 881 ri.activityInfo = ai; 882 } catch (PackageManager.NameNotFoundException ignored) { 883 // ai will == null below 884 } 885 } 886 if (ai == null) { 887 ri = pm.resolveActivity(ii, PackageManager.MATCH_DEFAULT_ONLY); 888 ai = ri != null ? ri.activityInfo : null; 889 } 890 if (ai == null) { 891 Log.w(TAG, "No activity found for " + ii); 892 continue; 893 } 894 UserManager userManager = 895 (UserManager) getSystemService(Context.USER_SERVICE); 896 if (ii instanceof LabeledIntent) { 897 LabeledIntent li = (LabeledIntent)ii; 898 ri.resolvePackageName = li.getSourcePackage(); 899 ri.labelRes = li.getLabelResource(); 900 ri.nonLocalizedLabel = li.getNonLocalizedLabel(); 901 ri.icon = li.getIconResource(); 902 ri.iconResourceId = ri.icon; 903 } 904 if (userManager.isManagedProfile()) { 905 ri.noResourceId = true; 906 ri.icon = 0; 907 } 908 mCallerTargets.add(new DisplayResolveInfo(ii, ri, 909 ri.loadLabel(pm), null, ii)); 910 } 911 } 912 } 913 914 @Override 915 public boolean showsExtendedInfo(TargetInfo info) { 916 // We have badges so we don't need this text shown. 917 return false; 918 } 919 920 @Override 921 public boolean isComponentPinned(ComponentName name) { 922 return mPinnedSharedPrefs.getBoolean(name.flattenToString(), false); 923 } 924 925 @Override 926 public float getScore(DisplayResolveInfo target) { 927 if (target == null) { 928 return CALLER_TARGET_SCORE_BOOST; 929 } 930 float score = super.getScore(target); 931 if (target.isPinned()) { 932 score += PINNED_TARGET_SCORE_BOOST; 933 } 934 return score; 935 } 936 937 @Override 938 public View onCreateView(ViewGroup parent) { 939 return mInflater.inflate( 940 com.android.internal.R.layout.resolve_grid_item, parent, false); 941 } 942 943 @Override 944 public void onListRebuilt() { 945 if (mServiceTargets != null) { 946 pruneServiceTargets(); 947 } 948 } 949 950 @Override 951 public boolean shouldGetResolvedFilter() { 952 return true; 953 } 954 955 @Override 956 public int getCount() { 957 return super.getCount() + getServiceTargetCount() + getCallerTargetCount(); 958 } 959 960 @Override 961 public int getUnfilteredCount() { 962 return super.getUnfilteredCount() + getServiceTargetCount() + getCallerTargetCount(); 963 } 964 965 public int getCallerTargetCount() { 966 return mCallerTargets.size(); 967 } 968 969 public int getServiceTargetCount() { 970 if (!mShowServiceTargets) { 971 return 0; 972 } 973 return Math.min(mServiceTargets.size(), MAX_SERVICE_TARGETS); 974 } 975 976 public int getStandardTargetCount() { 977 return super.getCount(); 978 } 979 980 public int getPositionTargetType(int position) { 981 int offset = 0; 982 983 final int callerTargetCount = getCallerTargetCount(); 984 if (position < callerTargetCount) { 985 return TARGET_CALLER; 986 } 987 offset += callerTargetCount; 988 989 final int serviceTargetCount = getServiceTargetCount(); 990 if (position - offset < serviceTargetCount) { 991 return TARGET_SERVICE; 992 } 993 offset += serviceTargetCount; 994 995 final int standardTargetCount = super.getCount(); 996 if (position - offset < standardTargetCount) { 997 return TARGET_STANDARD; 998 } 999 1000 return TARGET_BAD; 1001 } 1002 1003 @Override 1004 public TargetInfo getItem(int position) { 1005 return targetInfoForPosition(position, true); 1006 } 1007 1008 @Override 1009 public TargetInfo targetInfoForPosition(int position, boolean filtered) { 1010 int offset = 0; 1011 1012 final int callerTargetCount = getCallerTargetCount(); 1013 if (position < callerTargetCount) { 1014 return mCallerTargets.get(position); 1015 } 1016 offset += callerTargetCount; 1017 1018 final int serviceTargetCount = getServiceTargetCount(); 1019 if (position - offset < serviceTargetCount) { 1020 return mServiceTargets.get(position - offset); 1021 } 1022 offset += serviceTargetCount; 1023 1024 return filtered ? super.getItem(position - offset) 1025 : getDisplayInfoAt(position - offset); 1026 } 1027 1028 public void addServiceResults(DisplayResolveInfo origTarget, List<ChooserTarget> targets) { 1029 if (DEBUG) Log.d(TAG, "addServiceResults " + origTarget + ", " + targets.size() 1030 + " targets"); 1031 final float parentScore = getScore(origTarget); 1032 Collections.sort(targets, mBaseTargetComparator); 1033 float lastScore = 0; 1034 for (int i = 0, N = Math.min(targets.size(), MAX_TARGETS_PER_SERVICE); i < N; i++) { 1035 final ChooserTarget target = targets.get(i); 1036 float targetScore = target.getScore(); 1037 targetScore *= parentScore; 1038 targetScore *= mLateFee; 1039 if (i > 0 && targetScore >= lastScore) { 1040 // Apply a decay so that the top app can't crowd out everything else. 1041 // This incents ChooserTargetServices to define what's truly better. 1042 targetScore = lastScore * 0.95f; 1043 } 1044 insertServiceTarget(new ChooserTargetInfo(origTarget, target, targetScore)); 1045 1046 if (DEBUG) { 1047 Log.d(TAG, " => " + target.toString() + " score=" + targetScore 1048 + " base=" + target.getScore() 1049 + " lastScore=" + lastScore 1050 + " parentScore=" + parentScore 1051 + " lateFee=" + mLateFee); 1052 } 1053 1054 lastScore = targetScore; 1055 } 1056 1057 mLateFee *= 0.95f; 1058 1059 notifyDataSetChanged(); 1060 } 1061 1062 /** 1063 * Set to true to reveal all service targets at once. 1064 */ 1065 public void setShowServiceTargets(boolean show) { 1066 mShowServiceTargets = show; 1067 notifyDataSetChanged(); 1068 } 1069 1070 private void insertServiceTarget(ChooserTargetInfo chooserTargetInfo) { 1071 final float newScore = chooserTargetInfo.getModifiedScore(); 1072 for (int i = 0, N = mServiceTargets.size(); i < N; i++) { 1073 final ChooserTargetInfo serviceTarget = mServiceTargets.get(i); 1074 if (newScore > serviceTarget.getModifiedScore()) { 1075 mServiceTargets.add(i, chooserTargetInfo); 1076 return; 1077 } 1078 } 1079 mServiceTargets.add(chooserTargetInfo); 1080 } 1081 1082 private void pruneServiceTargets() { 1083 if (DEBUG) Log.d(TAG, "pruneServiceTargets"); 1084 for (int i = mServiceTargets.size() - 1; i >= 0; i--) { 1085 final ChooserTargetInfo cti = mServiceTargets.get(i); 1086 if (!hasResolvedTarget(cti.getResolveInfo())) { 1087 if (DEBUG) Log.d(TAG, " => " + i + " " + cti); 1088 mServiceTargets.remove(i); 1089 } 1090 } 1091 } 1092 } 1093 1094 static class BaseChooserTargetComparator implements Comparator<ChooserTarget> { 1095 @Override 1096 public int compare(ChooserTarget lhs, ChooserTarget rhs) { 1097 // Descending order 1098 return (int) Math.signum(rhs.getScore() - lhs.getScore()); 1099 } 1100 } 1101 1102 static class RowScale { 1103 private static final int DURATION = 400; 1104 1105 float mScale; 1106 ChooserRowAdapter mAdapter; 1107 private final ObjectAnimator mAnimator; 1108 1109 public static final FloatProperty<RowScale> PROPERTY = 1110 new FloatProperty<RowScale>("scale") { 1111 @Override 1112 public void setValue(RowScale object, float value) { 1113 object.mScale = value; 1114 object.mAdapter.notifyDataSetChanged(); 1115 } 1116 1117 @Override 1118 public Float get(RowScale object) { 1119 return object.mScale; 1120 } 1121 }; 1122 1123 public RowScale(@NonNull ChooserRowAdapter adapter, float from, float to) { 1124 mAdapter = adapter; 1125 mScale = from; 1126 if (from == to) { 1127 mAnimator = null; 1128 return; 1129 } 1130 1131 mAnimator = ObjectAnimator.ofFloat(this, PROPERTY, from, to).setDuration(DURATION); 1132 } 1133 1134 public RowScale setInterpolator(Interpolator interpolator) { 1135 if (mAnimator != null) { 1136 mAnimator.setInterpolator(interpolator); 1137 } 1138 return this; 1139 } 1140 1141 public float get() { 1142 return mScale; 1143 } 1144 1145 public void startAnimation() { 1146 if (mAnimator != null) { 1147 mAnimator.start(); 1148 } 1149 } 1150 1151 public void cancelAnimation() { 1152 if (mAnimator != null) { 1153 mAnimator.cancel(); 1154 } 1155 } 1156 } 1157 1158 class ChooserRowAdapter extends BaseAdapter { 1159 private ChooserListAdapter mChooserListAdapter; 1160 private final LayoutInflater mLayoutInflater; 1161 private final int mColumnCount = 4; 1162 private RowScale[] mServiceTargetScale; 1163 private final Interpolator mInterpolator; 1164 1165 public ChooserRowAdapter(ChooserListAdapter wrappedAdapter) { 1166 mChooserListAdapter = wrappedAdapter; 1167 mLayoutInflater = LayoutInflater.from(ChooserActivity.this); 1168 1169 mInterpolator = AnimationUtils.loadInterpolator(ChooserActivity.this, 1170 android.R.interpolator.decelerate_quint); 1171 1172 wrappedAdapter.registerDataSetObserver(new DataSetObserver() { 1173 @Override 1174 public void onChanged() { 1175 super.onChanged(); 1176 final int rcount = getServiceTargetRowCount(); 1177 if (mServiceTargetScale == null 1178 || mServiceTargetScale.length != rcount) { 1179 RowScale[] old = mServiceTargetScale; 1180 int oldRCount = old != null ? old.length : 0; 1181 mServiceTargetScale = new RowScale[rcount]; 1182 if (old != null && rcount > 0) { 1183 System.arraycopy(old, 0, mServiceTargetScale, 0, 1184 Math.min(old.length, rcount)); 1185 } 1186 1187 for (int i = rcount; i < oldRCount; i++) { 1188 old[i].cancelAnimation(); 1189 } 1190 1191 for (int i = oldRCount; i < rcount; i++) { 1192 final RowScale rs = new RowScale(ChooserRowAdapter.this, 0.f, 1.f) 1193 .setInterpolator(mInterpolator); 1194 mServiceTargetScale[i] = rs; 1195 } 1196 1197 // Start the animations in a separate loop. 1198 // The process of starting animations will result in 1199 // binding views to set up initial values, and we must 1200 // have ALL of the new RowScale objects created above before 1201 // we get started. 1202 for (int i = oldRCount; i < rcount; i++) { 1203 mServiceTargetScale[i].startAnimation(); 1204 } 1205 } 1206 1207 notifyDataSetChanged(); 1208 } 1209 1210 @Override 1211 public void onInvalidated() { 1212 super.onInvalidated(); 1213 notifyDataSetInvalidated(); 1214 if (mServiceTargetScale != null) { 1215 for (RowScale rs : mServiceTargetScale) { 1216 rs.cancelAnimation(); 1217 } 1218 } 1219 } 1220 }); 1221 } 1222 1223 private float getRowScale(int rowPosition) { 1224 final int start = getCallerTargetRowCount(); 1225 final int end = start + getServiceTargetRowCount(); 1226 if (rowPosition >= start && rowPosition < end) { 1227 return mServiceTargetScale[rowPosition - start].get(); 1228 } 1229 return 1.f; 1230 } 1231 1232 @Override 1233 public int getCount() { 1234 return (int) ( 1235 getCallerTargetRowCount() 1236 + getServiceTargetRowCount() 1237 + Math.ceil((float) mChooserListAdapter.getStandardTargetCount() / mColumnCount) 1238 ); 1239 } 1240 1241 public int getCallerTargetRowCount() { 1242 return (int) Math.ceil( 1243 (float) mChooserListAdapter.getCallerTargetCount() / mColumnCount); 1244 } 1245 1246 public int getServiceTargetRowCount() { 1247 return (int) Math.ceil( 1248 (float) mChooserListAdapter.getServiceTargetCount() / mColumnCount); 1249 } 1250 1251 @Override 1252 public Object getItem(int position) { 1253 // We have nothing useful to return here. 1254 return position; 1255 } 1256 1257 @Override 1258 public long getItemId(int position) { 1259 return position; 1260 } 1261 1262 @Override 1263 public View getView(int position, View convertView, ViewGroup parent) { 1264 final RowViewHolder holder; 1265 if (convertView == null) { 1266 holder = createViewHolder(parent); 1267 } else { 1268 holder = (RowViewHolder) convertView.getTag(); 1269 } 1270 bindViewHolder(position, holder); 1271 1272 return holder.row; 1273 } 1274 1275 RowViewHolder createViewHolder(ViewGroup parent) { 1276 final ViewGroup row = (ViewGroup) mLayoutInflater.inflate(R.layout.chooser_row, 1277 parent, false); 1278 final RowViewHolder holder = new RowViewHolder(row, mColumnCount); 1279 final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 1280 1281 for (int i = 0; i < mColumnCount; i++) { 1282 final View v = mChooserListAdapter.createView(row); 1283 final int column = i; 1284 v.setOnClickListener(new OnClickListener() { 1285 @Override 1286 public void onClick(View v) { 1287 startSelected(holder.itemIndices[column], false, true); 1288 } 1289 }); 1290 v.setOnLongClickListener(new OnLongClickListener() { 1291 @Override 1292 public boolean onLongClick(View v) { 1293 showTargetDetails( 1294 mChooserListAdapter.resolveInfoForPosition( 1295 holder.itemIndices[column], true)); 1296 return true; 1297 } 1298 }); 1299 row.addView(v); 1300 holder.cells[i] = v; 1301 1302 // Force height to be a given so we don't have visual disruption during scaling. 1303 LayoutParams lp = v.getLayoutParams(); 1304 v.measure(spec, spec); 1305 if (lp == null) { 1306 lp = new LayoutParams(LayoutParams.MATCH_PARENT, v.getMeasuredHeight()); 1307 row.setLayoutParams(lp); 1308 } else { 1309 lp.height = v.getMeasuredHeight(); 1310 } 1311 } 1312 1313 // Pre-measure so we can scale later. 1314 holder.measure(); 1315 LayoutParams lp = row.getLayoutParams(); 1316 if (lp == null) { 1317 lp = new LayoutParams(LayoutParams.MATCH_PARENT, holder.measuredRowHeight); 1318 row.setLayoutParams(lp); 1319 } else { 1320 lp.height = holder.measuredRowHeight; 1321 } 1322 row.setTag(holder); 1323 return holder; 1324 } 1325 1326 void bindViewHolder(int rowPosition, RowViewHolder holder) { 1327 final int start = getFirstRowPosition(rowPosition); 1328 final int startType = mChooserListAdapter.getPositionTargetType(start); 1329 1330 int end = start + mColumnCount - 1; 1331 while (mChooserListAdapter.getPositionTargetType(end) != startType && end >= start) { 1332 end--; 1333 } 1334 1335 if (startType == ChooserListAdapter.TARGET_SERVICE) { 1336 holder.row.setBackgroundColor( 1337 getColor(R.color.chooser_service_row_background_color)); 1338 } else { 1339 holder.row.setBackgroundColor(Color.TRANSPARENT); 1340 } 1341 1342 final int oldHeight = holder.row.getLayoutParams().height; 1343 holder.row.getLayoutParams().height = Math.max(1, 1344 (int) (holder.measuredRowHeight * getRowScale(rowPosition))); 1345 if (holder.row.getLayoutParams().height != oldHeight) { 1346 holder.row.requestLayout(); 1347 } 1348 1349 for (int i = 0; i < mColumnCount; i++) { 1350 final View v = holder.cells[i]; 1351 if (start + i <= end) { 1352 v.setVisibility(View.VISIBLE); 1353 holder.itemIndices[i] = start + i; 1354 mChooserListAdapter.bindView(holder.itemIndices[i], v); 1355 } else { 1356 v.setVisibility(View.GONE); 1357 } 1358 } 1359 } 1360 1361 int getFirstRowPosition(int row) { 1362 final int callerCount = mChooserListAdapter.getCallerTargetCount(); 1363 final int callerRows = (int) Math.ceil((float) callerCount / mColumnCount); 1364 1365 if (row < callerRows) { 1366 return row * mColumnCount; 1367 } 1368 1369 final int serviceCount = mChooserListAdapter.getServiceTargetCount(); 1370 final int serviceRows = (int) Math.ceil((float) serviceCount / mColumnCount); 1371 1372 if (row < callerRows + serviceRows) { 1373 return callerCount + (row - callerRows) * mColumnCount; 1374 } 1375 1376 return callerCount + serviceCount 1377 + (row - callerRows - serviceRows) * mColumnCount; 1378 } 1379 } 1380 1381 static class RowViewHolder { 1382 final View[] cells; 1383 final ViewGroup row; 1384 int measuredRowHeight; 1385 int[] itemIndices; 1386 1387 public RowViewHolder(ViewGroup row, int cellCount) { 1388 this.row = row; 1389 this.cells = new View[cellCount]; 1390 this.itemIndices = new int[cellCount]; 1391 } 1392 1393 public void measure() { 1394 final int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 1395 row.measure(spec, spec); 1396 measuredRowHeight = row.getMeasuredHeight(); 1397 } 1398 } 1399 1400 static class ChooserTargetServiceConnection implements ServiceConnection { 1401 private DisplayResolveInfo mOriginalTarget; 1402 private ComponentName mConnectedComponent; 1403 private ChooserActivity mChooserActivity; 1404 private final Object mLock = new Object(); 1405 1406 private final IChooserTargetResult mChooserTargetResult = new IChooserTargetResult.Stub() { 1407 @Override 1408 public void sendResult(List<ChooserTarget> targets) throws RemoteException { 1409 synchronized (mLock) { 1410 if (mChooserActivity == null) { 1411 Log.e(TAG, "destroyed ChooserTargetServiceConnection received result from " 1412 + mConnectedComponent + "; ignoring..."); 1413 return; 1414 } 1415 mChooserActivity.filterServiceTargets( 1416 mOriginalTarget.getResolveInfo().activityInfo.packageName, targets); 1417 final Message msg = Message.obtain(); 1418 msg.what = CHOOSER_TARGET_SERVICE_RESULT; 1419 msg.obj = new ServiceResultInfo(mOriginalTarget, targets, 1420 ChooserTargetServiceConnection.this); 1421 mChooserActivity.mChooserHandler.sendMessage(msg); 1422 } 1423 } 1424 }; 1425 1426 public ChooserTargetServiceConnection(ChooserActivity chooserActivity, 1427 DisplayResolveInfo dri) { 1428 mChooserActivity = chooserActivity; 1429 mOriginalTarget = dri; 1430 } 1431 1432 @Override 1433 public void onServiceConnected(ComponentName name, IBinder service) { 1434 if (DEBUG) Log.d(TAG, "onServiceConnected: " + name); 1435 synchronized (mLock) { 1436 if (mChooserActivity == null) { 1437 Log.e(TAG, "destroyed ChooserTargetServiceConnection got onServiceConnected"); 1438 return; 1439 } 1440 1441 final IChooserTargetService icts = IChooserTargetService.Stub.asInterface(service); 1442 try { 1443 icts.getChooserTargets(mOriginalTarget.getResolvedComponentName(), 1444 mOriginalTarget.getResolveInfo().filter, mChooserTargetResult); 1445 } catch (RemoteException e) { 1446 Log.e(TAG, "Querying ChooserTargetService " + name + " failed.", e); 1447 mChooserActivity.unbindService(this); 1448 destroy(); 1449 mChooserActivity.mServiceConnections.remove(this); 1450 } 1451 } 1452 } 1453 1454 @Override 1455 public void onServiceDisconnected(ComponentName name) { 1456 if (DEBUG) Log.d(TAG, "onServiceDisconnected: " + name); 1457 synchronized (mLock) { 1458 if (mChooserActivity == null) { 1459 Log.e(TAG, 1460 "destroyed ChooserTargetServiceConnection got onServiceDisconnected"); 1461 return; 1462 } 1463 1464 mChooserActivity.unbindService(this); 1465 destroy(); 1466 mChooserActivity.mServiceConnections.remove(this); 1467 if (mChooserActivity.mServiceConnections.isEmpty()) { 1468 mChooserActivity.mChooserHandler.removeMessages( 1469 CHOOSER_TARGET_SERVICE_WATCHDOG_TIMEOUT); 1470 mChooserActivity.sendVoiceChoicesIfNeeded(); 1471 } 1472 mConnectedComponent = null; 1473 } 1474 } 1475 1476 public void destroy() { 1477 synchronized (mLock) { 1478 mChooserActivity = null; 1479 mOriginalTarget = null; 1480 } 1481 } 1482 1483 @Override 1484 public String toString() { 1485 return "ChooserTargetServiceConnection{service=" 1486 + mConnectedComponent + ", activity=" 1487 + (mOriginalTarget != null 1488 ? mOriginalTarget.getResolveInfo().activityInfo.toString() 1489 : "<connection destroyed>") + "}"; 1490 } 1491 } 1492 1493 static class ServiceResultInfo { 1494 public final DisplayResolveInfo originalTarget; 1495 public final List<ChooserTarget> resultTargets; 1496 public final ChooserTargetServiceConnection connection; 1497 1498 public ServiceResultInfo(DisplayResolveInfo ot, List<ChooserTarget> rt, 1499 ChooserTargetServiceConnection c) { 1500 originalTarget = ot; 1501 resultTargets = rt; 1502 connection = c; 1503 } 1504 } 1505 1506 static class RefinementResultReceiver extends ResultReceiver { 1507 private ChooserActivity mChooserActivity; 1508 private TargetInfo mSelectedTarget; 1509 1510 public RefinementResultReceiver(ChooserActivity host, TargetInfo target, 1511 Handler handler) { 1512 super(handler); 1513 mChooserActivity = host; 1514 mSelectedTarget = target; 1515 } 1516 1517 @Override 1518 protected void onReceiveResult(int resultCode, Bundle resultData) { 1519 if (mChooserActivity == null) { 1520 Log.e(TAG, "Destroyed RefinementResultReceiver received a result"); 1521 return; 1522 } 1523 if (resultData == null) { 1524 Log.e(TAG, "RefinementResultReceiver received null resultData"); 1525 return; 1526 } 1527 1528 switch (resultCode) { 1529 case RESULT_CANCELED: 1530 mChooserActivity.onRefinementCanceled(); 1531 break; 1532 case RESULT_OK: 1533 Parcelable intentParcelable = resultData.getParcelable(Intent.EXTRA_INTENT); 1534 if (intentParcelable instanceof Intent) { 1535 mChooserActivity.onRefinementResult(mSelectedTarget, 1536 (Intent) intentParcelable); 1537 } else { 1538 Log.e(TAG, "RefinementResultReceiver received RESULT_OK but no Intent" 1539 + " in resultData with key Intent.EXTRA_INTENT"); 1540 } 1541 break; 1542 default: 1543 Log.w(TAG, "Unknown result code " + resultCode 1544 + " sent to RefinementResultReceiver"); 1545 break; 1546 } 1547 } 1548 1549 public void destroy() { 1550 mChooserActivity = null; 1551 mSelectedTarget = null; 1552 } 1553 } 1554 1555 class OffsetDataSetObserver extends DataSetObserver { 1556 private final AbsListView mListView; 1557 private int mCachedViewType = -1; 1558 private View mCachedView; 1559 1560 public OffsetDataSetObserver(AbsListView listView) { 1561 mListView = listView; 1562 } 1563 1564 @Override 1565 public void onChanged() { 1566 if (mResolverDrawerLayout == null) { 1567 return; 1568 } 1569 1570 final int chooserTargetRows = mChooserRowAdapter.getServiceTargetRowCount(); 1571 int offset = 0; 1572 for (int i = 0; i < chooserTargetRows; i++) { 1573 final int pos = mChooserRowAdapter.getCallerTargetRowCount() + i; 1574 final int vt = mChooserRowAdapter.getItemViewType(pos); 1575 if (vt != mCachedViewType) { 1576 mCachedView = null; 1577 } 1578 final View v = mChooserRowAdapter.getView(pos, mCachedView, mListView); 1579 int height = ((RowViewHolder) (v.getTag())).measuredRowHeight; 1580 1581 offset += (int) (height * mChooserRowAdapter.getRowScale(pos)); 1582 1583 if (vt >= 0) { 1584 mCachedViewType = vt; 1585 mCachedView = v; 1586 } else { 1587 mCachedViewType = -1; 1588 } 1589 } 1590 1591 mResolverDrawerLayout.setCollapsibleHeightReserved(offset); 1592 } 1593 } 1594} 1595