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

Add an option to set cache mode on Android webview #728

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ Otherwise you'll still not be able to display content from pages with untrusted

You can test your ignorance if ssl certificates is working e.g. through https://self-signed.badssl.com/

### Set cache mode

Set the `cacheMode` option to override the way the cache is used on Android Webview. It could be `LOAD_CACHE_ONLY`, `LOAD_CACHE_ELSE_NETWORK`, `LOAD_NO_CACHE` or `LOAD_DEFAULT`. The default value is affected by another option `appCacheEnabled`. If `appCacheEnabled` is true, `cacheMode` is decided by what user set. If `appCacheEnabled` is false, `cacheMode` becomes `LOAD_NO_CACHE`.


### Webview Events
Expand Down Expand Up @@ -239,6 +241,7 @@ Future<Null> launch(String url, {
bool geolocationEnabled: false,
bool debuggingEnabled: false,
bool ignoreSSLErrors: false,
String cacheMode: null,
});
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ void openUrl(MethodCall call, MethodChannel.Result result) {
boolean geolocationEnabled = call.argument("geolocationEnabled");
boolean debuggingEnabled = call.argument("debuggingEnabled");
boolean ignoreSSLErrors = call.argument("ignoreSSLErrors");
String cacheMode = call.argument("cacheMode");

if (webViewManager == null || webViewManager.closed == true) {
Map<String, Object> arguments = (Map<String, Object>) call.arguments;
Expand Down Expand Up @@ -162,7 +163,8 @@ void openUrl(MethodCall call, MethodChannel.Result result) {
invalidUrlRegex,
geolocationEnabled,
debuggingEnabled,
ignoreSSLErrors
ignoreSSLErrors,
cacheMode
);
result.success(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,26 @@ private void clearCache() {
webView.clearFormData();
}

private void setCacheMode(Boolean appCacheEnabled, String cacheModeString) {
Integer cacheMode;
if (appCacheEnabled) {
switch (cacheModeString) {
case "LOAD_CACHE_ONLY":
cacheMode = WebSettings.LOAD_CACHE_ONLY;
break;
case "LOAD_CACHE_ELSE_NETWORK":
cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK;
break;
default:
cacheMode = WebSettings.LOAD_DEFAULT;
break;
}
} else {
cacheMode = WebSettings.LOAD_NO_CACHE;
}
webView.getSettings().setCacheMode(cacheMode);
}

private void registerJavaScriptChannelNames(List<String> channelNames) {
for (String channelName : channelNames) {
webView.addJavascriptInterface(
Expand Down Expand Up @@ -380,7 +400,8 @@ void openUrl(
String invalidUrlRegex,
boolean geolocationEnabled,
boolean debuggingEnabled,
boolean ignoreSSLErrors
boolean ignoreSSLErrors,
String cacheMode
) {
webView.getSettings().setJavaScriptEnabled(withJavascript);
webView.getSettings().setBuiltInZoomControls(withZoom);
Expand Down Expand Up @@ -445,6 +466,10 @@ void openUrl(
} else {
webView.loadUrl(url);
}

if (cacheMode != null) {
setCacheMode(appCacheEnabled, cacheMode);
}
}

void reloadUrl(String url) {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ class FlutterWebviewPlugin {
/// - [withOverviewMode]: enable overview mode for Android webview ( setLoadWithOverviewMode )
/// - [useWideViewPort]: use wide viewport for Android webview ( setUseWideViewPort )
/// - [ignoreSSLErrors]: use to bypass Android/iOS SSL checks e.g. for self-signed certificates
/// - [cacheMode]: use to set cache mode for Android webview
Future<Null> launch(
String url, {
Map<String, String> headers,
Expand Down Expand Up @@ -168,6 +169,7 @@ class FlutterWebviewPlugin {
bool geolocationEnabled,
bool debuggingEnabled,
bool ignoreSSLErrors,
String cacheMode,
}) async {
final args = <String, dynamic>{
'url': url,
Expand All @@ -193,6 +195,7 @@ class FlutterWebviewPlugin {
'withOverviewMode': withOverviewMode ?? false,
'debuggingEnabled': debuggingEnabled ?? false,
'ignoreSSLErrors': ignoreSSLErrors ?? false,
'cacheMode': cacheMode,
};

if (headers != null) {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/webview_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class WebviewScaffold extends StatefulWidget {
this.geolocationEnabled,
this.debuggingEnabled = false,
this.ignoreSSLErrors = false,
this.cacheMode,
}) : super(key: key);

final PreferredSizeWidget appBar;
Expand Down Expand Up @@ -74,6 +75,7 @@ class WebviewScaffold extends StatefulWidget {
final bool useWideViewPort;
final bool debuggingEnabled;
final bool ignoreSSLErrors;
final String cacheMode;

@override
_WebviewScaffoldState createState() => _WebviewScaffoldState();
Expand Down Expand Up @@ -180,6 +182,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
geolocationEnabled: widget.geolocationEnabled,
debuggingEnabled: widget.debuggingEnabled,
ignoreSSLErrors: widget.ignoreSSLErrors,
cacheMode: widget.cacheMode,
);
} else {
if (_rect != value) {
Expand Down