implement passing a URI to open inside the application

Supported URI schemes will also be added to the .desktop file for --install

The URI is passed as named parameter --uri. This allows us to use unnamed parameters
for split APK apps in the future
This commit is contained in:
Julian Winkler
2024-10-05 22:12:14 +02:00
parent 1b46d728e3
commit a8ef5a533a
5 changed files with 74 additions and 6 deletions

View File

@@ -50,11 +50,13 @@ public class Activity extends ContextThemeWrapper implements Window.Callback {
* @return instance of main activity class
* @throws Exception
*/
private static Activity createMainActivity(String className, long native_window) throws Exception {
private static Activity createMainActivity(String className, long native_window, String uriString) throws Exception {
Uri uri = uriString != null ? Uri.parse(uriString) : null;
if (className == null) {
for (PackageParser.Activity activity: pkg.activities) {
for (PackageParser.IntentInfo intent: activity.intents) {
if (intent.hasCategory("android.intent.category.LAUNCHER")) {
if ((uri == null && intent.hasCategory("android.intent.category.LAUNCHER")) ||
(uri != null && intent.hasDataScheme(uri.getScheme()))) {
className = activity.className;
break;
}
@@ -65,10 +67,16 @@ public class Activity extends ContextThemeWrapper implements Window.Callback {
} else {
className = className.replace('/', '.');
}
if (className == null) {
System.err.println("Failed to find Activity to launch URI: " + uri);
System.exit(1);
}
Class<? extends Activity> cls = Class.forName(className).asSubclass(Activity.class);
Constructor<? extends Activity> constructor = cls.getConstructor();
Activity activity = constructor.newInstance();
activity.window.native_window = native_window;
if (uri != null)
activity.setIntent(new Intent("android.intent.action.VIEW", uri));
return activity;
}
@@ -467,7 +475,9 @@ public class Activity extends ContextThemeWrapper implements Window.Callback {
finish();
}
public void setIntent(Intent newIntent) {}
public void setIntent(Intent newIntent) {
this.intent = newIntent;
}
public void unregisterReceiver(BroadcastReceiver receiver) {}

View File

@@ -4,6 +4,7 @@ import android.os.Bundle;
import android.content.res.Configuration;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.PackageParser;
public class Application extends ContextWrapper {
public long native_window;
@@ -16,6 +17,21 @@ public class Application extends ContextWrapper {
return getString(pkg.applicationInfo.labelRes);
}
String get_supported_mime_types() {
String mimeTypes = "";
for (PackageParser.Activity activity: pkg.activities) {
for (PackageParser.IntentInfo intent: activity.intents) {
for (int i = 0; i < intent.countDataSchemes(); i++) {
String scheme = intent.getDataScheme(i);
// ignore http and https, as there is no way to only handle specific hosts in a .desktop file
if (!"http".equals(scheme) && !"https".equals(scheme))
mimeTypes += "x-scheme-handler/" + intent.getDataScheme(i) + ";";
}
}
}
return "".equals(mimeTypes) ? null : mimeTypes;
}
public interface ActivityLifecycleCallbacks {
void onActivityCreated(Activity activity, Bundle savedInstanceState);
void onActivityStarted(Activity activity);

View File

@@ -9,6 +9,7 @@ public class IntentFilter {
private List<String> actions = new ArrayList<>();
private Set<String> categories = new HashSet<>();
private List<String> dataSchemes = new ArrayList<>();
public IntentFilter() {}
public IntentFilter(String action) {
@@ -42,4 +43,20 @@ public class IntentFilter {
}
public void setPriority(int priority) {}
public void addDataScheme(String dataScheme) {
dataSchemes.add(dataScheme);
}
public boolean hasDataScheme(String dataScheme) {
return dataSchemes.contains(dataScheme);
}
public int countDataSchemes() {
return dataSchemes.size();
}
public String getDataScheme(int index) {
return dataSchemes.get(index);
}
}

View File

@@ -28,6 +28,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.PatternMatcher;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Base64;
import android.util.DisplayMetrics;
@@ -2247,6 +2248,10 @@ public class PackageParser {
XmlUtils.skipCurrentTag(parser);
outInfo.addCategory(value);
} else if (nodeName.equals("data")) {
String scheme = parser.getAttributeValue(
ANDROID_RESOURCES, "scheme");
if (!TextUtils.isEmpty(scheme))
outInfo.addDataScheme(scheme);
XmlUtils.skipCurrentTag(parser);
} else if (!RIGID_PARSER) {
Slog.w(TAG, "Unknown element under <intent-filter>: "