@capgo/capacitor-updater 6.51.21 → 6.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.
- package/CapgoCapacitorUpdater.podspec +0 -3
- package/Package.swift +3 -11
- package/README.md +202 -67
- package/android/build.gradle +2 -3
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +610 -177
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +909 -196
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +53 -19
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +226 -106
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +210 -58
- package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +325 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +63 -4
- package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +306 -0
- package/dist/docs.json +394 -27
- package/dist/esm/definitions.d.ts +220 -7
- package/dist/esm/definitions.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +104 -52
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +562 -187
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoSemanticVersion.swift +162 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +564 -109
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +11 -2
- package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +204 -93
- package/ios/Sources/CapacitorUpdaterPlugin/Logger.swift +75 -5
- package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
- package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +53 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +9 -3
- package/ios/Sources/CapacitorUpdaterPlugin/ZipArchiveReader.swift +247 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ZipCentralDirectory.swift +280 -0
- package/package.json +7 -3
|
@@ -225,15 +225,24 @@ public struct CryptoCipher {
|
|
|
225
225
|
return String(hexString(from: sha256).prefix(16))
|
|
226
226
|
}
|
|
227
227
|
|
|
228
|
+
public static func isValidSessionKey(_ sessionKey: String) -> Bool {
|
|
229
|
+
if sessionKey.isEmpty {
|
|
230
|
+
return false
|
|
231
|
+
}
|
|
232
|
+
let sessionKeyParts = sessionKey.components(separatedBy: ":")
|
|
233
|
+
return sessionKeyParts.count == 2 && !sessionKeyParts[0].isEmpty && !sessionKeyParts[1].isEmpty
|
|
234
|
+
}
|
|
235
|
+
|
|
228
236
|
public static func decryptFile(filePath: URL, publicKey: String, sessionKey: String, version: String) throws {
|
|
229
|
-
if publicKey.isEmpty || sessionKey
|
|
237
|
+
if publicKey.isEmpty || !isValidSessionKey(sessionKey) {
|
|
230
238
|
logger.info("Encryption not set, no public key or session, ignored")
|
|
231
239
|
return
|
|
232
240
|
}
|
|
233
241
|
|
|
242
|
+
// The bundle is encrypted from here on: an unusable key must fail, never leave it encrypted.
|
|
234
243
|
if !publicKey.hasPrefix("-----BEGIN RSA PUBLIC KEY-----") {
|
|
235
244
|
logger.error("The public key is not a valid RSA Public key")
|
|
236
|
-
|
|
245
|
+
throw CustomError.cannotDecode
|
|
237
246
|
}
|
|
238
247
|
|
|
239
248
|
do {
|
|
@@ -13,17 +13,19 @@
|
|
|
13
13
|
//
|
|
14
14
|
|
|
15
15
|
import Foundation
|
|
16
|
-
import Version
|
|
17
16
|
|
|
18
17
|
public class DelayUpdateUtils {
|
|
19
18
|
|
|
20
19
|
// swiftlint:disable identifier_name
|
|
21
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"
|
|
22
24
|
static let BACKGROUND_TIMESTAMP_KEY = "BACKGROUND_TIMESTAMP_KEY_CAPGO"
|
|
23
25
|
// swiftlint:enable identifier_name
|
|
24
26
|
private let logger: Logger
|
|
25
27
|
|
|
26
|
-
private let currentVersionNative:
|
|
28
|
+
private let currentVersionNative: CapgoSemanticVersion
|
|
27
29
|
|
|
28
30
|
public enum CancelDelaySource {
|
|
29
31
|
case killed
|
|
@@ -39,7 +41,7 @@ public class DelayUpdateUtils {
|
|
|
39
41
|
}
|
|
40
42
|
}
|
|
41
43
|
|
|
42
|
-
public init(currentVersionNative:
|
|
44
|
+
public init(currentVersionNative: CapgoSemanticVersion, logger: Logger) {
|
|
43
45
|
self.currentVersionNative = currentVersionNative
|
|
44
46
|
self.logger = logger
|
|
45
47
|
}
|
|
@@ -47,114 +49,219 @@ public class DelayUpdateUtils {
|
|
|
47
49
|
public func checkCancelDelay(source: CancelDelaySource) {
|
|
48
50
|
let delayUpdatePreferences = UserDefaults.standard.string(
|
|
49
51
|
forKey: DelayUpdateUtils.DELAY_CONDITION_PREFERENCES) ?? "[]"
|
|
50
|
-
let delayConditionList
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return DelayCondition(kind: kind, value: value)
|
|
52
|
+
let delayConditionList = DelayUpdateUtils.parseDelayConditions(json: delayUpdatePreferences)
|
|
53
|
+
if delayConditionList.isEmpty {
|
|
54
|
+
return
|
|
54
55
|
}
|
|
55
56
|
|
|
57
|
+
let useOrMode = isOrConditionMode()
|
|
56
58
|
var delayConditionListToKeep: [DelayCondition] = []
|
|
57
59
|
var index = 0
|
|
60
|
+
var anyConditionMet = false
|
|
58
61
|
|
|
59
62
|
for condition in delayConditionList {
|
|
60
|
-
let
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
if source == .foreground {
|
|
66
|
-
let backgroundedAt = getBackgroundTimestamp()
|
|
67
|
-
let now = Int64(Date().timeIntervalSince1970 * 1000) // Convert to milliseconds
|
|
68
|
-
let delta = max(0, now - backgroundedAt)
|
|
69
|
-
|
|
70
|
-
var longValue: Int64 = 0
|
|
71
|
-
if let value = value, !value.isEmpty {
|
|
72
|
-
longValue = Int64(value) ?? 0
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if delta > longValue {
|
|
76
|
-
// swiftlint:disable:next line_length
|
|
77
|
-
logger.info("Background condition (value: \(value ?? "")) deleted at index \(index). Delta: \(delta), longValue: \(longValue)")
|
|
78
|
-
} else {
|
|
79
|
-
delayConditionListToKeep.append(condition)
|
|
80
|
-
// swiftlint:disable:next line_length
|
|
81
|
-
logger.info("Background delay (value: \(value ?? "")) condition kept at index \(index) (source: \(source.description))")
|
|
82
|
-
}
|
|
83
|
-
} else {
|
|
84
|
-
delayConditionListToKeep.append(condition)
|
|
85
|
-
// swiftlint:disable:next line_length
|
|
86
|
-
logger.info("Background delay (value: \(value ?? "")) condition kept at index \(index) (source: \(source.description))")
|
|
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)
|
|
87
68
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if source == .killed {
|
|
91
|
-
logger.info("Kill delay (value: \(value ?? "")) removed at index \(index) after app kill")
|
|
92
|
-
} else {
|
|
69
|
+
} else {
|
|
70
|
+
if result.keep {
|
|
93
71
|
delayConditionListToKeep.append(condition)
|
|
94
|
-
// swiftlint:disable:next line_length
|
|
95
|
-
logger.info("Kill delay (value: \(value ?? "")) condition kept at index \(index) (source: \(source.description))")
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
case "date":
|
|
99
|
-
if let value = value, !value.isEmpty {
|
|
100
|
-
if let date = parseDateCondition(value) {
|
|
101
|
-
if Date() > date {
|
|
102
|
-
// swiftlint:disable:next line_length
|
|
103
|
-
logger.info("Date delay (value: \(value)) condition removed due to expired date at index \(index)")
|
|
104
|
-
} else {
|
|
105
|
-
delayConditionListToKeep.append(condition)
|
|
106
|
-
logger.info("Date delay (value: \(value)) kept at index \(index)")
|
|
107
|
-
}
|
|
108
|
-
} else {
|
|
109
|
-
// swiftlint:disable:next line_length
|
|
110
|
-
logger.error("Date delay (value: \(value)) condition removed due to parsing issue at index \(index)")
|
|
111
|
-
}
|
|
112
|
-
} else {
|
|
113
|
-
// swiftlint:disable:next line_length
|
|
114
|
-
logger.error("Date delay (value: \(value ?? "")) condition removed due to empty value at index \(index)")
|
|
115
72
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
if currentVersionNative >= versionLimit {
|
|
122
|
-
// swiftlint:disable:next line_length
|
|
123
|
-
logger.info("Native version delay (value: \(value)) condition removed due to above limit at index \(index)")
|
|
124
|
-
} else {
|
|
125
|
-
delayConditionListToKeep.append(condition)
|
|
126
|
-
logger.info("Native version delay (value: \(value)) kept at index \(index)")
|
|
127
|
-
}
|
|
128
|
-
} catch {
|
|
129
|
-
// swiftlint:disable:next line_length
|
|
130
|
-
logger.error("Native version delay (value: \(value)) condition removed due to parsing issue at index \(index): \(error)")
|
|
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)
|
|
131
78
|
}
|
|
132
|
-
} else {
|
|
133
|
-
// swiftlint:disable:next line_length
|
|
134
|
-
logger.error("Native version delay (value: \(value ?? "")) condition removed due to empty value at index \(index)")
|
|
135
79
|
}
|
|
136
|
-
|
|
137
|
-
default:
|
|
138
|
-
logger.error("Unknown delay condition kind: \(kind) at index \(index)")
|
|
139
80
|
}
|
|
140
|
-
|
|
141
81
|
index += 1
|
|
142
82
|
}
|
|
143
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
|
+
|
|
144
97
|
if !delayConditionListToKeep.isEmpty {
|
|
145
98
|
let json = toJson(object: delayConditionListToKeep.map { $0.toJSON() })
|
|
146
99
|
_ = setMultiDelay(delayConditions: json)
|
|
147
100
|
} else {
|
|
148
|
-
// Clear all delay conditions if none are left to keep
|
|
149
101
|
_ = cancelDelay(source: "checkCancelDelay")
|
|
150
102
|
}
|
|
151
103
|
}
|
|
152
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
|
+
|
|
153
256
|
public func setMultiDelay(delayConditions: String) -> Bool {
|
|
154
257
|
UserDefaults.standard.set(delayConditions, forKey: DelayUpdateUtils.DELAY_CONDITION_PREFERENCES)
|
|
155
|
-
UserDefaults.standard.synchronize()
|
|
156
|
-
|
|
157
|
-
|
|
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
|
|
158
265
|
}
|
|
159
266
|
|
|
160
267
|
public func setBackgroundTimestamp(_ backgroundTimestamp: Int64) {
|
|
@@ -176,6 +283,7 @@ public class DelayUpdateUtils {
|
|
|
176
283
|
|
|
177
284
|
public func cancelDelay(source: String) -> Bool {
|
|
178
285
|
UserDefaults.standard.removeObject(forKey: DelayUpdateUtils.DELAY_CONDITION_PREFERENCES)
|
|
286
|
+
UserDefaults.standard.removeObject(forKey: DelayUpdateUtils.DELAY_CONDITION_MODE_PREFERENCES)
|
|
179
287
|
UserDefaults.standard.synchronize()
|
|
180
288
|
logger.info("All delays canceled from \(source)")
|
|
181
289
|
return true
|
|
@@ -219,14 +327,17 @@ public class DelayUpdateUtils {
|
|
|
219
327
|
return String(data: data, encoding: String.Encoding.utf8) ?? ""
|
|
220
328
|
}
|
|
221
329
|
|
|
222
|
-
|
|
223
|
-
|
|
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 {
|
|
224
334
|
return []
|
|
225
335
|
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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
|
+
}
|
|
231
342
|
}
|
|
232
343
|
}
|
|
@@ -4,6 +4,8 @@ import os.log
|
|
|
4
4
|
import WebKit
|
|
5
5
|
|
|
6
6
|
public class Logger {
|
|
7
|
+
static let maxWebViewLogPayloadChars = 4096
|
|
8
|
+
|
|
7
9
|
public enum LogLevel: Int {
|
|
8
10
|
case silent = 0
|
|
9
11
|
case error
|
|
@@ -199,17 +201,85 @@ public class Logger {
|
|
|
199
201
|
// This will never fail, but we have to keep swift happy
|
|
200
202
|
if let label = _labels[level] {
|
|
201
203
|
log(atLevel: level, label: label, tag: tag, message: message)
|
|
202
|
-
if let webView = self.webView {
|
|
204
|
+
if let webView = self.webView, let script = buildWebViewConsoleScript(level: level, label: label, tag: tag, message: message) {
|
|
203
205
|
DispatchQueue.main.async {
|
|
204
|
-
|
|
205
|
-
let jsArg = self.toJSStringLiteral(combined)
|
|
206
|
-
webView.evaluateJavaScript("console.\(level.asString())(\(jsArg))", completionHandler: nil)
|
|
206
|
+
webView.evaluateJavaScript(script, completionHandler: nil)
|
|
207
207
|
}
|
|
208
208
|
}
|
|
209
209
|
}
|
|
210
210
|
}
|
|
211
211
|
|
|
212
|
-
|
|
212
|
+
func consoleMethodName(for level: LogLevel) -> String? {
|
|
213
|
+
switch level {
|
|
214
|
+
case .error:
|
|
215
|
+
return "error"
|
|
216
|
+
case .warn:
|
|
217
|
+
return "warn"
|
|
218
|
+
case .info:
|
|
219
|
+
return "info"
|
|
220
|
+
case .debug:
|
|
221
|
+
return "debug"
|
|
222
|
+
case .silent:
|
|
223
|
+
return nil
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private func utf8SequenceLength(_ lead: UInt8) -> Int {
|
|
228
|
+
if (lead & 0x80) == 0 {
|
|
229
|
+
return 1
|
|
230
|
+
}
|
|
231
|
+
if (lead & 0xE0) == 0xC0 {
|
|
232
|
+
return 2
|
|
233
|
+
}
|
|
234
|
+
if (lead & 0xF0) == 0xE0 {
|
|
235
|
+
return 3
|
|
236
|
+
}
|
|
237
|
+
if (lead & 0xF8) == 0xF0 {
|
|
238
|
+
return 4
|
|
239
|
+
}
|
|
240
|
+
return 0
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
func capWebViewLogPayload(_ payload: String) -> String {
|
|
244
|
+
let suffix = "..."
|
|
245
|
+
let maxPayloadBytes = Logger.maxWebViewLogPayloadChars
|
|
246
|
+
let payloadBytes = Array(payload.utf8.prefix(maxPayloadBytes + 1))
|
|
247
|
+
if payloadBytes.count <= maxPayloadBytes {
|
|
248
|
+
return payload
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
var end = maxPayloadBytes
|
|
252
|
+
while end > 0 && (payloadBytes[end - 1] & 0xC0) == 0x80 {
|
|
253
|
+
end -= 1
|
|
254
|
+
}
|
|
255
|
+
while end > 0 {
|
|
256
|
+
let leadIndex = end - 1
|
|
257
|
+
let lead = payloadBytes[leadIndex]
|
|
258
|
+
let seqLength = utf8SequenceLength(lead)
|
|
259
|
+
if seqLength == 0 {
|
|
260
|
+
end -= 1
|
|
261
|
+
continue
|
|
262
|
+
}
|
|
263
|
+
if leadIndex + seqLength <= maxPayloadBytes {
|
|
264
|
+
end = leadIndex + seqLength
|
|
265
|
+
break
|
|
266
|
+
}
|
|
267
|
+
end -= 1
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return String(decoding: payloadBytes[0..<end], as: UTF8.self) + suffix
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
func buildWebViewConsoleScript(level: LogLevel, label: String, tag: String, message: String) -> String? {
|
|
274
|
+
guard let consoleMethod = consoleMethodName(for: level) else {
|
|
275
|
+
return nil
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
let payload = capWebViewLogPayload("\(label) \(tag) : \(message)")
|
|
279
|
+
return "console.\(consoleMethod)(\(toJSStringLiteral(payload)))"
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
func toJSStringLiteral(_ value: String) -> String {
|
|
213
283
|
// Prefer JSON encoding to produce a valid JS string literal
|
|
214
284
|
if let data = try? JSONEncoder().encode(value),
|
|
215
285
|
let encoded = String(data: data, encoding: .utf8) {
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import UIKit
|
|
3
|
+
|
|
4
|
+
final class NativeUpdatePromptCoordinator {
|
|
5
|
+
static let lastNativeVersionDefaultsKey = "CapacitorUpdater.nativeUpdatePromptLastNativeVersion"
|
|
6
|
+
static let lastShownMsDefaultsKey = "CapacitorUpdater.nativeUpdatePromptLastShownMs"
|
|
7
|
+
|
|
8
|
+
private static var sessionShownNativeVersions = Set<String>()
|
|
9
|
+
|
|
10
|
+
static func resetSessionStateForTesting() {
|
|
11
|
+
sessionShownNativeVersions.removeAll()
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
private let enabled: Bool
|
|
15
|
+
private let onStoreCheck: Bool
|
|
16
|
+
private let title: String?
|
|
17
|
+
private let message: String?
|
|
18
|
+
private let updateButton: String?
|
|
19
|
+
private let laterButton: String?
|
|
20
|
+
private let countryFilter: String?
|
|
21
|
+
private let cooldownMs: TimeInterval
|
|
22
|
+
private weak var plugin: CapacitorUpdaterPlugin?
|
|
23
|
+
|
|
24
|
+
convenience init(plugin: CapacitorUpdaterPlugin) {
|
|
25
|
+
let config = plugin.getConfig()
|
|
26
|
+
self.init(
|
|
27
|
+
plugin: plugin,
|
|
28
|
+
enabled: config.getBoolean("nativeUpdatePrompt", false),
|
|
29
|
+
onStoreCheck: config.getBoolean("nativeUpdatePromptOnStoreCheck", false),
|
|
30
|
+
title: config.getString("nativeUpdatePromptTitle"),
|
|
31
|
+
message: config.getString("nativeUpdatePromptMessage"),
|
|
32
|
+
updateButton: config.getString("nativeUpdatePromptUpdateButton"),
|
|
33
|
+
laterButton: config.getString("nativeUpdatePromptLaterButton"),
|
|
34
|
+
countryFilter: config.getString("nativeUpdatePromptCountry"),
|
|
35
|
+
cooldownHours: config.getInt("nativeUpdatePromptCooldownHours", 24)
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
init(
|
|
40
|
+
plugin: CapacitorUpdaterPlugin?,
|
|
41
|
+
enabled: Bool = false,
|
|
42
|
+
onStoreCheck: Bool = false,
|
|
43
|
+
title: String? = nil,
|
|
44
|
+
message: String? = nil,
|
|
45
|
+
updateButton: String? = nil,
|
|
46
|
+
laterButton: String? = nil,
|
|
47
|
+
countryFilter: String? = nil,
|
|
48
|
+
cooldownHours: Int = 24
|
|
49
|
+
) {
|
|
50
|
+
self.plugin = plugin
|
|
51
|
+
self.enabled = enabled
|
|
52
|
+
self.onStoreCheck = onStoreCheck
|
|
53
|
+
self.title = title
|
|
54
|
+
self.message = message
|
|
55
|
+
self.updateButton = updateButton
|
|
56
|
+
self.laterButton = laterButton
|
|
57
|
+
self.countryFilter = countryFilter
|
|
58
|
+
let normalizedCooldownHours = max(1, cooldownHours)
|
|
59
|
+
self.cooldownMs = TimeInterval(normalizedCooldownHours) * 60 * 60
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
var isOnStoreCheckEnabled: Bool {
|
|
63
|
+
enabled && onStoreCheck
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
static func isNativeStoreUpdateBlock(response: AppVersion) -> Bool {
|
|
67
|
+
if response.breaking == true {
|
|
68
|
+
return true
|
|
69
|
+
}
|
|
70
|
+
if response.message == "store_update_required" {
|
|
71
|
+
return true
|
|
72
|
+
}
|
|
73
|
+
let error = response.error ?? ""
|
|
74
|
+
return error.hasPrefix("disable_auto_update")
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
func maybeShowForUpdateBlock(response: AppVersion) {
|
|
78
|
+
guard enabled, Self.isNativeStoreUpdateBlock(response: response) else {
|
|
79
|
+
return
|
|
80
|
+
}
|
|
81
|
+
scheduleShow()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
func maybeShowForStoreUpdateAvailable() {
|
|
85
|
+
guard isOnStoreCheckEnabled else {
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
scheduleShow()
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
private func scheduleShow() {
|
|
92
|
+
guard matchesCountryFilter() else {
|
|
93
|
+
return
|
|
94
|
+
}
|
|
95
|
+
guard let plugin = plugin else {
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
let nativeVersion = plugin.currentBuildVersionForNativeUpdatePrompt()
|
|
99
|
+
guard !nativeVersion.isEmpty else {
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
guard shouldShowForNativeVersion(nativeVersion) else {
|
|
103
|
+
return
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(600)) {
|
|
107
|
+
self.showDialog(nativeVersion: nativeVersion)
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
func shouldShowForNativeVersion(_ nativeVersion: String) -> Bool {
|
|
112
|
+
if Self.sessionShownNativeVersions.contains(nativeVersion) {
|
|
113
|
+
return false
|
|
114
|
+
}
|
|
115
|
+
let lastVersion = UserDefaults.standard.string(forKey: Self.lastNativeVersionDefaultsKey) ?? ""
|
|
116
|
+
let lastShown = UserDefaults.standard.double(forKey: Self.lastShownMsDefaultsKey)
|
|
117
|
+
if nativeVersion == lastVersion, lastShown > 0 {
|
|
118
|
+
let elapsed = Date().timeIntervalSince1970 - lastShown
|
|
119
|
+
if elapsed < cooldownMs {
|
|
120
|
+
return false
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return true
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
func matchesCountryFilter() -> Bool {
|
|
127
|
+
guard let filter = countryFilter?.trimmingCharacters(in: .whitespacesAndNewlines), !filter.isEmpty else {
|
|
128
|
+
return true
|
|
129
|
+
}
|
|
130
|
+
let deviceCountry = Self.currentDeviceRegionCode()
|
|
131
|
+
if deviceCountry.isEmpty {
|
|
132
|
+
return false
|
|
133
|
+
}
|
|
134
|
+
let normalizedDevice = deviceCountry.uppercased()
|
|
135
|
+
return filter
|
|
136
|
+
.split(separator: ",")
|
|
137
|
+
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines).uppercased() }
|
|
138
|
+
.contains(normalizedDevice)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private func showDialog(nativeVersion: String) {
|
|
142
|
+
guard let plugin = plugin else {
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
if let topVC = UIApplication.topViewController(plugin.bridge?.viewController),
|
|
146
|
+
topVC.isKind(of: UIAlertController.self) {
|
|
147
|
+
return
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let dialogTitle = (title?.isEmpty == false ? title : nil) ?? "Update available"
|
|
151
|
+
let dialogMessage = (message?.isEmpty == false ? message : nil)
|
|
152
|
+
?? "A new version of this app is available in the store. Update now to continue receiving live updates."
|
|
153
|
+
let positive = (updateButton?.isEmpty == false ? updateButton : nil) ?? "Update"
|
|
154
|
+
let negative = (laterButton?.isEmpty == false ? laterButton : nil) ?? "Not now"
|
|
155
|
+
|
|
156
|
+
let alert = UIAlertController(title: dialogTitle, message: dialogMessage, preferredStyle: .alert)
|
|
157
|
+
alert.addAction(UIAlertAction(title: negative, style: .cancel) { _ in
|
|
158
|
+
self.recordShown(nativeVersion: nativeVersion)
|
|
159
|
+
})
|
|
160
|
+
alert.addAction(UIAlertAction(title: positive, style: .default) { _ in
|
|
161
|
+
self.recordShown(nativeVersion: nativeVersion)
|
|
162
|
+
plugin.openAppStoreFromNativeUpdatePrompt()
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
if let topVC = UIApplication.topViewController(plugin.bridge?.viewController) {
|
|
166
|
+
topVC.present(alert, animated: true)
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
func recordShown(nativeVersion: String) {
|
|
171
|
+
Self.sessionShownNativeVersions.insert(nativeVersion)
|
|
172
|
+
UserDefaults.standard.set(nativeVersion, forKey: Self.lastNativeVersionDefaultsKey)
|
|
173
|
+
UserDefaults.standard.set(Date().timeIntervalSince1970, forKey: Self.lastShownMsDefaultsKey)
|
|
174
|
+
UserDefaults.standard.synchronize()
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private static func currentDeviceRegionCode() -> String {
|
|
178
|
+
if #available(iOS 16, *) {
|
|
179
|
+
return Locale.current.region?.identifier ?? ""
|
|
180
|
+
}
|
|
181
|
+
return Locale.current.regionCode ?? ""
|
|
182
|
+
}
|
|
183
|
+
}
|