@capgo/capacitor-updater 5.2.18 → 5.2.20

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.
@@ -41,13 +41,14 @@ import java.util.Iterator;
41
41
  import java.util.List;
42
42
  import java.util.UUID;
43
43
  import java.util.concurrent.CountDownLatch;
44
+ import java.util.concurrent.Phaser;
45
+ import java.util.concurrent.Semaphore;
44
46
  import java.util.concurrent.TimeUnit;
47
+ import java.util.concurrent.TimeoutException;
45
48
  import org.json.JSONException;
46
49
 
47
50
  @CapacitorPlugin(name = "CapacitorUpdater")
48
- public class CapacitorUpdaterPlugin
49
- extends Plugin
50
- implements Application.ActivityLifecycleCallbacks {
51
+ public class CapacitorUpdaterPlugin extends Plugin {
51
52
 
52
53
  private static final String updateUrlDefault =
53
54
  "https://api.capgo.app/updates";
@@ -57,7 +58,7 @@ public class CapacitorUpdaterPlugin
57
58
  private static final String channelUrlDefault =
58
59
  "https://api.capgo.app/channel_self";
59
60
 
60
- private final String PLUGIN_VERSION = "5.2.18";
61
+ private final String PLUGIN_VERSION = "5.2.20";
61
62
  private static final String DELAY_CONDITION_PREFERENCES = "";
62
63
 
63
64
  private SharedPreferences.Editor editor;
@@ -77,36 +78,34 @@ public class CapacitorUpdaterPlugin
77
78
 
78
79
  private Boolean isPreviousMainActivity = true;
79
80
 
81
+ private volatile Thread backgroundDownloadTask;
80
82
  private volatile Thread appReadyCheck;
81
83
 
82
- private static final CountDownLatch semaphoreReady = new CountDownLatch(0);
84
+ // private static final CountDownLatch semaphoreReady = new CountDownLatch(1);
85
+ private static final Phaser semaphoreReady = new Phaser(1);
83
86
 
84
- public void startNewThread(final Runnable function) {
85
- new Thread(() -> {
87
+ public Thread startNewThread(final Runnable function, Number waitTime) {
88
+ Thread bgTask = new Thread(() -> {
86
89
  try {
90
+ if (waitTime.longValue() > 0) {
91
+ Thread.sleep(waitTime.longValue());
92
+ }
87
93
  function.run();
88
94
  } catch (Exception e) {
89
95
  e.printStackTrace();
90
96
  }
91
- })
92
- .start();
97
+ });
98
+ bgTask.start();
99
+ return bgTask;
100
+ }
101
+
102
+ public Thread startNewThread(final Runnable function) {
103
+ return startNewThread(function, 0);
93
104
  }
94
105
 
95
106
  @Override
96
107
  public void load() {
97
108
  super.load();
98
- startNewThread(() -> {
99
- Log.i(CapacitorUpdater.TAG, "semaphoreReady load");
100
- try {
101
- CapacitorUpdaterPlugin.this.semaphoreReady.await(
102
- CapacitorUpdaterPlugin.this.appReadyTimeout,
103
- TimeUnit.SECONDS
104
- );
105
- } catch (InterruptedException e) {
106
- e.printStackTrace();
107
- }
108
- Log.i(CapacitorUpdater.TAG, "semaphoreReady load done");
109
- });
110
109
  this.prefs =
111
110
  this.getContext()
112
111
  .getSharedPreferences(
@@ -197,30 +196,66 @@ public class CapacitorUpdaterPlugin
197
196
  }
198
197
  final Application application = (Application) this.getContext()
199
198
  .getApplicationContext();
200
- application.registerActivityLifecycleCallbacks(this);
199
+ // application.registerActivityLifecycleCallbacks(this);
201
200
  }
202
201
 
203
- private void directUpdateFinish(final BundleInfo latest) {
202
+ private void semaphoreWait(Number waitTime) {
203
+ Log.i(CapacitorUpdater.TAG, "semaphoreWait " + waitTime);
204
+ try {
205
+ // Log.i(CapacitorUpdater.TAG, "semaphoreReady count " + CapacitorUpdaterPlugin.this.semaphoreReady.getCount());
206
+ CapacitorUpdaterPlugin.this.semaphoreReady.awaitAdvanceInterruptibly(
207
+ CapacitorUpdaterPlugin.this.semaphoreReady.getPhase(),
208
+ waitTime.longValue(),
209
+ TimeUnit.SECONDS
210
+ );
211
+ // Log.i(CapacitorUpdater.TAG, "semaphoreReady await " + res);
212
+ Log.i(
213
+ CapacitorUpdater.TAG,
214
+ "semaphoreReady count " +
215
+ CapacitorUpdaterPlugin.this.semaphoreReady.getPhase()
216
+ );
217
+ } catch (InterruptedException e) {
218
+ Log.i(CapacitorUpdater.TAG, "semaphoreWait InterruptedException");
219
+ e.printStackTrace();
220
+ } catch (TimeoutException e) {
221
+ throw new RuntimeException(e);
222
+ }
223
+ }
224
+
225
+ private void semaphoreUp() {
226
+ Log.i(CapacitorUpdater.TAG, "semaphoreUp");
227
+ CapacitorUpdaterPlugin.this.semaphoreReady.register();
228
+ }
229
+
230
+ private void semaphoreDown() {
231
+ Log.i(CapacitorUpdater.TAG, "semaphoreDown");
232
+ Log.i(
233
+ CapacitorUpdater.TAG,
234
+ "semaphoreDown count " +
235
+ CapacitorUpdaterPlugin.this.semaphoreReady.getPhase()
236
+ );
237
+ CapacitorUpdaterPlugin.this.semaphoreReady.arriveAndDeregister();
238
+ }
239
+
240
+ private void sendReadyToJs(final BundleInfo current, final String msg) {
241
+ Log.i(CapacitorUpdater.TAG, "sendReadyToJs");
204
242
  final JSObject ret = new JSObject();
205
- ret.put("bundle", latest.toJSON());
206
- ret.put("status", "update installed");
207
- CapacitorUpdaterPlugin.this.implementation.set(latest);
208
- CapacitorUpdaterPlugin.this._reload();
243
+ ret.put("bundle", current.toJSON());
244
+ ret.put("status", msg);
209
245
  startNewThread(() -> {
210
- Log.i(CapacitorUpdater.TAG, "semaphoreReady directUpdateFinish");
211
- try {
212
- CapacitorUpdaterPlugin.this.semaphoreReady.await(
213
- CapacitorUpdaterPlugin.this.appReadyTimeout,
214
- TimeUnit.SECONDS
215
- );
216
- } catch (InterruptedException e) {
217
- e.printStackTrace();
218
- }
219
- Log.i(CapacitorUpdater.TAG, "semaphoreReady directUpdateFinish done");
246
+ Log.i(CapacitorUpdater.TAG, "semaphoreReady sendReadyToJs");
247
+ semaphoreWait(CapacitorUpdaterPlugin.this.appReadyTimeout);
248
+ Log.i(CapacitorUpdater.TAG, "semaphoreReady sendReadyToJs done");
220
249
  CapacitorUpdaterPlugin.this.notifyListeners("appReady", ret);
221
250
  });
222
251
  }
223
252
 
253
+ private void directUpdateFinish(final BundleInfo latest) {
254
+ CapacitorUpdaterPlugin.this.implementation.set(latest);
255
+ CapacitorUpdaterPlugin.this._reload();
256
+ sendReadyToJs(latest, "update installed");
257
+ }
258
+
224
259
  private void cleanupObsoleteVersions() {
225
260
  try {
226
261
  final Version previous = new Version(
@@ -457,18 +492,7 @@ public class CapacitorUpdaterPlugin
457
492
 
458
493
  private boolean _reload() {
459
494
  final String path = this.implementation.getCurrentBundlePath();
460
- startNewThread(() -> {
461
- Log.i(CapacitorUpdater.TAG, "semaphoreReady _reload");
462
- try {
463
- CapacitorUpdaterPlugin.this.semaphoreReady.await(
464
- CapacitorUpdaterPlugin.this.appReadyTimeout,
465
- TimeUnit.SECONDS
466
- );
467
- } catch (InterruptedException e) {
468
- e.printStackTrace();
469
- }
470
- Log.i(CapacitorUpdater.TAG, "semaphoreReady _reload done");
471
- });
495
+ this.semaphoreUp();
472
496
  Log.i(CapacitorUpdater.TAG, "Reloading: " + path);
473
497
  if (this.implementation.isUsingBuiltin()) {
474
498
  this.bridge.setServerAssetPath(path);
@@ -676,7 +700,7 @@ public class CapacitorUpdaterPlugin
676
700
  bundle
677
701
  );
678
702
  Log.i(CapacitorUpdater.TAG, "semaphoreReady countDown");
679
- this.semaphoreReady.countDown();
703
+ this.semaphoreDown();
680
704
  Log.i(CapacitorUpdater.TAG, "semaphoreReady countDown done");
681
705
  call.resolve();
682
706
  } catch (final Exception e) {
@@ -852,8 +876,7 @@ public class CapacitorUpdaterPlugin
852
876
  if (this.appReadyCheck != null) {
853
877
  this.appReadyCheck.interrupt();
854
878
  }
855
- this.appReadyCheck = new Thread(new DeferredNotifyAppReadyCheck());
856
- this.appReadyCheck.start();
879
+ this.appReadyCheck = startNewThread(new DeferredNotifyAppReadyCheck());
857
880
  } catch (final Exception e) {
858
881
  Log.e(
859
882
  CapacitorUpdater.TAG,
@@ -879,6 +902,7 @@ public class CapacitorUpdaterPlugin
879
902
  Boolean error
880
903
  ) {
881
904
  if (error) {
905
+ Log.i(CapacitorUpdater.TAG, "endBackGroundTaskWithNotif error" + error);
882
906
  this.implementation.sendStats("download_fail", current.getVersionName());
883
907
  final JSObject ret = new JSObject();
884
908
  ret.put("version", latestVersionName);
@@ -887,257 +911,214 @@ public class CapacitorUpdaterPlugin
887
911
  final JSObject ret = new JSObject();
888
912
  ret.put("bundle", current.toJSON());
889
913
  this.notifyListeners("noNeedUpdate", ret);
890
- ret.put("message", msg);
891
- new Thread(() -> {
892
- try {
893
- Log.i(
894
- CapacitorUpdater.TAG,
895
- "semaphoreReady endBackGroundTaskWithNotif"
896
- );
897
- CapacitorUpdaterPlugin.this.semaphoreReady.await(
898
- CapacitorUpdaterPlugin.this.appReadyTimeout,
899
- TimeUnit.SECONDS
900
- );
901
- Log.i(
902
- CapacitorUpdater.TAG,
903
- "semaphoreReady endBackGroundTaskWithNotif done"
904
- );
905
- } catch (InterruptedException e) {
906
- e.printStackTrace();
907
- }
908
- CapacitorUpdaterPlugin.this.notifyListeners("appReady", ret);
909
- })
910
- .start();
914
+ this.sendReadyToJs(current, msg);
915
+ this.backgroundDownloadTask = null;
911
916
  Log.i(CapacitorUpdater.TAG, "endBackGroundTaskWithNotif " + msg);
912
917
  }
913
918
 
914
- private void backgroundDownload() {
919
+ private Thread backgroundDownload() {
915
920
  String messageUpdate = this.implementation.directUpdate
916
921
  ? "Update will occur now."
917
922
  : "Update will occur next time app moves to background.";
918
- new Thread(
919
- new Runnable() {
920
- @Override
921
- public void run() {
922
- Log.i(
923
- CapacitorUpdater.TAG,
924
- "Check for update via: " + CapacitorUpdaterPlugin.this.updateUrl
925
- );
926
- CapacitorUpdaterPlugin.this.implementation.getLatest(
927
- CapacitorUpdaterPlugin.this.updateUrl,
928
- res -> {
929
- final BundleInfo current =
930
- CapacitorUpdaterPlugin.this.implementation.getCurrentBundle();
931
- try {
932
- if (res.has("message")) {
933
- Log.i(
934
- CapacitorUpdater.TAG,
935
- "API message " + res.get("message")
923
+ return startNewThread(() -> {
924
+ Log.i(
925
+ CapacitorUpdater.TAG,
926
+ "Check for update via: " + CapacitorUpdaterPlugin.this.updateUrl
927
+ );
928
+ CapacitorUpdaterPlugin.this.implementation.getLatest(
929
+ CapacitorUpdaterPlugin.this.updateUrl,
930
+ res -> {
931
+ final BundleInfo current =
932
+ CapacitorUpdaterPlugin.this.implementation.getCurrentBundle();
933
+ try {
934
+ if (res.has("message")) {
935
+ Log.i(
936
+ CapacitorUpdater.TAG,
937
+ "API message " + res.get("message")
938
+ );
939
+ if (
940
+ res.has("major") &&
941
+ res.getBoolean("major") &&
942
+ res.has("version")
943
+ ) {
944
+ final JSObject majorAvailable = new JSObject();
945
+ majorAvailable.put("version", res.getString("version"));
946
+ CapacitorUpdaterPlugin.this.notifyListeners(
947
+ "majorAvailable",
948
+ majorAvailable
936
949
  );
937
- if (
938
- res.has("major") &&
939
- res.getBoolean("major") &&
940
- res.has("version")
941
- ) {
942
- final JSObject majorAvailable = new JSObject();
943
- majorAvailable.put("version", res.getString("version"));
944
- CapacitorUpdaterPlugin.this.notifyListeners(
945
- "majorAvailable",
946
- majorAvailable
947
- );
948
- }
949
- CapacitorUpdaterPlugin.this.endBackGroundTaskWithNotif(
950
- res.getString("message"),
951
- current.getVersionName(),
952
- current,
953
- true
954
- );
955
- return;
956
- }
950
+ }
951
+ CapacitorUpdaterPlugin.this.endBackGroundTaskWithNotif(
952
+ res.getString("message"),
953
+ current.getVersionName(),
954
+ current,
955
+ true
956
+ );
957
+ return;
958
+ }
957
959
 
958
- if (
959
- !res.has("url") ||
960
- !CapacitorUpdaterPlugin.this.isValidURL(
961
- res.getString("url")
962
- )
963
- ) {
964
- Log.e(CapacitorUpdater.TAG, "Error no url or wrong format");
960
+ if (
961
+ !res.has("url") ||
962
+ !CapacitorUpdaterPlugin.this.isValidURL(res.getString("url"))
963
+ ) {
964
+ Log.e(CapacitorUpdater.TAG, "Error no url or wrong format");
965
+ CapacitorUpdaterPlugin.this.endBackGroundTaskWithNotif(
966
+ "Error no url or wrong format",
967
+ current.getVersionName(),
968
+ current,
969
+ true
970
+ );
971
+ return;
972
+ }
973
+ final String latestVersionName = res.getString("version");
974
+
975
+ if (
976
+ latestVersionName != null &&
977
+ !"".equals(latestVersionName) &&
978
+ !current.getVersionName().equals(latestVersionName)
979
+ ) {
980
+ final BundleInfo latest =
981
+ CapacitorUpdaterPlugin.this.implementation.getBundleInfoByName(
982
+ latestVersionName
983
+ );
984
+ if (latest != null) {
985
+ final JSObject ret = new JSObject();
986
+ ret.put("bundle", latest.toJSON());
987
+ if (latest.isErrorStatus()) {
988
+ Log.e(
989
+ CapacitorUpdater.TAG,
990
+ "Latest bundle already exists, and is in error state. Aborting update."
991
+ );
965
992
  CapacitorUpdaterPlugin.this.endBackGroundTaskWithNotif(
966
- "Error no url or wrong format",
967
- current.getVersionName(),
993
+ "Latest bundle already exists, and is in error state. Aborting update.",
994
+ latestVersionName,
968
995
  current,
969
996
  true
970
997
  );
971
998
  return;
972
999
  }
973
- final String latestVersionName = res.getString("version");
974
-
975
- if (
976
- latestVersionName != null &&
977
- !"".equals(latestVersionName) &&
978
- !current.getVersionName().equals(latestVersionName)
979
- ) {
980
- final BundleInfo latest =
981
- CapacitorUpdaterPlugin.this.implementation.getBundleInfoByName(
982
- latestVersionName
1000
+ if (latest.isDownloaded()) {
1001
+ Log.i(
1002
+ CapacitorUpdater.TAG,
1003
+ "Latest bundle already exists and download is NOT required. " +
1004
+ messageUpdate
1005
+ );
1006
+ if (
1007
+ CapacitorUpdaterPlugin.this.implementation.directUpdate
1008
+ ) {
1009
+ CapacitorUpdaterPlugin.this.implementation.set(latest);
1010
+ CapacitorUpdaterPlugin.this._reload();
1011
+ CapacitorUpdaterPlugin.this.endBackGroundTaskWithNotif(
1012
+ "Update installed",
1013
+ latestVersionName,
1014
+ latest,
1015
+ false
983
1016
  );
984
- if (latest != null) {
985
- final JSObject ret = new JSObject();
986
- ret.put("bundle", latest.toJSON());
987
- if (latest.isErrorStatus()) {
988
- Log.e(
989
- CapacitorUpdater.TAG,
990
- "Latest bundle already exists, and is in error state. Aborting update."
1017
+ } else {
1018
+ CapacitorUpdaterPlugin.this.notifyListeners(
1019
+ "updateAvailable",
1020
+ ret
1021
+ );
1022
+ CapacitorUpdaterPlugin.this.implementation.setNextBundle(
1023
+ latest.getId()
1024
+ );
1025
+ CapacitorUpdaterPlugin.this.endBackGroundTaskWithNotif(
1026
+ "update downloaded, will install next background",
1027
+ latestVersionName,
1028
+ latest,
1029
+ false
991
1030
  );
992
- CapacitorUpdaterPlugin.this.endBackGroundTaskWithNotif(
993
- "Latest bundle already exists, and is in error state. Aborting update.",
994
- latestVersionName,
995
- current,
1031
+ }
1032
+ return;
1033
+ }
1034
+ if (latest.isDeleted()) {
1035
+ Log.i(
1036
+ CapacitorUpdater.TAG,
1037
+ "Latest bundle already exists and will be deleted, download will overwrite it."
1038
+ );
1039
+ try {
1040
+ final Boolean deleted =
1041
+ CapacitorUpdaterPlugin.this.implementation.delete(
1042
+ latest.getId(),
996
1043
  true
997
1044
  );
998
- return;
999
- }
1000
- if (latest.isDownloaded()) {
1045
+ if (deleted) {
1001
1046
  Log.i(
1002
1047
  CapacitorUpdater.TAG,
1003
- "Latest bundle already exists and download is NOT required. " +
1004
- messageUpdate
1048
+ "Failed bundle deleted: " + latest.getVersionName()
1005
1049
  );
1006
- if (
1007
- CapacitorUpdaterPlugin.this.implementation.directUpdate
1008
- ) {
1009
- CapacitorUpdaterPlugin.this.implementation.set(
1010
- latest
1011
- );
1012
- CapacitorUpdaterPlugin.this._reload();
1013
- CapacitorUpdaterPlugin.this.endBackGroundTaskWithNotif(
1014
- "Update installed",
1015
- latestVersionName,
1016
- latest,
1017
- false
1018
- );
1019
- } else {
1020
- CapacitorUpdaterPlugin.this.notifyListeners(
1021
- "updateAvailable",
1022
- ret
1023
- );
1024
- CapacitorUpdaterPlugin.this.implementation.setNextBundle(
1025
- latest.getId()
1026
- );
1027
- CapacitorUpdaterPlugin.this.endBackGroundTaskWithNotif(
1028
- "update downloaded, will install next background",
1029
- latestVersionName,
1030
- latest,
1031
- false
1032
- );
1033
- }
1034
- return;
1035
- }
1036
- if (latest.isDeleted()) {
1037
- Log.i(
1038
- CapacitorUpdater.TAG,
1039
- "Latest bundle already exists and will be deleted, download will overwrite it."
1040
- );
1041
- try {
1042
- final Boolean deleted =
1043
- CapacitorUpdaterPlugin.this.implementation.delete(
1044
- latest.getId(),
1045
- true
1046
- );
1047
- if (deleted) {
1048
- Log.i(
1049
- CapacitorUpdater.TAG,
1050
- "Failed bundle deleted: " +
1051
- latest.getVersionName()
1052
- );
1053
- }
1054
- } catch (final IOException e) {
1055
- Log.e(
1056
- CapacitorUpdater.TAG,
1057
- "Failed to delete failed bundle: " +
1058
- latest.getVersionName(),
1059
- e
1060
- );
1061
- }
1062
1050
  }
1051
+ } catch (final IOException e) {
1052
+ Log.e(
1053
+ CapacitorUpdater.TAG,
1054
+ "Failed to delete failed bundle: " +
1055
+ latest.getVersionName(),
1056
+ e
1057
+ );
1063
1058
  }
1064
-
1065
- new Thread(
1066
- new Runnable() {
1067
- @Override
1068
- public void run() {
1069
- try {
1070
- Log.i(
1071
- CapacitorUpdater.TAG,
1072
- "New bundle: " +
1073
- latestVersionName +
1074
- " found. Current is: " +
1075
- current.getVersionName() +
1076
- ". " +
1077
- messageUpdate
1078
- );
1079
-
1080
- final String url = res.getString("url");
1081
- final String sessionKey = res.has("sessionKey")
1082
- ? res.getString("sessionKey")
1083
- : "";
1084
- final String checksum = res.has("checksum")
1085
- ? res.getString("checksum")
1086
- : "";
1087
- CapacitorUpdaterPlugin.this.implementation.downloadBackground(
1088
- url,
1089
- latestVersionName,
1090
- sessionKey,
1091
- checksum
1092
- );
1093
- } catch (final Exception e) {
1094
- Log.e(
1095
- CapacitorUpdater.TAG,
1096
- "error downloading file",
1097
- e
1098
- );
1099
- final BundleInfo current =
1100
- CapacitorUpdaterPlugin.this.implementation.getCurrentBundle();
1101
- CapacitorUpdaterPlugin.this.endBackGroundTaskWithNotif(
1102
- "Error downloading file",
1103
- latestVersionName,
1104
- current,
1105
- true
1106
- );
1107
- }
1108
- }
1109
- }
1110
- )
1111
- .start();
1112
- } else {
1059
+ }
1060
+ }
1061
+ startNewThread(() -> {
1062
+ try {
1113
1063
  Log.i(
1114
1064
  CapacitorUpdater.TAG,
1115
- "No need to update, " +
1116
- current.getId() +
1117
- " is the latest bundle."
1065
+ "New bundle: " +
1066
+ latestVersionName +
1067
+ " found. Current is: " +
1068
+ current.getVersionName() +
1069
+ ". " +
1070
+ messageUpdate
1118
1071
  );
1072
+
1073
+ final String url = res.getString("url");
1074
+ final String sessionKey = res.has("sessionKey")
1075
+ ? res.getString("sessionKey")
1076
+ : "";
1077
+ final String checksum = res.has("checksum")
1078
+ ? res.getString("checksum")
1079
+ : "";
1080
+ CapacitorUpdaterPlugin.this.implementation.downloadBackground(
1081
+ url,
1082
+ latestVersionName,
1083
+ sessionKey,
1084
+ checksum
1085
+ );
1086
+ } catch (final Exception e) {
1087
+ Log.e(CapacitorUpdater.TAG, "error downloading file", e);
1119
1088
  CapacitorUpdaterPlugin.this.endBackGroundTaskWithNotif(
1120
- "No need to update",
1089
+ "Error downloading file",
1121
1090
  latestVersionName,
1122
- current,
1123
- false
1091
+ CapacitorUpdaterPlugin.this.implementation.getCurrentBundle(),
1092
+ true
1124
1093
  );
1125
1094
  }
1126
- } catch (final JSONException e) {
1127
- Log.e(CapacitorUpdater.TAG, "error parsing JSON", e);
1128
- CapacitorUpdaterPlugin.this.endBackGroundTaskWithNotif(
1129
- "Error parsing JSON",
1130
- current.getVersionName(),
1131
- current,
1132
- true
1133
- );
1134
- }
1095
+ });
1096
+ } else {
1097
+ Log.i(
1098
+ CapacitorUpdater.TAG,
1099
+ "No need to update, " +
1100
+ current.getId() +
1101
+ " is the latest bundle."
1102
+ );
1103
+ CapacitorUpdaterPlugin.this.endBackGroundTaskWithNotif(
1104
+ "No need to update",
1105
+ latestVersionName,
1106
+ current,
1107
+ false
1108
+ );
1135
1109
  }
1136
- );
1137
- }
1138
- }
1139
- )
1140
- .start();
1110
+ } catch (final JSONException e) {
1111
+ Log.e(CapacitorUpdater.TAG, "error parsing JSON", e);
1112
+ CapacitorUpdaterPlugin.this.endBackGroundTaskWithNotif(
1113
+ "Error parsing JSON",
1114
+ current.getVersionName(),
1115
+ current,
1116
+ true
1117
+ );
1118
+ }
1119
+ }
1120
+ );
1121
+ });
1141
1122
  }
1142
1123
 
1143
1124
  private void installNext() {
@@ -1273,27 +1254,14 @@ public class CapacitorUpdaterPlugin
1273
1254
  current.getVersionName()
1274
1255
  );
1275
1256
  this._checkCancelDelay(true);
1276
- if (CapacitorUpdaterPlugin.this._isAutoUpdateEnabled()) {
1277
- this.backgroundDownload();
1257
+ if (
1258
+ CapacitorUpdaterPlugin.this._isAutoUpdateEnabled() &&
1259
+ this.backgroundDownloadTask == null
1260
+ ) {
1261
+ this.backgroundDownloadTask = this.backgroundDownload();
1278
1262
  } else {
1279
1263
  Log.i(CapacitorUpdater.TAG, "Auto update is disabled");
1280
- new Thread(() -> {
1281
- try {
1282
- Log.i(CapacitorUpdater.TAG, "semaphoreReady Auto update");
1283
- CapacitorUpdaterPlugin.this.semaphoreReady.await(
1284
- CapacitorUpdaterPlugin.this.appReadyTimeout,
1285
- TimeUnit.SECONDS
1286
- );
1287
- Log.i(CapacitorUpdater.TAG, "semaphoreReady Auto update done");
1288
- } catch (InterruptedException e) {
1289
- e.printStackTrace();
1290
- }
1291
- final JSObject ret = new JSObject();
1292
- ret.put("bundle", current.toJSON());
1293
- ret.put("status", "disabled");
1294
- CapacitorUpdaterPlugin.this.notifyListeners("appReady", ret);
1295
- })
1296
- .start();
1264
+ this.sendReadyToJs(current, "disabled");
1297
1265
  }
1298
1266
  this.checkAppReady();
1299
1267
  }
@@ -1331,25 +1299,14 @@ public class CapacitorUpdaterPlugin
1331
1299
  backgroundTask.interrupt();
1332
1300
  }
1333
1301
  backgroundTask =
1334
- new Thread(
1335
- new Runnable() {
1336
- @Override
1337
- public void run() {
1338
- try {
1339
- Thread.sleep(timeout);
1340
- taskRunning = false;
1341
- _checkCancelDelay(false);
1342
- installNext();
1343
- } catch (InterruptedException e) {
1344
- Log.i(
1345
- CapacitorUpdater.TAG,
1346
- "Background Task canceled, Activity resumed before timer completes"
1347
- );
1348
- }
1349
- }
1350
- }
1302
+ startNewThread(
1303
+ () -> {
1304
+ taskRunning = false;
1305
+ _checkCancelDelay(false);
1306
+ installNext();
1307
+ },
1308
+ timeout
1351
1309
  );
1352
- backgroundTask.start();
1353
1310
  } else {
1354
1311
  this._checkCancelDelay(false);
1355
1312
  this.installNext();
@@ -1387,15 +1344,24 @@ public class CapacitorUpdaterPlugin
1387
1344
  }
1388
1345
 
1389
1346
  @Override
1390
- public void onActivityStarted(@NonNull final Activity activity) {
1347
+ public void handleOnStart() {
1348
+ this.counterActivityCreate++;
1349
+ // @Override
1350
+ // public void onActivityStarted(@NonNull final Activity activity) {
1391
1351
  if (isPreviousMainActivity) {
1392
1352
  this.appMovedToForeground();
1393
1353
  }
1354
+ Log.i(
1355
+ CapacitorUpdater.TAG,
1356
+ "onActivityStarted " + getActivity().getClass().getName()
1357
+ );
1394
1358
  isPreviousMainActivity = true;
1395
1359
  }
1396
1360
 
1397
1361
  @Override
1398
- public void onActivityStopped(@NonNull final Activity activity) {
1362
+ public void handleOnStop() {
1363
+ // @Override
1364
+ // public void onActivityStopped(@NonNull final Activity activity) {
1399
1365
  isPreviousMainActivity = isMainActivity();
1400
1366
  if (isPreviousMainActivity) {
1401
1367
  this.appMovedToBackground();
@@ -1403,40 +1369,52 @@ public class CapacitorUpdaterPlugin
1403
1369
  }
1404
1370
 
1405
1371
  @Override
1406
- public void onActivityResumed(@NonNull final Activity activity) {
1372
+ public void handleOnResume() {
1373
+ // @Override
1374
+ // public void onActivityResumed(@NonNull final Activity activity) {
1407
1375
  if (backgroundTask != null && taskRunning) {
1408
1376
  backgroundTask.interrupt();
1409
1377
  }
1410
- this.implementation.activity = activity;
1378
+ this.implementation.activity = getActivity();
1411
1379
  this.implementation.onResume();
1412
1380
  }
1413
1381
 
1414
1382
  @Override
1415
- public void onActivityPaused(@NonNull final Activity activity) {
1416
- this.implementation.activity = activity;
1383
+ public void handleOnPause() {
1384
+ // @Override
1385
+ // public void onActivityPaused(@NonNull final Activity activity) {
1386
+ this.implementation.activity = getActivity();
1417
1387
  this.implementation.onPause();
1418
1388
  }
1419
1389
 
1420
- @Override
1421
- public void onActivityCreated(
1422
- @NonNull final Activity activity,
1423
- @Nullable final Bundle savedInstanceState
1424
- ) {
1425
- this.implementation.activity = activity;
1426
- this.counterActivityCreate++;
1427
- }
1390
+ // @Override
1391
+ // public void handleOnDestroy() {
1392
+ // @Override
1393
+ // public void onActivityCreated(
1394
+ // @NonNull final Activity activity,
1395
+ // @Nullable final Bundle savedInstanceState
1396
+ // ) {
1397
+ // this.implementation.activity = activity;
1398
+ // this.counterActivityCreate++;
1399
+ // }
1400
+ //
1401
+ // @Override
1402
+ // public void onActivitySaveInstanceState(
1403
+ // @NonNull final Activity activity,
1404
+ // @NonNull final Bundle outState
1405
+ // ) {
1406
+ // this.implementation.activity = activity;
1407
+ // }
1428
1408
 
1429
1409
  @Override
1430
- public void onActivitySaveInstanceState(
1431
- @NonNull final Activity activity,
1432
- @NonNull final Bundle outState
1433
- ) {
1434
- this.implementation.activity = activity;
1435
- }
1436
-
1437
- @Override
1438
- public void onActivityDestroyed(@NonNull final Activity activity) {
1439
- this.implementation.activity = activity;
1410
+ public void handleOnDestroy() {
1411
+ // @Override
1412
+ // public void onActivityDestroyed(@NonNull final Activity activity) {
1413
+ Log.i(
1414
+ CapacitorUpdater.TAG,
1415
+ "onActivityDestroyed " + getActivity().getClass().getName()
1416
+ );
1417
+ this.implementation.activity = getActivity();
1440
1418
  counterActivityCreate--;
1441
1419
  if (counterActivityCreate == 0) {
1442
1420
  this.appKilled();
@@ -15,7 +15,7 @@ import Version
15
15
  @objc(CapacitorUpdaterPlugin)
16
16
  public class CapacitorUpdaterPlugin: CAPPlugin {
17
17
  private var implementation = CapacitorUpdater()
18
- private let PLUGIN_VERSION: String = "5.2.18"
18
+ private let PLUGIN_VERSION: String = "5.2.20"
19
19
  static let updateUrlDefault = "https://api.capgo.app/updates"
20
20
  static let statsUrlDefault = "https://api.capgo.app/stats"
21
21
  static let channelUrlDefault = "https://api.capgo.app/channel_self"
@@ -37,7 +37,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
37
37
  let semaphoreReady = DispatchSemaphore(value: 0)
38
38
 
39
39
  override public func load() {
40
- _ = semaphoreReady.wait(timeout: .now())
40
+ self.semaphoreUp()
41
41
  print("\(self.implementation.TAG) init for device \(self.implementation.deviceID)")
42
42
  do {
43
43
  currentVersionNative = try Version(getConfig().getString("version", Bundle.main.versionName ?? "0.0.0")!)
@@ -73,6 +73,23 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
73
73
  self.appMovedToForeground()
74
74
  }
75
75
 
76
+ private func semaphoreWait(waitTime: Int) {
77
+ print("\(self.implementation.TAG) semaphoreWait \(waitTime)")
78
+ _ = semaphoreReady.wait(timeout: .now() + .milliseconds(waitTime))
79
+ }
80
+
81
+ private func semaphoreUp() {
82
+ print("\(self.implementation.TAG) semaphoreUp")
83
+ DispatchQueue.global().async {
84
+ self.semaphoreWait(waitTime: 0)
85
+ }
86
+ }
87
+
88
+ private func semaphoreDown() {
89
+ print("\(self.implementation.TAG) semaphoreDown")
90
+ semaphoreReady.signal()
91
+ }
92
+
76
93
  private func cleanupObsoleteVersions() {
77
94
  var LatestVersionNative: Version = "0.0.0"
78
95
  do {
@@ -160,7 +177,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
160
177
 
161
178
  private func _reload() -> Bool {
162
179
  guard let bridge = self.bridge else { return false }
163
- _ = self.semaphoreReady.wait(timeout: .now())
180
+ self.semaphoreUp()
164
181
  let id = self.implementation.getCurrentBundleId()
165
182
  let destHot = self.implementation.getPathHot(id: id)
166
183
  print("\(self.implementation.TAG) Reloading \(id)")
@@ -329,7 +346,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
329
346
  }
330
347
 
331
348
  @objc func notifyAppReady(_ call: CAPPluginCall) {
332
- self.semaphoreReady.signal()
349
+ self.semaphoreDown()
333
350
  let version = self.implementation.getCurrentBundle()
334
351
  self.implementation.setSuccess(bundle: version, autoDeletePrevious: self.autoDeletePrevious)
335
352
  print("\(self.implementation.TAG) Current bundle loaded successfully. ['notifyAppReady()' was called] \(version.toString())")
@@ -502,17 +519,21 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
502
519
  self.backgroundTaskID = UIBackgroundTaskIdentifier.invalid
503
520
  }
504
521
 
522
+ func sendReadyToJs(current: BundleInfo, msg: String) {
523
+ print("\(self.implementation.TAG) sendReadyToJs")
524
+ DispatchQueue.global().async {
525
+ self.semaphoreWait(waitTime: self.appReadyTimeout)
526
+ self.notifyListeners("appReady", data: ["bundle": current.toJSON(), "status": msg])
527
+ }
528
+ }
529
+
505
530
  func endBackGroundTaskWithNotif(msg: String, latestVersionName: String, current: BundleInfo, error: Bool = true) {
506
531
  if error {
507
532
  self.implementation.sendStats(action: "download_fail", versionName: current.getVersionName())
508
533
  self.notifyListeners("downloadFailed", data: ["version": latestVersionName])
509
534
  }
510
535
  self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
511
-
512
- DispatchQueue.global().async {
513
- _ = self.semaphoreReady.wait(timeout: .now() + .milliseconds(self.appReadyTimeout))
514
- self.notifyListeners("appReady", data: ["bundle": current.toJSON(), "message": msg])
515
- }
536
+ self.sendReadyToJs(current: current, msg: msg)
516
537
  print("\(self.implementation.TAG) endBackGroundTaskWithNotif \(msg)")
517
538
  self.endBackGroundTask()
518
539
  }
@@ -663,10 +684,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
663
684
  self.backgroundDownload()
664
685
  } else {
665
686
  print("\(self.implementation.TAG) Auto update is disabled")
666
- DispatchQueue.global().async {
667
- _ = self.semaphoreReady.wait(timeout: .now() + .milliseconds(self.appReadyTimeout))
668
- self.notifyListeners("appReady", data: ["bundle": current.toJSON(), "status": "disabled"])
669
- }
687
+ self.sendReadyToJs(current: current, msg: "disabled")
670
688
  }
671
689
  self.checkAppReady()
672
690
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-updater",
3
- "version": "5.2.18",
3
+ "version": "5.2.20",
4
4
  "packageManager": "pnpm@8.6.12",
5
5
  "license": "MPL-2.0",
6
6
  "description": "Live update for capacitor apps",