@capgo/capacitor-updater 4.13.6 → 4.13.8

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.6";
57
+ public static final String pluginVersion = "4.13.8";
58
58
 
59
59
  public SharedPreferences.Editor editor;
60
60
  public SharedPreferences prefs;
@@ -803,245 +803,231 @@ public class CapacitorUpdaterPlugin
803
803
  }
804
804
  }
805
805
 
806
- @Override // appMovedToForeground
807
- public void onActivityStarted(@NonNull final Activity activity) {
808
- if (CapacitorUpdaterPlugin.this._isAutoUpdateEnabled()) {
809
- new Thread(
810
- new Runnable() {
811
- @Override
812
- public void run() {
813
- Log.i(
814
- CapacitorUpdater.TAG,
815
- "Check for update via: " + CapacitorUpdaterPlugin.this.updateUrl
816
- );
817
- CapacitorUpdaterPlugin.this.implementation.getLatest(
818
- CapacitorUpdaterPlugin.this.updateUrl,
819
- res -> {
820
- final BundleInfo current =
821
- CapacitorUpdaterPlugin.this.implementation.getCurrentBundle();
822
- try {
823
- if (res.has("message")) {
824
- Log.i(
825
- CapacitorUpdater.TAG,
826
- "message " + res.get("message")
827
- );
828
- if (
829
- res.has("major") &&
830
- res.getBoolean("major") &&
831
- res.has("version")
832
- ) {
833
- final JSObject majorAvailable = new JSObject();
834
- majorAvailable.put("version", res.getString("version"));
835
- CapacitorUpdaterPlugin.this.notifyListeners(
836
- "majorAvailable",
837
- majorAvailable
838
- );
839
- }
840
- final JSObject retNoNeed = new JSObject();
841
- retNoNeed.put("bundle", current.toJSON());
842
- CapacitorUpdaterPlugin.this.notifyListeners(
843
- "noNeedUpdate",
844
- retNoNeed
845
- );
846
- return;
847
- }
848
-
806
+ private void backgroundDownload() {
807
+ new Thread(
808
+ new Runnable() {
809
+ @Override
810
+ public void run() {
811
+ Log.i(
812
+ CapacitorUpdater.TAG,
813
+ "Check for update via: " + CapacitorUpdaterPlugin.this.updateUrl
814
+ );
815
+ CapacitorUpdaterPlugin.this.implementation.getLatest(
816
+ CapacitorUpdaterPlugin.this.updateUrl,
817
+ res -> {
818
+ final BundleInfo current =
819
+ CapacitorUpdaterPlugin.this.implementation.getCurrentBundle();
820
+ try {
821
+ if (res.has("message")) {
822
+ Log.i(
823
+ CapacitorUpdater.TAG,
824
+ "message " + res.get("message")
825
+ );
849
826
  if (
850
- !res.has("url") ||
851
- !CapacitorUpdaterPlugin.this.isValidURL(
852
- res.getString("url")
853
- )
827
+ res.has("major") &&
828
+ res.getBoolean("major") &&
829
+ res.has("version")
854
830
  ) {
855
- Log.e(
856
- CapacitorUpdater.TAG,
857
- "Error no url or wrong format"
858
- );
859
- final JSObject retNoNeed = new JSObject();
860
- retNoNeed.put("bundle", current.toJSON());
831
+ final JSObject majorAvailable = new JSObject();
832
+ majorAvailable.put("version", res.getString("version"));
861
833
  CapacitorUpdaterPlugin.this.notifyListeners(
862
- "noNeedUpdate",
863
- retNoNeed
834
+ "majorAvailable",
835
+ majorAvailable
864
836
  );
865
837
  }
866
- final String latestVersionName = res.getString("version");
838
+ final JSObject retNoNeed = new JSObject();
839
+ retNoNeed.put("bundle", current.toJSON());
840
+ CapacitorUpdaterPlugin.this.notifyListeners(
841
+ "noNeedUpdate",
842
+ retNoNeed
843
+ );
844
+ return;
845
+ }
867
846
 
868
- if (
869
- latestVersionName != null &&
870
- !"".equals(latestVersionName) &&
871
- !current.getVersionName().equals(latestVersionName)
872
- ) {
873
- final BundleInfo latest =
874
- CapacitorUpdaterPlugin.this.implementation.getBundleInfoByName(
875
- latestVersionName
876
- );
877
- if (latest != null) {
878
- if (latest.isErrorStatus()) {
879
- Log.e(
880
- CapacitorUpdater.TAG,
881
- "Latest bundle already exists, and is in error state. Aborting update."
847
+ if (
848
+ !res.has("url") ||
849
+ !CapacitorUpdaterPlugin.this.isValidURL(
850
+ res.getString("url")
851
+ )
852
+ ) {
853
+ Log.e(CapacitorUpdater.TAG, "Error no url or wrong format");
854
+ final JSObject retNoNeed = new JSObject();
855
+ retNoNeed.put("bundle", current.toJSON());
856
+ CapacitorUpdaterPlugin.this.notifyListeners(
857
+ "noNeedUpdate",
858
+ retNoNeed
859
+ );
860
+ }
861
+ final String latestVersionName = res.getString("version");
862
+
863
+ if (
864
+ latestVersionName != null &&
865
+ !"".equals(latestVersionName) &&
866
+ !current.getVersionName().equals(latestVersionName)
867
+ ) {
868
+ final BundleInfo latest =
869
+ CapacitorUpdaterPlugin.this.implementation.getBundleInfoByName(
870
+ latestVersionName
871
+ );
872
+ if (latest != null) {
873
+ if (latest.isErrorStatus()) {
874
+ Log.e(
875
+ CapacitorUpdater.TAG,
876
+ "Latest bundle already exists, and is in error state. Aborting update."
877
+ );
878
+ final JSObject retNoNeed = new JSObject();
879
+ retNoNeed.put("bundle", current.toJSON());
880
+ CapacitorUpdaterPlugin.this.notifyListeners(
881
+ "noNeedUpdate",
882
+ retNoNeed
882
883
  );
883
- final JSObject retNoNeed = new JSObject();
884
- retNoNeed.put("bundle", current.toJSON());
885
- CapacitorUpdaterPlugin.this.notifyListeners(
886
- "noNeedUpdate",
887
- retNoNeed
888
- );
889
- return;
890
- }
891
- if (latest.isDownloaded()) {
892
- Log.i(
893
- CapacitorUpdater.TAG,
894
- "Latest bundle already exists and download is NOT required. Update will occur next time app moves to background."
884
+ return;
885
+ }
886
+ if (latest.isDownloaded()) {
887
+ Log.i(
888
+ CapacitorUpdater.TAG,
889
+ "Latest bundle already exists and download is NOT required. Update will occur next time app moves to background."
890
+ );
891
+ final JSObject ret = new JSObject();
892
+ ret.put("bundle", latest.toJSON());
893
+ CapacitorUpdaterPlugin.this.notifyListeners(
894
+ "updateAvailable",
895
+ ret
895
896
  );
896
- final JSObject ret = new JSObject();
897
- ret.put("bundle", latest.toJSON());
898
- CapacitorUpdaterPlugin.this.notifyListeners(
899
- "updateAvailable",
900
- ret
901
- );
902
- CapacitorUpdaterPlugin.this.implementation.setNextBundle(
903
- latest.getId()
904
- );
905
- return;
906
- }
907
- if (latest.isDeleted()) {
908
- Log.i(
909
- CapacitorUpdater.TAG,
910
- "Latest bundle already exists and will be deleted, download will overwrite it."
897
+ CapacitorUpdaterPlugin.this.implementation.setNextBundle(
898
+ latest.getId()
911
899
  );
912
- try {
913
- final Boolean deleted =
914
- CapacitorUpdaterPlugin.this.implementation.delete(
915
- latest.getId(),
916
- true
917
- );
918
- if (deleted) {
919
- Log.i(
920
- CapacitorUpdater.TAG,
921
- "Failed bundle deleted: " +
922
- latest.getVersionName()
900
+ return;
901
+ }
902
+ if (latest.isDeleted()) {
903
+ Log.i(
904
+ CapacitorUpdater.TAG,
905
+ "Latest bundle already exists and will be deleted, download will overwrite it."
906
+ );
907
+ try {
908
+ final Boolean deleted =
909
+ CapacitorUpdaterPlugin.this.implementation.delete(
910
+ latest.getId(),
911
+ true
923
912
  );
924
- }
925
- } catch (final IOException e) {
926
- Log.e(
913
+ if (deleted) {
914
+ Log.i(
927
915
  CapacitorUpdater.TAG,
928
- "Failed to delete failed bundle: " +
929
- latest.getVersionName(),
930
- e
916
+ "Failed bundle deleted: " +
917
+ latest.getVersionName()
931
918
  );
932
919
  }
920
+ } catch (final IOException e) {
921
+ Log.e(
922
+ CapacitorUpdater.TAG,
923
+ "Failed to delete failed bundle: " +
924
+ latest.getVersionName(),
925
+ e
926
+ );
933
927
  }
934
928
  }
929
+ }
935
930
 
936
- new Thread(
937
- new Runnable() {
938
- @Override
939
- public void run() {
940
- try {
941
- Log.i(
942
- CapacitorUpdater.TAG,
943
- "New bundle: " +
944
- latestVersionName +
945
- " found. Current is: " +
946
- current.getVersionName() +
947
- ". Update will occur next time app moves to background."
948
- );
931
+ new Thread(
932
+ new Runnable() {
933
+ @Override
934
+ public void run() {
935
+ try {
936
+ Log.i(
937
+ CapacitorUpdater.TAG,
938
+ "New bundle: " +
939
+ latestVersionName +
940
+ " found. Current is: " +
941
+ current.getVersionName() +
942
+ ". Update will occur next time app moves to background."
943
+ );
949
944
 
950
- final String url = res.getString("url");
951
- final String session_key = res.has("session_key")
952
- ? res.getString("session_key")
953
- : "";
954
- final BundleInfo next =
955
- CapacitorUpdaterPlugin.this.implementation.download(
956
- url,
957
- latestVersionName,
958
- session_key
959
- );
960
- final String checksum = res.has("checksum")
961
- ? res.getString("checksum")
962
- : "";
963
- if (
964
- !checksum.equals("") &&
965
- !next.getChecksum().equals(checksum)
966
- ) {
967
- Log.e(
968
- CapacitorUpdater.TAG,
969
- "Error checksum " +
970
- next.getChecksum() +
971
- " " +
972
- checksum
945
+ final String url = res.getString("url");
946
+ final String session_key = res.has("session_key")
947
+ ? res.getString("session_key")
948
+ : "";
949
+ final BundleInfo next =
950
+ CapacitorUpdaterPlugin.this.implementation.download(
951
+ url,
952
+ latestVersionName,
953
+ session_key
973
954
  );
974
- CapacitorUpdaterPlugin.this.implementation.sendStats(
975
- "checksum_fail",
976
- current.getVersionName()
977
- );
978
- final Boolean res =
979
- CapacitorUpdaterPlugin.this.implementation.delete(
980
- next.getId()
981
- );
982
- if (res) {
983
- Log.i(
984
- CapacitorUpdater.TAG,
985
- "Failed bundle deleted: " +
986
- next.getVersionName()
987
- );
988
- }
989
- return;
990
- }
991
- final JSObject ret = new JSObject();
992
- ret.put("bundle", next.toJSON());
993
- CapacitorUpdaterPlugin.this.notifyListeners(
994
- "updateAvailable",
995
- ret
996
- );
997
- CapacitorUpdaterPlugin.this.implementation.setNextBundle(
998
- next.getId()
999
- );
1000
- } catch (final Exception e) {
955
+ final String checksum = res.has("checksum")
956
+ ? res.getString("checksum")
957
+ : "";
958
+ if (
959
+ !checksum.equals("") &&
960
+ !next.getChecksum().equals(checksum)
961
+ ) {
1001
962
  Log.e(
1002
963
  CapacitorUpdater.TAG,
1003
- "error downloading file",
1004
- e
964
+ "Error checksum " +
965
+ next.getChecksum() +
966
+ " " +
967
+ checksum
1005
968
  );
1006
- final JSObject ret = new JSObject();
1007
- ret.put("version", latestVersionName);
1008
- CapacitorUpdaterPlugin.this.notifyListeners(
1009
- "downloadFailed",
1010
- ret
1011
- );
1012
- final BundleInfo current =
1013
- CapacitorUpdaterPlugin.this.implementation.getCurrentBundle();
1014
969
  CapacitorUpdaterPlugin.this.implementation.sendStats(
1015
- "download_fail",
970
+ "checksum_fail",
1016
971
  current.getVersionName()
1017
972
  );
1018
- final JSObject retNoNeed = new JSObject();
1019
- retNoNeed.put("bundle", current.toJSON());
1020
- CapacitorUpdaterPlugin.this.notifyListeners(
1021
- "noNeedUpdate",
1022
- retNoNeed
973
+ final Boolean res =
974
+ CapacitorUpdaterPlugin.this.implementation.delete(
975
+ next.getId()
976
+ );
977
+ if (res) {
978
+ Log.i(
979
+ CapacitorUpdater.TAG,
980
+ "Failed bundle deleted: " +
981
+ next.getVersionName()
1023
982
  );
983
+ }
984
+ return;
1024
985
  }
986
+ final JSObject ret = new JSObject();
987
+ ret.put("bundle", next.toJSON());
988
+ CapacitorUpdaterPlugin.this.notifyListeners(
989
+ "updateAvailable",
990
+ ret
991
+ );
992
+ CapacitorUpdaterPlugin.this.implementation.setNextBundle(
993
+ next.getId()
994
+ );
995
+ } catch (final Exception e) {
996
+ Log.e(
997
+ CapacitorUpdater.TAG,
998
+ "error downloading file",
999
+ e
1000
+ );
1001
+ final JSObject ret = new JSObject();
1002
+ ret.put("version", latestVersionName);
1003
+ CapacitorUpdaterPlugin.this.notifyListeners(
1004
+ "downloadFailed",
1005
+ ret
1006
+ );
1007
+ final BundleInfo current =
1008
+ CapacitorUpdaterPlugin.this.implementation.getCurrentBundle();
1009
+ CapacitorUpdaterPlugin.this.implementation.sendStats(
1010
+ "download_fail",
1011
+ current.getVersionName()
1012
+ );
1013
+ final JSObject retNoNeed = new JSObject();
1014
+ retNoNeed.put("bundle", current.toJSON());
1015
+ CapacitorUpdaterPlugin.this.notifyListeners(
1016
+ "noNeedUpdate",
1017
+ retNoNeed
1018
+ );
1025
1019
  }
1026
1020
  }
1027
- )
1028
- .start();
1029
- } else {
1030
- Log.i(
1031
- CapacitorUpdater.TAG,
1032
- "No need to update, " +
1033
- current.getId() +
1034
- " is the latest bundle."
1035
- );
1036
- final JSObject retNoNeed = new JSObject();
1037
- retNoNeed.put("bundle", current.toJSON());
1038
- CapacitorUpdaterPlugin.this.notifyListeners(
1039
- "noNeedUpdate",
1040
- retNoNeed
1041
- );
1042
- }
1043
- } catch (final JSONException e) {
1044
- Log.e(CapacitorUpdater.TAG, "error parsing JSON", e);
1021
+ }
1022
+ )
1023
+ .start();
1024
+ } else {
1025
+ Log.i(
1026
+ CapacitorUpdater.TAG,
1027
+ "No need to update, " +
1028
+ current.getId() +
1029
+ " is the latest bundle."
1030
+ );
1045
1031
  final JSObject retNoNeed = new JSObject();
1046
1032
  retNoNeed.put("bundle", current.toJSON());
1047
1033
  CapacitorUpdaterPlugin.this.notifyListeners(
@@ -1049,14 +1035,28 @@ public class CapacitorUpdaterPlugin
1049
1035
  retNoNeed
1050
1036
  );
1051
1037
  }
1038
+ } catch (final JSONException e) {
1039
+ Log.e(CapacitorUpdater.TAG, "error parsing JSON", e);
1040
+ final JSObject retNoNeed = new JSObject();
1041
+ retNoNeed.put("bundle", current.toJSON());
1042
+ CapacitorUpdaterPlugin.this.notifyListeners(
1043
+ "noNeedUpdate",
1044
+ retNoNeed
1045
+ );
1052
1046
  }
1053
- );
1054
- }
1047
+ }
1048
+ );
1055
1049
  }
1056
- )
1057
- .start();
1058
- }
1050
+ }
1051
+ )
1052
+ .start();
1053
+ }
1059
1054
 
1055
+ @Override // appMovedToForeground
1056
+ public void onActivityStarted(@NonNull final Activity activity) {
1057
+ if (CapacitorUpdaterPlugin.this._isAutoUpdateEnabled()) {
1058
+ this.backgroundDownload();
1059
+ }
1060
1060
  this.checkAppReady();
1061
1061
  }
1062
1062
 
@@ -28,7 +28,7 @@ public class SetChannel: NSObject {
28
28
  }
29
29
  extension SetChannel {
30
30
  func toDict() -> [String: Any] {
31
- var dict: [String : Any] = [String: Any]()
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 : Any] = [String: Any]()
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 : Any] = [String: Any]()
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: String = "https://api.capgo.app/updates"
13
- static let statsUrlDefault: String = "https://api.capgo.app/stats"
14
- static let channelUrlDefault: String = "https://api.capgo.app/channel_self"
15
- let DELAY_CONDITION_PREFERENCES: String = ""
16
- private var updateUrl: String = ""
17
- private var statsUrl: String = ""
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: Bool = false
20
- private var appReadyTimeout: Int = 10000
20
+ private var autoUpdate = false
21
+ private var appReadyTimeout = 10000
21
22
  private var appReadyCheck: DispatchWorkItem?
22
- private var resetWhenUpdate: Bool = true
23
- private var autoDeleteFailed: Bool = false
24
- private var autoDeletePrevious: Bool = false
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: Bool = false
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: NotificationCenter = NotificationCenter.default
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: String = UserDefaults.standard.string(forKey: DELAY_CONDITION_PREFERENCES) ?? "[]"
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,92 +466,110 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
465
466
  self.appReadyCheck = nil
466
467
  }
467
468
 
468
- @objc func appMovedToForeground() {
469
- if backgroundWork != nil && taskRunning {
470
- backgroundWork!.cancel()
471
- print("\(self.implementation.TAG) Background Timer Task canceled, Activity resumed before timer completes")
472
- }
473
- if self._isAutoUpdateEnabled() {
474
- DispatchQueue.global(qos: .background).async {
475
- print("\(self.implementation.TAG) Check for update via \(self.updateUrl)")
476
- let url: URL = URL(string: self.updateUrl)!
477
- let res = self.implementation.getLatest(url: url)
478
- let current = self.implementation.getCurrentBundle()
479
-
480
- if (res.message) != nil {
481
- print("\(self.implementation.TAG) message \(res.message ?? "")")
482
- if res.major == true {
483
- self.notifyListeners("majorAvailable", data: ["version": res.version])
484
- }
485
- self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
486
- return
487
- }
488
- let sessionKey = res.sessionKey ?? ""
489
- guard let downloadUrl = URL(string: res.url) else {
490
- print("\(self.implementation.TAG) Error no url or wrong format")
491
- self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
492
- return
469
+ func endBackGroundTask() {
470
+ UIApplication.shared.endBackgroundTask(self.backgroundTaskID)
471
+ self.backgroundTaskID = UIBackgroundTaskIdentifier.invalid
472
+ }
473
+
474
+ func backgroundDownload() {
475
+ DispatchQueue.global(qos: .background).async {
476
+ self.backgroundTaskID = UIApplication.shared.beginBackgroundTask(withName: "Finish Download Tasks") {
477
+ // End the task if time expires.
478
+ self.endBackGroundTask()
479
+ }
480
+ print("\(self.implementation.TAG) Check for update via \(self.updateUrl)")
481
+ let url = URL(string: self.updateUrl)!
482
+ let res = self.implementation.getLatest(url: url)
483
+ let current = self.implementation.getCurrentBundle()
484
+
485
+ if (res.message) != nil {
486
+ print("\(self.implementation.TAG) message \(res.message ?? "")")
487
+ if res.major == true {
488
+ self.notifyListeners("majorAvailable", data: ["version": res.version])
493
489
  }
494
- let latestVersionName = res.version
495
- if latestVersionName != "" && current.getVersionName() != latestVersionName {
496
- let latest = self.implementation.getBundleInfoByVersionName(version: latestVersionName)
497
- if latest != nil {
498
- if latest!.isErrorStatus() {
499
- print("\(self.implementation.TAG) Latest version already exists, and is in error state. Aborting update.")
500
- self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
501
- return
502
- }
503
- if latest!.isDownloaded() {
504
- print("\(self.implementation.TAG) Latest version already exists and download is NOT required. Update will occur next time app moves to background.")
505
- self.notifyListeners("updateAvailable", data: ["bundle": current.toJSON()])
506
- _ = self.implementation.setNextBundle(next: latest!.getId())
507
- return
508
- }
509
- if latest!.isDeleted() {
510
- print("\(self.implementation.TAG) Latest bundle already exists and will be deleted, download will overwrite it.")
511
- let res = self.implementation.delete(id: latest!.getId(), removeInfo: true)
512
- if !res {
513
- print("\(self.implementation.TAG) Delete version deleted: \(latest!.toString())")
514
- } else {
515
- print("\(self.implementation.TAG) Failed to delete failed bundle: \(latest!.toString())")
516
- }
490
+ self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
491
+ self.endBackGroundTask()
492
+ return
493
+ }
494
+ let sessionKey = res.sessionKey ?? ""
495
+ guard let downloadUrl = URL(string: res.url) else {
496
+ print("\(self.implementation.TAG) Error no url or wrong format")
497
+ self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
498
+ self.endBackGroundTask()
499
+ return
500
+ }
501
+ let latestVersionName = res.version
502
+ if latestVersionName != "" && current.getVersionName() != latestVersionName {
503
+ let latest = self.implementation.getBundleInfoByVersionName(version: latestVersionName)
504
+ if latest != nil {
505
+ if latest!.isErrorStatus() {
506
+ print("\(self.implementation.TAG) Latest version already exists, and is in error state. Aborting update.")
507
+ self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
508
+ self.endBackGroundTask()
509
+ return
510
+ }
511
+ if latest!.isDownloaded() {
512
+ print("\(self.implementation.TAG) Latest version already exists and download is NOT required. Update will occur next time app moves to background.")
513
+ self.notifyListeners("updateAvailable", data: ["bundle": current.toJSON()])
514
+ _ = self.implementation.setNextBundle(next: latest!.getId())
515
+ self.endBackGroundTask()
516
+ return
517
+ }
518
+ if latest!.isDeleted() {
519
+ print("\(self.implementation.TAG) Latest bundle already exists and will be deleted, download will overwrite it.")
520
+ let res = self.implementation.delete(id: latest!.getId(), removeInfo: true)
521
+ if !res {
522
+ print("\(self.implementation.TAG) Delete version deleted: \(latest!.toString())")
523
+ } else {
524
+ print("\(self.implementation.TAG) Failed to delete failed bundle: \(latest!.toString())")
517
525
  }
518
526
  }
527
+ }
519
528
 
520
- do {
521
- print("\(self.implementation.TAG) New bundle: \(latestVersionName) found. Current is: \(current.getVersionName()). Update will occur next time app moves to background.")
522
- let next = try self.implementation.download(url: downloadUrl, version: latestVersionName, sessionKey: sessionKey)
523
- if res.checksum != "" && next.getChecksum() != res.checksum {
524
- print("\(self.implementation.TAG) Error checksum", next.getChecksum(), res.checksum)
525
- self.implementation.sendStats(action: "checksum_fail", versionName: next.getVersionName())
526
- let resDel = self.implementation.delete(id: next.getId())
527
- if !resDel {
528
- print("\(self.implementation.TAG) Delete failed, id \(next.getId()) doesn't exist")
529
- }
530
- return
529
+ do {
530
+ print("\(self.implementation.TAG) New bundle: \(latestVersionName) found. Current is: \(current.getVersionName()). Update will occur next time app moves to background.")
531
+ let next = try self.implementation.download(url: downloadUrl, version: latestVersionName, sessionKey: sessionKey)
532
+ if res.checksum != "" && next.getChecksum() != res.checksum {
533
+ print("\(self.implementation.TAG) Error checksum", next.getChecksum(), res.checksum)
534
+ self.implementation.sendStats(action: "checksum_fail", versionName: next.getVersionName())
535
+ let resDel = self.implementation.delete(id: next.getId())
536
+ if !resDel {
537
+ print("\(self.implementation.TAG) Delete failed, id \(next.getId()) doesn't exist")
531
538
  }
532
- self.notifyListeners("updateAvailable", data: ["bundle": next.toJSON()])
533
- _ = self.implementation.setNextBundle(next: next.getId())
534
- } catch {
535
- print("\(self.implementation.TAG) Error downloading file", error.localizedDescription)
536
- let current: BundleInfo = self.implementation.getCurrentBundle()
537
- self.implementation.sendStats(action: "download_fail", versionName: current.getVersionName())
538
- self.notifyListeners("downloadFailed", data: ["version": latestVersionName])
539
- self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
539
+ self.endBackGroundTask()
540
+ return
540
541
  }
541
- } else {
542
- print("\(self.implementation.TAG) No need to update, \(current.getId()) is the latest bundle.")
542
+ self.notifyListeners("updateAvailable", data: ["bundle": next.toJSON()])
543
+ _ = self.implementation.setNextBundle(next: next.getId())
544
+ } catch {
545
+ print("\(self.implementation.TAG) Error downloading file", error.localizedDescription)
546
+ let current: BundleInfo = self.implementation.getCurrentBundle()
547
+ self.implementation.sendStats(action: "download_fail", versionName: current.getVersionName())
548
+ self.notifyListeners("downloadFailed", data: ["version": latestVersionName])
543
549
  self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
544
550
  }
551
+ } else {
552
+ print("\(self.implementation.TAG) No need to update, \(current.getId()) is the latest bundle.")
553
+ self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
545
554
  }
555
+ self.endBackGroundTask()
546
556
  }
557
+ }
547
558
 
559
+ @objc func appMovedToForeground() {
560
+ if backgroundWork != nil && taskRunning {
561
+ backgroundWork!.cancel()
562
+ print("\(self.implementation.TAG) Background Timer Task canceled, Activity resumed before timer completes")
563
+ }
564
+ if self._isAutoUpdateEnabled() {
565
+ self.backgroundDownload()
566
+ }
548
567
  self.checkAppReady()
549
568
  }
550
569
 
551
570
  @objc func appMovedToBackground() {
552
571
  print("\(self.implementation.TAG) Check for pending update")
553
- let delayUpdatePreferences: String = UserDefaults.standard.string(forKey: DELAY_CONDITION_PREFERENCES) ?? "[]"
572
+ let delayUpdatePreferences = UserDefaults.standard.string(forKey: DELAY_CONDITION_PREFERENCES) ?? "[]"
554
573
 
555
574
  let delayConditionList: [DelayCondition] = fromJsonArr(json: delayUpdatePreferences).map { obj -> DelayCondition in
556
575
  let kind: String = obj.value(forKey: "kind") as! String
@@ -587,7 +606,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
587
606
  }
588
607
 
589
608
  private func installNext() {
590
- let delayUpdatePreferences: String = UserDefaults.standard.string(forKey: DELAY_CONDITION_PREFERENCES) ?? "[]"
609
+ let delayUpdatePreferences = UserDefaults.standard.string(forKey: DELAY_CONDITION_PREFERENCES) ?? "[]"
591
610
  let delayConditionList: [DelayCondition]? = fromJsonArr(json: delayUpdatePreferences).map { obj -> DelayCondition in
592
611
  let kind: String = obj.value(forKey: "kind") as! String
593
612
  let value: String? = obj.value(forKey: "value") as? String
@@ -612,15 +631,15 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
612
631
  }
613
632
 
614
633
  @objc private func toJson(object: Any) -> String {
615
- guard let data: Data = try? JSONSerialization.data(withJSONObject: object, options: []) else {
634
+ guard let data = try? JSONSerialization.data(withJSONObject: object, options: []) else {
616
635
  return ""
617
636
  }
618
637
  return String(data: data, encoding: String.Encoding.utf8) ?? ""
619
638
  }
620
639
 
621
640
  @objc private func fromJsonArr(json: String) -> [NSObject] {
622
- let jsonData: Data = json.data(using: .utf8)!
623
- let object: [NSObject]? = try? JSONSerialization.jsonObject(
641
+ let jsonData = json.data(using: .utf8)!
642
+ let object = try? JSONSerialization.jsonObject(
624
643
  with: jsonData,
625
644
  options: .mutableContainers
626
645
  ) as? [NSObject]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-updater",
3
- "version": "4.13.6",
3
+ "version": "4.13.8",
4
4
  "license": "LGPL-3.0-only",
5
5
  "description": "OTA update for capacitor apps",
6
6
  "main": "dist/plugin.cjs.js",