• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.GlobalScreenshot.ACTION_TYPE_EDIT;
20 import static com.android.systemui.screenshot.GlobalScreenshot.ACTION_TYPE_SHARE;
21 import static com.android.systemui.screenshot.GlobalScreenshot.EXTRA_ACTION_INTENT;
22 import static com.android.systemui.screenshot.GlobalScreenshot.EXTRA_DISALLOW_ENTER_PIP;
23 import static com.android.systemui.screenshot.GlobalScreenshot.EXTRA_ID;
24 import static com.android.systemui.screenshot.GlobalScreenshot.EXTRA_SMART_ACTIONS_ENABLED;
25 import static com.android.systemui.statusbar.phone.StatusBar.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 
34 import com.android.systemui.shared.system.ActivityManagerWrapper;
35 import com.android.systemui.statusbar.phone.StatusBar;
36 
37 import java.util.Optional;
38 import java.util.concurrent.ExecutionException;
39 import java.util.concurrent.TimeUnit;
40 import java.util.concurrent.TimeoutException;
41 
42 import javax.inject.Inject;
43 
44 /**
45  * Receiver to proxy the share or edit intent, used to clean up the notification and send
46  * appropriate signals to the system (ie. to dismiss the keyguard if necessary).
47  */
48 public class ActionProxyReceiver extends BroadcastReceiver {
49     private static final String TAG = "ActionProxyReceiver";
50 
51     private static final int CLOSE_WINDOWS_TIMEOUT_MILLIS = 3000;
52     private final StatusBar mStatusBar;
53     private final ActivityManagerWrapper mActivityManagerWrapper;
54     private final ScreenshotSmartActions mScreenshotSmartActions;
55 
56     @Inject
ActionProxyReceiver(Optional<StatusBar> statusBar, ActivityManagerWrapper activityManagerWrapper, ScreenshotSmartActions screenshotSmartActions)57     public ActionProxyReceiver(Optional<StatusBar> statusBar,
58             ActivityManagerWrapper activityManagerWrapper,
59             ScreenshotSmartActions screenshotSmartActions) {
60         mStatusBar = statusBar.orElse(null);
61         mActivityManagerWrapper = activityManagerWrapper;
62         mScreenshotSmartActions = screenshotSmartActions;
63     }
64 
65     @Override
onReceive(Context context, final Intent intent)66     public void onReceive(Context context, final Intent intent) {
67         Runnable startActivityRunnable = () -> {
68             try {
69                 mActivityManagerWrapper.closeSystemWindows(
70                         SYSTEM_DIALOG_REASON_SCREENSHOT).get(
71                         CLOSE_WINDOWS_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
72             } catch (TimeoutException | InterruptedException | ExecutionException e) {
73                 Log.e(TAG, "Unable to share screenshot", e);
74                 return;
75             }
76 
77             PendingIntent actionIntent = intent.getParcelableExtra(EXTRA_ACTION_INTENT);
78             ActivityOptions opts = ActivityOptions.makeBasic();
79             opts.setDisallowEnterPictureInPictureWhileLaunching(
80                     intent.getBooleanExtra(EXTRA_DISALLOW_ENTER_PIP, false));
81             try {
82                 actionIntent.send(context, 0, null, null, null, null, opts.toBundle());
83             } catch (PendingIntent.CanceledException e) {
84                 Log.e(TAG, "Pending intent canceled", e);
85             }
86 
87         };
88 
89         if (mStatusBar != null) {
90             mStatusBar.executeRunnableDismissingKeyguard(startActivityRunnable, null,
91                     true /* dismissShade */, true /* afterKeyguardGone */,
92                     true /* deferred */);
93         } else {
94             startActivityRunnable.run();
95         }
96 
97         if (intent.getBooleanExtra(EXTRA_SMART_ACTIONS_ENABLED, false)) {
98             String actionType = Intent.ACTION_EDIT.equals(intent.getAction())
99                     ? ACTION_TYPE_EDIT
100                     : ACTION_TYPE_SHARE;
101             mScreenshotSmartActions.notifyScreenshotAction(
102                     context, intent.getStringExtra(EXTRA_ID), actionType, false);
103         }
104     }
105 }
106