From efe2a58ba267ef31427b2e9b9f69d60a55c05ed3 Mon Sep 17 00:00:00 2001 From: Sunny Date: Sun, 30 Jun 2019 23:19:53 +0800 Subject: [PATCH] [url_launcher]: add option to enable DOM storage in android webview --- packages/url_launcher/CHANGELOG.md | 4 + .../urllauncher/UrlLauncherPlugin.java | 6 + packages/url_launcher/example/lib/main.dart | 147 ++++++++++-------- packages/url_launcher/lib/url_launcher.dart | 4 + packages/url_launcher/pubspec.yaml | 2 +- .../url_launcher/test/url_launcher_test.dart | 24 +++ 6 files changed, 123 insertions(+), 64 deletions(-) diff --git a/packages/url_launcher/CHANGELOG.md b/packages/url_launcher/CHANGELOG.md index 2a448106287f..20b64b11cd5f 100644 --- a/packages/url_launcher/CHANGELOG.md +++ b/packages/url_launcher/CHANGELOG.md @@ -1,3 +1,7 @@ +## 5.0.5 + +* Add `enableDomStorage` field to `launch` to enable DOM storage in Android WebView. + ## 5.0.4 * Update Dart code to conform to current Dart formatter. diff --git a/packages/url_launcher/android/src/main/java/io/flutter/plugins/urllauncher/UrlLauncherPlugin.java b/packages/url_launcher/android/src/main/java/io/flutter/plugins/urllauncher/UrlLauncherPlugin.java index 888d3c34fc2e..30d067664b85 100644 --- a/packages/url_launcher/android/src/main/java/io/flutter/plugins/urllauncher/UrlLauncherPlugin.java +++ b/packages/url_launcher/android/src/main/java/io/flutter/plugins/urllauncher/UrlLauncherPlugin.java @@ -68,6 +68,7 @@ private void launch(MethodCall call, Result result, String url) { Intent launchIntent; boolean useWebView = call.argument("useWebView"); boolean enableJavaScript = call.argument("enableJavaScript"); + boolean enableDomStorage = call.argument("enableDomStorage"); Activity activity = mRegistrar.activity(); if (activity == null) { 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) { launchIntent = new Intent(activity, WebViewActivity.class); launchIntent.putExtra("url", url); launchIntent.putExtra("enableJavaScript", enableJavaScript); + launchIntent.putExtra("enableDomStorage", enableDomStorage); } else { launchIntent = new Intent(Intent.ACTION_VIEW); launchIntent.setData(Uri.parse(url)); @@ -105,10 +107,14 @@ public void onCreate(Bundle savedInstanceState) { Intent intent = getIntent(); String url = intent.getStringExtra("url"); Boolean enableJavaScript = intent.getBooleanExtra("enableJavaScript", false); + Boolean enableDomStorage = intent.getBooleanExtra("enableDomStorage", false); webview.loadUrl(url); if (enableJavaScript) { webview.getSettings().setJavaScriptEnabled(enableJavaScript); } + if (enableDomStorage) { + webview.getSettings().setDomStorageEnabled(enableDomStorage); + } // Open new urls inside the webview itself. webview.setWebViewClient( new WebViewClient() { diff --git a/packages/url_launcher/example/lib/main.dart b/packages/url_launcher/example/lib/main.dart index 2a38c9d58fc3..66b5e9f538e9 100644 --- a/packages/url_launcher/example/lib/main.dart +++ b/packages/url_launcher/example/lib/main.dart @@ -65,6 +65,19 @@ class _MyHomePageState extends State { } } + Future _launchInWebViewWithDomStorage(String url) async { + if (await canLaunch(url)) { + await launch( + url, + forceSafariVC: true, + forceWebView: true, + enableDomStorage: true, + ); + } else { + throw 'Could not launch $url'; + } + } + Future _launchUniversalLinkIos(String url) async { if (await canLaunch('https://youtube.com')) { final bool nativeAppLaunchSucceeded = await launch( @@ -104,69 +117,77 @@ class _MyHomePageState extends State { appBar: AppBar( title: Text(widget.title), ), - body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Padding( - padding: const EdgeInsets.all(16.0), - child: TextField( - onChanged: (String text) => _phone = text, - decoration: const InputDecoration( - hintText: 'Input the phone number to launch')), - ), - RaisedButton( - onPressed: () => setState(() { - _launched = _makePhoneCall('tel:$_phone'); - }), - child: const Text('Make phone call'), - ), - const Padding( - padding: EdgeInsets.all(16.0), - child: Text(toLaunch), - ), - RaisedButton( - onPressed: () => setState(() { - _launched = _launchInBrowser(toLaunch); - }), - child: const Text('Launch in browser'), - ), - const Padding(padding: EdgeInsets.all(16.0)), - RaisedButton( - onPressed: () => setState(() { - _launched = _launchInWebViewOrVC(toLaunch); - }), - child: const Text('Launch in app'), - ), - const Padding(padding: EdgeInsets.all(16.0)), - RaisedButton( - onPressed: () => setState(() { - _launched = _launchInWebViewWithJavaScript(toLaunch); - }), - child: const Text('Launch in app(JavaScript ON)'), - ), - RaisedButton( - onPressed: () => setState(() { - _launched = _launchUniversalLinkIos(toLaunch); - }), - child: const Text( - 'Launch a universal link in a native app, fallback to Safari.(Youtube)'), - ), - const Padding(padding: EdgeInsets.all(16.0)), - RaisedButton( - onPressed: () => setState(() { - _launched = _launchInWebViewOrVC(toLaunch); - Timer(const Duration(seconds: 5), () { - print('Closing WebView after 5 seconds...'); - closeWebView(); - }); - }), - child: const Text('Launch in app + close after 5 seconds'), - ), - const Padding(padding: EdgeInsets.all(16.0)), - FutureBuilder(future: _launched, builder: _launchStatus), - ], - ), + body: ListView( + children: [ + Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Padding( + padding: const EdgeInsets.all(16.0), + child: TextField( + onChanged: (String text) => _phone = text, + decoration: const InputDecoration( + hintText: 'Input the phone number to launch')), + ), + RaisedButton( + onPressed: () => setState(() { + _launched = _makePhoneCall('tel:$_phone'); + }), + child: const Text('Make phone call'), + ), + const Padding( + padding: EdgeInsets.all(16.0), + child: Text(toLaunch), + ), + RaisedButton( + onPressed: () => setState(() { + _launched = _launchInBrowser(toLaunch); + }), + child: const Text('Launch in browser'), + ), + const Padding(padding: EdgeInsets.all(16.0)), + RaisedButton( + onPressed: () => setState(() { + _launched = _launchInWebViewOrVC(toLaunch); + }), + child: const Text('Launch in app'), + ), + RaisedButton( + onPressed: () => setState(() { + _launched = _launchInWebViewWithJavaScript(toLaunch); + }), + child: const Text('Launch in app(JavaScript ON)'), + ), + RaisedButton( + onPressed: () => setState(() { + _launched = _launchInWebViewWithDomStorage(toLaunch); + }), + child: const Text('Launch in app(DOM storage ON)'), + ), + const Padding(padding: EdgeInsets.all(16.0)), + RaisedButton( + onPressed: () => setState(() { + _launched = _launchUniversalLinkIos(toLaunch); + }), + child: const Text( + 'Launch a universal link in a native app, fallback to Safari.(Youtube)'), + ), + const Padding(padding: EdgeInsets.all(16.0)), + RaisedButton( + onPressed: () => setState(() { + _launched = _launchInWebViewOrVC(toLaunch); + Timer(const Duration(seconds: 5), () { + print('Closing WebView after 5 seconds...'); + closeWebView(); + }); + }), + child: const Text('Launch in app + close after 5 seconds'), + ), + const Padding(padding: EdgeInsets.all(16.0)), + FutureBuilder(future: _launched, builder: _launchStatus), + ], + ), + ], ), ); } diff --git a/packages/url_launcher/lib/url_launcher.dart b/packages/url_launcher/lib/url_launcher.dart index a942b5053e63..edae489b8dc0 100644 --- a/packages/url_launcher/lib/url_launcher.dart +++ b/packages/url_launcher/lib/url_launcher.dart @@ -42,6 +42,8 @@ const MethodChannel _channel = MethodChannel('plugins.flutter.io/url_launcher'); /// WebViews. /// [enableJavaScript] is an Android only setting. If true, WebView enable /// javascript. +/// [enableDomStorage] is an Android only setting. If true, WebView enable +/// DOM storage. /// /// Note that if any of the above are set to true but the URL is not a web URL, /// this will throw a [PlatformException]. @@ -57,6 +59,7 @@ Future launch( bool forceSafariVC, bool forceWebView, bool enableJavaScript, + bool enableDomStorage, bool universalLinksOnly, Brightness statusBarBrightness, }) async { @@ -86,6 +89,7 @@ Future launch( 'useSafariVC': forceSafariVC ?? isWebURL, 'useWebView': forceWebView ?? false, 'enableJavaScript': enableJavaScript ?? false, + 'enableDomStorage': enableDomStorage ?? false, 'universalLinksOnly': universalLinksOnly ?? false, }, ); diff --git a/packages/url_launcher/pubspec.yaml b/packages/url_launcher/pubspec.yaml index a71b26adcf3c..0c0711222338 100644 --- a/packages/url_launcher/pubspec.yaml +++ b/packages/url_launcher/pubspec.yaml @@ -3,7 +3,7 @@ description: Flutter plugin for launching a URL on Android and iOS. Supports web, phone, SMS, and email schemes. author: Flutter Team homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher -version: 5.0.4 +version: 5.0.5 flutter: plugin: diff --git a/packages/url_launcher/test/url_launcher_test.dart b/packages/url_launcher/test/url_launcher_test.dart index 0a6acc59b951..db746902f3d3 100644 --- a/packages/url_launcher/test/url_launcher_test.dart +++ b/packages/url_launcher/test/url_launcher_test.dart @@ -40,6 +40,7 @@ void main() { 'useSafariVC': true, 'useWebView': false, 'enableJavaScript': false, + 'enableDomStorage': false, 'universalLinksOnly': false, }) ], @@ -56,6 +57,7 @@ void main() { 'useSafariVC': true, 'useWebView': false, 'enableJavaScript': false, + 'enableDomStorage': false, 'universalLinksOnly': false, }) ], @@ -73,6 +75,7 @@ void main() { 'useSafariVC': false, 'useWebView': false, 'enableJavaScript': false, + 'enableDomStorage': false, 'universalLinksOnly': true, }) ], @@ -89,6 +92,7 @@ void main() { 'useSafariVC': true, 'useWebView': true, 'enableJavaScript': false, + 'enableDomStorage': false, 'universalLinksOnly': false, }) ], @@ -106,6 +110,25 @@ void main() { 'useSafariVC': true, 'useWebView': true, 'enableJavaScript': true, + 'enableDomStorage': false, + 'universalLinksOnly': false, + }) + ], + ); + }); + + test('launch force WebView enable DOM storage', () async { + await launch('http://example.com/', + forceWebView: true, enableDomStorage: true); + expect( + log, + [ + isMethodCall('launch', arguments: { + 'url': 'http://example.com/', + 'useSafariVC': true, + 'useWebView': true, + 'enableJavaScript': false, + 'enableDomStorage': true, 'universalLinksOnly': false, }) ], @@ -122,6 +145,7 @@ void main() { 'useSafariVC': false, 'useWebView': false, 'enableJavaScript': false, + 'enableDomStorage': false, 'universalLinksOnly': false, }) ],