@capgo/capacitor-updater 8.53.0 → 8.53.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +5 -2
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +173 -21
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +4 -4
- package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +10 -4
- package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +34 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +69 -11
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +10 -0
- package/package.json +1 -1
|
@@ -819,9 +819,8 @@ public class CapacitorUpdaterPlugin extends Plugin {
|
|
|
819
819
|
DownloadService.setLogger(logger);
|
|
820
820
|
DownloadWorkerManager.setLogger(logger);
|
|
821
821
|
|
|
822
|
-
final CapConfig config = CapConfig.loadDefault(this.getActivity());
|
|
823
822
|
this.implementation.appId = InternalUtils.getPackageName(getContext().getPackageManager(), getContext().getPackageName());
|
|
824
|
-
this.implementation.appId =
|
|
823
|
+
this.implementation.appId = InternalUtils.getRootConfigString(getContext(), "appId", this.implementation.appId);
|
|
825
824
|
this.implementation.appId = this.getConfig().getString("appId", this.implementation.appId);
|
|
826
825
|
if (this.implementation.appId == null || this.implementation.appId.isEmpty()) {
|
|
827
826
|
// crash the app on purpose it should not happen
|
|
@@ -913,6 +912,7 @@ public class CapacitorUpdaterPlugin extends Plugin {
|
|
|
913
912
|
// Use DeviceIdHelper to get or create device ID that persists across reinstalls
|
|
914
913
|
this.implementation.deviceID = DeviceIdHelper.getOrCreateDeviceId(this.getContext(), this.prefs);
|
|
915
914
|
this.implementation.restorePendingStats();
|
|
915
|
+
this.implementation.claimStatsOwnership();
|
|
916
916
|
|
|
917
917
|
// Update User-Agent for shared OkHttpClient with OS version
|
|
918
918
|
DownloadService.updateUserAgent(this.implementation.appId, this.pluginVersion, this.implementation.versionOs);
|
|
@@ -6269,6 +6269,9 @@ public class CapacitorUpdaterPlugin extends Plugin {
|
|
|
6269
6269
|
|
|
6270
6270
|
private void handleOnDestroyInternal() {
|
|
6271
6271
|
instanceLoaded = false;
|
|
6272
|
+
if (this.implementation != null) {
|
|
6273
|
+
this.implementation.releaseStatsOwnership();
|
|
6274
|
+
}
|
|
6272
6275
|
// Original handleOnDestroy code
|
|
6273
6276
|
try {
|
|
6274
6277
|
logger.info("onActivityDestroyed " + getActivity().getClass().getName());
|
|
@@ -27,14 +27,12 @@ import androidx.work.PeriodicWorkRequest;
|
|
|
27
27
|
import androidx.work.WorkInfo;
|
|
28
28
|
import androidx.work.WorkManager;
|
|
29
29
|
import java.io.BufferedInputStream;
|
|
30
|
-
import java.io.BufferedReader;
|
|
31
30
|
import java.io.File;
|
|
32
31
|
import java.io.FileInputStream;
|
|
33
32
|
import java.io.FileNotFoundException;
|
|
34
33
|
import java.io.FileOutputStream;
|
|
35
34
|
import java.io.FilenameFilter;
|
|
36
35
|
import java.io.IOException;
|
|
37
|
-
import java.io.InputStreamReader;
|
|
38
36
|
import java.nio.charset.StandardCharsets;
|
|
39
37
|
import java.nio.file.Files;
|
|
40
38
|
import java.security.SecureRandom;
|
|
@@ -87,7 +85,6 @@ public class CapgoUpdater {
|
|
|
87
85
|
private static final String LEGACY_MANIFEST_STAGING_FOLDER_PATTERN = "^[0-9A-Za-z]{10}$";
|
|
88
86
|
private static final long DELETE_PACE_MS = 75L;
|
|
89
87
|
private final Object deleteLock = new Object();
|
|
90
|
-
private static final String CAPACITOR_CONFIG_ASSET = "capacitor.config.json";
|
|
91
88
|
private static final String BACKGROUND_RUNNER_CONFIG_KEY = "BackgroundRunner";
|
|
92
89
|
private static final String BACKGROUND_RUNNER_WORKER_CLASS = "io.ionic.backgroundrunner.plugin.RunnerWorker";
|
|
93
90
|
|
|
@@ -149,15 +146,27 @@ public class CapgoUpdater {
|
|
|
149
146
|
private static final long STATS_FLUSH_INTERVAL_MS = 1000;
|
|
150
147
|
private static final String PENDING_STATS_FILE = "capgo_pending_stats.json";
|
|
151
148
|
private static final int MAX_PENDING_STATS = 200;
|
|
149
|
+
// The live plugin instance owns the stats file. A headless worker hands its unsent events to it
|
|
150
|
+
// instead of writing the file; events handed over before the plugin claims ownership wait here.
|
|
151
|
+
private static final Object statsOwnerLock = new Object();
|
|
152
|
+
private static CapgoUpdater statsOwner = null;
|
|
153
|
+
private static final List<JSONObject> orphanedStats = new ArrayList<>();
|
|
152
154
|
|
|
153
155
|
private static final class QueuedStatsEvent {
|
|
154
156
|
|
|
155
157
|
private final JSONObject event;
|
|
156
158
|
private final Runnable onSent;
|
|
159
|
+
/** True when the event was loaded from the shared pending-stats file (not created by this instance). */
|
|
160
|
+
private final boolean restored;
|
|
157
161
|
|
|
158
162
|
private QueuedStatsEvent(final JSONObject event, final Runnable onSent) {
|
|
163
|
+
this(event, onSent, false);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private QueuedStatsEvent(final JSONObject event, final Runnable onSent, final boolean restored) {
|
|
159
167
|
this.event = event;
|
|
160
168
|
this.onSent = onSent;
|
|
169
|
+
this.restored = restored;
|
|
161
170
|
}
|
|
162
171
|
}
|
|
163
172
|
|
|
@@ -1662,21 +1671,6 @@ public class CapgoUpdater {
|
|
|
1662
1671
|
return config == null ? null : config.label;
|
|
1663
1672
|
}
|
|
1664
1673
|
|
|
1665
|
-
private String readAssetAsString(final String assetPath) throws IOException {
|
|
1666
|
-
final StringBuilder buffer = new StringBuilder();
|
|
1667
|
-
try (
|
|
1668
|
-
final BufferedReader reader = new BufferedReader(
|
|
1669
|
-
new InputStreamReader(this.context().getAssets().open(assetPath), StandardCharsets.UTF_8)
|
|
1670
|
-
)
|
|
1671
|
-
) {
|
|
1672
|
-
String line;
|
|
1673
|
-
while ((line = reader.readLine()) != null) {
|
|
1674
|
-
buffer.append(line).append('\n');
|
|
1675
|
-
}
|
|
1676
|
-
}
|
|
1677
|
-
return buffer.toString();
|
|
1678
|
-
}
|
|
1679
|
-
|
|
1680
1674
|
private void copyFileAtomically(final File source, final File dest) throws IOException {
|
|
1681
1675
|
final File parent = dest.getParentFile();
|
|
1682
1676
|
if (parent != null && !parent.exists() && !parent.mkdirs()) {
|
|
@@ -1735,7 +1729,9 @@ public class CapgoUpdater {
|
|
|
1735
1729
|
|
|
1736
1730
|
final BackgroundRunnerWorkConfig config;
|
|
1737
1731
|
try {
|
|
1738
|
-
config = getBackgroundRunnerWorkConfigFromConfig(
|
|
1732
|
+
config = getBackgroundRunnerWorkConfigFromConfig(
|
|
1733
|
+
InternalUtils.readAssetAsString(this.context(), InternalUtils.CAPACITOR_CONFIG_ASSET)
|
|
1734
|
+
);
|
|
1739
1735
|
} catch (IOException ignored) {
|
|
1740
1736
|
return;
|
|
1741
1737
|
}
|
|
@@ -3427,7 +3423,7 @@ public class CapgoUpdater {
|
|
|
3427
3423
|
if (statsQueue.size() >= MAX_PENDING_STATS) {
|
|
3428
3424
|
break;
|
|
3429
3425
|
}
|
|
3430
|
-
statsQueue.add(new QueuedStatsEvent(arr.getJSONObject(i), null));
|
|
3426
|
+
statsQueue.add(new QueuedStatsEvent(arr.getJSONObject(i), null, true));
|
|
3431
3427
|
}
|
|
3432
3428
|
}
|
|
3433
3429
|
if (backup.exists() && !backup.delete()) {
|
|
@@ -3449,6 +3445,147 @@ public class CapgoUpdater {
|
|
|
3449
3445
|
}
|
|
3450
3446
|
}
|
|
3451
3447
|
|
|
3448
|
+
/** Makes this instance the stats file owner and adopts events handed over before it loaded. */
|
|
3449
|
+
public void claimStatsOwnership() {
|
|
3450
|
+
final List<JSONObject> adopted;
|
|
3451
|
+
synchronized (statsOwnerLock) {
|
|
3452
|
+
statsOwner = this;
|
|
3453
|
+
adopted = new ArrayList<>(orphanedStats);
|
|
3454
|
+
orphanedStats.clear();
|
|
3455
|
+
}
|
|
3456
|
+
if (!adoptPendingStats(adopted)) {
|
|
3457
|
+
parkOrphanedStats(adopted);
|
|
3458
|
+
}
|
|
3459
|
+
}
|
|
3460
|
+
|
|
3461
|
+
public void releaseStatsOwnership() {
|
|
3462
|
+
synchronized (statsOwnerLock) {
|
|
3463
|
+
if (statsOwner == this) {
|
|
3464
|
+
statsOwner = null;
|
|
3465
|
+
}
|
|
3466
|
+
}
|
|
3467
|
+
}
|
|
3468
|
+
|
|
3469
|
+
/** Moves this instance's unsent events to the stats file owner (or parks them until one claims). */
|
|
3470
|
+
void handOverPendingStats() {
|
|
3471
|
+
// Stop first so a flush finishing later cannot requeue or persist what we hand over.
|
|
3472
|
+
statsStopped.set(true);
|
|
3473
|
+
final CapgoUpdater owner;
|
|
3474
|
+
synchronized (statsOwnerLock) {
|
|
3475
|
+
owner = statsOwner == this ? null : statsOwner;
|
|
3476
|
+
}
|
|
3477
|
+
final List<JSONObject> events = new ArrayList<>();
|
|
3478
|
+
synchronized (statsQueue) {
|
|
3479
|
+
collectHandoverEvents(statsInFlight, events, owner);
|
|
3480
|
+
statsInFlight.clear();
|
|
3481
|
+
collectHandoverEvents(statsQueue, events, owner);
|
|
3482
|
+
statsQueue.clear();
|
|
3483
|
+
}
|
|
3484
|
+
if (events.isEmpty()) {
|
|
3485
|
+
return;
|
|
3486
|
+
}
|
|
3487
|
+
// adoptPendingStats persists; never call it while holding statsOwnerLock (lock order).
|
|
3488
|
+
if (owner != null && owner.adoptPendingStats(events)) {
|
|
3489
|
+
return;
|
|
3490
|
+
}
|
|
3491
|
+
List<JSONObject> retryEvents = null;
|
|
3492
|
+
CapgoUpdater retryOwner = null;
|
|
3493
|
+
synchronized (statsOwnerLock) {
|
|
3494
|
+
addOrphanedStats(events);
|
|
3495
|
+
retryOwner = statsOwner == this ? null : statsOwner;
|
|
3496
|
+
if (retryOwner != null && !orphanedStats.isEmpty()) {
|
|
3497
|
+
retryEvents = new ArrayList<>(orphanedStats);
|
|
3498
|
+
orphanedStats.clear();
|
|
3499
|
+
}
|
|
3500
|
+
}
|
|
3501
|
+
if (retryOwner != null && retryEvents != null) {
|
|
3502
|
+
if (retryOwner.adoptPendingStats(retryEvents)) {
|
|
3503
|
+
return;
|
|
3504
|
+
}
|
|
3505
|
+
parkOrphanedStats(retryEvents);
|
|
3506
|
+
}
|
|
3507
|
+
}
|
|
3508
|
+
|
|
3509
|
+
private void collectHandoverEvents(final List<QueuedStatsEvent> source, final List<JSONObject> events, final CapgoUpdater owner) {
|
|
3510
|
+
for (final QueuedStatsEvent queued : source) {
|
|
3511
|
+
if (shouldHandOverQueuedEvent(queued, owner)) {
|
|
3512
|
+
events.add(queued.event);
|
|
3513
|
+
}
|
|
3514
|
+
}
|
|
3515
|
+
}
|
|
3516
|
+
|
|
3517
|
+
/**
|
|
3518
|
+
* Restored file events are not re-sent to an owner that shares the same pending-stats path
|
|
3519
|
+
* (the owner already loaded them from disk). They are still handed over when the paths differ.
|
|
3520
|
+
*/
|
|
3521
|
+
private boolean shouldHandOverQueuedEvent(final QueuedStatsEvent queued, final CapgoUpdater owner) {
|
|
3522
|
+
if (!queued.restored) {
|
|
3523
|
+
return true;
|
|
3524
|
+
}
|
|
3525
|
+
if (owner == null) {
|
|
3526
|
+
return true;
|
|
3527
|
+
}
|
|
3528
|
+
return !sharesPendingStatsFileWith(owner);
|
|
3529
|
+
}
|
|
3530
|
+
|
|
3531
|
+
private boolean sharesPendingStatsFileWith(final CapgoUpdater other) {
|
|
3532
|
+
final File mine = pendingStatsFile();
|
|
3533
|
+
final File theirs = other.pendingStatsFile();
|
|
3534
|
+
return mine != null && theirs != null && mine.getAbsolutePath().equals(theirs.getAbsolutePath());
|
|
3535
|
+
}
|
|
3536
|
+
|
|
3537
|
+
private static void addOrphanedStats(final List<JSONObject> events) {
|
|
3538
|
+
orphanedStats.addAll(events);
|
|
3539
|
+
while (orphanedStats.size() > MAX_PENDING_STATS) {
|
|
3540
|
+
orphanedStats.remove(0);
|
|
3541
|
+
}
|
|
3542
|
+
}
|
|
3543
|
+
|
|
3544
|
+
private static void parkOrphanedStats(final List<JSONObject> events) {
|
|
3545
|
+
synchronized (statsOwnerLock) {
|
|
3546
|
+
addOrphanedStats(events);
|
|
3547
|
+
}
|
|
3548
|
+
}
|
|
3549
|
+
|
|
3550
|
+
/** Returns false (keeping nothing) when this instance has stopped and can no longer send the events. */
|
|
3551
|
+
private boolean adoptPendingStats(final List<JSONObject> events) {
|
|
3552
|
+
if (events.isEmpty()) {
|
|
3553
|
+
return true;
|
|
3554
|
+
}
|
|
3555
|
+
final List<QueuedStatsEvent> displaced = new ArrayList<>();
|
|
3556
|
+
synchronized (statsQueue) {
|
|
3557
|
+
if (statsStopped.get()) {
|
|
3558
|
+
return false;
|
|
3559
|
+
}
|
|
3560
|
+
for (final JSONObject event : events) {
|
|
3561
|
+
while (statsQueue.size() >= MAX_PENDING_STATS) {
|
|
3562
|
+
displaced.add(0, statsQueue.remove(0));
|
|
3563
|
+
}
|
|
3564
|
+
statsQueue.add(new QueuedStatsEvent(event, null));
|
|
3565
|
+
}
|
|
3566
|
+
}
|
|
3567
|
+
synchronized (statsOwnerLock) {
|
|
3568
|
+
if (statsOwner != null && statsOwner != this) {
|
|
3569
|
+
rollbackAdoptedEvents(events.size(), displaced);
|
|
3570
|
+
return false;
|
|
3571
|
+
}
|
|
3572
|
+
}
|
|
3573
|
+
persistStatsQueue();
|
|
3574
|
+
ensureStatsTimerStarted();
|
|
3575
|
+
return true;
|
|
3576
|
+
}
|
|
3577
|
+
|
|
3578
|
+
private void rollbackAdoptedEvents(final int adoptedCount, final List<QueuedStatsEvent> displaced) {
|
|
3579
|
+
synchronized (statsQueue) {
|
|
3580
|
+
for (int removed = 0; removed < adoptedCount && !statsQueue.isEmpty(); removed++) {
|
|
3581
|
+
statsQueue.remove(statsQueue.size() - 1);
|
|
3582
|
+
}
|
|
3583
|
+
for (int i = 0; i < displaced.size(); i++) {
|
|
3584
|
+
statsQueue.add(i, displaced.get(i));
|
|
3585
|
+
}
|
|
3586
|
+
}
|
|
3587
|
+
}
|
|
3588
|
+
|
|
3452
3589
|
int pendingStatsCount() {
|
|
3453
3590
|
return statsQueue.size();
|
|
3454
3591
|
}
|
|
@@ -3475,6 +3612,11 @@ public class CapgoUpdater {
|
|
|
3475
3612
|
return;
|
|
3476
3613
|
}
|
|
3477
3614
|
synchronized (pendingStatsPersistLock) {
|
|
3615
|
+
synchronized (statsOwnerLock) {
|
|
3616
|
+
if (statsOwner != null && statsOwner != this) {
|
|
3617
|
+
return;
|
|
3618
|
+
}
|
|
3619
|
+
}
|
|
3478
3620
|
if (statsStopped.get() && !force) {
|
|
3479
3621
|
return;
|
|
3480
3622
|
}
|
|
@@ -3923,6 +4065,14 @@ public class CapgoUpdater {
|
|
|
3923
4065
|
* Should be called when the plugin is destroyed to prevent resource leaks.
|
|
3924
4066
|
*/
|
|
3925
4067
|
public void shutdown() {
|
|
4068
|
+
shutdown(true);
|
|
4069
|
+
}
|
|
4070
|
+
|
|
4071
|
+
/**
|
|
4072
|
+
* @param persistStats false when another live instance owns the stats file (headless worker
|
|
4073
|
+
* handing over to the plugin): writing this instance's queue would replace the newer file.
|
|
4074
|
+
*/
|
|
4075
|
+
public void shutdown(final boolean persistStats) {
|
|
3926
4076
|
statsStopped.set(true);
|
|
3927
4077
|
// Cancel the scheduled task
|
|
3928
4078
|
if (statsFlushTask != null) {
|
|
@@ -3931,7 +4081,9 @@ public class CapgoUpdater {
|
|
|
3931
4081
|
}
|
|
3932
4082
|
|
|
3933
4083
|
// Write once, then ignore later callbacks so they cannot delete a newer instance's file.
|
|
3934
|
-
|
|
4084
|
+
if (persistStats) {
|
|
4085
|
+
persistStatsQueue(true);
|
|
4086
|
+
}
|
|
3935
4087
|
|
|
3936
4088
|
// Shutdown the scheduler
|
|
3937
4089
|
statsScheduler.shutdown();
|
|
@@ -110,7 +110,7 @@ public class DelayUpdateUtils {
|
|
|
110
110
|
DelayUntilNext kind = condition.getKind();
|
|
111
111
|
String value = condition.getValue();
|
|
112
112
|
switch (kind) {
|
|
113
|
-
case
|
|
113
|
+
case background:
|
|
114
114
|
if (source == CancelDelaySource.FOREGROUND) {
|
|
115
115
|
long backgroundedAt = getBackgroundTimestamp();
|
|
116
116
|
long now = System.currentTimeMillis();
|
|
@@ -159,7 +159,7 @@ public class DelayUpdateUtils {
|
|
|
159
159
|
false,
|
|
160
160
|
"Background delay (value: " + value + ") condition kept at index " + index + " (source: " + source + ")"
|
|
161
161
|
);
|
|
162
|
-
case
|
|
162
|
+
case kill:
|
|
163
163
|
if (source == CancelDelaySource.KILLED) {
|
|
164
164
|
return new ConditionCheckResult(
|
|
165
165
|
true,
|
|
@@ -174,7 +174,7 @@ public class DelayUpdateUtils {
|
|
|
174
174
|
false,
|
|
175
175
|
"Kill delay (value: " + value + ") condition kept at index " + index + " (source: " + source + ")"
|
|
176
176
|
);
|
|
177
|
-
case
|
|
177
|
+
case date:
|
|
178
178
|
if (!"".equals(value)) {
|
|
179
179
|
Date date = parseDateCondition(value);
|
|
180
180
|
if (date != null) {
|
|
@@ -206,7 +206,7 @@ public class DelayUpdateUtils {
|
|
|
206
206
|
false,
|
|
207
207
|
"Date delay (value: " + value + ") condition removed due to empty value at index " + index
|
|
208
208
|
);
|
|
209
|
-
case
|
|
209
|
+
case nativeVersion:
|
|
210
210
|
if (!"".equals(value)) {
|
|
211
211
|
try {
|
|
212
212
|
final Version versionLimit = new Version(value);
|
|
@@ -132,7 +132,7 @@ public class HeadlessUpdateWorker extends Worker {
|
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
};
|
|
135
|
-
configure(updater, context,
|
|
135
|
+
configure(updater, context, config, prefs, packageInfo, versionCode);
|
|
136
136
|
try {
|
|
137
137
|
String updateUrl = config.getString("updateUrl", CapacitorUpdaterPlugin.updateUrlDefault);
|
|
138
138
|
if (config.getBoolean("persistModifyUrl", false) && prefs.contains(CapacitorUpdaterPlugin.UPDATE_URL_PREF_KEY)) {
|
|
@@ -205,7 +205,14 @@ public class HeadlessUpdateWorker extends Worker {
|
|
|
205
205
|
}
|
|
206
206
|
return result.get();
|
|
207
207
|
} finally {
|
|
208
|
-
|
|
208
|
+
// Once the plugin instance is up it owns the stats file: hand it our unsent events
|
|
209
|
+
// instead of overwriting its newer file.
|
|
210
|
+
if (CapacitorUpdaterPlugin.instanceLoaded) {
|
|
211
|
+
updater.handOverPendingStats();
|
|
212
|
+
updater.shutdown(false);
|
|
213
|
+
} else {
|
|
214
|
+
updater.shutdown(true);
|
|
215
|
+
}
|
|
209
216
|
}
|
|
210
217
|
}
|
|
211
218
|
|
|
@@ -265,7 +272,6 @@ public class HeadlessUpdateWorker extends Worker {
|
|
|
265
272
|
private static void configure(
|
|
266
273
|
final CapgoUpdater updater,
|
|
267
274
|
final Context context,
|
|
268
|
-
final CapConfig capConfig,
|
|
269
275
|
final PluginConfig config,
|
|
270
276
|
final SharedPreferences prefs,
|
|
271
277
|
final PackageInfo packageInfo,
|
|
@@ -285,7 +291,7 @@ public class HeadlessUpdateWorker extends Worker {
|
|
|
285
291
|
updater.versionOs = Build.VERSION.RELEASE;
|
|
286
292
|
|
|
287
293
|
String appId = InternalUtils.getPackageName(context.getPackageManager(), context.getPackageName());
|
|
288
|
-
appId =
|
|
294
|
+
appId = InternalUtils.getRootConfigString(context, "appId", appId);
|
|
289
295
|
updater.appId = config.getString("appId", appId);
|
|
290
296
|
updater.setPublicKey(config.getString("publicKey", ""));
|
|
291
297
|
|
|
@@ -1,13 +1,23 @@
|
|
|
1
1
|
package ee.forgr.capacitor_updater;
|
|
2
2
|
|
|
3
|
+
import android.content.Context;
|
|
3
4
|
import android.content.pm.PackageInfo;
|
|
4
5
|
import android.content.pm.PackageManager;
|
|
5
6
|
import android.os.Build;
|
|
6
7
|
import com.getcapacitor.JSObject;
|
|
8
|
+
import com.getcapacitor.util.JSONUtils;
|
|
9
|
+
import java.io.BufferedReader;
|
|
10
|
+
import java.io.IOException;
|
|
11
|
+
import java.io.InputStreamReader;
|
|
12
|
+
import java.nio.charset.StandardCharsets;
|
|
7
13
|
import java.util.Map;
|
|
14
|
+
import org.json.JSONException;
|
|
15
|
+
import org.json.JSONObject;
|
|
8
16
|
|
|
9
17
|
public class InternalUtils {
|
|
10
18
|
|
|
19
|
+
static final String CAPACITOR_CONFIG_ASSET = "capacitor.config.json";
|
|
20
|
+
|
|
11
21
|
/**
|
|
12
22
|
* Converts a Map to JSObject for proper bridge serialization.
|
|
13
23
|
*/
|
|
@@ -29,6 +39,30 @@ public class InternalUtils {
|
|
|
29
39
|
}
|
|
30
40
|
}
|
|
31
41
|
|
|
42
|
+
static String readAssetAsString(final Context context, final String assetPath) throws IOException {
|
|
43
|
+
final StringBuilder buffer = new StringBuilder();
|
|
44
|
+
try (
|
|
45
|
+
final BufferedReader reader = new BufferedReader(
|
|
46
|
+
new InputStreamReader(context.getAssets().open(assetPath), StandardCharsets.UTF_8)
|
|
47
|
+
)
|
|
48
|
+
) {
|
|
49
|
+
String line;
|
|
50
|
+
while ((line = reader.readLine()) != null) {
|
|
51
|
+
buffer.append(line).append('\n');
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return buffer.toString();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Replaces CapConfig.getString for root keys: Capacitor 9 removed it. */
|
|
58
|
+
public static String getRootConfigString(Context context, String key, String defaultValue) {
|
|
59
|
+
try {
|
|
60
|
+
return JSONUtils.getString(new JSONObject(readAssetAsString(context, CAPACITOR_CONFIG_ASSET)), key, defaultValue);
|
|
61
|
+
} catch (IOException | JSONException e) {
|
|
62
|
+
return defaultValue;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
32
66
|
private static PackageInfo getPackageInfoInternal(PackageManager pm, String packageName) throws PackageManager.NameNotFoundException {
|
|
33
67
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
34
68
|
return pm.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0));
|
|
@@ -95,7 +95,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
95
95
|
deinit {
|
|
96
96
|
implementation.shutdown()
|
|
97
97
|
}
|
|
98
|
-
private let pluginVersion: String = "8.53.
|
|
98
|
+
private let pluginVersion: String = "8.53.1"
|
|
99
99
|
private let launchStartedAtMs = Int64(Date().timeIntervalSince1970 * 1000)
|
|
100
100
|
static let updateUrlDefault = "https://plugin.capgo.app/updates"
|
|
101
101
|
static let statsUrlDefault = "https://plugin.capgo.app/stats"
|
|
@@ -4939,10 +4939,11 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
4939
4939
|
}
|
|
4940
4940
|
if self.queueBundleForNextBackgroundInstall(next) {
|
|
4941
4941
|
self.notifyListenersOnMain("updateAvailable", data: ["bundle": next.toJSON()])
|
|
4942
|
+
let installed = self.installNextIfInBackground()
|
|
4942
4943
|
self.endBackGroundTaskWithNotif(
|
|
4943
|
-
msg: "update downloaded, will install next background",
|
|
4944
|
+
msg: installed ? "update downloaded and installed in background" : "update downloaded, will install next background",
|
|
4944
4945
|
latestVersionName: latestVersionName,
|
|
4945
|
-
current:
|
|
4946
|
+
current: self.implementation.getCurrentBundle(),
|
|
4946
4947
|
error: false,
|
|
4947
4948
|
plannedDirectUpdate: plannedDirectUpdate
|
|
4948
4949
|
)
|
|
@@ -4992,29 +4993,85 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
4992
4993
|
}
|
|
4993
4994
|
}
|
|
4994
4995
|
|
|
4995
|
-
|
|
4996
|
-
|
|
4996
|
+
/// A download that finishes while the app is in the background (silent push,
|
|
4997
|
+
/// background fetch, cold background launch) would otherwise wait for the next
|
|
4998
|
+
/// didEnterBackground, which a background launch never posts, so the user would
|
|
4999
|
+
/// open the old bundle. Install now; the notifyAppReady check stays deferred
|
|
5000
|
+
/// until the app reaches the foreground.
|
|
5001
|
+
@discardableResult
|
|
5002
|
+
func installNextIfInBackground() -> Bool {
|
|
5003
|
+
let install = {
|
|
5004
|
+
self.appInBackground && self.shouldInstallQueuedBundleWhileInBackground() && self.installNext()
|
|
5005
|
+
}
|
|
5006
|
+
if Thread.isMainThread {
|
|
5007
|
+
return install()
|
|
5008
|
+
}
|
|
5009
|
+
return DispatchQueue.main.sync(execute: install)
|
|
5010
|
+
}
|
|
5011
|
+
|
|
5012
|
+
/// Immediate background install applies to deferred (`atBackground`) updates only.
|
|
5013
|
+
/// Direct-update modes retry from `appMovedToForeground` when a reload failed earlier.
|
|
5014
|
+
private func shouldInstallQueuedBundleWhileInBackground() -> Bool {
|
|
5015
|
+
autoUpdateMode == Self.autoUpdateModeBackground
|
|
5016
|
+
}
|
|
5017
|
+
|
|
5018
|
+
/// Direct-update modes queue `next` when an immediate reload fails; retry once the app is active again.
|
|
5019
|
+
private func shouldInstallQueuedBundleOnForeground() -> Bool {
|
|
5020
|
+
switch directUpdateMode {
|
|
5021
|
+
case Self.autoUpdateModeAlways, Self.autoUpdateModeLaunch:
|
|
5022
|
+
return true
|
|
5023
|
+
default:
|
|
5024
|
+
return false
|
|
5025
|
+
}
|
|
5026
|
+
}
|
|
5027
|
+
|
|
5028
|
+
private func installQueuedBundleOnForegroundIfNeeded() {
|
|
5029
|
+
guard shouldInstallQueuedBundleOnForeground() else {
|
|
4997
5030
|
return
|
|
4998
5031
|
}
|
|
5032
|
+
let current = self.implementation.getCurrentBundle()
|
|
5033
|
+
guard let next = self.implementation.getNextBundle(),
|
|
5034
|
+
!next.isErrorStatus(),
|
|
5035
|
+
next.getVersionName() != current.getVersionName() else {
|
|
5036
|
+
return
|
|
5037
|
+
}
|
|
5038
|
+
_ = self.installNext()
|
|
5039
|
+
}
|
|
5040
|
+
|
|
5041
|
+
/// Returns true when the next bundle became the active WebView bundle.
|
|
5042
|
+
@discardableResult
|
|
5043
|
+
private func installNext() -> Bool {
|
|
5044
|
+
if self.shouldBlockAutoUpdateForPreviewSession() {
|
|
5045
|
+
return false
|
|
5046
|
+
}
|
|
4999
5047
|
let delayUpdatePreferences = UserDefaults.standard.string(forKey: DelayUpdateUtils.DELAY_CONDITION_PREFERENCES) ?? "[]"
|
|
5000
5048
|
let delayConditionList = DelayUpdateUtils.parseDelayConditions(json: delayUpdatePreferences)
|
|
5001
5049
|
if !delayConditionList.isEmpty {
|
|
5002
5050
|
logger.info("Update delayed until delay conditions met")
|
|
5003
|
-
return
|
|
5051
|
+
return false
|
|
5004
5052
|
}
|
|
5005
5053
|
let current: BundleInfo = self.implementation.getCurrentBundle()
|
|
5006
5054
|
let next: BundleInfo? = self.implementation.getNextBundle()
|
|
5007
5055
|
|
|
5008
5056
|
if next != nil && !next!.isErrorStatus() && next!.getVersionName() != current.getVersionName() {
|
|
5009
5057
|
logger.info("Next bundle is: \(next!.toString())")
|
|
5010
|
-
|
|
5011
|
-
|
|
5012
|
-
self.notifyBundleSet(next!)
|
|
5013
|
-
_ = self.implementation.setNextBundle(next: Optional<String>.none)
|
|
5014
|
-
} else {
|
|
5058
|
+
let resetState = self.implementation.captureResetState()
|
|
5059
|
+
guard self.implementation.set(bundle: next!) else {
|
|
5015
5060
|
logger.error("Update to bundle: \(next!.toString()) Failed!")
|
|
5061
|
+
return false
|
|
5016
5062
|
}
|
|
5063
|
+
guard self._reload() else {
|
|
5064
|
+
// Built-in `set` may reset fallback/next; restore the full pre-install state for retry.
|
|
5065
|
+
self.implementation.restoreResetState(resetState)
|
|
5066
|
+
logger.error("Update to bundle: \(next!.toString()) Failed! Reload failed, keeping it as next")
|
|
5067
|
+
return false
|
|
5068
|
+
}
|
|
5069
|
+
logger.info("Updated to bundle: \(next!.toString())")
|
|
5070
|
+
self.notifyBundleSet(next!)
|
|
5071
|
+
_ = self.implementation.setNextBundle(next: Optional<String>.none)
|
|
5072
|
+
return true
|
|
5017
5073
|
}
|
|
5074
|
+
return false
|
|
5018
5075
|
}
|
|
5019
5076
|
|
|
5020
5077
|
@objc private func toJson(object: Any) -> String {
|
|
@@ -5035,6 +5092,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
|
5035
5092
|
backgroundWork!.cancel()
|
|
5036
5093
|
logger.info("Background Timer Task canceled, Activity resumed before timer completes")
|
|
5037
5094
|
}
|
|
5095
|
+
self.installQueuedBundleOnForegroundIfNeeded()
|
|
5038
5096
|
if self._isAutoUpdateEnabled() {
|
|
5039
5097
|
// Check if download is already in progress (with timeout protection)
|
|
5040
5098
|
if !isDownloadStuckOrTimedOut() {
|
|
@@ -3124,6 +3124,16 @@ import UIKit
|
|
|
3124
3124
|
return true
|
|
3125
3125
|
}
|
|
3126
3126
|
|
|
3127
|
+
/// Points the current bundle back at `bundle` without sending stats, after an
|
|
3128
|
+
/// install whose reload failed left the WebView on it.
|
|
3129
|
+
func restoreCurrentBundle(_ bundle: BundleInfo) {
|
|
3130
|
+
if bundle.isBuiltin() {
|
|
3131
|
+
self.setCurrentBundle(bundle: "")
|
|
3132
|
+
} else {
|
|
3133
|
+
_ = self.stagePendingReload(bundle: bundle)
|
|
3134
|
+
}
|
|
3135
|
+
}
|
|
3136
|
+
|
|
3127
3137
|
func stagePreviewFallbackReload(bundle: BundleInfo) -> Bool {
|
|
3128
3138
|
guard !bundle.isErrorStatus() else {
|
|
3129
3139
|
return false
|