@capgo/capacitor-updater 4.2.3 → 4.2.4

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,17 +1,16 @@
1
1
  package ee.forgr.capacitor_updater;
2
2
 
3
3
  import com.getcapacitor.JSObject;
4
-
5
- import org.json.JSONException;
6
- import org.json.JSONObject;
7
- import org.json.JSONTokener;
8
-
9
4
  import java.text.SimpleDateFormat;
10
5
  import java.util.Date;
11
6
  import java.util.Objects;
12
7
  import java.util.TimeZone;
8
+ import org.json.JSONException;
9
+ import org.json.JSONObject;
10
+ import org.json.JSONTokener;
13
11
 
14
12
  public class BundleInfo {
13
+
15
14
  private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
16
15
 
17
16
  public static final String ID_BUILTIN = "builtin";
@@ -47,12 +46,15 @@ public class BundleInfo {
47
46
  public Boolean isBuiltin() {
48
47
  return ID_BUILTIN.equals(this.id);
49
48
  }
49
+
50
50
  public Boolean isUnknown() {
51
51
  return VERSION_UNKNOWN.equals(this.id);
52
52
  }
53
+
53
54
  public Boolean isErrorStatus() {
54
55
  return BundleStatus.ERROR == this.status;
55
56
  }
57
+
56
58
  public boolean isDownloaded() {
57
59
  return !this.isBuiltin() && this.downloaded != null && !this.downloaded.equals("");
58
60
  }
@@ -104,11 +106,11 @@ public class BundleInfo {
104
106
  public static BundleInfo fromJSON(final String jsonString) throws JSONException {
105
107
  JSONObject json = new JSONObject(new JSONTokener(jsonString));
106
108
  return new BundleInfo(
107
- json.has("id") ? json.getString("id") : "",
108
- json.has("version") ? json.getString("version") : BundleInfo.VERSION_UNKNOWN,
109
- json.has("status") ? BundleStatus.fromString(json.getString("status")) : BundleStatus.PENDING,
110
- json.has("downloaded") ? json.getString("downloaded") : "",
111
- json.has("checksum") ? json.getString("checksum") : ""
109
+ json.has("id") ? json.getString("id") : "",
110
+ json.has("version") ? json.getString("version") : BundleInfo.VERSION_UNKNOWN,
111
+ json.has("status") ? BundleStatus.fromString(json.getString("status")) : BundleStatus.PENDING,
112
+ json.has("downloaded") ? json.getString("downloaded") : "",
113
+ json.has("checksum") ? json.getString("checksum") : ""
112
114
  );
113
115
  }
114
116
 
@@ -139,4 +141,4 @@ public class BundleInfo {
139
141
  public String toString() {
140
142
  return this.toJSON().toString();
141
143
  }
142
- }
144
+ }
@@ -12,8 +12,9 @@ public enum BundleStatus {
12
12
  public final String label;
13
13
 
14
14
  private static final Map<String, BundleStatus> BY_LABEL = new HashMap<>();
15
+
15
16
  static {
16
- for (final BundleStatus e: values()) {
17
+ for (final BundleStatus e : values()) {
17
18
  BY_LABEL.put(e.label, e);
18
19
  }
19
20
  }
@@ -28,7 +29,7 @@ public enum BundleStatus {
28
29
  }
29
30
 
30
31
  public static BundleStatus fromString(final String status) {
31
- if(status == null || status.isEmpty()) {
32
+ if (status == null || status.isEmpty()) {
32
33
  return BundleStatus.PENDING;
33
34
  }
34
35
  return BundleStatus.BY_LABEL.get(status);
@@ -2,17 +2,12 @@ package ee.forgr.capacitor_updater;
2
2
 
3
3
  import android.content.SharedPreferences;
4
4
  import android.util.Log;
5
-
6
5
  import com.android.volley.Request;
7
6
  import com.android.volley.RequestQueue;
8
7
  import com.android.volley.Response;
9
8
  import com.android.volley.VolleyError;
10
9
  import com.android.volley.toolbox.JsonObjectRequest;
11
10
  import com.getcapacitor.plugin.WebView;
12
-
13
- import org.json.JSONException;
14
- import org.json.JSONObject;
15
-
16
11
  import java.io.BufferedInputStream;
17
12
  import java.io.DataInputStream;
18
13
  import java.io.File;
@@ -31,12 +26,15 @@ import java.util.List;
31
26
  import java.util.zip.CRC32;
32
27
  import java.util.zip.ZipEntry;
33
28
  import java.util.zip.ZipInputStream;
29
+ import org.json.JSONException;
30
+ import org.json.JSONObject;
34
31
 
35
32
  interface Callback {
36
33
  void callback(JSONObject jsonObject);
37
34
  }
38
35
 
39
36
  public class CapacitorUpdater {
37
+
40
38
  private static final String AB = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
41
39
  private static final SecureRandom rnd = new SecureRandom();
42
40
 
@@ -47,7 +45,7 @@ public class CapacitorUpdater {
47
45
  private static final String bundleDirectory = "versions";
48
46
 
49
47
  public static final String TAG = "Capacitor-updater";
50
- public static final String pluginVersion = "4.2.3";
48
+ public static final String pluginVersion = "4.2.4";
51
49
 
52
50
  public SharedPreferences.Editor editor;
53
51
  public SharedPreferences prefs;
@@ -79,10 +77,9 @@ public class CapacitorUpdater {
79
77
  return;
80
78
  }
81
79
 
82
- private String randomString(final int len){
80
+ private String randomString(final int len) {
83
81
  final StringBuilder sb = new StringBuilder(len);
84
- for(int i = 0; i < len; i++)
85
- sb.append(AB.charAt(rnd.nextInt(AB.length())));
82
+ for (int i = 0; i < len; i++) sb.append(AB.charAt(rnd.nextInt(AB.length())));
86
83
  return sb.toString();
87
84
  }
88
85
 
@@ -106,25 +103,24 @@ public class CapacitorUpdater {
106
103
  final File dir = entry.isDirectory() ? file : file.getParentFile();
107
104
 
108
105
  if (!canonicalPath.startsWith(canonicalDir)) {
109
- throw new FileNotFoundException("SecurityException, Failed to ensure directory is the start path : " +
110
- canonicalDir + " of " + canonicalPath);
106
+ throw new FileNotFoundException(
107
+ "SecurityException, Failed to ensure directory is the start path : " + canonicalDir + " of " + canonicalPath
108
+ );
111
109
  }
112
110
 
113
111
  if (!dir.isDirectory() && !dir.mkdirs()) {
114
- throw new FileNotFoundException("Failed to ensure directory: " +
115
- dir.getAbsolutePath());
112
+ throw new FileNotFoundException("Failed to ensure directory: " + dir.getAbsolutePath());
116
113
  }
117
114
 
118
115
  if (entry.isDirectory()) {
119
116
  continue;
120
117
  }
121
118
 
122
- try(final FileOutputStream outputStream = new FileOutputStream(file)) {
123
- while ((count = zis.read(buffer)) != -1)
124
- outputStream.write(buffer, 0, count);
119
+ try (final FileOutputStream outputStream = new FileOutputStream(file)) {
120
+ while ((count = zis.read(buffer)) != -1) outputStream.write(buffer, 0, count);
125
121
  }
126
122
 
127
- final int newPercent = (int)((lengthRead * 100) / lengthTotal);
123
+ final int newPercent = (int) ((lengthRead * 100) / lengthTotal);
128
124
  if (lengthTotal > 1 && newPercent != percent) {
129
125
  percent = newPercent;
130
126
  this.notifyDownload(id, this.calcTotalPercent(percent, 75, 90));
@@ -162,7 +158,6 @@ public class CapacitorUpdater {
162
158
  }
163
159
 
164
160
  private File downloadFile(final String id, final String url, final String dest) throws IOException {
165
-
166
161
  final URL u = new URL(url);
167
162
  final URLConnection connection = u.openConnection();
168
163
  final InputStream is = u.openStream();
@@ -181,9 +176,9 @@ public class CapacitorUpdater {
181
176
  int bytesRead = bufferSize;
182
177
  int percent = 0;
183
178
  this.notifyDownload(id, 10);
184
- while ((length = dis.read(buffer))>0) {
179
+ while ((length = dis.read(buffer)) > 0) {
185
180
  fos.write(buffer, 0, length);
186
- final int newPercent = (int)((bytesRead * 100) / totalLength);
181
+ final int newPercent = (int) ((bytesRead * 100) / totalLength);
187
182
  if (totalLength > 1 && newPercent != percent) {
188
183
  percent = newPercent;
189
184
  this.notifyDownload(id, this.calcTotalPercent(percent, 10, 70));
@@ -215,7 +210,7 @@ public class CapacitorUpdater {
215
210
 
216
211
  private String getChecksum(File file) throws IOException {
217
212
  byte[] bytes = new byte[(int) file.length()];
218
- try(FileInputStream fis = new FileInputStream(file)){
213
+ try (FileInputStream fis = new FileInputStream(file)) {
219
214
  fis.read(bytes);
220
215
  }
221
216
  CRC32 crc = new CRC32();
@@ -288,7 +283,7 @@ public class CapacitorUpdater {
288
283
 
289
284
  private boolean bundleExists(final String id) {
290
285
  final File bundle = this.getBundleDirectory(id);
291
- if(bundle == null || !bundle.exists()) {
286
+ if (bundle == null || !bundle.exists()) {
292
287
  return false;
293
288
  }
294
289
  return new File(bundle.getPath(), "/index.html").exists();
@@ -300,7 +295,7 @@ public class CapacitorUpdater {
300
295
 
301
296
  public Boolean set(final String id) {
302
297
  final BundleInfo newBundle = this.getBundleInfo(id);
303
- if(newBundle.isBuiltin()) {
298
+ if (newBundle.isBuiltin()) {
304
299
  this.reset();
305
300
  return true;
306
301
  }
@@ -325,7 +320,7 @@ public class CapacitorUpdater {
325
320
  final BundleInfo fallback = this.getFallbackBundle();
326
321
  Log.d(CapacitorUpdater.TAG, "Fallback bundle is: " + fallback);
327
322
  Log.i(CapacitorUpdater.TAG, "Version successfully loaded: " + bundle.getVersionName());
328
- if(autoDeletePrevious && !fallback.isBuiltin()) {
323
+ if (autoDeletePrevious && !fallback.isBuiltin()) {
329
324
  try {
330
325
  final Boolean res = this.delete(fallback.getId());
331
326
  if (res) {
@@ -347,7 +342,7 @@ public class CapacitorUpdater {
347
342
  this.setCurrentBundle(new File("public"));
348
343
  this.setFallbackBundle(null);
349
344
  this.setNextBundle(null);
350
- if(!internal) {
345
+ if (!internal) {
351
346
  this.sendStats("reset", this.getCurrentBundle().getVersionName());
352
347
  }
353
348
  }
@@ -374,31 +369,34 @@ public class CapacitorUpdater {
374
369
  Log.e(CapacitorUpdater.TAG, "Auto-update parameters: " + json.toString());
375
370
  // Building a request
376
371
  JsonObjectRequest request = new JsonObjectRequest(
377
- Request.Method.POST,
378
- updateUrl,
379
- json,
380
- new Response.Listener<JSONObject>() {
381
- @Override
382
- public void onResponse(JSONObject response) {
383
- callback.callback(response);
384
- }
385
- },
386
- new Response.ErrorListener(){
387
- @Override
388
- public void onErrorResponse(VolleyError error) {
389
- Log.e(TAG, "Error getting Latest " + error);
390
- }
391
- });
372
+ Request.Method.POST,
373
+ updateUrl,
374
+ json,
375
+ new Response.Listener<JSONObject>() {
376
+ @Override
377
+ public void onResponse(JSONObject response) {
378
+ callback.callback(response);
379
+ }
380
+ },
381
+ new Response.ErrorListener() {
382
+ @Override
383
+ public void onErrorResponse(VolleyError error) {
384
+ Log.e(TAG, "Error getting Latest " + error);
385
+ }
386
+ }
387
+ );
392
388
  this.requestQueue.add(request);
393
- } catch(JSONException ex){
389
+ } catch (JSONException ex) {
394
390
  // Catch if something went wrong with the params
395
- Log.e(TAG, "Error getLatest JSONException " + ex);
391
+ Log.e(TAG, "Error getLatest JSONException " + ex);
396
392
  }
397
393
  }
398
394
 
399
395
  public void sendStats(final String action, final String versionName) {
400
396
  String statsUrl = this.statsUrl;
401
- if (statsUrl == null || "".equals(statsUrl) || statsUrl.length() == 0) { return; }
397
+ if (statsUrl == null || "".equals(statsUrl) || statsUrl.length() == 0) {
398
+ return;
399
+ }
402
400
  try {
403
401
  JSONObject json = new JSONObject();
404
402
  json.put("platform", "android");
@@ -413,38 +411,39 @@ public class CapacitorUpdater {
413
411
 
414
412
  // Building a request
415
413
  JsonObjectRequest request = new JsonObjectRequest(
416
- Request.Method.POST,
417
- statsUrl,
418
- json,
419
- new Response.Listener<JSONObject>() {
420
- @Override
421
- public void onResponse(JSONObject response) {
422
- Log.i(TAG, "Stats send for \"" + action + "\", version " + versionName);
423
- }
424
- },
425
- new Response.ErrorListener(){
426
- @Override
427
- public void onErrorResponse(VolleyError error) {
428
- Log.i(TAG, "Error sending stats: " + error);
429
- }
430
- });
414
+ Request.Method.POST,
415
+ statsUrl,
416
+ json,
417
+ new Response.Listener<JSONObject>() {
418
+ @Override
419
+ public void onResponse(JSONObject response) {
420
+ Log.i(TAG, "Stats send for \"" + action + "\", version " + versionName);
421
+ }
422
+ },
423
+ new Response.ErrorListener() {
424
+ @Override
425
+ public void onErrorResponse(VolleyError error) {
426
+ Log.i(TAG, "Error sending stats: " + error);
427
+ }
428
+ }
429
+ );
431
430
  this.requestQueue.add(request);
432
- } catch(JSONException ex){
431
+ } catch (JSONException ex) {
433
432
  // Catch if something went wrong with the params
434
- Log.e(TAG, "Error sendStats JSONException " + ex);
433
+ Log.e(TAG, "Error sendStats JSONException " + ex);
435
434
  }
436
435
  }
437
436
 
438
437
  public BundleInfo getBundleInfo(final String id) {
439
438
  String trueId = BundleInfo.VERSION_UNKNOWN;
440
- if(id != null) {
439
+ if (id != null) {
441
440
  trueId = id;
442
441
  }
443
442
  Log.d(TAG, "Getting info for bundle [" + trueId + "]");
444
443
  BundleInfo result;
445
- if(BundleInfo.ID_BUILTIN.equals(trueId)) {
444
+ if (BundleInfo.ID_BUILTIN.equals(trueId)) {
446
445
  result = new BundleInfo(trueId, (String) null, BundleStatus.SUCCESS, "", "");
447
- } else if(BundleInfo.VERSION_UNKNOWN.equals(trueId)) {
446
+ } else if (BundleInfo.VERSION_UNKNOWN.equals(trueId)) {
448
447
  result = new BundleInfo(trueId, (String) null, BundleStatus.ERROR, "", "");
449
448
  } else {
450
449
  try {
@@ -462,8 +461,8 @@ public class CapacitorUpdater {
462
461
 
463
462
  public BundleInfo getBundleInfoByName(final String versionName) {
464
463
  final List<BundleInfo> installed = this.list();
465
- for(final BundleInfo i : installed) {
466
- if(i.getVersionName().equals(versionName)) {
464
+ for (final BundleInfo i : installed) {
465
+ if (i.getVersionName().equals(versionName)) {
467
466
  return i;
468
467
  }
469
468
  }
@@ -475,12 +474,12 @@ public class CapacitorUpdater {
475
474
  }
476
475
 
477
476
  private void saveBundleInfo(final String id, final BundleInfo info) {
478
- if(id == null || (info != null && (info.isBuiltin() || info.isUnknown()))) {
477
+ if (id == null || (info != null && (info.isBuiltin() || info.isUnknown()))) {
479
478
  Log.d(TAG, "Not saving info for bundle: [" + id + "] " + info);
480
479
  return;
481
480
  }
482
481
 
483
- if(info == null) {
482
+ if (info == null) {
484
483
  Log.d(TAG, "Removing info for bundle [" + id + "]");
485
484
  this.editor.remove(id + INFO_SUFFIX);
486
485
  } else {
@@ -492,7 +491,7 @@ public class CapacitorUpdater {
492
491
  }
493
492
 
494
493
  public void setVersionName(final String id, final String name) {
495
- if(id != null) {
494
+ if (id != null) {
496
495
  Log.d(TAG, "Setting name for bundle [" + id + "] to " + name);
497
496
  BundleInfo info = this.getBundleInfo(id);
498
497
  this.saveBundleInfo(id, info.setVersionName(name));
@@ -500,7 +499,7 @@ public class CapacitorUpdater {
500
499
  }
501
500
 
502
501
  private void setBundleStatus(final String id, final BundleStatus status) {
503
- if(id != null && status != null) {
502
+ if (id != null && status != null) {
504
503
  BundleInfo info = this.getBundleInfo(id);
505
504
  Log.d(TAG, "Setting status for bundle [" + id + "] to " + status);
506
505
  this.saveBundleInfo(id, info.setStatus(status));
@@ -508,7 +507,7 @@ public class CapacitorUpdater {
508
507
  }
509
508
 
510
509
  private String getCurrentBundleId() {
511
- if(this.isUsingBuiltin()) {
510
+ if (this.isUsingBuiltin()) {
512
511
  return BundleInfo.ID_BUILTIN;
513
512
  } else {
514
513
  final String path = this.getCurrentBundlePath();
@@ -522,7 +521,7 @@ public class CapacitorUpdater {
522
521
 
523
522
  public String getCurrentBundlePath() {
524
523
  String path = this.prefs.getString(WebView.CAP_SERVER_PATH, "public");
525
- if("".equals(path.trim())) {
524
+ if ("".equals(path.trim())) {
526
525
  return "public";
527
526
  }
528
527
  return path;
@@ -538,11 +537,7 @@ public class CapacitorUpdater {
538
537
  }
539
538
 
540
539
  private void setFallbackBundle(final BundleInfo fallback) {
541
- this.editor.putString(FALLBACK_VERSION,
542
- fallback == null
543
- ? BundleInfo.ID_BUILTIN
544
- : fallback.getId()
545
- );
540
+ this.editor.putString(FALLBACK_VERSION, fallback == null ? BundleInfo.ID_BUILTIN : fallback.getId());
546
541
  this.editor.commit();
547
542
  }
548
543
 
@@ -565,5 +560,4 @@ public class CapacitorUpdater {
565
560
  this.editor.commit();
566
561
  return true;
567
562
  }
568
-
569
563
  }