1 /* 2 * Copyright (C) 2023 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 17 package com.android.systemui.tv.notifications; 18 19 import android.annotation.Nullable; 20 import android.app.Notification; 21 import android.service.notification.NotificationListenerService; 22 import android.service.notification.StatusBarNotification; 23 import android.util.Log; 24 import android.util.SparseArray; 25 26 import com.android.systemui.CoreStartable; 27 import com.android.systemui.dagger.SysUISingleton; 28 import com.android.systemui.statusbar.NotificationListener; 29 30 import javax.inject.Inject; 31 32 /** 33 * Keeps track of the notifications on TV. 34 */ 35 @SysUISingleton 36 public class TvNotificationHandler implements CoreStartable, 37 NotificationListener.NotificationHandler { 38 private static final String TAG = "TvNotificationHandler"; 39 private final NotificationListener mNotificationListener; 40 private final SparseArray<StatusBarNotification> mNotifications = new SparseArray<>(); 41 @Nullable 42 private Listener mUpdateListener; 43 44 @Inject TvNotificationHandler(NotificationListener notificationListener)45 public TvNotificationHandler(NotificationListener notificationListener) { 46 mNotificationListener = notificationListener; 47 } 48 getCurrentNotifications()49 public SparseArray<StatusBarNotification> getCurrentNotifications() { 50 return mNotifications; 51 } 52 setTvNotificationListener(Listener listener)53 public void setTvNotificationListener(Listener listener) { 54 mUpdateListener = listener; 55 } 56 57 @Override start()58 public void start() { 59 mNotificationListener.addNotificationHandler(this); 60 mNotificationListener.registerAsSystemService(); 61 } 62 63 @Override onNotificationPosted(StatusBarNotification sbn, NotificationListenerService.RankingMap rankingMap)64 public void onNotificationPosted(StatusBarNotification sbn, 65 NotificationListenerService.RankingMap rankingMap) { 66 if (!new Notification.TvExtender(sbn.getNotification()).isAvailableOnTv()) { 67 Log.v(TAG, "Notification not added because it isn't relevant for tv"); 68 return; 69 } 70 71 mNotifications.put(sbn.getId(), sbn); 72 if (mUpdateListener != null) { 73 mUpdateListener.notificationsUpdated(mNotifications); 74 } 75 Log.d(TAG, "Notification added"); 76 } 77 78 @Override onNotificationRemoved(StatusBarNotification sbn, NotificationListenerService.RankingMap rankingMap)79 public void onNotificationRemoved(StatusBarNotification sbn, 80 NotificationListenerService.RankingMap rankingMap) { 81 82 if (mNotifications.contains(sbn.getId())) { 83 mNotifications.remove(sbn.getId()); 84 Log.d(TAG, "Notification removed"); 85 86 if (mUpdateListener != null) { 87 mUpdateListener.notificationsUpdated(mNotifications); 88 } 89 } 90 } 91 92 @Override onNotificationRemoved(StatusBarNotification sbn, NotificationListenerService.RankingMap rankingMap, int reason)93 public void onNotificationRemoved(StatusBarNotification sbn, 94 NotificationListenerService.RankingMap rankingMap, int reason) { 95 onNotificationRemoved(sbn, rankingMap); 96 } 97 98 @Override onNotificationRankingUpdate(NotificationListenerService.RankingMap rankingMap)99 public void onNotificationRankingUpdate(NotificationListenerService.RankingMap rankingMap) { 100 // noop 101 } 102 103 @Override onNotificationsInitialized()104 public void onNotificationsInitialized() { 105 // noop 106 } 107 108 /** 109 * Get notified when the notifications are updated. 110 */ 111 interface Listener { notificationsUpdated(SparseArray<StatusBarNotification> sbns)112 void notificationsUpdated(SparseArray<StatusBarNotification> sbns); 113 } 114 115 } 116