Browse Source

Merge branch 'master' into master

Jonghoon Park 7 years ago
parent
commit
129f2dba67

+ 2 - 1
README.md

@@ -93,7 +93,8 @@ Future<Null> launch(String url,
          Rect rect: null,
          String userAgent: null,
          bool withZoom: false,
-         bool withLocalStorage: true});
+         bool withLocalStorage: true,
+         bool scrollBar: true});
 ```
 ```dart
 Future<String> evalJavascript(String code);

+ 1 - 0
android/build.gradle

@@ -26,6 +26,7 @@ android {
 
     defaultConfig {
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
+        minSdkVersion 16
     }
     lintOptions {
         disable 'InvalidPackage'

+ 16 - 3
android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java

@@ -3,6 +3,7 @@ package com.flutter_webview_plugin;
 
 import android.app.Activity;
 import android.content.Context;
+import android.content.Intent;
 import android.graphics.Point;
 import android.view.Display;
 import android.widget.FrameLayout;
@@ -17,7 +18,7 @@ import io.flutter.plugin.common.PluginRegistry;
 /**
  * FlutterWebviewPlugin
  */
-public class FlutterWebviewPlugin implements MethodCallHandler {
+public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.ActivityResultListener {
     private Activity activity;
     private WebviewManager webViewManager;
     static MethodChannel channel;
@@ -25,7 +26,8 @@ public class FlutterWebviewPlugin implements MethodCallHandler {
 
     public static void registerWith(PluginRegistry.Registrar registrar) {
         channel = new MethodChannel(registrar.messenger(), CHANNEL_NAME);
-        FlutterWebviewPlugin instance = new FlutterWebviewPlugin(registrar.activity());
+        final FlutterWebviewPlugin instance = new FlutterWebviewPlugin(registrar.activity());
+        registrar.addActivityResultListener(instance);
         channel.setMethodCallHandler(instance);
     }
 
@@ -82,6 +84,7 @@ public class FlutterWebviewPlugin implements MethodCallHandler {
         boolean withZoom = call.argument("withZoom");
         boolean withLocalStorage = call.argument("withLocalStorage");
         Map<String, String> headers = call.argument("headers");
+        boolean scrollBar = call.argument("scrollBar");
 
         if (webViewManager == null || webViewManager.closed == true) {
             webViewManager = new WebviewManager(activity);
@@ -99,7 +102,8 @@ public class FlutterWebviewPlugin implements MethodCallHandler {
                 url,
                 headers,
                 withZoom,
-                withLocalStorage
+                withLocalStorage,
+                scrollBar
         );
         result.success(null);
     }
@@ -167,6 +171,7 @@ public class FlutterWebviewPlugin implements MethodCallHandler {
                     url,
                     null,
                     false,
+                    false,
                     false
             );
         }
@@ -199,4 +204,12 @@ public class FlutterWebviewPlugin implements MethodCallHandler {
         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){
+            return webViewManager.resultHandler.handleResult(i, i1, intent);
+        }
+        return false;
+    }
 }

+ 113 - 2
android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

@@ -1,5 +1,7 @@
 package com.flutter_webview_plugin;
 
+import android.content.Intent;
+import android.net.Uri;
 import android.util.Log;
 import android.annotation.TargetApi;
 import android.app.Activity;
@@ -9,6 +11,7 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.webkit.CookieManager;
 import android.webkit.ValueCallback;
+import android.webkit.WebChromeClient;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
 import android.widget.FrameLayout;
@@ -18,17 +21,62 @@ import java.util.Map;
 import io.flutter.plugin.common.MethodCall;
 import io.flutter.plugin.common.MethodChannel;
 
+import static android.app.Activity.RESULT_OK;
+
 /**
  * Created by lejard_h on 20/12/2017.
  */
 
 class WebviewManager {
 
+    private ValueCallback<Uri> mUploadMessage;
+    private ValueCallback<Uri[]> mUploadMessageArray;
+    private final static int FILECHOOSER_RESULTCODE=1;
+
+    @TargetApi(7)
+    class ResultHandler {
+        public boolean handleResult(int requestCode, int resultCode, Intent intent){
+            boolean handled = false;
+            if(Build.VERSION.SDK_INT >= 21){
+                Uri[] results = null;
+                // check result
+                if(resultCode == Activity.RESULT_OK){
+                    if(requestCode == FILECHOOSER_RESULTCODE){
+                        if(mUploadMessageArray != null){
+                            String dataString = intent.getDataString();
+                            if(dataString != null){
+                                results = new Uri[]{Uri.parse(dataString)};
+                            }
+                        }
+                        handled = true;
+                    }
+                }
+                mUploadMessageArray.onReceiveValue(results);
+                mUploadMessageArray = null;
+            }else {
+                if (requestCode == FILECHOOSER_RESULTCODE) {
+                    if (null != mUploadMessage) {
+                        Uri result = intent == null || resultCode != RESULT_OK ? null
+                                : intent.getData();
+                        mUploadMessage.onReceiveValue(result);
+                        mUploadMessage = null;
+                    }
+                    handled = true;
+                }
+            }
+            return handled;
+        }
+    }
+
     boolean closed = false;
     WebView webView;
+    Activity activity;
+    ResultHandler resultHandler;
 
-    WebviewManager(Activity activity) {
+    WebviewManager(final Activity activity) {
         this.webView = new WebView(activity);
+        this.activity = activity;
+        this.resultHandler = new ResultHandler();
         WebViewClient webViewClient = new BrowserClient();
         webView.setOnKeyListener(new View.OnKeyListener() {
             @Override
@@ -50,6 +98,65 @@ class WebviewManager {
         });
 
         webView.setWebViewClient(webViewClient);
+        webView.setWebChromeClient(new WebChromeClient()
+        {
+            //The undocumented magic method override
+            //Eclipse will swear at you if you try to put @Override here
+            // For Android 3.0+
+            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
+
+                mUploadMessage = uploadMsg;
+                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
+                i.addCategory(Intent.CATEGORY_OPENABLE);
+                i.setType("image/*");
+                activity.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);
+
+            }
+
+            // For Android 3.0+
+            public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
+                mUploadMessage = uploadMsg;
+                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
+                i.addCategory(Intent.CATEGORY_OPENABLE);
+                i.setType("*/*");
+               activity.startActivityForResult(
+                        Intent.createChooser(i, "File Browser"),
+                        FILECHOOSER_RESULTCODE);
+            }
+
+            //For Android 4.1
+            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
+                mUploadMessage = uploadMsg;
+                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
+                i.addCategory(Intent.CATEGORY_OPENABLE);
+                i.setType("image/*");
+                activity.startActivityForResult( Intent.createChooser( i, "File Chooser" ), FILECHOOSER_RESULTCODE );
+
+            }
+
+            //For Android 5.0+
+            public boolean onShowFileChooser(
+                    WebView webView, ValueCallback<Uri[]> filePathCallback,
+                    FileChooserParams fileChooserParams){
+                if(mUploadMessageArray != null){
+                    mUploadMessageArray.onReceiveValue(null);
+                }
+                mUploadMessageArray = filePathCallback;
+
+                Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
+                contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
+                contentSelectionIntent.setType("*/*");
+                Intent[] intentArray;
+                intentArray = new Intent[0];
+
+                Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
+                chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
+                chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
+                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
+                activity.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
+                return true;
+            }
+        });
     }
 
     private void clearCookies() {
@@ -70,7 +177,7 @@ class WebviewManager {
         webView.clearFormData();
     }
 
-    void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, Map<String, String> headers, boolean withZoom, boolean withLocalStorage) {
+    void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, Map<String, String> headers, boolean withZoom, boolean withLocalStorage, boolean scrollBar) {
         webView.getSettings().setJavaScriptEnabled(withJavascript);
         webView.getSettings().setBuiltInZoomControls(withZoom);
         webView.getSettings().setSupportZoom(withZoom);
@@ -91,6 +198,10 @@ class WebviewManager {
         if (userAgent != null) {
             webView.getSettings().setUserAgentString(userAgent);
         }
+      
+        if(!scrollBar){
+            webView.setVerticalScrollBarEnabled(false);
+        }
 
         if (headers != null) {
             webView.loadUrl(url, headers);

+ 3 - 0
ios/Classes/FlutterWebviewPlugin.m

@@ -68,6 +68,7 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     _enableAppScheme = call.arguments[@"enableAppScheme"];
     NSString *userAgent = call.arguments[@"userAgent"];
     NSNumber *withZoom = call.arguments[@"withZoom"];
+    NSNumber *scrollBar = call.arguments[@"scrollBar"];
     
     if (clearCache != (id)[NSNull null] && [clearCache boolValue]) {
         [[NSURLCache sharedURLCache] removeAllCachedResponses];
@@ -93,6 +94,8 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     self.webview.navigationDelegate = self;
     self.webview.scrollView.delegate = self;
     self.webview.hidden = [hidden boolValue];
+    self.webview.showsHorizontalScrollIndicator = [scrollBar boolValue];
+    self.webView.showsVerticalScrollIndicator = [scrollBar boolValue];
 
     _enableZoom = [withZoom boolValue];
 

+ 15 - 4
lib/src/base.dart

@@ -77,6 +77,7 @@ class FlutterWebviewPlugin {
   ///     It is always enabled in UIWebView of iOS and  can not be disabled.
   /// - [withLocalUrl]: allow url as a local path
   ///     Allow local files on iOs > 9.0
+  /// - [scrollBar]: enable or disable scrollbar
   Future<Null> launch(String url,
       {Map<String, String> headers,
       bool withJavascript,
@@ -88,7 +89,8 @@ class FlutterWebviewPlugin {
       String userAgent,
       bool withZoom,
       bool withLocalStorage,
-      bool withLocalUrl}) async {
+      bool withLocalUrl,
+      bool scrollBar}) async {
     Map<String, dynamic> args = {
       "url": url,
       "withJavascript": withJavascript ?? true,
@@ -99,7 +101,8 @@ class FlutterWebviewPlugin {
       "userAgent": userAgent,
       "withZoom": withZoom ?? false,
       "withLocalStorage": withLocalStorage ?? true,
-      "withLocalUrl": withLocalUrl ?? false
+      "withLocalUrl": withLocalUrl ?? false,
+      "scrollBar": scrollBar ?? true
     };
 
     if (headers != null) {
@@ -153,6 +156,14 @@ class FlutterWebviewPlugin {
     await _channel.invokeMethod("reloadUrl", args);
   }
 
+  /// adds the plugin as ActivityResultListener
+  /// Only needed and used on Android
+  Future registerAcitivityResultListener() => _channel.invokeMethod("registerAcitivityResultListener");
+
+  /// removes the plugin as ActivityResultListener
+  /// Only needed and used on Android
+  Future removeAcitivityResultListener() => _channel.invokeMethod("removeAcitivityResultListener");
+
   /// Close all Streams
   void dispose() {
     _onDestroy.close();
@@ -162,9 +173,9 @@ class FlutterWebviewPlugin {
     _instance = null;
   }
 
-  Future<Map<String, dynamic>> getCookies() async {
+  Future<Map<String, String>> getCookies() async {
     final cookiesString = await evalJavascript("document.cookie");
-    final cookies = {};
+    final cookies = <String, String>{};
 
     if (cookiesString?.isNotEmpty == true) {
       cookiesString.split(";").forEach((String cookie) {

+ 7 - 2
lib/src/webview_scaffold.dart

@@ -20,6 +20,7 @@ class WebviewScaffold extends StatefulWidget {
   final bool withZoom;
   final bool withLocalStorage;
   final bool withLocalUrl;
+  final bool scrollBar;
 
   final Map<String, String> headers;
 
@@ -38,7 +39,8 @@ class WebviewScaffold extends StatefulWidget {
       this.bottomNavigationBar,
       this.withZoom,
       this.withLocalStorage,
-      this.withLocalUrl})
+      this.withLocalUrl,
+      this.scrollBar})
       : super(key: key);
 
   @override
@@ -75,7 +77,8 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
           rect: _rect,
           withZoom: widget.withZoom,
           withLocalStorage: widget.withLocalStorage,
-          withLocalUrl: widget.withLocalUrl);
+          withLocalUrl: widget.withLocalUrl,
+          scrollBar: widget.scrollBar);
     } else {
       Rect rect = _buildRect(context);
       if (_rect != rect) {
@@ -117,6 +120,8 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
       }
     }
 
+    if (height < 0.0) height = 0.0;
+
     return new Rect.fromLTWH(0.0, top, mediaQuery.size.width, height);
   }
 }