Files
VscoolOS/app/src/main/java/com/uiuios/aios/service/NotificationService.java
tongtongstudio ba540d4689 version:1.1
fix:
update:迁移到UIUISOS
2022-10-21 14:18:49 +08:00

91 lines
2.8 KiB
Java

package com.uiuios.aios.service;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author TT
*/
public class NotificationService extends NotificationListenerService {
private static final String TAG = NotificationService.class.getSimpleName();
private static HashMap<String, List<StatusBarNotification>> notificationMap = new HashMap<String, List<StatusBarNotification>>();
private static Set<NotificationListener> mListener = new HashSet<NotificationListener>();
private void updateNotification() {
notificationMap.clear();
StatusBarNotification[] statusBarNotifications = getActiveNotifications();
for (StatusBarNotification sbn : statusBarNotifications) {
String pkg = sbn.getPackageName();
Log.e(TAG, "onListenerUpdate: " + pkg);
if (notificationMap.get(pkg) == null) {
notificationMap.put(pkg, new ArrayList<>());
notificationMap.get(pkg).add(sbn);
} else {
notificationMap.get(pkg).add(sbn);
}
}
Log.e(TAG, "onListenerUpdate: " + notificationMap);
}
public static int getNotificationLength(String pkg) {
if (notificationMap.get(pkg) == null) {
return 0;
} else {
return notificationMap.get(pkg).size();
}
}
public static void setListener(NotificationListener listener) {
mListener.add(listener);
}
public interface NotificationListener {
void onListenerUpdate();
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onListenerConnected() {
super.onListenerConnected();
Log.e(TAG, "onListenerUpdate: " + getActiveNotifications().length);
updateNotification();
for (NotificationListener listener : mListener) {
listener.onListenerUpdate();
}
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
Log.e(TAG, "onNotificationPosted: " + getActiveNotifications().length);
updateNotification();
for (NotificationListener listener : mListener) {
listener.onListenerUpdate();
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
Log.e(TAG, "onNotificationRemoved: " + getActiveNotifications().length);
updateNotification();
for (NotificationListener listener : mListener) {
listener.onListenerUpdate();
}
}
}