这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
69 changes: 69 additions & 0 deletions app/src/main/java/com/termux/api/NotificationListAPI.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/termux/api/NotificationService.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/com/termux/api/TermuxApiReceiver.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -98,6 +100,17 @@ public void onReceive(Context context, Intent intent) {
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;
Expand Down