@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,18 +1,29 @@
1
1
  package ee.forgr.capacitor_inappbrowser;
2
2
 
3
+ import android.app.Activity;
4
+ import android.app.AlertDialog;
3
5
  import android.app.Dialog;
6
+ import android.content.ActivityNotFoundException;
4
7
  import android.content.Context;
8
+ import android.content.DialogInterface;
9
+ import android.content.Intent;
5
10
  import android.graphics.Bitmap;
11
+ import android.graphics.Color;
12
+ import android.net.Uri;
6
13
  import android.text.TextUtils;
14
+ import android.util.Log;
7
15
  import android.view.View;
8
16
  import android.view.Window;
9
17
  import android.view.WindowManager;
18
+ import android.webkit.PermissionRequest;
19
+ import android.webkit.WebChromeClient;
10
20
  import android.webkit.WebResourceError;
11
21
  import android.webkit.WebResourceRequest;
12
22
  import android.webkit.WebView;
13
23
  import android.webkit.WebViewClient;
14
24
  import android.widget.ImageButton;
15
25
  import android.widget.TextView;
26
+ import android.widget.Toast;
16
27
  import android.widget.Toolbar;
17
28
  import java.net.URI;
18
29
  import java.net.URISyntaxException;
@@ -25,11 +36,28 @@ public class WebViewDialog extends Dialog {
25
36
  private WebView _webView;
26
37
  private Toolbar _toolbar;
27
38
  private Options _options;
39
+ private Context _context;
40
+ public Activity activity;
28
41
  private boolean isInitialized = false;
29
42
 
30
- public WebViewDialog(Context context, int theme, Options options) {
43
+ public PermissionRequest currentPermissionRequest;
44
+
45
+ public interface PermissionHandler {
46
+ void handleCameraPermissionRequest(PermissionRequest request);
47
+ }
48
+
49
+ private PermissionHandler permissionHandler;
50
+
51
+ public WebViewDialog(
52
+ Context context,
53
+ int theme,
54
+ Options options,
55
+ PermissionHandler permissionHandler
56
+ ) {
31
57
  super(context, theme);
32
58
  this._options = options;
59
+ this._context = context;
60
+ this.permissionHandler = permissionHandler;
33
61
  this.isInitialized = false;
34
62
  }
35
63
 
@@ -54,18 +82,66 @@ public class WebViewDialog extends Dialog {
54
82
  _webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
55
83
  _webView.getSettings().setDatabaseEnabled(true);
56
84
  _webView.getSettings().setDomStorageEnabled(true);
85
+ _webView.getSettings().setAllowFileAccess(true);
57
86
  _webView
58
87
  .getSettings()
59
88
  .setPluginState(android.webkit.WebSettings.PluginState.ON);
60
89
  _webView.getSettings().setLoadWithOverviewMode(true);
61
90
  _webView.getSettings().setUseWideViewPort(true);
91
+ _webView.getSettings().setAllowFileAccessFromFileURLs(true);
92
+ _webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
93
+
94
+ _webView.setWebViewClient(new WebViewClient());
95
+
96
+ _webView.setWebChromeClient(
97
+ new WebChromeClient() {
98
+ // Grant permissions for cam
99
+
100
+ @Override
101
+ public void onPermissionRequest(final PermissionRequest request) {
102
+ Log.i(
103
+ "INAPPBROWSER",
104
+ "onPermissionRequest " + request.getResources().toString()
105
+ );
106
+ final String[] requestedResources = request.getResources();
107
+ for (String r : requestedResources) {
108
+ Log.i("INAPPBROWSER", "requestedResources " + r);
109
+ if (r.equals(PermissionRequest.RESOURCE_VIDEO_CAPTURE)) {
110
+ Log.i("INAPPBROWSER", "RESOURCE_VIDEO_CAPTURE req");
111
+ // Store the permission request
112
+ currentPermissionRequest = request;
113
+ // Initiate the permission request through the plugin
114
+ if (permissionHandler != null) {
115
+ permissionHandler.handleCameraPermissionRequest(request);
116
+ }
117
+ break;
118
+ }
119
+ }
120
+ }
121
+
122
+ @Override
123
+ public void onPermissionRequestCanceled(PermissionRequest request) {
124
+ super.onPermissionRequestCanceled(request);
125
+ Toast.makeText(
126
+ WebViewDialog.this.activity,
127
+ "Permission Denied",
128
+ Toast.LENGTH_SHORT
129
+ ).show();
130
+ // Handle the denied permission
131
+ if (currentPermissionRequest != null) {
132
+ currentPermissionRequest.deny();
133
+ currentPermissionRequest = null;
134
+ }
135
+ }
136
+ }
137
+ );
62
138
 
63
139
  Map<String, String> requestHeaders = new HashMap<>();
64
140
  if (_options.getHeaders() != null) {
65
141
  Iterator<String> keys = _options.getHeaders().keys();
66
142
  while (keys.hasNext()) {
67
143
  String key = keys.next();
68
- if (TextUtils.equals(key, "User-Agent")) {
144
+ if (TextUtils.equals(key.toLowerCase(), "user-agent")) {
69
145
  _webView
70
146
  .getSettings()
71
147
  .setUserAgentString(_options.getHeaders().getString(key));
@@ -88,13 +164,29 @@ public class WebViewDialog extends Dialog {
88
164
  }
89
165
  }
90
166
 
167
+ public void reload() {
168
+ _webView.reload();
169
+ }
170
+
171
+ public void destroy() {
172
+ _webView.destroy();
173
+ }
174
+
175
+ public String getUrl() {
176
+ return _webView.getUrl();
177
+ }
178
+
179
+ public void executeScript(String script) {
180
+ _webView.evaluateJavascript(script, null);
181
+ }
182
+
91
183
  public void setUrl(String url) {
92
184
  Map<String, String> requestHeaders = new HashMap<>();
93
185
  if (_options.getHeaders() != null) {
94
186
  Iterator<String> keys = _options.getHeaders().keys();
95
187
  while (keys.hasNext()) {
96
188
  String key = keys.next();
97
- if (TextUtils.equals(key, "User-Agent")) {
189
+ if (TextUtils.equals(key.toLowerCase(), "user-agent")) {
98
190
  _webView
99
191
  .getSettings()
100
192
  .setUserAgentString(_options.getHeaders().getString(key));
@@ -108,11 +200,27 @@ public class WebViewDialog extends Dialog {
108
200
 
109
201
  private void setTitle(String newTitleText) {
110
202
  TextView textView = (TextView) _toolbar.findViewById(R.id.titleText);
111
- textView.setText(newTitleText);
203
+ if (_options.getVisibleTitle()) {
204
+ textView.setText(newTitleText);
205
+ } else {
206
+ textView.setText("");
207
+ }
112
208
  }
113
209
 
114
210
  private void setupToolbar() {
115
211
  _toolbar = this.findViewById(R.id.tool_bar);
212
+ int color = Color.parseColor("#ffffff");
213
+ try {
214
+ color = Color.parseColor(_options.getToolbarColor());
215
+ } catch (IllegalArgumentException e) {
216
+ // Do nothing
217
+ }
218
+ _toolbar.setBackgroundColor(color);
219
+ _toolbar.findViewById(R.id.backButton).setBackgroundColor(color);
220
+ _toolbar.findViewById(R.id.forwardButton).setBackgroundColor(color);
221
+ _toolbar.findViewById(R.id.closeButton).setBackgroundColor(color);
222
+ _toolbar.findViewById(R.id.reloadButton).setBackgroundColor(color);
223
+
116
224
  if (!TextUtils.isEmpty(_options.getTitle())) {
117
225
  this.setTitle(_options.getTitle());
118
226
  } else {
@@ -153,12 +261,50 @@ public class WebViewDialog extends Dialog {
153
261
  new View.OnClickListener() {
154
262
  @Override
155
263
  public void onClick(View view) {
156
- dismiss();
157
- _options.getCallbacks().closeEvent(_webView.getUrl());
264
+ // if closeModal true then display a native modal to check if the user is sure to close the browser
265
+ if (_options.getCloseModal()) {
266
+ new AlertDialog.Builder(_context)
267
+ .setTitle(_options.getCloseModalTitle())
268
+ .setMessage(_options.getCloseModalDescription())
269
+ .setPositiveButton(
270
+ _options.getCloseModalOk(),
271
+ new DialogInterface.OnClickListener() {
272
+ public void onClick(DialogInterface dialog, int which) {
273
+ // Close button clicked, do something
274
+ dismiss();
275
+ _options.getCallbacks().closeEvent(_webView.getUrl());
276
+ _webView.destroy();
277
+ }
278
+ }
279
+ )
280
+ .setNegativeButton(_options.getCloseModalCancel(), null)
281
+ .show();
282
+ } else {
283
+ dismiss();
284
+ _options.getCallbacks().closeEvent(_webView.getUrl());
285
+ _webView.destroy();
286
+ }
158
287
  }
159
288
  }
160
289
  );
161
290
 
291
+ if (_options.showArrow()) {
292
+ closeButton.setBackgroundResource(R.drawable.arrow_forward_enabled);
293
+ }
294
+
295
+ if (_options.getShowReloadButton()) {
296
+ View reloadButton = _toolbar.findViewById(R.id.reloadButton);
297
+ reloadButton.setVisibility(View.VISIBLE);
298
+ reloadButton.setOnClickListener(
299
+ new View.OnClickListener() {
300
+ @Override
301
+ public void onClick(View view) {
302
+ _webView.reload();
303
+ }
304
+ }
305
+ );
306
+ }
307
+
162
308
  if (TextUtils.equals(_options.getToolbarType(), "activity")) {
163
309
  _toolbar.findViewById(R.id.forwardButton).setVisibility(View.GONE);
164
310
  _toolbar.findViewById(R.id.backButton).setVisibility(View.GONE);
@@ -181,6 +327,19 @@ public class WebViewDialog extends Dialog {
181
327
  WebView view,
182
328
  WebResourceRequest request
183
329
  ) {
330
+ Context context = view.getContext();
331
+ String url = request.getUrl().toString();
332
+
333
+ if (!url.startsWith("https://") && !url.startsWith("http://")) {
334
+ try {
335
+ Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
336
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
337
+ context.startActivity(intent);
338
+ return true;
339
+ } catch (ActivityNotFoundException e) {
340
+ // Do nothing
341
+ }
342
+ }
184
343
  return false;
185
344
  }
186
345
 
@@ -198,7 +357,17 @@ public class WebViewDialog extends Dialog {
198
357
  } catch (URISyntaxException e) {
199
358
  // Do nothing
200
359
  }
201
- _options.getCallbacks().urlChangeEvent(url);
360
+ }
361
+
362
+ public void doUpdateVisitedHistory(
363
+ WebView view,
364
+ String url,
365
+ boolean isReload
366
+ ) {
367
+ if (!isReload) {
368
+ _options.getCallbacks().urlChangeEvent(url);
369
+ }
370
+ super.doUpdateVisitedHistory(view, url, isReload);
202
371
  }
203
372
 
204
373
  @Override
@@ -252,10 +421,11 @@ public class WebViewDialog extends Dialog {
252
421
  public void onBackPressed() {
253
422
  if (
254
423
  _webView.canGoBack() &&
255
- TextUtils.equals(_options.getToolbarType(), "navigation")
424
+ (TextUtils.equals(_options.getToolbarType(), "navigation") ||
425
+ _options.getActiveNativeNavigationForWebview())
256
426
  ) {
257
427
  _webView.goBack();
258
- } else {
428
+ } else if (!_options.getDisableGoBackOnNativeApplication()) {
259
429
  super.onBackPressed();
260
430
  }
261
431
  }
@@ -0,0 +1,9 @@
1
+ <vector xmlns:android="http://schemas.android.com/apk/res/android"
2
+ android:width="24dp"
3
+ android:height="24dp"
4
+ android:viewportWidth="24.0"
5
+ android:viewportHeight="24.0">
6
+ <path
7
+ android:fillColor="@color/enable"
8
+ android:pathData="M17.65,6.35C16.2,4.9 14.21,4 12,4c-4.42,0 -7.99,3.58 -7.99,8s3.57,8 7.99,8c3.73,0 6.84,-2.55 7.73,-6h-2.08c-0.82,2.33 -3.04,4 -5.65,4 -3.31,0 -6,-2.69 -6,-6s2.69,-6 6,-6c1.66,0 3.14,0.69 4.22,1.78L13,11h7V4l-2.35,2.35z"/>
9
+ </vector>
@@ -1,5 +1,5 @@
1
1
  <?xml version="1.0" encoding="utf-8"?>
2
- <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
2
+ <android.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
3
3
  xmlns:app="http://schemas.android.com/apk/res-auto"
4
4
  android:layout_width="match_parent"
5
5
  android:layout_height="wrap_content"
@@ -15,6 +15,16 @@
15
15
  android:layout_gravity="start"
16
16
  android:background="#eeeeef"
17
17
  android:contentDescription="@string/close_button" />
18
+ <ImageButton
19
+ android:id="@+id/reloadButton"
20
+ android:layout_width="wrap_content"
21
+ android:layout_height="wrap_content"
22
+ android:layout_gravity="end"
23
+ android:background="#eeeeef"
24
+ android:paddingRight="10dp"
25
+ android:contentDescription="@string/reload_button"
26
+ android:src="@drawable/ic_refresh"
27
+ android:visibility="gone" />
18
28
 
19
29
  <TextView
20
30
  android:id="@+id/titleText"
@@ -34,7 +44,6 @@
34
44
  android:layout_gravity="end"
35
45
  android:background="#eeeeef"
36
46
  android:contentDescription="@string/forward_button"
37
- android:paddingLeft="10dp"
38
47
  android:paddingRight="10dp"
39
48
  android:src="@drawable/arrow_forward_disabled" />
40
49
 
@@ -47,4 +56,4 @@
47
56
  android:contentDescription="@string/back_button"
48
57
  android:paddingRight="10dp"
49
58
  android:src="@drawable/arrow_back_disabled" />
50
- </androidx.appcompat.widget.Toolbar>
59
+ </android.widget.Toolbar>
@@ -3,6 +3,7 @@
3
3
  <string name="title_activity_browser">BrowserActivity</string>
4
4
  <string name="title_activity_basic">BasicActivity</string>
5
5
  <string name="close_button">Close Button</string>
6
+ <string name="reload_button">Reload Button</string>
6
7
  <string name="back_button">Back Button</string>
7
8
  <string name="title">Title</string>
8
9
  <string name="forward_button">Forward Button</string>