Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit e919c02

Browse files
nekolabMichael Klimushyn
authored andcommitted
[url_launcher]: add option to enable DOM storage in android webview (#1782)
1 parent 738f445 commit e919c02

File tree

6 files changed

+123
-64
lines changed

6 files changed

+123
-64
lines changed

packages/url_launcher/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 5.0.5
2+
3+
* Add `enableDomStorage` field to `launch` to enable DOM storage in Android WebView.
4+
15
## 5.0.4
26

37
* Update Dart code to conform to current Dart formatter.

packages/url_launcher/android/src/main/java/io/flutter/plugins/urllauncher/UrlLauncherPlugin.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ private void launch(MethodCall call, Result result, String url) {
6868
Intent launchIntent;
6969
boolean useWebView = call.argument("useWebView");
7070
boolean enableJavaScript = call.argument("enableJavaScript");
71+
boolean enableDomStorage = call.argument("enableDomStorage");
7172
Activity activity = mRegistrar.activity();
7273
if (activity == null) {
7374
result.error("NO_ACTIVITY", "Launching a URL requires a foreground activity.", null);
@@ -77,6 +78,7 @@ private void launch(MethodCall call, Result result, String url) {
7778
launchIntent = new Intent(activity, WebViewActivity.class);
7879
launchIntent.putExtra("url", url);
7980
launchIntent.putExtra("enableJavaScript", enableJavaScript);
81+
launchIntent.putExtra("enableDomStorage", enableDomStorage);
8082
} else {
8183
launchIntent = new Intent(Intent.ACTION_VIEW);
8284
launchIntent.setData(Uri.parse(url));
@@ -105,10 +107,14 @@ public void onCreate(Bundle savedInstanceState) {
105107
Intent intent = getIntent();
106108
String url = intent.getStringExtra("url");
107109
Boolean enableJavaScript = intent.getBooleanExtra("enableJavaScript", false);
110+
Boolean enableDomStorage = intent.getBooleanExtra("enableDomStorage", false);
108111
webview.loadUrl(url);
109112
if (enableJavaScript) {
110113
webview.getSettings().setJavaScriptEnabled(enableJavaScript);
111114
}
115+
if (enableDomStorage) {
116+
webview.getSettings().setDomStorageEnabled(enableDomStorage);
117+
}
112118
// Open new urls inside the webview itself.
113119
webview.setWebViewClient(
114120
new WebViewClient() {

packages/url_launcher/example/lib/main.dart

Lines changed: 84 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ class _MyHomePageState extends State<MyHomePage> {
6565
}
6666
}
6767

68+
Future<void> _launchInWebViewWithDomStorage(String url) async {
69+
if (await canLaunch(url)) {
70+
await launch(
71+
url,
72+
forceSafariVC: true,
73+
forceWebView: true,
74+
enableDomStorage: true,
75+
);
76+
} else {
77+
throw 'Could not launch $url';
78+
}
79+
}
80+
6881
Future<void> _launchUniversalLinkIos(String url) async {
6982
if (await canLaunch('https://youtube.com')) {
7083
final bool nativeAppLaunchSucceeded = await launch(
@@ -104,69 +117,77 @@ class _MyHomePageState extends State<MyHomePage> {
104117
appBar: AppBar(
105118
title: Text(widget.title),
106119
),
107-
body: Center(
108-
child: Column(
109-
mainAxisAlignment: MainAxisAlignment.center,
110-
children: <Widget>[
111-
Padding(
112-
padding: const EdgeInsets.all(16.0),
113-
child: TextField(
114-
onChanged: (String text) => _phone = text,
115-
decoration: const InputDecoration(
116-
hintText: 'Input the phone number to launch')),
117-
),
118-
RaisedButton(
119-
onPressed: () => setState(() {
120-
_launched = _makePhoneCall('tel:$_phone');
121-
}),
122-
child: const Text('Make phone call'),
123-
),
124-
const Padding(
125-
padding: EdgeInsets.all(16.0),
126-
child: Text(toLaunch),
127-
),
128-
RaisedButton(
129-
onPressed: () => setState(() {
130-
_launched = _launchInBrowser(toLaunch);
131-
}),
132-
child: const Text('Launch in browser'),
133-
),
134-
const Padding(padding: EdgeInsets.all(16.0)),
135-
RaisedButton(
136-
onPressed: () => setState(() {
137-
_launched = _launchInWebViewOrVC(toLaunch);
138-
}),
139-
child: const Text('Launch in app'),
140-
),
141-
const Padding(padding: EdgeInsets.all(16.0)),
142-
RaisedButton(
143-
onPressed: () => setState(() {
144-
_launched = _launchInWebViewWithJavaScript(toLaunch);
145-
}),
146-
child: const Text('Launch in app(JavaScript ON)'),
147-
),
148-
RaisedButton(
149-
onPressed: () => setState(() {
150-
_launched = _launchUniversalLinkIos(toLaunch);
151-
}),
152-
child: const Text(
153-
'Launch a universal link in a native app, fallback to Safari.(Youtube)'),
154-
),
155-
const Padding(padding: EdgeInsets.all(16.0)),
156-
RaisedButton(
157-
onPressed: () => setState(() {
158-
_launched = _launchInWebViewOrVC(toLaunch);
159-
Timer(const Duration(seconds: 5), () {
160-
print('Closing WebView after 5 seconds...');
161-
closeWebView();
162-
});
163-
}),
164-
child: const Text('Launch in app + close after 5 seconds'),
165-
),
166-
const Padding(padding: EdgeInsets.all(16.0)),
167-
FutureBuilder<void>(future: _launched, builder: _launchStatus),
168-
],
169-
),
120+
body: ListView(
121+
children: <Widget>[
122+
Column(
123+
mainAxisAlignment: MainAxisAlignment.center,
124+
children: <Widget>[
125+
Padding(
126+
padding: const EdgeInsets.all(16.0),
127+
child: TextField(
128+
onChanged: (String text) => _phone = text,
129+
decoration: const InputDecoration(
130+
hintText: 'Input the phone number to launch')),
131+
),
132+
RaisedButton(
133+
onPressed: () => setState(() {
134+
_launched = _makePhoneCall('tel:$_phone');
135+
}),
136+
child: const Text('Make phone call'),
137+
),
138+
const Padding(
139+
padding: EdgeInsets.all(16.0),
140+
child: Text(toLaunch),
141+
),
142+
RaisedButton(
143+
onPressed: () => setState(() {
144+
_launched = _launchInBrowser(toLaunch);
145+
}),
146+
child: const Text('Launch in browser'),
147+
),
148+
const Padding(padding: EdgeInsets.all(16.0)),
149+
RaisedButton(
150+
onPressed: () => setState(() {
151+
_launched = _launchInWebViewOrVC(toLaunch);
152+
}),
153+
child: const Text('Launch in app'),
154+
),
155+
RaisedButton(
156+
onPressed: () => setState(() {
157+
_launched = _launchInWebViewWithJavaScript(toLaunch);
158+
}),
159+
child: const Text('Launch in app(JavaScript ON)'),
160+
),
161+
RaisedButton(
162+
onPressed: () => setState(() {
163+
_launched = _launchInWebViewWithDomStorage(toLaunch);
164+
}),
165+
child: const Text('Launch in app(DOM storage ON)'),
166+
),
167+
const Padding(padding: EdgeInsets.all(16.0)),
168+
RaisedButton(
169+
onPressed: () => setState(() {
170+
_launched = _launchUniversalLinkIos(toLaunch);
171+
}),
172+
child: const Text(
173+
'Launch a universal link in a native app, fallback to Safari.(Youtube)'),
174+
),
175+
const Padding(padding: EdgeInsets.all(16.0)),
176+
RaisedButton(
177+
onPressed: () => setState(() {
178+
_launched = _launchInWebViewOrVC(toLaunch);
179+
Timer(const Duration(seconds: 5), () {
180+
print('Closing WebView after 5 seconds...');
181+
closeWebView();
182+
});
183+
}),
184+
child: const Text('Launch in app + close after 5 seconds'),
185+
),
186+
const Padding(padding: EdgeInsets.all(16.0)),
187+
FutureBuilder<void>(future: _launched, builder: _launchStatus),
188+
],
189+
),
190+
],
170191
),
171192
);
172193
}

packages/url_launcher/lib/url_launcher.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ const MethodChannel _channel = MethodChannel('plugins.flutter.io/url_launcher');
4242
/// WebViews.
4343
/// [enableJavaScript] is an Android only setting. If true, WebView enable
4444
/// javascript.
45+
/// [enableDomStorage] is an Android only setting. If true, WebView enable
46+
/// DOM storage.
4547
///
4648
/// Note that if any of the above are set to true but the URL is not a web URL,
4749
/// this will throw a [PlatformException].
@@ -57,6 +59,7 @@ Future<bool> launch(
5759
bool forceSafariVC,
5860
bool forceWebView,
5961
bool enableJavaScript,
62+
bool enableDomStorage,
6063
bool universalLinksOnly,
6164
Brightness statusBarBrightness,
6265
}) async {
@@ -86,6 +89,7 @@ Future<bool> launch(
8689
'useSafariVC': forceSafariVC ?? isWebURL,
8790
'useWebView': forceWebView ?? false,
8891
'enableJavaScript': enableJavaScript ?? false,
92+
'enableDomStorage': enableDomStorage ?? false,
8993
'universalLinksOnly': universalLinksOnly ?? false,
9094
},
9195
);

packages/url_launcher/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for launching a URL on Android and iOS. Supports
33
web, phone, SMS, and email schemes.
44
author: Flutter Team <flutter-dev@googlegroups.com>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher
6-
version: 5.0.4
6+
version: 5.0.5
77

88
flutter:
99
plugin:

packages/url_launcher/test/url_launcher_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void main() {
4040
'useSafariVC': true,
4141
'useWebView': false,
4242
'enableJavaScript': false,
43+
'enableDomStorage': false,
4344
'universalLinksOnly': false,
4445
})
4546
],
@@ -56,6 +57,7 @@ void main() {
5657
'useSafariVC': true,
5758
'useWebView': false,
5859
'enableJavaScript': false,
60+
'enableDomStorage': false,
5961
'universalLinksOnly': false,
6062
})
6163
],
@@ -73,6 +75,7 @@ void main() {
7375
'useSafariVC': false,
7476
'useWebView': false,
7577
'enableJavaScript': false,
78+
'enableDomStorage': false,
7679
'universalLinksOnly': true,
7780
})
7881
],
@@ -89,6 +92,7 @@ void main() {
8992
'useSafariVC': true,
9093
'useWebView': true,
9194
'enableJavaScript': false,
95+
'enableDomStorage': false,
9296
'universalLinksOnly': false,
9397
})
9498
],
@@ -106,6 +110,25 @@ void main() {
106110
'useSafariVC': true,
107111
'useWebView': true,
108112
'enableJavaScript': true,
113+
'enableDomStorage': false,
114+
'universalLinksOnly': false,
115+
})
116+
],
117+
);
118+
});
119+
120+
test('launch force WebView enable DOM storage', () async {
121+
await launch('http://example.com/',
122+
forceWebView: true, enableDomStorage: true);
123+
expect(
124+
log,
125+
<Matcher>[
126+
isMethodCall('launch', arguments: <String, Object>{
127+
'url': 'http://example.com/',
128+
'useSafariVC': true,
129+
'useWebView': true,
130+
'enableJavaScript': false,
131+
'enableDomStorage': true,
109132
'universalLinksOnly': false,
110133
})
111134
],
@@ -122,6 +145,7 @@ void main() {
122145
'useSafariVC': false,
123146
'useWebView': false,
124147
'enableJavaScript': false,
148+
'enableDomStorage': false,
125149
'universalLinksOnly': false,
126150
})
127151
],

0 commit comments

Comments
 (0)