@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
|
@@ -11,8 +11,11 @@ import android.content.Context;
|
|
|
11
11
|
import android.content.SharedPreferences;
|
|
12
12
|
import android.content.pm.PackageManager;
|
|
13
13
|
import android.os.Build;
|
|
14
|
+
import android.os.Handler;
|
|
15
|
+
import android.os.Looper;
|
|
14
16
|
import androidx.annotation.NonNull;
|
|
15
|
-
import androidx.lifecycle.
|
|
17
|
+
import androidx.lifecycle.LiveData;
|
|
18
|
+
import androidx.lifecycle.Observer;
|
|
16
19
|
import androidx.work.Constraints;
|
|
17
20
|
import androidx.work.Data;
|
|
18
21
|
import androidx.work.ExistingPeriodicWorkPolicy;
|
|
@@ -33,6 +36,7 @@ import java.io.FilenameFilter;
|
|
|
33
36
|
import java.io.IOException;
|
|
34
37
|
import java.io.InputStreamReader;
|
|
35
38
|
import java.nio.charset.StandardCharsets;
|
|
39
|
+
import java.nio.file.Files;
|
|
36
40
|
import java.security.SecureRandom;
|
|
37
41
|
import java.util.ArrayList;
|
|
38
42
|
import java.util.Date;
|
|
@@ -49,8 +53,10 @@ import java.util.concurrent.ConcurrentHashMap;
|
|
|
49
53
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
50
54
|
import java.util.concurrent.ExecutorService;
|
|
51
55
|
import java.util.concurrent.Executors;
|
|
56
|
+
import java.util.concurrent.LinkedBlockingQueue;
|
|
52
57
|
import java.util.concurrent.ScheduledExecutorService;
|
|
53
58
|
import java.util.concurrent.ScheduledFuture;
|
|
59
|
+
import java.util.concurrent.ThreadPoolExecutor;
|
|
54
60
|
import java.util.concurrent.TimeUnit;
|
|
55
61
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
56
62
|
import java.util.zip.ZipEntry;
|
|
@@ -76,6 +82,9 @@ public class CapgoUpdater {
|
|
|
76
82
|
private static final String PENDING_DELETE_IDS = "pendingDeleteIds";
|
|
77
83
|
private static final String bundleDirectory = "versions";
|
|
78
84
|
private static final String TEMP_UNZIP_PREFIX = "capgo_unzip_";
|
|
85
|
+
static final String MANIFEST_STAGING_PREFIX = "capgo_manifest_";
|
|
86
|
+
static final String MANIFEST_DEST_SUFFIX = "_manifest_dest";
|
|
87
|
+
private static final String LEGACY_MANIFEST_STAGING_FOLDER_PATTERN = "^[0-9A-Za-z]{10}$";
|
|
79
88
|
private static final long DELETE_PACE_MS = 75L;
|
|
80
89
|
private final Object deleteLock = new Object();
|
|
81
90
|
private static final String CAPACITOR_CONFIG_ASSET = "capacitor.config.json";
|
|
@@ -93,6 +102,9 @@ public class CapgoUpdater {
|
|
|
93
102
|
public File noBackupDir;
|
|
94
103
|
public Boolean directUpdate = false;
|
|
95
104
|
public Activity activity;
|
|
105
|
+
// Application context: lets downloads run and finish without an Activity (app in the
|
|
106
|
+
// background, or a process woken by a push with no UI at all).
|
|
107
|
+
public Context appContext;
|
|
96
108
|
public String pluginVersion = "";
|
|
97
109
|
public String versionBuild = "";
|
|
98
110
|
public String versionCode = "";
|
|
@@ -137,20 +149,52 @@ public class CapgoUpdater {
|
|
|
137
149
|
private static final long STATS_FLUSH_INTERVAL_MS = 1000;
|
|
138
150
|
private static final String PENDING_STATS_FILE = "capgo_pending_stats.json";
|
|
139
151
|
private static final int MAX_PENDING_STATS = 200;
|
|
152
|
+
// The live plugin instance owns the stats file. A headless worker hands its unsent events to it
|
|
153
|
+
// instead of writing the file; events handed over before the plugin claims ownership wait here.
|
|
154
|
+
private static final Object statsOwnerLock = new Object();
|
|
155
|
+
private static CapgoUpdater statsOwner = null;
|
|
156
|
+
private static final List<JSONObject> orphanedStats = new ArrayList<>();
|
|
140
157
|
|
|
141
158
|
private static final class QueuedStatsEvent {
|
|
142
159
|
|
|
143
160
|
private final JSONObject event;
|
|
144
161
|
private final Runnable onSent;
|
|
162
|
+
/** True when the event was loaded from the shared pending-stats file (not created by this instance). */
|
|
163
|
+
private final boolean restored;
|
|
145
164
|
|
|
146
165
|
private QueuedStatsEvent(final JSONObject event, final Runnable onSent) {
|
|
166
|
+
this(event, onSent, false);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
private QueuedStatsEvent(final JSONObject event, final Runnable onSent, final boolean restored) {
|
|
147
170
|
this.event = event;
|
|
148
171
|
this.onSent = onSent;
|
|
172
|
+
this.restored = restored;
|
|
149
173
|
}
|
|
150
174
|
}
|
|
151
175
|
|
|
152
176
|
private final Map<String, CompletableFuture<BundleInfo>> downloadFutures = new ConcurrentHashMap<>();
|
|
153
|
-
|
|
177
|
+
|
|
178
|
+
/** A download failure whose downloadFailed event and stats were already sent by the updater. */
|
|
179
|
+
static final class ReportedDownloadFailureException extends IOException {
|
|
180
|
+
|
|
181
|
+
ReportedDownloadFailureException(final String message) {
|
|
182
|
+
super(message);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Versions whose WorkManager download is observed by this process. WorkManager persists work across a
|
|
187
|
+
// process kill, but the observer that finishes the download does not survive it.
|
|
188
|
+
private final Set<String> observedDownloadVersions = ConcurrentHashMap.newKeySet();
|
|
189
|
+
private final ExecutorService io = createIoExecutor();
|
|
190
|
+
|
|
191
|
+
private static ExecutorService createIoExecutor() {
|
|
192
|
+
final ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
|
|
193
|
+
executor.allowCoreThreadTimeOut(true);
|
|
194
|
+
return executor;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private volatile Handler mainHandler;
|
|
154
198
|
|
|
155
199
|
public CapgoUpdater(Logger logger) {
|
|
156
200
|
this.logger = logger;
|
|
@@ -161,12 +205,22 @@ public class CapgoUpdater {
|
|
|
161
205
|
return !name.startsWith("__MACOSX") && !name.startsWith(".") && !name.startsWith(".DS_Store");
|
|
162
206
|
};
|
|
163
207
|
|
|
208
|
+
/** The Activity when the app has one, otherwise the application context. */
|
|
209
|
+
Context context() {
|
|
210
|
+
final Activity current = this.activity;
|
|
211
|
+
if (current != null) {
|
|
212
|
+
return current;
|
|
213
|
+
}
|
|
214
|
+
return this.appContext;
|
|
215
|
+
}
|
|
216
|
+
|
|
164
217
|
private boolean isProd() {
|
|
165
218
|
try {
|
|
166
|
-
|
|
167
|
-
|
|
219
|
+
final Context context = this.context();
|
|
220
|
+
if (context == null) {
|
|
221
|
+
return true; // Default to production if no context
|
|
168
222
|
}
|
|
169
|
-
return (
|
|
223
|
+
return (context.getApplicationInfo().flags & android.content.pm.ApplicationInfo.FLAG_DEBUGGABLE) == 0;
|
|
170
224
|
} catch (Exception e) {
|
|
171
225
|
return true; // Default to production if we can't determine
|
|
172
226
|
}
|
|
@@ -194,13 +248,14 @@ public class CapgoUpdater {
|
|
|
194
248
|
|
|
195
249
|
@SuppressWarnings("deprecation")
|
|
196
250
|
private String getInstallSource() {
|
|
197
|
-
|
|
251
|
+
final Context context = this.context();
|
|
252
|
+
if (context == null) {
|
|
198
253
|
return "";
|
|
199
254
|
}
|
|
200
255
|
|
|
201
256
|
try {
|
|
202
|
-
PackageManager packageManager =
|
|
203
|
-
String packageName =
|
|
257
|
+
PackageManager packageManager = context.getPackageManager();
|
|
258
|
+
String packageName = context.getPackageName();
|
|
204
259
|
String installerPackageName;
|
|
205
260
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
206
261
|
android.content.pm.InstallSourceInfo installSourceInfo = packageManager.getInstallSourceInfo(packageName);
|
|
@@ -255,14 +310,301 @@ public class CapgoUpdater {
|
|
|
255
310
|
|
|
256
311
|
void directUpdateFinish(final BundleInfo latest) {}
|
|
257
312
|
|
|
313
|
+
/** Launch downloads have no waiter. The plugin emits appReady from here when WorkManager settles. */
|
|
314
|
+
void backgroundDownloadSettled(final BundleInfo bundle, final String status) {}
|
|
315
|
+
|
|
258
316
|
void notifyListeners(final String id, final Map<String, Object> res) {}
|
|
259
317
|
|
|
318
|
+
/** A downloaded bundle was stored as next bundle and waits for its install moment. */
|
|
319
|
+
void nextBundleReady(final BundleInfo bundle) {}
|
|
320
|
+
|
|
321
|
+
static boolean shouldNotifyLaunchDownloadReady(
|
|
322
|
+
final boolean awaitedByCaller,
|
|
323
|
+
final boolean success,
|
|
324
|
+
final boolean directInstall,
|
|
325
|
+
final boolean previewSession
|
|
326
|
+
) {
|
|
327
|
+
if (awaitedByCaller) {
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
if (!success) {
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
return !directInstall && !previewSession;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
static boolean shouldRestartOrphanedDownload(final boolean observedByThisProcess) {
|
|
337
|
+
return !observedByThisProcess;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
static boolean shouldReleaseLaunchWhileRetrying(
|
|
341
|
+
final boolean awaitedByCaller,
|
|
342
|
+
final boolean setNext,
|
|
343
|
+
final boolean directUpdate,
|
|
344
|
+
final boolean previewSession
|
|
345
|
+
) {
|
|
346
|
+
return !awaitedByCaller && setNext && directUpdate && !previewSession;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
static String launchDownloadReadyStatus(final boolean success, final boolean setNext) {
|
|
350
|
+
if (!success) {
|
|
351
|
+
return "Error downloading file";
|
|
352
|
+
}
|
|
353
|
+
if (setNext) {
|
|
354
|
+
return "update downloaded, will install next background";
|
|
355
|
+
}
|
|
356
|
+
return "update downloaded, autoUpdate onlyDownload";
|
|
357
|
+
}
|
|
358
|
+
|
|
260
359
|
public String randomString() {
|
|
261
360
|
final StringBuilder sb = new StringBuilder(10);
|
|
262
361
|
for (int i = 0; i < 10; i++) sb.append(AB.charAt(rnd.nextInt(AB.length())));
|
|
263
362
|
return sb.toString();
|
|
264
363
|
}
|
|
265
364
|
|
|
365
|
+
static boolean isManifestStagingFolderName(final String name) {
|
|
366
|
+
if (name == null || !name.startsWith(MANIFEST_STAGING_PREFIX)) {
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
369
|
+
final String suffix = name.substring(MANIFEST_STAGING_PREFIX.length());
|
|
370
|
+
return suffix.length() == 10 && suffix.matches("[0-9A-Za-z]{10}");
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
String manifestStagingDest() {
|
|
374
|
+
return MANIFEST_STAGING_PREFIX + this.randomString();
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
private static boolean isLegacyBareManifestStagingFolderName(final String name) {
|
|
378
|
+
return name != null && name.matches(LEGACY_MANIFEST_STAGING_FOLDER_PATTERN);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
private static boolean isSymbolicLinkEntry(final File file) {
|
|
382
|
+
return Files.isSymbolicLink(file.toPath());
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
private static boolean isRegularFileEntry(final File file) {
|
|
386
|
+
return file.isFile() && !isSymbolicLinkEntry(file);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
private static boolean isRegularDirectoryEntry(final File file) {
|
|
390
|
+
return file.isDirectory() && !isSymbolicLinkEntry(file);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
private static final long LEGACY_MANIFEST_STAGING_MIN_AGE_MS = 24L * 60L * 60L * 1000L;
|
|
394
|
+
|
|
395
|
+
private static boolean looksLikeLeakedManifestStaging(final File dir) {
|
|
396
|
+
if (dir == null || !isRegularDirectoryEntry(dir)) {
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
if (isRegularFileEntry(new File(dir, "index.html"))) {
|
|
400
|
+
return true;
|
|
401
|
+
}
|
|
402
|
+
if (isRegularDirectoryEntry(new File(dir, "assets"))) {
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
return isRegularDirectoryEntry(new File(dir, "www"));
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
private static boolean isOldEnoughForLegacyManifestCleanup(final File dir) {
|
|
409
|
+
final long ageMs = System.currentTimeMillis() - dir.lastModified();
|
|
410
|
+
return ageMs >= LEGACY_MANIFEST_STAGING_MIN_AGE_MS;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
void saveManifestStagingDest(final String bundleId, final String dest) {
|
|
414
|
+
if (this.editor == null || bundleId == null || dest == null || dest.isEmpty()) {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
this.editor.putString(bundleId + MANIFEST_DEST_SUFFIX, dest);
|
|
418
|
+
this.editor.commit();
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
void clearManifestStagingDest(final String bundleId) {
|
|
422
|
+
if (this.editor == null || bundleId == null) {
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
this.editor.remove(bundleId + MANIFEST_DEST_SUFFIX);
|
|
426
|
+
this.editor.commit();
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
void cleanupManifestStagingAfterDownloadFailed(final String bundleId, final String failedDest, final boolean failedManifest) {
|
|
430
|
+
if (failedManifest) {
|
|
431
|
+
this.deleteManifestStagingFolder(failedDest);
|
|
432
|
+
}
|
|
433
|
+
this.clearManifestStagingDest(bundleId);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
void cleanupManifestStagingAfterDownloadCancelled(final String bundleId) {
|
|
437
|
+
final String cancelledDest = this.prefs != null ? this.prefs.getString(bundleId + MANIFEST_DEST_SUFFIX, null) : null;
|
|
438
|
+
this.deleteManifestStagingFolder(cancelledDest);
|
|
439
|
+
this.clearManifestStagingDest(bundleId);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
Set<String> activeManifestStagingDests() {
|
|
443
|
+
final Set<String> activeDests = new HashSet<>();
|
|
444
|
+
if (this.prefs == null) {
|
|
445
|
+
return activeDests;
|
|
446
|
+
}
|
|
447
|
+
for (final BundleInfo info : this.list(true)) {
|
|
448
|
+
if (info == null || info.getId() == null || info.getId().isEmpty()) {
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
if (info.getStatus() != BundleStatus.DOWNLOADING) {
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
final String dest = this.prefs.getString(info.getId() + MANIFEST_DEST_SUFFIX, null);
|
|
455
|
+
if (dest != null && !dest.isEmpty()) {
|
|
456
|
+
activeDests.add(dest);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
return activeDests;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
public void deleteManifestStagingFolder(final String dest) {
|
|
463
|
+
if (this.documentsDir == null || dest == null || dest.isEmpty() || !isManifestStagingFolderName(dest)) {
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
final File folder = new File(this.documentsDir, dest);
|
|
467
|
+
try {
|
|
468
|
+
this.assertPathInsideDocumentsDir(folder);
|
|
469
|
+
deleteManifestStagingTree(folder);
|
|
470
|
+
} catch (IOException e) {
|
|
471
|
+
logger.warn("Failed to delete manifest staging folder: " + e.getMessage());
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
static void deleteManifestStagingFolderAt(final File documentsDir, final String dest, final Logger log) {
|
|
476
|
+
if (documentsDir == null || dest == null || dest.isEmpty() || !isManifestStagingFolderName(dest)) {
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
final File folder = new File(documentsDir, dest);
|
|
480
|
+
if (!folder.exists()) {
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
try {
|
|
484
|
+
final File canonicalBase = documentsDir.getCanonicalFile();
|
|
485
|
+
final File canonicalTarget = folder.getCanonicalFile();
|
|
486
|
+
final String basePath = canonicalBase.getPath();
|
|
487
|
+
final String normalizedBasePath = basePath.endsWith(File.separator) ? basePath : basePath + File.separator;
|
|
488
|
+
final String targetPath = canonicalTarget.getPath();
|
|
489
|
+
if (!targetPath.equals(basePath) && !targetPath.startsWith(normalizedBasePath)) {
|
|
490
|
+
if (log != null) {
|
|
491
|
+
log.warn("Refusing unsafe manifest staging delete: " + folder.getAbsolutePath());
|
|
492
|
+
}
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
if (canonicalTarget.isDirectory()) {
|
|
496
|
+
deleteManifestStagingTree(canonicalTarget);
|
|
497
|
+
} else if (!canonicalTarget.delete()) {
|
|
498
|
+
if (log != null) {
|
|
499
|
+
log.warn("Failed to delete manifest staging file: " + canonicalTarget.getAbsolutePath());
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
} catch (IOException e) {
|
|
503
|
+
if (log != null) {
|
|
504
|
+
log.warn("Failed to delete manifest staging folder: " + e.getMessage());
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
private static void deleteManifestStagingTree(final File file) throws IOException {
|
|
510
|
+
if (isSymbolicLinkEntry(file)) {
|
|
511
|
+
if (!file.delete()) {
|
|
512
|
+
throw new IOException("Failed to delete symlink: " + file);
|
|
513
|
+
}
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
if (file.isDirectory()) {
|
|
517
|
+
final File[] entries = file.listFiles();
|
|
518
|
+
if (entries != null) {
|
|
519
|
+
for (final File entry : entries) {
|
|
520
|
+
deleteManifestStagingTree(entry);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
if (!file.delete()) {
|
|
525
|
+
throw new IOException("Failed to delete: " + file);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
public void cleanupOrphanedManifestStagingFolders(final Thread threadToCheck) {
|
|
530
|
+
if (this.documentsDir == null) {
|
|
531
|
+
logger.warn("Documents directory is null, skipping manifest staging cleanup");
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
final File[] entries = this.documentsDir.listFiles();
|
|
536
|
+
if (entries == null) {
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
final Set<String> protectedDests = this.activeManifestStagingDests();
|
|
541
|
+
|
|
542
|
+
for (final File entry : entries) {
|
|
543
|
+
if (threadToCheck != null && threadToCheck.isInterrupted()) {
|
|
544
|
+
logger.warn("cleanupOrphanedManifestStagingFolders was cancelled");
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
if (!entry.isDirectory()) {
|
|
548
|
+
continue;
|
|
549
|
+
}
|
|
550
|
+
final String folderName = entry.getName();
|
|
551
|
+
if (!isManifestStagingFolderName(folderName)) {
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
if (protectedDests.contains(folderName)) {
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
try {
|
|
558
|
+
deleteManifestStagingTree(entry);
|
|
559
|
+
logger.info("Deleted orphaned manifest staging folder");
|
|
560
|
+
logger.debug("Folder: " + folderName);
|
|
561
|
+
} catch (IOException e) {
|
|
562
|
+
logger.error("Failed to delete orphaned manifest staging folder");
|
|
563
|
+
logger.debug("Folder: " + folderName + ", Error: " + e.getMessage());
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
this.cleanupLegacyBareManifestStagingFolders(threadToCheck, protectedDests);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
private void cleanupLegacyBareManifestStagingFolders(final Thread threadToCheck, final Set<String> protectedDests) {
|
|
571
|
+
final File[] entries = this.documentsDir.listFiles();
|
|
572
|
+
if (entries == null) {
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
for (final File entry : entries) {
|
|
577
|
+
if (threadToCheck != null && threadToCheck.isInterrupted()) {
|
|
578
|
+
logger.warn("cleanupLegacyBareManifestStagingFolders was cancelled");
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
if (!entry.isDirectory()) {
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
final String folderName = entry.getName();
|
|
585
|
+
if (!isLegacyBareManifestStagingFolderName(folderName)) {
|
|
586
|
+
continue;
|
|
587
|
+
}
|
|
588
|
+
if (protectedDests.contains(folderName)) {
|
|
589
|
+
continue;
|
|
590
|
+
}
|
|
591
|
+
if (!looksLikeLeakedManifestStaging(entry)) {
|
|
592
|
+
continue;
|
|
593
|
+
}
|
|
594
|
+
if (!isOldEnoughForLegacyManifestCleanup(entry)) {
|
|
595
|
+
continue;
|
|
596
|
+
}
|
|
597
|
+
try {
|
|
598
|
+
deleteManifestStagingTree(entry);
|
|
599
|
+
logger.info("Deleted legacy bare manifest staging folder");
|
|
600
|
+
logger.debug("Folder: " + folderName);
|
|
601
|
+
} catch (IOException e) {
|
|
602
|
+
logger.error("Failed to delete legacy bare manifest staging folder");
|
|
603
|
+
logger.debug("Folder: " + folderName + ", Error: " + e.getMessage());
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
266
608
|
public void setPublicKey(String publicKey) {
|
|
267
609
|
// Empty string means no encryption - proceed normally
|
|
268
610
|
if (publicKey == null || publicKey.isEmpty()) {
|
|
@@ -437,8 +779,8 @@ public class CapgoUpdater {
|
|
|
437
779
|
}
|
|
438
780
|
|
|
439
781
|
void cacheBundleFiles(final String id) {
|
|
440
|
-
if (this.
|
|
441
|
-
logger.debug("Skip delta cache population:
|
|
782
|
+
if (this.context() == null) {
|
|
783
|
+
logger.debug("Skip delta cache population: no context");
|
|
442
784
|
return;
|
|
443
785
|
}
|
|
444
786
|
|
|
@@ -454,7 +796,7 @@ public class CapgoUpdater {
|
|
|
454
796
|
return;
|
|
455
797
|
}
|
|
456
798
|
|
|
457
|
-
final File cacheDir = new File(this.
|
|
799
|
+
final File cacheDir = new File(this.context().getCacheDir(), "capgo_downloads");
|
|
458
800
|
if (cacheDir.exists() && !cacheDir.isDirectory()) {
|
|
459
801
|
logger.debug("Skip delta cache population: cache dir is not a directory");
|
|
460
802
|
return;
|
|
@@ -464,7 +806,7 @@ public class CapgoUpdater {
|
|
|
464
806
|
return;
|
|
465
807
|
}
|
|
466
808
|
|
|
467
|
-
final File builtinFolder = new File(this.
|
|
809
|
+
final File builtinFolder = new File(this.context().getFilesDir(), "public");
|
|
468
810
|
|
|
469
811
|
final List<File> files = new ArrayList<>();
|
|
470
812
|
collectFiles(bundleDir, files);
|
|
@@ -554,16 +896,16 @@ public class CapgoUpdater {
|
|
|
554
896
|
private boolean isManifestEntryAvailableLocally(final JSONObject entry, final String sessionKey) {
|
|
555
897
|
final String fileName = entry.optString("file_name", "");
|
|
556
898
|
final String fileHash = resolveManifestFileHash(entry, sessionKey);
|
|
557
|
-
if (fileName.isEmpty() || fileHash.isEmpty() || this.
|
|
899
|
+
if (fileName.isEmpty() || fileHash.isEmpty() || this.context() == null) {
|
|
558
900
|
return false;
|
|
559
901
|
}
|
|
560
902
|
|
|
561
|
-
if (DownloadService.builtinAssetMatches(this.
|
|
903
|
+
if (DownloadService.builtinAssetMatches(this.context().getAssets(), fileName, fileHash)) {
|
|
562
904
|
return true;
|
|
563
905
|
}
|
|
564
906
|
|
|
565
907
|
try {
|
|
566
|
-
final File builtinFile = DownloadService.resolveManifestBuiltinFile(new File(this.
|
|
908
|
+
final File builtinFile = DownloadService.resolveManifestBuiltinFile(new File(this.context().getFilesDir(), "public"), fileName);
|
|
567
909
|
if (verifyChecksum(builtinFile, fileHash)) {
|
|
568
910
|
return true;
|
|
569
911
|
}
|
|
@@ -575,7 +917,7 @@ public class CapgoUpdater {
|
|
|
575
917
|
final String fileNameWithoutPath = new File(fileName).getName();
|
|
576
918
|
final String cacheBaseName = isBrotli ? fileNameWithoutPath.substring(0, fileNameWithoutPath.length() - 3) : fileNameWithoutPath;
|
|
577
919
|
if (isSafeCacheHash(fileHash)) {
|
|
578
|
-
final File cacheFolder = new File(this.
|
|
920
|
+
final File cacheFolder = new File(this.context().getCacheDir(), "capgo_downloads");
|
|
579
921
|
final File cacheFile = new File(cacheFolder, fileHash + "_" + cacheBaseName);
|
|
580
922
|
// Cache files are named `{hash}_{filename}` and were checksum-verified
|
|
581
923
|
// when written. Re-hashing every hit re-reads the whole bundle and
|
|
@@ -703,115 +1045,183 @@ public class CapgoUpdater {
|
|
|
703
1045
|
}
|
|
704
1046
|
}
|
|
705
1047
|
|
|
706
|
-
private void
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
1048
|
+
private void notifyLaunchDownloadReady(
|
|
1049
|
+
final boolean awaitedByCaller,
|
|
1050
|
+
final boolean success,
|
|
1051
|
+
final boolean directInstall,
|
|
1052
|
+
final boolean previewSession,
|
|
1053
|
+
final boolean setNext,
|
|
1054
|
+
final BundleInfo bundle
|
|
1055
|
+
) {
|
|
1056
|
+
if (!shouldNotifyLaunchDownloadReady(awaitedByCaller, success, directInstall, previewSession)) {
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
final BundleInfo readyBundle = bundle != null ? bundle : this.getCurrentBundle();
|
|
1060
|
+
this.backgroundDownloadSettled(readyBundle, launchDownloadReadyStatus(success, setNext));
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
// Created on first use: plain JVM unit tests construct CapgoUpdater without a main Looper.
|
|
1064
|
+
private Handler mainHandler() {
|
|
1065
|
+
Handler handler = this.mainHandler;
|
|
1066
|
+
if (handler == null) {
|
|
1067
|
+
handler = new Handler(Looper.getMainLooper());
|
|
1068
|
+
this.mainHandler = handler;
|
|
1069
|
+
}
|
|
1070
|
+
return handler;
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
@SuppressWarnings("unchecked")
|
|
1074
|
+
private void observeWorkProgress(Context context, String id, String observedVersion, boolean setNext) {
|
|
1075
|
+
final AtomicBoolean terminalHandled = new AtomicBoolean(false);
|
|
1076
|
+
final AtomicBoolean launchReleasedWhileRetrying = new AtomicBoolean(false);
|
|
1077
|
+
final LiveData<List<WorkInfo>> workInfosLiveData = WorkManager.getInstance(
|
|
1078
|
+
context.getApplicationContext()
|
|
1079
|
+
).getWorkInfosByTagLiveData(id);
|
|
1080
|
+
// observeForever, not observe(activity): a stopped Activity pauses LiveData, so a download
|
|
1081
|
+
// that completes while the app is in the background would only be finished once the user
|
|
1082
|
+
// opens the app again. The observer removes itself on a terminal state.
|
|
1083
|
+
final Observer<List<WorkInfo>>[] observerRef = new Observer[1];
|
|
1084
|
+
final Runnable removeObserver = () -> mainHandler().post(() -> workInfosLiveData.removeObserver(observerRef[0]));
|
|
1085
|
+
mainHandler().post(() -> {
|
|
1086
|
+
observerRef[0] = (workInfos) -> {
|
|
1087
|
+
if (workInfos == null || workInfos.isEmpty()) return;
|
|
1088
|
+
|
|
1089
|
+
WorkInfo workInfo = workInfos.get(0);
|
|
1090
|
+
Data progress = workInfo.getProgress();
|
|
1091
|
+
|
|
1092
|
+
switch (workInfo.getState()) {
|
|
1093
|
+
case ENQUEUED:
|
|
1094
|
+
// A run attempt already happened, so WorkManager scheduled a retry (network lost midway,
|
|
1095
|
+
// 5xx, ...). Retries back off without limit, so a direct install must not keep the launch
|
|
1096
|
+
// (and its splashscreen) blocked until the network comes back.
|
|
1097
|
+
if (
|
|
1098
|
+
workInfo.getRunAttemptCount() > 0 &&
|
|
1099
|
+
shouldReleaseLaunchWhileRetrying(
|
|
1100
|
+
downloadFutures.containsKey(id),
|
|
1101
|
+
setNext,
|
|
1102
|
+
Boolean.TRUE.equals(CapgoUpdater.this.directUpdate),
|
|
1103
|
+
CapgoUpdater.this.previewSession
|
|
1104
|
+
) &&
|
|
1105
|
+
launchReleasedWhileRetrying.compareAndSet(false, true)
|
|
1106
|
+
) {
|
|
1107
|
+
logger.warn("Direct update download is retrying, continuing launch on the current bundle");
|
|
1108
|
+
// Same fallback as the autoSplashscreen timeout: a later success installs on next background.
|
|
1109
|
+
CapgoUpdater.this.directUpdate = false;
|
|
1110
|
+
io.execute(() -> backgroundDownloadSettled(getCurrentBundle(), launchDownloadReadyStatus(false, false)));
|
|
1111
|
+
}
|
|
1112
|
+
break;
|
|
1113
|
+
case RUNNING:
|
|
1114
|
+
int percent = progress.getInt(DownloadService.PERCENT, 0);
|
|
1115
|
+
notifyDownload(id, percent);
|
|
1116
|
+
break;
|
|
1117
|
+
case SUCCEEDED:
|
|
1118
|
+
if (!terminalHandled.compareAndSet(false, true)) break;
|
|
1119
|
+
removeObserver.run();
|
|
1120
|
+
observedDownloadVersions.remove(observedVersion);
|
|
1121
|
+
logger.info("Download succeeded: " + workInfo.getState());
|
|
1122
|
+
Data outputData = workInfo.getOutputData();
|
|
1123
|
+
String dest = outputData.getString(DownloadService.FILEDEST);
|
|
1124
|
+
String version = outputData.getString(DownloadService.VERSION);
|
|
1125
|
+
String sessionKey = outputData.getString(DownloadService.SESSIONKEY);
|
|
1126
|
+
String checksum = outputData.getString(DownloadService.CHECKSUM);
|
|
1127
|
+
boolean isManifest = outputData.getBoolean(DownloadService.IS_MANIFEST, false);
|
|
1128
|
+
|
|
1129
|
+
io.execute(() -> {
|
|
1130
|
+
// finishDownload clears directUpdate, so read the install plan first.
|
|
1131
|
+
final boolean directInstall =
|
|
1132
|
+
setNext && Boolean.TRUE.equals(CapgoUpdater.this.directUpdate) && !CapgoUpdater.this.previewSession;
|
|
1133
|
+
final boolean previewSession = CapgoUpdater.this.previewSession;
|
|
1134
|
+
boolean success = finishDownload(id, dest, version, sessionKey, checksum, setNext, isManifest);
|
|
1135
|
+
BundleInfo resultBundle;
|
|
1136
|
+
if (!success) {
|
|
1137
|
+
logger.error("Finish download failed");
|
|
1138
|
+
logger.debug("Version: " + version);
|
|
1139
|
+
resultBundle = new BundleInfo(id, version, BundleStatus.ERROR, new Date(System.currentTimeMillis()), "");
|
|
1140
|
+
saveBundleInfo(id, resultBundle);
|
|
1141
|
+
// Cleanup download tracking
|
|
1142
|
+
DownloadWorkerManager.cancelBundleDownload(context, id, version);
|
|
1143
|
+
sendStats("finish_download_fail", version);
|
|
1144
|
+
// finishDownload already emitted downloadFailed for this failure.
|
|
1145
|
+
} else {
|
|
1146
|
+
// Successful download - cleanup tracking
|
|
1147
|
+
DownloadWorkerManager.cancelBundleDownload(context, id, version);
|
|
1148
|
+
resultBundle = getBundleInfo(id);
|
|
1149
|
+
}
|
|
761
1150
|
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
case FAILED:
|
|
770
|
-
Data failedData = workInfo.getOutputData();
|
|
771
|
-
String error = failedData.getString(DownloadService.ERROR);
|
|
772
|
-
logger.error("Download failed");
|
|
773
|
-
logger.debug("Error: " + error + ", State: " + workInfo.getState());
|
|
774
|
-
String failedVersion = failedData.getString(DownloadService.VERSION);
|
|
775
|
-
|
|
776
|
-
io.execute(() -> {
|
|
777
|
-
BundleInfo failedBundle = new BundleInfo(
|
|
778
|
-
id,
|
|
779
|
-
failedVersion,
|
|
780
|
-
BundleStatus.ERROR,
|
|
781
|
-
new Date(System.currentTimeMillis()),
|
|
782
|
-
""
|
|
783
|
-
);
|
|
784
|
-
saveBundleInfo(id, failedBundle);
|
|
785
|
-
// Cleanup download tracking for failed downloads
|
|
786
|
-
DownloadWorkerManager.cancelBundleDownload(activity, id, failedVersion);
|
|
787
|
-
Map<String, Object> ret = new HashMap<>();
|
|
788
|
-
ret.put("version", failedVersion);
|
|
789
|
-
if ("low_mem_fail".equals(error)) {
|
|
790
|
-
sendStats("low_mem_fail", failedVersion);
|
|
791
|
-
}
|
|
792
|
-
if ("insufficient_disk_space".equals(error)) {
|
|
793
|
-
sendStats("insufficient_disk_space", failedVersion);
|
|
794
|
-
}
|
|
795
|
-
ret.put("error", error != null ? error : "download_fail");
|
|
796
|
-
sendStats("download_fail", failedVersion);
|
|
797
|
-
notifyListeners("downloadFailed", ret);
|
|
798
|
-
|
|
799
|
-
// Complete the future with error status
|
|
800
|
-
CompletableFuture<BundleInfo> failedFuture = downloadFutures.remove(id);
|
|
801
|
-
if (failedFuture != null) {
|
|
802
|
-
failedFuture.complete(failedBundle);
|
|
803
|
-
}
|
|
804
|
-
});
|
|
805
|
-
break;
|
|
806
|
-
case CANCELLED:
|
|
807
|
-
DataManager.getInstance().clearManifest(id);
|
|
808
|
-
CompletableFuture<BundleInfo> cancelledFuture = downloadFutures.remove(id);
|
|
809
|
-
if (cancelledFuture != null) {
|
|
810
|
-
cancelledFuture.cancel(true);
|
|
1151
|
+
CapgoUpdater.this.clearManifestStagingDest(id);
|
|
1152
|
+
|
|
1153
|
+
// Complete the future if it exists. download() waits on it.
|
|
1154
|
+
// downloadBackground does not, so the launch check must emit appReady here.
|
|
1155
|
+
CompletableFuture<BundleInfo> future = downloadFutures.remove(id);
|
|
1156
|
+
if (future != null) {
|
|
1157
|
+
future.complete(resultBundle);
|
|
811
1158
|
}
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
1159
|
+
final BundleInfo readyBundle = success && setNext ? resultBundle : null;
|
|
1160
|
+
if (!launchReleasedWhileRetrying.get()) {
|
|
1161
|
+
notifyLaunchDownloadReady(future != null, success, directInstall, previewSession, setNext, readyBundle);
|
|
1162
|
+
}
|
|
1163
|
+
});
|
|
1164
|
+
break;
|
|
1165
|
+
case FAILED:
|
|
1166
|
+
if (!terminalHandled.compareAndSet(false, true)) break;
|
|
1167
|
+
removeObserver.run();
|
|
1168
|
+
observedDownloadVersions.remove(observedVersion);
|
|
1169
|
+
Data failedData = workInfo.getOutputData();
|
|
1170
|
+
String error = failedData.getString(DownloadService.ERROR);
|
|
1171
|
+
logger.error("Download failed");
|
|
1172
|
+
logger.debug("Error: " + error + ", State: " + workInfo.getState());
|
|
1173
|
+
String failedVersion = failedData.getString(DownloadService.VERSION);
|
|
1174
|
+
final String failedDest = failedData.getString(DownloadService.FILEDEST);
|
|
1175
|
+
final boolean failedManifest = failedData.getBoolean(DownloadService.IS_MANIFEST, false);
|
|
1176
|
+
|
|
1177
|
+
io.execute(() -> {
|
|
1178
|
+
CapgoUpdater.this.cleanupManifestStagingAfterDownloadFailed(id, failedDest, failedManifest);
|
|
1179
|
+
BundleInfo failedBundle = new BundleInfo(
|
|
1180
|
+
id,
|
|
1181
|
+
failedVersion,
|
|
1182
|
+
BundleStatus.ERROR,
|
|
1183
|
+
new Date(System.currentTimeMillis()),
|
|
1184
|
+
""
|
|
1185
|
+
);
|
|
1186
|
+
saveBundleInfo(id, failedBundle);
|
|
1187
|
+
// Cleanup download tracking for failed downloads
|
|
1188
|
+
DownloadWorkerManager.cancelBundleDownload(context, id, failedVersion);
|
|
1189
|
+
Map<String, Object> ret = new HashMap<>();
|
|
1190
|
+
ret.put("version", failedVersion);
|
|
1191
|
+
if ("low_mem_fail".equals(error)) {
|
|
1192
|
+
sendStats("low_mem_fail", failedVersion);
|
|
1193
|
+
}
|
|
1194
|
+
if ("insufficient_disk_space".equals(error)) {
|
|
1195
|
+
sendStats("insufficient_disk_space", failedVersion);
|
|
1196
|
+
}
|
|
1197
|
+
ret.put("error", error != null ? error : "download_fail");
|
|
1198
|
+
sendStats("download_fail", failedVersion);
|
|
1199
|
+
notifyListeners("downloadFailed", ret);
|
|
1200
|
+
|
|
1201
|
+
// Complete the future with error status
|
|
1202
|
+
CompletableFuture<BundleInfo> failedFuture = downloadFutures.remove(id);
|
|
1203
|
+
if (failedFuture != null) {
|
|
1204
|
+
failedFuture.complete(failedBundle);
|
|
1205
|
+
}
|
|
1206
|
+
if (!launchReleasedWhileRetrying.get()) {
|
|
1207
|
+
notifyLaunchDownloadReady(failedFuture != null, false, false, false, false, null);
|
|
1208
|
+
}
|
|
1209
|
+
});
|
|
1210
|
+
break;
|
|
1211
|
+
case CANCELLED:
|
|
1212
|
+
if (!terminalHandled.compareAndSet(false, true)) break;
|
|
1213
|
+
removeObserver.run();
|
|
1214
|
+
observedDownloadVersions.remove(observedVersion);
|
|
1215
|
+
DataManager.getInstance().clearManifest(id);
|
|
1216
|
+
io.execute(() -> CapgoUpdater.this.cleanupManifestStagingAfterDownloadCancelled(id));
|
|
1217
|
+
CompletableFuture<BundleInfo> cancelledFuture = downloadFutures.remove(id);
|
|
1218
|
+
if (cancelledFuture != null) {
|
|
1219
|
+
cancelledFuture.cancel(true);
|
|
1220
|
+
}
|
|
1221
|
+
break;
|
|
1222
|
+
}
|
|
1223
|
+
};
|
|
1224
|
+
workInfosLiveData.observeForever(observerRef[0]);
|
|
815
1225
|
});
|
|
816
1226
|
}
|
|
817
1227
|
|
|
@@ -825,18 +1235,21 @@ public class CapgoUpdater {
|
|
|
825
1235
|
final JSONArray manifest,
|
|
826
1236
|
final boolean setNext
|
|
827
1237
|
) {
|
|
828
|
-
|
|
829
|
-
|
|
1238
|
+
final Context context = this.context();
|
|
1239
|
+
if (context == null) {
|
|
1240
|
+
logger.error("No context, cannot observe work progress");
|
|
830
1241
|
return;
|
|
831
1242
|
}
|
|
832
|
-
|
|
1243
|
+
observedDownloadVersions.add(version);
|
|
1244
|
+
observeWorkProgress(context, id, version, setNext);
|
|
833
1245
|
|
|
834
1246
|
if (manifest != null) {
|
|
835
1247
|
DataManager.getInstance().setManifest(id, manifest);
|
|
1248
|
+
this.saveManifestStagingDest(id, dest);
|
|
836
1249
|
}
|
|
837
1250
|
|
|
838
1251
|
DownloadWorkerManager.enqueueDownload(
|
|
839
|
-
|
|
1252
|
+
context,
|
|
840
1253
|
url,
|
|
841
1254
|
id,
|
|
842
1255
|
this.documentsDir.getAbsolutePath(),
|
|
@@ -899,9 +1312,7 @@ public class CapgoUpdater {
|
|
|
899
1312
|
}
|
|
900
1313
|
// Remove the decryption for manifest downloads
|
|
901
1314
|
} catch (Exception e) {
|
|
902
|
-
|
|
903
|
-
safeDelete(downloaded);
|
|
904
|
-
}
|
|
1315
|
+
safeDelete(downloaded);
|
|
905
1316
|
final Boolean res = this.delete(id);
|
|
906
1317
|
if (!res) {
|
|
907
1318
|
logger.info("Failed to cleanup after error");
|
|
@@ -948,14 +1359,14 @@ public class CapgoUpdater {
|
|
|
948
1359
|
this.directUpdate = false;
|
|
949
1360
|
} else {
|
|
950
1361
|
logger.info("directUpdate: " + this.directUpdate);
|
|
951
|
-
this.setNextBundle(next.getId())
|
|
1362
|
+
if (this.setNextBundle(next.getId())) {
|
|
1363
|
+
this.nextBundleReady(next);
|
|
1364
|
+
}
|
|
952
1365
|
}
|
|
953
1366
|
}
|
|
954
1367
|
} catch (IOException e) {
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
safeDelete(downloaded);
|
|
958
|
-
}
|
|
1368
|
+
safeDelete(extractedDir);
|
|
1369
|
+
safeDelete(downloaded);
|
|
959
1370
|
e.printStackTrace();
|
|
960
1371
|
final Map<String, Object> ret = new HashMap<>();
|
|
961
1372
|
ret.put("version", version);
|
|
@@ -997,11 +1408,11 @@ public class CapgoUpdater {
|
|
|
997
1408
|
}
|
|
998
1409
|
|
|
999
1410
|
public void cleanupDeltaCache(final Thread threadToCheck) {
|
|
1000
|
-
if (this.
|
|
1411
|
+
if (this.context() == null) {
|
|
1001
1412
|
logger.warn("Activity is null, skipping delta cache cleanup");
|
|
1002
1413
|
return;
|
|
1003
1414
|
}
|
|
1004
|
-
final File cacheFolder = new File(this.
|
|
1415
|
+
final File cacheFolder = new File(this.context().getCacheDir(), "capgo_downloads");
|
|
1005
1416
|
if (!cacheFolder.exists()) {
|
|
1006
1417
|
return;
|
|
1007
1418
|
}
|
|
@@ -1267,7 +1678,7 @@ public class CapgoUpdater {
|
|
|
1267
1678
|
final StringBuilder buffer = new StringBuilder();
|
|
1268
1679
|
try (
|
|
1269
1680
|
final BufferedReader reader = new BufferedReader(
|
|
1270
|
-
new InputStreamReader(this.
|
|
1681
|
+
new InputStreamReader(this.context().getAssets().open(assetPath), StandardCharsets.UTF_8)
|
|
1271
1682
|
)
|
|
1272
1683
|
) {
|
|
1273
1684
|
String line;
|
|
@@ -1305,7 +1716,7 @@ public class CapgoUpdater {
|
|
|
1305
1716
|
}
|
|
1306
1717
|
|
|
1307
1718
|
private void syncBackgroundRunnerScriptFromBundle(final File bundle, final BackgroundRunnerWorkConfig config) {
|
|
1308
|
-
if (this.
|
|
1719
|
+
if (this.context() == null || bundle == null || config == null || config.src == null || config.src.isEmpty()) {
|
|
1309
1720
|
return;
|
|
1310
1721
|
}
|
|
1311
1722
|
|
|
@@ -1319,7 +1730,7 @@ public class CapgoUpdater {
|
|
|
1319
1730
|
return;
|
|
1320
1731
|
}
|
|
1321
1732
|
|
|
1322
|
-
final File publicDir = new File(this.
|
|
1733
|
+
final File publicDir = new File(this.context().getFilesDir(), "public");
|
|
1323
1734
|
final File dest = resolvePathInsideDirectory(publicDir, config.src);
|
|
1324
1735
|
this.copyFileAtomically(source, dest);
|
|
1325
1736
|
logger.info("Synced Background Runner script into native public storage before bundle switch.");
|
|
@@ -1330,7 +1741,7 @@ public class CapgoUpdater {
|
|
|
1330
1741
|
}
|
|
1331
1742
|
|
|
1332
1743
|
private void resetBackgroundRunnerWorkForBundleSwitch(final File bundle) {
|
|
1333
|
-
if (this.
|
|
1744
|
+
if (this.context() == null) {
|
|
1334
1745
|
return;
|
|
1335
1746
|
}
|
|
1336
1747
|
|
|
@@ -1346,7 +1757,7 @@ public class CapgoUpdater {
|
|
|
1346
1757
|
}
|
|
1347
1758
|
|
|
1348
1759
|
try {
|
|
1349
|
-
final WorkManager workManager = WorkManager.getInstance(this.
|
|
1760
|
+
final WorkManager workManager = WorkManager.getInstance(this.context().getApplicationContext());
|
|
1350
1761
|
workManager.cancelUniqueWork(config.label);
|
|
1351
1762
|
workManager.cancelAllWorkByTag(config.label);
|
|
1352
1763
|
logger.info("Cancelled Background Runner work before bundle switch.");
|
|
@@ -1376,7 +1787,7 @@ public class CapgoUpdater {
|
|
|
1376
1787
|
.putString("event", config.event)
|
|
1377
1788
|
.build();
|
|
1378
1789
|
final Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
|
|
1379
|
-
final WorkManager workManager = WorkManager.getInstance(this.
|
|
1790
|
+
final WorkManager workManager = WorkManager.getInstance(this.context().getApplicationContext());
|
|
1380
1791
|
|
|
1381
1792
|
if (!config.repeat) {
|
|
1382
1793
|
final OneTimeWorkRequest work = new OneTimeWorkRequest.Builder(workerClass)
|
|
@@ -1446,7 +1857,7 @@ public class CapgoUpdater {
|
|
|
1446
1857
|
downloadBackground(url, version, sessionKey, checksum, manifest, true);
|
|
1447
1858
|
}
|
|
1448
1859
|
|
|
1449
|
-
public
|
|
1860
|
+
public String downloadBackground(
|
|
1450
1861
|
final String url,
|
|
1451
1862
|
final String version,
|
|
1452
1863
|
final String sessionKey,
|
|
@@ -1461,27 +1872,45 @@ public class CapgoUpdater {
|
|
|
1461
1872
|
}
|
|
1462
1873
|
} catch (final IOException e) {
|
|
1463
1874
|
logger.error("Download blocked: " + e.getMessage());
|
|
1464
|
-
return;
|
|
1875
|
+
return null;
|
|
1465
1876
|
}
|
|
1466
1877
|
if (!this.runDownloadGateQuiet()) {
|
|
1467
|
-
return;
|
|
1878
|
+
return null;
|
|
1468
1879
|
}
|
|
1469
1880
|
final String id = this.randomString();
|
|
1470
1881
|
|
|
1471
1882
|
// Check if version is already downloading, but allow retry if previous download failed
|
|
1472
|
-
if (this.
|
|
1883
|
+
if (this.context() != null && DownloadWorkerManager.isVersionDownloading(this.context(), version)) {
|
|
1473
1884
|
// Check if there's an existing bundle with error status that we can retry
|
|
1474
1885
|
BundleInfo existingBundle = this.getBundleInfoByName(version);
|
|
1475
1886
|
if (existingBundle != null && existingBundle.isErrorStatus()) {
|
|
1476
1887
|
// Cancel the failed download and allow retry
|
|
1477
|
-
if (!DownloadWorkerManager.cancelVersionDownloadAndAwait(this.
|
|
1888
|
+
if (!DownloadWorkerManager.cancelVersionDownloadAndAwait(this.context(), version)) {
|
|
1478
1889
|
logger.error("Failed to cancel previous download before retry");
|
|
1479
|
-
return;
|
|
1890
|
+
return null;
|
|
1480
1891
|
}
|
|
1481
1892
|
logger.info("Retrying failed download for version: " + version);
|
|
1893
|
+
} else if (shouldRestartOrphanedDownload(observedDownloadVersions.contains(version))) {
|
|
1894
|
+
// Left over from a killed process: nothing would finish it or release the launch, so start over.
|
|
1895
|
+
if (!DownloadWorkerManager.cancelVersionDownloadAndAwait(this.context(), version)) {
|
|
1896
|
+
logger.error("Failed to cancel orphaned download before restarting it");
|
|
1897
|
+
return null;
|
|
1898
|
+
}
|
|
1899
|
+
BundleInfo orphanedBundle = null;
|
|
1900
|
+
for (final BundleInfo info : this.list(true)) {
|
|
1901
|
+
if (info != null && info.getStatus() == BundleStatus.DOWNLOADING && version.equals(info.getVersionName())) {
|
|
1902
|
+
orphanedBundle = info;
|
|
1903
|
+
break;
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
if (orphanedBundle != null) {
|
|
1907
|
+
this.cleanupManifestStagingAfterDownloadCancelled(orphanedBundle.getId());
|
|
1908
|
+
this.saveBundleInfo(orphanedBundle.getId(), orphanedBundle.setStatus(BundleStatus.ERROR));
|
|
1909
|
+
}
|
|
1910
|
+
logger.info("Restarting download orphaned by a previous process for version: " + version);
|
|
1482
1911
|
} else {
|
|
1483
1912
|
logger.info("Version already downloading: " + version);
|
|
1484
|
-
return;
|
|
1913
|
+
return null;
|
|
1485
1914
|
}
|
|
1486
1915
|
}
|
|
1487
1916
|
|
|
@@ -1489,7 +1918,9 @@ public class CapgoUpdater {
|
|
|
1489
1918
|
this.notifyDownload(id, 0);
|
|
1490
1919
|
this.notifyDownload(id, 5);
|
|
1491
1920
|
|
|
1492
|
-
this.
|
|
1921
|
+
final String dest = manifest != null ? this.manifestStagingDest() : this.randomString();
|
|
1922
|
+
this.download(id, url, dest, version, sessionKey, checksum, manifest, setNext);
|
|
1923
|
+
return id;
|
|
1493
1924
|
}
|
|
1494
1925
|
|
|
1495
1926
|
public BundleInfo download(final String url, final String version, final String sessionKey, final String checksum) throws IOException {
|
|
@@ -1522,7 +1953,8 @@ public class CapgoUpdater {
|
|
|
1522
1953
|
try {
|
|
1523
1954
|
BundleInfo result = downloadFuture.get();
|
|
1524
1955
|
if (result.isErrorStatus()) {
|
|
1525
|
-
|
|
1956
|
+
// The download observer already emitted downloadFailed and its stats.
|
|
1957
|
+
throw new ReportedDownloadFailureException("Download failed with status: " + result.getStatus());
|
|
1526
1958
|
}
|
|
1527
1959
|
return result;
|
|
1528
1960
|
} catch (Exception e) {
|
|
@@ -1565,7 +1997,7 @@ public class CapgoUpdater {
|
|
|
1565
1997
|
saveBundleInfo(id, new BundleInfo(id, version, BundleStatus.DOWNLOADING, new Date(System.currentTimeMillis()), ""));
|
|
1566
1998
|
this.notifyDownload(id, 0);
|
|
1567
1999
|
this.notifyDownload(id, 5);
|
|
1568
|
-
final String dest = this.
|
|
2000
|
+
final String dest = this.manifestStagingDest();
|
|
1569
2001
|
|
|
1570
2002
|
// Create a CompletableFuture to track download completion
|
|
1571
2003
|
CompletableFuture<BundleInfo> downloadFuture = new CompletableFuture<>();
|
|
@@ -1578,7 +2010,8 @@ public class CapgoUpdater {
|
|
|
1578
2010
|
try {
|
|
1579
2011
|
BundleInfo result = downloadFuture.get();
|
|
1580
2012
|
if (result.isErrorStatus()) {
|
|
1581
|
-
|
|
2013
|
+
// The download observer already emitted downloadFailed and its stats.
|
|
2014
|
+
throw new ReportedDownloadFailureException("Download failed with status: " + result.getStatus());
|
|
1582
2015
|
}
|
|
1583
2016
|
return result;
|
|
1584
2017
|
} catch (Exception e) {
|
|
@@ -1679,8 +2112,8 @@ public class CapgoUpdater {
|
|
|
1679
2112
|
}
|
|
1680
2113
|
|
|
1681
2114
|
// Cancel download for this version if active
|
|
1682
|
-
if (cancelActiveDownload && this.
|
|
1683
|
-
if (!DownloadWorkerManager.cancelVersionDownloadAndAwait(this.
|
|
2115
|
+
if (cancelActiveDownload && this.context() != null) {
|
|
2116
|
+
if (!DownloadWorkerManager.cancelVersionDownloadAndAwait(this.context(), deleted.getVersionName())) {
|
|
1684
2117
|
logger.error("Failed to cancel active download before delete");
|
|
1685
2118
|
return false;
|
|
1686
2119
|
}
|
|
@@ -1873,8 +2306,8 @@ public class CapgoUpdater {
|
|
|
1873
2306
|
}
|
|
1874
2307
|
|
|
1875
2308
|
void finalizeResetTransition(final String previousBundleName, final boolean internal) {
|
|
1876
|
-
if (this.
|
|
1877
|
-
DownloadWorkerManager.cancelAllDownloads(this.
|
|
2309
|
+
if (this.context() != null) {
|
|
2310
|
+
DownloadWorkerManager.cancelAllDownloads(this.context());
|
|
1878
2311
|
}
|
|
1879
2312
|
if (!internal) {
|
|
1880
2313
|
this.sendStats("reset", this.getCurrentBundle().getVersionName(), previousBundleName);
|
|
@@ -2052,8 +2485,8 @@ public class CapgoUpdater {
|
|
|
2052
2485
|
final String asyncPreviousFallbackId = previousFallbackId;
|
|
2053
2486
|
final String asyncPreviousFallbackVersion = previousFallbackVersion;
|
|
2054
2487
|
io.execute(() -> {
|
|
2055
|
-
if (this.
|
|
2056
|
-
if (!DownloadWorkerManager.cancelVersionDownloadAndAwait(this.
|
|
2488
|
+
if (this.context() != null) {
|
|
2489
|
+
if (!DownloadWorkerManager.cancelVersionDownloadAndAwait(this.context(), asyncPreviousFallbackVersion)) {
|
|
2057
2490
|
logger.error("Failed to cancel previous version download before delete");
|
|
2058
2491
|
return;
|
|
2059
2492
|
}
|
|
@@ -2507,8 +2940,18 @@ public class CapgoUpdater {
|
|
|
2507
2940
|
final SharedPreferences.Editor editor,
|
|
2508
2941
|
final String defaultChannelKey,
|
|
2509
2942
|
final String configDefaultChannel,
|
|
2943
|
+
final boolean allowSetDefaultChannel,
|
|
2510
2944
|
final Callback callback
|
|
2511
2945
|
) {
|
|
2946
|
+
if (!allowSetDefaultChannel) {
|
|
2947
|
+
logger.error("unsetChannel is disabled by allowSetDefaultChannel config");
|
|
2948
|
+
final Map<String, Object> retError = new HashMap<>();
|
|
2949
|
+
retError.put("message", "unsetChannel is disabled by configuration");
|
|
2950
|
+
retError.put("error", "disabled_by_config");
|
|
2951
|
+
callback.callback(retError);
|
|
2952
|
+
return;
|
|
2953
|
+
}
|
|
2954
|
+
|
|
2512
2955
|
// Clear persisted defaultChannel and revert to config value
|
|
2513
2956
|
editor.remove(defaultChannelKey);
|
|
2514
2957
|
editor.apply();
|
|
@@ -2996,7 +3439,7 @@ public class CapgoUpdater {
|
|
|
2996
3439
|
if (statsQueue.size() >= MAX_PENDING_STATS) {
|
|
2997
3440
|
break;
|
|
2998
3441
|
}
|
|
2999
|
-
statsQueue.add(new QueuedStatsEvent(arr.getJSONObject(i), null));
|
|
3442
|
+
statsQueue.add(new QueuedStatsEvent(arr.getJSONObject(i), null, true));
|
|
3000
3443
|
}
|
|
3001
3444
|
}
|
|
3002
3445
|
if (backup.exists() && !backup.delete()) {
|
|
@@ -3018,6 +3461,147 @@ public class CapgoUpdater {
|
|
|
3018
3461
|
}
|
|
3019
3462
|
}
|
|
3020
3463
|
|
|
3464
|
+
/** Makes this instance the stats file owner and adopts events handed over before it loaded. */
|
|
3465
|
+
public void claimStatsOwnership() {
|
|
3466
|
+
final List<JSONObject> adopted;
|
|
3467
|
+
synchronized (statsOwnerLock) {
|
|
3468
|
+
statsOwner = this;
|
|
3469
|
+
adopted = new ArrayList<>(orphanedStats);
|
|
3470
|
+
orphanedStats.clear();
|
|
3471
|
+
}
|
|
3472
|
+
if (!adoptPendingStats(adopted)) {
|
|
3473
|
+
parkOrphanedStats(adopted);
|
|
3474
|
+
}
|
|
3475
|
+
}
|
|
3476
|
+
|
|
3477
|
+
public void releaseStatsOwnership() {
|
|
3478
|
+
synchronized (statsOwnerLock) {
|
|
3479
|
+
if (statsOwner == this) {
|
|
3480
|
+
statsOwner = null;
|
|
3481
|
+
}
|
|
3482
|
+
}
|
|
3483
|
+
}
|
|
3484
|
+
|
|
3485
|
+
/** Moves this instance's unsent events to the stats file owner (or parks them until one claims). */
|
|
3486
|
+
void handOverPendingStats() {
|
|
3487
|
+
// Stop first so a flush finishing later cannot requeue or persist what we hand over.
|
|
3488
|
+
statsStopped.set(true);
|
|
3489
|
+
final CapgoUpdater owner;
|
|
3490
|
+
synchronized (statsOwnerLock) {
|
|
3491
|
+
owner = statsOwner == this ? null : statsOwner;
|
|
3492
|
+
}
|
|
3493
|
+
final List<JSONObject> events = new ArrayList<>();
|
|
3494
|
+
synchronized (statsQueue) {
|
|
3495
|
+
collectHandoverEvents(statsInFlight, events, owner);
|
|
3496
|
+
statsInFlight.clear();
|
|
3497
|
+
collectHandoverEvents(statsQueue, events, owner);
|
|
3498
|
+
statsQueue.clear();
|
|
3499
|
+
}
|
|
3500
|
+
if (events.isEmpty()) {
|
|
3501
|
+
return;
|
|
3502
|
+
}
|
|
3503
|
+
// adoptPendingStats persists; never call it while holding statsOwnerLock (lock order).
|
|
3504
|
+
if (owner != null && owner.adoptPendingStats(events)) {
|
|
3505
|
+
return;
|
|
3506
|
+
}
|
|
3507
|
+
List<JSONObject> retryEvents = null;
|
|
3508
|
+
CapgoUpdater retryOwner = null;
|
|
3509
|
+
synchronized (statsOwnerLock) {
|
|
3510
|
+
addOrphanedStats(events);
|
|
3511
|
+
retryOwner = statsOwner == this ? null : statsOwner;
|
|
3512
|
+
if (retryOwner != null && !orphanedStats.isEmpty()) {
|
|
3513
|
+
retryEvents = new ArrayList<>(orphanedStats);
|
|
3514
|
+
orphanedStats.clear();
|
|
3515
|
+
}
|
|
3516
|
+
}
|
|
3517
|
+
if (retryOwner != null && retryEvents != null) {
|
|
3518
|
+
if (retryOwner.adoptPendingStats(retryEvents)) {
|
|
3519
|
+
return;
|
|
3520
|
+
}
|
|
3521
|
+
parkOrphanedStats(retryEvents);
|
|
3522
|
+
}
|
|
3523
|
+
}
|
|
3524
|
+
|
|
3525
|
+
private void collectHandoverEvents(final List<QueuedStatsEvent> source, final List<JSONObject> events, final CapgoUpdater owner) {
|
|
3526
|
+
for (final QueuedStatsEvent queued : source) {
|
|
3527
|
+
if (shouldHandOverQueuedEvent(queued, owner)) {
|
|
3528
|
+
events.add(queued.event);
|
|
3529
|
+
}
|
|
3530
|
+
}
|
|
3531
|
+
}
|
|
3532
|
+
|
|
3533
|
+
/**
|
|
3534
|
+
* Restored file events are not re-sent to an owner that shares the same pending-stats path
|
|
3535
|
+
* (the owner already loaded them from disk). They are still handed over when the paths differ.
|
|
3536
|
+
*/
|
|
3537
|
+
private boolean shouldHandOverQueuedEvent(final QueuedStatsEvent queued, final CapgoUpdater owner) {
|
|
3538
|
+
if (!queued.restored) {
|
|
3539
|
+
return true;
|
|
3540
|
+
}
|
|
3541
|
+
if (owner == null) {
|
|
3542
|
+
return true;
|
|
3543
|
+
}
|
|
3544
|
+
return !sharesPendingStatsFileWith(owner);
|
|
3545
|
+
}
|
|
3546
|
+
|
|
3547
|
+
private boolean sharesPendingStatsFileWith(final CapgoUpdater other) {
|
|
3548
|
+
final File mine = pendingStatsFile();
|
|
3549
|
+
final File theirs = other.pendingStatsFile();
|
|
3550
|
+
return mine != null && theirs != null && mine.getAbsolutePath().equals(theirs.getAbsolutePath());
|
|
3551
|
+
}
|
|
3552
|
+
|
|
3553
|
+
private static void addOrphanedStats(final List<JSONObject> events) {
|
|
3554
|
+
orphanedStats.addAll(events);
|
|
3555
|
+
while (orphanedStats.size() > MAX_PENDING_STATS) {
|
|
3556
|
+
orphanedStats.remove(0);
|
|
3557
|
+
}
|
|
3558
|
+
}
|
|
3559
|
+
|
|
3560
|
+
private static void parkOrphanedStats(final List<JSONObject> events) {
|
|
3561
|
+
synchronized (statsOwnerLock) {
|
|
3562
|
+
addOrphanedStats(events);
|
|
3563
|
+
}
|
|
3564
|
+
}
|
|
3565
|
+
|
|
3566
|
+
/** Returns false (keeping nothing) when this instance has stopped and can no longer send the events. */
|
|
3567
|
+
private boolean adoptPendingStats(final List<JSONObject> events) {
|
|
3568
|
+
if (events.isEmpty()) {
|
|
3569
|
+
return true;
|
|
3570
|
+
}
|
|
3571
|
+
final List<QueuedStatsEvent> displaced = new ArrayList<>();
|
|
3572
|
+
synchronized (statsQueue) {
|
|
3573
|
+
if (statsStopped.get()) {
|
|
3574
|
+
return false;
|
|
3575
|
+
}
|
|
3576
|
+
for (final JSONObject event : events) {
|
|
3577
|
+
while (statsQueue.size() >= MAX_PENDING_STATS) {
|
|
3578
|
+
displaced.add(0, statsQueue.remove(0));
|
|
3579
|
+
}
|
|
3580
|
+
statsQueue.add(new QueuedStatsEvent(event, null));
|
|
3581
|
+
}
|
|
3582
|
+
}
|
|
3583
|
+
synchronized (statsOwnerLock) {
|
|
3584
|
+
if (statsOwner != null && statsOwner != this) {
|
|
3585
|
+
rollbackAdoptedEvents(events.size(), displaced);
|
|
3586
|
+
return false;
|
|
3587
|
+
}
|
|
3588
|
+
}
|
|
3589
|
+
persistStatsQueue();
|
|
3590
|
+
ensureStatsTimerStarted();
|
|
3591
|
+
return true;
|
|
3592
|
+
}
|
|
3593
|
+
|
|
3594
|
+
private void rollbackAdoptedEvents(final int adoptedCount, final List<QueuedStatsEvent> displaced) {
|
|
3595
|
+
synchronized (statsQueue) {
|
|
3596
|
+
for (int removed = 0; removed < adoptedCount && !statsQueue.isEmpty(); removed++) {
|
|
3597
|
+
statsQueue.remove(statsQueue.size() - 1);
|
|
3598
|
+
}
|
|
3599
|
+
for (int i = 0; i < displaced.size(); i++) {
|
|
3600
|
+
statsQueue.add(i, displaced.get(i));
|
|
3601
|
+
}
|
|
3602
|
+
}
|
|
3603
|
+
}
|
|
3604
|
+
|
|
3021
3605
|
int pendingStatsCount() {
|
|
3022
3606
|
return statsQueue.size();
|
|
3023
3607
|
}
|
|
@@ -3044,6 +3628,11 @@ public class CapgoUpdater {
|
|
|
3044
3628
|
return;
|
|
3045
3629
|
}
|
|
3046
3630
|
synchronized (pendingStatsPersistLock) {
|
|
3631
|
+
synchronized (statsOwnerLock) {
|
|
3632
|
+
if (statsOwner != null && statsOwner != this) {
|
|
3633
|
+
return;
|
|
3634
|
+
}
|
|
3635
|
+
}
|
|
3047
3636
|
if (statsStopped.get() && !force) {
|
|
3048
3637
|
return;
|
|
3049
3638
|
}
|
|
@@ -3425,6 +4014,16 @@ public class CapgoUpdater {
|
|
|
3425
4014
|
return this.getBundleInfo(id);
|
|
3426
4015
|
}
|
|
3427
4016
|
|
|
4017
|
+
public boolean clearNextBundleIfCurrent() {
|
|
4018
|
+
final BundleInfo next = this.getNextBundle();
|
|
4019
|
+
if (next == null || !next.getId().equals(this.getCurrentBundle().getId())) {
|
|
4020
|
+
return false;
|
|
4021
|
+
}
|
|
4022
|
+
logger.info("Next bundle " + next.getId() + " is already the current bundle. Clearing next.");
|
|
4023
|
+
this.setNextBundle(null);
|
|
4024
|
+
return true;
|
|
4025
|
+
}
|
|
4026
|
+
|
|
3428
4027
|
public BundleInfo getPreviewFallbackBundle() {
|
|
3429
4028
|
final String id = this.prefs.getString(PREVIEW_FALLBACK_VERSION, null);
|
|
3430
4029
|
if (id == null) return null;
|
|
@@ -3459,6 +4058,10 @@ public class CapgoUpdater {
|
|
|
3459
4058
|
if (!newBundle.isBuiltin() && !this.bundleExists(next)) {
|
|
3460
4059
|
return false;
|
|
3461
4060
|
}
|
|
4061
|
+
if (next.equals(this.getCurrentBundleId()) && BundleStatus.SUCCESS == newBundle.getStatus()) {
|
|
4062
|
+
logger.info("Bundle " + next + " is already the current successful bundle. Skip next().");
|
|
4063
|
+
return true;
|
|
4064
|
+
}
|
|
3462
4065
|
this.editor.putString(NEXT_VERSION, next);
|
|
3463
4066
|
this.setBundleStatus(next, BundleStatus.PENDING);
|
|
3464
4067
|
bundleToNotify = newBundle;
|
|
@@ -3478,6 +4081,14 @@ public class CapgoUpdater {
|
|
|
3478
4081
|
* Should be called when the plugin is destroyed to prevent resource leaks.
|
|
3479
4082
|
*/
|
|
3480
4083
|
public void shutdown() {
|
|
4084
|
+
shutdown(true);
|
|
4085
|
+
}
|
|
4086
|
+
|
|
4087
|
+
/**
|
|
4088
|
+
* @param persistStats false when another live instance owns the stats file (headless worker
|
|
4089
|
+
* handing over to the plugin): writing this instance's queue would replace the newer file.
|
|
4090
|
+
*/
|
|
4091
|
+
public void shutdown(final boolean persistStats) {
|
|
3481
4092
|
statsStopped.set(true);
|
|
3482
4093
|
// Cancel the scheduled task
|
|
3483
4094
|
if (statsFlushTask != null) {
|
|
@@ -3486,7 +4097,9 @@ public class CapgoUpdater {
|
|
|
3486
4097
|
}
|
|
3487
4098
|
|
|
3488
4099
|
// Write once, then ignore later callbacks so they cannot delete a newer instance's file.
|
|
3489
|
-
|
|
4100
|
+
if (persistStats) {
|
|
4101
|
+
persistStatsQueue(true);
|
|
4102
|
+
}
|
|
3490
4103
|
|
|
3491
4104
|
// Shutdown the scheduler
|
|
3492
4105
|
statsScheduler.shutdown();
|