tharak-android 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +1 -0
- package/build.gradle +45 -0
- package/consumer-rules.pro +0 -0
- package/libs/vital-sdk.aar +0 -0
- package/package.json +22 -0
- package/proguard-rules.pro +21 -0
- package/src/androidTest/androidTest.iml +11 -0
- package/src/androidTest/java/org/tharak/core/ExampleInstrumentedTest.java +27 -0
- package/src/androidTest/java/org/thiragatisoft/core/ExampleInstrumentedTest.java +26 -0
- package/src/main/AndroidManifest.xml +5 -0
- package/src/main/java/org/thiragatisoft/core/AppUtils.java +31 -0
- package/src/main/java/org/thiragatisoft/core/FileSelector.java +162 -0
- package/src/main/java/org/thiragatisoft/core/MainActivity.java +27 -0
- package/src/main/java/org/thiragatisoft/core/RestClient.java +298 -0
- package/src/main/java/org/thiragatisoft/core/VitalActivity.java +612 -0
- package/src/main/java/org/thiragatisoft/core/VitalsPlugin.java +15 -0
- package/src/main/java/org/thiragatisoft/core/VolleyMultipartRequest.java +218 -0
- package/src/main/java/org/thiragatisoft/core/http/CapacitorCookieManager.java +175 -0
- package/src/main/java/org/thiragatisoft/core/http/CapacitorHttpUrlConnection.java +408 -0
- package/src/main/java/org/thiragatisoft/core/http/FilesystemUtils.java +65 -0
- package/src/main/java/org/thiragatisoft/core/http/FormUploader.java +219 -0
- package/src/main/java/org/thiragatisoft/core/http/Http.java +275 -0
- package/src/main/java/org/thiragatisoft/core/http/HttpRequestHandler.java +515 -0
- package/src/main/java/org/thiragatisoft/core/http/ICapacitorHttpUrlConnection.java +15 -0
- package/src/main/java/org/thiragatisoft/core/http/JSValue.java +68 -0
- package/src/main/java/org/thiragatisoft/core/http/MimeType.java +17 -0
- package/src/main/main.iml +12 -0
- package/src/test/java/org/tharak/ExampleUnitTest.java +17 -0
- package/src/test/java/org/tharak/JSObjectTest.java +209 -0
- package/src/test/java/org/tharak/PluginMethodHandleTest.java +44 -0
- package/src/test/java/org/tharak/util/HostMaskTest.java +70 -0
- package/src/test/java/org/thiragatisoft/core/ExampleUnitTest.java +17 -0
- package/src/test/test.iml +11 -0
@@ -0,0 +1,275 @@
|
|
1
|
+
package org.thiragatisoft.core.http;
|
2
|
+
|
3
|
+
import android.Manifest;
|
4
|
+
import android.util.Log;
|
5
|
+
import com.getcapacitor.CapConfig;
|
6
|
+
import com.getcapacitor.JSArray;
|
7
|
+
import com.getcapacitor.JSObject;
|
8
|
+
import com.getcapacitor.Plugin;
|
9
|
+
import com.getcapacitor.PluginCall;
|
10
|
+
import com.getcapacitor.PluginMethod;
|
11
|
+
import com.getcapacitor.NativePlugin;
|
12
|
+
|
13
|
+
import java.io.IOException;
|
14
|
+
import java.net.HttpCookie;
|
15
|
+
import java.net.MalformedURLException;
|
16
|
+
import java.net.URI;
|
17
|
+
@NativePlugin()
|
18
|
+
public class Http extends Plugin {
|
19
|
+
|
20
|
+
public static final int HTTP_REQUEST_DOWNLOAD_WRITE_PERMISSIONS = 9022;
|
21
|
+
public static final int HTTP_REQUEST_UPLOAD_READ_PERMISSIONS = 9023;
|
22
|
+
|
23
|
+
CapConfig capConfig;
|
24
|
+
CapacitorCookieManager cookieManager;
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Helper function for getting the serverUrl from the Capacitor Config. Returns an empty
|
28
|
+
* string if it is invalid and will auto-reject through {@code call}
|
29
|
+
* @param call the {@code PluginCall} context
|
30
|
+
* @return the string of the server specified in the Capacitor config
|
31
|
+
*/
|
32
|
+
private String getServerUrl(PluginCall call) {
|
33
|
+
String url = call.getString("url", "");
|
34
|
+
|
35
|
+
URI uri = getUri(url);
|
36
|
+
if (uri == null) {
|
37
|
+
call.reject("Invalid URL. Check that \"server\" is passed in correctly");
|
38
|
+
return "";
|
39
|
+
}
|
40
|
+
|
41
|
+
return url;
|
42
|
+
}
|
43
|
+
|
44
|
+
/**
|
45
|
+
* Try to parse a url string and if it can't be parsed, return null
|
46
|
+
* @param url the url string to try to parse
|
47
|
+
* @return a parsed URI
|
48
|
+
*/
|
49
|
+
private URI getUri(String url) {
|
50
|
+
try {
|
51
|
+
return new URI(url);
|
52
|
+
} catch (Exception ex) {
|
53
|
+
return null;
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
private boolean isStoragePermissionGranted(PluginCall call, String permission) {
|
58
|
+
if (hasPermission(permission)) {
|
59
|
+
Log.v(getLogTag(), "Permission '" + permission + "' is granted");
|
60
|
+
return true;
|
61
|
+
} else {
|
62
|
+
Log.v(getLogTag(), "Permission '" + permission + "' denied. Asking user for it.");
|
63
|
+
requestPermissions(call);
|
64
|
+
return false;
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
private void http(final PluginCall call, final String httpMethod) {
|
69
|
+
Runnable asyncHttpCall = new Runnable() {
|
70
|
+
@Override
|
71
|
+
public void run() {
|
72
|
+
try {
|
73
|
+
JSObject response = HttpRequestHandler.request(call, httpMethod);
|
74
|
+
call.resolve(response);
|
75
|
+
} catch (Exception e) {
|
76
|
+
System.out.println(e.toString());
|
77
|
+
call.reject(e.getClass().getSimpleName(), e);
|
78
|
+
}
|
79
|
+
}
|
80
|
+
};
|
81
|
+
Thread httpThread = new Thread(asyncHttpCall);
|
82
|
+
httpThread.start();
|
83
|
+
}
|
84
|
+
|
85
|
+
@Override
|
86
|
+
public void load() {
|
87
|
+
this.cookieManager = new CapacitorCookieManager(null, java.net.CookiePolicy.ACCEPT_ALL);
|
88
|
+
java.net.CookieHandler.setDefault(cookieManager);
|
89
|
+
capConfig = getBridge().getConfig();
|
90
|
+
}
|
91
|
+
|
92
|
+
@PluginMethod
|
93
|
+
public void request(final PluginCall call) {
|
94
|
+
this.http(call, null);
|
95
|
+
}
|
96
|
+
|
97
|
+
@PluginMethod
|
98
|
+
public void get(final PluginCall call) {
|
99
|
+
this.http(call, "GET");
|
100
|
+
}
|
101
|
+
|
102
|
+
@PluginMethod
|
103
|
+
public void post(final PluginCall call) {
|
104
|
+
this.http(call, "POST");
|
105
|
+
}
|
106
|
+
|
107
|
+
@PluginMethod
|
108
|
+
public void put(final PluginCall call) {
|
109
|
+
this.http(call, "PUT");
|
110
|
+
}
|
111
|
+
|
112
|
+
@PluginMethod
|
113
|
+
public void patch(final PluginCall call) {
|
114
|
+
this.http(call, "PATCH");
|
115
|
+
}
|
116
|
+
|
117
|
+
@PluginMethod
|
118
|
+
public void del(final PluginCall call) {
|
119
|
+
this.http(call, "DELETE");
|
120
|
+
}
|
121
|
+
|
122
|
+
@PluginMethod
|
123
|
+
public void downloadFile(final PluginCall call) {
|
124
|
+
try {
|
125
|
+
bridge.saveCall(call);
|
126
|
+
String fileDirectory = call.getString("fileDirectory", FilesystemUtils.DIRECTORY_DOCUMENTS);
|
127
|
+
|
128
|
+
if (
|
129
|
+
!FilesystemUtils.isPublicDirectory(fileDirectory) ||
|
130
|
+
isStoragePermissionGranted(call, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
131
|
+
) {
|
132
|
+
call.release(bridge);
|
133
|
+
|
134
|
+
HttpRequestHandler.ProgressEmitter emitter = new HttpRequestHandler.ProgressEmitter() {
|
135
|
+
@Override
|
136
|
+
public void emit(Integer bytes, Integer contentLength) {
|
137
|
+
// no-op
|
138
|
+
}
|
139
|
+
};
|
140
|
+
Boolean progress = call.getBoolean("progress", false);
|
141
|
+
if (progress) {
|
142
|
+
emitter =
|
143
|
+
new HttpRequestHandler.ProgressEmitter() {
|
144
|
+
@Override
|
145
|
+
public void emit(final Integer bytes, final Integer contentLength) {
|
146
|
+
JSObject ret = new JSObject();
|
147
|
+
ret.put("type", "DOWNLOAD");
|
148
|
+
ret.put("url", call.getString("url"));
|
149
|
+
ret.put("bytes", bytes);
|
150
|
+
ret.put("contentLength", contentLength);
|
151
|
+
|
152
|
+
notifyListeners("progress", ret);
|
153
|
+
}
|
154
|
+
};
|
155
|
+
}
|
156
|
+
|
157
|
+
JSObject response = HttpRequestHandler.downloadFile(call, getContext(), emitter);
|
158
|
+
call.resolve(response);
|
159
|
+
}
|
160
|
+
} catch (MalformedURLException ex) {
|
161
|
+
call.reject("Invalid URL", ex);
|
162
|
+
} catch (IOException ex) {
|
163
|
+
call.reject("IO Error", ex);
|
164
|
+
} catch (Exception ex) {
|
165
|
+
call.reject("Error", ex);
|
166
|
+
}
|
167
|
+
}
|
168
|
+
|
169
|
+
@PluginMethod
|
170
|
+
public void uploadFile(PluginCall call) {
|
171
|
+
try {
|
172
|
+
String fileDirectory = call.getString("fileDirectory", FilesystemUtils.DIRECTORY_DOCUMENTS);
|
173
|
+
bridge.saveCall(call);
|
174
|
+
|
175
|
+
if (
|
176
|
+
!FilesystemUtils.isPublicDirectory(fileDirectory) ||
|
177
|
+
isStoragePermissionGranted(call, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
178
|
+
) {
|
179
|
+
call.release(bridge);
|
180
|
+
JSObject response = HttpRequestHandler.uploadFile(call, getContext());
|
181
|
+
call.resolve(response);
|
182
|
+
}
|
183
|
+
} catch (Exception ex) {
|
184
|
+
call.reject("Error", ex);
|
185
|
+
}
|
186
|
+
}
|
187
|
+
|
188
|
+
@PluginMethod
|
189
|
+
public void setCookie(PluginCall call) {
|
190
|
+
String key = call.getString("key");
|
191
|
+
String value = call.getString("value");
|
192
|
+
String url = getServerUrl(call);
|
193
|
+
|
194
|
+
if (!url.isEmpty()) {
|
195
|
+
cookieManager.setCookie(url, key, value);
|
196
|
+
call.resolve();
|
197
|
+
}
|
198
|
+
}
|
199
|
+
|
200
|
+
@PluginMethod
|
201
|
+
public void getCookiesMap(PluginCall call) {
|
202
|
+
String url = getServerUrl(call);
|
203
|
+
if (!url.isEmpty()) {
|
204
|
+
HttpCookie[] cookies = cookieManager.getCookies(url);
|
205
|
+
JSObject cookiesJsObject = new JSObject();
|
206
|
+
for (HttpCookie cookie : cookies) {
|
207
|
+
cookiesJsObject.put(cookie.getName(), cookie.getValue());
|
208
|
+
}
|
209
|
+
call.resolve(cookiesJsObject);
|
210
|
+
}
|
211
|
+
}
|
212
|
+
|
213
|
+
@PluginMethod
|
214
|
+
public void getCookies(PluginCall call) {
|
215
|
+
String url = getServerUrl(call);
|
216
|
+
if (!url.isEmpty()) {
|
217
|
+
HttpCookie[] cookies = cookieManager.getCookies(url);
|
218
|
+
JSArray cookiesJsArray = new JSArray();
|
219
|
+
for (HttpCookie cookie : cookies) {
|
220
|
+
JSObject cookieJsPair = new JSObject();
|
221
|
+
cookieJsPair.put("key", cookie.getName());
|
222
|
+
cookieJsPair.put("value", cookie.getValue());
|
223
|
+
cookiesJsArray.put(cookieJsPair);
|
224
|
+
}
|
225
|
+
JSObject cookiesJsObject = new JSObject();
|
226
|
+
cookiesJsObject.put("cookies", cookiesJsArray);
|
227
|
+
call.resolve(cookiesJsObject);
|
228
|
+
}
|
229
|
+
}
|
230
|
+
|
231
|
+
@PluginMethod
|
232
|
+
public void getCookie(PluginCall call) {
|
233
|
+
String key = call.getString("key");
|
234
|
+
String url = getServerUrl(call);
|
235
|
+
if (!url.isEmpty()) {
|
236
|
+
HttpCookie cookie = cookieManager.getCookie(url, key);
|
237
|
+
JSObject cookieJsObject = new JSObject();
|
238
|
+
cookieJsObject.put("key", key);
|
239
|
+
if (cookie != null) {
|
240
|
+
cookieJsObject.put("value", cookie.getValue());
|
241
|
+
} else {
|
242
|
+
cookieJsObject.put("value", "");
|
243
|
+
}
|
244
|
+
call.resolve(cookieJsObject);
|
245
|
+
}
|
246
|
+
}
|
247
|
+
|
248
|
+
@PluginMethod
|
249
|
+
public void deleteCookie(PluginCall call) {
|
250
|
+
String key = call.getString("key");
|
251
|
+
String url = getServerUrl(call);
|
252
|
+
if (!url.isEmpty()) {
|
253
|
+
cookieManager.setCookie(url, key + "=; Expires=Wed, 31 Dec 2000 23:59:59 GMT");
|
254
|
+
call.resolve();
|
255
|
+
}
|
256
|
+
}
|
257
|
+
|
258
|
+
@PluginMethod
|
259
|
+
public void clearCookies(PluginCall call) {
|
260
|
+
String url = getServerUrl(call);
|
261
|
+
if (!url.isEmpty()) {
|
262
|
+
HttpCookie[] cookies = cookieManager.getCookies(url);
|
263
|
+
for (HttpCookie cookie : cookies) {
|
264
|
+
cookieManager.setCookie(url, cookie.getName() + "=; Expires=Wed, 31 Dec 2000 23:59:59 GMT");
|
265
|
+
}
|
266
|
+
call.resolve();
|
267
|
+
}
|
268
|
+
}
|
269
|
+
|
270
|
+
@PluginMethod
|
271
|
+
public void clearAllCookies(PluginCall call) {
|
272
|
+
cookieManager.removeAllCookies();
|
273
|
+
call.resolve();
|
274
|
+
}
|
275
|
+
}
|