forked from flutter/plugins
-
Notifications
You must be signed in to change notification settings - Fork 8
/
WebViewBuilder.java
140 lines (124 loc) · 5.39 KB
/
WebViewBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package io.flutter.plugins.webviewflutter;
import android.content.Context;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import androidx.annotation.NonNull;
/** Builder used to create {@link android.webkit.WebView} objects. */
public class WebViewBuilder {
/** Factory used to create a new {@link android.webkit.WebView} instance. */
static class WebViewFactory {
/**
* Creates a new {@link android.webkit.WebView} instance.
*
* @param context an Activity Context to access application assets. This value cannot be null.
* @param usesHybridComposition If {@code false} a {@link InputAwareWebView} instance is
* returned.
* @param containerView must be supplied when the {@code useHybridComposition} parameter is set
* to {@code false}. Used to create an InputConnection on the WebView's dedicated input, or
* IME, thread (see also {@link InputAwareWebView})
* @return A new instance of the {@link android.webkit.WebView} object.
*/
static WebView create(Context context, boolean usesHybridComposition, View containerView) {
return usesHybridComposition
? new WebView(context)
: new InputAwareWebView(context, containerView);
}
}
private final Context context;
private final View containerView;
private boolean enableDomStorage;
private boolean javaScriptCanOpenWindowsAutomatically;
private boolean supportMultipleWindows;
private boolean usesHybridComposition;
private WebChromeClient webChromeClient;
/**
* Constructs a new {@link WebViewBuilder} object with a custom implementation of the {@link
* WebViewFactory} object.
*
* @param context an Activity Context to access application assets. This value cannot be null.
* @param containerView must be supplied when the {@code useHybridComposition} parameter is set to
* {@code false}. Used to create an InputConnection on the WebView's dedicated input, or IME,
* thread (see also {@link InputAwareWebView})
*/
WebViewBuilder(@NonNull final Context context, View containerView) {
this.context = context;
this.containerView = containerView;
}
/**
* Sets whether the DOM storage API is enabled. The default value is {@code false}.
*
* @param flag {@code true} is {@link android.webkit.WebView} should use the DOM storage API.
* @return This builder. This value cannot be {@code null}.
*/
public WebViewBuilder setDomStorageEnabled(boolean flag) {
this.enableDomStorage = flag;
return this;
}
/**
* Sets whether JavaScript is allowed to open windows automatically. This applies to the
* JavaScript function {@code window.open()}. The default value is {@code false}.
*
* @param flag {@code true} if JavaScript is allowed to open windows automatically.
* @return This builder. This value cannot be {@code null}.
*/
public WebViewBuilder setJavaScriptCanOpenWindowsAutomatically(boolean flag) {
this.javaScriptCanOpenWindowsAutomatically = flag;
return this;
}
/**
* Sets whether the {@link WebView} supports multiple windows. If set to {@code true}, {@link
* WebChromeClient#onCreateWindow} must be implemented by the host application. The default is
* {@code false}.
*
* @param flag {@code true} if multiple windows are supported.
* @return This builder. This value cannot be {@code null}.
*/
public WebViewBuilder setSupportMultipleWindows(boolean flag) {
this.supportMultipleWindows = flag;
return this;
}
/**
* Sets whether the hybrid composition should be used.
*
* <p>If set to {@code true} a standard {@link WebView} is created. If set to {@code false} the
* {@link WebViewBuilder} will create a {@link InputAwareWebView} to workaround issues using the
* {@link WebView} on Android versions below N.
*
* @param flag {@code true} if uses hybrid composition. The default is {@code false}.
* @return This builder. This value cannot be {@code null}
*/
public WebViewBuilder setUsesHybridComposition(boolean flag) {
this.usesHybridComposition = flag;
return this;
}
/**
* Sets the chrome handler. This is an implementation of WebChromeClient for use in handling
* JavaScript dialogs, favicons, titles, and the progress. This will replace the current handler.
*
* @param webChromeClient an implementation of WebChromeClient This value may be null.
* @return This builder. This value cannot be {@code null}.
*/
public WebViewBuilder setWebChromeClient(WebChromeClient webChromeClient) {
this.webChromeClient = webChromeClient;
return this;
}
/**
* Build the {@link android.webkit.WebView} using the current settings.
*
* @return The {@link android.webkit.WebView} using the current settings.
*/
public WebView build() {
WebView webView = WebViewFactory.create(context, usesHybridComposition, containerView);
WebSettings webSettings = webView.getSettings();
webSettings.setDomStorageEnabled(enableDomStorage);
webSettings.setJavaScriptCanOpenWindowsAutomatically(javaScriptCanOpenWindowsAutomatically);
webSettings.setSupportMultipleWindows(supportMultipleWindows);
webView.setWebChromeClient(webChromeClient);
return webView;
}
}