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 androidx.annotation.NonNull; 23 24 import com.android.internal.statusbar.IStatusBarService; 25 import com.android.internal.statusbar.NotificationVisibility; 26 import com.android.systemui.Dumpable; 27 import com.android.systemui.dump.DumpManager; 28 import com.android.systemui.statusbar.dagger.CentralSurfacesModule; 29 import com.android.systemui.statusbar.notification.collection.NotificationEntry; 30 import com.android.systemui.statusbar.notification.collection.render.NotificationVisibilityProvider; 31 32 import java.io.PrintWriter; 33 import java.util.Set; 34 35 /** 36 * Handles when smart replies are added to a notification 37 * and clicked upon. 38 */ 39 public class SmartReplyController implements Dumpable { 40 private final IStatusBarService mBarService; 41 private final NotificationVisibilityProvider mVisibilityProvider; 42 private final NotificationClickNotifier mClickNotifier; 43 private final Set<String> mSendingKeys = new ArraySet<>(); 44 private Callback mCallback; 45 46 /** 47 * Injected constructor. See {@link CentralSurfacesModule}. 48 */ SmartReplyController( DumpManager dumpManager, NotificationVisibilityProvider visibilityProvider, IStatusBarService statusBarService, NotificationClickNotifier clickNotifier)49 public SmartReplyController( 50 DumpManager dumpManager, 51 NotificationVisibilityProvider visibilityProvider, 52 IStatusBarService statusBarService, 53 NotificationClickNotifier clickNotifier) { 54 mBarService = statusBarService; 55 mVisibilityProvider = visibilityProvider; 56 mClickNotifier = clickNotifier; 57 dumpManager.registerDumpable(this); 58 } 59 setCallback(Callback callback)60 public void setCallback(Callback callback) { 61 mCallback = callback; 62 } 63 64 /** 65 * Notifies StatusBarService a smart reply is sent. 66 */ smartReplySent(NotificationEntry entry, int replyIndex, CharSequence reply, int notificationLocation, boolean modifiedBeforeSending)67 public void smartReplySent(NotificationEntry entry, int replyIndex, CharSequence reply, 68 int notificationLocation, boolean modifiedBeforeSending) { 69 mCallback.onSmartReplySent(entry, reply); 70 mSendingKeys.add(entry.getKey()); 71 try { 72 mBarService.onNotificationSmartReplySent(entry.getSbn().getKey(), replyIndex, reply, 73 notificationLocation, modifiedBeforeSending); 74 } catch (RemoteException e) { 75 // Nothing to do, system going down 76 } 77 } 78 79 /** 80 * Notifies StatusBarService a smart action is clicked. 81 */ smartActionClicked( NotificationEntry entry, int actionIndex, Notification.Action action, boolean generatedByAssistant)82 public void smartActionClicked( 83 NotificationEntry entry, int actionIndex, Notification.Action action, 84 boolean generatedByAssistant) { 85 final NotificationVisibility nv = mVisibilityProvider.obtain(entry, true); 86 mClickNotifier.onNotificationActionClick( 87 entry.getKey(), actionIndex, action, nv, generatedByAssistant); 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.getSbn().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.getSbn().getKey()); 114 } 115 } 116 117 @Override dump(@onNull PrintWriter pw, @NonNull String[] args)118 public void dump(@NonNull PrintWriter pw, @NonNull String[] args) { 119 pw.println("mSendingKeys: " + mSendingKeys.size()); 120 for (String key : mSendingKeys) { 121 pw.println(" * " + key); 122 } 123 } 124 125 /** 126 * Callback for any class that needs to do something in response to a smart reply being sent. 127 */ 128 public interface Callback { 129 /** 130 * A smart reply has just been sent for a notification 131 * 132 * @param entry the entry for the notification 133 * @param reply the reply that was sent 134 */ onSmartReplySent(NotificationEntry entry, CharSequence reply)135 void onSmartReplySent(NotificationEntry entry, CharSequence reply); 136 } 137 } 138