90 lines
2.7 KiB
Java
90 lines
2.7 KiB
Java
package com.uiui.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.List;
|
|
|
|
/**
|
|
* @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 NotificationListener mListener;
|
|
|
|
private void updateNotification() {
|
|
notificationMap.clear();
|
|
StatusBarNotification[] statusBarNotifications = getActiveNotifications();
|
|
for (StatusBarNotification sbn : statusBarNotifications) {
|
|
String pkg = sbn.getPackageName();
|
|
Log.e(TAG, "onListenerConnected: " + 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, "onListenerConnected: " + 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 = listener;
|
|
}
|
|
|
|
public interface NotificationListener {
|
|
void onListenerConnected();
|
|
|
|
void onNotificationPosted(StatusBarNotification sbn);
|
|
|
|
void onNotificationRemoved(StatusBarNotification sbn);
|
|
}
|
|
|
|
@Override
|
|
public void onCreate() {
|
|
super.onCreate();
|
|
}
|
|
|
|
@Override
|
|
public void onListenerConnected() {
|
|
super.onListenerConnected();
|
|
Log.e(TAG, "onListenerConnected: " + getActiveNotifications().length);
|
|
updateNotification();
|
|
if (mListener != null)
|
|
mListener.onListenerConnected();
|
|
}
|
|
|
|
@Override
|
|
public void onNotificationPosted(StatusBarNotification sbn) {
|
|
super.onNotificationPosted(sbn);
|
|
Log.e(TAG, "onNotificationPosted: " + getActiveNotifications().length);
|
|
updateNotification();
|
|
if (mListener != null)
|
|
mListener.onNotificationPosted(sbn);
|
|
}
|
|
|
|
@Override
|
|
public void onNotificationRemoved(StatusBarNotification sbn) {
|
|
super.onNotificationRemoved(sbn);
|
|
Log.e(TAG, "onNotificationRemoved: " + getActiveNotifications().length);
|
|
updateNotification();
|
|
if (mListener != null)
|
|
mListener.onNotificationRemoved(sbn);
|
|
}
|
|
|
|
|
|
}
|