@capgo/capacitor-updater 4.13.6 → 4.13.7
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.
|
@@ -54,7 +54,7 @@ public class CapacitorUpdater {
|
|
|
54
54
|
private static final String bundleDirectory = "versions";
|
|
55
55
|
|
|
56
56
|
public static final String TAG = "Capacitor-updater";
|
|
57
|
-
public static final String pluginVersion = "4.13.
|
|
57
|
+
public static final String pluginVersion = "4.13.7";
|
|
58
58
|
|
|
59
59
|
public SharedPreferences.Editor editor;
|
|
60
60
|
public SharedPreferences prefs;
|
|
@@ -28,7 +28,7 @@ public class SetChannel: NSObject {
|
|
|
28
28
|
}
|
|
29
29
|
extension SetChannel {
|
|
30
30
|
func toDict() -> [String: Any] {
|
|
31
|
-
var dict: [String
|
|
31
|
+
var dict: [String: Any] = [String: Any]()
|
|
32
32
|
let otherSelf: Mirror = Mirror(reflecting: self)
|
|
33
33
|
for child: Mirror.Child in otherSelf.children {
|
|
34
34
|
if let key: String = child.label {
|
|
@@ -54,7 +54,7 @@ public class GetChannel: NSObject {
|
|
|
54
54
|
}
|
|
55
55
|
extension GetChannel {
|
|
56
56
|
func toDict() -> [String: Any] {
|
|
57
|
-
var dict: [String
|
|
57
|
+
var dict: [String: Any] = [String: Any]()
|
|
58
58
|
let otherSelf: Mirror = Mirror(reflecting: self)
|
|
59
59
|
for child: Mirror.Child in otherSelf.children {
|
|
60
60
|
if let key: String = child.label {
|
|
@@ -100,7 +100,7 @@ public class AppVersion: NSObject {
|
|
|
100
100
|
|
|
101
101
|
extension AppVersion {
|
|
102
102
|
func toDict() -> [String: Any] {
|
|
103
|
-
var dict: [String
|
|
103
|
+
var dict: [String: Any] = [String: Any]()
|
|
104
104
|
let otherSelf: Mirror = Mirror(reflecting: self)
|
|
105
105
|
for child: Mirror.Child in otherSelf.children {
|
|
106
106
|
if let key: String = child.label {
|
|
@@ -9,21 +9,22 @@ import Version
|
|
|
9
9
|
@objc(CapacitorUpdaterPlugin)
|
|
10
10
|
public class CapacitorUpdaterPlugin: CAPPlugin {
|
|
11
11
|
private var implementation = CapacitorUpdater()
|
|
12
|
-
static let updateUrlDefault
|
|
13
|
-
static let statsUrlDefault
|
|
14
|
-
static let channelUrlDefault
|
|
15
|
-
let DELAY_CONDITION_PREFERENCES
|
|
16
|
-
private var updateUrl
|
|
17
|
-
private var statsUrl
|
|
12
|
+
static let updateUrlDefault = "https://api.capgo.app/updates"
|
|
13
|
+
static let statsUrlDefault = "https://api.capgo.app/stats"
|
|
14
|
+
static let channelUrlDefault = "https://api.capgo.app/channel_self"
|
|
15
|
+
let DELAY_CONDITION_PREFERENCES = ""
|
|
16
|
+
private var updateUrl = ""
|
|
17
|
+
private var statsUrl = ""
|
|
18
|
+
private var backgroundTaskID: UIBackgroundTaskIdentifier = UIBackgroundTaskIdentifier.invalid
|
|
18
19
|
private var currentVersionNative: Version = "0.0.0"
|
|
19
|
-
private var autoUpdate
|
|
20
|
-
private var appReadyTimeout
|
|
20
|
+
private var autoUpdate = false
|
|
21
|
+
private var appReadyTimeout = 10000
|
|
21
22
|
private var appReadyCheck: DispatchWorkItem?
|
|
22
|
-
private var resetWhenUpdate
|
|
23
|
-
private var autoDeleteFailed
|
|
24
|
-
private var autoDeletePrevious
|
|
23
|
+
private var resetWhenUpdate = true
|
|
24
|
+
private var autoDeleteFailed = false
|
|
25
|
+
private var autoDeletePrevious = false
|
|
25
26
|
private var backgroundWork: DispatchWorkItem?
|
|
26
|
-
private var taskRunning
|
|
27
|
+
private var taskRunning = false
|
|
27
28
|
|
|
28
29
|
override public func load() {
|
|
29
30
|
print("\(self.implementation.TAG) init for device \(self.implementation.deviceID)")
|
|
@@ -50,7 +51,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
|
|
|
50
51
|
if resetWhenUpdate {
|
|
51
52
|
self.cleanupObsoleteVersions()
|
|
52
53
|
}
|
|
53
|
-
let nc
|
|
54
|
+
let nc = NotificationCenter.default
|
|
54
55
|
nc.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
|
|
55
56
|
nc.addObserver(self, selector: #selector(appMovedToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
|
|
56
57
|
nc.addObserver(self, selector: #selector(appKilled), name: UIApplication.willTerminateNotification, object: nil)
|
|
@@ -347,7 +348,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
|
|
|
347
348
|
}
|
|
348
349
|
|
|
349
350
|
private func _checkCancelDelay(killed: Bool) {
|
|
350
|
-
let delayUpdatePreferences
|
|
351
|
+
let delayUpdatePreferences = UserDefaults.standard.string(forKey: DELAY_CONDITION_PREFERENCES) ?? "[]"
|
|
351
352
|
let delayConditionList: [DelayCondition] = fromJsonArr(json: delayUpdatePreferences).map { obj -> DelayCondition in
|
|
352
353
|
let kind: String = obj.value(forKey: "kind") as! String
|
|
353
354
|
let value: String? = obj.value(forKey: "value") as? String
|
|
@@ -465,6 +466,10 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
|
|
|
465
466
|
self.appReadyCheck = nil
|
|
466
467
|
}
|
|
467
468
|
|
|
469
|
+
func EndBackGroundTask() {
|
|
470
|
+
UIApplication.shared.endBackgroundTask(self.backgroundTaskID)
|
|
471
|
+
self.backgroundTaskID = UIBackgroundTaskIdentifier.invalid
|
|
472
|
+
}
|
|
468
473
|
@objc func appMovedToForeground() {
|
|
469
474
|
if backgroundWork != nil && taskRunning {
|
|
470
475
|
backgroundWork!.cancel()
|
|
@@ -472,8 +477,13 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
|
|
|
472
477
|
}
|
|
473
478
|
if self._isAutoUpdateEnabled() {
|
|
474
479
|
DispatchQueue.global(qos: .background).async {
|
|
480
|
+
self.backgroundTaskID = UIApplication.shared.beginBackgroundTask(withName: "Finish Download Tasks") {
|
|
481
|
+
// End the task if time expires.
|
|
482
|
+
UIApplication.shared.endBackgroundTask(self.backgroundTaskID)
|
|
483
|
+
self.backgroundTaskID = UIBackgroundTaskIdentifier.invalid
|
|
484
|
+
}
|
|
475
485
|
print("\(self.implementation.TAG) Check for update via \(self.updateUrl)")
|
|
476
|
-
let url
|
|
486
|
+
let url = URL(string: self.updateUrl)!
|
|
477
487
|
let res = self.implementation.getLatest(url: url)
|
|
478
488
|
let current = self.implementation.getCurrentBundle()
|
|
479
489
|
|
|
@@ -483,12 +493,14 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
|
|
|
483
493
|
self.notifyListeners("majorAvailable", data: ["version": res.version])
|
|
484
494
|
}
|
|
485
495
|
self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
|
|
496
|
+
self.EndBackGroundTask()
|
|
486
497
|
return
|
|
487
498
|
}
|
|
488
499
|
let sessionKey = res.sessionKey ?? ""
|
|
489
500
|
guard let downloadUrl = URL(string: res.url) else {
|
|
490
501
|
print("\(self.implementation.TAG) Error no url or wrong format")
|
|
491
502
|
self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
|
|
503
|
+
self.EndBackGroundTask()
|
|
492
504
|
return
|
|
493
505
|
}
|
|
494
506
|
let latestVersionName = res.version
|
|
@@ -498,12 +510,14 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
|
|
|
498
510
|
if latest!.isErrorStatus() {
|
|
499
511
|
print("\(self.implementation.TAG) Latest version already exists, and is in error state. Aborting update.")
|
|
500
512
|
self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
|
|
513
|
+
self.EndBackGroundTask()
|
|
501
514
|
return
|
|
502
515
|
}
|
|
503
516
|
if latest!.isDownloaded() {
|
|
504
517
|
print("\(self.implementation.TAG) Latest version already exists and download is NOT required. Update will occur next time app moves to background.")
|
|
505
518
|
self.notifyListeners("updateAvailable", data: ["bundle": current.toJSON()])
|
|
506
519
|
_ = self.implementation.setNextBundle(next: latest!.getId())
|
|
520
|
+
self.EndBackGroundTask()
|
|
507
521
|
return
|
|
508
522
|
}
|
|
509
523
|
if latest!.isDeleted() {
|
|
@@ -527,6 +541,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
|
|
|
527
541
|
if !resDel {
|
|
528
542
|
print("\(self.implementation.TAG) Delete failed, id \(next.getId()) doesn't exist")
|
|
529
543
|
}
|
|
544
|
+
self.EndBackGroundTask()
|
|
530
545
|
return
|
|
531
546
|
}
|
|
532
547
|
self.notifyListeners("updateAvailable", data: ["bundle": next.toJSON()])
|
|
@@ -544,13 +559,13 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
|
|
|
544
559
|
}
|
|
545
560
|
}
|
|
546
561
|
}
|
|
547
|
-
|
|
548
562
|
self.checkAppReady()
|
|
563
|
+
self.EndBackGroundTask()
|
|
549
564
|
}
|
|
550
565
|
|
|
551
566
|
@objc func appMovedToBackground() {
|
|
552
567
|
print("\(self.implementation.TAG) Check for pending update")
|
|
553
|
-
let delayUpdatePreferences
|
|
568
|
+
let delayUpdatePreferences = UserDefaults.standard.string(forKey: DELAY_CONDITION_PREFERENCES) ?? "[]"
|
|
554
569
|
|
|
555
570
|
let delayConditionList: [DelayCondition] = fromJsonArr(json: delayUpdatePreferences).map { obj -> DelayCondition in
|
|
556
571
|
let kind: String = obj.value(forKey: "kind") as! String
|
|
@@ -587,7 +602,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
|
|
|
587
602
|
}
|
|
588
603
|
|
|
589
604
|
private func installNext() {
|
|
590
|
-
let delayUpdatePreferences
|
|
605
|
+
let delayUpdatePreferences = UserDefaults.standard.string(forKey: DELAY_CONDITION_PREFERENCES) ?? "[]"
|
|
591
606
|
let delayConditionList: [DelayCondition]? = fromJsonArr(json: delayUpdatePreferences).map { obj -> DelayCondition in
|
|
592
607
|
let kind: String = obj.value(forKey: "kind") as! String
|
|
593
608
|
let value: String? = obj.value(forKey: "value") as? String
|
|
@@ -612,15 +627,15 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
|
|
|
612
627
|
}
|
|
613
628
|
|
|
614
629
|
@objc private func toJson(object: Any) -> String {
|
|
615
|
-
guard let data
|
|
630
|
+
guard let data = try? JSONSerialization.data(withJSONObject: object, options: []) else {
|
|
616
631
|
return ""
|
|
617
632
|
}
|
|
618
633
|
return String(data: data, encoding: String.Encoding.utf8) ?? ""
|
|
619
634
|
}
|
|
620
635
|
|
|
621
636
|
@objc private func fromJsonArr(json: String) -> [NSObject] {
|
|
622
|
-
let jsonData
|
|
623
|
-
let object
|
|
637
|
+
let jsonData = json.data(using: .utf8)!
|
|
638
|
+
let object = try? JSONSerialization.jsonObject(
|
|
624
639
|
with: jsonData,
|
|
625
640
|
options: .mutableContainers
|
|
626
641
|
) as? [NSObject]
|