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.LogConfig.DEBUG_ACTIONS;
20 import static com.android.systemui.screenshot.ScreenshotController.EXTRA_ACTION_INTENT;
21 import static com.android.systemui.screenshot.ScreenshotController.EXTRA_ACTION_TYPE;
22 import static com.android.systemui.screenshot.ScreenshotController.EXTRA_ID;
23 
24 import android.app.ActivityOptions;
25 import android.app.PendingIntent;
26 import android.content.BroadcastReceiver;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.util.Log;
30 
31 import javax.inject.Inject;
32 
33 
34 /**
35  * Executes the smart action tapped by the user in the notification.
36  */
37 public class SmartActionsReceiver extends BroadcastReceiver {
38     private static final String TAG = "SmartActionsReceiver";
39 
40     private final ScreenshotSmartActions mScreenshotSmartActions;
41 
42     @Inject
SmartActionsReceiver(ScreenshotSmartActions screenshotSmartActions)43     SmartActionsReceiver(ScreenshotSmartActions screenshotSmartActions) {
44         mScreenshotSmartActions = screenshotSmartActions;
45     }
46 
47     @Override
onReceive(Context context, Intent intent)48     public void onReceive(Context context, Intent intent) {
49         PendingIntent pendingIntent = intent.getParcelableExtra(EXTRA_ACTION_INTENT);
50         String actionType = intent.getStringExtra(EXTRA_ACTION_TYPE);
51         if (DEBUG_ACTIONS) {
52             Log.d(TAG, "Executing smart action [" + actionType + "]:" + pendingIntent.getIntent());
53         }
54         ActivityOptions opts = ActivityOptions.makeBasic();
55 
56         try {
57             pendingIntent.send(context, 0, null, null, null, null, opts.toBundle());
58         } catch (PendingIntent.CanceledException e) {
59             Log.e(TAG, "Pending intent canceled", e);
60         }
61 
62         mScreenshotSmartActions.notifyScreenshotAction(
63                 context, intent.getStringExtra(EXTRA_ID), actionType, true,
64                 pendingIntent.getIntent());
65     }
66 }
67