2023-01-14 14:32:37 +01:00
|
|
|
package android.app;
|
|
|
|
|
2024-10-28 19:06:17 +01:00
|
|
|
import java.util.Collections;
|
2024-07-20 08:25:52 +02:00
|
|
|
import java.util.HashMap;
|
2024-10-28 19:06:17 +01:00
|
|
|
import java.util.List;
|
2024-07-20 08:25:52 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
2024-07-15 16:39:45 +02:00
|
|
|
import android.app.Notification.MediaStyle;
|
2024-03-17 11:05:42 +01:00
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2024-03-18 18:13:56 +01:00
|
|
|
import android.os.Handler;
|
2024-06-15 22:32:01 +02:00
|
|
|
import android.os.Looper;
|
2024-03-17 11:05:42 +01:00
|
|
|
|
2023-01-14 14:32:37 +01:00
|
|
|
public class NotificationManager {
|
2024-07-20 14:13:12 +02:00
|
|
|
|
|
|
|
private static int mpris_notification_id = -1;
|
|
|
|
|
2024-07-20 08:25:52 +02:00
|
|
|
// store Intents in map, as long as Parcelable serialization is not yet implemented
|
|
|
|
private static Map<Integer, Intent> intents = new HashMap<Integer, Intent>();
|
|
|
|
|
2023-01-14 14:32:37 +01:00
|
|
|
public void cancelAll() {}
|
2023-09-21 22:49:36 +02:00
|
|
|
|
|
|
|
public void notify(String tag, int id, Notification notification) {
|
2024-07-16 22:10:44 +02:00
|
|
|
if (notification.style instanceof MediaStyle) { // MPRIS content is handled by MediaSession implementation
|
2024-07-20 14:13:12 +02:00
|
|
|
if (mpris_notification_id == -1) {
|
|
|
|
nativeShowMPRIS(Context.this_application.getPackageName(), Context.this_application.get_app_label());
|
|
|
|
mpris_notification_id = id;
|
|
|
|
}
|
2024-07-16 22:10:44 +02:00
|
|
|
return;
|
2024-07-15 16:39:45 +02:00
|
|
|
}
|
|
|
|
|
2023-09-21 22:49:36 +02:00
|
|
|
System.out.println("notify(" + tag + ", " + id + ", " + notification + ") called");
|
2024-03-17 11:05:42 +01:00
|
|
|
long builder = nativeInitBuilder();
|
|
|
|
for (Notification.Action action : notification.actions) {
|
|
|
|
int intentType = -1;
|
|
|
|
String actionName = null;
|
|
|
|
String className = null;
|
|
|
|
if (action.intent != null) {
|
|
|
|
intentType = action.intent.type;
|
|
|
|
actionName = action.intent.intent.getAction();
|
|
|
|
className = action.intent.intent.getComponent() != null ? action.intent.intent.getComponent().getClassName() : null;
|
|
|
|
}
|
|
|
|
nativeAddAction(builder, action.title, intentType, actionName, className);
|
|
|
|
}
|
|
|
|
int intentType = -1;
|
|
|
|
String actionName = null;
|
|
|
|
String className = null;
|
|
|
|
if (notification.intent != null) {
|
|
|
|
intentType = notification.intent.type;
|
|
|
|
actionName = notification.intent.intent.getAction();
|
|
|
|
className = notification.intent.intent.getComponent() != null ? notification.intent.intent.getComponent().getClassName() : null;
|
2024-07-20 08:25:52 +02:00
|
|
|
intents.put(id, notification.intent.intent);
|
2024-03-17 11:05:42 +01:00
|
|
|
}
|
2024-03-18 15:41:44 +01:00
|
|
|
nativeShowNotification(builder, id, notification.title, notification.text, notification.iconPath, notification.ongoing, intentType, actionName, className);
|
2023-09-21 22:49:36 +02:00
|
|
|
}
|
2024-03-16 12:49:28 +01:00
|
|
|
|
|
|
|
public void notify(int id, Notification notification) {
|
2024-03-17 11:05:42 +01:00
|
|
|
notify(null, id, notification);
|
2024-03-16 12:49:28 +01:00
|
|
|
}
|
2024-03-17 11:05:42 +01:00
|
|
|
|
2024-03-18 18:13:56 +01:00
|
|
|
public void cancel(String tag, final int id) {
|
|
|
|
// remove_notification doesn't work reliably when sent directly after add_notification in GNOME session.
|
|
|
|
// So we give some extra delay here.
|
2024-06-15 22:32:01 +02:00
|
|
|
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
2024-03-18 18:13:56 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2024-07-20 14:13:12 +02:00
|
|
|
if (mpris_notification_id == id) {
|
|
|
|
mpris_notification_id = -1;
|
|
|
|
nativeCancelMPRIS();
|
|
|
|
} else {
|
|
|
|
nativeCancel(id);
|
|
|
|
}
|
2024-03-18 18:13:56 +01:00
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancel(int id) {
|
|
|
|
cancel(null, id);
|
|
|
|
}
|
2024-03-17 11:05:42 +01:00
|
|
|
|
|
|
|
protected static void notificationActionCallback(int id, int intentType, String action, String className) {
|
|
|
|
Context context = Context.this_application;
|
2024-07-20 08:25:52 +02:00
|
|
|
action = "".equals(action) ? null : action;
|
|
|
|
className = "".equals(className) ? null : className;
|
|
|
|
Intent intent = intents.remove(id);
|
2025-02-09 16:47:13 +01:00
|
|
|
if (intent == null || !Objects.equals(action, intent.getAction()) || !Objects.equals(className, intent.getComponent() == null ? null : intent.getComponent().getClassName())) {
|
2024-07-20 08:25:52 +02:00
|
|
|
intent = new Intent(action);
|
|
|
|
if (className != null) {
|
|
|
|
intent.setComponent(new ComponentName(context, className));
|
|
|
|
}
|
2024-03-17 11:05:42 +01:00
|
|
|
}
|
|
|
|
if (intentType == 0) { // type Activity
|
|
|
|
context.startActivity(intent);
|
|
|
|
} else if (intentType == 1) { // type Service
|
|
|
|
context.startService(intent);
|
|
|
|
} else if (intentType == 2) { // type Broadcast
|
|
|
|
context.sendBroadcast(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-10 23:23:14 +01:00
|
|
|
public void createNotificationChannel(NotificationChannel channel) {}
|
|
|
|
|
2024-03-17 11:05:42 +01:00
|
|
|
protected native long nativeInitBuilder();
|
|
|
|
protected native void nativeAddAction(long builder, String title, int intentType, String action, String className);
|
2024-03-18 15:41:44 +01:00
|
|
|
protected native void nativeShowNotification(long builder, int id, String title, String text, String iconPath, boolean ongoing, int intentType, String action, String className);
|
2024-07-16 22:10:44 +02:00
|
|
|
protected native void nativeShowMPRIS(String packageName, String identiy);
|
2024-03-18 18:13:56 +01:00
|
|
|
protected native void nativeCancel(int id);
|
2024-07-20 14:13:12 +02:00
|
|
|
protected native void nativeCancelMPRIS();
|
2024-10-28 19:06:17 +01:00
|
|
|
|
|
|
|
public void createNotificationChannelGroup(NotificationChannelGroup v) {}
|
|
|
|
|
|
|
|
public List<NotificationChannel> getNotificationChannels() {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<NotificationChannelGroup> getNotificationChannelGroups() {
|
|
|
|
return Collections.emptyList();
|
|
|
|
}
|
2025-04-21 10:19:48 +02:00
|
|
|
|
|
|
|
public boolean areNotificationsEnabled() {
|
|
|
|
return true;
|
|
|
|
}
|
2023-01-14 14:32:37 +01:00
|
|
|
}
|