@capgo/inappbrowser 0.0.4 → 0.1.0

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.
@@ -55,4 +55,10 @@ dependencies {
55
55
  testImplementation "junit:junit:$junitVersion"
56
56
  androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
57
57
  androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
58
+ implementation 'com.android.support:customtabs:28.0.0'
59
+ implementation 'com.android.support.constraint:constraint-layout:1.1.3'
60
+ implementation 'com.android.support:design:28.0.0'
61
+ implementation 'com.android.support:appcompat-v7:28.0.0'
62
+ implementation 'androidx.coordinatorlayout:coordinatorlayout:1.1.0'
63
+ implementation 'com.google.android.material:material:1.0.0'
58
64
  }
@@ -4,24 +4,25 @@ import android.content.ComponentName;
4
4
  import android.content.Intent;
5
5
  import android.net.Uri;
6
6
  import android.os.Bundle;
7
- import android.support.customtabs.CustomTabsCallback;
8
- import android.support.customtabs.CustomTabsClient;
9
- import android.support.customtabs.CustomTabsIntent;
10
- import android.support.customtabs.CustomTabsServiceConnection;
11
- import android.support.customtabs.CustomTabsSession;
12
7
  import android.text.TextUtils;
13
8
  import android.util.Log;
14
9
 
10
+ import androidx.browser.customtabs.CustomTabsCallback;
11
+ import androidx.browser.customtabs.CustomTabsClient;
12
+ import androidx.browser.customtabs.CustomTabsIntent;
13
+ import androidx.browser.customtabs.CustomTabsServiceConnection;
14
+ import androidx.browser.customtabs.CustomTabsSession;
15
+
15
16
  import com.getcapacitor.JSObject;
16
- import com.getcapacitor.NativePlugin;
17
17
  import com.getcapacitor.Plugin;
18
18
  import com.getcapacitor.PluginCall;
19
19
  import com.getcapacitor.PluginMethod;
20
+ import com.getcapacitor.annotation.CapacitorPlugin;
20
21
 
21
22
  import java.util.Iterator;
22
23
 
23
- @NativePlugin()
24
- public class CapBrowser extends Plugin {
24
+ @CapacitorPlugin(name = "InAppBrowser")
25
+ public class InAppBrowserPlugin extends Plugin {
25
26
  public static final String CUSTOM_TAB_PACKAGE_NAME = "com.android.chrome"; // Change when in stable
26
27
  private CustomTabsClient customTabsClient;
27
28
  private CustomTabsSession currentSession;
@@ -41,31 +42,40 @@ public class CapBrowser extends Plugin {
41
42
 
42
43
  @PluginMethod()
43
44
  public void setUrl(PluginCall call) {
44
- call.success();
45
+ String url = call.getString("url");
46
+ if(url == null || TextUtils.isEmpty(url)) {
47
+ call.reject("Invalid URL");
48
+ }
49
+ getActivity().runOnUiThread(new Runnable() {
50
+ @Override
51
+ public void run() {
52
+ webViewDialog.setUrl(url);
53
+ }
54
+ });
55
+ call.resolve();
45
56
  }
46
57
 
47
58
  @PluginMethod()
48
59
  public void open(PluginCall call) {
49
60
  String url = call.getString("url");
50
61
  if(url == null || TextUtils.isEmpty(url)) {
51
- call.error("Invalid URL");
62
+ call.reject("Invalid URL");
52
63
  }
53
64
  CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(getCustomTabsSession());
54
- builder.addDefaultShareMenuItem();
55
65
  CustomTabsIntent tabsIntent = builder.build();
56
66
  tabsIntent.intent.putExtra(Intent.EXTRA_REFERRER,
57
67
  Uri.parse(Intent.URI_ANDROID_APP_SCHEME + "//" + getContext().getPackageName()));
58
68
  tabsIntent.intent.putExtra(android.provider.Browser.EXTRA_HEADERS, this.getHeaders(call));
59
69
  tabsIntent.launchUrl(getContext(), Uri.parse(url));
60
70
 
61
- call.success();
71
+ call.resolve();
62
72
  }
63
73
 
64
74
  @PluginMethod()
65
75
  public void openWebView(PluginCall call) {
66
76
  String url = call.getString("url");
67
77
  if(url == null || TextUtils.isEmpty(url)) {
68
- call.error("Invalid URL");
78
+ call.reject("Invalid URL");
69
79
  }
70
80
  final Options options = new Options();
71
81
  options.setUrl(url);
@@ -111,7 +121,7 @@ public class CapBrowser extends Plugin {
111
121
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
112
122
  getContext().startActivity(intent);
113
123
  }
114
- call.success();
124
+ call.resolve();
115
125
  }
116
126
 
117
127
  private Bundle getHeaders(PluginCall pluginCall) {
@@ -157,9 +167,4 @@ public class CapBrowser extends Plugin {
157
167
  }
158
168
  return currentSession;
159
169
  }
160
-
161
- @Override
162
- protected void handleOnActivityResult(int requestCode, int resultCode, Intent data) {
163
- super.handleOnActivityResult(requestCode, resultCode, data);
164
- }
165
170
  }
@@ -3,7 +3,6 @@ package ee.forgr.capacitor_inappbrowser;
3
3
  import android.app.Dialog;
4
4
  import android.content.Context;
5
5
  import android.graphics.Bitmap;
6
- import android.support.v7.widget.Toolbar;
7
6
  import android.text.TextUtils;
8
7
  import android.view.View;
9
8
  import android.view.Window;
@@ -14,8 +13,7 @@ import android.webkit.WebView;
14
13
  import android.webkit.WebViewClient;
15
14
  import android.widget.ImageButton;
16
15
  import android.widget.TextView;
17
-
18
- import com.cap.browser.plugin.capbrowser.R;
16
+ import androidx.appcompat.widget.Toolbar;
19
17
 
20
18
  import java.net.URI;
21
19
  import java.net.URISyntaxException;
@@ -78,6 +76,22 @@ public class WebViewDialog extends Dialog {
78
76
  }
79
77
  }
80
78
 
79
+ public void setUrl(String url) {
80
+ Map<String, String> requestHeaders = new HashMap<>();
81
+ if(_options.getHeaders() != null) {
82
+ Iterator<String> keys = _options.getHeaders().keys();
83
+ while(keys.hasNext()) {
84
+ String key = keys.next();
85
+ if(TextUtils.equals(key, "User-Agent")) {
86
+ _webView.getSettings().setUserAgentString(_options.getHeaders().getString(key));
87
+ } else {
88
+ requestHeaders.put(key, _options.getHeaders().getString(key));
89
+ }
90
+ }
91
+ }
92
+ _webView.loadUrl(url, requestHeaders);
93
+ }
94
+
81
95
  private void setTitle(String newTitleText) {
82
96
  TextView textView = (TextView) _toolbar.findViewById(R.id.titleText);
83
97
  textView.setText(newTitleText);
@@ -1,12 +1,12 @@
1
1
  <?xml version="1.0" encoding="utf-8"?>
2
- <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
2
+ <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
3
  xmlns:app="http://schemas.android.com/apk/res-auto"
4
4
  xmlns:tools="http://schemas.android.com/tools"
5
5
  android:layout_width="match_parent"
6
6
  android:layout_height="match_parent"
7
7
  tools:context="com.cap.browser.plugin.WebViewActivity">
8
8
 
9
- <android.support.design.widget.AppBarLayout
9
+ <com.google.android.material.appbar.AppBarLayout
10
10
  android:layout_width="match_parent"
11
11
  android:layout_height="wrap_content"
12
12
  android:theme="@style/AppTheme.AppBarOverlay">
@@ -15,8 +15,8 @@
15
15
  android:id="@+id/tool_bar"
16
16
  layout="@layout/tool_bar" />
17
17
 
18
- </android.support.design.widget.AppBarLayout>
18
+ </com.google.android.material.appbar.AppBarLayout>
19
19
 
20
20
  <include layout="@layout/content_browser" />
21
21
 
22
- </android.support.design.widget.CoordinatorLayout>
22
+ </androidx.coordinatorlayout.widget.CoordinatorLayout>
@@ -1,5 +1,5 @@
1
1
  <?xml version="1.0" encoding="utf-8"?>
2
- <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
2
+ <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
3
  xmlns:app="http://schemas.android.com/apk/res-auto"
4
4
  xmlns:tools="http://schemas.android.com/tools"
5
5
  android:layout_width="match_parent"
@@ -12,4 +12,4 @@
12
12
  android:layout_width="fill_parent"
13
13
  android:layout_height="fill_parent" />
14
14
 
15
- </android.support.design.widget.CoordinatorLayout>
15
+ </androidx.coordinatorlayout.widget.CoordinatorLayout>
@@ -1,5 +1,5 @@
1
1
  <?xml version="1.0" encoding="utf-8"?>
2
- <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
2
+ <androidx.appcompat.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"
@@ -47,4 +47,4 @@
47
47
  android:contentDescription="@string/back_button"
48
48
  android:paddingRight="10dp"
49
49
  android:src="@drawable/arrow_back_disabled" />
50
- </android.support.v7.widget.Toolbar>
50
+ </androidx.appcompat.widget.Toolbar>
@@ -12,6 +12,7 @@ public class InAppBrowserPlugin: CAPPlugin {
12
12
  private var isSetupDone = false
13
13
  var currentPluginCall: CAPPluginCall?
14
14
  var isPresentAfterPageLoad = false
15
+ var webViewController: WKWebViewController?
15
16
 
16
17
  private func setup(){
17
18
  self.isSetupDone = true
@@ -59,25 +60,24 @@ public class InAppBrowserPlugin: CAPPlugin {
59
60
 
60
61
  DispatchQueue.main.async {
61
62
  let url = URL(string: urlString)
62
- let webViewController: WKWebViewController?
63
63
 
64
64
  if self.isPresentAfterPageLoad {
65
- webViewController = WKWebViewController.init(url: url!, headers: headers)
65
+ self.webViewController = WKWebViewController.init(url: url!, headers: headers)
66
66
  } else {
67
- webViewController = WKWebViewController.init()
68
- webViewController?.setHeaders(headers: headers)
67
+ self.webViewController = WKWebViewController.init()
68
+ self.webViewController?.setHeaders(headers: headers)
69
69
  }
70
70
 
71
- webViewController?.source = .remote(url!)
72
- webViewController?.leftNavigaionBarItemTypes = self.getToolbarItems(toolbarType: toolbarType ?? "")
71
+ self.webViewController?.source = .remote(url!)
72
+ self.webViewController?.leftNavigaionBarItemTypes = self.getToolbarItems(toolbarType: toolbarType ?? "")
73
73
 
74
- webViewController?.toolbarItemTypes = []
75
- webViewController?.doneBarButtonItemPosition = .right
76
- webViewController?.capBrowserPlugin = self
77
- webViewController?.title = call.getString("title") ?? ""
78
- webViewController?.shareSubject = call.getString("shareSubject")
79
- webViewController?.shareDisclaimer = disclaimerContent
80
- self.navigationWebViewController = UINavigationController.init(rootViewController: webViewController!)
74
+ self.webViewController?.toolbarItemTypes = []
75
+ self.webViewController?.doneBarButtonItemPosition = .right
76
+ self.webViewController?.capBrowserPlugin = self
77
+ self.webViewController?.title = call.getString("title") ?? ""
78
+ self.webViewController?.shareSubject = call.getString("shareSubject")
79
+ self.webViewController?.shareDisclaimer = disclaimerContent
80
+ self.navigationWebViewController = UINavigationController.init(rootViewController: self.webViewController!)
81
81
  self.navigationWebViewController?.navigationBar.isTranslucent = false
82
82
  self.navigationWebViewController?.toolbar.isTranslucent = false
83
83
  self.navigationWebViewController?.navigationBar.backgroundColor = .white
@@ -104,6 +104,11 @@ public class InAppBrowserPlugin: CAPPlugin {
104
104
  }
105
105
 
106
106
  @objc func setUrl(_ call: CAPPluginCall) {
107
+ guard let url = call.getString("url") else {
108
+ call.reject("Cannot get new url to set")
109
+ return
110
+ }
111
+ self.webViewController?.load(remote: URL(string: url)!)
107
112
  call.resolve()
108
113
  }
109
114
 
@@ -130,21 +135,20 @@ public class InAppBrowserPlugin: CAPPlugin {
130
135
 
131
136
  DispatchQueue.main.async {
132
137
  let url = URL(string: urlString)
133
- let webViewController: WKWebViewController?
134
138
 
135
139
  if self.isPresentAfterPageLoad {
136
- webViewController = WKWebViewController.init(url: url!, headers: headers)
140
+ self.webViewController = WKWebViewController.init(url: url!, headers: headers)
137
141
  } else {
138
- webViewController = WKWebViewController.init()
139
- webViewController?.setHeaders(headers: headers)
142
+ self.webViewController = WKWebViewController.init()
143
+ self.webViewController?.setHeaders(headers: headers)
140
144
  }
141
145
 
142
- webViewController?.source = .remote(url!)
143
- webViewController?.leftNavigaionBarItemTypes = [.reload]
144
- webViewController?.toolbarItemTypes = [.back, .forward, .activity]
145
- webViewController?.capBrowserPlugin = self
146
- webViewController?.hasDynamicTitle = true
147
- self.navigationWebViewController = UINavigationController.init(rootViewController: webViewController!)
146
+ self.webViewController?.source = .remote(url!)
147
+ self.webViewController?.leftNavigaionBarItemTypes = [.reload]
148
+ self.webViewController?.toolbarItemTypes = [.back, .forward, .activity]
149
+ self.webViewController?.capBrowserPlugin = self
150
+ self.webViewController?.hasDynamicTitle = true
151
+ self.navigationWebViewController = UINavigationController.init(rootViewController: self.webViewController!)
148
152
  self.navigationWebViewController?.navigationBar.isTranslucent = false
149
153
  self.navigationWebViewController?.toolbar.isTranslucent = false
150
154
  self.navigationWebViewController?.navigationBar.backgroundColor = .white
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/inappbrowser",
3
- "version": "0.0.4",
3
+ "version": "0.1.0",
4
4
  "description": "Capacitor plugin in app browser",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",