forked from NativeScript/NativeScript
/
web-view.android.ts
176 lines (145 loc) · 5.24 KB
/
web-view.android.ts
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { WebViewBase, knownFolders, traceEnabled, traceWrite, traceCategories } from "./web-view-common";
export * from "./web-view-common";
interface WebViewClient {
new (owner: WebView): android.webkit.WebViewClient;
}
let WebViewClient: WebViewClient;
export class WebViewClientImpl extends android.webkit.WebViewClient {
constructor(public owner: WebViewBase) {
super();
return global.__native(this);
}
public shouldOverrideUrlLoading(view: android.webkit.WebView, url: string) {
if (traceEnabled()) {
traceWrite("WebViewClientClass.shouldOverrideUrlLoading(" + url + ")", traceCategories.Debug);
}
return false;
}
public onPageStarted(view: android.webkit.WebView, url: string, favicon: android.graphics.Bitmap) {
super.onPageStarted(view, url, favicon);
const owner = this.owner;
if (owner) {
if (traceEnabled()) {
traceWrite("WebViewClientClass.onPageStarted(" + url + ", " + favicon + ")", traceCategories.Debug);
}
owner._onLoadStarted(url, undefined);
}
}
public onPageFinished(view: android.webkit.WebView, url: string) {
super.onPageFinished(view, url);
const owner = this.owner;
if (owner) {
if (traceEnabled()) {
traceWrite("WebViewClientClass.onPageFinished(" + url + ")", traceCategories.Debug);
}
owner._onLoadFinished(url, undefined);
}
}
public onReceivedError() {
let view: android.webkit.WebView = arguments[0];
if (arguments.length === 4) {
let errorCode: number = arguments[1];
let description: string = arguments[2];
let failingUrl: string = arguments[3];
super.onReceivedError(view, errorCode, description, failingUrl);
const owner = this.owner;
if (owner) {
if (traceEnabled()) {
traceWrite("WebViewClientClass.onReceivedError(" + errorCode + ", " + description + ", " + failingUrl + ")", traceCategories.Debug);
}
owner._onLoadFinished(failingUrl, description + "(" + errorCode + ")");
}
} else {
let request: any = arguments[1];
let error: any = arguments[2];
super.onReceivedError(view, request, error);
const owner = this.owner;
if (owner) {
if (traceEnabled()) {
traceWrite("WebViewClientClass.onReceivedError(" + error.getErrorCode() + ", " + error.getDescription() + ", " + (error.getUrl && error.getUrl()) + ")", traceCategories.Debug);
}
owner._onLoadFinished(error.getUrl && error.getUrl(), error.getDescription() + "(" + error.getErrorCode() + ")");
}
}
}
};
function initializeWebViewClient(): void {
if (WebViewClient) {
return;
}
WebViewClient = WebViewClientImpl;
}
export class WebView extends WebViewBase {
nativeViewProtected: android.webkit.WebView;
public createNativeView() {
initializeWebViewClient();
const nativeView = new android.webkit.WebView(this._context);
nativeView.getSettings().setJavaScriptEnabled(true);
nativeView.getSettings().setBuiltInZoomControls(true);
const client = new WebViewClient(this);
nativeView.setWebViewClient(client);
(<any>nativeView).client = client;
return nativeView;
}
public initNativeView(): void {
super.initNativeView();
(<any>this.nativeViewProtected).client.owner = this;
}
public disposeNativeView() {
const nativeView = this.nativeViewProtected;
if (nativeView) {
nativeView.destroy();
}
(<any>nativeView).client.owner = null;
super.disposeNativeView();
}
public _loadUrl(src: string) {
const nativeView = this.nativeViewProtected;
if (!nativeView) {
return;
}
nativeView.loadUrl(src);
}
public _loadData(src: string) {
const nativeView = this.nativeViewProtected;
if (!nativeView) {
return;
}
const baseUrl = `file:///${knownFolders.currentApp().path}/`;
nativeView.loadDataWithBaseURL(baseUrl, src, "text/html", "utf-8", null);
}
get canGoBack(): boolean {
return this.nativeViewProtected.canGoBack();
}
public stopLoading() {
const nativeView = this.nativeViewProtected;
if (nativeView) {
nativeView.stopLoading();
}
}
get canGoForward(): boolean {
const nativeView = this.nativeViewProtected;
if (nativeView) {
return nativeView.canGoForward();
}
return false;
}
public goBack() {
const nativeView = this.nativeViewProtected;
if (nativeView) {
return nativeView.goBack();
}
}
public goForward() {
const nativeView = this.nativeViewProtected;
if (nativeView) {
return nativeView.goForward();
}
}
public reload() {
const nativeView = this.nativeViewProtected;
if (nativeView) {
return nativeView.reload();
}
}
}