@capgo/capacitor-updater 8.52.1-beta.pr937.1951.1 → 8.53.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.
Files changed (65) hide show
  1. package/CapgoCapacitorUpdater.podspec +0 -2
  2. package/Package.swift +0 -5
  3. package/README.md +199 -67
  4. package/android/build.gradle +9 -88
  5. package/android/src/main/java/ee/forgr/capacitor_updater/AndroidAppExitReporter.java +60 -32
  6. package/android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java +246 -0
  7. package/android/src/main/java/ee/forgr/capacitor_updater/BundleStatus.java +45 -0
  8. package/android/src/main/java/ee/forgr/capacitor_updater/Callback.java +13 -0
  9. package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +5539 -1066
  10. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +3754 -494
  11. package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +496 -0
  12. package/android/src/main/java/ee/forgr/capacitor_updater/DataManager.java +44 -0
  13. package/android/src/main/java/ee/forgr/capacitor_updater/DelayCondition.java +56 -0
  14. package/android/src/main/java/ee/forgr/capacitor_updater/DelayUntilNext.java +14 -0
  15. package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +427 -0
  16. package/android/src/main/java/ee/forgr/capacitor_updater/DeviceIdHelper.java +6 -23
  17. package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +1541 -0
  18. package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +258 -0
  19. package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +318 -0
  20. package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +45 -0
  21. package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +1 -6
  22. package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
  23. package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +417 -217
  24. package/android/src/main/java/ee/forgr/capacitor_updater/ThreeFingerPinchDetector.java +1 -3
  25. package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +306 -0
  26. package/dist/docs.json +379 -28
  27. package/dist/esm/definitions.d.ts +209 -10
  28. package/dist/esm/definitions.js.map +1 -1
  29. package/dist/plugin.cjs.js.map +1 -1
  30. package/dist/plugin.js.map +1 -1
  31. package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +262 -0
  32. package/ios/Sources/CapacitorUpdaterPlugin/AppHealthTracker.swift +82 -0
  33. package/ios/Sources/CapacitorUpdaterPlugin/BundleInfo.swift +185 -0
  34. package/ios/Sources/CapacitorUpdaterPlugin/BundleStatus.swift +127 -0
  35. package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +5247 -199
  36. package/ios/Sources/CapacitorUpdaterPlugin/CapgoRawRsa.swift +36 -0
  37. package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +3878 -365
  38. package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +328 -0
  39. package/ios/Sources/CapacitorUpdaterPlugin/DelayCondition.swift +74 -0
  40. package/ios/Sources/CapacitorUpdaterPlugin/DelayUntilNext.swift +30 -0
  41. package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +343 -0
  42. package/ios/Sources/CapacitorUpdaterPlugin/InternalUtils.swift +471 -0
  43. package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
  44. package/ios/Sources/CapacitorUpdaterPlugin/RSA.swift +96 -0
  45. package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +53 -0
  46. package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +221 -63
  47. package/ios/Sources/CapacitorUpdaterPlugin/UserDefaultsExtension.swift +46 -0
  48. package/ios/Sources/CapacitorUpdaterPlugin/WebViewStatsReporter.swift +147 -5
  49. package/ios/Sources/CapacitorUpdaterPlugin/ZipArchiveReader.swift +247 -0
  50. package/ios/Sources/CapacitorUpdaterPlugin/ZipCentralDirectory.swift +280 -0
  51. package/package.json +15 -19
  52. package/android/consumer-rules.pro +0 -15
  53. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoCore.java +0 -67
  54. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoCoreNative.java +0 -97
  55. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoDownloadWorker.java +0 -188
  56. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoEngine.java +0 -114
  57. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoEngineHolder.java +0 -59
  58. package/android/src/main/java/ee/forgr/capacitor_updater/CapgoEngineHost.java +0 -118
  59. package/android/src/main/java/ee/forgr/capacitor_updater/EngineMethodLanes.java +0 -168
  60. package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin+AppStore.swift +0 -171
  61. package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin+Methods.swift +0 -168
  62. package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin+Overlays.swift +0 -285
  63. package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin+WebView.swift +0 -134
  64. package/ios/Sources/CapacitorUpdaterPlugin/CapgoCore.swift +0 -61
  65. package/ios/Sources/CapacitorUpdaterPlugin/CapgoEngine.swift +0 -319
@@ -0,0 +1,343 @@
1
+ /*
2
+ * This Source Code Form is subject to the terms of the Mozilla Public
3
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
4
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
+ */
6
+
7
+ //
8
+ // DelayUpdateUtils.swift
9
+ // Plugin
10
+ //
11
+ // Created by Auto-generated based on Android implementation
12
+ // Copyright © 2024 Capgo. All rights reserved.
13
+ //
14
+
15
+ import Foundation
16
+
17
+ public class DelayUpdateUtils {
18
+
19
+ // swiftlint:disable identifier_name
20
+ static let DELAY_CONDITION_PREFERENCES = "DELAY_CONDITION_PREFERENCES_CAPGO"
21
+ static let DELAY_CONDITION_MODE_PREFERENCES = "DELAY_CONDITION_MODE_PREFERENCES_CAPGO"
22
+ static let DELAY_CONDITION_MODE_AND = "and"
23
+ static let DELAY_CONDITION_MODE_OR = "or"
24
+ static let BACKGROUND_TIMESTAMP_KEY = "BACKGROUND_TIMESTAMP_KEY_CAPGO"
25
+ // swiftlint:enable identifier_name
26
+ private let logger: Logger
27
+
28
+ private let currentVersionNative: CapgoSemanticVersion
29
+
30
+ public enum CancelDelaySource {
31
+ case killed
32
+ case background
33
+ case foreground
34
+
35
+ var description: String {
36
+ switch self {
37
+ case .killed: return "KILLED"
38
+ case .background: return "BACKGROUND"
39
+ case .foreground: return "FOREGROUND"
40
+ }
41
+ }
42
+ }
43
+
44
+ public init(currentVersionNative: CapgoSemanticVersion, logger: Logger) {
45
+ self.currentVersionNative = currentVersionNative
46
+ self.logger = logger
47
+ }
48
+
49
+ public func checkCancelDelay(source: CancelDelaySource) {
50
+ let delayUpdatePreferences = UserDefaults.standard.string(
51
+ forKey: DelayUpdateUtils.DELAY_CONDITION_PREFERENCES) ?? "[]"
52
+ let delayConditionList = DelayUpdateUtils.parseDelayConditions(json: delayUpdatePreferences)
53
+ if delayConditionList.isEmpty {
54
+ return
55
+ }
56
+
57
+ let useOrMode = isOrConditionMode()
58
+ var delayConditionListToKeep: [DelayCondition] = []
59
+ var index = 0
60
+ var anyConditionMet = false
61
+
62
+ for condition in delayConditionList {
63
+ let result = evaluateCondition(condition, source: source, index: index)
64
+ if result.met {
65
+ anyConditionMet = true
66
+ if !useOrMode, let logMessage = result.logMessage {
67
+ logger.info(logMessage)
68
+ }
69
+ } else {
70
+ if result.keep {
71
+ delayConditionListToKeep.append(condition)
72
+ }
73
+ if let logMessage = result.logMessage, !useOrMode || result.error {
74
+ if result.error {
75
+ logger.error(logMessage)
76
+ } else if !useOrMode {
77
+ logger.info(logMessage)
78
+ }
79
+ }
80
+ }
81
+ index += 1
82
+ }
83
+
84
+ if useOrMode {
85
+ if anyConditionMet {
86
+ logger.info("Delay condition met in OR mode (source: \(source.description)), canceling delay")
87
+ _ = cancelDelay(source: "checkCancelDelay")
88
+ } else if delayConditionListToKeep.isEmpty {
89
+ _ = cancelDelay(source: "checkCancelDelay")
90
+ } else if delayConditionListToKeep.count != delayConditionList.count {
91
+ let json = toJson(object: delayConditionListToKeep.map { $0.toJSON() })
92
+ _ = setMultiDelay(delayConditions: json)
93
+ }
94
+ return
95
+ }
96
+
97
+ if !delayConditionListToKeep.isEmpty {
98
+ let json = toJson(object: delayConditionListToKeep.map { $0.toJSON() })
99
+ _ = setMultiDelay(delayConditions: json)
100
+ } else {
101
+ _ = cancelDelay(source: "checkCancelDelay")
102
+ }
103
+ }
104
+
105
+ private struct ConditionCheckResult {
106
+ let met: Bool
107
+ let keep: Bool
108
+ let error: Bool
109
+ let logMessage: String?
110
+ }
111
+
112
+ private func evaluateCondition(_ condition: DelayCondition, source: CancelDelaySource, index: Int) -> ConditionCheckResult {
113
+ let kind = condition.getKind()
114
+ let value = condition.getValue()
115
+
116
+ switch kind {
117
+ case "background":
118
+ if source == .foreground {
119
+ let backgroundedAt = getBackgroundTimestamp()
120
+ let now = Int64(Date().timeIntervalSince1970 * 1000)
121
+ let delta = max(0, now - backgroundedAt)
122
+ var longValue: Int64 = 0
123
+ if let value = value, !value.isEmpty {
124
+ longValue = Int64(value) ?? 0
125
+ }
126
+
127
+ if delta > longValue {
128
+ return ConditionCheckResult(
129
+ met: true,
130
+ keep: false,
131
+ error: false,
132
+ logMessage: "Background condition (value: \(value ?? "")) deleted at index \(index). Delta: \(delta), longValue: \(longValue)"
133
+ )
134
+ }
135
+ return ConditionCheckResult(
136
+ met: false,
137
+ keep: true,
138
+ error: false,
139
+ logMessage: "Background delay (value: \(value ?? "")) condition kept at index \(index) (source: \(source.description))"
140
+ )
141
+ }
142
+ return ConditionCheckResult(
143
+ met: false,
144
+ keep: true,
145
+ error: false,
146
+ logMessage: "Background delay (value: \(value ?? "")) condition kept at index \(index) (source: \(source.description))"
147
+ )
148
+
149
+ case "kill":
150
+ if source == .killed {
151
+ return ConditionCheckResult(
152
+ met: true,
153
+ keep: false,
154
+ error: false,
155
+ logMessage: "Kill delay (value: \(value ?? "")) removed at index \(index) after app kill"
156
+ )
157
+ }
158
+ return ConditionCheckResult(
159
+ met: false,
160
+ keep: true,
161
+ error: false,
162
+ logMessage: "Kill delay (value: \(value ?? "")) condition kept at index \(index) (source: \(source.description))"
163
+ )
164
+
165
+ case "date":
166
+ if let value = value, !value.isEmpty {
167
+ if let date = parseDateCondition(value) {
168
+ if Date() > date {
169
+ return ConditionCheckResult(
170
+ met: true,
171
+ keep: false,
172
+ error: false,
173
+ logMessage: "Date delay (value: \(value)) condition removed due to expired date at index \(index)"
174
+ )
175
+ }
176
+ return ConditionCheckResult(met: false, keep: true, error: false, logMessage: "Date delay (value: \(value)) kept at index \(index)")
177
+ }
178
+ return ConditionCheckResult(
179
+ met: false,
180
+ keep: false,
181
+ error: true,
182
+ logMessage: "Date delay (value: \(value)) condition removed due to parsing issue at index \(index)"
183
+ )
184
+ }
185
+ return ConditionCheckResult(
186
+ met: false,
187
+ keep: false,
188
+ error: true,
189
+ logMessage: "Date delay (value: \(value ?? "")) condition removed due to empty value at index \(index)"
190
+ )
191
+
192
+ case "nativeVersion":
193
+ if let value = value, !value.isEmpty {
194
+ do {
195
+ let versionLimit = try CapgoSemanticVersion(value)
196
+ if currentVersionNative >= versionLimit {
197
+ return ConditionCheckResult(
198
+ met: true,
199
+ keep: false,
200
+ error: false,
201
+ logMessage: "Native version delay (value: \(value)) condition removed due to above limit at index \(index)"
202
+ )
203
+ }
204
+ return ConditionCheckResult(met: false, keep: true, error: false, logMessage: "Native version delay (value: \(value)) kept at index \(index)")
205
+ } catch {
206
+ return ConditionCheckResult(
207
+ met: false,
208
+ keep: false,
209
+ error: true,
210
+ logMessage: "Native version delay (value: \(value)) condition removed due to parsing issue at index \(index): \(error)"
211
+ )
212
+ }
213
+ }
214
+ return ConditionCheckResult(
215
+ met: false,
216
+ keep: false,
217
+ error: true,
218
+ logMessage: "Native version delay (value: \(value ?? "")) condition removed due to empty value at index \(index)"
219
+ )
220
+
221
+ default:
222
+ return ConditionCheckResult(met: false, keep: false, error: true, logMessage: "Unknown delay condition kind: \(kind) at index \(index)")
223
+ }
224
+ }
225
+
226
+ public func isOrConditionMode() -> Bool {
227
+ return getConditionMode() == DelayUpdateUtils.DELAY_CONDITION_MODE_OR
228
+ }
229
+
230
+ public func getConditionMode() -> String {
231
+ let mode = UserDefaults.standard.string(forKey: DelayUpdateUtils.DELAY_CONDITION_MODE_PREFERENCES)
232
+ ?? DelayUpdateUtils.DELAY_CONDITION_MODE_AND
233
+ return mode == DelayUpdateUtils.DELAY_CONDITION_MODE_OR
234
+ ? DelayUpdateUtils.DELAY_CONDITION_MODE_OR
235
+ : DelayUpdateUtils.DELAY_CONDITION_MODE_AND
236
+ }
237
+
238
+ public func setConditionMode(_ conditionMode: String) -> Bool {
239
+ let normalized = conditionMode == DelayUpdateUtils.DELAY_CONDITION_MODE_OR
240
+ ? DelayUpdateUtils.DELAY_CONDITION_MODE_OR
241
+ : DelayUpdateUtils.DELAY_CONDITION_MODE_AND
242
+ if conditionMode != DelayUpdateUtils.DELAY_CONDITION_MODE_AND
243
+ && conditionMode != DelayUpdateUtils.DELAY_CONDITION_MODE_OR {
244
+ logger.warn("Unknown delay condition mode '\(conditionMode)', defaulting to '\(DelayUpdateUtils.DELAY_CONDITION_MODE_AND)'")
245
+ }
246
+ UserDefaults.standard.set(normalized, forKey: DelayUpdateUtils.DELAY_CONDITION_MODE_PREFERENCES)
247
+ let synchronized = UserDefaults.standard.synchronize()
248
+ if synchronized {
249
+ logger.info("Delay condition mode saved: \(normalized)")
250
+ } else {
251
+ logger.error("Failed to save delay condition mode: synchronize returned false")
252
+ }
253
+ return synchronized
254
+ }
255
+
256
+ public func setMultiDelay(delayConditions: String) -> Bool {
257
+ UserDefaults.standard.set(delayConditions, forKey: DelayUpdateUtils.DELAY_CONDITION_PREFERENCES)
258
+ let synchronized = UserDefaults.standard.synchronize()
259
+ if synchronized {
260
+ logger.info("Delay update saved")
261
+ } else {
262
+ logger.error("Failed to delay update: synchronize returned false")
263
+ }
264
+ return synchronized
265
+ }
266
+
267
+ public func setBackgroundTimestamp(_ backgroundTimestamp: Int64) {
268
+ UserDefaults.standard.set(backgroundTimestamp, forKey: DelayUpdateUtils.BACKGROUND_TIMESTAMP_KEY)
269
+ UserDefaults.standard.synchronize()
270
+ logger.info("Background timestamp saved")
271
+ }
272
+
273
+ public func unsetBackgroundTimestamp() {
274
+ UserDefaults.standard.removeObject(forKey: DelayUpdateUtils.BACKGROUND_TIMESTAMP_KEY)
275
+ UserDefaults.standard.synchronize()
276
+ logger.info("Background timestamp removed")
277
+ }
278
+
279
+ private func getBackgroundTimestamp() -> Int64 {
280
+ let key = DelayUpdateUtils.BACKGROUND_TIMESTAMP_KEY
281
+ return UserDefaults.standard.object(forKey: key) as? Int64 ?? 0
282
+ }
283
+
284
+ public func cancelDelay(source: String) -> Bool {
285
+ UserDefaults.standard.removeObject(forKey: DelayUpdateUtils.DELAY_CONDITION_PREFERENCES)
286
+ UserDefaults.standard.removeObject(forKey: DelayUpdateUtils.DELAY_CONDITION_MODE_PREFERENCES)
287
+ UserDefaults.standard.synchronize()
288
+ logger.info("All delays canceled from \(source)")
289
+ return true
290
+ }
291
+
292
+ // MARK: - Helper methods
293
+
294
+ private func parseDateCondition(_ value: String) -> Date? {
295
+ let withFractionalSeconds = ISO8601DateFormatter()
296
+ withFractionalSeconds.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
297
+ if let date = withFractionalSeconds.date(from: value) {
298
+ return date
299
+ }
300
+
301
+ let withoutFractionalSeconds = ISO8601DateFormatter()
302
+ withoutFractionalSeconds.formatOptions = [.withInternetDateTime]
303
+ if let date = withoutFractionalSeconds.date(from: value) {
304
+ return date
305
+ }
306
+
307
+ // Legacy fallback for strings without timezone.
308
+ for format in ["yyyy-MM-dd'T'HH:mm:ss.SSS", "yyyy-MM-dd'T'HH:mm:ss"] {
309
+ let formatter = DateFormatter()
310
+ formatter.locale = Locale(identifier: "en_US_POSIX")
311
+ formatter.calendar = Calendar(identifier: .gregorian)
312
+ formatter.timeZone = .current
313
+ formatter.isLenient = false
314
+ formatter.dateFormat = format
315
+ if let date = formatter.date(from: value) {
316
+ return date
317
+ }
318
+ }
319
+
320
+ return nil
321
+ }
322
+
323
+ private func toJson(object: Any) -> String {
324
+ guard let data = try? JSONSerialization.data(withJSONObject: object, options: []) else {
325
+ return ""
326
+ }
327
+ return String(data: data, encoding: String.Encoding.utf8) ?? ""
328
+ }
329
+
330
+ /// Parses stored delay conditions. Malformed JSON and entries without a string `kind` are dropped.
331
+ static func parseDelayConditions(json: String) -> [DelayCondition] {
332
+ guard let jsonData = json.data(using: .utf8),
333
+ let entries = (try? JSONSerialization.jsonObject(with: jsonData)) as? [Any] else {
334
+ return []
335
+ }
336
+ return entries.compactMap { entry in
337
+ guard let object = entry as? [String: Any], let kind = object["kind"] as? String else {
338
+ return nil
339
+ }
340
+ return DelayCondition(kind: kind, value: object["value"] as? String)
341
+ }
342
+ }
343
+ }