• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 import android.app.ActivityOptions;
21 import android.app.ActivityTaskManager;
22 import android.app.PendingIntent;
23 import android.content.Intent;
24 import android.os.RemoteException;
25 import android.util.Log;
26 import android.util.Pair;
27 import android.view.View;
28 import android.widget.RemoteViews;
29 import android.window.SplashScreen;
30 
31 import com.android.launcher3.Utilities;
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 /** Provides a Quickstep specific animation when launching an activity from an app widget. */
38 class QuickstepInteractionHandler implements RemoteViews.InteractionHandler {
39 
40     private static final String TAG = "QuickstepInteractionHandler";
41 
42     private final QuickstepLauncher mLauncher;
43 
QuickstepInteractionHandler(QuickstepLauncher launcher)44     QuickstepInteractionHandler(QuickstepLauncher launcher) {
45         mLauncher = launcher;
46     }
47 
48     @SuppressWarnings("NewApi")
49     @Override
onInteraction(View view, PendingIntent pendingIntent, RemoteViews.RemoteResponse remoteResponse)50     public boolean onInteraction(View view, PendingIntent pendingIntent,
51             RemoteViews.RemoteResponse remoteResponse) {
52         LauncherAppWidgetHostView hostView = findHostViewAncestor(view);
53         if (hostView == null) {
54             Log.e(TAG, "View did not have a LauncherAppWidgetHostView ancestor.");
55             return RemoteViews.startPendingIntent(hostView, pendingIntent,
56                     remoteResponse.getLaunchOptions(view));
57         }
58         Pair<Intent, ActivityOptions> options = remoteResponse.getLaunchOptions(view);
59         ActivityOptionsWrapper activityOptions = mLauncher.getAppTransitionManager()
60                 .getActivityLaunchOptions(hostView);
61         if (Utilities.ATLEAST_S && !pendingIntent.isActivity()) {
62             // In the event this pending intent eventually launches an activity, i.e. a trampoline,
63             // use the Quickstep transition animation.
64             try {
65                 ActivityTaskManager.getService()
66                         .registerRemoteAnimationForNextActivityStart(
67                                 pendingIntent.getCreatorPackage(),
68                                 activityOptions.options.getRemoteAnimationAdapter());
69             } catch (RemoteException e) {
70                 // Do nothing.
71             }
72         }
73         activityOptions.options.setPendingIntentLaunchFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
74         activityOptions.options.setSplashscreenStyle(SplashScreen.SPLASH_SCREEN_STYLE_ICON);
75         Object itemInfo = hostView.getTag();
76         if (itemInfo instanceof ItemInfo) {
77             mLauncher.addLaunchCookie((ItemInfo) itemInfo, activityOptions.options);
78         }
79         options = Pair.create(options.first, activityOptions.options);
80         if (pendingIntent.isActivity()) {
81             logAppLaunch(itemInfo);
82         }
83         return RemoteViews.startPendingIntent(hostView, pendingIntent, options);
84     }
85 
86     /**
87      * Logs that the app was launched from the widget.
88      * @param itemInfo the widget info.
89      */
logAppLaunch(Object itemInfo)90     private void logAppLaunch(Object itemInfo) {
91         StatsLogManager.StatsLogger logger = mLauncher.getStatsLogManager().logger();
92         if (itemInfo instanceof ItemInfo) {
93             logger.withItemInfo((ItemInfo) itemInfo);
94         }
95         logger.log(LAUNCHER_APP_LAUNCH_TAP);
96     }
97 
findHostViewAncestor(View v)98     private LauncherAppWidgetHostView findHostViewAncestor(View v) {
99         while (v != null) {
100             if (v instanceof LauncherAppWidgetHostView) return (LauncherAppWidgetHostView) v;
101             v = (View) v.getParent();
102         }
103         return null;
104     }
105 }
106