notification support using libportal

This commit is contained in:
Julian Winkler
2024-03-17 11:05:42 +01:00
parent b14549e639
commit 45de09a191
9 changed files with 286 additions and 19 deletions

View File

@@ -1,5 +1,8 @@
package android.app;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.AudioAttributes;
@@ -44,8 +47,21 @@ public class Notification {
public Bundle extras;
String text;
String title;
List<Action> actions = new ArrayList<Action>();
PendingIntent intent;
public String toString() {
return "Notification [" + title + ", " + text + ", " + actions + "]";
}
public static class Builder {
public Builder(Context context) {}
private Notification notification;
public Builder(Context context) {
notification = new Notification();
}
public Builder setWhen(long when) {return this;}
@@ -67,13 +83,22 @@ public class Notification {
public Builder setDefaults(int defaults) {return this;}
public Builder setContentTitle(CharSequence title) {return this;}
public Builder setContentTitle(CharSequence title) {
notification.title = title != null ? title.toString() : null;
return this;
}
public Builder setContentText(CharSequence text) {return this;}
public Builder setContentText(CharSequence text) {
notification.text = text != null ? text.toString() : null;
return this;
}
public Builder setContentInfo(CharSequence info) {return this;}
public Builder setContentIntent(PendingIntent intent) {return this;}
public Builder setContentIntent(PendingIntent intent) {
notification.intent = intent;
return this;
}
public Builder setDeleteIntent(PendingIntent intent) {return this;}
@@ -111,26 +136,44 @@ public class Notification {
public Builder setSound(Uri sound, AudioAttributes audioAttributes) {return this;}
public Builder addAction(Action action) {return this;}
public Builder addAction(Action action) {
notification.actions.add(action);
return this;
}
public Builder setStyle(Style style) {return this;}
public Builder setExtras(Bundle extras) {return this;}
public Notification build() {
return new Notification();
return notification;
}
}
public static class Action {
int icon;
String title;
PendingIntent intent;
public String toString() {
return "Action [" + icon + ", " + title + ", " + intent + "]";
}
public static final class Builder {
public Builder(int icon, CharSequence title, PendingIntent intent) {}
private Action action;
public Builder(int icon, CharSequence title, PendingIntent intent) {
action = new Action();
action.icon = icon;
action.title = String.valueOf(title);
action.intent = intent;
}
public Builder addExtras(Bundle extras) {return this;}
public Action build() {
return new Action();
return action;
}
}
}

View File

@@ -1,13 +1,59 @@
package android.app;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
public class NotificationManager {
public void cancelAll() {}
public void notify(String tag, int id, Notification notification) {
System.out.println("notify(" + tag + ", " + id + ", " + notification + ") called");
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;
}
nativeShowNotification(builder, id, notification.title, notification.text, intentType, actionName, className);
}
public void notify(int id, Notification notification) {
System.out.println("notify(" + id + ", " + notification + ") called");
notify(null, id, notification);
}
public void cancel(String tag, int id) {}
protected static void notificationActionCallback(int id, int intentType, String action, String className) {
Context context = Context.this_application;
Intent intent = new Intent(action);
if (className != null && !className.isEmpty()) {
intent.setComponent(new ComponentName(context, className));
}
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);
}
}
protected native long nativeInitBuilder();
protected native void nativeAddAction(long builder, String title, int intentType, String action, String className);
protected native void nativeShowNotification(long builder, int id, String title, String text, int intentType, String action, String className);
}

View File

@@ -5,8 +5,18 @@ import android.content.Intent;
import android.content.IntentSender;
public class PendingIntent {
private int requestCode;
Intent intent;
int type; // 0: activity, 1: service, 2: broadcast
private PendingIntent(int requestCode, Intent intent, int type) {
this.requestCode = requestCode;
this.intent = intent;
this.type = type;
}
public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags) {
return new PendingIntent();
return new PendingIntent(requestCode, intent, 2);
}
public IntentSender getIntentSender() {
@@ -16,11 +26,16 @@ public class PendingIntent {
public void send(Context context, int code, Intent intent) {}
public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags) {
return new PendingIntent();
return new PendingIntent(requestCode, intent, 0);
}
public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags) {
return new PendingIntent();
return new PendingIntent(requestCode, intent, 1);
}
public String toString() {
return "PendingIntent [requestCode=" + requestCode + ", intent=" + intent + ", type="
+ new String[] { "activity", "service", "broadcast" }[type] + "]";
}
public class CanceledException extends Exception {

View File

@@ -1,4 +1,6 @@
package android.content;
public class BroadcastReceiver {
public abstract class BroadcastReceiver {
public abstract void onReceive(Context context, Intent intent);
}

View File

@@ -82,6 +82,8 @@ public class Context extends Object {
File obb_dir = null;
File cache_dir = null;
private static Map<IntentFilter, BroadcastReceiver> receiverMap = new HashMap<IntentFilter, BroadcastReceiver>();
static {
assets = new AssetManager();
dm = new DisplayMetrics();
@@ -202,6 +204,7 @@ public class Context extends Object {
}
public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
receiverMap.put(filter, receiver);
return new Intent();
}
@@ -467,7 +470,13 @@ public class Context extends Object {
return new File(databaseDir, dbName);
}
public void sendBroadcast(Intent intent) {}
public void sendBroadcast(Intent intent) {
for (IntentFilter filter : receiverMap.keySet()) {
if (filter.matchAction(intent.getAction())) {
receiverMap.get(filter).onReceive(this, intent);
}
}
}
public boolean stopService(Intent intent) {return false;}

View File

@@ -1,9 +1,25 @@
package android.content;
public class IntentFilter {
public IntentFilter() {}
public IntentFilter(String dummy) {}
import java.util.HashSet;
import java.util.Set;
public void addAction(String action) {}
public int countActions() { return 0; /*maybe?*/ }
public class IntentFilter {
private Set<String> actions = new HashSet<>();
public IntentFilter() {}
public IntentFilter(String action) {
addAction(action);
}
public void addAction(String action) {
actions.add(action);
}
public int countActions() {
return actions.size();
}
public final boolean matchAction(String action) {
return actions.contains(action);
}
}