@capgo/capacitor-updater 3.3.11 → 4.0.0-alpha.10

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.
@@ -0,0 +1,130 @@
1
+ package ee.forgr.capacitor_updater;
2
+
3
+ import com.getcapacitor.JSObject;
4
+
5
+ import org.json.JSONException;
6
+ import org.json.JSONObject;
7
+ import org.json.JSONTokener;
8
+
9
+ import java.text.SimpleDateFormat;
10
+ import java.util.Date;
11
+ import java.util.Objects;
12
+ import java.util.TimeZone;
13
+
14
+ public class BundleInfo {
15
+ private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
16
+
17
+ public static final String ID_BUILTIN = "builtin";
18
+ public static final String VERSION_UNKNOWN = "unknown";
19
+ public static final String DOWNLOADED_BUILTIN = "1970-01-01T00:00:00.000Z";
20
+
21
+ private final String downloaded;
22
+ private final String id;
23
+ private final String version;
24
+ private final BundleStatus status;
25
+
26
+ static {
27
+ sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
28
+ }
29
+
30
+ public BundleInfo(final BundleInfo source) {
31
+ this(source.id, source.version, source.status, source.downloaded);
32
+ }
33
+
34
+ public BundleInfo(final String id, final String version, final BundleStatus status, final Date downloaded) {
35
+ this(id, version, status, sdf.format(downloaded));
36
+ }
37
+
38
+ public BundleInfo(final String id, final String version, final BundleStatus status, final String downloaded) {
39
+ this.downloaded = downloaded.trim();
40
+ this.id = id;
41
+ this.version = version;
42
+ this.status = status;
43
+ }
44
+
45
+ public Boolean isBuiltin() {
46
+ return ID_BUILTIN.equals(this.id);
47
+ }
48
+ public Boolean isUnknown() {
49
+ return VERSION_UNKNOWN.equals(this.id);
50
+ }
51
+ public Boolean isErrorStatus() {
52
+ return BundleStatus.ERROR == this.status;
53
+ }
54
+ public boolean isDownloaded() {
55
+ return !this.isBuiltin() && this.downloaded != null && !this.downloaded.equals("");
56
+ }
57
+
58
+ public String getDownloaded() {
59
+ return this.isBuiltin() ? DOWNLOADED_BUILTIN : this.downloaded;
60
+ }
61
+
62
+ public BundleInfo setDownloaded(Date downloaded) {
63
+ return new BundleInfo(this.id, this.version, this.status, downloaded);
64
+ }
65
+
66
+ public String getId() {
67
+ return this.isBuiltin() ? ID_BUILTIN : this.id;
68
+ }
69
+
70
+ public BundleInfo setId(String id) {
71
+ return new BundleInfo(id, this.version, this.status, this.downloaded);
72
+ }
73
+
74
+ public String getVersionName() {
75
+ return this.version == null ? ID_BUILTIN : this.version;
76
+ }
77
+
78
+ public BundleInfo setVersionName(String version) {
79
+ return new BundleInfo(this.id, version, this.status, this.downloaded);
80
+ }
81
+
82
+ public BundleStatus getStatus() {
83
+ return this.isBuiltin() ? BundleStatus.SUCCESS : this.status;
84
+ }
85
+
86
+ public BundleInfo setStatus(BundleStatus status) {
87
+ return new BundleInfo(this.id, this.version, status, this.downloaded);
88
+ }
89
+
90
+ public static BundleInfo fromJSON(final JSObject json) throws JSONException {
91
+ return BundleInfo.fromJSON(json.toString());
92
+ }
93
+
94
+ public static BundleInfo fromJSON(final String jsonString) throws JSONException {
95
+ JSONObject json = new JSONObject(new JSONTokener(jsonString));
96
+ return new BundleInfo(
97
+ json.has("id") ? json.getString("id") : "",
98
+ json.has("version") ? json.getString("version") : BundleInfo.VERSION_UNKNOWN,
99
+ json.has("status") ? BundleStatus.fromString(json.getString("status")) : BundleStatus.PENDING,
100
+ json.has("downloaded") ? json.getString("downloaded") : ""
101
+ );
102
+ }
103
+
104
+ public JSObject toJSON() {
105
+ final JSObject result = new JSObject();
106
+ result.put("id", this.getId());
107
+ result.put("version", this.getVersionName());
108
+ result.put("downloaded", this.getDownloaded());
109
+ result.put("status", this.getStatus());
110
+ return result;
111
+ }
112
+
113
+ @Override
114
+ public boolean equals(final Object o) {
115
+ if (this == o) return true;
116
+ if (!(o instanceof BundleInfo)) return false;
117
+ final BundleInfo that = (BundleInfo) o;
118
+ return this.getId().equals(that.getId());
119
+ }
120
+
121
+ @Override
122
+ public int hashCode() {
123
+ return Objects.hash(this.version);
124
+ }
125
+
126
+ @Override
127
+ public String toString() {
128
+ return this.toJSON().toString();
129
+ }
130
+ }
@@ -0,0 +1,36 @@
1
+ package ee.forgr.capacitor_updater;
2
+
3
+ import java.util.HashMap;
4
+ import java.util.Map;
5
+
6
+ public enum BundleStatus {
7
+ SUCCESS("success"),
8
+ ERROR("error"),
9
+ PENDING("pending"),
10
+ DOWNLOADING("donwloading");
11
+
12
+ public final String label;
13
+
14
+ private static final Map<String, BundleStatus> BY_LABEL = new HashMap<>();
15
+ static {
16
+ for (final BundleStatus e: values()) {
17
+ BY_LABEL.put(e.label, e);
18
+ }
19
+ }
20
+
21
+ BundleStatus(final String label) {
22
+ this.label = label;
23
+ }
24
+
25
+ @Override
26
+ public String toString() {
27
+ return this.label;
28
+ }
29
+
30
+ public static BundleStatus fromString(final String status) {
31
+ if(status == null || status.isEmpty()) {
32
+ return BundleStatus.PENDING;
33
+ }
34
+ return BundleStatus.BY_LABEL.get(status);
35
+ }
36
+ }