Skip to content

Commit

Permalink
Pull request #21: merge Android SDK webViewUrl fix
Browse files Browse the repository at this point in the history
Merge in MML/infobip-mobile-messaging-huawei from okoroleva_MM-4839-webViewUrl_fix to master

Squashed commit of the following:

commit 4724a471b5ad23bc1a7596ebc28b2e2cd180058f
Author: Olga Koroleva <[email protected]>
Date:   Mon Nov 8 13:55:44 2021 +0300

    Android SDK version changed

commit 91dafe567e218525b3f5df093461ed035f07c3da
Author: Olga Koroleva <[email protected]>
Date:   Mon Nov 8 13:46:56 2021 +0300

    merge Android SDK webViewUrl fix
  • Loading branch information
riskpp committed Nov 8, 2021
1 parent 2fe028e commit 8f308f4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ext {
mm_buildToolsVersion = "28.0.3"
mm_supportLibrariesVersion = "28.0.0"
mm_apiVersion = "3.0.4"
mm_androidSdkVersion = "5.3.3"
mm_androidSdkVersion = "5.3.4"
}

allprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void startCallbackActivity(@NonNull Intent callbackIntent) {
* @param webViewIntent Intent with extras/actions etc. to forward to the web view activity
*/
public void startWebViewActivity(@NonNull Intent webViewIntent, @NonNull String url) {
if (WebViewActivity.canOpenURLWithOtherApp(url, context)) return;
webViewIntent.putExtra(WebViewActivity.EXTRA_URL, url);
webViewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
webViewIntent.setClass(context, WebViewActivity.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package org.infobip.mobile.messaging.view;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.graphics.drawable.DrawableCompat;
Expand All @@ -17,6 +24,7 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
Expand All @@ -31,6 +39,7 @@
import org.infobip.mobile.messaging.util.StringUtils;

import java.net.URISyntaxException;
import java.util.List;


public class WebViewActivity extends AppCompatActivity {
Expand Down Expand Up @@ -72,6 +81,13 @@ public void onCreate(Bundle savedInstanceState) {
settings.setJavaScriptEnabled(true);

webView.setWebViewClient(new WebViewClient() {

@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return WebViewActivity.this.shouldOverrideUrlLoading(request.getUrl().toString());
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String innerUrl) {
return WebViewActivity.this.shouldOverrideUrlLoading(innerUrl);
Expand Down Expand Up @@ -135,16 +151,47 @@ private boolean shouldOverrideUrlLoading(String innerUrl) {
}
}

// try to open any scheme/activity that can be resolved
Uri parsedUri = Uri.parse(innerUrl);
if (parsedUri != null) {
Intent parsedUriIntent = new Intent(Intent.ACTION_VIEW, parsedUri);
if (parsedUriIntent.resolveActivity(packageManager) != null) {
startActivity(parsedUriIntent);
return canOpenURLWithOtherApp(innerUrl, this);
}

/**
* Opens URL in its default application unless it needs to be opened in a browser. Links which should be opened by default in browser app are trying to be opened directly in this web view. Returns `true` if URL is opened in another application, `false` if in this web view.
*/
public static boolean canOpenURLWithOtherApp(String url, Context context) {
Uri parsedUri = Uri.parse(url);
if (parsedUri == null) return false;

Intent parsedUriIntent = new Intent(Intent.ACTION_VIEW, parsedUri);
int flags = Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED |
Intent.FLAG_ACTIVITY_CLEAR_TOP;
if (Build.VERSION.SDK_INT > 29) {
flags = flags | (int) 1024; // FLAG_ACTIVITY_REQUIRE_NON_BROWSER
parsedUriIntent.addFlags(flags);
try {
context.startActivity(parsedUriIntent);
return true;
} catch (ActivityNotFoundException e) {
MobileMessagingLogger.d("Browser is the default app for this intent, URL will be opened in webView");
return false;
}
} else {
final List<ResolveInfo> otherApps = context.getPackageManager().queryIntentActivities(parsedUriIntent, 0);
for (ResolveInfo otherApp : otherApps) {
ActivityInfo otherAppActivity = otherApp.activityInfo;
ComponentName componentName = new ComponentName(
otherAppActivity.applicationInfo.packageName,
otherAppActivity.name
);
if (otherApp.activityInfo.applicationInfo.packageName.equals("com.android.chrome")) {
return false;
}
parsedUriIntent.addFlags(flags);
parsedUriIntent.setComponent(componentName);
context.startActivity(parsedUriIntent);
return true;
}
}

return false;
}

Expand Down

0 comments on commit 8f308f4

Please sign in to comment.