forked from Blendman974/faceclaw
Notifications menu; UI refinements; teleprompt by sharing a text file
This commit is contained in:
1 parent
ef6cb3bf55
commit
8d6fe60c79
12 files changed
+991
-36
No files matched your search
+256
-3
@@ -2,6 +2,7 @@ package com.faceclaw.app;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
@@ -13,10 +14,19 @@ import android.service.notification.StatusBarNotification;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class FaceclawMediaNotificationListenerService extends NotificationListenerService {
|
||||
private static final String TAG = "FaceclawNotify";
|
||||
private static final double NOTIFICATION_ICON_GAMMA = 1.6;
|
||||
private static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
|
||||
|
||||
private static volatile FaceclawMediaNotificationListenerService activeService;
|
||||
|
||||
@@ -105,16 +115,24 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
|
||||
}
|
||||
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(size * size * Math.min(limit, notifications.length));
|
||||
Set<String> emittedGroupKeys = new HashSet<>();
|
||||
int emitted = 0;
|
||||
for (StatusBarNotification statusBarNotification : notifications) {
|
||||
if (!shouldShowNotificationIcon(service, statusBarNotification)) {
|
||||
continue;
|
||||
}
|
||||
String dedupeGroupKey = getNotificationDedupeGroupKey(statusBarNotification);
|
||||
if (dedupeGroupKey != null && emittedGroupKeys.contains(dedupeGroupKey)) {
|
||||
continue;
|
||||
}
|
||||
Drawable drawable = loadNotificationIcon(service, statusBarNotification.getNotification());
|
||||
if (drawable == null) {
|
||||
continue;
|
||||
}
|
||||
appendIconGrayBytes(drawable, size, out);
|
||||
if (dedupeGroupKey != null) {
|
||||
emittedGroupKeys.add(dedupeGroupKey);
|
||||
}
|
||||
emitted += 1;
|
||||
if (emitted >= limit) {
|
||||
break;
|
||||
@@ -123,7 +141,108 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public static String getActiveNotificationsJson(int maxNotifications) {
|
||||
FaceclawMediaNotificationListenerService service = activeService;
|
||||
int limit = Math.max(0, Math.min(100, maxNotifications));
|
||||
if (service == null || limit == 0) {
|
||||
return "[]";
|
||||
}
|
||||
|
||||
StatusBarNotification[] notifications;
|
||||
try {
|
||||
notifications = service.getActiveNotifications();
|
||||
} catch (SecurityException e) {
|
||||
Log.w(TAG, "notification access denied while reading notifications", e);
|
||||
return "[]";
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "failed to read notifications", t);
|
||||
return "[]";
|
||||
}
|
||||
if (notifications == null || notifications.length == 0) {
|
||||
return "[]";
|
||||
}
|
||||
|
||||
Arrays.sort(notifications, new Comparator<StatusBarNotification>() {
|
||||
@Override
|
||||
public int compare(StatusBarNotification a, StatusBarNotification b) {
|
||||
long left = a == null ? 0 : a.getPostTime();
|
||||
long right = b == null ? 0 : b.getPostTime();
|
||||
return Long.compare(right, left);
|
||||
}
|
||||
});
|
||||
|
||||
JSONArray out = new JSONArray();
|
||||
for (StatusBarNotification statusBarNotification : notifications) {
|
||||
if (out.length() >= limit) {
|
||||
break;
|
||||
}
|
||||
if (!shouldShowNotificationInList(service, statusBarNotification)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
out.put(buildNotificationJson(service, statusBarNotification));
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "failed to serialize notification", t);
|
||||
}
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
public static boolean invokeNotificationAction(String key, int actionIndex) {
|
||||
FaceclawMediaNotificationListenerService service = activeService;
|
||||
StatusBarNotification statusBarNotification = findActiveNotificationByKey(service, key);
|
||||
if (statusBarNotification == null || statusBarNotification.getNotification() == null) {
|
||||
return false;
|
||||
}
|
||||
Notification.Action[] actions = statusBarNotification.getNotification().actions;
|
||||
if (actions == null || actionIndex < 0 || actionIndex >= actions.length) {
|
||||
return false;
|
||||
}
|
||||
PendingIntent intent = actions[actionIndex].actionIntent;
|
||||
if (intent == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
intent.send();
|
||||
return true;
|
||||
} catch (PendingIntent.CanceledException e) {
|
||||
Log.w(TAG, "notification action pending intent was canceled", e);
|
||||
return false;
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "failed to invoke notification action", t);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean dismissNotification(String key) {
|
||||
FaceclawMediaNotificationListenerService service = activeService;
|
||||
if (service == null || key == null || key.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
service.cancelNotification(key);
|
||||
return true;
|
||||
} catch (SecurityException e) {
|
||||
Log.w(TAG, "notification access denied while dismissing notification", e);
|
||||
return false;
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "failed to dismiss notification", t);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean shouldShowNotificationIcon(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) {
|
||||
if (!shouldShowNotificationInList(service, statusBarNotification)) {
|
||||
return false;
|
||||
}
|
||||
Notification notification = statusBarNotification.getNotification();
|
||||
if ((notification.flags & Notification.FLAG_GROUP_SUMMARY) != 0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean shouldShowNotificationInList(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) {
|
||||
if (statusBarNotification == null || statusBarNotification.getNotification() == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -131,9 +250,6 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
|
||||
return false;
|
||||
}
|
||||
Notification notification = statusBarNotification.getNotification();
|
||||
if ((notification.flags & Notification.FLAG_GROUP_SUMMARY) != 0) {
|
||||
return false;
|
||||
}
|
||||
if (Notification.CATEGORY_TRANSPORT.equals(notification.category)) {
|
||||
return false;
|
||||
}
|
||||
@@ -154,6 +270,19 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
|
||||
return importance > NotificationManager.IMPORTANCE_MIN;
|
||||
}
|
||||
|
||||
private static String getNotificationDedupeGroupKey(StatusBarNotification statusBarNotification) {
|
||||
Notification notification = statusBarNotification.getNotification();
|
||||
if (notification.getGroup() == null && statusBarNotification.getOverrideGroupKey() == null) {
|
||||
return null;
|
||||
}
|
||||
String groupKey = statusBarNotification.getGroupKey();
|
||||
if (groupKey == null || groupKey.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
// Group children often share the same small icon. Emit only one icon for the group.
|
||||
return groupKey;
|
||||
}
|
||||
|
||||
private static Drawable loadNotificationIcon(FaceclawMediaNotificationListenerService service, Notification notification) {
|
||||
try {
|
||||
Icon smallIcon = notification.getSmallIcon();
|
||||
@@ -193,4 +322,128 @@ public class FaceclawMediaNotificationListenerService extends NotificationListen
|
||||
}
|
||||
bitmap.recycle();
|
||||
}
|
||||
|
||||
private static StatusBarNotification findActiveNotificationByKey(FaceclawMediaNotificationListenerService service, String key) {
|
||||
if (service == null || key == null || key.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
StatusBarNotification[] notifications;
|
||||
try {
|
||||
notifications = service.getActiveNotifications();
|
||||
} catch (Throwable t) {
|
||||
Log.w(TAG, "failed to find active notification", t);
|
||||
return null;
|
||||
}
|
||||
if (notifications == null) {
|
||||
return null;
|
||||
}
|
||||
for (StatusBarNotification statusBarNotification : notifications) {
|
||||
if (statusBarNotification != null && key.equals(statusBarNotification.getKey())) {
|
||||
return statusBarNotification;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static JSONObject buildNotificationJson(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification)
|
||||
throws JSONException {
|
||||
Notification notification = statusBarNotification.getNotification();
|
||||
Bundle extras = notification.extras;
|
||||
JSONObject out = new JSONObject();
|
||||
out.put("key", statusBarNotification.getKey());
|
||||
out.put("packageName", statusBarNotification.getPackageName());
|
||||
out.put("appName", getNotificationAppName(service, statusBarNotification));
|
||||
out.put("postTime", statusBarNotification.getPostTime());
|
||||
out.put("when", notification.when);
|
||||
putString(out, "category", notification.category);
|
||||
if (extras != null) {
|
||||
putCharSequence(out, "title", firstNonEmpty(
|
||||
extras.getCharSequence(Notification.EXTRA_TITLE_BIG),
|
||||
extras.getCharSequence(Notification.EXTRA_TITLE)
|
||||
));
|
||||
putCharSequence(out, "text", extras.getCharSequence(Notification.EXTRA_TEXT));
|
||||
putCharSequence(out, "bigText", extras.getCharSequence(Notification.EXTRA_BIG_TEXT));
|
||||
putCharSequence(out, "subText", extras.getCharSequence(Notification.EXTRA_SUB_TEXT));
|
||||
putCharSequence(out, "infoText", extras.getCharSequence(Notification.EXTRA_INFO_TEXT));
|
||||
putCharSequence(out, "summaryText", extras.getCharSequence(Notification.EXTRA_SUMMARY_TEXT));
|
||||
CharSequence[] textLines = extras.getCharSequenceArray(Notification.EXTRA_TEXT_LINES);
|
||||
JSONArray lines = new JSONArray();
|
||||
if (textLines != null) {
|
||||
for (CharSequence line : textLines) {
|
||||
String text = charSequenceToString(line);
|
||||
if (!text.isEmpty()) {
|
||||
lines.put(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
out.put("lines", lines);
|
||||
} else {
|
||||
out.put("lines", new JSONArray());
|
||||
}
|
||||
|
||||
JSONArray actionsJson = new JSONArray();
|
||||
Notification.Action[] actions = notification.actions;
|
||||
if (actions != null) {
|
||||
for (int index = 0; index < actions.length; index++) {
|
||||
Notification.Action action = actions[index];
|
||||
if (action == null) {
|
||||
continue;
|
||||
}
|
||||
String title = charSequenceToString(action.title);
|
||||
if (title.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
JSONObject actionJson = new JSONObject();
|
||||
actionJson.put("index", index);
|
||||
actionJson.put("title", title);
|
||||
actionJson.put("enabled", action.actionIntent != null);
|
||||
actionsJson.put(actionJson);
|
||||
}
|
||||
}
|
||||
out.put("actions", actionsJson);
|
||||
return out;
|
||||
}
|
||||
|
||||
private static String getNotificationAppName(FaceclawMediaNotificationListenerService service, StatusBarNotification statusBarNotification) {
|
||||
Notification notification = statusBarNotification.getNotification();
|
||||
Bundle extras = notification.extras;
|
||||
if (extras != null) {
|
||||
String substituteName = charSequenceToString(extras.getCharSequence(EXTRA_SUBSTITUTE_APP_NAME));
|
||||
if (!substituteName.isEmpty()) {
|
||||
return substituteName;
|
||||
}
|
||||
}
|
||||
return getAppLabel(service, statusBarNotification.getPackageName());
|
||||
}
|
||||
|
||||
private static String getAppLabel(FaceclawMediaNotificationListenerService service, String packageName) {
|
||||
if (packageName == null || packageName.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
CharSequence label = service
|
||||
.getPackageManager()
|
||||
.getApplicationLabel(service.getPackageManager().getApplicationInfo(packageName, 0));
|
||||
String text = charSequenceToString(label);
|
||||
return text.isEmpty() ? packageName : text;
|
||||
} catch (Throwable t) {
|
||||
return packageName;
|
||||
}
|
||||
}
|
||||
|
||||
private static void putString(JSONObject out, String key, String value) throws JSONException {
|
||||
out.put(key, value == null ? "" : value);
|
||||
}
|
||||
|
||||
private static void putCharSequence(JSONObject out, String key, CharSequence value) throws JSONException {
|
||||
out.put(key, charSequenceToString(value));
|
||||
}
|
||||
|
||||
private static CharSequence firstNonEmpty(CharSequence first, CharSequence second) {
|
||||
return charSequenceToString(first).isEmpty() ? second : first;
|
||||
}
|
||||
|
||||
private static String charSequenceToString(CharSequence value) {
|
||||
return value == null ? "" : value.toString();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user