You've already forked android_translation_layer
mirror of
https://gitlab.com/android_translation_layer/android_translation_layer.git
synced 2025-10-27 11:48:10 -07:00
notification support using libportal
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user