1
0
mirror of https://github.com/danog/termux-api.git synced 2024-11-26 20:04:42 +01:00

NotificationList API (#210)

This commit is contained in:
Kieran Coldron 2018-08-25 23:56:15 +01:00 committed by Fredrik Fornwall
parent 5b3f31bf14
commit f29350a842
4 changed files with 114 additions and 0 deletions

View File

@ -75,6 +75,16 @@
<service android:name=".MediaPlayerAPI$PlayerService" android:exported="false"/>
<service android:name=".MicRecorderAPI$MicRecorderService" android:exported="false"/>
<service android:name=".WallpaperAPI$WallpaperService"/>
<service
android:name=".NotificationService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
</application>
</manifest>

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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;