@capgo/capacitor-updater 8.52.1 → 8.53.1

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.
Files changed (25) hide show
  1. package/README.md +198 -66
  2. package/android/build.gradle +2 -1
  3. package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +414 -150
  4. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +693 -217
  5. package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +2 -1
  6. package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +226 -105
  7. package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +30 -10
  8. package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +324 -0
  9. package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +34 -0
  10. package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
  11. package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +4 -4
  12. package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +1 -1
  13. package/dist/docs.json +378 -27
  14. package/dist/esm/definitions.d.ts +206 -7
  15. package/dist/esm/definitions.js.map +1 -1
  16. package/dist/plugin.cjs.js.map +1 -1
  17. package/dist/plugin.js.map +1 -1
  18. package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +104 -52
  19. package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +360 -104
  20. package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +166 -10
  21. package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +2 -1
  22. package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +202 -90
  23. package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
  24. package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +5 -2
  25. package/package.json +7 -3
@@ -150,11 +150,12 @@ public class CryptoCipher {
150
150
  }
151
151
  return;
152
152
  }
153
+ // The bundle is encrypted from here on: an unusable key must fail, never leave it encrypted.
153
154
  if (!publicKey.startsWith("-----BEGIN RSA PUBLIC KEY-----")) {
154
155
  if (logger != null) {
155
156
  logger.error("The public key is not a valid RSA Public key");
156
157
  }
157
- return;
158
+ throw new IOException("AES file decryption failed: the public key is not a valid RSA public key");
158
159
  }
159
160
 
160
161
  try {
@@ -16,6 +16,9 @@ public class DelayUpdateUtils {
16
16
  private final Logger logger;
17
17
 
18
18
  public static final String DELAY_CONDITION_PREFERENCES = "DELAY_CONDITION_PREFERENCES_CAPGO";
19
+ public static final String DELAY_CONDITION_MODE_PREFERENCES = "DELAY_CONDITION_MODE_PREFERENCES_CAPGO";
20
+ public static final String DELAY_CONDITION_MODE_AND = "and";
21
+ public static final String DELAY_CONDITION_MODE_OR = "or";
19
22
  public static final String BACKGROUND_TIMESTAMP_KEY = "BACKGROUND_TIMESTAMP_KEY_CAPGO";
20
23
 
21
24
  private final SharedPreferences prefs;
@@ -38,128 +41,242 @@ public class DelayUpdateUtils {
38
41
  public void checkCancelDelay(CancelDelaySource source) {
39
42
  String delayUpdatePreferences = prefs.getString(DELAY_CONDITION_PREFERENCES, "[]");
40
43
  ArrayList<DelayCondition> delayConditionList = parseDelayConditions(delayUpdatePreferences);
44
+ if (delayConditionList.isEmpty()) {
45
+ return;
46
+ }
47
+
48
+ final boolean useOrMode = isOrConditionMode();
41
49
  ArrayList<DelayCondition> delayConditionListToKeep = new ArrayList<>(delayConditionList.size());
42
50
  int index = 0;
51
+ boolean anyConditionMet = false;
43
52
 
44
53
  for (DelayCondition condition : delayConditionList) {
45
- DelayUntilNext kind = condition.getKind();
46
- String value = condition.getValue();
47
- switch (kind) {
48
- case DelayUntilNext.background:
49
- if (source == CancelDelaySource.FOREGROUND) {
50
- long backgroundedAt = getBackgroundTimestamp();
51
- long now = System.currentTimeMillis();
52
- long delta = Math.max(0, now - backgroundedAt);
53
- long longValue = 0L;
54
- try {
55
- longValue = Long.parseLong(value);
56
- } catch (NumberFormatException e) {
57
- logger.error(
58
- "Background condition (value: " +
59
- value +
60
- ") had an invalid value at index " +
61
- index +
62
- ". We will likely remove it."
63
- );
64
- }
65
-
66
- if (delta > longValue) {
67
- logger.info(
68
- "Background condition (value: " +
69
- value +
70
- ") deleted at index " +
71
- index +
72
- ". Delta: " +
73
- delta +
74
- ", longValue: " +
75
- longValue
76
- );
77
- } else {
78
- delayConditionListToKeep.add(condition);
79
- logger.info(
80
- "Background delay (value: " +
81
- value +
82
- ") condition kept at index " +
83
- index +
84
- " (source: " +
85
- source.toString() +
86
- ")"
87
- );
88
- }
54
+ ConditionCheckResult result = evaluateCondition(condition, source, index);
55
+ if (result.met) {
56
+ anyConditionMet = true;
57
+ if (!useOrMode) {
58
+ logger.info(result.logMessage);
59
+ }
60
+ } else if (!useOrMode) {
61
+ if (result.keep) {
62
+ delayConditionListToKeep.add(condition);
63
+ }
64
+ if (result.logMessage != null) {
65
+ if (result.error) {
66
+ logger.error(result.logMessage);
89
67
  } else {
90
- delayConditionListToKeep.add(condition);
91
- logger.info(
92
- "Background delay (value: " +
68
+ logger.info(result.logMessage);
69
+ }
70
+ }
71
+ } else if (result.keep) {
72
+ delayConditionListToKeep.add(condition);
73
+ }
74
+ index++;
75
+ }
76
+
77
+ if (useOrMode) {
78
+ if (anyConditionMet || delayConditionListToKeep.isEmpty()) {
79
+ logger.info("Delay condition met in OR mode (source: " + source + "), canceling delay");
80
+ this.cancelDelay("checkCancelDelay");
81
+ } else if (delayConditionListToKeep.size() != delayConditionList.size()) {
82
+ this.setMultiDelay(convertDelayConditionsToJson(delayConditionListToKeep));
83
+ }
84
+ return;
85
+ }
86
+
87
+ if (delayConditionListToKeep.isEmpty()) {
88
+ this.cancelDelay("checkCancelDelay");
89
+ } else {
90
+ this.setMultiDelay(convertDelayConditionsToJson(delayConditionListToKeep));
91
+ }
92
+ }
93
+
94
+ private static final class ConditionCheckResult {
95
+
96
+ final boolean met;
97
+ final boolean keep;
98
+ final boolean error;
99
+ final String logMessage;
100
+
101
+ ConditionCheckResult(boolean met, boolean keep, boolean error, String logMessage) {
102
+ this.met = met;
103
+ this.keep = keep;
104
+ this.error = error;
105
+ this.logMessage = logMessage;
106
+ }
107
+ }
108
+
109
+ private ConditionCheckResult evaluateCondition(DelayCondition condition, CancelDelaySource source, int index) {
110
+ DelayUntilNext kind = condition.getKind();
111
+ String value = condition.getValue();
112
+ switch (kind) {
113
+ case background:
114
+ if (source == CancelDelaySource.FOREGROUND) {
115
+ long backgroundedAt = getBackgroundTimestamp();
116
+ long now = System.currentTimeMillis();
117
+ long delta = Math.max(0, now - backgroundedAt);
118
+ long longValue = 0L;
119
+ try {
120
+ longValue = Long.parseLong(value);
121
+ } catch (NumberFormatException e) {
122
+ return new ConditionCheckResult(
123
+ false,
124
+ false,
125
+ true,
126
+ "Background condition (value: " +
93
127
  value +
94
- ") condition kept at index " +
128
+ ") had an invalid value at index " +
95
129
  index +
96
- " (source: " +
97
- source.toString() +
98
- ")"
130
+ ". We will likely remove it."
99
131
  );
100
132
  }
101
- break;
102
- case DelayUntilNext.kill:
103
- if (source == CancelDelaySource.KILLED) {
104
- logger.info("Kill delay (value: " + value + ") condition removed at index " + index + " after app kill");
105
- } else {
106
- delayConditionListToKeep.add(condition);
107
- logger.info(
108
- "Kill delay (value: " + value + ") condition kept at index " + index + " (source: " + source.toString() + ")"
133
+
134
+ if (delta > longValue) {
135
+ return new ConditionCheckResult(
136
+ true,
137
+ false,
138
+ false,
139
+ "Background condition (value: " +
140
+ value +
141
+ ") deleted at index " +
142
+ index +
143
+ ". Delta: " +
144
+ delta +
145
+ ", longValue: " +
146
+ longValue
109
147
  );
110
148
  }
111
- break;
112
- case DelayUntilNext.date:
113
- if (!"".equals(value)) {
114
- Date date = parseDateCondition(value);
115
- if (date != null) {
116
- if (new Date().compareTo(date) > 0) {
117
- logger.info("Date delay (value: " + value + ") condition removed due to expired date at index " + index);
118
- } else {
119
- delayConditionListToKeep.add(condition);
120
- logger.info("Date delay (value: " + value + ") condition kept at index " + index);
121
- }
122
- } else {
123
- logger.error("Date delay (value: " + value + ") condition removed due to parsing issue at index " + index);
149
+ return new ConditionCheckResult(
150
+ false,
151
+ true,
152
+ false,
153
+ "Background delay (value: " + value + ") condition kept at index " + index + " (source: " + source + ")"
154
+ );
155
+ }
156
+ return new ConditionCheckResult(
157
+ false,
158
+ true,
159
+ false,
160
+ "Background delay (value: " + value + ") condition kept at index " + index + " (source: " + source + ")"
161
+ );
162
+ case kill:
163
+ if (source == CancelDelaySource.KILLED) {
164
+ return new ConditionCheckResult(
165
+ true,
166
+ false,
167
+ false,
168
+ "Kill delay (value: " + value + ") condition removed at index " + index + " after app kill"
169
+ );
170
+ }
171
+ return new ConditionCheckResult(
172
+ false,
173
+ true,
174
+ false,
175
+ "Kill delay (value: " + value + ") condition kept at index " + index + " (source: " + source + ")"
176
+ );
177
+ case date:
178
+ if (!"".equals(value)) {
179
+ Date date = parseDateCondition(value);
180
+ if (date != null) {
181
+ if (new Date().compareTo(date) > 0) {
182
+ return new ConditionCheckResult(
183
+ true,
184
+ false,
185
+ false,
186
+ "Date delay (value: " + value + ") condition removed due to expired date at index " + index
187
+ );
124
188
  }
125
- } else {
126
- logger.debug("Date delay (value: " + value + ") condition removed due to empty value at index " + index);
189
+ return new ConditionCheckResult(
190
+ false,
191
+ true,
192
+ false,
193
+ "Date delay (value: " + value + ") condition kept at index " + index
194
+ );
127
195
  }
128
- break;
129
- case DelayUntilNext.nativeVersion:
130
- if (!"".equals(value)) {
131
- try {
132
- final Version versionLimit = new Version(value);
133
- if (this.currentVersionNative.isAtLeast(versionLimit)) {
134
- logger.info(
135
- "Native version delay (value: " + value + ") condition removed due to above limit at index " + index
136
- );
137
- } else {
138
- delayConditionListToKeep.add(condition);
139
- logger.info("Native version delay (value: " + value + ") condition kept at index " + index);
140
- }
141
- } catch (final Exception e) {
142
- logger.error(
143
- "Native version delay (value: " +
144
- value +
145
- ") condition removed due to parsing issue at index " +
146
- index +
147
- " " +
148
- e.getMessage()
196
+ return new ConditionCheckResult(
197
+ false,
198
+ false,
199
+ true,
200
+ "Date delay (value: " + value + ") condition removed due to parsing issue at index " + index
201
+ );
202
+ }
203
+ return new ConditionCheckResult(
204
+ false,
205
+ false,
206
+ false,
207
+ "Date delay (value: " + value + ") condition removed due to empty value at index " + index
208
+ );
209
+ case nativeVersion:
210
+ if (!"".equals(value)) {
211
+ try {
212
+ final Version versionLimit = new Version(value);
213
+ if (this.currentVersionNative.isAtLeast(versionLimit)) {
214
+ return new ConditionCheckResult(
215
+ true,
216
+ false,
217
+ false,
218
+ "Native version delay (value: " + value + ") condition removed due to above limit at index " + index
149
219
  );
150
220
  }
151
- } else {
152
- logger.debug("Native version delay (value: " + value + ") condition removed due to empty value at index " + index);
221
+ return new ConditionCheckResult(
222
+ false,
223
+ true,
224
+ false,
225
+ "Native version delay (value: " + value + ") condition kept at index " + index
226
+ );
227
+ } catch (final Exception e) {
228
+ return new ConditionCheckResult(
229
+ false,
230
+ false,
231
+ true,
232
+ "Native version delay (value: " +
233
+ value +
234
+ ") condition removed due to parsing issue at index " +
235
+ index +
236
+ " " +
237
+ e.getMessage()
238
+ );
153
239
  }
154
- break;
155
- }
156
- index++;
240
+ }
241
+ return new ConditionCheckResult(
242
+ false,
243
+ false,
244
+ false,
245
+ "Native version delay (value: " + value + ") condition removed due to empty value at index " + index
246
+ );
247
+ default:
248
+ return new ConditionCheckResult(false, false, true, "Unknown delay condition kind at index " + index);
157
249
  }
250
+ }
158
251
 
159
- if (delayConditionListToKeep.isEmpty()) {
160
- this.cancelDelay("checkCancelDelay");
161
- } else {
162
- this.setMultiDelay(convertDelayConditionsToJson(delayConditionListToKeep));
252
+ public boolean isOrConditionMode() {
253
+ return DELAY_CONDITION_MODE_OR.equals(getConditionMode());
254
+ }
255
+
256
+ public String getConditionMode() {
257
+ String mode = prefs.getString(DELAY_CONDITION_MODE_PREFERENCES, DELAY_CONDITION_MODE_AND);
258
+ if (DELAY_CONDITION_MODE_OR.equals(mode)) {
259
+ return DELAY_CONDITION_MODE_OR;
260
+ }
261
+ return DELAY_CONDITION_MODE_AND;
262
+ }
263
+
264
+ public Boolean setConditionMode(String conditionMode) {
265
+ try {
266
+ String normalized = DELAY_CONDITION_MODE_OR.equals(conditionMode) ? DELAY_CONDITION_MODE_OR : DELAY_CONDITION_MODE_AND;
267
+ if (!DELAY_CONDITION_MODE_AND.equals(conditionMode) && !DELAY_CONDITION_MODE_OR.equals(conditionMode)) {
268
+ logger.warn("Unknown delay condition mode '" + conditionMode + "', defaulting to '" + DELAY_CONDITION_MODE_AND + "'");
269
+ }
270
+ this.editor.putString(DELAY_CONDITION_MODE_PREFERENCES, normalized);
271
+ if (!this.editor.commit()) {
272
+ logger.error("Failed to save delay condition mode: commit returned false");
273
+ return false;
274
+ }
275
+ logger.info("Delay condition mode saved: " + normalized);
276
+ return true;
277
+ } catch (final Exception e) {
278
+ logger.error("Failed to save delay condition mode: " + e.getMessage());
279
+ return false;
163
280
  }
164
281
  }
165
282
 
@@ -254,7 +371,10 @@ public class DelayUpdateUtils {
254
371
  public Boolean setMultiDelay(String delayConditions) {
255
372
  try {
256
373
  this.editor.putString(DELAY_CONDITION_PREFERENCES, delayConditions);
257
- this.editor.commit();
374
+ if (!this.editor.commit()) {
375
+ logger.error("Failed to delay update: commit returned false");
376
+ return false;
377
+ }
258
378
  logger.info("Delay update saved");
259
379
  return true;
260
380
  } catch (final Exception e) {
@@ -295,6 +415,7 @@ public class DelayUpdateUtils {
295
415
  public boolean cancelDelay(String source) {
296
416
  try {
297
417
  this.editor.remove(DELAY_CONDITION_PREFERENCES);
418
+ this.editor.remove(DELAY_CONDITION_MODE_PREFERENCES);
298
419
  this.editor.commit();
299
420
  logger.info("All delays canceled from " + source);
300
421
  return true;
@@ -277,8 +277,21 @@ public class DownloadService extends Worker {
277
277
  }
278
278
 
279
279
  private Result createFailureResult(String error) {
280
- Data output = new Data.Builder().putString(ERROR, error).build();
281
- return Result.failure(output);
280
+ return createFailureResult(error, null, null, false);
281
+ }
282
+
283
+ private Result createFailureResult(String error, String version, String dest, boolean isManifest) {
284
+ Data.Builder builder = new Data.Builder().putString(ERROR, error);
285
+ if (version != null) {
286
+ builder.putString(VERSION, version);
287
+ }
288
+ if (dest != null) {
289
+ builder.putString(FILEDEST, dest);
290
+ }
291
+ if (isManifest) {
292
+ builder.putBoolean(IS_MANIFEST, true);
293
+ }
294
+ return Result.failure(builder.build());
282
295
  }
283
296
 
284
297
  private Result createSuccessResult(String dest, String version, String sessionKey, String checksum, boolean isManifest) {
@@ -469,16 +482,16 @@ public class DownloadService extends Worker {
469
482
  @NonNull
470
483
  @Override
471
484
  public Result doWork() {
485
+ final String documentsDir = getInputData().getString(DOCDIR);
486
+ final String dest = getInputData().getString(FILEDEST);
487
+ final String version = getInputData().getString(VERSION);
488
+ final boolean isManifest = getInputData().getBoolean(IS_MANIFEST, false);
472
489
  try {
473
490
  String url = getInputData().getString(URL);
474
491
  String id = getInputData().getString(ID);
475
- String documentsDir = getInputData().getString(DOCDIR);
476
- String dest = getInputData().getString(FILEDEST);
477
- String version = getInputData().getString(VERSION);
478
492
  String sessionKey = getInputData().getString(SESSIONKEY);
479
493
  String checksum = getInputData().getString(CHECKSUM);
480
494
  String publicKey = getInputData().getString(PUBLIC_KEY);
481
- boolean isManifest = getInputData().getBoolean(IS_MANIFEST, false);
482
495
 
483
496
  logger.debug("doWork isManifest: " + isManifest);
484
497
 
@@ -488,10 +501,12 @@ public class DownloadService extends Worker {
488
501
  handleManifestDownload(id, documentsDir, dest, version, sessionKey, publicKey, manifest);
489
502
  return createSuccessResult(dest, version, sessionKey, checksum, true);
490
503
  } else if (isStopped()) {
491
- return createFailureResult("download_cancelled");
504
+ CapgoUpdater.deleteManifestStagingFolderAt(documentsDir != null ? new File(documentsDir) : null, dest, logger);
505
+ return createFailureResult("download_cancelled", version, dest, true);
492
506
  } else {
493
507
  logger.error("Manifest is null");
494
- return createFailureResult("Manifest is null");
508
+ CapgoUpdater.deleteManifestStagingFolderAt(documentsDir != null ? new File(documentsDir) : null, dest, logger);
509
+ return createFailureResult("Manifest is null", version, dest, true);
495
510
  }
496
511
  } else {
497
512
  handleSingleFileDownload(url, id, documentsDir, dest, version, sessionKey, checksum, publicKey);
@@ -503,7 +518,10 @@ public class DownloadService extends Worker {
503
518
  }
504
519
  return Result.retry();
505
520
  } catch (Exception e) {
506
- return createFailureResult(e.getMessage());
521
+ if (isManifest) {
522
+ CapgoUpdater.deleteManifestStagingFolderAt(documentsDir != null ? new File(documentsDir) : null, dest, logger);
523
+ }
524
+ return createFailureResult(e.getMessage(), version, dest, isManifest);
507
525
  }
508
526
  }
509
527
 
@@ -855,7 +873,9 @@ public class DownloadService extends Worker {
855
873
  ZipWritePlan writePlan = planZipResumeWrite(responseCode, downloadedBytes, response.header("Content-Range"));
856
874
  long responseBodyLength = responseBody.contentLength();
857
875
  long contentLength = shouldAppendHttpBody(writePlan.statusCode, writePlan.writeOffset)
858
- ? (responseBodyLength >= 0 ? responseBodyLength + writePlan.writeOffset : -1)
876
+ ? responseBodyLength >= 0
877
+ ? responseBodyLength + writePlan.writeOffset
878
+ : -1
859
879
  : responseBodyLength;
860
880
 
861
881
  // Check if we have enough space for the actual file