handle 'SEND' intent by copying to clipboard, make Activity subclass ContextWrapper, code style fixes

This commit is contained in:
Mis012
2024-02-10 20:37:28 +01:00
parent 50ba9b952a
commit 056b911637
7 changed files with 69 additions and 29 deletions

View File

@@ -11,7 +11,8 @@
static GList *activity_backlog = NULL; static GList *activity_backlog = NULL;
static jobject activity_current = NULL; static jobject activity_current = NULL;
static void activity_close(JNIEnv *env, jobject activity) { static void activity_close(JNIEnv *env, jobject activity)
{
// in case some exception was left unhandled in native code, print it here so we don't confuse it with an exception thrown by onDestroy // in case some exception was left unhandled in native code, print it here so we don't confuse it with an exception thrown by onDestroy
if((*env)->ExceptionCheck(env)) { if((*env)->ExceptionCheck(env)) {
fprintf(stderr, "app_exit: seems there was a pending exception... :"); fprintf(stderr, "app_exit: seems there was a pending exception... :");
@@ -24,7 +25,8 @@ static void activity_close(JNIEnv *env, jobject activity) {
(*env)->ExceptionDescribe(env); (*env)->ExceptionDescribe(env);
} }
static void activity_update_current(JNIEnv *env) { static void activity_update_current(JNIEnv *env)
{
jobject activity_new = activity_backlog ? g_list_first(activity_backlog)->data : NULL; jobject activity_new = activity_backlog ? g_list_first(activity_backlog)->data : NULL;
if (activity_current != activity_new) { if (activity_current != activity_new) {
@@ -59,7 +61,8 @@ static void activity_update_current(JNIEnv *env) {
} }
} }
void activity_window_ready(void) { void activity_window_ready(void)
{
JNIEnv *env = get_jni_env(); JNIEnv *env = get_jni_env();
for (GList *l = activity_backlog; l != NULL; l = l->next) { for (GList *l = activity_backlog; l != NULL; l = l->next) {
@@ -69,7 +72,8 @@ void activity_window_ready(void) {
} }
} }
void activity_close_all(void) { void activity_close_all(void)
{
GList *activities, *l; GList *activities, *l;
JNIEnv *env = get_jni_env(); JNIEnv *env = get_jni_env();
// local backup of the backlog // local backup of the backlog
@@ -85,7 +89,8 @@ void activity_close_all(void) {
g_list_free(activities); g_list_free(activities);
} }
void activity_start(JNIEnv *env, jobject activity_object) { void activity_start(JNIEnv *env, jobject activity_object)
{
/* -- run the activity's onCreate -- */ /* -- run the activity's onCreate -- */
(*env)->CallVoidMethod(env, activity_object, handle_cache.activity.onCreate, NULL); (*env)->CallVoidMethod(env, activity_object, handle_cache.activity.onCreate, NULL);
if((*env)->ExceptionCheck(env)) if((*env)->ExceptionCheck(env))
@@ -95,7 +100,8 @@ void activity_start(JNIEnv *env, jobject activity_object) {
activity_update_current(env); activity_update_current(env);
} }
JNIEXPORT void JNICALL Java_android_app_Activity_nativeFinish(JNIEnv *env, jobject this, jlong window) { JNIEXPORT void JNICALL Java_android_app_Activity_nativeFinish(JNIEnv *env, jobject this, jlong window)
{
GList *l; GList *l;
jobject removed_activity = NULL; jobject removed_activity = NULL;
for (l = activity_backlog; l != NULL; l = l->next) { for (l = activity_backlog; l != NULL; l = l->next) {
@@ -113,17 +119,29 @@ JNIEXPORT void JNICALL Java_android_app_Activity_nativeFinish(JNIEnv *env, jobje
gtk_window_close(GTK_WINDOW(_PTR(window))); gtk_window_close(GTK_WINDOW(_PTR(window)));
} }
JNIEXPORT void JNICALL Java_android_app_Activity_nativeStartActivity(JNIEnv *env, jclass class, jobject activity) { JNIEXPORT void JNICALL Java_android_app_Activity_nativeStartActivity(JNIEnv *env, jclass class, jobject activity)
{
activity_start(env, activity); activity_start(env, activity);
} }
static XdpPortal *portal = NULL; JNIEXPORT void JNICALL Java_android_app_Activity_nativeOpenURI(JNIEnv *env, jclass class, jstring uriString)
{
JNIEXPORT void JNICALL Java_android_app_Activity_nativeOpenURI(JNIEnv *env, jclass class, jstring uriString) { static XdpPortal *portal = NULL;
if (!portal) { if (!portal) {
portal = xdp_portal_new(); portal = xdp_portal_new();
} }
const char* uri = (*env)->GetStringUTFChars(env, uriString, NULL);
const char *uri = (*env)->GetStringUTFChars(env, uriString, NULL);
xdp_portal_open_uri(portal, NULL, uri, XDP_OPEN_URI_FLAG_NONE, NULL, NULL, NULL); xdp_portal_open_uri(portal, NULL, uri, XDP_OPEN_URI_FLAG_NONE, NULL, NULL, NULL);
(*env)->ReleaseStringUTFChars(env, uriString, uri); (*env)->ReleaseStringUTFChars(env, uriString, uri);
} }
extern GtkWindow *window; // TODO: get this in a better way
JNIEXPORT void JNICALL Java_android_app_Activity_nativeShare(JNIEnv *env, jclass class, jstring text_jstring)
{
const char *text = (*env)->GetStringUTFChars(env, text_jstring, NULL);
GdkClipboard *clipboard = gdk_display_get_clipboard(gtk_root_get_display(GTK_ROOT(window)));
gdk_clipboard_set_text(clipboard, text);
(*env)->ReleaseStringUTFChars(env, text_jstring, text);
}

View File

@@ -33,6 +33,14 @@ JNIEXPORT void JNICALL Java_android_app_Activity_nativeStartActivity
JNIEXPORT void JNICALL Java_android_app_Activity_nativeOpenURI JNIEXPORT void JNICALL Java_android_app_Activity_nativeOpenURI
(JNIEnv *, jclass, jstring); (JNIEnv *, jclass, jstring);
/*
* Class: android_app_Activity
* Method: nativeShare
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_android_app_Activity_nativeShare
(JNIEnv *, jclass, jstring);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -22,19 +22,20 @@ static void location_updated (
(*env)->CallStaticVoidMethod(env, class, _STATIC_METHOD(class, "locationUpdated", "(DDD)V"), latitude, longitude, heading); (*env)->CallStaticVoidMethod(env, class, _STATIC_METHOD(class, "locationUpdated", "(DDD)V"), latitude, longitude, heading);
} }
static XdpPortal *portal = NULL;
JNIEXPORT void JNICALL Java_android_location_LocationManager_nativeGetLocation(JNIEnv *env, jobject) { JNIEXPORT void JNICALL Java_android_location_LocationManager_nativeGetLocation(JNIEnv *env, jobject) {
if (!getenv("ATL_UGLY_ENABLE_LOCATION")) { if (!getenv("ATL_UGLY_ENABLE_LOCATION")) {
// Location access is prohibited by default until sanboxing is implemented. // Location access is prohibited by default until sanboxing is implemented.
// Set ATL_UGLY_ENABLE_LOCATION environment variable to enable it. // Set ATL_UGLY_ENABLE_LOCATION environment variable to enable it.
return; return;
} }
static XdpPortal *portal = NULL;
if (!portal) { if (!portal) {
portal = xdp_portal_new(); portal = xdp_portal_new();
JavaVM *jvm; JavaVM *jvm;
(*env)->GetJavaVM(env, &jvm); (*env)->GetJavaVM(env, &jvm);
g_signal_connect(portal, "location-updated", G_CALLBACK(location_updated), jvm); g_signal_connect(portal, "location-updated", G_CALLBACK(location_updated), jvm);
} }
xdp_portal_location_monitor_start (portal, NULL, 0, 0, XDP_LOCATION_ACCURACY_EXACT, XDP_LOCATION_MONITOR_FLAG_NONE, NULL, NULL, NULL); xdp_portal_location_monitor_start (portal, NULL, 0, 0, XDP_LOCATION_ACCURACY_EXACT, XDP_LOCATION_MONITOR_FLAG_NONE, NULL, NULL, NULL);
} }

View File

@@ -4,6 +4,7 @@ import android.R;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent; import android.content.Intent;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.XmlResourceParser; import android.content.res.XmlResourceParser;
@@ -27,7 +28,7 @@ import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Activity extends Context implements Window.Callback { public class Activity extends ContextWrapper implements Window.Callback {
LayoutInflater layout_inflater; LayoutInflater layout_inflater;
Window window = new Window(this); Window window = new Window(this);
int requested_orientation = -1 /*ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED*/; // dummy int requested_orientation = -1 /*ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED*/; // dummy
@@ -64,6 +65,7 @@ public class Activity extends Context implements Window.Callback {
} }
public Activity() { public Activity() {
super(null);
layout_inflater = new LayoutInflater(); layout_inflater = new LayoutInflater();
intent = new Intent(); intent = new Intent();
} }
@@ -320,10 +322,6 @@ public class Activity extends Context implements Window.Callback {
public boolean isChangingConfigurations() {return false;} public boolean isChangingConfigurations() {return false;}
private native void nativeFinish(long native_window);
public static native void nativeStartActivity(Activity activity);
public static native void nativeOpenURI(String uri);
@Override @Override
public void onContentChanged() { public void onContentChanged() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
@@ -393,4 +391,9 @@ public class Activity extends Context implements Window.Callback {
public void setIntent(Intent newIntent) {} public void setIntent(Intent newIntent) {}
public void unregisterReceiver(BroadcastReceiver receiver) {} public void unregisterReceiver(BroadcastReceiver receiver) {}
private native void nativeFinish(long native_window);
public static native void nativeStartActivity(Activity activity);
public static native void nativeOpenURI(String uri);
public static native void nativeShare(String text);
} }

View File

@@ -393,12 +393,17 @@ public class Context extends Object {
public void startActivity(Intent intent) { public void startActivity(Intent intent) {
Slog.i(TAG, "startActivity(" + intent + ") called"); Slog.i(TAG, "startActivity(" + intent + ") called");
if ("android.intent.action.CHOOSER".equals(intent.getAction())) { if (intent.getAction() != null && intent.getAction().equals("android.intent.action.CHOOSER")) {
intent = (Intent) intent.getExtras().get("android.intent.extra.INTENT"); intent = (Intent) intent.getExtras().get("android.intent.extra.INTENT");
} }
if (intent.getComponent() == null) { if (intent.getComponent() == null) {
if(intent.getAction() != null && intent.getAction().equals("android.intent.action.SEND")) {
Slog.i(TAG, "starting extern activity with intent: " + intent);
Activity.nativeShare((String) intent.getExtras().get("android.intent.extra.TEXT"));
} else if (intent.getData() != null) {
Slog.i(TAG, "starting extern activity with intent: " + intent); Slog.i(TAG, "starting extern activity with intent: " + intent);
Activity.nativeOpenURI(String.valueOf(intent.getData())); Activity.nativeOpenURI(String.valueOf(intent.getData()));
}
return; return;
} }
try { try {

View File

@@ -10,5 +10,4 @@ public class ContextWrapper extends Context {
public Context getBaseContext() { public Context getBaseContext() {
return baseContext; return baseContext;
} }
} }

View File

@@ -11,6 +11,7 @@ public class Intent {
private String action; private String action;
private Uri data; private Uri data;
private int flags; private int flags;
private String type;
public Intent() {} public Intent() {}
public Intent(Intent o) { public Intent(Intent o) {
@@ -43,10 +44,20 @@ public class Intent {
this.flags = flags; this.flags = flags;
return this; return this;
} }
public int getFlags() {
return flags;
}
public Intent setPackage(String packageName) { public Intent setPackage(String packageName) {
return this; //?? return this; //??
} }
public Intent setType(String type) {
this.type = type;
return this;
}
public Intent putExtra(String name, Parcelable value) { public Intent putExtra(String name, Parcelable value) {
extras.putParcelable(name, value); extras.putParcelable(name, value);
return this; return this;
@@ -255,15 +266,10 @@ public class Intent {
@Override @Override
public String toString() { public String toString() {
return "Intent [component=" + component + ", extras=" + extras + ", action=" + action + ", uri=" + data + "]"; return "Intent [component=" + component + ", extras=" + extras + ", action=" + action + ", type=" + type + ", uri=" + data + "]";
} }
public static Intent createChooser(Intent target, CharSequence title) { public static Intent createChooser(Intent target, CharSequence title) {
return target; return target;
} }
public int getFlags() {
return flags;
}
} }