Skip to content

Commit

Permalink
Merge pull request #1128 from gree/feature/intent-url
Browse files Browse the repository at this point in the history
introduced support for intent:/android-app: url.
  • Loading branch information
KojiNakamaru authored Jan 5, 2025
2 parents 3aa60f0 + 9c9d917 commit f6fde98
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import android.webkit.PermissionRequest;

import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -125,6 +126,29 @@ public class CWebViewPlugin {
private String mBasicAuthUserName;
private String mBasicAuthPassword;

// cf. https://chromium.googlesource.com/chromium/src/+/3e5a94daf32200d65dea6072dd4d1b9a2025508b/components/external_intents/android/java/src/org/chromium/components/external_intents/ExternalNavigationHandler.java#121
private static final int ALLOWED_INTENT_FLAGS
= Intent.FLAG_EXCLUDE_STOPPED_PACKAGES
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_MATCH_EXTERNAL
| Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_MULTIPLE_TASK
| Intent.FLAG_ACTIVITY_NEW_DOCUMENT
| Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS
| Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT;

// cf. https://chromium.googlesource.com/chromium/src/+/3e5a94daf32200d65dea6072dd4d1b9a2025508b/components/external_intents/android/java/src/org/chromium/components/external_intents/ExternalNavigationHandler.java#1808
private static void sanitizeQueryIntentActivitiesIntent(Intent intent) {
intent.setFlags(intent.getFlags() & ALLOWED_INTENT_FLAGS);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setComponent(null);

// Intent Selectors allow intents to bypass the intent filter and potentially send apps URIs
// they were not expecting to handle. https://crbug.com/1254422
intent.setSelector(null);
}

// cf. https://github.com/gree/unity-webview/issues/753
// cf. https://github.com/mixpanel/mixpanel-android/issues/400
// cf. https://github.com/mixpanel/mixpanel-android/commit/98bb530f9263f3bac0737971acc00dfef7ea4c35
Expand Down Expand Up @@ -462,9 +486,19 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebViewPlugin.call("CallOnStarted", url);
// Let webview handle the URL
return false;
} else if (url.startsWith("intent://") || url.startsWith("android-app://")) {
try {
Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
// cf. https://www.m3tech.blog/entry/android-webview-intent-scheme
sanitizeQueryIntentActivitiesIntent(intent);
view.getContext().startActivity(intent);
} catch (URISyntaxException ex) {
} catch (ActivityNotFoundException ex) {
}
return true;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
PackageManager pm = a.getPackageManager();
// PackageManager pm = a.getPackageManager();
// List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0);
// if (apps.size() > 0) {
// view.getContext().startActivity(intent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -168,6 +169,29 @@ public class CWebViewPlugin extends Fragment {
private String mBasicAuthUserName;
private String mBasicAuthPassword;

// cf. https://chromium.googlesource.com/chromium/src/+/3e5a94daf32200d65dea6072dd4d1b9a2025508b/components/external_intents/android/java/src/org/chromium/components/external_intents/ExternalNavigationHandler.java#121
private static final int ALLOWED_INTENT_FLAGS
= Intent.FLAG_EXCLUDE_STOPPED_PACKAGES
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_MATCH_EXTERNAL
| Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_MULTIPLE_TASK
| Intent.FLAG_ACTIVITY_NEW_DOCUMENT
| Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS
| Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT;

// cf. https://chromium.googlesource.com/chromium/src/+/3e5a94daf32200d65dea6072dd4d1b9a2025508b/components/external_intents/android/java/src/org/chromium/components/external_intents/ExternalNavigationHandler.java#1808
private static void sanitizeQueryIntentActivitiesIntent(Intent intent) {
intent.setFlags(intent.getFlags() & ALLOWED_INTENT_FLAGS);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setComponent(null);

// Intent Selectors allow intents to bypass the intent filter and potentially send apps URIs
// they were not expecting to handle. https://crbug.com/1254422
intent.setSelector(null);
}

public void SaveDataURL(final String fileName, final String dataURL) {
if (!dataURL.startsWith("data:")) {
return;
Expand Down Expand Up @@ -716,6 +740,16 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
mWebViewPlugin.call("CallOnStarted", url);
// Let webview handle the URL
return false;
} else if (url.startsWith("intent://") || url.startsWith("android-app://")) {
try {
Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
// cf. https://www.m3tech.blog/entry/android-webview-intent-scheme
sanitizeQueryIntentActivitiesIntent(intent);
view.getContext().startActivity(intent);
} catch (URISyntaxException ex) {
} catch (ActivityNotFoundException ex) {
}
return true;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
// PackageManager pm = a.getPackageManager();
Expand Down

0 comments on commit f6fde98

Please sign in to comment.