Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle intent:// scheme links with browser_fallback_url param #1167

Merged
merged 5 commits into from
Mar 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions framework/src/org/apache/cordova/CordovaWebViewImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Licensed to the Apache Software Foundation (ASF) under one
package org.apache.cordova;

import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
Expand All @@ -34,6 +35,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.json.JSONObject;

import java.lang.reflect.Constructor;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -232,25 +234,37 @@ public void showWebPage(String url, boolean openExternal, boolean clearHistory,
LOG.w(TAG, "showWebPage: Refusing to send intent for URL since it is not in the <allow-intent> whitelist. URL=" + url);
return;
}

Intent intent = null;
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
// To send an intent without CATEGORY_BROWSER, a custom plugin should be used.
intent.addCategory(Intent.CATEGORY_BROWSABLE);
Uri uri = Uri.parse(url);
// Omitting the MIME type for file: URLs causes "No Activity found to handle Intent".
// Adding the MIME type to http: URLs causes them to not be handled by the downloader.
if ("file".equals(uri.getScheme())) {
intent.setDataAndType(uri, resourceApi.getMimeType(uri));
if (url.startsWith("intent://")) {
intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
} else {
intent.setData(uri);
intent = new Intent(Intent.ACTION_VIEW);
// To send an intent without CATEGORY_BROWSER, a custom plugin should be used.
intent.addCategory(Intent.CATEGORY_BROWSABLE);
Uri uri = Uri.parse(url);
// Omitting the MIME type for file: URLs causes "No Activity found to handle Intent".
// Adding the MIME type to http: URLs causes them to not be handled by the downloader.
if ("file".equals(uri.getScheme())) {
intent.setDataAndType(uri, resourceApi.getMimeType(uri));
} else {
intent.setData(uri);
}
}
if (cordova.getActivity() != null) {
cordova.getActivity().startActivity(intent);
} else {
LOG.d(TAG, "Cordova activity does not exist.");
}
} catch (android.content.ActivityNotFoundException e) {
LOG.e(TAG, "Error loading url " + url, e);
} catch (URISyntaxException e) {
LOG.e(TAG, "Error parsing url " + url, e);
} catch (ActivityNotFoundException e) {
if (url.startsWith("intent://") && intent != null && intent.getStringExtra("browser_fallback_url") != null) {
showWebPage(intent.getStringExtra("browser_fallback_url"), openExternal, clearHistory, params);
} else {
LOG.e(TAG, "Error loading url " + url, e);
}
}
}

Expand Down