diff --git a/core/api/system-current.txt b/core/api/system-current.txt index 728affae2a3bc..35e91ac0a5baa 100644 --- a/core/api/system-current.txt +++ b/core/api/system-current.txt @@ -9743,6 +9743,7 @@ package android.service.notification { method public void onActionInvoked(@NonNull String, @NonNull android.app.Notification.Action, int); method public void onAllowedAdjustmentsChanged(); method @NonNull public final android.os.IBinder onBind(@Nullable android.content.Intent); + method public void onNotificationClicked(@NonNull String); method public void onNotificationDirectReplied(@NonNull String); method @Nullable public abstract android.service.notification.Adjustment onNotificationEnqueued(@NonNull android.service.notification.StatusBarNotification); method @Nullable public android.service.notification.Adjustment onNotificationEnqueued(@NonNull android.service.notification.StatusBarNotification, @NonNull android.app.NotificationChannel); diff --git a/core/api/test-current.txt b/core/api/test-current.txt index 9e83136b978d5..9f57502d5cf02 100644 --- a/core/api/test-current.txt +++ b/core/api/test-current.txt @@ -292,6 +292,7 @@ package android.app { } public class StatusBarManager { + method public void clickNotification(@Nullable String, int, int, boolean); method @RequiresPermission(android.Manifest.permission.STATUS_BAR) public void collapsePanels(); method public void expandNotificationsPanel(); method @RequiresPermission(android.Manifest.permission.STATUS_BAR) public void setExpansionDisabledForSimNetworkLock(boolean); diff --git a/core/java/android/app/StatusBarManager.java b/core/java/android/app/StatusBarManager.java index 4e3d85ca1b780..7d2bc1a5f8e28 100644 --- a/core/java/android/app/StatusBarManager.java +++ b/core/java/android/app/StatusBarManager.java @@ -35,6 +35,7 @@ import android.util.Slog; import android.view.View; import com.android.internal.statusbar.IStatusBarService; +import com.android.internal.statusbar.NotificationVisibility; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -262,6 +263,28 @@ public class StatusBarManager { } } + /** + * Simulate notification click for testing + * + * @hide + */ + @TestApi + public void clickNotification(@Nullable String key, int rank, int count, boolean visible) { + clickNotificationInternal(key, rank, count, visible); + } + + private void clickNotificationInternal(String key, int rank, int count, boolean visible) { + try { + final IStatusBarService svc = getService(); + if (svc != null) { + svc.onNotificationClick(key, + NotificationVisibility.obtain(key, rank, count, visible)); + } + } catch (RemoteException ex) { + throw ex.rethrowFromSystemServer(); + } + } + /** * Expand the notifications panel. * diff --git a/core/java/android/service/notification/INotificationListener.aidl b/core/java/android/service/notification/INotificationListener.aidl index 4ead3fc672608..e0f3018e3d0e0 100644 --- a/core/java/android/service/notification/INotificationListener.aidl +++ b/core/java/android/service/notification/INotificationListener.aidl @@ -56,5 +56,6 @@ oneway interface INotificationListener void onNotificationDirectReply(String key); void onSuggestedReplySent(String key, in CharSequence reply, int source); void onActionClicked(String key, in Notification.Action action, int source); + void onNotificationClicked(String key); void onAllowedAdjustmentsChanged(); } diff --git a/core/java/android/service/notification/NotificationAssistantService.java b/core/java/android/service/notification/NotificationAssistantService.java index 6320149364258..cf2152cc3ad48 100644 --- a/core/java/android/service/notification/NotificationAssistantService.java +++ b/core/java/android/service/notification/NotificationAssistantService.java @@ -241,6 +241,13 @@ public abstract class NotificationAssistantService extends NotificationListenerS @Source int source) { } + /** + * Implement this to know when a notification is clicked by user. + * @param key the notification key + */ + public void onNotificationClicked(@NonNull String key) { + } + /** * Implement this to know when a user has changed which features of * their notifications the assistant can modify. @@ -421,6 +428,13 @@ public abstract class NotificationAssistantService extends NotificationListenerS mHandler.obtainMessage(MyHandler.MSG_ON_ACTION_INVOKED, args).sendToTarget(); } + @Override + public void onNotificationClicked(String key) { + SomeArgs args = SomeArgs.obtain(); + args.arg1 = key; + mHandler.obtainMessage(MyHandler.MSG_ON_NOTIFICATION_CLICKED, args).sendToTarget(); + } + @Override public void onAllowedAdjustmentsChanged() { mHandler.obtainMessage(MyHandler.MSG_ON_ALLOWED_ADJUSTMENTS_CHANGED).sendToTarget(); @@ -445,6 +459,7 @@ public abstract class NotificationAssistantService extends NotificationListenerS public static final int MSG_ON_PANEL_REVEALED = 9; public static final int MSG_ON_PANEL_HIDDEN = 10; public static final int MSG_ON_NOTIFICATION_VISIBILITY_CHANGED = 11; + public static final int MSG_ON_NOTIFICATION_CLICKED = 12; public MyHandler(Looper looper) { super(looper, null, false); @@ -550,6 +565,13 @@ public abstract class NotificationAssistantService extends NotificationListenerS onNotificationVisibilityChanged(key, isVisible); break; } + case MSG_ON_NOTIFICATION_CLICKED: { + SomeArgs args = (SomeArgs) msg.obj; + String key = (String) args.arg1; + args.recycle(); + onNotificationClicked(key); + break; + } } } } diff --git a/core/java/android/service/notification/NotificationListenerService.java b/core/java/android/service/notification/NotificationListenerService.java index f79b59fe54324..01e52608ab029 100644 --- a/core/java/android/service/notification/NotificationListenerService.java +++ b/core/java/android/service/notification/NotificationListenerService.java @@ -1486,6 +1486,11 @@ public abstract class NotificationListenerService extends Service { // no-op in the listener } + @Override + public void onNotificationClicked(String key) { + // no-op in the listener + } + @Override public void onAllowedAdjustmentsChanged() { // no-op in the listener diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index 60d83f1fd4e8a..c3977242b5dd6 100755 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -998,6 +998,7 @@ public class NotificationManagerService extends SystemService { REASON_CLICK, nv.rank, nv.count, null); nv.recycle(); reportUserInteraction(r); + mAssistants.notifyAssistantNotificationClicked(r); } } @@ -9474,6 +9475,22 @@ public class NotificationManagerService extends SystemService { }); } + @GuardedBy("mNotificationLock") + void notifyAssistantNotificationClicked(final NotificationRecord r) { + final String key = r.getSbn().getKey(); + notifyAssistantLocked( + r.getSbn(), + r.getNotificationType(), + true /* sameUserOnly */, + (assistant, sbnHolder) -> { + try { + assistant.onNotificationClicked(key); + } catch (RemoteException ex) { + Slog.e(TAG, "unable to notify assistant (clicked): " + assistant, ex); + } + }); + } + /** * Notifies the assistant something about the specified notification, only assistant * that is visible to the notification will be notified.