diff --git a/packages/url_launcher/CHANGELOG.md b/packages/url_launcher/CHANGELOG.md index d4d785eeccb0..db2ace15b1e9 100644 --- a/packages/url_launcher/CHANGELOG.md +++ b/packages/url_launcher/CHANGELOG.md @@ -1,3 +1,8 @@ +## 5.1.2 + +* Update AGP and gradle. +* Split plugin and WebViewActivity class files. + ## 5.1.1 * Suppress a handled deprecation warning on iOS diff --git a/packages/url_launcher/android/build.gradle b/packages/url_launcher/android/build.gradle index 641b2e29f81a..e2c6ea5af7ae 100644 --- a/packages/url_launcher/android/build.gradle +++ b/packages/url_launcher/android/build.gradle @@ -21,7 +21,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.3.0' + classpath 'com.android.tools.build:gradle:3.4.2' } } diff --git a/packages/url_launcher/android/src/main/AndroidManifest.xml b/packages/url_launcher/android/src/main/AndroidManifest.xml index 3b455eabc021..f43e5ba2474d 100644 --- a/packages/url_launcher/android/src/main/AndroidManifest.xml +++ b/packages/url_launcher/android/src/main/AndroidManifest.xml @@ -1,7 +1,7 @@ - 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 b877a45bdb25..b2b0eb906952 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 @@ -4,26 +4,17 @@ package io.flutter.plugins.urllauncher; -import android.app.Activity; -import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; import android.net.Uri; -import android.os.Build; import android.os.Bundle; import android.provider.Browser; -import android.view.KeyEvent; -import android.webkit.WebResourceRequest; -import android.webkit.WebView; -import android.webkit.WebViewClient; import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; -import java.util.HashMap; import java.util.Map; /** UrlLauncherPlugin */ @@ -43,15 +34,20 @@ private UrlLauncherPlugin(Registrar registrar) { @Override public void onMethodCall(MethodCall call, Result result) { - String url = call.argument("url"); - if (call.method.equals("canLaunch")) { - canLaunch(url, result); - } else if (call.method.equals("launch")) { - launch(call, result, url); - } else if (call.method.equals("closeWebView")) { - closeWebView(result); - } else { - result.notImplemented(); + final String url = call.argument("url"); + switch (call.method) { + case "canLaunch": + canLaunch(url, result); + break; + case "launch": + launch(call, result, url); + break; + case "closeWebView": + closeWebView(result); + break; + default: + result.notImplemented(); + break; } } @@ -74,31 +70,31 @@ private void launch(MethodCall call, Result result, String url) { final boolean enableJavaScript = call.argument("enableJavaScript"); final boolean enableDomStorage = call.argument("enableDomStorage"); final Map headersMap = call.argument("headers"); - final Activity activity = mRegistrar.activity(); + final Bundle headersBundle = extractBundle(headersMap); + final Context context = mRegistrar.activity(); - if (activity == null) { + if (context == null) { result.error("NO_ACTIVITY", "Launching a URL requires a foreground activity.", null); return; } + if (useWebView) { - launchIntent = new Intent(activity, WebViewActivity.class); - launchIntent.putExtra("url", url); - launchIntent.putExtra("enableJavaScript", enableJavaScript); - launchIntent.putExtra("enableDomStorage", enableDomStorage); + launchIntent = + WebViewActivity.createIntent( + context, url, enableJavaScript, enableDomStorage, headersBundle); } else { - launchIntent = new Intent(Intent.ACTION_VIEW); - launchIntent.setData(Uri.parse(url)); + launchIntent = + new Intent(Intent.ACTION_VIEW) + .setData(Uri.parse(url)) + .putExtra(Browser.EXTRA_HEADERS, headersBundle); } - final Bundle headersBundle = extractBundle(headersMap); - launchIntent.putExtra(Browser.EXTRA_HEADERS, headersBundle); - - activity.startActivity(launchIntent); + context.startActivity(launchIntent); result.success(true); } private void closeWebView(Result result) { - Intent intent = new Intent("close"); + Intent intent = new Intent(WebViewActivity.ACTION_CLOSE); mRegistrar.context().sendBroadcast(intent); result.success(null); } @@ -111,89 +107,4 @@ private Bundle extractBundle(Map headersMap) { } return headersBundle; } - - /* Launches WebView activity */ - public static class WebViewActivity extends Activity { - private WebView webview; - private BroadcastReceiver broadcastReceiver; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - webview = new WebView(this); - setContentView(webview); - // Get the Intent that started this activity and extract the string - final Intent intent = getIntent(); - final String url = intent.getStringExtra("url"); - final boolean enableJavaScript = intent.getBooleanExtra("enableJavaScript", false); - final boolean enableDomStorage = intent.getBooleanExtra("enableDomStorage", false); - final Bundle headersBundle = intent.getBundleExtra(Browser.EXTRA_HEADERS); - - final Map headersMap = extractHeaders(headersBundle); - webview.loadUrl(url, headersMap); - - webview.getSettings().setJavaScriptEnabled(enableJavaScript); - webview.getSettings().setDomStorageEnabled(enableDomStorage); - - // Open new urls inside the webview itself. - webview.setWebViewClient( - new WebViewClient() { - - @Override - @SuppressWarnings("deprecation") - public boolean shouldOverrideUrlLoading(WebView view, String url) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - view.loadUrl(url); - return false; - } - return super.shouldOverrideUrlLoading(view, url); - } - - @Override - public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - view.loadUrl(request.getUrl().toString()); - } - return false; - } - }); - - // Set broadcast receiver to handle calls to close the web view - broadcastReceiver = - new BroadcastReceiver() { - @Override - public void onReceive(Context arg0, Intent intent) { - String action = intent.getAction(); - if ("close".equals(action)) { - finish(); - } - } - }; - registerReceiver(broadcastReceiver, new IntentFilter("close")); - } - - private Map extractHeaders(Bundle headersBundle) { - final Map headersMap = new HashMap<>(); - for (String key : headersBundle.keySet()) { - final String value = headersBundle.getString(key); - headersMap.put(key, value); - } - return headersMap; - } - - @Override - protected void onDestroy() { - super.onDestroy(); - unregisterReceiver(broadcastReceiver); - } - - @Override - public boolean onKeyDown(int keyCode, KeyEvent event) { - if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) { - webview.goBack(); - return true; - } - return super.onKeyDown(keyCode, event); - } - } } diff --git a/packages/url_launcher/android/src/main/java/io/flutter/plugins/urllauncher/WebViewActivity.java b/packages/url_launcher/android/src/main/java/io/flutter/plugins/urllauncher/WebViewActivity.java new file mode 100644 index 000000000000..52714790a25c --- /dev/null +++ b/packages/url_launcher/android/src/main/java/io/flutter/plugins/urllauncher/WebViewActivity.java @@ -0,0 +1,129 @@ +package io.flutter.plugins.urllauncher; + +import android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.Build; +import android.os.Bundle; +import android.provider.Browser; +import android.view.KeyEvent; +import android.webkit.WebResourceRequest; +import android.webkit.WebView; +import android.webkit.WebViewClient; +import java.util.HashMap; +import java.util.Map; + +/* Launches WebView activity */ +public class WebViewActivity extends Activity { + + /* + * Use this to trigger a BroadcastReceiver inside WebViewActivity + * that will request the current instance to finish. + * */ + public static String ACTION_CLOSE = "close action"; + + private final BroadcastReceiver broadcastReceiver = + new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (ACTION_CLOSE.equals(action)) { + finish(); + } + } + }; + + private final WebViewClient webViewClient = + new WebViewClient() { + + @Override + public boolean shouldOverrideUrlLoading(WebView view, String url) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + view.loadUrl(url); + return false; + } + return super.shouldOverrideUrlLoading(view, url); + } + + @Override + public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + view.loadUrl(request.getUrl().toString()); + } + return false; + } + }; + + private WebView webview; + + private IntentFilter closeIntentFilter = new IntentFilter(ACTION_CLOSE); + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + webview = new WebView(this); + setContentView(webview); + // Get the Intent that started this activity and extract the string + final Intent intent = getIntent(); + final String url = intent.getStringExtra(URL_EXTRA); + final boolean enableJavaScript = intent.getBooleanExtra(ENABLE_JS_EXTRA, false); + final boolean enableDomStorage = intent.getBooleanExtra(ENABLE_DOM_EXTRA, false); + final Bundle headersBundle = intent.getBundleExtra(Browser.EXTRA_HEADERS); + + final Map headersMap = extractHeaders(headersBundle); + webview.loadUrl(url, headersMap); + + webview.getSettings().setJavaScriptEnabled(enableJavaScript); + webview.getSettings().setDomStorageEnabled(enableDomStorage); + + // Open new urls inside the webview itself. + webview.setWebViewClient(webViewClient); + + // Register receiver that may finish this Activity. + registerReceiver(broadcastReceiver, closeIntentFilter); + } + + private Map extractHeaders(Bundle headersBundle) { + final Map headersMap = new HashMap<>(); + for (String key : headersBundle.keySet()) { + final String value = headersBundle.getString(key); + headersMap.put(key, value); + } + return headersMap; + } + + @Override + protected void onDestroy() { + super.onDestroy(); + unregisterReceiver(broadcastReceiver); + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) { + webview.goBack(); + return true; + } + return super.onKeyDown(keyCode, event); + } + + private static String URL_EXTRA = "url"; + private static String ENABLE_JS_EXTRA = "enableJavaScript"; + private static String ENABLE_DOM_EXTRA = "enableDomStorage"; + + /* Hides the constants used to forward data to the Activity instance. */ + public static Intent createIntent( + Context context, + String url, + boolean enableJavaScript, + boolean enableDomStorage, + Bundle headersBundle) { + return new Intent(context, WebViewActivity.class) + .putExtra(URL_EXTRA, url) + .putExtra(ENABLE_JS_EXTRA, enableJavaScript) + .putExtra(ENABLE_DOM_EXTRA, enableDomStorage) + .putExtra(Browser.EXTRA_HEADERS, headersBundle); + } +} diff --git a/packages/url_launcher/example/android/build.gradle b/packages/url_launcher/example/android/build.gradle index 541636cc492a..6b1a639efd76 100644 --- a/packages/url_launcher/example/android/build.gradle +++ b/packages/url_launcher/example/android/build.gradle @@ -5,7 +5,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.3.0' + classpath 'com.android.tools.build:gradle:3.4.2' } } diff --git a/packages/url_launcher/example/android/gradle/wrapper/gradle-wrapper.properties b/packages/url_launcher/example/android/gradle/wrapper/gradle-wrapper.properties index 019065d1d650..1cedb28ea41f 100644 --- a/packages/url_launcher/example/android/gradle/wrapper/gradle-wrapper.properties +++ b/packages/url_launcher/example/android/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Wed Jul 31 20:16:04 BRT 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip diff --git a/packages/url_launcher/pubspec.yaml b/packages/url_launcher/pubspec.yaml index 71a94f9e565c..f7b77db58818 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.1.1 +version: 5.1.2 flutter: plugin: