summaryrefslogtreecommitdiff
path: root/quickstep/src/com/android/launcher3/uioverrides/QuickstepInteractionHandler.java
blob: 8092582bafecfabce6fda4f80ebcaf0ec9ee900b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.launcher3.uioverrides;

import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_APP_LAUNCH_TAP;

import android.app.ActivityOptions;
import android.app.ActivityTaskManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;
import android.window.SplashScreen;

import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.logging.StatsLogManager;
import com.android.launcher3.model.data.ItemInfo;
import com.android.launcher3.util.ActivityOptionsWrapper;
import com.android.launcher3.widget.LauncherAppWidgetHostView;

/** Provides a Quickstep specific animation when launching an activity from an app widget. */
class QuickstepInteractionHandler implements RemoteViews.InteractionHandler {

    private static final String TAG = "QuickstepInteractionHandler";

    private final QuickstepLauncher mLauncher;

    QuickstepInteractionHandler(QuickstepLauncher launcher) {
        mLauncher = launcher;
    }

    @SuppressWarnings("NewApi")
    @Override
    public boolean onInteraction(View view, PendingIntent pendingIntent,
            RemoteViews.RemoteResponse remoteResponse) {
        LauncherAppWidgetHostView hostView = findHostViewAncestor(view);
        if (hostView == null) {
            Log.e(TAG, "View did not have a LauncherAppWidgetHostView ancestor.");
            return RemoteViews.startPendingIntent(hostView, pendingIntent,
                    remoteResponse.getLaunchOptions(view));
        }
        if (mLauncher.isSplitSelectionEnabled()) {
            Toast.makeText(hostView.getContext(), R.string.split_widgets_not_supported,
                    Toast.LENGTH_SHORT).show();
            return true;
        }
        Pair<Intent, ActivityOptions> options = remoteResponse.getLaunchOptions(view);
        ActivityOptionsWrapper activityOptions = mLauncher.getAppTransitionManager()
                .getActivityLaunchOptions(hostView);
        Object itemInfo = hostView.getTag();
        IBinder launchCookie = null;
        if (itemInfo instanceof ItemInfo) {
            launchCookie = mLauncher.getLaunchCookie((ItemInfo) itemInfo);
            activityOptions.options.setLaunchCookie(launchCookie);
        }
        if (Utilities.ATLEAST_S && !pendingIntent.isActivity()) {
            // In the event this pending intent eventually launches an activity, i.e. a trampoline,
            // use the Quickstep transition animation.
            try {
                ActivityTaskManager.getService()
                        .registerRemoteAnimationForNextActivityStart(
                                pendingIntent.getCreatorPackage(),
                                activityOptions.options.getRemoteAnimationAdapter(),
                                launchCookie);
            } catch (RemoteException e) {
                // Do nothing.
            }
        }
        activityOptions.options.setPendingIntentLaunchFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activityOptions.options.setSplashScreenStyle(SplashScreen.SPLASH_SCREEN_STYLE_SOLID_COLOR);
        activityOptions.options.setPendingIntentBackgroundActivityStartMode(
                ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED);
        options = Pair.create(options.first, activityOptions.options);
        if (pendingIntent.isActivity()) {
            logAppLaunch(itemInfo);
        }
        return RemoteViews.startPendingIntent(hostView, pendingIntent, options);
    }

    /**
     * Logs that the app was launched from the widget.
     * @param itemInfo the widget info.
     */
    private void logAppLaunch(Object itemInfo) {
        StatsLogManager.StatsLogger logger = mLauncher.getStatsLogManager().logger();
        if (itemInfo instanceof ItemInfo) {
            logger.withItemInfo((ItemInfo) itemInfo);
        }
        logger.log(LAUNCHER_APP_LAUNCH_TAP);
    }

    private LauncherAppWidgetHostView findHostViewAncestor(View v) {
        while (v != null) {
            if (v instanceof LauncherAppWidgetHostView) return (LauncherAppWidgetHostView) v;
            v = (View) v.getParent();
        }
        return null;
    }
}