@capgo/capacitor-updater 6.51.23 → 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 +201 -66
- package/android/build.gradle +2 -3
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +593 -176
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +775 -162
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +2 -1
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +226 -106
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +107 -30
- 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 +472 -144
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoSemanticVersion.swift +162 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +392 -82
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +2 -1
- 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 +5 -2
- package/ios/Sources/CapacitorUpdaterPlugin/ZipArchiveReader.swift +247 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ZipCentralDirectory.swift +280 -0
- package/package.json +7 -3
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
package ee.forgr.capacitor_updater;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import android.app.AlertDialog;
|
|
5
|
+
import android.content.Intent;
|
|
6
|
+
import android.content.SharedPreferences;
|
|
7
|
+
import android.net.Uri;
|
|
8
|
+
import android.os.Handler;
|
|
9
|
+
import android.os.Looper;
|
|
10
|
+
import com.getcapacitor.JSObject;
|
|
11
|
+
import com.getcapacitor.PluginConfig;
|
|
12
|
+
import com.google.android.play.core.appupdate.AppUpdateInfo;
|
|
13
|
+
import com.google.android.play.core.appupdate.AppUpdateManager;
|
|
14
|
+
import com.google.android.play.core.appupdate.AppUpdateOptions;
|
|
15
|
+
import com.google.android.play.core.install.model.AppUpdateType;
|
|
16
|
+
import com.google.android.play.core.install.model.UpdateAvailability;
|
|
17
|
+
import java.util.HashSet;
|
|
18
|
+
import java.util.Locale;
|
|
19
|
+
import java.util.Set;
|
|
20
|
+
|
|
21
|
+
final class NativeUpdatePrompt {
|
|
22
|
+
|
|
23
|
+
static final String PREF_LAST_NATIVE_VERSION = "CapacitorUpdater.nativeUpdatePromptLastNativeVersion";
|
|
24
|
+
static final String PREF_LAST_SHOWN_MS = "CapacitorUpdater.nativeUpdatePromptLastShownMs";
|
|
25
|
+
|
|
26
|
+
private static final Set<String> SESSION_SHOWN_NATIVE_VERSIONS = new HashSet<>();
|
|
27
|
+
|
|
28
|
+
static void resetSessionStateForTesting() {
|
|
29
|
+
SESSION_SHOWN_NATIVE_VERSIONS.clear();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
void resetPresentationStateForTesting() {
|
|
33
|
+
this.promptPresentationActive = false;
|
|
34
|
+
if (this.pendingShowRunnable != null) {
|
|
35
|
+
this.mainHandler.removeCallbacks(this.pendingShowRunnable);
|
|
36
|
+
this.pendingShowRunnable = null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
boolean isPromptPresentationActiveForTesting() {
|
|
41
|
+
return this.promptPresentationActive;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private final boolean enabled;
|
|
45
|
+
private final boolean onStoreCheck;
|
|
46
|
+
private final String title;
|
|
47
|
+
private final String message;
|
|
48
|
+
private final String updateButton;
|
|
49
|
+
private final String laterButton;
|
|
50
|
+
private final String countryFilter;
|
|
51
|
+
private final long cooldownMs;
|
|
52
|
+
private final SharedPreferences prefs;
|
|
53
|
+
private final Logger logger;
|
|
54
|
+
private final NativeUpdatePromptHost host;
|
|
55
|
+
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
56
|
+
private volatile boolean promptPresentationActive = false;
|
|
57
|
+
private Runnable pendingShowRunnable;
|
|
58
|
+
|
|
59
|
+
interface NativeUpdatePromptHost {
|
|
60
|
+
Activity getActivity();
|
|
61
|
+
|
|
62
|
+
String getNativeBuildVersion();
|
|
63
|
+
|
|
64
|
+
AppUpdateManager getAppUpdateManager();
|
|
65
|
+
|
|
66
|
+
AppUpdateInfo getCachedAppUpdateInfo();
|
|
67
|
+
|
|
68
|
+
void setCachedAppUpdateInfo(AppUpdateInfo info);
|
|
69
|
+
|
|
70
|
+
String getPackageNameForStore();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
NativeUpdatePrompt(final PluginConfig config, final SharedPreferences prefs, final Logger logger, final NativeUpdatePromptHost host) {
|
|
74
|
+
this.prefs = prefs;
|
|
75
|
+
this.logger = logger;
|
|
76
|
+
this.host = host;
|
|
77
|
+
this.enabled = config.getBoolean("nativeUpdatePrompt", false);
|
|
78
|
+
this.onStoreCheck = config.getBoolean("nativeUpdatePromptOnStoreCheck", false);
|
|
79
|
+
this.title = config.getString("nativeUpdatePromptTitle", null);
|
|
80
|
+
this.message = config.getString("nativeUpdatePromptMessage", null);
|
|
81
|
+
this.updateButton = config.getString("nativeUpdatePromptUpdateButton", null);
|
|
82
|
+
this.laterButton = config.getString("nativeUpdatePromptLaterButton", null);
|
|
83
|
+
this.countryFilter = config.getString("nativeUpdatePromptCountry", null);
|
|
84
|
+
final int cooldownHours = Math.max(1, config.getInt("nativeUpdatePromptCooldownHours", 24));
|
|
85
|
+
this.cooldownMs = (long) cooldownHours * 60L * 60L * 1000L;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
boolean isOnStoreCheckEnabled() {
|
|
89
|
+
return this.enabled && this.onStoreCheck;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static boolean isNativeStoreUpdateBlock(final JSObject response) {
|
|
93
|
+
if (response == null) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
if (response.optBoolean("breaking", false)) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
if ("store_update_required".equals(response.optString("message", ""))) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
final String error = response.optString("error", "");
|
|
103
|
+
return error.startsWith("disable_auto_update");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
void maybeShowForUpdateBlock(final JSObject response) {
|
|
107
|
+
if (!this.enabled || !isNativeStoreUpdateBlock(response)) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
this.scheduleShow();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
void maybeShowForStoreUpdateAvailable() {
|
|
114
|
+
if (!this.isOnStoreCheckEnabled()) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
this.scheduleShow();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private void scheduleShow() {
|
|
121
|
+
if (!this.matchesCountryFilter()) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
final String nativeVersion = this.host.getNativeBuildVersion();
|
|
125
|
+
if (nativeVersion == null || nativeVersion.isEmpty()) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (!this.shouldShowForNativeVersion(nativeVersion)) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (this.promptPresentationActive) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
this.promptPresentationActive = true;
|
|
135
|
+
if (this.pendingShowRunnable != null) {
|
|
136
|
+
this.mainHandler.removeCallbacks(this.pendingShowRunnable);
|
|
137
|
+
}
|
|
138
|
+
this.pendingShowRunnable = () -> this.showDialog(nativeVersion);
|
|
139
|
+
this.mainHandler.postDelayed(this.pendingShowRunnable, 600);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private void clearPresentationGuard() {
|
|
143
|
+
this.promptPresentationActive = false;
|
|
144
|
+
this.pendingShowRunnable = null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
boolean shouldShowForNativeVersion(final String nativeVersion) {
|
|
148
|
+
if (SESSION_SHOWN_NATIVE_VERSIONS.contains(nativeVersion)) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
final String lastVersion = this.prefs.getString(PREF_LAST_NATIVE_VERSION, "");
|
|
152
|
+
final long lastShown = this.prefs.getLong(PREF_LAST_SHOWN_MS, 0L);
|
|
153
|
+
if (nativeVersion.equals(lastVersion) && lastShown > 0L) {
|
|
154
|
+
final long elapsed = System.currentTimeMillis() - lastShown;
|
|
155
|
+
if (elapsed < this.cooldownMs) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
boolean matchesCountryFilter() {
|
|
163
|
+
if (this.countryFilter == null || this.countryFilter.trim().isEmpty()) {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
final String deviceCountry = Locale.getDefault().getCountry();
|
|
167
|
+
if (deviceCountry == null || deviceCountry.isEmpty()) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
final String normalizedDevice = deviceCountry.toUpperCase(Locale.ROOT);
|
|
171
|
+
final String[] allowed = this.countryFilter.split(",");
|
|
172
|
+
for (final String entry : allowed) {
|
|
173
|
+
if (entry != null && normalizedDevice.equals(entry.trim().toUpperCase(Locale.ROOT))) {
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private void showDialog(final String nativeVersion) {
|
|
181
|
+
final Activity activity = this.host.getActivity();
|
|
182
|
+
if (activity == null || activity.isFinishing()) {
|
|
183
|
+
this.clearPresentationGuard();
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
final String dialogTitle = this.title != null && !this.title.isEmpty() ? this.title : "Update available";
|
|
188
|
+
final String dialogMessage =
|
|
189
|
+
this.message != null && !this.message.isEmpty()
|
|
190
|
+
? this.message
|
|
191
|
+
: "A new version of this app is available in the store. Update now to continue receiving live updates.";
|
|
192
|
+
final String positive = this.updateButton != null && !this.updateButton.isEmpty() ? this.updateButton : "Update";
|
|
193
|
+
final String negative = this.laterButton != null && !this.laterButton.isEmpty() ? this.laterButton : "Not now";
|
|
194
|
+
|
|
195
|
+
try {
|
|
196
|
+
final AlertDialog dialog = new AlertDialog.Builder(activity)
|
|
197
|
+
.setTitle(dialogTitle)
|
|
198
|
+
.setMessage(dialogMessage)
|
|
199
|
+
.setCancelable(true)
|
|
200
|
+
.setPositiveButton(positive, (d, which) -> this.onUpdateSelected())
|
|
201
|
+
.setNegativeButton(negative, (d, which) -> d.dismiss())
|
|
202
|
+
.create();
|
|
203
|
+
final boolean[] recorded = { false };
|
|
204
|
+
final Runnable markRecorded = () -> {
|
|
205
|
+
if (!recorded[0]) {
|
|
206
|
+
recorded[0] = true;
|
|
207
|
+
this.recordShown(nativeVersion);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
dialog.setOnShowListener((d) -> markRecorded.run());
|
|
211
|
+
dialog.setOnCancelListener((d) -> markRecorded.run());
|
|
212
|
+
dialog.setOnDismissListener((d) -> this.clearPresentationGuard());
|
|
213
|
+
dialog.show();
|
|
214
|
+
} catch (final Exception e) {
|
|
215
|
+
this.logger.warn("Could not show native update prompt: " + e.getMessage());
|
|
216
|
+
this.clearPresentationGuard();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
void recordShown(final String nativeVersion) {
|
|
221
|
+
SESSION_SHOWN_NATIVE_VERSIONS.add(nativeVersion);
|
|
222
|
+
this.prefs
|
|
223
|
+
.edit()
|
|
224
|
+
.putString(PREF_LAST_NATIVE_VERSION, nativeVersion)
|
|
225
|
+
.putLong(PREF_LAST_SHOWN_MS, System.currentTimeMillis())
|
|
226
|
+
.apply();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private void onUpdateSelected() {
|
|
230
|
+
final AppUpdateInfo cached = this.host.getCachedAppUpdateInfo();
|
|
231
|
+
if (
|
|
232
|
+
cached != null &&
|
|
233
|
+
cached.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
|
|
234
|
+
cached.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
|
|
235
|
+
) {
|
|
236
|
+
this.startImmediateUpdate(cached);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
final AppUpdateManager manager = this.host.getAppUpdateManager();
|
|
241
|
+
manager
|
|
242
|
+
.getAppUpdateInfo()
|
|
243
|
+
.addOnSuccessListener((appUpdateInfo) -> {
|
|
244
|
+
this.host.setCachedAppUpdateInfo(appUpdateInfo);
|
|
245
|
+
if (
|
|
246
|
+
appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
|
|
247
|
+
appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
|
|
248
|
+
) {
|
|
249
|
+
this.startImmediateUpdate(appUpdateInfo);
|
|
250
|
+
} else {
|
|
251
|
+
this.openPlayStore();
|
|
252
|
+
}
|
|
253
|
+
})
|
|
254
|
+
.addOnFailureListener((e) -> this.openPlayStore());
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private void startImmediateUpdate(final AppUpdateInfo appUpdateInfo) {
|
|
258
|
+
final Activity activity = this.host.getActivity();
|
|
259
|
+
if (activity == null || activity.isFinishing()) {
|
|
260
|
+
this.openPlayStore();
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
try {
|
|
264
|
+
final AppUpdateManager manager = this.host.getAppUpdateManager();
|
|
265
|
+
final boolean started = manager.startUpdateFlowForResult(
|
|
266
|
+
appUpdateInfo,
|
|
267
|
+
activity,
|
|
268
|
+
AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build(),
|
|
269
|
+
CapacitorUpdaterPlugin.APP_UPDATE_REQUEST_CODE
|
|
270
|
+
);
|
|
271
|
+
if (!started) {
|
|
272
|
+
this.openPlayStore();
|
|
273
|
+
}
|
|
274
|
+
} catch (final Exception e) {
|
|
275
|
+
this.logger.warn("Immediate update from prompt failed: " + e.getMessage());
|
|
276
|
+
this.openPlayStore();
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
private void openPlayStore() {
|
|
281
|
+
final String packageName = this.host.getPackageNameForStore();
|
|
282
|
+
final Activity activity = this.host.getActivity();
|
|
283
|
+
if (activity == null) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName));
|
|
288
|
+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
289
|
+
activity.startActivity(intent);
|
|
290
|
+
} catch (final android.content.ActivityNotFoundException e) {
|
|
291
|
+
try {
|
|
292
|
+
final Intent intent = new Intent(
|
|
293
|
+
Intent.ACTION_VIEW,
|
|
294
|
+
Uri.parse("https://play.google.com/store/apps/details?id=" + packageName)
|
|
295
|
+
);
|
|
296
|
+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
297
|
+
activity.startActivity(intent);
|
|
298
|
+
} catch (final Exception ex) {
|
|
299
|
+
this.logger.error("Failed to open Play Store from native update prompt: " + ex.getMessage());
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
+
*
|
|
6
|
+
* Parsing and comparison logic ported from version-compare 1.5.0
|
|
7
|
+
* (https://github.com/G00fY2/version-compare), Copyright G00fY2,
|
|
8
|
+
* licensed under the Apache License, Version 2.0
|
|
9
|
+
* (https://www.apache.org/licenses/LICENSE-2.0).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
package ee.forgr.capacitor_updater;
|
|
13
|
+
|
|
14
|
+
import androidx.annotation.NonNull;
|
|
15
|
+
import androidx.annotation.Nullable;
|
|
16
|
+
import java.util.ArrayList;
|
|
17
|
+
import java.util.List;
|
|
18
|
+
import java.util.Locale;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Lenient version parser and comparator.
|
|
22
|
+
*
|
|
23
|
+
* <p>Behavior-compatible port of {@code io.github.g00fy2:versioncompare:1.5.0} (Apache-2.0), kept in-repo so the
|
|
24
|
+
* plugin does not ship an extra third-party dependency. Parsing never throws for non-null input that starts with a
|
|
25
|
+
* digit; unparsable input (null or not starting with a digit) yields an empty version equal to {@code 0}.
|
|
26
|
+
*
|
|
27
|
+
* <p>Pre-release suffix ordering: {@code snapshot < pre-alpha < alpha < beta < rc < stable}, with an optional number
|
|
28
|
+
* right after the qualifier ({@code 1.0.0-rc2 > 1.0.0-rc1}).
|
|
29
|
+
*/
|
|
30
|
+
public class Version implements Comparable<Version> {
|
|
31
|
+
|
|
32
|
+
enum ReleaseType {
|
|
33
|
+
SNAPSHOT,
|
|
34
|
+
PRE_ALPHA,
|
|
35
|
+
ALPHA,
|
|
36
|
+
BETA,
|
|
37
|
+
RC,
|
|
38
|
+
STABLE
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private static final String SNAPSHOT_STRING = "snapshot";
|
|
42
|
+
private static final String PRE_STRING = "pre";
|
|
43
|
+
private static final String ALPHA_STRING = "alpha";
|
|
44
|
+
private static final String BETA_STRING = "beta";
|
|
45
|
+
private static final String RC_STRING = "rc";
|
|
46
|
+
|
|
47
|
+
@Nullable
|
|
48
|
+
private final String originalString;
|
|
49
|
+
|
|
50
|
+
private final List<Long> subversionNumbers = new ArrayList<>();
|
|
51
|
+
private final List<Long> trimmedSubversionNumbers = new ArrayList<>();
|
|
52
|
+
private final String suffix;
|
|
53
|
+
private final ReleaseType releaseType;
|
|
54
|
+
private final long preReleaseVersion;
|
|
55
|
+
|
|
56
|
+
public Version(@Nullable String versionString) {
|
|
57
|
+
originalString = versionString;
|
|
58
|
+
if (originalString != null && startsNumeric(originalString)) {
|
|
59
|
+
String[] versionTokens = originalString.replaceAll("\\s", "").split("\\.");
|
|
60
|
+
boolean suffixFound = false;
|
|
61
|
+
StringBuilder suffixSb = null;
|
|
62
|
+
|
|
63
|
+
for (String versionToken : versionTokens) {
|
|
64
|
+
if (suffixFound) {
|
|
65
|
+
suffixSb.append(".");
|
|
66
|
+
suffixSb.append(versionToken);
|
|
67
|
+
} else if (isNumeric(versionToken)) {
|
|
68
|
+
subversionNumbers.add(safeParseLong(versionToken));
|
|
69
|
+
} else {
|
|
70
|
+
for (int i = 0; i < versionToken.length(); i++) {
|
|
71
|
+
if (!Character.isDigit(versionToken.charAt(i))) {
|
|
72
|
+
suffixSb = new StringBuilder();
|
|
73
|
+
if (i > 0) {
|
|
74
|
+
subversionNumbers.add(safeParseLong(versionToken.substring(0, i)));
|
|
75
|
+
suffixSb.append(versionToken.substring(i));
|
|
76
|
+
} else {
|
|
77
|
+
suffixSb.append(versionToken);
|
|
78
|
+
}
|
|
79
|
+
suffixFound = true;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
suffix = suffixSb != null ? suffixSb.toString() : "";
|
|
86
|
+
trimmedSubversionNumbers.addAll(subversionNumbers);
|
|
87
|
+
while (!trimmedSubversionNumbers.isEmpty() && trimmedSubversionNumbers.get(trimmedSubversionNumbers.size() - 1) == 0L) {
|
|
88
|
+
trimmedSubversionNumbers.remove(trimmedSubversionNumbers.size() - 1);
|
|
89
|
+
}
|
|
90
|
+
} else {
|
|
91
|
+
suffix = "";
|
|
92
|
+
}
|
|
93
|
+
releaseType = qualifierToReleaseType(suffix);
|
|
94
|
+
preReleaseVersion = preReleaseVersion(suffix, releaseType);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
public long getMajor() {
|
|
98
|
+
return trimmedSubversionNumbers.size() > 0 ? trimmedSubversionNumbers.get(0) : 0L;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
public long getMinor() {
|
|
102
|
+
return trimmedSubversionNumbers.size() > 1 ? trimmedSubversionNumbers.get(1) : 0L;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
public long getPatch() {
|
|
106
|
+
return trimmedSubversionNumbers.size() > 2 ? trimmedSubversionNumbers.get(2) : 0L;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
@NonNull
|
|
110
|
+
public List<Long> getSubversionNumbers() {
|
|
111
|
+
return subversionNumbers;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
@NonNull
|
|
115
|
+
public String getSuffix() {
|
|
116
|
+
return suffix;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
@Nullable
|
|
120
|
+
public String getOriginalString() {
|
|
121
|
+
return originalString;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
public boolean isHigherThan(String otherVersion) {
|
|
125
|
+
return isHigherThan(new Version(otherVersion));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
public boolean isHigherThan(Version otherVersion) {
|
|
129
|
+
return compareTo(otherVersion) > 0;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
public boolean isLowerThan(String otherVersion) {
|
|
133
|
+
return isLowerThan(new Version(otherVersion));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public boolean isLowerThan(Version otherVersion) {
|
|
137
|
+
return compareTo(otherVersion) < 0;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
public boolean isEqual(String otherVersion) {
|
|
141
|
+
return isEqual(new Version(otherVersion));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public boolean isEqual(Version otherVersion) {
|
|
145
|
+
return compareTo(otherVersion) == 0;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public boolean isAtLeast(String otherVersion) {
|
|
149
|
+
return isAtLeast(new Version(otherVersion));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public boolean isAtLeast(Version otherVersion) {
|
|
153
|
+
return compareTo(otherVersion) >= 0;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public boolean isAtLeast(String otherVersion, boolean ignoreSuffix) {
|
|
157
|
+
return isAtLeast(new Version(otherVersion), ignoreSuffix);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
public boolean isAtLeast(Version otherVersion, boolean ignoreSuffix) {
|
|
161
|
+
return compareTo(otherVersion, ignoreSuffix) >= 0;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
@Override
|
|
165
|
+
public final int compareTo(@NonNull Version version) {
|
|
166
|
+
return compareTo(version, false);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private int compareTo(@NonNull Version version, boolean ignoreSuffix) {
|
|
170
|
+
int versionNumberResult = compareSubversionNumbers(trimmedSubversionNumbers, version.trimmedSubversionNumbers);
|
|
171
|
+
if (versionNumberResult != 0 || ignoreSuffix) {
|
|
172
|
+
return versionNumberResult;
|
|
173
|
+
}
|
|
174
|
+
int releaseTypeResult = releaseType.compareTo(version.releaseType);
|
|
175
|
+
if (releaseTypeResult != 0) {
|
|
176
|
+
return releaseTypeResult;
|
|
177
|
+
}
|
|
178
|
+
return Long.compare(preReleaseVersion, version.preReleaseVersion);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
@Override
|
|
182
|
+
public final boolean equals(Object o) {
|
|
183
|
+
return o instanceof Version && isEqual((Version) o);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
@Override
|
|
187
|
+
public final int hashCode() {
|
|
188
|
+
int result = trimmedSubversionNumbers.hashCode();
|
|
189
|
+
result = 31 * result + releaseType.hashCode();
|
|
190
|
+
result = 31 * result + (int) (preReleaseVersion ^ (preReleaseVersion >>> 32));
|
|
191
|
+
return result;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
@NonNull
|
|
195
|
+
@Override
|
|
196
|
+
public String toString() {
|
|
197
|
+
return String.valueOf(originalString);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// helpers
|
|
201
|
+
|
|
202
|
+
private static int compareSubversionNumbers(@NonNull final List<Long> versionNumbersA, @NonNull final List<Long> versionNumbersB) {
|
|
203
|
+
final int numbersSizeA = versionNumbersA.size();
|
|
204
|
+
final int numbersSizeB = versionNumbersB.size();
|
|
205
|
+
final int maxSize = Math.max(numbersSizeA, numbersSizeB);
|
|
206
|
+
|
|
207
|
+
for (int i = 0; i < maxSize; i++) {
|
|
208
|
+
final long a = i < numbersSizeA ? versionNumbersA.get(i) : 0L;
|
|
209
|
+
final long b = i < numbersSizeB ? versionNumbersB.get(i) : 0L;
|
|
210
|
+
if (a > b) {
|
|
211
|
+
return 1;
|
|
212
|
+
} else if (a < b) {
|
|
213
|
+
return -1;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return 0;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
static ReleaseType qualifierToReleaseType(@NonNull String suffix) {
|
|
220
|
+
if (suffix.length() > 0) {
|
|
221
|
+
suffix = suffix.toLowerCase(Locale.ROOT);
|
|
222
|
+
if (suffix.contains(RC_STRING)) return ReleaseType.RC;
|
|
223
|
+
if (suffix.contains(BETA_STRING)) return ReleaseType.BETA;
|
|
224
|
+
if (suffix.contains(ALPHA_STRING)) {
|
|
225
|
+
if (suffix.substring(0, suffix.indexOf(ALPHA_STRING)).contains(PRE_STRING)) {
|
|
226
|
+
return ReleaseType.PRE_ALPHA;
|
|
227
|
+
}
|
|
228
|
+
return ReleaseType.ALPHA;
|
|
229
|
+
}
|
|
230
|
+
if (suffix.contains(SNAPSHOT_STRING)) return ReleaseType.SNAPSHOT;
|
|
231
|
+
}
|
|
232
|
+
return ReleaseType.STABLE;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
private static long preReleaseVersion(@NonNull final String suffix, final ReleaseType releaseType) {
|
|
236
|
+
if (releaseType == ReleaseType.STABLE || releaseType == ReleaseType.SNAPSHOT) return 0;
|
|
237
|
+
|
|
238
|
+
final int startIndex = indexOfQualifier(suffix, releaseType);
|
|
239
|
+
if (startIndex < suffix.length()) {
|
|
240
|
+
final int maxStartIndex = Math.min(startIndex + 2, suffix.length());
|
|
241
|
+
if (containsNumeric(suffix.substring(startIndex, maxStartIndex))) {
|
|
242
|
+
final StringBuilder versionNumber = new StringBuilder();
|
|
243
|
+
for (int i = startIndex; i < suffix.length(); i++) {
|
|
244
|
+
final char c = suffix.charAt(i);
|
|
245
|
+
if (Character.isDigit(c)) {
|
|
246
|
+
versionNumber.append(c);
|
|
247
|
+
} else if (i != startIndex) {
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return safeParseLong(versionNumber.toString());
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return 0;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private static int indexOfQualifier(@NonNull String suffix, final ReleaseType releaseType) {
|
|
258
|
+
suffix = suffix.toLowerCase(Locale.ROOT);
|
|
259
|
+
switch (releaseType) {
|
|
260
|
+
case RC:
|
|
261
|
+
return suffix.indexOf(RC_STRING) + RC_STRING.length();
|
|
262
|
+
case BETA:
|
|
263
|
+
return suffix.indexOf(BETA_STRING) + BETA_STRING.length();
|
|
264
|
+
case ALPHA:
|
|
265
|
+
case PRE_ALPHA:
|
|
266
|
+
return suffix.indexOf(ALPHA_STRING) + ALPHA_STRING.length();
|
|
267
|
+
default:
|
|
268
|
+
return 0;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
private static boolean startsNumeric(@NonNull String str) {
|
|
273
|
+
str = str.trim();
|
|
274
|
+
return str.length() > 0 && Character.isDigit(str.charAt(0));
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
private static long safeParseLong(@NonNull String numbers) {
|
|
278
|
+
if (numbers.length() > 19) {
|
|
279
|
+
numbers = numbers.substring(0, 19);
|
|
280
|
+
}
|
|
281
|
+
return Long.parseLong(numbers);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
private static boolean isNumeric(@NonNull final CharSequence cs) {
|
|
285
|
+
final int sz = cs.length();
|
|
286
|
+
if (sz > 0) {
|
|
287
|
+
for (int i = 0; i < sz; i++) {
|
|
288
|
+
if (!Character.isDigit(cs.charAt(i))) {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private static boolean containsNumeric(@NonNull final CharSequence cs) {
|
|
298
|
+
final int sz = cs.length();
|
|
299
|
+
for (int i = 0; i < sz; i++) {
|
|
300
|
+
if (Character.isDigit(cs.charAt(i))) {
|
|
301
|
+
return true;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
}
|