@capawesome/capacitor-in-app-browser 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapawesomeCapacitorInAppBrowser.podspec +17 -0
- package/LICENSE +21 -0
- package/Package.swift +28 -0
- package/README.md +690 -0
- package/android/build.gradle +60 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/InAppBrowser.java +242 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/InAppBrowserPlugin.java +289 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/CustomException.java +20 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/CustomExceptions.java +20 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/WebViewDialog.java +343 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/events/BrowserPageNavigationCompletedEvent.java +23 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/events/MessageReceivedEvent.java +23 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/options/ExecuteScriptOptions.java +29 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/options/GetCookiesOptions.java +29 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/options/OpenInExternalBrowserOptions.java +30 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/options/OpenInSystemBrowserOptions.java +74 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/options/OpenInWebViewOptions.java +112 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/options/PostMessageOptions.java +30 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/options/WebViewToolbarOptions.java +87 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/results/ExecuteScriptResult.java +25 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/classes/results/GetCookiesResult.java +28 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/interfaces/Callback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/interfaces/EmptyCallback.java +5 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/interfaces/NonEmptyResultCallback.java +7 -0
- package/android/src/main/java/io/capawesome/capacitorjs/plugins/inappbrowser/interfaces/Result.java +7 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +1173 -0
- package/dist/esm/definitions.d.ts +540 -0
- package/dist/esm/definitions.js +19 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/web.d.ts +14 -0
- package/dist/esm/web.js +35 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +68 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +71 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Plugin/Classes/Events/BrowserPageNavigationCompletedEvent.swift +16 -0
- package/ios/Plugin/Classes/Events/MessageReceivedEvent.swift +24 -0
- package/ios/Plugin/Classes/Options/ExecuteScriptOptions.swift +17 -0
- package/ios/Plugin/Classes/Options/GetCookiesOptions.swift +20 -0
- package/ios/Plugin/Classes/Options/OpenInExternalBrowserOptions.swift +20 -0
- package/ios/Plugin/Classes/Options/OpenInSystemBrowserOptions.swift +52 -0
- package/ios/Plugin/Classes/Options/OpenInWebViewOptions.swift +48 -0
- package/ios/Plugin/Classes/Options/PostMessageOptions.swift +17 -0
- package/ios/Plugin/Classes/Options/WebViewToolbarOptions.swift +37 -0
- package/ios/Plugin/Classes/Results/ExecuteScriptResult.swift +25 -0
- package/ios/Plugin/Classes/Results/GetCookiesResult.swift +20 -0
- package/ios/Plugin/Classes/WebViewController.swift +224 -0
- package/ios/Plugin/Enums/CustomError.swift +49 -0
- package/ios/Plugin/InAppBrowser.swift +194 -0
- package/ios/Plugin/InAppBrowserHelper.swift +49 -0
- package/ios/Plugin/InAppBrowserPlugin.swift +191 -0
- package/ios/Plugin/Info.plist +24 -0
- package/ios/Plugin/Protocols/Result.swift +5 -0
- package/package.json +91 -0
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.inappbrowser.classes;
|
|
2
|
+
|
|
3
|
+
import android.Manifest;
|
|
4
|
+
import android.annotation.SuppressLint;
|
|
5
|
+
import android.app.Dialog;
|
|
6
|
+
import android.content.Context;
|
|
7
|
+
import android.content.pm.PackageManager;
|
|
8
|
+
import android.graphics.Bitmap;
|
|
9
|
+
import android.graphics.Color;
|
|
10
|
+
import android.graphics.Typeface;
|
|
11
|
+
import android.os.Bundle;
|
|
12
|
+
import android.text.TextUtils;
|
|
13
|
+
import android.util.TypedValue;
|
|
14
|
+
import android.view.Gravity;
|
|
15
|
+
import android.view.ViewGroup;
|
|
16
|
+
import android.webkit.JavascriptInterface;
|
|
17
|
+
import android.webkit.PermissionRequest;
|
|
18
|
+
import android.webkit.ValueCallback;
|
|
19
|
+
import android.webkit.WebChromeClient;
|
|
20
|
+
import android.webkit.WebSettings;
|
|
21
|
+
import android.webkit.WebView;
|
|
22
|
+
import android.webkit.WebViewClient;
|
|
23
|
+
import android.widget.LinearLayout;
|
|
24
|
+
import android.widget.TextView;
|
|
25
|
+
import androidx.annotation.NonNull;
|
|
26
|
+
import androidx.annotation.Nullable;
|
|
27
|
+
import androidx.core.content.ContextCompat;
|
|
28
|
+
import io.capawesome.capacitorjs.plugins.inappbrowser.classes.options.OpenInWebViewOptions;
|
|
29
|
+
import io.capawesome.capacitorjs.plugins.inappbrowser.classes.options.WebViewToolbarOptions;
|
|
30
|
+
import java.util.ArrayList;
|
|
31
|
+
import java.util.List;
|
|
32
|
+
|
|
33
|
+
public class WebViewDialog extends Dialog {
|
|
34
|
+
|
|
35
|
+
public interface Listener {
|
|
36
|
+
void onClosed(@NonNull WebViewDialog dialog);
|
|
37
|
+
void onMessageReceived(@NonNull String data);
|
|
38
|
+
void onPageLoaded();
|
|
39
|
+
void onPageNavigationCompleted(@NonNull String url);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private static final String BRIDGE_JAVASCRIPT =
|
|
43
|
+
"window.CapacitorInAppBrowser = window.CapacitorInAppBrowser || { postMessage: function (data) { CapacitorInAppBrowserAndroid.postMessage(JSON.stringify(data)); } };";
|
|
44
|
+
private static final String BRIDGE_NAME = "CapacitorInAppBrowserAndroid";
|
|
45
|
+
private static final int TOOLBAR_HEIGHT_IN_DP = 56;
|
|
46
|
+
|
|
47
|
+
@Nullable
|
|
48
|
+
private TextView backButton;
|
|
49
|
+
|
|
50
|
+
@Nullable
|
|
51
|
+
private TextView forwardButton;
|
|
52
|
+
|
|
53
|
+
private boolean initialLoadNotified = false;
|
|
54
|
+
|
|
55
|
+
@NonNull
|
|
56
|
+
private final Listener listener;
|
|
57
|
+
|
|
58
|
+
@NonNull
|
|
59
|
+
private final OpenInWebViewOptions options;
|
|
60
|
+
|
|
61
|
+
@Nullable
|
|
62
|
+
private TextView titleView;
|
|
63
|
+
|
|
64
|
+
@Nullable
|
|
65
|
+
private WebView webView;
|
|
66
|
+
|
|
67
|
+
public WebViewDialog(@NonNull Context context, @NonNull OpenInWebViewOptions options, @NonNull Listener listener) {
|
|
68
|
+
super(context, android.R.style.Theme_Material_Light_NoActionBar);
|
|
69
|
+
this.options = options;
|
|
70
|
+
this.listener = listener;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public void executeScript(@NonNull String script, @NonNull ValueCallback<String> resultCallback) {
|
|
74
|
+
WebView webView = this.webView;
|
|
75
|
+
if (webView != null) {
|
|
76
|
+
webView.evaluateJavascript(script, resultCallback);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public void handleOnPause() {
|
|
81
|
+
WebView webView = this.webView;
|
|
82
|
+
if (options.getPauseMediaWhenHidden() && webView != null) {
|
|
83
|
+
webView.onPause();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public void handleOnResume() {
|
|
88
|
+
WebView webView = this.webView;
|
|
89
|
+
if (options.getPauseMediaWhenHidden() && webView != null) {
|
|
90
|
+
webView.onResume();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
@Override
|
|
95
|
+
public void onBackPressed() {
|
|
96
|
+
WebView webView = this.webView;
|
|
97
|
+
if (options.getHardwareBackButton() && webView != null && webView.canGoBack()) {
|
|
98
|
+
webView.goBack();
|
|
99
|
+
} else {
|
|
100
|
+
dismiss();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public void postMessage(@NonNull String data) {
|
|
105
|
+
WebView webView = this.webView;
|
|
106
|
+
if (webView != null) {
|
|
107
|
+
webView.evaluateJavascript(
|
|
108
|
+
"window.dispatchEvent(new CustomEvent('capacitorInAppBrowserMessage', { detail: " + data + " }));",
|
|
109
|
+
null
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
@Override
|
|
115
|
+
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
116
|
+
super.onCreate(savedInstanceState);
|
|
117
|
+
WebView webView = createWebView();
|
|
118
|
+
this.webView = webView;
|
|
119
|
+
setContentView(createContentView(webView));
|
|
120
|
+
setOnDismissListener(dialog -> handleDismiss());
|
|
121
|
+
webView.loadUrl(options.getUri().toString(), options.getHeaders());
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
@NonNull
|
|
125
|
+
private LinearLayout createContentView(@NonNull WebView webView) {
|
|
126
|
+
LinearLayout contentView = new LinearLayout(getContext());
|
|
127
|
+
contentView.setOrientation(LinearLayout.VERTICAL);
|
|
128
|
+
contentView.setFitsSystemWindows(true);
|
|
129
|
+
contentView.setBackgroundColor(Color.WHITE);
|
|
130
|
+
if (options.getToolbar().getVisible()) {
|
|
131
|
+
contentView.addView(
|
|
132
|
+
createToolbar(),
|
|
133
|
+
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpToPx(TOOLBAR_HEIGHT_IN_DP))
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
contentView.addView(webView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1));
|
|
137
|
+
return contentView;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
@NonNull
|
|
141
|
+
private LinearLayout createToolbar() {
|
|
142
|
+
WebViewToolbarOptions toolbarOptions = options.getToolbar();
|
|
143
|
+
int textColor = toolbarOptions.getColor() == null ? Color.BLACK : toolbarOptions.getColor();
|
|
144
|
+
|
|
145
|
+
LinearLayout toolbar = new LinearLayout(getContext());
|
|
146
|
+
toolbar.setOrientation(LinearLayout.HORIZONTAL);
|
|
147
|
+
toolbar.setGravity(Gravity.CENTER_VERTICAL);
|
|
148
|
+
toolbar.setBackgroundColor(toolbarOptions.getBackgroundColor() == null ? Color.WHITE : toolbarOptions.getBackgroundColor());
|
|
149
|
+
toolbar.setPadding(dpToPx(8), 0, dpToPx(8), 0);
|
|
150
|
+
|
|
151
|
+
TextView closeButton = createToolbarButton(toolbarOptions.getCloseButtonText(), textColor);
|
|
152
|
+
closeButton.setOnClickListener(view -> dismiss());
|
|
153
|
+
toolbar.addView(closeButton);
|
|
154
|
+
|
|
155
|
+
TextView titleView = new TextView(getContext());
|
|
156
|
+
titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
|
|
157
|
+
titleView.setTypeface(null, Typeface.BOLD);
|
|
158
|
+
titleView.setTextColor(textColor);
|
|
159
|
+
titleView.setMaxLines(1);
|
|
160
|
+
titleView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
|
|
161
|
+
titleView.setPadding(dpToPx(8), 0, dpToPx(8), 0);
|
|
162
|
+
toolbar.addView(titleView, new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
|
|
163
|
+
this.titleView = titleView;
|
|
164
|
+
|
|
165
|
+
if (toolbarOptions.getShowNavigationButtons()) {
|
|
166
|
+
TextView backButton = createToolbarButton("‹", textColor);
|
|
167
|
+
backButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
|
|
168
|
+
backButton.setOnClickListener(view -> {
|
|
169
|
+
WebView webView = this.webView;
|
|
170
|
+
if (webView != null && webView.canGoBack()) {
|
|
171
|
+
webView.goBack();
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
toolbar.addView(backButton);
|
|
175
|
+
this.backButton = backButton;
|
|
176
|
+
|
|
177
|
+
TextView forwardButton = createToolbarButton("›", textColor);
|
|
178
|
+
forwardButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
|
|
179
|
+
forwardButton.setOnClickListener(view -> {
|
|
180
|
+
WebView webView = this.webView;
|
|
181
|
+
if (webView != null && webView.canGoForward()) {
|
|
182
|
+
webView.goForward();
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
toolbar.addView(forwardButton);
|
|
186
|
+
this.forwardButton = forwardButton;
|
|
187
|
+
|
|
188
|
+
updateNavigationButtons();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return toolbar;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
@NonNull
|
|
195
|
+
private TextView createToolbarButton(@NonNull String text, int textColor) {
|
|
196
|
+
TextView button = new TextView(getContext());
|
|
197
|
+
button.setText(text);
|
|
198
|
+
button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
|
|
199
|
+
button.setTextColor(textColor);
|
|
200
|
+
button.setPadding(dpToPx(8), dpToPx(8), dpToPx(8), dpToPx(8));
|
|
201
|
+
button.setClickable(true);
|
|
202
|
+
return button;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
@NonNull
|
|
206
|
+
@SuppressLint("SetJavaScriptEnabled")
|
|
207
|
+
private WebView createWebView() {
|
|
208
|
+
WebView webView = new WebView(getContext());
|
|
209
|
+
WebSettings settings = webView.getSettings();
|
|
210
|
+
settings.setJavaScriptEnabled(true);
|
|
211
|
+
settings.setDomStorageEnabled(true);
|
|
212
|
+
settings.setMediaPlaybackRequiresUserGesture(options.getMediaPlaybackRequiresUserAction());
|
|
213
|
+
settings.setSupportZoom(options.getAllowZoom());
|
|
214
|
+
settings.setBuiltInZoomControls(options.getAllowZoom());
|
|
215
|
+
settings.setDisplayZoomControls(false);
|
|
216
|
+
String userAgent = options.getUserAgent();
|
|
217
|
+
if (userAgent != null) {
|
|
218
|
+
settings.setUserAgentString(userAgent);
|
|
219
|
+
}
|
|
220
|
+
webView.addJavascriptInterface(new Bridge(), BRIDGE_NAME);
|
|
221
|
+
webView.setWebViewClient(
|
|
222
|
+
new WebViewClient() {
|
|
223
|
+
@Override
|
|
224
|
+
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
|
225
|
+
injectBridgeJavaScript();
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
@Override
|
|
229
|
+
public void onPageFinished(WebView view, String url) {
|
|
230
|
+
injectBridgeJavaScript();
|
|
231
|
+
updateNavigationButtons();
|
|
232
|
+
updateTitle();
|
|
233
|
+
if (!initialLoadNotified) {
|
|
234
|
+
initialLoadNotified = true;
|
|
235
|
+
listener.onPageLoaded();
|
|
236
|
+
}
|
|
237
|
+
if (url != null) {
|
|
238
|
+
listener.onPageNavigationCompleted(url);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
@Override
|
|
243
|
+
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
|
|
244
|
+
updateNavigationButtons();
|
|
245
|
+
updateTitle();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
);
|
|
249
|
+
webView.setWebChromeClient(
|
|
250
|
+
new WebChromeClient() {
|
|
251
|
+
@Override
|
|
252
|
+
public void onPermissionRequest(PermissionRequest request) {
|
|
253
|
+
handlePermissionRequest(request);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
@Override
|
|
257
|
+
public void onReceivedTitle(WebView view, String title) {
|
|
258
|
+
updateTitle();
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
);
|
|
262
|
+
return webView;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
private int dpToPx(int dp) {
|
|
266
|
+
return Math.round(dp * getContext().getResources().getDisplayMetrics().density);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
private void handleDismiss() {
|
|
270
|
+
WebView webView = this.webView;
|
|
271
|
+
if (webView != null) {
|
|
272
|
+
webView.destroy();
|
|
273
|
+
this.webView = null;
|
|
274
|
+
}
|
|
275
|
+
listener.onClosed(this);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
private void handlePermissionRequest(@NonNull PermissionRequest request) {
|
|
279
|
+
List<String> grantedResources = new ArrayList<>();
|
|
280
|
+
for (String resource : request.getResources()) {
|
|
281
|
+
if (PermissionRequest.RESOURCE_VIDEO_CAPTURE.equals(resource) && hasPermission(Manifest.permission.CAMERA)) {
|
|
282
|
+
grantedResources.add(resource);
|
|
283
|
+
} else if (PermissionRequest.RESOURCE_AUDIO_CAPTURE.equals(resource) && hasPermission(Manifest.permission.RECORD_AUDIO)) {
|
|
284
|
+
grantedResources.add(resource);
|
|
285
|
+
} else if (PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID.equals(resource)) {
|
|
286
|
+
grantedResources.add(resource);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if (grantedResources.isEmpty()) {
|
|
290
|
+
request.deny();
|
|
291
|
+
} else {
|
|
292
|
+
request.grant(grantedResources.toArray(new String[0]));
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private boolean hasPermission(@NonNull String permission) {
|
|
297
|
+
return ContextCompat.checkSelfPermission(getContext(), permission) == PackageManager.PERMISSION_GRANTED;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
private void injectBridgeJavaScript() {
|
|
301
|
+
WebView webView = this.webView;
|
|
302
|
+
if (webView != null) {
|
|
303
|
+
webView.evaluateJavascript(BRIDGE_JAVASCRIPT, null);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
private void updateNavigationButtons() {
|
|
308
|
+
WebView webView = this.webView;
|
|
309
|
+
TextView backButton = this.backButton;
|
|
310
|
+
TextView forwardButton = this.forwardButton;
|
|
311
|
+
if (webView == null || backButton == null || forwardButton == null) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
backButton.setAlpha(webView.canGoBack() ? 1.0f : 0.3f);
|
|
315
|
+
forwardButton.setAlpha(webView.canGoForward() ? 1.0f : 0.3f);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
private void updateTitle() {
|
|
319
|
+
TextView titleView = this.titleView;
|
|
320
|
+
WebView webView = this.webView;
|
|
321
|
+
if (titleView == null || webView == null) {
|
|
322
|
+
return;
|
|
323
|
+
}
|
|
324
|
+
WebViewToolbarOptions toolbarOptions = options.getToolbar();
|
|
325
|
+
if (toolbarOptions.getTitle() != null) {
|
|
326
|
+
titleView.setText(toolbarOptions.getTitle());
|
|
327
|
+
} else if (toolbarOptions.getShowUrl()) {
|
|
328
|
+
titleView.setText(webView.getUrl());
|
|
329
|
+
} else {
|
|
330
|
+
titleView.setText(webView.getTitle());
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
private class Bridge {
|
|
335
|
+
|
|
336
|
+
@JavascriptInterface
|
|
337
|
+
public void postMessage(@Nullable String data) {
|
|
338
|
+
if (data != null) {
|
|
339
|
+
listener.onMessageReceived(data);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.inappbrowser.classes.events;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.JSObject;
|
|
5
|
+
import io.capawesome.capacitorjs.plugins.inappbrowser.interfaces.Result;
|
|
6
|
+
|
|
7
|
+
public class BrowserPageNavigationCompletedEvent implements Result {
|
|
8
|
+
|
|
9
|
+
@NonNull
|
|
10
|
+
private final String url;
|
|
11
|
+
|
|
12
|
+
public BrowserPageNavigationCompletedEvent(@NonNull String url) {
|
|
13
|
+
this.url = url;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@Override
|
|
17
|
+
@NonNull
|
|
18
|
+
public JSObject toJSObject() {
|
|
19
|
+
JSObject result = new JSObject();
|
|
20
|
+
result.put("url", url);
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.inappbrowser.classes.events;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.JSObject;
|
|
5
|
+
import io.capawesome.capacitorjs.plugins.inappbrowser.interfaces.Result;
|
|
6
|
+
|
|
7
|
+
public class MessageReceivedEvent implements Result {
|
|
8
|
+
|
|
9
|
+
@NonNull
|
|
10
|
+
private final Object data;
|
|
11
|
+
|
|
12
|
+
public MessageReceivedEvent(@NonNull Object data) {
|
|
13
|
+
this.data = data;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@Override
|
|
17
|
+
@NonNull
|
|
18
|
+
public JSObject toJSObject() {
|
|
19
|
+
JSObject result = new JSObject();
|
|
20
|
+
result.put("data", data);
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.inappbrowser.classes.options;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.PluginCall;
|
|
5
|
+
import io.capawesome.capacitorjs.plugins.inappbrowser.classes.CustomExceptions;
|
|
6
|
+
|
|
7
|
+
public class ExecuteScriptOptions {
|
|
8
|
+
|
|
9
|
+
@NonNull
|
|
10
|
+
private final String script;
|
|
11
|
+
|
|
12
|
+
public ExecuteScriptOptions(@NonNull PluginCall call) throws Exception {
|
|
13
|
+
this.script = ExecuteScriptOptions.getScriptFromCall(call);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@NonNull
|
|
17
|
+
public String getScript() {
|
|
18
|
+
return script;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@NonNull
|
|
22
|
+
private static String getScriptFromCall(@NonNull PluginCall call) throws Exception {
|
|
23
|
+
String script = call.getString("script");
|
|
24
|
+
if (script == null || script.isEmpty()) {
|
|
25
|
+
throw CustomExceptions.SCRIPT_MISSING;
|
|
26
|
+
}
|
|
27
|
+
return script;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.inappbrowser.classes.options;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.PluginCall;
|
|
5
|
+
import io.capawesome.capacitorjs.plugins.inappbrowser.classes.CustomExceptions;
|
|
6
|
+
|
|
7
|
+
public class GetCookiesOptions {
|
|
8
|
+
|
|
9
|
+
@NonNull
|
|
10
|
+
private final String url;
|
|
11
|
+
|
|
12
|
+
public GetCookiesOptions(@NonNull PluginCall call) throws Exception {
|
|
13
|
+
this.url = GetCookiesOptions.getUrlFromCall(call);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@NonNull
|
|
17
|
+
public String getUrl() {
|
|
18
|
+
return url;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@NonNull
|
|
22
|
+
private static String getUrlFromCall(@NonNull PluginCall call) throws Exception {
|
|
23
|
+
String url = call.getString("url");
|
|
24
|
+
if (url == null || url.isEmpty()) {
|
|
25
|
+
throw CustomExceptions.URL_MISSING;
|
|
26
|
+
}
|
|
27
|
+
return url;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.inappbrowser.classes.options;
|
|
2
|
+
|
|
3
|
+
import android.net.Uri;
|
|
4
|
+
import androidx.annotation.NonNull;
|
|
5
|
+
import com.getcapacitor.PluginCall;
|
|
6
|
+
import io.capawesome.capacitorjs.plugins.inappbrowser.classes.CustomExceptions;
|
|
7
|
+
|
|
8
|
+
public class OpenInExternalBrowserOptions {
|
|
9
|
+
|
|
10
|
+
@NonNull
|
|
11
|
+
private final Uri uri;
|
|
12
|
+
|
|
13
|
+
public OpenInExternalBrowserOptions(@NonNull PluginCall call) throws Exception {
|
|
14
|
+
this.uri = OpenInExternalBrowserOptions.getUriFromCall(call);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@NonNull
|
|
18
|
+
public Uri getUri() {
|
|
19
|
+
return uri;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@NonNull
|
|
23
|
+
private static Uri getUriFromCall(@NonNull PluginCall call) throws Exception {
|
|
24
|
+
String url = call.getString("url");
|
|
25
|
+
if (url == null || url.isEmpty()) {
|
|
26
|
+
throw CustomExceptions.URL_MISSING;
|
|
27
|
+
}
|
|
28
|
+
return Uri.parse(url);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.inappbrowser.classes.options;
|
|
2
|
+
|
|
3
|
+
import android.graphics.Color;
|
|
4
|
+
import android.net.Uri;
|
|
5
|
+
import androidx.annotation.NonNull;
|
|
6
|
+
import androidx.annotation.Nullable;
|
|
7
|
+
import com.getcapacitor.JSObject;
|
|
8
|
+
import com.getcapacitor.PluginCall;
|
|
9
|
+
import io.capawesome.capacitorjs.plugins.inappbrowser.classes.CustomExceptions;
|
|
10
|
+
|
|
11
|
+
public class OpenInSystemBrowserOptions {
|
|
12
|
+
|
|
13
|
+
private final boolean hideToolbarOnScroll;
|
|
14
|
+
private final boolean showTitle;
|
|
15
|
+
|
|
16
|
+
@Nullable
|
|
17
|
+
private final Integer toolbarColor;
|
|
18
|
+
|
|
19
|
+
@NonNull
|
|
20
|
+
private final Uri uri;
|
|
21
|
+
|
|
22
|
+
public OpenInSystemBrowserOptions(@NonNull PluginCall call) throws Exception {
|
|
23
|
+
JSObject androidOptions = call.getObject("android");
|
|
24
|
+
this.hideToolbarOnScroll = androidOptions != null && androidOptions.optBoolean("hideToolbarOnScroll", false);
|
|
25
|
+
this.showTitle = androidOptions != null && androidOptions.optBoolean("showTitle", false);
|
|
26
|
+
this.toolbarColor = OpenInSystemBrowserOptions.getToolbarColorFromObject(androidOptions);
|
|
27
|
+
this.uri = OpenInSystemBrowserOptions.getUriFromCall(call);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public boolean getHideToolbarOnScroll() {
|
|
31
|
+
return hideToolbarOnScroll;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public boolean getShowTitle() {
|
|
35
|
+
return showTitle;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@Nullable
|
|
39
|
+
public Integer getToolbarColor() {
|
|
40
|
+
return toolbarColor;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
@NonNull
|
|
44
|
+
public Uri getUri() {
|
|
45
|
+
return uri;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@Nullable
|
|
49
|
+
private static Integer getToolbarColorFromObject(@Nullable JSObject object) throws Exception {
|
|
50
|
+
String toolbarColor = object == null ? null : object.getString("toolbarColor");
|
|
51
|
+
if (toolbarColor == null) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
return Color.parseColor(toolbarColor);
|
|
56
|
+
} catch (IllegalArgumentException exception) {
|
|
57
|
+
throw CustomExceptions.TOOLBAR_COLOR_INVALID;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@NonNull
|
|
62
|
+
private static Uri getUriFromCall(@NonNull PluginCall call) throws Exception {
|
|
63
|
+
String url = call.getString("url");
|
|
64
|
+
if (url == null || url.isEmpty()) {
|
|
65
|
+
throw CustomExceptions.URL_MISSING;
|
|
66
|
+
}
|
|
67
|
+
Uri uri = Uri.parse(url);
|
|
68
|
+
String scheme = uri.getScheme();
|
|
69
|
+
if (scheme == null || !(scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))) {
|
|
70
|
+
throw CustomExceptions.URL_INVALID;
|
|
71
|
+
}
|
|
72
|
+
return uri;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.inappbrowser.classes.options;
|
|
2
|
+
|
|
3
|
+
import android.net.Uri;
|
|
4
|
+
import androidx.annotation.NonNull;
|
|
5
|
+
import androidx.annotation.Nullable;
|
|
6
|
+
import com.getcapacitor.JSObject;
|
|
7
|
+
import com.getcapacitor.PluginCall;
|
|
8
|
+
import io.capawesome.capacitorjs.plugins.inappbrowser.classes.CustomExceptions;
|
|
9
|
+
import java.util.HashMap;
|
|
10
|
+
import java.util.Iterator;
|
|
11
|
+
import java.util.Map;
|
|
12
|
+
|
|
13
|
+
public class OpenInWebViewOptions {
|
|
14
|
+
|
|
15
|
+
private final boolean allowZoom;
|
|
16
|
+
private final boolean hardwareBackButton;
|
|
17
|
+
|
|
18
|
+
@NonNull
|
|
19
|
+
private final Map<String, String> headers;
|
|
20
|
+
|
|
21
|
+
private final boolean mediaPlaybackRequiresUserAction;
|
|
22
|
+
private final boolean pauseMediaWhenHidden;
|
|
23
|
+
|
|
24
|
+
@NonNull
|
|
25
|
+
private final WebViewToolbarOptions toolbar;
|
|
26
|
+
|
|
27
|
+
@NonNull
|
|
28
|
+
private final Uri uri;
|
|
29
|
+
|
|
30
|
+
@Nullable
|
|
31
|
+
private final String userAgent;
|
|
32
|
+
|
|
33
|
+
public OpenInWebViewOptions(@NonNull PluginCall call) throws Exception {
|
|
34
|
+
JSObject androidOptions = call.getObject("android");
|
|
35
|
+
this.allowZoom = androidOptions != null && androidOptions.optBoolean("allowZoom", false);
|
|
36
|
+
this.hardwareBackButton = androidOptions == null || androidOptions.optBoolean("hardwareBackButton", true);
|
|
37
|
+
this.headers = OpenInWebViewOptions.getHeadersFromCall(call);
|
|
38
|
+
this.mediaPlaybackRequiresUserAction = call.getBoolean("mediaPlaybackRequiresUserAction", false);
|
|
39
|
+
this.pauseMediaWhenHidden = androidOptions == null || androidOptions.optBoolean("pauseMediaWhenHidden", true);
|
|
40
|
+
this.toolbar = new WebViewToolbarOptions(call.getObject("toolbar"));
|
|
41
|
+
this.uri = OpenInWebViewOptions.getUriFromCall(call);
|
|
42
|
+
this.userAgent = call.getString("userAgent");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public boolean getAllowZoom() {
|
|
46
|
+
return allowZoom;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
public boolean getHardwareBackButton() {
|
|
50
|
+
return hardwareBackButton;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@NonNull
|
|
54
|
+
public Map<String, String> getHeaders() {
|
|
55
|
+
return headers;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public boolean getMediaPlaybackRequiresUserAction() {
|
|
59
|
+
return mediaPlaybackRequiresUserAction;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public boolean getPauseMediaWhenHidden() {
|
|
63
|
+
return pauseMediaWhenHidden;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
@NonNull
|
|
67
|
+
public WebViewToolbarOptions getToolbar() {
|
|
68
|
+
return toolbar;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@NonNull
|
|
72
|
+
public Uri getUri() {
|
|
73
|
+
return uri;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@Nullable
|
|
77
|
+
public String getUserAgent() {
|
|
78
|
+
return userAgent;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
@NonNull
|
|
82
|
+
private static Map<String, String> getHeadersFromCall(@NonNull PluginCall call) {
|
|
83
|
+
Map<String, String> headers = new HashMap<>();
|
|
84
|
+
JSObject headersObject = call.getObject("headers");
|
|
85
|
+
if (headersObject == null) {
|
|
86
|
+
return headers;
|
|
87
|
+
}
|
|
88
|
+
Iterator<String> keys = headersObject.keys();
|
|
89
|
+
while (keys.hasNext()) {
|
|
90
|
+
String key = keys.next();
|
|
91
|
+
String value = headersObject.getString(key);
|
|
92
|
+
if (value != null) {
|
|
93
|
+
headers.put(key, value);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return headers;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@NonNull
|
|
100
|
+
private static Uri getUriFromCall(@NonNull PluginCall call) throws Exception {
|
|
101
|
+
String url = call.getString("url");
|
|
102
|
+
if (url == null || url.isEmpty()) {
|
|
103
|
+
throw CustomExceptions.URL_MISSING;
|
|
104
|
+
}
|
|
105
|
+
Uri uri = Uri.parse(url);
|
|
106
|
+
String scheme = uri.getScheme();
|
|
107
|
+
if (scheme == null || !(scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https"))) {
|
|
108
|
+
throw CustomExceptions.URL_INVALID;
|
|
109
|
+
}
|
|
110
|
+
return uri;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
package io.capawesome.capacitorjs.plugins.inappbrowser.classes.options;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
import com.getcapacitor.JSObject;
|
|
5
|
+
import com.getcapacitor.PluginCall;
|
|
6
|
+
import io.capawesome.capacitorjs.plugins.inappbrowser.classes.CustomExceptions;
|
|
7
|
+
|
|
8
|
+
public class PostMessageOptions {
|
|
9
|
+
|
|
10
|
+
@NonNull
|
|
11
|
+
private final JSObject data;
|
|
12
|
+
|
|
13
|
+
public PostMessageOptions(@NonNull PluginCall call) throws Exception {
|
|
14
|
+
this.data = PostMessageOptions.getDataFromCall(call);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@NonNull
|
|
18
|
+
public JSObject getData() {
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@NonNull
|
|
23
|
+
private static JSObject getDataFromCall(@NonNull PluginCall call) throws Exception {
|
|
24
|
+
JSObject data = call.getObject("data");
|
|
25
|
+
if (data == null) {
|
|
26
|
+
throw CustomExceptions.DATA_MISSING;
|
|
27
|
+
}
|
|
28
|
+
return data;
|
|
29
|
+
}
|
|
30
|
+
}
|