add: notification listener service

This commit is contained in:
hyb1996
2017-10-31 00:07:45 +08:00
parent bfd6a0c40a
commit cbd2600c33
11 changed files with 255 additions and 60 deletions

View File

@@ -0,0 +1,92 @@
package com.stardust.notification;
import android.app.PendingIntent;
import android.os.Build;
import android.os.Parcel;
import android.support.annotation.RequiresApi;
/**
* Created by Stardust on 2017/10/30.
*/
public class Notification extends android.app.Notification {
private String mPackageName;
private Notification(String packageName) {
mPackageName = packageName;
}
public static Notification create(android.app.Notification n, String packageName) {
Notification notification = new Notification(packageName);
clone(n, notification);
return notification;
}
public String getPackageName() {
return mPackageName;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public String getText() {
return extras.getString(EXTRA_TEXT);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public String getTitle() {
return extras.getString(EXTRA_TITLE);
}
public void click() {
try {
this.contentIntent.send();
} catch (PendingIntent.CanceledException e) {
throw new RuntimeException(e);
}
}
public void delete() {
try {
this.deleteIntent.send();
} catch (PendingIntent.CanceledException e) {
throw new RuntimeException(e);
}
}
public static void clone(android.app.Notification from, android.app.Notification to) {
to.when = from.when;
to.icon = from.icon;
to.iconLevel = from.iconLevel;
to.number = from.number;
to.contentIntent = from.contentIntent;
to.deleteIntent = from.deleteIntent;
to.fullScreenIntent = from.fullScreenIntent;
to.tickerText = from.tickerText;
to.tickerView = from.tickerView;
to.contentView = from.contentView;
to.bigContentView = from.bigContentView;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
to.headsUpContentView = from.headsUpContentView;
to.audioAttributes = from.audioAttributes;
to.color = from.color;
to.visibility = from.visibility;
to.category = from.category;
to.publicVersion = from.publicVersion;
}
to.largeIcon = from.largeIcon;
to.sound = from.sound;
to.audioStreamType = from.audioStreamType;
to.vibrate = from.vibrate;
to.ledARGB = from.ledARGB;
to.ledOnMS = from.ledOnMS;
to.ledOffMS = from.ledOffMS;
to.defaults = from.defaults;
to.flags = from.flags;
to.priority = from.priority;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
to.extras = from.extras;
to.actions = from.actions;
}
}
}

View File

@@ -0,0 +1,69 @@
package com.stardust.notification;
import android.app.Notification;
import android.os.Build;
import android.os.Parcel;
import android.service.notification.StatusBarNotification;
import android.support.annotation.RequiresApi;
import com.stardust.view.accessibility.NotificationListener;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Created by Stardust on 2017/10/30.
*/
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationListenerService extends android.service.notification.NotificationListenerService {
private CopyOnWriteArrayList<NotificationListener> mNotificationListeners = new CopyOnWriteArrayList<>();
private static NotificationListenerService sInstance;
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
}
public static NotificationListenerService getInstance() {
return sInstance;
}
@Override
public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
onNotificationPosted(sbn);
}
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
for (NotificationListener listener : mNotificationListeners) {
listener.onNotification(com.stardust.notification.Notification.create(
sbn.getNotification(), sbn.getPackageName()));
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
}
public void addListener(NotificationListener listener) {
mNotificationListeners.add(listener);
}
public boolean removeListener(NotificationListener listener) {
return mNotificationListeners.remove(listener);
}
@Override
public void onDestroy() {
super.onDestroy();
sInstance = null;
}
}

View File

@@ -1,12 +1,14 @@
package com.stardust.view.accessibility;
import android.accessibilityservice.AccessibilityService;
import android.app.Notification;
import android.content.Context;
import android.service.notification.StatusBarNotification;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import com.stardust.notification.Notification;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -19,6 +21,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
public interface NotificationListener {
class NotificationInfo {
private String mPackageName;
@@ -73,9 +76,9 @@ public interface NotificationListener {
}
}
void onNotification(AccessibilityEvent event, NotificationInfo notification);
void onNotification(AccessibilityEvent event, Notification notification);
void onNotification(Notification notification);
class Observer implements NotificationListener, AccessibilityDelegate {
@@ -91,27 +94,6 @@ public interface NotificationListener {
mContext = context;
}
@Override
public void onNotification(AccessibilityEvent event, NotificationInfo notification) {
for (NotificationListener listener : mNotificationListeners) {
try {
listener.onNotification(event, notification);
} catch (Exception e) {
Log.e(TAG, "Error onNotification: " + notification + " Listener: " + listener, e);
}
}
}
@Override
public void onNotification(AccessibilityEvent event, Notification notification) {
for (NotificationListener listener : mNotificationListeners) {
try {
listener.onNotification(event, notification);
} catch (Exception e) {
Log.e(TAG, "Error onNotification: " + notification + " Listener: " + listener, e);
}
}
}
public void addListener(NotificationListener listener) {
mNotificationListeners.add(listener);
@@ -124,18 +106,9 @@ public interface NotificationListener {
@Override
public boolean onAccessibilityEvent(AccessibilityService service, AccessibilityEvent event) {
if (event.getParcelableData() instanceof Notification) {
Notification notification = (Notification) event.getParcelableData();
android.app.Notification notification = (android.app.Notification) event.getParcelableData();
Log.d(TAG, "onNotification: " + notification + "; " + event);
onNotification(event, notification);
} else {
List<CharSequence> list = event.getText();
Log.d(TAG, "onNotification: " + list + "; " + event);
if (event.getPackageName().equals(mContext.getPackageName())) {
return false;
}
if (list != null) {
onNotification(event, new NotificationInfo(event.getPackageName(), list));
}
onNotification(Notification.create(notification, event.getPackageName().toString()));
}
return false;
}
@@ -145,5 +118,16 @@ public interface NotificationListener {
public Set<Integer> getEventTypes() {
return EVENT_TYPES;
}
@Override
public void onNotification(Notification notification) {
for (NotificationListener listener : mNotificationListeners) {
try {
listener.onNotification(notification);
} catch (Exception e) {
Log.e(TAG, "Error onNotification: " + notification + " Listener: " + listener, e);
}
}
}
}
}