@capgo/capacitor-updater 4.2.6 → 4.3.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.
package/README.md CHANGED
@@ -127,6 +127,7 @@ Capacitor Updator works by unzipping a compiled app bundle to the native device
127
127
  * [`reset(...)`](#reset)
128
128
  * [`current()`](#current)
129
129
  * [`reload()`](#reload)
130
+ * [`setMultiDelay(...)`](#setmultidelay)
130
131
  * [`setDelay(...)`](#setdelay)
131
132
  * [`cancelDelay()`](#canceldelay)
132
133
  * [`getLatest(...)`](#getlatest)
@@ -137,6 +138,7 @@ Capacitor Updator works by unzipping a compiled app bundle to the native device
137
138
  * [`addListener('majorAvailable', ...)`](#addlistenermajoravailable)
138
139
  * [`addListener('updateFailed', ...)`](#addlistenerupdatefailed)
139
140
  * [`addListener('downloadFailed', ...)`](#addlistenerdownloadfailed)
141
+ * [`addListener('appReloaded', ...)`](#addlistenerappreloaded)
140
142
  * [`getDeviceId()`](#getdeviceid)
141
143
  * [`getPluginVersion()`](#getpluginversion)
142
144
  * [`isAutoUpdateEnabled()`](#isautoupdateenabled)
@@ -279,17 +281,34 @@ Reload the view
279
281
  --------------------
280
282
 
281
283
 
284
+ ### setMultiDelay(...)
285
+
286
+ ```typescript
287
+ setMultiDelay(options: { delayConditions: DelayCondition[]; }) => Promise<void>
288
+ ```
289
+
290
+ Set <a href="#delaycondition">DelayCondition</a>, skip updates until one of the conditions is met
291
+
292
+ | Param | Type | Description |
293
+ | ------------- | --------------------------------------------------- | ------------------------------------------------------------------------ |
294
+ | **`options`** | <code>{ delayConditions: DelayCondition[]; }</code> | are the {@link <a href="#delaycondition">DelayCondition</a>} list to set |
295
+
296
+ **Since:** 4.3.0
297
+
298
+ --------------------
299
+
300
+
282
301
  ### setDelay(...)
283
302
 
284
303
  ```typescript
285
- setDelay(options: { kind: DelayUntilNext; value?: string; }) => Promise<void>
304
+ setDelay(options: DelayCondition) => Promise<void>
286
305
  ```
287
306
 
288
- Set delay to skip updates in the next time the app goes into the background
307
+ Set <a href="#delaycondition">DelayCondition</a>, skip updates until the condition is met
289
308
 
290
- | Param | Type |
291
- | ------------- | ------------------------------------------------------------------------------------ |
292
- | **`options`** | <code>{ kind: <a href="#delayuntilnext">DelayUntilNext</a>; value?: string; }</code> |
309
+ | Param | Type | Description |
310
+ | ------------- | --------------------------------------------------------- | ------------------------------------------------------------------ |
311
+ | **`options`** | <code><a href="#delaycondition">DelayCondition</a></code> | is the {@link <a href="#delaycondition">DelayCondition</a>} to set |
293
312
 
294
313
  **Since:** 4.0.0
295
314
 
@@ -468,6 +487,26 @@ Listen for download fail event in the App, let you know when download has fail f
468
487
  --------------------
469
488
 
470
489
 
490
+ ### addListener('appReloaded', ...)
491
+
492
+ ```typescript
493
+ addListener(eventName: 'appReloaded', listenerFunc: AppReloadedListener) => Promise<PluginListenerHandle> & PluginListenerHandle
494
+ ```
495
+
496
+ Listen for download fail event in the App, let you know when download has fail finished
497
+
498
+ | Param | Type |
499
+ | ------------------ | ------------------------------------------------------------------- |
500
+ | **`eventName`** | <code>'appReloaded'</code> |
501
+ | **`listenerFunc`** | <code><a href="#appreloadedlistener">AppReloadedListener</a></code> |
502
+
503
+ **Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt; & <a href="#pluginlistenerhandle">PluginListenerHandle</a></code>
504
+
505
+ **Since:** 4.3.0
506
+
507
+ --------------------
508
+
509
+
471
510
  ### getDeviceId()
472
511
 
473
512
  ```typescript
@@ -546,6 +585,14 @@ removeAllListeners() => Promise<void>
546
585
  | **`status`** | <code><a href="#bundlestatus">BundleStatus</a></code> |
547
586
 
548
587
 
588
+ #### DelayCondition
589
+
590
+ | Prop | Type | Description |
591
+ | ----------- | --------------------------------------------------------- | ---------------------------------------- |
592
+ | **`kind`** | <code><a href="#delayuntilnext">DelayUntilNext</a></code> | Set up delay conditions in setMultiDelay |
593
+ | **`value`** | <code>string</code> | |
594
+
595
+
549
596
  #### latestVersion
550
597
 
551
598
  | Prop | Type | Description | Since |
@@ -661,6 +708,11 @@ removeAllListeners() => Promise<void>
661
708
 
662
709
  <code>(state: <a href="#downloadfailedevent">DownloadFailedEvent</a>): void</code>
663
710
 
711
+
712
+ #### AppReloadedListener
713
+
714
+ <code>(state: void): void</code>
715
+
664
716
  </docgen-api>
665
717
 
666
718
  ### Listen to download events
@@ -8,10 +8,10 @@ ext {
8
8
  buildscript {
9
9
  repositories {
10
10
  google()
11
- jcenter()
11
+ mavenCentral()
12
12
  }
13
13
  dependencies {
14
- classpath 'com.android.tools.build:gradle:4.2.1'
14
+ classpath 'com.android.tools.build:gradle:4.2.2'
15
15
  }
16
16
  }
17
17
 
@@ -43,7 +43,6 @@ android {
43
43
 
44
44
  repositories {
45
45
  google()
46
- jcenter()
47
46
  mavenCentral()
48
47
  }
49
48
 
@@ -54,6 +53,7 @@ dependencies {
54
53
  implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
55
54
  implementation 'com.android.volley:volley:1.2.1'
56
55
  implementation 'io.github.g00fy2:versioncompare:1.5.0'
56
+ implementation 'com.google.code.gson:gson:2.8.6'
57
57
  testImplementation "junit:junit:$junitVersion"
58
58
  androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
59
59
  androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
@@ -45,7 +45,7 @@ public class CapacitorUpdater {
45
45
  private static final String bundleDirectory = "versions";
46
46
 
47
47
  public static final String TAG = "Capacitor-updater";
48
- public static final String pluginVersion = "4.2.6";
48
+ public static final String pluginVersion = "4.3.0";
49
49
 
50
50
  public SharedPreferences.Editor editor;
51
51
  public SharedPreferences prefs;
@@ -366,7 +366,7 @@ public class CapacitorUpdater {
366
366
  json.put("version_name", version);
367
367
  json.put("plugin_version", pluginVersion);
368
368
 
369
- Log.e(CapacitorUpdater.TAG, "Auto-update parameters: " + json.toString());
369
+ Log.i(CapacitorUpdater.TAG, "Auto-update parameters: " + json);
370
370
  // Building a request
371
371
  JsonObjectRequest request = new JsonObjectRequest(
372
372
  Request.Method.POST,
@@ -381,14 +381,14 @@ public class CapacitorUpdater {
381
381
  new Response.ErrorListener() {
382
382
  @Override
383
383
  public void onErrorResponse(VolleyError error) {
384
- Log.e(TAG, "Error getting Latest " + error);
384
+ Log.e(TAG, "Error getting Latest", error);
385
385
  }
386
386
  }
387
387
  );
388
388
  this.requestQueue.add(request);
389
389
  } catch (JSONException ex) {
390
390
  // Catch if something went wrong with the params
391
- Log.e(TAG, "Error getLatest JSONException " + ex);
391
+ Log.e(TAG, "Error getLatest JSONException", ex);
392
392
  }
393
393
  }
394
394
 
@@ -423,14 +423,14 @@ public class CapacitorUpdater {
423
423
  new Response.ErrorListener() {
424
424
  @Override
425
425
  public void onErrorResponse(VolleyError error) {
426
- Log.i(TAG, "Error sending stats: " + error);
426
+ Log.e(TAG, "Error sending stats: " + error);
427
427
  }
428
428
  }
429
429
  );
430
430
  this.requestQueue.add(request);
431
431
  } catch (JSONException ex) {
432
432
  // Catch if something went wrong with the params
433
- Log.e(TAG, "Error sendStats JSONException " + ex);
433
+ Log.e(TAG, "Error sendStats JSONException", ex);
434
434
  }
435
435
  }
436
436
 
@@ -442,16 +442,16 @@ public class CapacitorUpdater {
442
442
  Log.d(TAG, "Getting info for bundle [" + trueId + "]");
443
443
  BundleInfo result;
444
444
  if (BundleInfo.ID_BUILTIN.equals(trueId)) {
445
- result = new BundleInfo(trueId, (String) null, BundleStatus.SUCCESS, "", "");
445
+ result = new BundleInfo(trueId, null, BundleStatus.SUCCESS, "", "");
446
446
  } else if (BundleInfo.VERSION_UNKNOWN.equals(trueId)) {
447
- result = new BundleInfo(trueId, (String) null, BundleStatus.ERROR, "", "");
447
+ result = new BundleInfo(trueId, null, BundleStatus.ERROR, "", "");
448
448
  } else {
449
449
  try {
450
450
  String stored = this.prefs.getString(trueId + INFO_SUFFIX, "");
451
451
  result = BundleInfo.fromJSON(stored);
452
452
  } catch (JSONException e) {
453
453
  Log.e(TAG, "Failed to parse info for bundle [" + trueId + "] ", e);
454
- result = new BundleInfo(trueId, (String) null, BundleStatus.PENDING, "", "");
454
+ result = new BundleInfo(trueId, null, BundleStatus.PENDING, "", "");
455
455
  }
456
456
  }
457
457
 
@@ -543,6 +543,7 @@ public class CapacitorUpdater {
543
543
 
544
544
  public BundleInfo getNextBundle() {
545
545
  final String id = this.prefs.getString(NEXT_VERSION, null);
546
+ if (id == null) return null;
546
547
  return this.getBundleInfo(id);
547
548
  }
548
549
 
@@ -7,7 +7,6 @@ import android.content.pm.PackageInfo;
7
7
  import android.content.pm.PackageManager;
8
8
  import android.os.Build;
9
9
  import android.os.Bundle;
10
- import android.provider.Settings;
11
10
  import android.util.Log;
12
11
  import androidx.annotation.NonNull;
13
12
  import androidx.annotation.Nullable;
@@ -20,11 +19,15 @@ import com.getcapacitor.PluginCall;
20
19
  import com.getcapacitor.PluginMethod;
21
20
  import com.getcapacitor.annotation.CapacitorPlugin;
22
21
  import com.getcapacitor.plugin.WebView;
22
+ import com.google.gson.Gson;
23
+ import com.google.gson.reflect.TypeToken;
23
24
  import io.github.g00fy2.versioncompare.Version;
24
25
  import java.io.IOException;
26
+ import java.lang.reflect.Type;
25
27
  import java.net.MalformedURLException;
26
28
  import java.net.URL;
27
29
  import java.text.SimpleDateFormat;
30
+ import java.util.ArrayList;
28
31
  import java.util.Date;
29
32
  import java.util.Iterator;
30
33
  import java.util.List;
@@ -36,8 +39,8 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
36
39
 
37
40
  private static final String updateUrlDefault = "https://api.capgo.app/updates";
38
41
  private static final String statsUrlDefault = "https://api.capgo.app/stats";
39
- private static final String DELAY_UPDATE = "delayUpdate";
40
- private static final String DELAY_UPDATE_VAL = "delayUpdateVal";
42
+
43
+ private static final String DELAY_CONDITION_PREFERENCES = "";
41
44
 
42
45
  private SharedPreferences.Editor editor;
43
46
  private SharedPreferences prefs;
@@ -50,6 +53,8 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
50
53
  private String updateUrl = "";
51
54
  private Version currentVersionNative;
52
55
  private Boolean resetWhenUpdate = true;
56
+ private Thread backgroundTask;
57
+ private Boolean taskRunning = false;
53
58
 
54
59
  private volatile Thread appReadyCheck;
55
60
 
@@ -89,7 +94,7 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
89
94
  this.implementation.versionOs = Build.VERSION.RELEASE;
90
95
  this.implementation.deviceID = this.prefs.getString("appUUID", UUID.randomUUID().toString());
91
96
  this.editor.putString("appUUID", this.implementation.deviceID);
92
- Log.e(CapacitorUpdater.TAG, "init for device " + this.implementation.deviceID);
97
+ Log.i(CapacitorUpdater.TAG, "init for device " + this.implementation.deviceID);
93
98
 
94
99
  this.autoDeleteFailed = this.getConfig().getBoolean("autoDeleteFailed", true);
95
100
  this.autoDeletePrevious = this.getConfig().getBoolean("autoDeletePrevious", true);
@@ -225,6 +230,7 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
225
230
  this.bridge.setServerBasePath(path);
226
231
  }
227
232
  this.checkAppReady();
233
+ this.notifyListeners("appReloaded", new JSObject());
228
234
  return true;
229
235
  }
230
236
 
@@ -261,7 +267,7 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
261
267
  }
262
268
  } catch (final Exception e) {
263
269
  Log.e(CapacitorUpdater.TAG, "Could not set next id " + id, e);
264
- call.reject("Could not set next id " + id, e);
270
+ call.reject("Could not set next id: " + id, e);
265
271
  }
266
272
  }
267
273
 
@@ -279,7 +285,7 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
279
285
  Log.i(CapacitorUpdater.TAG, "No such bundle " + id);
280
286
  call.reject("Update failed, id " + id + " does not exist.");
281
287
  } else {
282
- Log.i(CapacitorUpdater.TAG, "Bundle successfully set to" + id);
288
+ Log.i(CapacitorUpdater.TAG, "Bundle successfully set to " + id);
283
289
  this.reload(call);
284
290
  }
285
291
  } catch (final Exception e) {
@@ -296,7 +302,7 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
296
302
  call.reject("missing id");
297
303
  return;
298
304
  }
299
- Log.i(CapacitorUpdater.TAG, "Deleting id: " + id);
305
+ Log.i(CapacitorUpdater.TAG, "Deleting id " + id);
300
306
  try {
301
307
  final Boolean res = this.implementation.delete(id);
302
308
  if (res) {
@@ -405,7 +411,7 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
405
411
  try {
406
412
  final BundleInfo bundle = this.implementation.getCurrentBundle();
407
413
  this.implementation.setSuccess(bundle, this.autoDeletePrevious);
408
- Log.i(CapacitorUpdater.TAG, "Current bundle loaded successfully. ['notifyAppReady()' was called] " + bundle.toString());
414
+ Log.i(CapacitorUpdater.TAG, "Current bundle loaded successfully. ['notifyAppReady()' was called] " + bundle);
409
415
  call.resolve();
410
416
  } catch (final Exception e) {
411
417
  Log.e(CapacitorUpdater.TAG, "Failed to notify app ready state. [Error calling 'notifyAppReady()']", e);
@@ -414,32 +420,60 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
414
420
  }
415
421
 
416
422
  @PluginMethod
417
- public void setDelay(final PluginCall call) {
423
+ public void setMultiDelay(final PluginCall call) {
418
424
  try {
419
- final String kind = call.getString("kind");
420
- final String value = call.getString("value");
421
- if (kind == null) {
422
- Log.e(CapacitorUpdater.TAG, "setDelay called without kind");
423
- call.reject("setDelay called without kind");
425
+ final Object delayConditions = call.getData().opt("delayConditions");
426
+ if (delayConditions == null) {
427
+ Log.e(CapacitorUpdater.TAG, "setMultiDelay called without delayCondition");
428
+ call.reject("setMultiDelay called without delayCondition");
424
429
  return;
425
430
  }
426
- this.editor.putString(DELAY_UPDATE, kind);
427
- this.editor.putString(DELAY_UPDATE_VAL, value);
431
+ if (_setMultiDelay(delayConditions.toString())) {
432
+ call.resolve();
433
+ } else {
434
+ call.reject("Failed to delay update");
435
+ }
436
+ } catch (final Exception e) {
437
+ Log.e(CapacitorUpdater.TAG, "Failed to delay update, [Error calling 'setMultiDelay()']", e);
438
+ call.reject("Failed to delay update", e);
439
+ }
440
+ }
441
+
442
+ private Boolean _setMultiDelay(String delayConditions) {
443
+ try {
444
+ this.editor.putString(DELAY_CONDITION_PREFERENCES, delayConditions);
428
445
  this.editor.commit();
429
446
  Log.i(CapacitorUpdater.TAG, "Delay update saved");
430
- call.resolve();
447
+ return true;
448
+ } catch (final Exception e) {
449
+ Log.e(CapacitorUpdater.TAG, "Failed to delay update, [Error calling '_setMultiDelay()']", e);
450
+ return false;
451
+ }
452
+ }
453
+
454
+ @Deprecated
455
+ @PluginMethod
456
+ public void setDelay(final PluginCall call) {
457
+ try {
458
+ String kind = call.getString("kind");
459
+ String value = call.getString("value");
460
+ String delayConditions = "[{\"kind\":\"" + kind + "\", \"value\":\"" + (value != null ? value : "") + "\"}]";
461
+ if (_setMultiDelay(delayConditions)) {
462
+ call.resolve();
463
+ } else {
464
+ call.reject("Failed to delay update");
465
+ }
431
466
  } catch (final Exception e) {
432
- Log.e(CapacitorUpdater.TAG, "Failed to delay update", e);
467
+ Log.e(CapacitorUpdater.TAG, "Failed to delay update, [Error calling 'setDelay()']", e);
433
468
  call.reject("Failed to delay update", e);
434
469
  }
435
470
  }
436
471
 
437
472
  private boolean _cancelDelay(String source) {
438
473
  try {
439
- this.editor.remove(DELAY_UPDATE);
440
- this.editor.remove(DELAY_UPDATE_VAL);
474
+ this.editor.remove(DELAY_CONDITION_PREFERENCES);
441
475
  this.editor.commit();
442
- Log.i(CapacitorUpdater.TAG, "delay canceled from " + source);
476
+ Log.i(CapacitorUpdater.TAG, "All delays canceled from " + source);
443
477
  return true;
444
478
  } catch (final Exception e) {
445
479
  Log.e(CapacitorUpdater.TAG, "Failed to cancel update delay", e);
@@ -457,34 +491,56 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
457
491
  }
458
492
 
459
493
  private void _checkCancelDelay(Boolean killed) {
460
- final String delayUpdate = this.prefs.getString(DELAY_UPDATE, "");
461
- if (!"".equals(delayUpdate)) {
462
- if ("background".equals(delayUpdate) && !killed) {
463
- this._cancelDelay("background check");
464
- } else if ("kill".equals(delayUpdate) && killed) {
465
- this._cancelDelay("kill check");
466
- }
467
- final String delayVal = this.prefs.getString(DELAY_UPDATE_VAL, "");
468
- if ("".equals(delayVal)) {
469
- this._cancelDelay("delayVal absent");
470
- } else if ("date".equals(delayUpdate)) {
471
- try {
472
- final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
473
- Date date = sdf.parse(delayVal);
474
- if (date.compareTo(new Date()) > 0) {
475
- this._cancelDelay("date expired");
476
- }
477
- } catch (final Exception e) {
478
- this._cancelDelay("date parsing issue");
479
- }
480
- } else if ("nativeVersion".equals(delayUpdate)) {
481
- try {
482
- final Version versionLimit = new Version(delayVal);
483
- if (this.currentVersionNative.isAtLeast(versionLimit)) {
484
- this._cancelDelay("nativeVersion above limit");
485
- }
486
- } catch (final Exception e) {
487
- this._cancelDelay("nativeVersion parsing issue");
494
+ Gson gson = new Gson();
495
+ String delayUpdatePreferences = prefs.getString(DELAY_CONDITION_PREFERENCES, "[]");
496
+ Type type = new TypeToken<ArrayList<DelayCondition>>() {}.getType();
497
+ ArrayList<DelayCondition> delayConditionList = gson.fromJson(delayUpdatePreferences, type);
498
+ for (DelayCondition condition : delayConditionList) {
499
+ String kind = condition.getKind().toString();
500
+ String value = condition.getValue();
501
+ if (!"".equals(kind)) {
502
+ switch (kind) {
503
+ case "background":
504
+ if (!killed) {
505
+ this._cancelDelay("background check");
506
+ }
507
+ break;
508
+ case "kill":
509
+ if (killed) {
510
+ this._cancelDelay("kill check");
511
+ this.installNext();
512
+ }
513
+ break;
514
+ case "date":
515
+ if (!"".equals(value)) {
516
+ try {
517
+ final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
518
+ Date date = sdf.parse(value);
519
+ assert date != null;
520
+ if (new Date().compareTo(date) > 0) {
521
+ this._cancelDelay("date expired");
522
+ }
523
+ } catch (final Exception e) {
524
+ this._cancelDelay("date parsing issue");
525
+ }
526
+ } else {
527
+ this._cancelDelay("delayVal absent");
528
+ }
529
+ break;
530
+ case "nativeVersion":
531
+ if (!"".equals(value)) {
532
+ try {
533
+ final Version versionLimit = new Version(value);
534
+ if (this.currentVersionNative.isAtLeast(versionLimit)) {
535
+ this._cancelDelay("nativeVersion above limit");
536
+ }
537
+ } catch (final Exception e) {
538
+ this._cancelDelay("nativeVersion parsing issue");
539
+ }
540
+ } else {
541
+ this._cancelDelay("delayVal absent");
542
+ }
543
+ break;
488
544
  }
489
545
  }
490
546
  }
@@ -580,7 +636,7 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
580
636
  return;
581
637
  }
582
638
  if (latest.isDownloaded()) {
583
- Log.e(
639
+ Log.i(
584
640
  CapacitorUpdater.TAG,
585
641
  "Latest bundle already exists and download is NOT required. Update will occur next time app moves to background."
586
642
  );
@@ -653,16 +709,62 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
653
709
  public void onActivityStopped(@NonNull final Activity activity) {
654
710
  Log.i(CapacitorUpdater.TAG, "Checking for pending update");
655
711
  try {
656
- final String delayUpdate = this.prefs.getString(DELAY_UPDATE, "");
657
- this._checkCancelDelay(false);
658
- if (!"".equals(delayUpdate)) {
712
+ Gson gson = new Gson();
713
+ String delayUpdatePreferences = prefs.getString(DELAY_CONDITION_PREFERENCES, "[]");
714
+ Type type = new TypeToken<ArrayList<DelayCondition>>() {}.getType();
715
+ ArrayList<DelayCondition> delayConditionList = gson.fromJson(delayUpdatePreferences, type);
716
+ String backgroundValue = null;
717
+ for (DelayCondition delayCondition : delayConditionList) {
718
+ if (delayCondition.getKind().toString().equals("background")) {
719
+ String value = delayCondition.getValue();
720
+ backgroundValue = (value != null || !value.equals("")) ? value : "0";
721
+ }
722
+ }
723
+ if (backgroundValue != null) {
724
+ taskRunning = true;
725
+ final Long timeout = Long.parseLong(backgroundValue);
726
+ if(backgroundTask != null) {
727
+ backgroundTask.interrupt();
728
+ }
729
+ backgroundTask = new Thread(
730
+ new Runnable() {
731
+ @Override
732
+ public void run() {
733
+ try {
734
+ Thread.sleep(timeout);
735
+ taskRunning = false;
736
+ _checkCancelDelay(false);
737
+ installNext();
738
+ } catch (InterruptedException e) {
739
+ Log.i(CapacitorUpdater.TAG, "Background Task canceled, Activity resumed before timer completes");
740
+ }
741
+ }
742
+ }
743
+ );
744
+ backgroundTask.start();
745
+ } else {
746
+ this._checkCancelDelay(false);
747
+ this.installNext();
748
+ }
749
+ } catch (final Exception e) {
750
+ Log.e(CapacitorUpdater.TAG, "Error during onActivityStopped", e);
751
+ }
752
+ }
753
+
754
+ private void installNext() {
755
+ try {
756
+ Gson gson = new Gson();
757
+ String delayUpdatePreferences = prefs.getString(DELAY_CONDITION_PREFERENCES, "[]");
758
+ Type type = new TypeToken<ArrayList<DelayCondition>>() {}.getType();
759
+ ArrayList<DelayCondition> delayConditionList = gson.fromJson(delayUpdatePreferences, type);
760
+ if (delayConditionList != null && delayConditionList.size() != 0) {
659
761
  Log.i(CapacitorUpdater.TAG, "Update delayed to next backgrounding");
660
762
  return;
661
763
  }
662
764
  final BundleInfo current = this.implementation.getCurrentBundle();
663
765
  final BundleInfo next = this.implementation.getNextBundle();
664
766
 
665
- if (next != null && !next.isErrorStatus() && next.getId() != current.getId()) {
767
+ if (next != null && !next.isErrorStatus() && !next.getId().equals(current.getId())) {
666
768
  // There is a next bundle waiting for activation
667
769
  Log.d(CapacitorUpdater.TAG, "Next bundle is: " + next.getVersionName());
668
770
  if (this.implementation.set(next) && this._reload()) {
@@ -730,16 +832,15 @@ public class CapacitorUpdaterPlugin extends Plugin implements Application.Activi
730
832
  }
731
833
  }
732
834
 
733
- // not use but necessary here to remove warnings
734
835
  @Override
735
836
  public void onActivityResumed(@NonNull final Activity activity) {
736
- // TODO: Implement background updating based on `backgroundUpdate` and `backgroundUpdateDelay` capacitor.config.ts settings
837
+ if (backgroundTask != null && taskRunning) {
838
+ backgroundTask.interrupt();
839
+ }
737
840
  }
738
841
 
739
842
  @Override
740
- public void onActivityPaused(@NonNull final Activity activity) {
741
- // TODO: Implement background updating based on `backgroundUpdate` and `backgroundUpdateDelay` capacitor.config.ts settings
742
- }
843
+ public void onActivityPaused(@NonNull final Activity activity) {}
743
844
 
744
845
  @Override
745
846
  public void onActivityCreated(@NonNull final Activity activity, @Nullable final Bundle savedInstanceState) {}
@@ -0,0 +1,48 @@
1
+ package ee.forgr.capacitor_updater;
2
+
3
+ import java.util.Objects;
4
+
5
+ public class DelayCondition {
6
+
7
+ private DelayUntilNext kind;
8
+ private String value;
9
+
10
+ public DelayCondition(DelayUntilNext kind, String value) {
11
+ this.kind = kind;
12
+ this.value = value;
13
+ }
14
+
15
+ public DelayUntilNext getKind() {
16
+ return kind;
17
+ }
18
+
19
+ public void setKind(DelayUntilNext kind) {
20
+ this.kind = kind;
21
+ }
22
+
23
+ public String getValue() {
24
+ return value;
25
+ }
26
+
27
+ public void setValue(String value) {
28
+ this.value = value;
29
+ }
30
+
31
+ @Override
32
+ public boolean equals(Object o) {
33
+ if (this == o) return true;
34
+ if (!(o instanceof DelayCondition)) return false;
35
+ DelayCondition that = (DelayCondition) o;
36
+ return getKind() == that.getKind() && Objects.equals(getValue(), that.getValue());
37
+ }
38
+
39
+ @Override
40
+ public int hashCode() {
41
+ return Objects.hash(getKind(), getValue());
42
+ }
43
+
44
+ @Override
45
+ public String toString() {
46
+ return "DelayCondition{" + "kind=" + kind + ", value='" + value + '\'' + '}';
47
+ }
48
+ }
@@ -0,0 +1,8 @@
1
+ package ee.forgr.capacitor_updater;
2
+
3
+ public enum DelayUntilNext {
4
+ background,
5
+ kill,
6
+ nativeVersion,
7
+ date
8
+ }