Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
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
5 changes: 5 additions & 0 deletions packages/url_launcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/url_launcher/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.android.tools.build:gradle:3.4.2'
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/url_launcher/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.flutter.plugins.urllauncher">
<application>
<activity android:name=".UrlLauncherPlugin$WebViewActivity"
<activity android:name=".WebViewActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:exported="false"/>
</application>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
}
}

Expand All @@ -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<String, String> 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);
}
Expand All @@ -111,89 +107,4 @@ private Bundle extractBundle(Map<String, String> 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<String, String> 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<String, String> extractHeaders(Bundle headersBundle) {
final Map<String, String> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Some Javadoc explaining what this constant is used for would be useful. https://google.github.io/styleguide/javaguide.html#s7.3-javadoc-where-required

I didn't realize spaces were supported here. Neat.


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<String, String> 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<String, String> extractHeaders(Bundle headersBundle) {
final Map<String, String> 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);
}
}
2 changes: 1 addition & 1 deletion packages/url_launcher/example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.android.tools.build:gradle:3.4.2'
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion packages/url_launcher/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher
version: 5.1.1
version: 5.1.2

flutter:
plugin:
Expand Down