@capgo/capacitor-updater 9.0.0 → 9.0.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 (109) hide show
  1. package/CapgoCapacitorUpdater.podspec +3 -6
  2. package/Package.swift +30 -0
  3. package/README.md +2382 -292
  4. package/android/build.gradle +37 -15
  5. package/android/consumer-rules.pro +48 -0
  6. package/android/proguard-rules.pro +23 -0
  7. package/android/src/main/AndroidManifest.xml +0 -1
  8. package/android/src/main/java/ee/forgr/capacitor_updater/AndroidAppExitReporter.java +92 -0
  9. package/android/src/main/java/ee/forgr/capacitor_updater/AppLifecycleObserver.java +115 -0
  10. package/android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java +227 -195
  11. package/android/src/main/java/ee/forgr/capacitor_updater/BundleStatus.java +24 -23
  12. package/android/src/main/java/ee/forgr/capacitor_updater/Callback.java +13 -0
  13. package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +7435 -1243
  14. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +5144 -0
  15. package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +564 -121
  16. package/android/src/main/java/ee/forgr/capacitor_updater/DataManager.java +44 -0
  17. package/android/src/main/java/ee/forgr/capacitor_updater/DelayCondition.java +43 -49
  18. package/android/src/main/java/ee/forgr/capacitor_updater/DelayUntilNext.java +4 -4
  19. package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +427 -0
  20. package/android/src/main/java/ee/forgr/capacitor_updater/DeviceIdHelper.java +230 -0
  21. package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +1969 -118
  22. package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +278 -0
  23. package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +385 -0
  24. package/android/src/main/java/ee/forgr/capacitor_updater/Http3.java +356 -0
  25. package/android/src/main/java/ee/forgr/capacitor_updater/HttpEngineTransport.java +553 -0
  26. package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +79 -0
  27. package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +419 -0
  28. package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
  29. package/android/src/main/java/ee/forgr/capacitor_updater/ShakeDetector.java +72 -0
  30. package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +959 -0
  31. package/android/src/main/java/ee/forgr/capacitor_updater/StatsBatchFailureDiagnostics.java +40 -0
  32. package/android/src/main/java/ee/forgr/capacitor_updater/ThreeFingerPinchDetector.java +323 -0
  33. package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +306 -0
  34. package/dist/docs.json +4431 -840
  35. package/dist/esm/definitions.d.ts +2607 -225
  36. package/dist/esm/definitions.js +103 -1
  37. package/dist/esm/definitions.js.map +1 -1
  38. package/dist/esm/history.d.ts +1 -0
  39. package/dist/esm/history.js +283 -0
  40. package/dist/esm/history.js.map +1 -0
  41. package/dist/esm/index.d.ts +3 -2
  42. package/dist/esm/index.js +5 -4
  43. package/dist/esm/index.js.map +1 -1
  44. package/dist/esm/web.d.ts +53 -42
  45. package/dist/esm/web.js +180 -37
  46. package/dist/esm/web.js.map +1 -1
  47. package/dist/plugin.cjs.js +570 -37
  48. package/dist/plugin.cjs.js.map +1 -1
  49. package/dist/plugin.js +570 -37
  50. package/dist/plugin.js.map +1 -1
  51. package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +262 -0
  52. package/ios/Sources/CapacitorUpdaterPlugin/AppHealthTracker.swift +82 -0
  53. package/ios/Sources/CapacitorUpdaterPlugin/BundleInfo.swift +185 -0
  54. package/ios/Sources/CapacitorUpdaterPlugin/BundleStatus.swift +127 -0
  55. package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +5642 -0
  56. package/ios/Sources/CapacitorUpdaterPlugin/CapgoRawRsa.swift +36 -0
  57. package/ios/Sources/CapacitorUpdaterPlugin/CapgoSemanticVersion.swift +162 -0
  58. package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +4572 -0
  59. package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +380 -0
  60. package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +343 -0
  61. package/ios/Sources/CapacitorUpdaterPlugin/DeviceIdHelper.swift +120 -0
  62. package/ios/Sources/CapacitorUpdaterPlugin/InternalUtils.swift +452 -0
  63. package/ios/Sources/CapacitorUpdaterPlugin/Logger.swift +380 -0
  64. package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
  65. package/ios/Sources/CapacitorUpdaterPlugin/RSA.swift +96 -0
  66. package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +97 -0
  67. package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +871 -0
  68. package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/UserDefaultsExtension.swift +0 -2
  69. package/ios/Sources/CapacitorUpdaterPlugin/WKWebView+CapgoObjcBridge.swift +20 -0
  70. package/ios/Sources/CapacitorUpdaterPlugin/WebViewStatsReporter.swift +304 -0
  71. package/ios/Sources/CapacitorUpdaterPlugin/ZipArchiveReader.swift +247 -0
  72. package/ios/Sources/CapacitorUpdaterPlugin/ZipCentralDirectory.swift +280 -0
  73. package/ios/Tests/CapacitorUpdaterPluginTests/BuiltinHashesTests.swift +210 -0
  74. package/ios/Tests/CapacitorUpdaterPluginTests/BundleHardeningTests.swift +292 -0
  75. package/ios/Tests/CapacitorUpdaterPluginTests/CapacitorUpdaterPluginTests.swift +3905 -0
  76. package/ios/Tests/CapacitorUpdaterPluginTests/CapgoSemanticVersionTests.swift +64 -0
  77. package/ios/Tests/CapacitorUpdaterPluginTests/ChecksumRequiredTests.swift +151 -0
  78. package/ios/Tests/CapacitorUpdaterPluginTests/CliCryptoFixtureTests.swift +249 -0
  79. package/ios/Tests/CapacitorUpdaterPluginTests/HttpsOnlyBuiltinMinimumTests.swift +528 -0
  80. package/ios/Tests/CapacitorUpdaterPluginTests/InstallPerfTests.swift +174 -0
  81. package/ios/Tests/CapacitorUpdaterPluginTests/ListChannelsTests.swift +74 -0
  82. package/ios/Tests/CapacitorUpdaterPluginTests/LoggerSecurityTests.swift +149 -0
  83. package/ios/Tests/CapacitorUpdaterPluginTests/NativeContractTests.swift +160 -0
  84. package/ios/Tests/CapacitorUpdaterPluginTests/NetworkTransportTests.swift +1095 -0
  85. package/ios/Tests/CapacitorUpdaterPluginTests/PopulateDeltaCacheTests.swift +573 -0
  86. package/ios/Tests/CapacitorUpdaterPluginTests/RedirectPolicyTests.swift +63 -0
  87. package/ios/Tests/CapacitorUpdaterPluginTests/RsaContractTests.swift +281 -0
  88. package/ios/Tests/CapacitorUpdaterPluginTests/SessionKeyRequiredTests.swift +192 -0
  89. package/ios/Tests/CapacitorUpdaterPluginTests/TestZipWriter.swift +235 -0
  90. package/ios/Tests/CapacitorUpdaterPluginTests/UpdateLifecycleTests.swift +454 -0
  91. package/ios/Tests/CapacitorUpdaterPluginTests/WebKitOverlayGuardTests.swift +32 -0
  92. package/ios/Tests/CapacitorUpdaterPluginTests/ZipArchiveReaderTests.swift +302 -0
  93. package/ios/Tests/CapacitorUpdaterPluginTests/ZipBundleExtractionTests.swift +122 -0
  94. package/package.json +80 -43
  95. package/scripts/builtin-hashes-lib.mjs +148 -0
  96. package/scripts/capacitor-builtin-hashes-hook.mjs +65 -0
  97. package/scripts/resolve-real-path.mjs +25 -0
  98. package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java +0 -1130
  99. package/ios/Plugin/BundleInfo.swift +0 -113
  100. package/ios/Plugin/BundleStatus.swift +0 -48
  101. package/ios/Plugin/CapacitorUpdater.swift +0 -858
  102. package/ios/Plugin/CapacitorUpdaterPlugin.h +0 -10
  103. package/ios/Plugin/CapacitorUpdaterPlugin.m +0 -27
  104. package/ios/Plugin/CapacitorUpdaterPlugin.swift +0 -677
  105. package/ios/Plugin/CryptoCipher.swift +0 -240
  106. /package/{LICENCE → LICENSE} +0 -0
  107. /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayCondition.swift +0 -0
  108. /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayUntilNext.swift +0 -0
  109. /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/Info.plist +0 -0
@@ -0,0 +1,44 @@
1
+ package ee.forgr.capacitor_updater;
2
+
3
+ import java.util.HashMap;
4
+ import java.util.Map;
5
+ import org.json.JSONArray;
6
+
7
+ public class DataManager {
8
+
9
+ private static DataManager instance;
10
+ private final Map<String, JSONArray> manifestsById = new HashMap<>();
11
+
12
+ private DataManager() {}
13
+
14
+ public static synchronized DataManager getInstance() {
15
+ if (instance == null) {
16
+ instance = new DataManager();
17
+ }
18
+ return instance;
19
+ }
20
+
21
+ public synchronized void setManifest(String downloadId, JSONArray manifest) {
22
+ if (downloadId == null || manifest == null) {
23
+ return;
24
+ }
25
+ this.manifestsById.put(downloadId, manifest);
26
+ }
27
+
28
+ public synchronized JSONArray getAndClearManifest(String downloadId) {
29
+ if (downloadId == null) {
30
+ return null;
31
+ }
32
+ return this.manifestsById.remove(downloadId);
33
+ }
34
+
35
+ public synchronized void clearManifest(String downloadId) {
36
+ if (downloadId != null) {
37
+ this.manifestsById.remove(downloadId);
38
+ }
39
+ }
40
+
41
+ public synchronized void clearAllManifests() {
42
+ this.manifestsById.clear();
43
+ }
44
+ }
@@ -6,57 +6,51 @@
6
6
 
7
7
  package ee.forgr.capacitor_updater;
8
8
 
9
- import com.google.gson.annotations.SerializedName;
9
+ import androidx.annotation.NonNull;
10
10
  import java.util.Objects;
11
11
 
12
12
  public class DelayCondition {
13
13
 
14
- @SerializedName("kind")
15
- private DelayUntilNext kind;
16
-
17
- @SerializedName("value")
18
- private String value;
19
-
20
- public DelayCondition(DelayUntilNext kind, String value) {
21
- this.kind = kind;
22
- this.value = value;
23
- }
24
-
25
- public DelayUntilNext getKind() {
26
- return kind;
27
- }
28
-
29
- public void setKind(DelayUntilNext kind) {
30
- this.kind = kind;
31
- }
32
-
33
- public String getValue() {
34
- return value;
35
- }
36
-
37
- public void setValue(String value) {
38
- this.value = value;
39
- }
40
-
41
- @Override
42
- public boolean equals(Object o) {
43
- if (this == o) return true;
44
- if (!(o instanceof DelayCondition)) return false;
45
- DelayCondition that = (DelayCondition) o;
46
- return (
47
- getKind() == that.getKind() && Objects.equals(getValue(), that.getValue())
48
- );
49
- }
50
-
51
- @Override
52
- public int hashCode() {
53
- return Objects.hash(getKind(), getValue());
54
- }
55
-
56
- @Override
57
- public String toString() {
58
- return (
59
- "DelayCondition{" + "kind=" + kind + ", value='" + value + '\'' + '}'
60
- );
61
- }
14
+ private DelayUntilNext kind;
15
+
16
+ private String value;
17
+
18
+ public DelayCondition(DelayUntilNext kind, String value) {
19
+ this.kind = kind;
20
+ this.value = value;
21
+ }
22
+
23
+ public DelayUntilNext getKind() {
24
+ return kind;
25
+ }
26
+
27
+ public void setKind(DelayUntilNext kind) {
28
+ this.kind = kind;
29
+ }
30
+
31
+ public String getValue() {
32
+ return value;
33
+ }
34
+
35
+ public void setValue(String value) {
36
+ this.value = value;
37
+ }
38
+
39
+ @Override
40
+ public boolean equals(Object o) {
41
+ if (this == o) return true;
42
+ if (!(o instanceof DelayCondition that)) return false;
43
+ return getKind() == that.getKind() && Objects.equals(getValue(), that.getValue());
44
+ }
45
+
46
+ @Override
47
+ public int hashCode() {
48
+ return Objects.hash(getKind(), getValue());
49
+ }
50
+
51
+ @NonNull
52
+ @Override
53
+ public String toString() {
54
+ return "DelayCondition{" + "kind=" + kind + ", value='" + value + '\'' + '}';
55
+ }
62
56
  }
@@ -7,8 +7,8 @@
7
7
  package ee.forgr.capacitor_updater;
8
8
 
9
9
  public enum DelayUntilNext {
10
- background,
11
- kill,
12
- nativeVersion,
13
- date,
10
+ background,
11
+ kill,
12
+ nativeVersion,
13
+ date
14
14
  }
@@ -0,0 +1,427 @@
1
+ package ee.forgr.capacitor_updater;
2
+
3
+ import android.content.SharedPreferences;
4
+ import java.text.ParsePosition;
5
+ import java.text.SimpleDateFormat;
6
+ import java.util.ArrayList;
7
+ import java.util.Date;
8
+ import java.util.Locale;
9
+ import java.util.TimeZone;
10
+ import org.json.JSONArray;
11
+ import org.json.JSONException;
12
+ import org.json.JSONObject;
13
+
14
+ public class DelayUpdateUtils {
15
+
16
+ private final Logger logger;
17
+
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";
22
+ public static final String BACKGROUND_TIMESTAMP_KEY = "BACKGROUND_TIMESTAMP_KEY_CAPGO";
23
+
24
+ private final SharedPreferences prefs;
25
+ private final SharedPreferences.Editor editor;
26
+ private final Version currentVersionNative;
27
+
28
+ public DelayUpdateUtils(SharedPreferences prefs, SharedPreferences.Editor editor, Version currentVersionNative, Logger logger) {
29
+ this.prefs = prefs;
30
+ this.editor = editor;
31
+ this.currentVersionNative = currentVersionNative;
32
+ this.logger = logger;
33
+ }
34
+
35
+ public enum CancelDelaySource {
36
+ KILLED,
37
+ BACKGROUND,
38
+ FOREGROUND
39
+ }
40
+
41
+ public void checkCancelDelay(CancelDelaySource source) {
42
+ String delayUpdatePreferences = prefs.getString(DELAY_CONDITION_PREFERENCES, "[]");
43
+ ArrayList<DelayCondition> delayConditionList = parseDelayConditions(delayUpdatePreferences);
44
+ if (delayConditionList.isEmpty()) {
45
+ return;
46
+ }
47
+
48
+ final boolean useOrMode = isOrConditionMode();
49
+ ArrayList<DelayCondition> delayConditionListToKeep = new ArrayList<>(delayConditionList.size());
50
+ int index = 0;
51
+ boolean anyConditionMet = false;
52
+
53
+ for (DelayCondition condition : delayConditionList) {
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);
67
+ } else {
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: " +
127
+ value +
128
+ ") had an invalid value at index " +
129
+ index +
130
+ ". We will likely remove it."
131
+ );
132
+ }
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
147
+ );
148
+ }
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
+ );
188
+ }
189
+ return new ConditionCheckResult(
190
+ false,
191
+ true,
192
+ false,
193
+ "Date delay (value: " + value + ") condition kept at index " + index
194
+ );
195
+ }
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
219
+ );
220
+ }
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
+ );
239
+ }
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);
249
+ }
250
+ }
251
+
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;
280
+ }
281
+ }
282
+
283
+ public ArrayList<DelayCondition> parseDelayConditions(String json) {
284
+ ArrayList<DelayCondition> conditions = new ArrayList<>();
285
+ if (json == null || json.isEmpty()) {
286
+ return conditions;
287
+ }
288
+ try {
289
+ JSONArray array = new JSONArray(json);
290
+ for (int i = 0; i < array.length(); i++) {
291
+ JSONObject item = array.optJSONObject(i);
292
+ if (item == null) {
293
+ continue;
294
+ }
295
+ String kindValue = item.optString("kind", "");
296
+ String value = item.optString("value", "");
297
+ if (kindValue.isEmpty()) {
298
+ logger.warn("Delay condition missing kind at index " + i);
299
+ continue;
300
+ }
301
+ try {
302
+ DelayUntilNext kind = DelayUntilNext.valueOf(kindValue);
303
+ conditions.add(new DelayCondition(kind, value));
304
+ } catch (IllegalArgumentException e) {
305
+ logger.warn("Unknown delay condition kind '" + kindValue + "' at index " + i);
306
+ }
307
+ }
308
+ } catch (JSONException e) {
309
+ logger.error("Failed to parse delay conditions: " + e.getMessage());
310
+ }
311
+ return conditions;
312
+ }
313
+
314
+ private Date parseDateCondition(String value) {
315
+ String[] patterns = {
316
+ "yyyy-MM-dd'T'HH:mm:ss.SSSXXX",
317
+ "yyyy-MM-dd'T'HH:mm:ssXXX",
318
+ "yyyy-MM-dd'T'HH:mm:ss.SSSXX",
319
+ "yyyy-MM-dd'T'HH:mm:ssXX",
320
+ "yyyy-MM-dd'T'HH:mm:ss.SSSX",
321
+ "yyyy-MM-dd'T'HH:mm:ssX",
322
+ "yyyy-MM-dd'T'HH:mm:ss.SSS",
323
+ "yyyy-MM-dd'T'HH:mm:ss"
324
+ };
325
+
326
+ for (String pattern : patterns) {
327
+ Date parsed = parseDateWithPattern(value, pattern);
328
+ if (parsed != null) {
329
+ return parsed;
330
+ }
331
+ }
332
+
333
+ return null;
334
+ }
335
+
336
+ private Date parseDateWithPattern(String value, String pattern) {
337
+ try {
338
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.US);
339
+ sdf.setLenient(false);
340
+
341
+ // If no timezone is provided, keep historical behavior and interpret as local time.
342
+ if (!pattern.contains("X")) {
343
+ sdf.setTimeZone(TimeZone.getDefault());
344
+ }
345
+
346
+ ParsePosition position = new ParsePosition(0);
347
+ Date parsed = sdf.parse(value, position);
348
+ if (parsed != null && position.getIndex() == value.length()) {
349
+ return parsed;
350
+ }
351
+ } catch (Exception ignored) {}
352
+
353
+ return null;
354
+ }
355
+
356
+ private String convertDelayConditionsToJson(ArrayList<DelayCondition> conditions) {
357
+ JSONArray array = new JSONArray();
358
+ for (DelayCondition condition : conditions) {
359
+ try {
360
+ JSONObject obj = new JSONObject();
361
+ obj.put("kind", condition.getKind().name());
362
+ obj.put("value", condition.getValue());
363
+ array.put(obj);
364
+ } catch (JSONException e) {
365
+ logger.error("Failed to serialize delay condition: " + e.getMessage());
366
+ }
367
+ }
368
+ return array.toString();
369
+ }
370
+
371
+ public Boolean setMultiDelay(String delayConditions) {
372
+ try {
373
+ this.editor.putString(DELAY_CONDITION_PREFERENCES, delayConditions);
374
+ if (!this.editor.commit()) {
375
+ logger.error("Failed to delay update: commit returned false");
376
+ return false;
377
+ }
378
+ logger.info("Delay update saved");
379
+ return true;
380
+ } catch (final Exception e) {
381
+ logger.error("Failed to delay update, [Error calling '_setMultiDelay()'] " + e.getMessage());
382
+ return false;
383
+ }
384
+ }
385
+
386
+ public void setBackgroundTimestamp(long backgroundTimestamp) {
387
+ try {
388
+ this.editor.putLong(BACKGROUND_TIMESTAMP_KEY, backgroundTimestamp);
389
+ this.editor.commit();
390
+ logger.info("Background timestamp set");
391
+ } catch (final Exception e) {
392
+ logger.error("Failed to delay update, [Error calling '_setBackgroundTimestamp()'] " + e.getMessage());
393
+ }
394
+ }
395
+
396
+ public void unsetBackgroundTimestamp() {
397
+ try {
398
+ this.editor.remove(BACKGROUND_TIMESTAMP_KEY);
399
+ this.editor.commit();
400
+ logger.info("Background timestamp unset");
401
+ } catch (final Exception e) {
402
+ logger.error("Failed to delay update, [Error calling '_unsetBackgroundTimestamp()'] " + e.getMessage());
403
+ }
404
+ }
405
+
406
+ private long getBackgroundTimestamp() {
407
+ try {
408
+ return this.prefs.getLong(BACKGROUND_TIMESTAMP_KEY, 0);
409
+ } catch (final Exception e) {
410
+ logger.error("Failed to delay update, [Error calling '_getBackgroundTimestamp()'] " + e.getMessage());
411
+ return 0;
412
+ }
413
+ }
414
+
415
+ public boolean cancelDelay(String source) {
416
+ try {
417
+ this.editor.remove(DELAY_CONDITION_PREFERENCES);
418
+ this.editor.remove(DELAY_CONDITION_MODE_PREFERENCES);
419
+ this.editor.commit();
420
+ logger.info("All delays canceled from " + source);
421
+ return true;
422
+ } catch (final Exception e) {
423
+ logger.error("Failed to cancel update delay " + e.getMessage());
424
+ return false;
425
+ }
426
+ }
427
+ }