Skip to content

Commit a9ed99a

Browse files
hnvnslightfoot
authored andcommitted
[Android] separate openUrl and reloadUrl to keep WebView settings (#187)
* separate openUrl and reloadUrl to keep WebView settings * enable mixed content support in Android WebView * enable mixed content support in Android WebView * increase version number * always allow mixed content * reconfig Mixed Content Mode as Compatibility Mode * fix bug UIViewController nil, support multiple windows in Android * add global clean cookie function
1 parent 227ca69 commit a9ed99a

File tree

6 files changed

+1456
-1105
lines changed

6 files changed

+1456
-1105
lines changed

android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import android.graphics.Point;
88
import android.view.Display;
99
import android.widget.FrameLayout;
10+
import android.webkit.CookieManager;
11+
import android.webkit.ValueCallback;
12+
import android.os.Build;
1013

1114
import java.util.Map;
1215

@@ -70,7 +73,10 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
7073
break;
7174
case "stopLoading":
7275
stopLoading(call, result);
73-
break;
76+
break;
77+
case "cleanCookie":
78+
cleanCookie(call, result);
79+
break;
7480
default:
7581
result.notImplemented();
7682
break;
@@ -86,6 +92,8 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
8692
boolean clearCookies = call.argument("clearCookies");
8793
boolean withZoom = call.argument("withZoom");
8894
boolean withLocalStorage = call.argument("withLocalStorage");
95+
boolean supportMultipleWindows = call.argument("supportMultipleWindows");
96+
boolean appCacheEnabled = call.argument("appCacheEnabled");
8997
Map<String, String> headers = call.argument("headers");
9098
boolean scrollBar = call.argument("scrollBar");
9199

@@ -106,7 +114,9 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
106114
headers,
107115
withZoom,
108116
withLocalStorage,
109-
scrollBar
117+
scrollBar,
118+
supportMultipleWindows,
119+
appCacheEnabled
110120
);
111121
result.success(null);
112122
}
@@ -132,7 +142,7 @@ private FrameLayout.LayoutParams buildLayoutParams(MethodCall call) {
132142
}
133143

134144
private void stopLoading(MethodCall call, MethodChannel.Result result) {
135-
if (webViewManager != null){
145+
if (webViewManager != null) {
136146
webViewManager.stopLoading(call, result);
137147
}
138148
}
@@ -144,47 +154,40 @@ private void close(MethodCall call, MethodChannel.Result result) {
144154
}
145155
}
146156

147-
/**
148-
* Navigates back on the Webview.
149-
*/
157+
/**
158+
* Navigates back on the Webview.
159+
*/
150160
private void back(MethodCall call, MethodChannel.Result result) {
151161
if (webViewManager != null) {
152162
webViewManager.back(call, result);
153163
}
154164
}
155-
/**
156-
* Navigates forward on the Webview.
157-
*/
165+
166+
/**
167+
* Navigates forward on the Webview.
168+
*/
158169
private void forward(MethodCall call, MethodChannel.Result result) {
159170
if (webViewManager != null) {
160171
webViewManager.forward(call, result);
161172
}
162173
}
163174

164-
/**
165-
* Reloads the Webview.
166-
*/
175+
/**
176+
* Reloads the Webview.
177+
*/
167178
private void reload(MethodCall call, MethodChannel.Result result) {
168179
if (webViewManager != null) {
169180
webViewManager.reload(call, result);
170181
}
171182
}
183+
172184
private void reloadUrl(MethodCall call, MethodChannel.Result result) {
173185
if (webViewManager != null) {
174186
String url = call.argument("url");
175-
webViewManager.openUrl(false,
176-
false,
177-
false,
178-
false,
179-
"",
180-
url,
181-
null,
182-
false,
183-
false,
184-
false
185-
);
187+
webViewManager.reloadUrl(url);
186188
}
187189
}
190+
188191
private void eval(MethodCall call, final MethodChannel.Result result) {
189192
if (webViewManager != null) {
190193
webViewManager.eval(call, result);
@@ -198,25 +201,41 @@ private void resize(MethodCall call, final MethodChannel.Result result) {
198201
}
199202
result.success(null);
200203
}
204+
201205
private void hide(MethodCall call, final MethodChannel.Result result) {
202206
if (webViewManager != null) {
203207
webViewManager.hide(call, result);
204208
}
205209
}
210+
206211
private void show(MethodCall call, final MethodChannel.Result result) {
207212
if (webViewManager != null) {
208213
webViewManager.show(call, result);
209214
}
210215
}
211216

217+
private void cleanCookie(MethodCall call, final MethodChannel.Result result) {
218+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
219+
CookieManager.getInstance().removeAllCookies(new ValueCallback<Boolean>() {
220+
@Override
221+
public void onReceiveValue(Boolean aBoolean) {
222+
223+
}
224+
});
225+
} else {
226+
CookieManager.getInstance().removeAllCookie();
227+
}
228+
result.success(null);
229+
}
230+
212231
private int dp2px(Context context, float dp) {
213232
final float scale = context.getResources().getDisplayMetrics().density;
214233
return (int) (dp * scale + 0.5f);
215234
}
216235

217236
@Override
218237
public boolean onActivityResult(int i, int i1, Intent intent) {
219-
if(webViewManager != null && webViewManager.resultHandler != null){
238+
if (webViewManager != null && webViewManager.resultHandler != null) {
220239
return webViewManager.resultHandler.handleResult(i, i1, intent);
221240
}
222241
return false;

android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.webkit.CookieManager;
1313
import android.webkit.ValueCallback;
1414
import android.webkit.WebChromeClient;
15+
import android.webkit.WebSettings;
1516
import android.webkit.WebView;
1617
import android.webkit.WebViewClient;
1718
import android.widget.FrameLayout;
@@ -193,11 +194,19 @@ private void clearCache() {
193194
webView.clearFormData();
194195
}
195196

196-
void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, Map<String, String> headers, boolean withZoom, boolean withLocalStorage, boolean scrollBar) {
197+
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) {
197198
webView.getSettings().setJavaScriptEnabled(withJavascript);
198199
webView.getSettings().setBuiltInZoomControls(withZoom);
199200
webView.getSettings().setSupportZoom(withZoom);
200201
webView.getSettings().setDomStorageEnabled(withLocalStorage);
202+
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(supportMultipleWindows);
203+
webView.getSettings().setSupportMultipleWindows(supportMultipleWindows);
204+
webView.getSettings().setAppCacheEnabled(appCacheEnabled);
205+
206+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
207+
Log.d("WebviewManager", "Mixed Content enabled");
208+
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
209+
}
201210

202211
if (clearCache) {
203212
clearCache();
@@ -226,6 +235,10 @@ void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean
226235
}
227236
}
228237

238+
void reloadUrl(String url) {
239+
webView.loadUrl(url);
240+
}
241+
229242
void close(MethodCall call, MethodChannel.Result result) {
230243
if (webView != null) {
231244
ViewGroup vg = (ViewGroup) (webView.getParent());

0 commit comments

Comments
 (0)