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