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

[Android] separate openUrl and reloadUrl to keep WebView settings #187

Merged
merged 9 commits into from
Nov 15, 2018
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import android.graphics.Point;
import android.view.Display;
import android.widget.FrameLayout;
import android.webkit.CookieManager;
import android.webkit.ValueCallback;
import android.os.Build;

import java.util.Map;

Expand Down Expand Up @@ -70,7 +73,10 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
break;
case "stopLoading":
stopLoading(call, result);
break;
break;
case "cleanCookie":
cleanCookie(call, result);
break;
default:
result.notImplemented();
break;
Expand All @@ -86,6 +92,8 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
boolean clearCookies = call.argument("clearCookies");
boolean withZoom = call.argument("withZoom");
boolean withLocalStorage = call.argument("withLocalStorage");
boolean supportMultipleWindows = call.argument("supportMultipleWindows");
boolean appCacheEnabled = call.argument("appCacheEnabled");
Map<String, String> headers = call.argument("headers");
boolean scrollBar = call.argument("scrollBar");

Expand All @@ -106,7 +114,9 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
headers,
withZoom,
withLocalStorage,
scrollBar
scrollBar,
supportMultipleWindows,
appCacheEnabled
);
result.success(null);
}
Expand All @@ -132,7 +142,7 @@ private FrameLayout.LayoutParams buildLayoutParams(MethodCall call) {
}

private void stopLoading(MethodCall call, MethodChannel.Result result) {
if (webViewManager != null){
if (webViewManager != null) {
webViewManager.stopLoading(call, result);
}
}
Expand All @@ -144,47 +154,40 @@ private void close(MethodCall call, MethodChannel.Result result) {
}
}

/**
* Navigates back on the Webview.
*/
/**
* Navigates back on the Webview.
*/
private void back(MethodCall call, MethodChannel.Result result) {
if (webViewManager != null) {
webViewManager.back(call, result);
}
}
/**
* Navigates forward on the Webview.
*/

/**
* Navigates forward on the Webview.
*/
private void forward(MethodCall call, MethodChannel.Result result) {
if (webViewManager != null) {
webViewManager.forward(call, result);
}
}

/**
* Reloads the Webview.
*/
/**
* Reloads the Webview.
*/
private void reload(MethodCall call, MethodChannel.Result result) {
if (webViewManager != null) {
webViewManager.reload(call, result);
}
}

private void reloadUrl(MethodCall call, MethodChannel.Result result) {
if (webViewManager != null) {
String url = call.argument("url");
webViewManager.openUrl(false,
false,
false,
false,
"",
url,
null,
false,
false,
false
);
webViewManager.reloadUrl(url);
}
}

private void eval(MethodCall call, final MethodChannel.Result result) {
if (webViewManager != null) {
webViewManager.eval(call, result);
Expand All @@ -198,25 +201,41 @@ private void resize(MethodCall call, final MethodChannel.Result result) {
}
result.success(null);
}

private void hide(MethodCall call, final MethodChannel.Result result) {
if (webViewManager != null) {
webViewManager.hide(call, result);
}
}

private void show(MethodCall call, final MethodChannel.Result result) {
if (webViewManager != null) {
webViewManager.show(call, result);
}
}

private void cleanCookie(MethodCall call, final MethodChannel.Result result) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().removeAllCookies(new ValueCallback<Boolean>() {
@Override
public void onReceiveValue(Boolean aBoolean) {

}
});
} else {
CookieManager.getInstance().removeAllCookie();
}
result.success(null);
}

private int dp2px(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}

@Override
public boolean onActivityResult(int i, int i1, Intent intent) {
if(webViewManager != null && webViewManager.resultHandler != null){
if (webViewManager != null && webViewManager.resultHandler != null) {
return webViewManager.resultHandler.handleResult(i, i1, intent);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.webkit.CookieManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
Expand Down Expand Up @@ -193,11 +194,19 @@ private void clearCache() {
webView.clearFormData();
}

void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, Map<String, String> headers, boolean withZoom, boolean withLocalStorage, boolean scrollBar) {
void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, Map<String, String> headers, boolean withZoom, boolean withLocalStorage, boolean scrollBar, boolean supportMultipleWindows, boolean appCacheEnabled) {
webView.getSettings().setJavaScriptEnabled(withJavascript);
webView.getSettings().setBuiltInZoomControls(withZoom);
webView.getSettings().setSupportZoom(withZoom);
webView.getSettings().setDomStorageEnabled(withLocalStorage);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(supportMultipleWindows);
webView.getSettings().setSupportMultipleWindows(supportMultipleWindows);
webView.getSettings().setAppCacheEnabled(appCacheEnabled);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Log.d("WebviewManager", "Mixed Content enabled");
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}

if (clearCache) {
clearCache();
Expand Down Expand Up @@ -226,6 +235,10 @@ void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean
}
}

void reloadUrl(String url) {
webView.loadUrl(url);
}

void close(MethodCall call, MethodChannel.Result result) {
if (webView != null) {
ViewGroup vg = (ViewGroup) (webView.getParent());
Expand Down
Loading