1 /*
2  * Copyright (C) 2020 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 
17 package com.android.systemui.screenshot;
18 
19 import static com.android.systemui.screenshot.ScreenshotController.ACTION_TYPE_EDIT;
20 import static com.android.systemui.screenshot.ScreenshotController.ACTION_TYPE_SHARE;
21 import static com.android.systemui.screenshot.ScreenshotController.EXTRA_ACTION_INTENT;
22 import static com.android.systemui.screenshot.ScreenshotController.EXTRA_DISALLOW_ENTER_PIP;
23 import static com.android.systemui.screenshot.ScreenshotController.EXTRA_ID;
24 import static com.android.systemui.screenshot.ScreenshotController.EXTRA_SMART_ACTIONS_ENABLED;
25 import static com.android.systemui.statusbar.phone.CentralSurfaces.SYSTEM_DIALOG_REASON_SCREENSHOT;
26 
27 import android.app.ActivityOptions;
28 import android.app.PendingIntent;
29 import android.content.BroadcastReceiver;
30 import android.content.Context;
31 import android.content.Intent;
32 import android.util.Log;
33 import android.view.RemoteAnimationAdapter;
34 import android.view.WindowManagerGlobal;
35 
36 import com.android.systemui.plugins.ActivityStarter;
37 import com.android.systemui.settings.DisplayTracker;
38 import com.android.systemui.shared.system.ActivityManagerWrapper;
39 
40 import javax.inject.Inject;
41 
42 /**
43  * Receiver to proxy the share or edit intent, used to clean up the notification and send
44  * appropriate signals to the system (ie. to dismiss the keyguard if necessary).
45  */
46 public class ActionProxyReceiver extends BroadcastReceiver {
47     private static final String TAG = "ActionProxyReceiver";
48 
49     private final ActivityManagerWrapper mActivityManagerWrapper;
50     private final ScreenshotSmartActions mScreenshotSmartActions;
51     private final DisplayTracker mDisplayTracker;
52     private final ActivityStarter mActivityStarter;
53 
54     @Inject
ActionProxyReceiver(ActivityManagerWrapper activityManagerWrapper, ScreenshotSmartActions screenshotSmartActions, DisplayTracker displayTracker, ActivityStarter activityStarter)55     public ActionProxyReceiver(ActivityManagerWrapper activityManagerWrapper,
56             ScreenshotSmartActions screenshotSmartActions,
57             DisplayTracker displayTracker,
58             ActivityStarter activityStarter) {
59         mActivityManagerWrapper = activityManagerWrapper;
60         mScreenshotSmartActions = screenshotSmartActions;
61         mDisplayTracker = displayTracker;
62         mActivityStarter = activityStarter;
63     }
64 
65     @Override
onReceive(Context context, final Intent intent)66     public void onReceive(Context context, final Intent intent) {
67         Runnable startActivityRunnable = () -> {
68             mActivityManagerWrapper.closeSystemWindows(SYSTEM_DIALOG_REASON_SCREENSHOT);
69 
70             PendingIntent actionIntent = intent.getParcelableExtra(EXTRA_ACTION_INTENT);
71             ActivityOptions opts = ActivityOptions.makeBasic();
72             opts.setDisallowEnterPictureInPictureWhileLaunching(
73                     intent.getBooleanExtra(EXTRA_DISALLOW_ENTER_PIP, false));
74             opts.setPendingIntentBackgroundActivityStartMode(
75                     ActivityOptions.MODE_BACKGROUND_ACTIVITY_START_ALLOWED);
76             try {
77                 actionIntent.send(context, 0, null, null, null, null, opts.toBundle());
78                 if (intent.getBooleanExtra(ScreenshotController.EXTRA_OVERRIDE_TRANSITION, false)) {
79                     RemoteAnimationAdapter runner = new RemoteAnimationAdapter(
80                             ScreenshotController.SCREENSHOT_REMOTE_RUNNER, 0, 0);
81                     try {
82                         WindowManagerGlobal.getWindowManagerService()
83                                 .overridePendingAppTransitionRemote(runner,
84                                         mDisplayTracker.getDefaultDisplayId());
85                     } catch (Exception e) {
86                         Log.e(TAG, "Error overriding screenshot app transition", e);
87                     }
88                 }
89             } catch (PendingIntent.CanceledException e) {
90                 Log.e(TAG, "Pending intent canceled", e);
91             }
92 
93         };
94 
95         mActivityStarter.executeRunnableDismissingKeyguard(startActivityRunnable, null,
96                 true /* dismissShade */, true /* afterKeyguardGone */,
97                 true /* deferred */);
98 
99         if (intent.getBooleanExtra(EXTRA_SMART_ACTIONS_ENABLED, false)) {
100             String actionType = Intent.ACTION_EDIT.equals(intent.getAction())
101                     ? ACTION_TYPE_EDIT
102                     : ACTION_TYPE_SHARE;
103             mScreenshotSmartActions.notifyScreenshotAction(
104                     intent.getStringExtra(EXTRA_ID), actionType, false, null);
105         }
106     }
107 }
108