diff --git a/analysis_options.yaml b/analysis_options.yaml index 18e07b8f..451e00ff 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -12,7 +12,7 @@ linter: # - always_specify_types - annotate_overrides # - avoid_annotating_with_dynamic # conflicts with always_specify_types - - avoid_as + # - avoid_as # - avoid_bool_literals_in_conditional_expressions # not yet tested # - avoid_catches_without_on_clauses # we do this commonly # - avoid_catching_errors # we do this commonly @@ -71,7 +71,7 @@ linter: # - parameter_assignments # we do this commonly - prefer_adjacent_string_concatenation - prefer_asserts_in_initializer_lists - - prefer_bool_in_asserts + # - prefer_bool_in_asserts - prefer_collection_literals - prefer_conditional_assignment - prefer_const_constructors @@ -97,7 +97,7 @@ linter: - slash_for_doc_comments - sort_constructors_first - sort_unnamed_constructors_first - - super_goes_last + # - super_goes_last - test_types_in_equals - throw_in_finally # - type_annotate_public_apis # subset of always_specify_types diff --git a/android/build.gradle b/android/build.gradle index aa2719b2..404d7cff 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -34,13 +34,15 @@ allprojects { apply plugin: 'com.android.library' android { - compileSdkVersion 28 + // compileSdkVersion 28 + compileSdkVersion 30 defaultConfig { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" // NOTE(jeffmikels): When targetSdkVersion or minSdkVersion is not set or < 4, gradle adds // additional scary permissions such as WRITE_EXTERNAL_STORAGE and READ_PHONE_STATE. minSdkVersion 16 + targetSdkVersion 30 } lintOptions { disable 'InvalidPackage' diff --git a/android/gradle.properties b/android/gradle.properties index 7be3d8b4..a5965ab8 100644 --- a/android/gradle.properties +++ b/android/gradle.properties @@ -1,2 +1,4 @@ org.gradle.jvmargs=-Xmx1536M android.enableR8=true +android.useAndroidX=true +android.enableJetifier=true \ No newline at end of file diff --git a/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java b/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java index 71e90444..7208e68a 100644 --- a/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java +++ b/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java @@ -20,11 +20,36 @@ import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.PluginRegistry; +import io.flutter.embedding.engine.plugins.FlutterPlugin; +import io.flutter.embedding.engine.plugins.activity.ActivityAware; +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; +import io.flutter.plugin.common.BinaryMessenger; /** * FlutterWebviewPlugin */ -public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.ActivityResultListener { +public class FlutterWebviewPlugin implements MethodCallHandler, FlutterPlugin, ActivityAware, PluginRegistry.ActivityResultListener { + // private Activity activity; + // private WebviewManager webViewManager; + // private Context context; + // static MethodChannel channel; + // private static final String CHANNEL_NAME = "flutter_webview_plugin"; + // private static final String JS_CHANNEL_NAMES_FIELD = "javascriptChannelNames"; + + // public static void registerWith(PluginRegistry.Registrar registrar) { + // if (registrar.activity() != null) { + // channel = new MethodChannel(registrar.messenger(), CHANNEL_NAME); + // final FlutterWebviewPlugin instance = new FlutterWebviewPlugin(registrar.activity(), registrar.activeContext()); + // registrar.addActivityResultListener(instance); + // channel.setMethodCallHandler(instance); + // } + // } + + // FlutterWebviewPlugin(Activity activity, Context context) { + // this.activity = activity; + // this.context = context; + // } + private Activity activity; private WebviewManager webViewManager; private Context context; @@ -32,18 +57,49 @@ public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.A private static final String CHANNEL_NAME = "flutter_webview_plugin"; private static final String JS_CHANNEL_NAMES_FIELD = "javascriptChannelNames"; - public static void registerWith(PluginRegistry.Registrar registrar) { - if (registrar.activity() != null) { - channel = new MethodChannel(registrar.messenger(), CHANNEL_NAME); - final FlutterWebviewPlugin instance = new FlutterWebviewPlugin(registrar.activity(), registrar.activeContext()); - registrar.addActivityResultListener(instance); - channel.setMethodCallHandler(instance); + @Override + public void onAttachedToEngine(FlutterPluginBinding binding) { + BinaryMessenger messenger = binding.getBinaryMessenger(); + channel = new MethodChannel(messenger, CHANNEL_NAME); + channel.setMethodCallHandler(this); + } + + @Override + public void onDetachedFromEngine(FlutterPluginBinding binding) { + if (channel != null) { + channel.setMethodCallHandler(null); + channel = null; } } - FlutterWebviewPlugin(Activity activity, Context context) { - this.activity = activity; - this.context = context; + @Override + public void onAttachedToActivity(ActivityPluginBinding binding) { + activity = binding.getActivity(); + context = activity.getApplicationContext(); + binding.addActivityResultListener(this); + } + + @Override + public void onDetachedFromActivityForConfigChanges() { + if (activity != null) { + activity = null; + context = null; + } + } + + @Override + public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) { + activity = binding.getActivity(); + context = activity.getApplicationContext(); + binding.addActivityResultListener(this); + } + + @Override + public void onDetachedFromActivity() { + if (activity != null) { + activity = null; + context = null; + } } @Override diff --git a/android/src/main/java/com/flutter_webview_plugin/WebviewManager.java b/android/src/main/java/com/flutter_webview_plugin/WebviewManager.java index e19b2a08..c5af5c9b 100644 --- a/android/src/main/java/com/flutter_webview_plugin/WebviewManager.java +++ b/android/src/main/java/com/flutter_webview_plugin/WebviewManager.java @@ -68,9 +68,9 @@ public boolean handleResult(int requestCode, int resultCode, Intent intent) { Uri[] results = null; if (resultCode == Activity.RESULT_OK) { if (fileUri != null && getFileSize(fileUri) > 0) { - results = new Uri[]{fileUri}; + results = new Uri[] { fileUri }; } else if (videoUri != null && getFileSize(videoUri) > 0) { - results = new Uri[]{videoUri}; + results = new Uri[] { videoUri }; } else if (intent != null) { results = getSelectedFiles(intent); } @@ -103,7 +103,7 @@ private Uri[] getSelectedFiles(Intent data) { if (data.getData() != null) { String dataString = data.getDataString(); if (dataString != null) { - return new Uri[]{Uri.parse(dataString)}; + return new Uri[] { Uri.parse(dataString) }; } } // we have multiple files selected @@ -136,9 +136,9 @@ private Uri[] getSelectedFiles(Intent data) { webViewClient = new BrowserClient() { @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { - if (ignoreSSLErrors){ + if (ignoreSSLErrors) { handler.proceed(); - }else { + } else { super.onReceivedSslError(view, handler, error); } } @@ -175,8 +175,8 @@ public void onScroll(int x, int y, int oldx, int oldy) { webView.setWebViewClient(webViewClient); webView.setWebChromeClient(new WebChromeClient() { - //The undocumented magic method override - //Eclipse will swear at you if you try to put @Override here + // 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 uploadMsg) { @@ -199,7 +199,7 @@ public void openFileChooser(ValueCallback uploadMsg, String acceptType) { FILECHOOSER_RESULTCODE); } - //For Android 4.1 + // For Android 4.1 public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); @@ -209,7 +209,7 @@ public void openFileChooser(ValueCallback uploadMsg, String acceptType, Str } - //For Android 5.0+ + // For Android 5.0+ public boolean onShowFileChooser( WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) { @@ -253,7 +253,6 @@ public boolean onShowFileChooser( return true; } - @Override public void onProgressChanged(WebView view, int progress) { Map args = new HashMap<>(); @@ -355,7 +354,8 @@ private void clearCache() { private void registerJavaScriptChannelNames(List channelNames) { for (String channelName : channelNames) { webView.addJavascriptInterface( - new JavaScriptChannel(FlutterWebviewPlugin.channel, channelName, platformThreadHandler), channelName); + new JavaScriptChannel(FlutterWebviewPlugin.channel, channelName, platformThreadHandler), + channelName); } } @@ -380,8 +380,7 @@ void openUrl( String invalidUrlRegex, boolean geolocationEnabled, boolean debuggingEnabled, - boolean ignoreSSLErrors - ) { + boolean ignoreSSLErrors) { webView.getSettings().setJavaScriptEnabled(withJavascript); webView.getSettings().setBuiltInZoomControls(withZoom); webView.getSettings().setSupportZoom(withZoom); @@ -392,7 +391,7 @@ void openUrl( webView.getSettings().setSupportMultipleWindows(supportMultipleWindows); - webView.getSettings().setAppCacheEnabled(appCacheEnabled); + webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); webView.getSettings().setAllowFileAccessFromFileURLs(allowFileURLs); webView.getSettings().setAllowUniversalAccessFromFileURLs(allowFileURLs); @@ -407,7 +406,7 @@ void openUrl( if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { webView.setWebContentsDebuggingEnabled(debuggingEnabled); } - //ignore SSL errors + // ignore SSL errors this.ignoreSSLErrors = ignoreSSLErrors; webViewClient.updateInvalidUrlRegex(invalidUrlRegex); @@ -533,7 +532,7 @@ boolean canGoForward() { /** * Clears cache */ - void cleanCache(){ + void cleanCache() { webView.clearCache(true); } diff --git a/example/android/app/src/main/java/com/yourcompany/flutter_webview_plugin_example/MainActivity.java b/example/android/app/src/main/java/com/yourcompany/flutter_webview_plugin_example/MainActivity.java index 5d63d22f..144b2285 100644 --- a/example/android/app/src/main/java/com/yourcompany/flutter_webview_plugin_example/MainActivity.java +++ b/example/android/app/src/main/java/com/yourcompany/flutter_webview_plugin_example/MainActivity.java @@ -1,14 +1,21 @@ -package com.yourcompany.flutter_webview_plugin_example; +// package com.yourcompany.flutter_webview_plugin_example; + +// import android.os.Bundle; + +// import io.flutter.app.FlutterActivity; +// import io.flutter.plugins.GeneratedPluginRegistrant; -import android.os.Bundle; +// public class MainActivity extends FlutterActivity { +// @Override +// protected void onCreate(Bundle savedInstanceState) { +// super.onCreate(savedInstanceState); +// GeneratedPluginRegistrant.registerWith(this); +// } +// } + +package com.yourcompany.flutter_webview_plugin_example; -import io.flutter.app.FlutterActivity; -import io.flutter.plugins.GeneratedPluginRegistrant; +import io.flutter.embedding.android.FlutterActivity; public class MainActivity extends FlutterActivity { - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - GeneratedPluginRegistrant.registerWith(this); - } } \ No newline at end of file diff --git a/example/ios/Runner/GeneratedPluginRegistrant.h b/example/ios/Runner/GeneratedPluginRegistrant.h index ed9a5c61..7a890927 100644 --- a/example/ios/Runner/GeneratedPluginRegistrant.h +++ b/example/ios/Runner/GeneratedPluginRegistrant.h @@ -2,6 +2,8 @@ // Generated file. Do not edit. // +// clang-format off + #ifndef GeneratedPluginRegistrant_h #define GeneratedPluginRegistrant_h diff --git a/example/ios/Runner/GeneratedPluginRegistrant.m b/example/ios/Runner/GeneratedPluginRegistrant.m index 0a6922d3..2844e6de 100644 --- a/example/ios/Runner/GeneratedPluginRegistrant.m +++ b/example/ios/Runner/GeneratedPluginRegistrant.m @@ -2,6 +2,8 @@ // Generated file. Do not edit. // +// clang-format off + #import "GeneratedPluginRegistrant.h" #if __has_include()