@capgo/inappbrowser 5.0.0 → 6.0.2

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.
@@ -1,5 +1,6 @@
1
1
  package ee.forgr.capacitor_inappbrowser;
2
2
 
3
+ import android.Manifest;
3
4
  import android.content.ComponentName;
4
5
  import android.content.Intent;
5
6
  import android.content.pm.PackageManager;
@@ -8,26 +9,71 @@ import android.net.Uri;
8
9
  import android.os.Bundle;
9
10
  import android.text.TextUtils;
10
11
  import android.util.Log;
11
- import android.webkit.WebStorage;
12
+ import android.webkit.CookieManager;
13
+ import android.webkit.PermissionRequest;
12
14
  import androidx.browser.customtabs.CustomTabsCallback;
13
15
  import androidx.browser.customtabs.CustomTabsClient;
14
16
  import androidx.browser.customtabs.CustomTabsIntent;
15
17
  import androidx.browser.customtabs.CustomTabsServiceConnection;
16
18
  import androidx.browser.customtabs.CustomTabsSession;
17
19
  import com.getcapacitor.JSObject;
20
+ import com.getcapacitor.PermissionState;
18
21
  import com.getcapacitor.Plugin;
19
22
  import com.getcapacitor.PluginCall;
20
23
  import com.getcapacitor.PluginMethod;
21
24
  import com.getcapacitor.annotation.CapacitorPlugin;
25
+ import com.getcapacitor.annotation.Permission;
26
+ import com.getcapacitor.annotation.PermissionCallback;
22
27
  import java.util.Iterator;
23
28
 
24
- @CapacitorPlugin(name = "InAppBrowser")
25
- public class InAppBrowserPlugin extends Plugin {
29
+ @CapacitorPlugin(
30
+ name = "InAppBrowser",
31
+ permissions = {
32
+ @Permission(alias = "camera", strings = { Manifest.permission.CAMERA }),
33
+ }
34
+ )
35
+ public class InAppBrowserPlugin
36
+ extends Plugin
37
+ implements WebViewDialog.PermissionHandler {
26
38
 
27
39
  public static final String CUSTOM_TAB_PACKAGE_NAME = "com.android.chrome"; // Change when in stable
28
40
  private CustomTabsClient customTabsClient;
29
41
  private CustomTabsSession currentSession;
30
42
  private WebViewDialog webViewDialog = null;
43
+ private String currentUrl = "";
44
+
45
+ private PermissionRequest currentPermissionRequest;
46
+
47
+ public void handleCameraPermissionRequest(PermissionRequest request) {
48
+ this.currentPermissionRequest = request;
49
+ if (getPermissionState("camera") != PermissionState.GRANTED) {
50
+ requestPermissionForAlias("camera", null, "cameraPermissionCallback");
51
+ } else {
52
+ grantCameraPermission();
53
+ }
54
+ }
55
+
56
+ @PermissionCallback
57
+ private void cameraPermissionCallback() {
58
+ if (getPermissionState("camera") == PermissionState.GRANTED) {
59
+ grantCameraPermission();
60
+ } else {
61
+ if (currentPermissionRequest != null) {
62
+ currentPermissionRequest.deny();
63
+ currentPermissionRequest = null;
64
+ }
65
+ // Handle the case where permission was not granted
66
+ }
67
+ }
68
+
69
+ private void grantCameraPermission() {
70
+ if (currentPermissionRequest != null) {
71
+ currentPermissionRequest.grant(
72
+ new String[] { PermissionRequest.RESOURCE_VIDEO_CAPTURE }
73
+ );
74
+ currentPermissionRequest = null;
75
+ }
76
+ }
31
77
 
32
78
  CustomTabsServiceConnection connection = new CustomTabsServiceConnection() {
33
79
  @Override
@@ -43,12 +89,40 @@ public class InAppBrowserPlugin extends Plugin {
43
89
  public void onServiceDisconnected(ComponentName name) {}
44
90
  };
45
91
 
92
+ @PluginMethod
93
+ public void requestCameraPermission(PluginCall call) {
94
+ if (getPermissionState("camera") != PermissionState.GRANTED) {
95
+ requestPermissionForAlias("camera", call, "cameraPermissionCallback");
96
+ } else {
97
+ call.resolve();
98
+ }
99
+ }
100
+
101
+ @PermissionCallback
102
+ private void cameraPermissionCallback(PluginCall call) {
103
+ if (getPermissionState("camera") == PermissionState.GRANTED) {
104
+ // Permission granted, notify the WebView to proceed
105
+ if (
106
+ webViewDialog != null && webViewDialog.currentPermissionRequest != null
107
+ ) {
108
+ webViewDialog.currentPermissionRequest.grant(
109
+ new String[] { PermissionRequest.RESOURCE_VIDEO_CAPTURE }
110
+ );
111
+ webViewDialog.currentPermissionRequest = null;
112
+ }
113
+ call.resolve();
114
+ } else {
115
+ call.reject("Camera permission is required");
116
+ }
117
+ }
118
+
46
119
  @PluginMethod
47
120
  public void setUrl(PluginCall call) {
48
121
  String url = call.getString("url");
49
122
  if (url == null || TextUtils.isEmpty(url)) {
50
123
  call.reject("Invalid URL");
51
124
  }
125
+ currentUrl = url;
52
126
  this.getActivity()
53
127
  .runOnUiThread(
54
128
  new Runnable() {
@@ -71,6 +145,7 @@ public class InAppBrowserPlugin extends Plugin {
71
145
  if (url == null || TextUtils.isEmpty(url)) {
72
146
  call.reject("Invalid URL");
73
147
  }
148
+ currentUrl = url;
74
149
  CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(
75
150
  getCustomTabsSession()
76
151
  );
@@ -112,8 +187,51 @@ public class InAppBrowserPlugin extends Plugin {
112
187
 
113
188
  @PluginMethod
114
189
  public void clearCookies(PluginCall call) {
115
- WebStorage.getInstance().deleteAllData();
116
- call.resolve();
190
+ String url = call.getString("url");
191
+ Boolean clearCache = call.getBoolean("cache", false);
192
+ if (url == null || TextUtils.isEmpty(url)) {
193
+ call.reject("Invalid URL");
194
+ } else {
195
+ CookieManager cookieManager = CookieManager.getInstance();
196
+ String cookie = cookieManager.getCookie(url);
197
+ if (cookie != null) {
198
+ String[] cookies = cookie.split(";");
199
+ for (String c : cookies) {
200
+ String cookieName = c.substring(0, c.indexOf("="));
201
+ cookieManager.setCookie(
202
+ url,
203
+ cookieName + "=; Expires=Thu, 01 Jan 1970 00:00:01 GMT"
204
+ );
205
+ if (clearCache) {
206
+ cookieManager.removeSessionCookie();
207
+ }
208
+ }
209
+ }
210
+ call.resolve();
211
+ }
212
+ }
213
+
214
+ @PluginMethod
215
+ public void getCookies(PluginCall call) {
216
+ String url = call.getString("url");
217
+ if (url == null || TextUtils.isEmpty(url)) {
218
+ call.reject("Invalid URL");
219
+ } else {
220
+ CookieManager cookieManager = CookieManager.getInstance();
221
+ String cookieString = cookieManager.getCookie(url);
222
+ if (cookieString != null) {
223
+ String[] cookiePairs = cookieString.split("; ");
224
+ JSObject result = new JSObject();
225
+ for (String cookie : cookiePairs) {
226
+ String[] parts = cookie.split("=", 2);
227
+ if (parts.length == 2) {
228
+ result.put(parts[0], parts[1]);
229
+ }
230
+ }
231
+ call.resolve(result);
232
+ }
233
+ call.resolve(new JSObject());
234
+ }
117
235
  }
118
236
 
119
237
  @PluginMethod
@@ -122,17 +240,44 @@ public class InAppBrowserPlugin extends Plugin {
122
240
  if (url == null || TextUtils.isEmpty(url)) {
123
241
  call.reject("Invalid URL");
124
242
  }
243
+ currentUrl = url;
125
244
  final Options options = new Options();
126
245
  options.setUrl(url);
127
246
  options.setHeaders(call.getObject("headers"));
128
- options.setTitle(call.getString("title", "New Window"));
247
+ options.setShowReloadButton(call.getBoolean("showReloadButton", false));
248
+ if (Boolean.TRUE.equals(call.getBoolean("visibleTitle", true))) {
249
+ options.setTitle(call.getString("title", "New Window"));
250
+ } else {
251
+ options.setTitle(call.getString("title", ""));
252
+ }
253
+ options.setToolbarColor(call.getString("toolbarColor", "#ffffff"));
254
+ options.setArrow(Boolean.TRUE.equals(call.getBoolean("showArrow", false)));
255
+
129
256
  options.setShareDisclaimer(call.getObject("shareDisclaimer", null));
130
257
  options.setShareSubject(call.getString("shareSubject", null));
131
258
  options.setToolbarType(call.getString("toolbarType", ""));
259
+ options.setActiveNativeNavigationForWebview(
260
+ call.getBoolean("activeNativeNavigationForWebview", false)
261
+ );
262
+ options.setDisableGoBackOnNativeApplication(
263
+ call.getBoolean("disableGoBackOnNativeApplication", false)
264
+ );
132
265
  options.setPresentAfterPageLoad(
133
266
  call.getBoolean("isPresentAfterPageLoad", false)
134
267
  );
268
+ if (call.getBoolean("closeModal", false)) {
269
+ options.setCloseModal(true);
270
+ options.setCloseModalTitle(call.getString("closeModalTitle", "Close"));
271
+ options.setCloseModalDescription(
272
+ call.getString("closeModalDescription", "Are you sure ?")
273
+ );
274
+ options.setCloseModalOk(call.getString("closeModalOk", "Ok"));
275
+ options.setCloseModalCancel(call.getString("closeModalCancel", "Cancel"));
276
+ } else {
277
+ options.setCloseModal(false);
278
+ }
135
279
  options.setPluginCall(call);
280
+ // options.getToolbarItemTypes().add(ToolbarItemType.RELOAD); TODO: fix this
136
281
  options.setCallbacks(
137
282
  new WebViewCallbacks() {
138
283
  @Override
@@ -156,39 +301,91 @@ public class InAppBrowserPlugin extends Plugin {
156
301
  }
157
302
  }
158
303
  );
159
- getActivity()
304
+ this.getActivity()
160
305
  .runOnUiThread(
161
306
  new Runnable() {
162
307
  @Override
163
308
  public void run() {
164
- webViewDialog =
165
- new WebViewDialog(
166
- getContext(),
167
- android.R.style.Theme_NoTitleBar,
168
- options
169
- );
309
+ webViewDialog = new WebViewDialog(
310
+ getContext(),
311
+ android.R.style.Theme_NoTitleBar,
312
+ options,
313
+ InAppBrowserPlugin.this
314
+ );
170
315
  webViewDialog.presentWebView();
316
+ webViewDialog.activity = InAppBrowserPlugin.this.getActivity();
171
317
  }
172
318
  }
173
319
  );
174
320
  }
175
321
 
176
322
  @PluginMethod
177
- public void close(PluginCall call) {
323
+ public void executeScript(PluginCall call) {
324
+ String script = call.getString("code");
325
+ if (script == null || TextUtils.isEmpty(script)) {
326
+ call.reject("No script to run");
327
+ }
328
+
178
329
  if (webViewDialog != null) {
179
- webViewDialog.dismiss();
180
- webViewDialog = null;
181
- } else {
182
- Intent intent = new Intent(
183
- getContext(),
184
- getBridge().getActivity().getClass()
185
- );
186
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
187
- getContext().startActivity(intent);
330
+ this.getActivity()
331
+ .runOnUiThread(
332
+ new Runnable() {
333
+ @Override
334
+ public void run() {
335
+ webViewDialog.executeScript(script);
336
+ }
337
+ }
338
+ );
188
339
  }
340
+
189
341
  call.resolve();
190
342
  }
191
343
 
344
+ @PluginMethod
345
+ public void reload(PluginCall call) {
346
+ if (webViewDialog != null) {
347
+ this.getActivity()
348
+ .runOnUiThread(
349
+ new Runnable() {
350
+ @Override
351
+ public void run() {
352
+ webViewDialog.reload();
353
+ }
354
+ }
355
+ );
356
+ }
357
+ call.resolve();
358
+ }
359
+
360
+ @PluginMethod
361
+ public void close(PluginCall call) {
362
+ this.getActivity()
363
+ .runOnUiThread(
364
+ new Runnable() {
365
+ @Override
366
+ public void run() {
367
+ if (webViewDialog != null) {
368
+ notifyListeners(
369
+ "closeEvent",
370
+ new JSObject().put("url", webViewDialog.getUrl())
371
+ );
372
+ webViewDialog.dismiss();
373
+ webViewDialog.destroy();
374
+ webViewDialog = null;
375
+ } else {
376
+ Intent intent = new Intent(
377
+ getContext(),
378
+ getBridge().getActivity().getClass()
379
+ );
380
+ intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
381
+ getContext().startActivity(intent);
382
+ }
383
+ call.resolve();
384
+ }
385
+ }
386
+ );
387
+ }
388
+
192
389
  private Bundle getHeaders(PluginCall pluginCall) {
193
390
  JSObject headersProvided = pluginCall.getObject("headers");
194
391
  Bundle headers = new Bundle();
@@ -223,19 +420,18 @@ public class InAppBrowserPlugin extends Plugin {
223
420
  }
224
421
 
225
422
  if (currentSession == null) {
226
- currentSession =
227
- customTabsClient.newSession(
228
- new CustomTabsCallback() {
229
- @Override
230
- public void onNavigationEvent(int navigationEvent, Bundle extras) {
231
- switch (navigationEvent) {
232
- case NAVIGATION_FINISHED:
233
- notifyListeners("browserPageLoaded", new JSObject());
234
- break;
235
- }
423
+ currentSession = customTabsClient.newSession(
424
+ new CustomTabsCallback() {
425
+ @Override
426
+ public void onNavigationEvent(int navigationEvent, Bundle extras) {
427
+ switch (navigationEvent) {
428
+ case NAVIGATION_FINISHED:
429
+ notifyListeners("browserPageLoaded", new JSObject());
430
+ break;
236
431
  }
237
432
  }
238
- );
433
+ }
434
+ );
239
435
  }
240
436
  return currentSession;
241
437
  }
@@ -6,14 +6,24 @@ import com.getcapacitor.PluginCall;
6
6
  public class Options {
7
7
 
8
8
  private String title;
9
+ private boolean CloseModal;
10
+ private String CloseModalTitle;
11
+ private String CloseModalDescription;
12
+ private String CloseModalCancel;
13
+ private String CloseModalOk;
9
14
  private String url;
10
15
  private JSObject headers;
11
16
  private String toolbarType;
12
17
  private JSObject shareDisclaimer;
13
18
  private String shareSubject;
19
+ private boolean disableGoBackOnNativeApplication;
20
+ private boolean activeNativeNavigationForWebview;
14
21
  private boolean isPresentAfterPageLoad;
15
22
  private WebViewCallbacks callbacks;
16
23
  private PluginCall pluginCall;
24
+ private boolean VisibleTitle;
25
+ private String ToolbarColor;
26
+ private boolean ShowArrow;
17
27
 
18
28
  public PluginCall getPluginCall() {
19
29
  return pluginCall;
@@ -31,6 +41,46 @@ public class Options {
31
41
  this.title = title;
32
42
  }
33
43
 
44
+ public boolean getCloseModal() {
45
+ return CloseModal;
46
+ }
47
+
48
+ public void setCloseModal(boolean CloseModal) {
49
+ this.CloseModal = CloseModal;
50
+ }
51
+
52
+ public String getCloseModalTitle() {
53
+ return CloseModalTitle;
54
+ }
55
+
56
+ public void setCloseModalTitle(String CloseModalTitle) {
57
+ this.CloseModalTitle = CloseModalTitle;
58
+ }
59
+
60
+ public String getCloseModalDescription() {
61
+ return CloseModalDescription;
62
+ }
63
+
64
+ public void setCloseModalDescription(String CloseModalDescription) {
65
+ this.CloseModalDescription = CloseModalDescription;
66
+ }
67
+
68
+ public String getCloseModalCancel() {
69
+ return CloseModalCancel;
70
+ }
71
+
72
+ public void setCloseModalCancel(String CloseModalCancel) {
73
+ this.CloseModalCancel = CloseModalCancel;
74
+ }
75
+
76
+ public String getCloseModalOk() {
77
+ return CloseModalOk;
78
+ }
79
+
80
+ public void setCloseModalOk(String CloseModalOk) {
81
+ this.CloseModalOk = CloseModalOk;
82
+ }
83
+
34
84
  public String getUrl() {
35
85
  return url;
36
86
  }
@@ -59,6 +109,16 @@ public class Options {
59
109
  return shareDisclaimer;
60
110
  }
61
111
 
112
+ public boolean showReloadButton;
113
+
114
+ public boolean getShowReloadButton() {
115
+ return showReloadButton;
116
+ }
117
+
118
+ public void setShowReloadButton(boolean showReloadButton) {
119
+ this.showReloadButton = showReloadButton;
120
+ }
121
+
62
122
  public void setShareDisclaimer(JSObject shareDisclaimer) {
63
123
  this.shareDisclaimer = shareDisclaimer;
64
124
  }
@@ -71,6 +131,26 @@ public class Options {
71
131
  this.shareSubject = shareSubject;
72
132
  }
73
133
 
134
+ public boolean getActiveNativeNavigationForWebview() {
135
+ return activeNativeNavigationForWebview;
136
+ }
137
+
138
+ public void setActiveNativeNavigationForWebview(
139
+ boolean activeNativeNavigationForWebview
140
+ ) {
141
+ this.activeNativeNavigationForWebview = activeNativeNavigationForWebview;
142
+ }
143
+
144
+ public boolean getDisableGoBackOnNativeApplication() {
145
+ return disableGoBackOnNativeApplication;
146
+ }
147
+
148
+ public void setDisableGoBackOnNativeApplication(
149
+ boolean disableGoBackOnNativeApplication
150
+ ) {
151
+ this.disableGoBackOnNativeApplication = disableGoBackOnNativeApplication;
152
+ }
153
+
74
154
  public boolean isPresentAfterPageLoad() {
75
155
  return isPresentAfterPageLoad;
76
156
  }
@@ -86,4 +166,28 @@ public class Options {
86
166
  public void setCallbacks(WebViewCallbacks callbacks) {
87
167
  this.callbacks = callbacks;
88
168
  }
169
+
170
+ public boolean getVisibleTitle() {
171
+ return VisibleTitle;
172
+ }
173
+
174
+ public void setVisibleTitle(boolean _visibleTitle) {
175
+ this.VisibleTitle = _visibleTitle;
176
+ }
177
+
178
+ public String getToolbarColor() {
179
+ return ToolbarColor;
180
+ }
181
+
182
+ public void setToolbarColor(String toolbarColor) {
183
+ this.ToolbarColor = toolbarColor;
184
+ }
185
+
186
+ public boolean showArrow() {
187
+ return ShowArrow;
188
+ }
189
+
190
+ public void setArrow(boolean _showArrow) {
191
+ this.ShowArrow = _showArrow;
192
+ }
89
193
  }