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

Migrate to Android V2 embedding #962

Open
wants to merge 2 commits 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
6 changes: 3 additions & 3 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,86 @@
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;
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);
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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<Uri> uploadMsg) {

Expand All @@ -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<Uri> uploadMsg, String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
Expand All @@ -209,7 +209,7 @@ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, Str

}

//For Android 5.0+
// For Android 5.0+
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
FileChooserParams fileChooserParams) {
Expand Down Expand Up @@ -253,7 +253,6 @@ public boolean onShowFileChooser(
return true;
}


@Override
public void onProgressChanged(WebView view, int progress) {
Map<String, Object> args = new HashMap<>();
Expand Down Expand Up @@ -355,7 +354,8 @@ private void clearCache() {
private void registerJavaScriptChannelNames(List<String> channelNames) {
for (String channelName : channelNames) {
webView.addJavascriptInterface(
new JavaScriptChannel(FlutterWebviewPlugin.channel, channelName, platformThreadHandler), channelName);
new JavaScriptChannel(FlutterWebviewPlugin.channel, channelName, platformThreadHandler),
channelName);
}
}

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -533,7 +532,7 @@ boolean canGoForward() {
/**
* Clears cache
*/
void cleanCache(){
void cleanCache() {
webView.clearCache(true);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions example/ios/Runner/GeneratedPluginRegistrant.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#ifndef GeneratedPluginRegistrant_h
#define GeneratedPluginRegistrant_h

Expand Down
2 changes: 2 additions & 0 deletions example/ios/Runner/GeneratedPluginRegistrant.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Generated file. Do not edit.
//

// clang-format off

#import "GeneratedPluginRegistrant.h"

#if __has_include(<flutter_webview_plugin/FlutterWebviewPlugin.h>)
Expand Down