blob: 7bdb21d0eac57f468c9035925219cf84f31bc98d [file] [log] [blame]
Kenny Guy23991102018-04-05 21:18:38 +01001/*
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 */
16package com.android.systemui.statusbar;
17
Tony Mak7d4b3a52018-11-27 17:29:36 +000018import android.app.Notification;
Kenny Guy23991102018-04-05 21:18:38 +010019import android.os.RemoteException;
Kenny Guy8cc15d22018-05-09 09:50:55 +010020import android.util.ArraySet;
Kenny Guya0f6de82018-04-06 16:20:16 +010021
Kenny Guy23991102018-04-05 21:18:38 +010022import com.android.internal.statusbar.IStatusBarService;
Tony Mak7d4b3a52018-11-27 17:29:36 +000023import com.android.internal.statusbar.NotificationVisibility;
Tony Mak7d4b3a52018-11-27 17:29:36 +000024import com.android.systemui.statusbar.notification.NotificationEntryManager;
Ned Burnsf81c4c42019-01-07 14:10:43 -050025import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Gustav Senntonf892fe92019-01-22 15:31:42 +000026import com.android.systemui.statusbar.notification.logging.NotificationLogger;
Kenny Guya0f6de82018-04-06 16:20:16 +010027
Kenny Guy8cc15d22018-05-09 09:50:55 +010028import java.util.Set;
Kenny Guy23991102018-04-05 21:18:38 +010029
Jason Monk27d01a622018-12-10 15:57:09 -050030import javax.inject.Inject;
31import javax.inject.Singleton;
32
Kenny Guy23991102018-04-05 21:18:38 +010033/**
Kenny Guya0f6de82018-04-06 16:20:16 +010034 * Handles when smart replies are added to a notification
Kenny Guy23991102018-04-05 21:18:38 +010035 * and clicked upon.
36 */
Jason Monk27d01a622018-12-10 15:57:09 -050037@Singleton
Kenny Guya0f6de82018-04-06 16:20:16 +010038public class SmartReplyController {
Jason Monk752c68f2018-12-21 12:07:55 -050039 private final IStatusBarService mBarService;
40 private final NotificationEntryManager mEntryManager;
Kenny Guy8cc15d22018-05-09 09:50:55 +010041 private Set<String> mSendingKeys = new ArraySet<>();
Kevina5ff1fa2018-08-21 16:35:48 -070042 private Callback mCallback;
Tony Mak7d4b3a52018-11-27 17:29:36 +000043
Jason Monk27d01a622018-12-10 15:57:09 -050044 @Inject
Jason Monk752c68f2018-12-21 12:07:55 -050045 public SmartReplyController(NotificationEntryManager entryManager,
46 IStatusBarService statusBarService) {
47 mBarService = statusBarService;
48 mEntryManager = entryManager;
Kenny Guy23991102018-04-05 21:18:38 +010049 }
50
Kevina5ff1fa2018-08-21 16:35:48 -070051 public void setCallback(Callback callback) {
52 mCallback = callback;
53 }
Kenny Guya0f6de82018-04-06 16:20:16 +010054
Tony Mak29996702018-11-26 16:23:34 +000055 /**
56 * Notifies StatusBarService a smart reply is sent.
57 */
Ned Burnsf81c4c42019-01-07 14:10:43 -050058 public void smartReplySent(NotificationEntry entry, int replyIndex, CharSequence reply,
Milo Sredkov13d88112019-02-01 12:23:24 +000059 int notificationLocation, boolean modifiedBeforeSending) {
Kevina5ff1fa2018-08-21 16:35:48 -070060 mCallback.onSmartReplySent(entry, reply);
Ned Burns00b4b2d2019-10-17 22:09:27 -040061 mSendingKeys.add(entry.getKey());
Kenny Guy23991102018-04-05 21:18:38 +010062 try {
Ned Burns00b4b2d2019-10-17 22:09:27 -040063 mBarService.onNotificationSmartReplySent(entry.getSbn().getKey(), replyIndex, reply,
Milo Sredkov13d88112019-02-01 12:23:24 +000064 notificationLocation, modifiedBeforeSending);
Kenny Guy23991102018-04-05 21:18:38 +010065 } catch (RemoteException e) {
66 // Nothing to do, system going down
67 }
68 }
69
Kenny Guy8cc15d22018-05-09 09:50:55 +010070 /**
Tony Mak7d4b3a52018-11-27 17:29:36 +000071 * Notifies StatusBarService a smart action is clicked.
72 */
73 public void smartActionClicked(
Ned Burnsf81c4c42019-01-07 14:10:43 -050074 NotificationEntry entry, int actionIndex, Notification.Action action,
Tony Mak7d4b3a52018-11-27 17:29:36 +000075 boolean generatedByAssistant) {
76 final int count = mEntryManager.getNotificationData().getActiveNotifications().size();
Ned Burns00b4b2d2019-10-17 22:09:27 -040077 final int rank = mEntryManager.getNotificationData().getRank(entry.getKey());
Gustav Senntonf892fe92019-01-22 15:31:42 +000078 NotificationVisibility.NotificationLocation location =
79 NotificationLogger.getNotificationLocation(entry);
80 final NotificationVisibility nv = NotificationVisibility.obtain(
Ned Burns00b4b2d2019-10-17 22:09:27 -040081 entry.getKey(), rank, count, true, location);
Tony Mak7d4b3a52018-11-27 17:29:36 +000082 try {
83 mBarService.onNotificationActionClick(
Ned Burns00b4b2d2019-10-17 22:09:27 -040084 entry.getKey(), actionIndex, action, nv, generatedByAssistant);
Tony Mak7d4b3a52018-11-27 17:29:36 +000085 } catch (RemoteException e) {
86 // Nothing to do, system going down
87 }
88 }
89
90 /**
Kenny Guy8cc15d22018-05-09 09:50:55 +010091 * Have we posted an intent to an app about sending a smart reply from the
92 * notification with this key.
93 */
94 public boolean isSendingSmartReply(String key) {
95 return mSendingKeys.contains(key);
96 }
97
Gustav Senntond25a64d2018-12-07 10:58:39 +000098 /**
99 * Smart Replies and Actions have been added to the UI.
100 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500101 public void smartSuggestionsAdded(final NotificationEntry entry, int replyCount,
Milo Sredkov13d88112019-02-01 12:23:24 +0000102 int actionCount, boolean generatedByAssistant, boolean editBeforeSending) {
Kenny Guy23991102018-04-05 21:18:38 +0100103 try {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400104 mBarService.onNotificationSmartSuggestionsAdded(entry.getSbn().getKey(), replyCount,
Milo Sredkov13d88112019-02-01 12:23:24 +0000105 actionCount, generatedByAssistant, editBeforeSending);
Kenny Guy23991102018-04-05 21:18:38 +0100106 } catch (RemoteException e) {
107 // Nothing to do, system going down
108 }
109 }
Kenny Guy8cc15d22018-05-09 09:50:55 +0100110
Ned Burnsf81c4c42019-01-07 14:10:43 -0500111 public void stopSending(final NotificationEntry entry) {
Kenny Guy8cc15d22018-05-09 09:50:55 +0100112 if (entry != null) {
Ned Burns00b4b2d2019-10-17 22:09:27 -0400113 mSendingKeys.remove(entry.getSbn().getKey());
Kenny Guy8cc15d22018-05-09 09:50:55 +0100114 }
115 }
Kevina5ff1fa2018-08-21 16:35:48 -0700116
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 */
Ned Burnsf81c4c42019-01-07 14:10:43 -0500127 void onSmartReplySent(NotificationEntry entry, CharSequence reply);
Kevina5ff1fa2018-08-21 16:35:48 -0700128 }
Kenny Guy23991102018-04-05 21:18:38 +0100129}