Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit b120e92

Browse files
juliocbcottaMichael Klimushyn
authored andcommitted
[url_launcher] Split Activity and plugin files (#1936)
This PR : - Updates AGP and Gradle versions. - Splits WebViewActivity and the plugin class to improve separation of concerns. The separation of classes and files provides encapsulation for constants used in the WebViewActivity.
1 parent 0472d05 commit b120e92

8 files changed

Lines changed: 167 additions & 121 deletions

File tree

packages/url_launcher/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 5.1.2
2+
3+
* Update AGP and gradle.
4+
* Split plugin and WebViewActivity class files.
5+
16
## 5.1.1
27

38
* Suppress a handled deprecation warning on iOS

packages/url_launcher/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ buildscript {
2121
}
2222

2323
dependencies {
24-
classpath 'com.android.tools.build:gradle:3.3.0'
24+
classpath 'com.android.tools.build:gradle:3.4.2'
2525
}
2626
}
2727

packages/url_launcher/android/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="io.flutter.plugins.urllauncher">
33
<application>
4-
<activity android:name=".UrlLauncherPlugin$WebViewActivity"
4+
<activity android:name=".WebViewActivity"
55
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
66
android:exported="false"/>
77
</application>

packages/url_launcher/android/src/main/java/io/flutter/plugins/urllauncher/UrlLauncherPlugin.java

Lines changed: 27 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,17 @@
44

55
package io.flutter.plugins.urllauncher;
66

7-
import android.app.Activity;
8-
import android.content.BroadcastReceiver;
97
import android.content.ComponentName;
108
import android.content.Context;
119
import android.content.Intent;
12-
import android.content.IntentFilter;
1310
import android.net.Uri;
14-
import android.os.Build;
1511
import android.os.Bundle;
1612
import android.provider.Browser;
17-
import android.view.KeyEvent;
18-
import android.webkit.WebResourceRequest;
19-
import android.webkit.WebView;
20-
import android.webkit.WebViewClient;
2113
import io.flutter.plugin.common.MethodCall;
2214
import io.flutter.plugin.common.MethodChannel;
2315
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
2416
import io.flutter.plugin.common.MethodChannel.Result;
2517
import io.flutter.plugin.common.PluginRegistry.Registrar;
26-
import java.util.HashMap;
2718
import java.util.Map;
2819

2920
/** UrlLauncherPlugin */
@@ -43,15 +34,20 @@ private UrlLauncherPlugin(Registrar registrar) {
4334

4435
@Override
4536
public void onMethodCall(MethodCall call, Result result) {
46-
String url = call.argument("url");
47-
if (call.method.equals("canLaunch")) {
48-
canLaunch(url, result);
49-
} else if (call.method.equals("launch")) {
50-
launch(call, result, url);
51-
} else if (call.method.equals("closeWebView")) {
52-
closeWebView(result);
53-
} else {
54-
result.notImplemented();
37+
final String url = call.argument("url");
38+
switch (call.method) {
39+
case "canLaunch":
40+
canLaunch(url, result);
41+
break;
42+
case "launch":
43+
launch(call, result, url);
44+
break;
45+
case "closeWebView":
46+
closeWebView(result);
47+
break;
48+
default:
49+
result.notImplemented();
50+
break;
5551
}
5652
}
5753

@@ -74,31 +70,31 @@ private void launch(MethodCall call, Result result, String url) {
7470
final boolean enableJavaScript = call.argument("enableJavaScript");
7571
final boolean enableDomStorage = call.argument("enableDomStorage");
7672
final Map<String, String> headersMap = call.argument("headers");
77-
final Activity activity = mRegistrar.activity();
73+
final Bundle headersBundle = extractBundle(headersMap);
74+
final Context context = mRegistrar.activity();
7875

79-
if (activity == null) {
76+
if (context == null) {
8077
result.error("NO_ACTIVITY", "Launching a URL requires a foreground activity.", null);
8178
return;
8279
}
80+
8381
if (useWebView) {
84-
launchIntent = new Intent(activity, WebViewActivity.class);
85-
launchIntent.putExtra("url", url);
86-
launchIntent.putExtra("enableJavaScript", enableJavaScript);
87-
launchIntent.putExtra("enableDomStorage", enableDomStorage);
82+
launchIntent =
83+
WebViewActivity.createIntent(
84+
context, url, enableJavaScript, enableDomStorage, headersBundle);
8885
} else {
89-
launchIntent = new Intent(Intent.ACTION_VIEW);
90-
launchIntent.setData(Uri.parse(url));
86+
launchIntent =
87+
new Intent(Intent.ACTION_VIEW)
88+
.setData(Uri.parse(url))
89+
.putExtra(Browser.EXTRA_HEADERS, headersBundle);
9190
}
9291

93-
final Bundle headersBundle = extractBundle(headersMap);
94-
launchIntent.putExtra(Browser.EXTRA_HEADERS, headersBundle);
95-
96-
activity.startActivity(launchIntent);
92+
context.startActivity(launchIntent);
9793
result.success(true);
9894
}
9995

10096
private void closeWebView(Result result) {
101-
Intent intent = new Intent("close");
97+
Intent intent = new Intent(WebViewActivity.ACTION_CLOSE);
10298
mRegistrar.context().sendBroadcast(intent);
10399
result.success(null);
104100
}
@@ -111,89 +107,4 @@ private Bundle extractBundle(Map<String, String> headersMap) {
111107
}
112108
return headersBundle;
113109
}
114-
115-
/* Launches WebView activity */
116-
public static class WebViewActivity extends Activity {
117-
private WebView webview;
118-
private BroadcastReceiver broadcastReceiver;
119-
120-
@Override
121-
public void onCreate(Bundle savedInstanceState) {
122-
super.onCreate(savedInstanceState);
123-
webview = new WebView(this);
124-
setContentView(webview);
125-
// Get the Intent that started this activity and extract the string
126-
final Intent intent = getIntent();
127-
final String url = intent.getStringExtra("url");
128-
final boolean enableJavaScript = intent.getBooleanExtra("enableJavaScript", false);
129-
final boolean enableDomStorage = intent.getBooleanExtra("enableDomStorage", false);
130-
final Bundle headersBundle = intent.getBundleExtra(Browser.EXTRA_HEADERS);
131-
132-
final Map<String, String> headersMap = extractHeaders(headersBundle);
133-
webview.loadUrl(url, headersMap);
134-
135-
webview.getSettings().setJavaScriptEnabled(enableJavaScript);
136-
webview.getSettings().setDomStorageEnabled(enableDomStorage);
137-
138-
// Open new urls inside the webview itself.
139-
webview.setWebViewClient(
140-
new WebViewClient() {
141-
142-
@Override
143-
@SuppressWarnings("deprecation")
144-
public boolean shouldOverrideUrlLoading(WebView view, String url) {
145-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
146-
view.loadUrl(url);
147-
return false;
148-
}
149-
return super.shouldOverrideUrlLoading(view, url);
150-
}
151-
152-
@Override
153-
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
154-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
155-
view.loadUrl(request.getUrl().toString());
156-
}
157-
return false;
158-
}
159-
});
160-
161-
// Set broadcast receiver to handle calls to close the web view
162-
broadcastReceiver =
163-
new BroadcastReceiver() {
164-
@Override
165-
public void onReceive(Context arg0, Intent intent) {
166-
String action = intent.getAction();
167-
if ("close".equals(action)) {
168-
finish();
169-
}
170-
}
171-
};
172-
registerReceiver(broadcastReceiver, new IntentFilter("close"));
173-
}
174-
175-
private Map<String, String> extractHeaders(Bundle headersBundle) {
176-
final Map<String, String> headersMap = new HashMap<>();
177-
for (String key : headersBundle.keySet()) {
178-
final String value = headersBundle.getString(key);
179-
headersMap.put(key, value);
180-
}
181-
return headersMap;
182-
}
183-
184-
@Override
185-
protected void onDestroy() {
186-
super.onDestroy();
187-
unregisterReceiver(broadcastReceiver);
188-
}
189-
190-
@Override
191-
public boolean onKeyDown(int keyCode, KeyEvent event) {
192-
if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
193-
webview.goBack();
194-
return true;
195-
}
196-
return super.onKeyDown(keyCode, event);
197-
}
198-
}
199110
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package io.flutter.plugins.urllauncher;
2+
3+
import android.app.Activity;
4+
import android.content.BroadcastReceiver;
5+
import android.content.Context;
6+
import android.content.Intent;
7+
import android.content.IntentFilter;
8+
import android.os.Build;
9+
import android.os.Bundle;
10+
import android.provider.Browser;
11+
import android.view.KeyEvent;
12+
import android.webkit.WebResourceRequest;
13+
import android.webkit.WebView;
14+
import android.webkit.WebViewClient;
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
18+
/* Launches WebView activity */
19+
public class WebViewActivity extends Activity {
20+
21+
/*
22+
* Use this to trigger a BroadcastReceiver inside WebViewActivity
23+
* that will request the current instance to finish.
24+
* */
25+
public static String ACTION_CLOSE = "close action";
26+
27+
private final BroadcastReceiver broadcastReceiver =
28+
new BroadcastReceiver() {
29+
@Override
30+
public void onReceive(Context context, Intent intent) {
31+
String action = intent.getAction();
32+
if (ACTION_CLOSE.equals(action)) {
33+
finish();
34+
}
35+
}
36+
};
37+
38+
private final WebViewClient webViewClient =
39+
new WebViewClient() {
40+
41+
@Override
42+
public boolean shouldOverrideUrlLoading(WebView view, String url) {
43+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
44+
view.loadUrl(url);
45+
return false;
46+
}
47+
return super.shouldOverrideUrlLoading(view, url);
48+
}
49+
50+
@Override
51+
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
52+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
53+
view.loadUrl(request.getUrl().toString());
54+
}
55+
return false;
56+
}
57+
};
58+
59+
private WebView webview;
60+
61+
private IntentFilter closeIntentFilter = new IntentFilter(ACTION_CLOSE);
62+
63+
@Override
64+
public void onCreate(Bundle savedInstanceState) {
65+
super.onCreate(savedInstanceState);
66+
webview = new WebView(this);
67+
setContentView(webview);
68+
// Get the Intent that started this activity and extract the string
69+
final Intent intent = getIntent();
70+
final String url = intent.getStringExtra(URL_EXTRA);
71+
final boolean enableJavaScript = intent.getBooleanExtra(ENABLE_JS_EXTRA, false);
72+
final boolean enableDomStorage = intent.getBooleanExtra(ENABLE_DOM_EXTRA, false);
73+
final Bundle headersBundle = intent.getBundleExtra(Browser.EXTRA_HEADERS);
74+
75+
final Map<String, String> headersMap = extractHeaders(headersBundle);
76+
webview.loadUrl(url, headersMap);
77+
78+
webview.getSettings().setJavaScriptEnabled(enableJavaScript);
79+
webview.getSettings().setDomStorageEnabled(enableDomStorage);
80+
81+
// Open new urls inside the webview itself.
82+
webview.setWebViewClient(webViewClient);
83+
84+
// Register receiver that may finish this Activity.
85+
registerReceiver(broadcastReceiver, closeIntentFilter);
86+
}
87+
88+
private Map<String, String> extractHeaders(Bundle headersBundle) {
89+
final Map<String, String> headersMap = new HashMap<>();
90+
for (String key : headersBundle.keySet()) {
91+
final String value = headersBundle.getString(key);
92+
headersMap.put(key, value);
93+
}
94+
return headersMap;
95+
}
96+
97+
@Override
98+
protected void onDestroy() {
99+
super.onDestroy();
100+
unregisterReceiver(broadcastReceiver);
101+
}
102+
103+
@Override
104+
public boolean onKeyDown(int keyCode, KeyEvent event) {
105+
if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) {
106+
webview.goBack();
107+
return true;
108+
}
109+
return super.onKeyDown(keyCode, event);
110+
}
111+
112+
private static String URL_EXTRA = "url";
113+
private static String ENABLE_JS_EXTRA = "enableJavaScript";
114+
private static String ENABLE_DOM_EXTRA = "enableDomStorage";
115+
116+
/* Hides the constants used to forward data to the Activity instance. */
117+
public static Intent createIntent(
118+
Context context,
119+
String url,
120+
boolean enableJavaScript,
121+
boolean enableDomStorage,
122+
Bundle headersBundle) {
123+
return new Intent(context, WebViewActivity.class)
124+
.putExtra(URL_EXTRA, url)
125+
.putExtra(ENABLE_JS_EXTRA, enableJavaScript)
126+
.putExtra(ENABLE_DOM_EXTRA, enableDomStorage)
127+
.putExtra(Browser.EXTRA_HEADERS, headersBundle);
128+
}
129+
}

packages/url_launcher/example/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
}
66

77
dependencies {
8-
classpath 'com.android.tools.build:gradle:3.3.0'
8+
classpath 'com.android.tools.build:gradle:3.4.2'
99
}
1010
}
1111

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#Wed Jul 31 20:16:04 BRT 2019
12
distributionBase=GRADLE_USER_HOME
23
distributionPath=wrapper/dists
34
zipStoreBase=GRADLE_USER_HOME
45
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip

packages/url_launcher/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for launching a URL on Android and iOS. Supports
33
web, phone, SMS, and email schemes.
44
author: Flutter Team <flutter-dev@googlegroups.com>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher
6-
version: 5.1.1
6+
version: 5.1.2
77

88
flutter:
99
plugin:

0 commit comments

Comments
 (0)