1 /* 2 * Copyright (C) 2021 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 package com.android.launcher3.uioverrides; 17 18 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_APP_LAUNCH_TAP; 19 import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_SPLIT_WIDGET_ATTEMPT; 20 21 import android.app.ActivityOptions; 22 import android.app.ActivityTaskManager; 23 import android.app.PendingIntent; 24 import android.content.Intent; 25 import android.os.RemoteException; 26 import android.util.Log; 27 import android.util.Pair; 28 import android.view.View; 29 import android.widget.RemoteViews; 30 import android.window.SplashScreen; 31 32 import com.android.launcher3.logging.StatsLogManager; 33 import com.android.launcher3.model.data.ItemInfo; 34 import com.android.launcher3.util.ActivityOptionsWrapper; 35 import com.android.launcher3.widget.LauncherAppWidgetHostView; 36 37 import java.util.function.Consumer; 38 39 /** Provides a Quickstep specific animation when launching an activity from an app widget. */ 40 class QuickstepInteractionHandler implements RemoteViews.InteractionHandler, 41 Consumer<LauncherAppWidgetHostView> { 42 43 private static final String TAG = "QuickstepInteractionHandler"; 44 45 private final QuickstepLauncher mLauncher; 46 QuickstepInteractionHandler(QuickstepLauncher launcher)47 QuickstepInteractionHandler(QuickstepLauncher launcher) { 48 mLauncher = launcher; 49 } 50 51 @Override accept(LauncherAppWidgetHostView host)52 public void accept(LauncherAppWidgetHostView host) { 53 host.setInteractionHandler(this); 54 } 55 56 @SuppressWarnings("NewApi") 57 @Override onInteraction(View view, PendingIntent pendingIntent, RemoteViews.RemoteResponse remoteResponse)58 public boolean onInteraction(View view, PendingIntent pendingIntent, 59 RemoteViews.RemoteResponse remoteResponse) { 60 LauncherAppWidgetHostView hostView = findHostViewAncestor(view); 61 if (hostView == null) { 62 Log.e(TAG, "View did not have a LauncherAppWidgetHostView ancestor."); 63 return RemoteViews.startPendingIntent(hostView, pendingIntent, 64 remoteResponse.getLaunchOptions(view)); 65 } 66 if (mLauncher.isSplitSelectionActive()) { 67 // Log metric 68 StatsLogManager.StatsLogger logger = mLauncher.getStatsLogManager().logger(); 69 logger.log(LAUNCHER_SPLIT_WIDGET_ATTEMPT); 70 mLauncher.handleIncorrectSplitTargetSelection(); 71 return true; 72 } 73 Pair<Intent, ActivityOptions> options = remoteResponse.getLaunchOptions(view); 74 ActivityOptionsWrapper activityOptions = mLauncher.getAppTransitionManager() 75 .getActivityLaunchOptions(hostView, (ItemInfo) hostView.getTag()); 76 if (!pendingIntent.isActivity()) { 77 // In the event this pending intent eventually launches an activity, i.e. a trampoline, 78 // use the Quickstep transition animation. 79 try { 80 ActivityTaskManager.getService() 81 .registerRemoteAnimationForNextActivityStart( 82 pendingIntent.getCreatorPackage(), 83 activityOptions.options.getRemoteAnimationAdapter(), 84 activityOptions.options.getLaunchCookie()); 85 } catch (RemoteException e) { 86 // Do nothing. 87 } 88 } 89 activityOptions.options.setPendingIntentLaunchFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 90 activityOptions.options.setSplashScreenStyle(SplashScreen.SPLASH_SCREEN_STYLE_SOLID_COLOR); 91 activityOptions.options.setPendingIntentBackgroundActivityStartMode( 92 ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED); 93 options = Pair.create(options.first, activityOptions.options); 94 if (pendingIntent.isActivity()) { 95 logAppLaunch(hostView.getTag()); 96 } 97 return RemoteViews.startPendingIntent(hostView, pendingIntent, options); 98 } 99 100 /** 101 * Logs that the app was launched from the widget. 102 * @param itemInfo the widget info. 103 */ logAppLaunch(Object itemInfo)104 private void logAppLaunch(Object itemInfo) { 105 StatsLogManager.StatsLogger logger = mLauncher.getStatsLogManager().logger(); 106 if (itemInfo instanceof ItemInfo) { 107 logger.withItemInfo((ItemInfo) itemInfo); 108 } 109 logger.log(LAUNCHER_APP_LAUNCH_TAP); 110 } 111 findHostViewAncestor(View v)112 private LauncherAppWidgetHostView findHostViewAncestor(View v) { 113 while (v != null) { 114 if (v instanceof LauncherAppWidgetHostView) return (LauncherAppWidgetHostView) v; 115 v = (View) v.getParent(); 116 } 117 return null; 118 } 119 } 120