[
https://issues.apache.org/jira/browse/CB-10795?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16622684#comment-16622684
]
ASF GitHub Bot commented on CB-10795:
-------------------------------------
janpio closed pull request #154: [CB-10795] Exclude current app from external
intent list
URL: https://github.com/apache/cordova-plugin-inappbrowser/pull/154
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java
index 9a695db4d..92ca3c1a1 100644
--- a/src/android/InAppBrowser.java
+++ b/src/android/InAppBrowser.java
@@ -19,8 +19,12 @@ Licensed to the Apache Software Foundation (ASF) under one
package org.apache.cordova.inappbrowser;
import android.annotation.SuppressLint;
+import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.os.Parcelable;
import android.provider.Browser;
import android.content.res.Resources;
import android.graphics.Bitmap;
@@ -71,6 +75,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.HashMap;
@@ -432,7 +437,8 @@ public String openExternal(String url) {
intent.setData(uri);
}
intent.putExtra(Browser.EXTRA_APPLICATION_ID,
cordova.getActivity().getPackageName());
- this.cordova.getActivity().startActivity(intent);
+ // CB-10795: Avoid circular loops by preventing it from opening in
the current app
+ this.openExternalExcludeCurrentApp(intent);
return "";
// not catching FileUriExposedException explicitly because
buildtools<24 doesn't know about it
} catch (java.lang.RuntimeException e) {
@@ -441,6 +447,46 @@ public String openExternal(String url) {
}
}
+ /**
+ * Opens the intent, providing a chooser that excludes the current app to
avoid
+ * circular loops.
+ */
+ private void openExternalExcludeCurrentApp(Intent intent) {
+ String currentPackage = cordova.getActivity().getPackageName();
+ boolean hasCurrentPackage = false;
+
+ PackageManager pm = cordova.getActivity().getPackageManager();
+ List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
+ ArrayList<Intent> targetIntents = new ArrayList<Intent>();
+
+ for (ResolveInfo ri : activities) {
+ if (!currentPackage.equals(ri.activityInfo.packageName)) {
+ Intent targetIntent = (Intent)intent.clone();
+ targetIntent.setPackage(ri.activityInfo.packageName);
+ targetIntents.add(targetIntent);
+ }
+ else {
+ hasCurrentPackage = true;
+ }
+ }
+
+ // If the current app package isn't a target for this URL, then use
+ // the normal launch behavior
+ if (hasCurrentPackage == false || targetIntents.size() == 0) {
+ this.cordova.getActivity().startActivity(intent);
+ }
+ // If there's only one possible intent, launch it directly
+ else if (targetIntents.size() == 1) {
+ this.cordova.getActivity().startActivity(targetIntents.get(0));
+ }
+ // Otherwise, show a custom chooser without the current app listed
+ else if (targetIntents.size() > 0) {
+ Intent chooser =
Intent.createChooser(targetIntents.remove(targetIntents.size()-1), null);
+ chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetIntents.toArray(new Parcelable[] {}));
+ this.cordova.getActivity().startActivity(chooser);
+ }
+ }
+
/**
* Closes the dialog
*/
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> [InAppBrowser] Circular loop if app had an intent-filter defined for the URL
> being opened
> -----------------------------------------------------------------------------------------
>
> Key: CB-10795
> URL: https://issues.apache.org/jira/browse/CB-10795
> Project: Apache Cordova
> Issue Type: Bug
> Components: cordova-plugin-inappbrowser
> Reporter: Dan Polivy
> Priority: Minor
> Labels: Android, triaged
>
> Consider the following scenario:
> 1. App "X" has an intent-filter defined that says it can open URLs for
> "www.example.com". (e.g., using
> https://github.com/EddyVerbruggen/Custom-URL-scheme plugin)
> 2. App "X" wants to launch a specific URL on www.example.com in the system
> browser.
> 3. App "X" uses InAppBrowser plugin and calls {{window.open}} with
> {{_system}} as the target.
> Actual:
> Since the app itself has an intent-filter defined for "www.example.com",
> InAppBrowser will just re-launch the app with the intent. In some cases, if a
> default handler is not chosen, one will get a chooser, however the app will
> still be listed as the default option.
> Expected:
> The app should be able to launch the URL in the system browser to avoid a
> circular redirect.
> There is some discussion of this issue on stackoverflow:
> http://stackoverflow.com/questions/18682833/how-to-exclude-a-specific-application-from-action-view-intent
> http://stackoverflow.com/questions/29529027/open-url-in-browser-even-though-my-app-registered-an-intent-filter-for-it
> I think it might be reasonable for InAppBrowser, in {{openExternal}}, to
> explicitly remove the current app from the list.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]