@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.
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java +1 -1
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +216 -216
- package/ios/Plugin/CapacitorUpdater.swift +3 -3
- package/ios/Plugin/CapacitorUpdaterPlugin.swift +107 -88
- package/package.json +1 -1
|
@@ -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.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
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
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
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
)
|
|
827
|
+
res.has("major") &&
|
|
828
|
+
res.getBoolean("major") &&
|
|
829
|
+
res.has("version")
|
|
854
830
|
) {
|
|
855
|
-
|
|
856
|
-
|
|
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
|
-
"
|
|
863
|
-
|
|
834
|
+
"majorAvailable",
|
|
835
|
+
majorAvailable
|
|
864
836
|
);
|
|
865
837
|
}
|
|
866
|
-
final
|
|
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
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
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
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
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
|
-
|
|
897
|
-
|
|
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
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
latest.
|
|
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
|
-
|
|
926
|
-
Log.e(
|
|
913
|
+
if (deleted) {
|
|
914
|
+
Log.i(
|
|
927
915
|
CapacitorUpdater.TAG,
|
|
928
|
-
"Failed
|
|
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
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
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
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
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
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
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
|
-
"
|
|
1004
|
-
|
|
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
|
-
"
|
|
970
|
+
"checksum_fail",
|
|
1016
971
|
current.getVersionName()
|
|
1017
972
|
);
|
|
1018
|
-
final
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
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
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
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
|
-
|
|
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
|
|
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,92 +466,110 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
|
|
|
465
466
|
self.appReadyCheck = nil
|
|
466
467
|
}
|
|
467
468
|
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
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
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
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
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
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.
|
|
533
|
-
|
|
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
|
-
|
|
542
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
623
|
-
let object
|
|
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]
|