forked from Blendman974/faceclaw
Turn on screen when Android notifications are received
This commit is contained in:
1 parent
468291aac3
commit
5a92637537
7 files changed
+205
-12
No files matched your search
@@ -335,8 +335,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
@Override public void run() {
|
||||
logLine(String.format(Locale.US, "communicator start R=%s L=%s ring=%s", rightAddress, leftAddress, ringAddress));
|
||||
while (true) {
|
||||
try {
|
||||
@@ -367,8 +366,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
logLine("communicator stop");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNotification(String address, String characteristicUuid, byte[] data) {
|
||||
@Override public void onNotification(String address, String characteristicUuid, byte[] data) {
|
||||
if (address == null || characteristicUuid == null || data == null) {
|
||||
return;
|
||||
}
|
||||
@@ -432,8 +430,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionStateChange(String address, boolean connected) {
|
||||
@Override public void onConnectionStateChange(String address, boolean connected) {
|
||||
synchronized (lock) {
|
||||
if (address == null) {
|
||||
return;
|
||||
@@ -464,7 +461,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
private void connectLoopOnce() throws InterruptedException { //{{{
|
||||
private void connectLoopOnce() throws InterruptedException {
|
||||
setStateDisplay("connecting", "Connecting to the glasses...");
|
||||
try {
|
||||
connectArm(rightAddress, true);
|
||||
@@ -499,7 +496,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
logLine("connect failed: " + safeMessage(t));
|
||||
handleTransportFailure("connect failed");
|
||||
}
|
||||
} //}}}
|
||||
}
|
||||
|
||||
private boolean sleepDuringConnectSettling(long delayMs) throws InterruptedException {
|
||||
long deadline = SystemClock.elapsedRealtime() + delayMs;
|
||||
@@ -544,7 +541,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
private void sendPrelude() throws InterruptedException { //{{{
|
||||
private void sendPrelude() throws InterruptedException {
|
||||
synchronized (lock) {
|
||||
clearAllMessagesLocked("prelude");
|
||||
}
|
||||
@@ -577,7 +574,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
throw new IllegalStateException("prelude ack timeout");
|
||||
}
|
||||
}
|
||||
} //}}}
|
||||
}
|
||||
|
||||
private long driveSession() {
|
||||
//Log.d(TAG, "driveSession called (pendingMessages.size=" + pendingMessages.size() + " inFlightMessages.size=" + inFlightMessages.size() + ")");
|
||||
@@ -870,6 +867,7 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private void resolveAckLocked(int sid, int magic, byte[] pb) {
|
||||
Iterator<OutboundMessage> iterator = inFlightMessages.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
|
||||
+112
@@ -9,6 +9,8 @@ import android.graphics.Color;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.service.notification.NotificationListenerService;
|
||||
import android.service.notification.StatusBarNotification;
|
||||
import android.util.Log;
|
||||
@@ -18,6 +20,7 @@ import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
@@ -29,6 +32,9 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
|
||||
private static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
|
||||
|
||||
private static volatile FaceclawMediaNotificationListenerService activeService;
|
||||
private static final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
private static final Set<FaceclawNotificationListener> notificationListeners = new CopyOnWriteArraySet<>();
|
||||
private static final Set<String> activeNotificationWakeKeys = new HashSet<>();
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
@@ -48,6 +54,7 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
|
||||
public void onListenerConnected() {
|
||||
activeService = this;
|
||||
super.onListenerConnected();
|
||||
refreshActiveNotificationWakeKeys(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -58,6 +65,36 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
|
||||
super.onListenerDisconnected();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNotificationPosted(StatusBarNotification statusBarNotification) {
|
||||
super.onNotificationPosted(statusBarNotification);
|
||||
if (!shouldShowNotificationInList(this, statusBarNotification)) {
|
||||
forgetActiveNotificationWakeKey(statusBarNotification);
|
||||
return;
|
||||
}
|
||||
if (shouldEmitNotificationPosted(statusBarNotification)) {
|
||||
emitNotificationPosted(statusBarNotification.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNotificationRemoved(StatusBarNotification statusBarNotification) {
|
||||
forgetActiveNotificationWakeKey(statusBarNotification);
|
||||
super.onNotificationRemoved(statusBarNotification);
|
||||
}
|
||||
|
||||
public static void addNotificationListener(FaceclawNotificationListener listener) {
|
||||
if (listener != null) {
|
||||
notificationListeners.add(listener);
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeNotificationListener(FaceclawNotificationListener listener) {
|
||||
if (listener != null) {
|
||||
notificationListeners.remove(listener);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean hasActiveNotificationTitle(String expectedTitle) {
|
||||
FaceclawMediaNotificationListenerService service = activeService;
|
||||
if (service == null || expectedTitle == null || expectedTitle.isEmpty()) {
|
||||
@@ -231,6 +268,81 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
|
||||
}
|
||||
}
|
||||
|
||||
private static void emitNotificationPosted(String key) {
|
||||
if (key == null || key.isEmpty() || notificationListeners.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
for (FaceclawNotificationListener listener : notificationListeners) {
|
||||
mainHandler.post(() -> {
|
||||
try {
|
||||
listener.onNotificationPosted(key);
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "notification listener failed", t);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private static void refreshActiveNotificationWakeKeys(FaceclawMediaNotificationListenerService service) {
|
||||
StatusBarNotification[] notifications;
|
||||
try {
|
||||
notifications = service.getActiveNotifications();
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "failed to refresh active notification wake keys", t);
|
||||
return;
|
||||
}
|
||||
synchronized (activeNotificationWakeKeys) {
|
||||
activeNotificationWakeKeys.clear();
|
||||
if (notifications == null) {
|
||||
return;
|
||||
}
|
||||
for (StatusBarNotification statusBarNotification : notifications) {
|
||||
if (shouldShowNotificationInList(service, statusBarNotification)) {
|
||||
String key = statusBarNotification.getKey();
|
||||
if (key != null && !key.isEmpty()) {
|
||||
activeNotificationWakeKeys.add(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean shouldEmitNotificationPosted(StatusBarNotification statusBarNotification) {
|
||||
String key = statusBarNotification.getKey();
|
||||
if (key == null || key.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean alreadyActive;
|
||||
synchronized (activeNotificationWakeKeys) {
|
||||
alreadyActive = activeNotificationWakeKeys.contains(key);
|
||||
activeNotificationWakeKeys.add(key);
|
||||
}
|
||||
return !alreadyActive || !isPersistentNotification(statusBarNotification);
|
||||
}
|
||||
|
||||
private static void forgetActiveNotificationWakeKey(StatusBarNotification statusBarNotification) {
|
||||
if (statusBarNotification == null) {
|
||||
return;
|
||||
}
|
||||
String key = statusBarNotification.getKey();
|
||||
if (key == null || key.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
synchronized (activeNotificationWakeKeys) {
|
||||
activeNotificationWakeKeys.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isPersistentNotification(StatusBarNotification statusBarNotification) {
|
||||
Notification notification = statusBarNotification.getNotification();
|
||||
if (notification == null) {
|
||||
return false;
|
||||
}
|
||||
int persistentFlags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
|
||||
return (notification.flags & persistentFlags) != 0;
|
||||
}
|
||||
|
||||
private static boolean shouldShowNotificationIcon(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) {
|
||||
if (!shouldShowNotificationInList(service, statusBarNotification)) {
|
||||
return false;
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.faceclaw.app;
|
||||
|
||||
public interface FaceclawNotificationListener {
|
||||
void onNotificationPosted(String key);
|
||||
}
|
||||
Reference in new issue
Block a user