1 /*
2  * Copyright (C) 2018 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.systemui.statusbar;
17 
18 import android.app.Notification;
19 import android.os.RemoteException;
20 import android.util.ArraySet;
21 
22 import com.android.internal.statusbar.IStatusBarService;
23 import com.android.internal.statusbar.NotificationVisibility;
24 import com.android.systemui.statusbar.notification.NotificationEntryManager;
25 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
26 import com.android.systemui.statusbar.notification.logging.NotificationLogger;
27 
28 import java.util.Set;
29 
30 import javax.inject.Inject;
31 import javax.inject.Singleton;
32 
33 /**
34  * Handles when smart replies are added to a notification
35  * and clicked upon.
36  */
37 @Singleton
38 public class SmartReplyController {
39     private final IStatusBarService mBarService;
40     private final NotificationEntryManager mEntryManager;
41     private Set<String> mSendingKeys = new ArraySet<>();
42     private Callback mCallback;
43 
44     @Inject
SmartReplyController(NotificationEntryManager entryManager, IStatusBarService statusBarService)45     public SmartReplyController(NotificationEntryManager entryManager,
46             IStatusBarService statusBarService) {
47         mBarService = statusBarService;
48         mEntryManager = entryManager;
49     }
50 
setCallback(Callback callback)51     public void setCallback(Callback callback) {
52         mCallback = callback;
53     }
54 
55     /**
56      * Notifies StatusBarService a smart reply is sent.
57      */
smartReplySent(NotificationEntry entry, int replyIndex, CharSequence reply, int notificationLocation, boolean modifiedBeforeSending)58     public void smartReplySent(NotificationEntry entry, int replyIndex, CharSequence reply,
59             int notificationLocation, boolean modifiedBeforeSending) {
60         mCallback.onSmartReplySent(entry, reply);
61         mSendingKeys.add(entry.key);
62         try {
63             mBarService.onNotificationSmartReplySent(entry.notification.getKey(), replyIndex, reply,
64                     notificationLocation, modifiedBeforeSending);
65         } catch (RemoteException e) {
66             // Nothing to do, system going down
67         }
68     }
69 
70     /**
71      * Notifies StatusBarService a smart action is clicked.
72      */
smartActionClicked( NotificationEntry entry, int actionIndex, Notification.Action action, boolean generatedByAssistant)73     public void smartActionClicked(
74             NotificationEntry entry, int actionIndex, Notification.Action action,
75             boolean generatedByAssistant) {
76         final int count = mEntryManager.getNotificationData().getActiveNotifications().size();
77         final int rank = mEntryManager.getNotificationData().getRank(entry.key);
78         NotificationVisibility.NotificationLocation location =
79                 NotificationLogger.getNotificationLocation(entry);
80         final NotificationVisibility nv = NotificationVisibility.obtain(
81                 entry.key, rank, count, true, location);
82         try {
83             mBarService.onNotificationActionClick(
84                     entry.key, actionIndex, action, nv, generatedByAssistant);
85         } catch (RemoteException e) {
86             // Nothing to do, system going down
87         }
88     }
89 
90     /**
91      * Have we posted an intent to an app about sending a smart reply from the
92      * notification with this key.
93      */
isSendingSmartReply(String key)94     public boolean isSendingSmartReply(String key) {
95         return mSendingKeys.contains(key);
96     }
97 
98     /**
99      * Smart Replies and Actions have been added to the UI.
100      */
smartSuggestionsAdded(final NotificationEntry entry, int replyCount, int actionCount, boolean generatedByAssistant, boolean editBeforeSending)101     public void smartSuggestionsAdded(final NotificationEntry entry, int replyCount,
102             int actionCount, boolean generatedByAssistant, boolean editBeforeSending) {
103         try {
104             mBarService.onNotificationSmartSuggestionsAdded(entry.notification.getKey(), replyCount,
105                     actionCount, generatedByAssistant, editBeforeSending);
106         } catch (RemoteException e) {
107             // Nothing to do, system going down
108         }
109     }
110 
stopSending(final NotificationEntry entry)111     public void stopSending(final NotificationEntry entry) {
112         if (entry != null) {
113             mSendingKeys.remove(entry.notification.getKey());
114         }
115     }
116 
117     /**
118      * Callback for any class that needs to do something in response to a smart reply being sent.
119      */
120     public interface Callback {
121         /**
122          * A smart reply has just been sent for a notification
123          *
124          * @param entry the entry for the notification
125          * @param reply the reply that was sent
126          */
onSmartReplySent(NotificationEntry entry, CharSequence reply)127         void onSmartReplySent(NotificationEntry entry, CharSequence reply);
128     }
129 }
130