diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0d58de2..ef04932 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -75,6 +75,16 @@ + + + + + + diff --git a/app/src/main/java/com/termux/api/NotificationListAPI.java b/app/src/main/java/com/termux/api/NotificationListAPI.java new file mode 100644 index 0000000..9b32bb9 --- /dev/null +++ b/app/src/main/java/com/termux/api/NotificationListAPI.java @@ -0,0 +1,69 @@ +package com.termux.api; + +import android.app.Notification; +import android.content.Context; +import android.content.Intent; +import android.service.notification.StatusBarNotification; +import android.util.JsonWriter; + +import com.termux.api.util.ResultReturner; +import com.termux.api.util.ResultReturner.ResultJsonWriter; + + +public class NotificationListAPI { + + public static void onReceive(TermuxApiReceiver apiReceiver, final Context context, Intent intent) { + + ResultReturner.returnData(apiReceiver, intent, new ResultJsonWriter() { + @Override + public void writeJson(JsonWriter out) throws Exception { + listNotifications(context, out); + } + }); + } + + + static void listNotifications(Context context, JsonWriter out) throws Exception { + NotificationService notificationService = NotificationService.get(); + StatusBarNotification[] notifications = notificationService.getActiveNotifications(); + + out.beginArray(); + for (StatusBarNotification n : notifications) { + int id = n.getId(); + String key = ""; + String title = ""; + String text = ""; + String packageName = ""; + String tag = ""; + String group = ""; + + if (n.getNotification().extras.getCharSequence(Notification.EXTRA_TITLE) != null) { + title = n.getNotification().extras.getCharSequence(Notification.EXTRA_TITLE).toString(); + } + if (n.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT) != null) { + text = n.getNotification().extras.getCharSequence(Notification.EXTRA_TEXT).toString(); + } + if (n.getTag() != null) { + tag = n.getTag(); + } + if (n.getNotification().getGroup() != null) { + group = n.getNotification().getGroup(); + } + if (n.getKey() != null) { + key = n.getKey(); + } + if (n.getPackageName() != null) { + packageName = n.getPackageName(); + } + out.beginObject() + .name("id").value(id) + .name("tag").value(tag) + .name("key").value(key) + .name("group").value(group) + .name("packageName").value(packageName) + .name("title").value(title) + .name("content").value(text).endObject(); + } + out.endArray(); + } + } diff --git a/app/src/main/java/com/termux/api/NotificationService.java b/app/src/main/java/com/termux/api/NotificationService.java new file mode 100644 index 0000000..25b4ab7 --- /dev/null +++ b/app/src/main/java/com/termux/api/NotificationService.java @@ -0,0 +1,22 @@ +package com.termux.api; + +import android.service.notification.NotificationListenerService; + +public class NotificationService extends NotificationListenerService { + static NotificationService _this; + + public static NotificationService get() { + NotificationService ret = _this; + return ret; + } + + @Override + public void onListenerConnected() { + _this = this; + } + + @Override + public void onListenerDisconnected() { + _this = null; + } +} diff --git a/app/src/main/java/com/termux/api/TermuxApiReceiver.java b/app/src/main/java/com/termux/api/TermuxApiReceiver.java index b7ba74d..b60bcf3 100644 --- a/app/src/main/java/com/termux/api/TermuxApiReceiver.java +++ b/app/src/main/java/com/termux/api/TermuxApiReceiver.java @@ -1,7 +1,9 @@ package com.termux.api; import android.Manifest; +import android.app.Notification; import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.Build; @@ -98,6 +100,17 @@ public class TermuxApiReceiver extends BroadcastReceiver { MicRecorderAPI.onReceive(context, intent); } break; + case "NotificationList": + ComponentName cn = new ComponentName(context, NotificationService.class); + String flat = Settings.Secure.getString(context.getContentResolver(), "enabled_notification_listeners"); + final boolean NotificationServiceEnabled = flat != null && flat.contains(cn.flattenToString()); + if (!NotificationServiceEnabled) { + Toast.makeText(context,"Please give Termux:API Notification Access", Toast.LENGTH_LONG).show(); + context.startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")); + } else { + NotificationListAPI.onReceive(this, context, intent); + } + break; case "Notification": NotificationAPI.onReceiveShowNotification(this, context, intent); break;