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.IBinder;
26 import android.os.RemoteException;
27 import android.util.Log;
28 import android.util.Pair;
29 import android.view.View;
30 import android.widget.RemoteViews;
31 import android.window.SplashScreen;
32 
33 import com.android.launcher3.Utilities;
34 import com.android.launcher3.logging.StatsLogManager;
35 import com.android.launcher3.model.data.ItemInfo;
36 import com.android.launcher3.util.ActivityOptionsWrapper;
37 import com.android.launcher3.widget.LauncherAppWidgetHostView;
38 
39 /** Provides a Quickstep specific animation when launching an activity from an app widget. */
40 class QuickstepInteractionHandler implements RemoteViews.InteractionHandler {
41 
42     private static final String TAG = "QuickstepInteractionHandler";
43 
44     private final QuickstepLauncher mLauncher;
45 
QuickstepInteractionHandler(QuickstepLauncher launcher)46     QuickstepInteractionHandler(QuickstepLauncher launcher) {
47         mLauncher = launcher;
48     }
49 
50     @SuppressWarnings("NewApi")
51     @Override
onInteraction(View view, PendingIntent pendingIntent, RemoteViews.RemoteResponse remoteResponse)52     public boolean onInteraction(View view, PendingIntent pendingIntent,
53             RemoteViews.RemoteResponse remoteResponse) {
54         LauncherAppWidgetHostView hostView = findHostViewAncestor(view);
55         if (hostView == null) {
56             Log.e(TAG, "View did not have a LauncherAppWidgetHostView ancestor.");
57             return RemoteViews.startPendingIntent(hostView, pendingIntent,
58                     remoteResponse.getLaunchOptions(view));
59         }
60         if (mLauncher.isSplitSelectionActive()) {
61             // Log metric
62             StatsLogManager.StatsLogger logger = mLauncher.getStatsLogManager().logger();
63             logger.log(LAUNCHER_SPLIT_WIDGET_ATTEMPT);
64             mLauncher.handleIncorrectSplitTargetSelection();
65             return true;
66         }
67         Pair<Intent, ActivityOptions> options = remoteResponse.getLaunchOptions(view);
68         ActivityOptionsWrapper activityOptions = mLauncher.getAppTransitionManager()
69                 .getActivityLaunchOptions(hostView);
70         Object itemInfo = hostView.getTag();
71         IBinder launchCookie = null;
72         if (itemInfo instanceof ItemInfo) {
73             launchCookie = mLauncher.getLaunchCookie((ItemInfo) itemInfo);
74             activityOptions.options.setLaunchCookie(launchCookie);
75         }
76         if (Utilities.ATLEAST_S && !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                                 launchCookie);
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(itemInfo);
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