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.os.RemoteException;
19 import android.service.notification.StatusBarNotification;
20 import android.util.ArraySet;
21 
22 import com.android.internal.statusbar.IStatusBarService;
23 import com.android.systemui.Dependency;
24 
25 import java.util.Set;
26 
27 /**
28  * Handles when smart replies are added to a notification
29  * and clicked upon.
30  */
31 public class SmartReplyController {
32     private IStatusBarService mBarService;
33     private Set<String> mSendingKeys = new ArraySet<>();
34 
SmartReplyController()35     public SmartReplyController() {
36         mBarService = Dependency.get(IStatusBarService.class);
37     }
38 
smartReplySent(NotificationData.Entry entry, int replyIndex, CharSequence reply)39     public void smartReplySent(NotificationData.Entry entry, int replyIndex, CharSequence reply) {
40         NotificationEntryManager notificationEntryManager
41                 = Dependency.get(NotificationEntryManager.class);
42         StatusBarNotification newSbn =
43                 notificationEntryManager.rebuildNotificationWithRemoteInput(entry, reply,
44                         true /* showSpinner */);
45         notificationEntryManager.updateNotification(newSbn, null /* ranking */);
46         mSendingKeys.add(entry.key);
47 
48         try {
49             mBarService.onNotificationSmartReplySent(entry.notification.getKey(),
50                     replyIndex);
51         } catch (RemoteException e) {
52             // Nothing to do, system going down
53         }
54     }
55 
56     /**
57      * Have we posted an intent to an app about sending a smart reply from the
58      * notification with this key.
59      */
isSendingSmartReply(String key)60     public boolean isSendingSmartReply(String key) {
61         return mSendingKeys.contains(key);
62     }
63 
smartRepliesAdded(final NotificationData.Entry entry, int replyCount)64     public void smartRepliesAdded(final NotificationData.Entry entry, int replyCount) {
65         try {
66             mBarService.onNotificationSmartRepliesAdded(entry.notification.getKey(),
67                     replyCount);
68         } catch (RemoteException e) {
69             // Nothing to do, system going down
70         }
71     }
72 
stopSending(final NotificationData.Entry entry)73     public void stopSending(final NotificationData.Entry entry) {
74         if (entry != null) {
75             mSendingKeys.remove(entry.notification.getKey());
76         }
77     }
78 }
79