@capgo/capacitor-updater 8.52.1-beta.pr937.1951.1 → 8.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 -2
- package/Package.swift +0 -5
- package/README.md +199 -67
- package/android/build.gradle +9 -88
- package/android/src/main/java/ee/forgr/capacitor_updater/AndroidAppExitReporter.java +60 -32
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java +246 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleStatus.java +45 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Callback.java +13 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +5539 -1066
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +3754 -494
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +496 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DataManager.java +44 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayCondition.java +56 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUntilNext.java +14 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +427 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DeviceIdHelper.java +6 -23
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +1541 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +258 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +318 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +45 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +1 -6
- package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +417 -217
- package/android/src/main/java/ee/forgr/capacitor_updater/ThreeFingerPinchDetector.java +1 -3
- package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +306 -0
- package/dist/docs.json +379 -28
- package/dist/esm/definitions.d.ts +209 -10
- package/dist/esm/definitions.js.map +1 -1
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +262 -0
- package/ios/Sources/CapacitorUpdaterPlugin/AppHealthTracker.swift +82 -0
- package/ios/Sources/CapacitorUpdaterPlugin/BundleInfo.swift +185 -0
- package/ios/Sources/CapacitorUpdaterPlugin/BundleStatus.swift +127 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +5247 -199
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoRawRsa.swift +36 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +3878 -365
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +328 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DelayCondition.swift +74 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DelayUntilNext.swift +30 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +343 -0
- package/ios/Sources/CapacitorUpdaterPlugin/InternalUtils.swift +471 -0
- package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
- package/ios/Sources/CapacitorUpdaterPlugin/RSA.swift +96 -0
- package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +53 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +221 -63
- package/ios/Sources/CapacitorUpdaterPlugin/UserDefaultsExtension.swift +46 -0
- package/ios/Sources/CapacitorUpdaterPlugin/WebViewStatsReporter.swift +147 -5
- package/ios/Sources/CapacitorUpdaterPlugin/ZipArchiveReader.swift +247 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ZipCentralDirectory.swift +280 -0
- package/package.json +15 -19
- package/android/consumer-rules.pro +0 -15
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoCore.java +0 -67
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoCoreNative.java +0 -97
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoDownloadWorker.java +0 -188
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoEngine.java +0 -114
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoEngineHolder.java +0 -59
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoEngineHost.java +0 -118
- package/android/src/main/java/ee/forgr/capacitor_updater/EngineMethodLanes.java +0 -168
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin+AppStore.swift +0 -171
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin+Methods.swift +0 -168
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin+Overlays.swift +0 -285
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin+WebView.swift +0 -134
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoCore.swift +0 -61
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoEngine.swift +0 -319
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
package ee.forgr.capacitor_updater;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.os.Build;
|
|
5
|
+
import androidx.work.BackoffPolicy;
|
|
6
|
+
import androidx.work.Configuration;
|
|
7
|
+
import androidx.work.Constraints;
|
|
8
|
+
import androidx.work.Data;
|
|
9
|
+
import androidx.work.ExistingWorkPolicy;
|
|
10
|
+
import androidx.work.NetworkType;
|
|
11
|
+
import androidx.work.OneTimeWorkRequest;
|
|
12
|
+
import androidx.work.OutOfQuotaPolicy;
|
|
13
|
+
import androidx.work.WorkInfo;
|
|
14
|
+
import androidx.work.WorkManager;
|
|
15
|
+
import androidx.work.WorkRequest;
|
|
16
|
+
import java.util.HashSet;
|
|
17
|
+
import java.util.List;
|
|
18
|
+
import java.util.Set;
|
|
19
|
+
import java.util.concurrent.ExecutorService;
|
|
20
|
+
import java.util.concurrent.Executors;
|
|
21
|
+
import java.util.concurrent.Future;
|
|
22
|
+
import java.util.concurrent.TimeUnit;
|
|
23
|
+
import java.util.concurrent.TimeoutException;
|
|
24
|
+
|
|
25
|
+
public class DownloadWorkerManager {
|
|
26
|
+
|
|
27
|
+
private static Logger logger;
|
|
28
|
+
|
|
29
|
+
public static void setLogger(Logger loggerInstance) {
|
|
30
|
+
logger = loggerInstance;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
private static volatile boolean isInitialized = false;
|
|
34
|
+
private static final ExecutorService cancelExecutor = Executors.newSingleThreadExecutor();
|
|
35
|
+
|
|
36
|
+
private static synchronized void initializeIfNeeded(Context context) {
|
|
37
|
+
if (!isInitialized) {
|
|
38
|
+
try {
|
|
39
|
+
Configuration config = new Configuration.Builder().setMinimumLoggingLevel(android.util.Log.INFO).build();
|
|
40
|
+
WorkManager.initialize(context, config);
|
|
41
|
+
isInitialized = true;
|
|
42
|
+
} catch (IllegalStateException e) {
|
|
43
|
+
// WorkManager was already initialized, ignore
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public static boolean isVersionDownloading(Context context, String version) {
|
|
49
|
+
initializeIfNeeded(context.getApplicationContext());
|
|
50
|
+
try {
|
|
51
|
+
return WorkManager.getInstance(context)
|
|
52
|
+
.getWorkInfosByTag(version)
|
|
53
|
+
.get()
|
|
54
|
+
.stream()
|
|
55
|
+
.anyMatch((workInfo) -> !workInfo.getState().isFinished());
|
|
56
|
+
} catch (Exception e) {
|
|
57
|
+
logger.error("Error checking download status: " + e.getMessage());
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public static void enqueueDownload(
|
|
63
|
+
Context context,
|
|
64
|
+
String url,
|
|
65
|
+
String id,
|
|
66
|
+
String documentsDir,
|
|
67
|
+
String dest,
|
|
68
|
+
String version,
|
|
69
|
+
String sessionKey,
|
|
70
|
+
String checksum,
|
|
71
|
+
String publicKey,
|
|
72
|
+
boolean isManifest,
|
|
73
|
+
boolean isEmulator,
|
|
74
|
+
String appId,
|
|
75
|
+
String pluginVersion,
|
|
76
|
+
boolean isProd,
|
|
77
|
+
String installSource,
|
|
78
|
+
String statsUrl,
|
|
79
|
+
String deviceId,
|
|
80
|
+
String versionBuild,
|
|
81
|
+
String versionCode,
|
|
82
|
+
String versionOs,
|
|
83
|
+
String customId,
|
|
84
|
+
String defaultChannel
|
|
85
|
+
) {
|
|
86
|
+
initializeIfNeeded(context.getApplicationContext());
|
|
87
|
+
|
|
88
|
+
// Use unique work name for this bundle to prevent duplicates
|
|
89
|
+
String uniqueWorkName = "bundle_" + id + "_" + version;
|
|
90
|
+
|
|
91
|
+
// Create input data
|
|
92
|
+
Data inputData = new Data.Builder()
|
|
93
|
+
.putString(DownloadService.URL, url)
|
|
94
|
+
.putString(DownloadService.ID, id)
|
|
95
|
+
.putString(DownloadService.DOCDIR, documentsDir)
|
|
96
|
+
.putString(DownloadService.FILEDEST, dest)
|
|
97
|
+
.putString(DownloadService.VERSION, version)
|
|
98
|
+
.putString(DownloadService.SESSIONKEY, sessionKey)
|
|
99
|
+
.putString(DownloadService.CHECKSUM, checksum)
|
|
100
|
+
.putBoolean(DownloadService.IS_MANIFEST, isManifest)
|
|
101
|
+
.putString(DownloadService.PUBLIC_KEY, publicKey)
|
|
102
|
+
.putString(DownloadService.APP_ID, appId)
|
|
103
|
+
.putString(DownloadService.pluginVersion, pluginVersion)
|
|
104
|
+
.putString(DownloadService.INSTALL_SOURCE, installSource)
|
|
105
|
+
.putString(DownloadService.STATS_URL, statsUrl)
|
|
106
|
+
.putString(DownloadService.DEVICE_ID, deviceId)
|
|
107
|
+
.putString(DownloadService.VERSION_BUILD, versionBuild)
|
|
108
|
+
.putString(DownloadService.VERSION_CODE, versionCode)
|
|
109
|
+
.putString(DownloadService.VERSION_OS, versionOs)
|
|
110
|
+
.putString(DownloadService.CUSTOM_ID, customId)
|
|
111
|
+
.putString(DownloadService.DEFAULT_CHANNEL, defaultChannel)
|
|
112
|
+
.putBoolean(DownloadService.IS_PROD, isProd)
|
|
113
|
+
.putBoolean(DownloadService.IS_EMULATOR, isEmulator)
|
|
114
|
+
.build();
|
|
115
|
+
|
|
116
|
+
// Create network constraints - be more lenient on emulators
|
|
117
|
+
Constraints.Builder constraintsBuilder = new Constraints.Builder();
|
|
118
|
+
if (isEmulator) {
|
|
119
|
+
logger.info("Emulator detected - using lenient network constraints");
|
|
120
|
+
// On emulators, use NOT_REQUIRED to avoid background network issues
|
|
121
|
+
constraintsBuilder.setRequiredNetworkType(NetworkType.NOT_REQUIRED);
|
|
122
|
+
} else {
|
|
123
|
+
constraintsBuilder.setRequiredNetworkType(NetworkType.CONNECTED);
|
|
124
|
+
}
|
|
125
|
+
Constraints constraints = constraintsBuilder.build();
|
|
126
|
+
|
|
127
|
+
// Create work request with tags for tracking
|
|
128
|
+
OneTimeWorkRequest.Builder workRequestBuilder = new OneTimeWorkRequest.Builder(DownloadService.class)
|
|
129
|
+
.setConstraints(constraints)
|
|
130
|
+
.setInputData(inputData)
|
|
131
|
+
.addTag(id)
|
|
132
|
+
.addTag(version)
|
|
133
|
+
.addTag("capacitor_updater_download");
|
|
134
|
+
// Android 12+ expedited jobs skip the WorkManager delay without a
|
|
135
|
+
// foreground service. Older APIs require getForegroundInfo().
|
|
136
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
137
|
+
workRequestBuilder.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// More aggressive retry policy for emulators
|
|
141
|
+
if (isEmulator) {
|
|
142
|
+
workRequestBuilder.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, 30, TimeUnit.SECONDS);
|
|
143
|
+
} else {
|
|
144
|
+
workRequestBuilder.setBackoffCriteria(BackoffPolicy.LINEAR, WorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
OneTimeWorkRequest workRequest = workRequestBuilder.build();
|
|
148
|
+
|
|
149
|
+
// Use beginUniqueWork to prevent duplicate downloads
|
|
150
|
+
WorkManager.getInstance(context)
|
|
151
|
+
.beginUniqueWork(
|
|
152
|
+
uniqueWorkName,
|
|
153
|
+
ExistingWorkPolicy.KEEP, // Don't start if already running
|
|
154
|
+
workRequest
|
|
155
|
+
)
|
|
156
|
+
.enqueue();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
public static void cancelVersionDownload(Context context, String version) {
|
|
160
|
+
initializeIfNeeded(context.getApplicationContext());
|
|
161
|
+
cancelExecutor.execute(() -> cancelVersionDownloadInternal(context, version, false));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
public static boolean cancelVersionDownloadAndAwait(Context context, String version) {
|
|
165
|
+
initializeIfNeeded(context.getApplicationContext());
|
|
166
|
+
Future<?> future = cancelExecutor.submit(() -> cancelVersionDownloadInternal(context, version, true));
|
|
167
|
+
try {
|
|
168
|
+
future.get(10, TimeUnit.SECONDS);
|
|
169
|
+
return true;
|
|
170
|
+
} catch (TimeoutException e) {
|
|
171
|
+
future.cancel(true);
|
|
172
|
+
logger.error("Timed out awaiting version download cancel");
|
|
173
|
+
return false;
|
|
174
|
+
} catch (Exception e) {
|
|
175
|
+
logger.error("Error awaiting version download cancel: " + e.getMessage());
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private static Set<String> collectManifestIdsForVersion(WorkManager workManager, String version) {
|
|
181
|
+
Set<String> downloadIds = new HashSet<>();
|
|
182
|
+
try {
|
|
183
|
+
List<WorkInfo> workInfos = workManager.getWorkInfosByTag(version).get();
|
|
184
|
+
for (WorkInfo workInfo : workInfos) {
|
|
185
|
+
for (String tag : workInfo.getTags()) {
|
|
186
|
+
if (!"capacitor_updater_download".equals(tag) && !version.equals(tag)) {
|
|
187
|
+
downloadIds.add(tag);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
} catch (InterruptedException e) {
|
|
192
|
+
Thread.currentThread().interrupt();
|
|
193
|
+
throw new IllegalStateException("Interrupted while collecting manifest ids before version cancel", e);
|
|
194
|
+
} catch (Exception e) {
|
|
195
|
+
logger.error("Error collecting manifest ids before version cancel: " + e.getMessage());
|
|
196
|
+
}
|
|
197
|
+
return downloadIds;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private static void clearManifestIds(Set<String> downloadIds) {
|
|
201
|
+
for (String downloadId : downloadIds) {
|
|
202
|
+
DataManager.getInstance().clearManifest(downloadId);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private static void cancelVersionDownloadInternal(Context context, String version, boolean awaitFinished) {
|
|
207
|
+
if (Thread.currentThread().isInterrupted()) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
WorkManager workManager = WorkManager.getInstance(context);
|
|
211
|
+
Set<String> downloadIds = collectManifestIdsForVersion(workManager, version);
|
|
212
|
+
workManager.cancelAllWorkByTag(version);
|
|
213
|
+
clearManifestIds(downloadIds);
|
|
214
|
+
if (awaitFinished) {
|
|
215
|
+
awaitVersionWorkFinished(workManager, version);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private static void awaitVersionWorkFinished(WorkManager workManager, String version) {
|
|
220
|
+
for (int i = 0; i < 100; i++) {
|
|
221
|
+
if (Thread.currentThread().isInterrupted()) {
|
|
222
|
+
throw new IllegalStateException("Interrupted while waiting for version download cancel");
|
|
223
|
+
}
|
|
224
|
+
try {
|
|
225
|
+
boolean anyActive = workManager
|
|
226
|
+
.getWorkInfosByTag(version)
|
|
227
|
+
.get()
|
|
228
|
+
.stream()
|
|
229
|
+
.anyMatch((workInfo) -> !workInfo.getState().isFinished());
|
|
230
|
+
if (!anyActive) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
Thread.sleep(100);
|
|
234
|
+
} catch (InterruptedException e) {
|
|
235
|
+
Thread.currentThread().interrupt();
|
|
236
|
+
throw new IllegalStateException("Interrupted while waiting for download cancel", e);
|
|
237
|
+
} catch (Exception e) {
|
|
238
|
+
throw new IllegalStateException("Error waiting for download cancel: " + e.getMessage(), e);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
throw new IllegalStateException("Timed out waiting for version download cancel: " + version);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
public static void cancelBundleDownload(Context context, String id, String version) {
|
|
245
|
+
String uniqueWorkName = "bundle_" + id + "_" + version;
|
|
246
|
+
initializeIfNeeded(context.getApplicationContext());
|
|
247
|
+
WorkManager workManager = WorkManager.getInstance(context);
|
|
248
|
+
workManager.cancelUniqueWork(uniqueWorkName);
|
|
249
|
+
DataManager.getInstance().clearManifest(id);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
public static void cancelAllDownloads(Context context) {
|
|
253
|
+
initializeIfNeeded(context.getApplicationContext());
|
|
254
|
+
WorkManager workManager = WorkManager.getInstance(context);
|
|
255
|
+
workManager.cancelAllWorkByTag("capacitor_updater_download");
|
|
256
|
+
DataManager.getInstance().clearAllManifests();
|
|
257
|
+
}
|
|
258
|
+
}
|
|
@@ -0,0 +1,318 @@
|
|
|
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
|
+
|
|
7
|
+
package ee.forgr.capacitor_updater;
|
|
8
|
+
|
|
9
|
+
import android.app.Activity;
|
|
10
|
+
import android.content.Context;
|
|
11
|
+
import android.content.SharedPreferences;
|
|
12
|
+
import android.content.pm.PackageInfo;
|
|
13
|
+
import android.content.pm.PackageManager;
|
|
14
|
+
import android.os.Build;
|
|
15
|
+
import androidx.annotation.NonNull;
|
|
16
|
+
import androidx.core.content.pm.PackageInfoCompat;
|
|
17
|
+
import androidx.work.Constraints;
|
|
18
|
+
import androidx.work.ExistingWorkPolicy;
|
|
19
|
+
import androidx.work.NetworkType;
|
|
20
|
+
import androidx.work.OneTimeWorkRequest;
|
|
21
|
+
import androidx.work.OutOfQuotaPolicy;
|
|
22
|
+
import androidx.work.WorkManager;
|
|
23
|
+
import androidx.work.Worker;
|
|
24
|
+
import androidx.work.WorkerParameters;
|
|
25
|
+
import com.getcapacitor.CapConfig;
|
|
26
|
+
import com.getcapacitor.PluginConfig;
|
|
27
|
+
import com.getcapacitor.plugin.WebView;
|
|
28
|
+
import java.util.Map;
|
|
29
|
+
import java.util.concurrent.CountDownLatch;
|
|
30
|
+
import java.util.concurrent.TimeUnit;
|
|
31
|
+
import java.util.concurrent.atomic.AtomicReference;
|
|
32
|
+
import org.json.JSONArray;
|
|
33
|
+
import org.json.JSONException;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Runs a live update check with no Activity and no Capacitor bridge, for example when an
|
|
37
|
+
* update-check push wakes a killed app. The bundle is downloaded and, when the app has no UI,
|
|
38
|
+
* applied as the current bundle so the next launch opens the new version (it still has to call
|
|
39
|
+
* notifyAppReady). Enqueued as unique expedited work so the process stays alive until the bundle
|
|
40
|
+
* is unzipped and stored.
|
|
41
|
+
*
|
|
42
|
+
* When a plugin instance is running in the process, use its native trigger instead
|
|
43
|
+
* (CapacitorUpdaterPlugin#triggerBackgroundUpdateCheck); this worker then only stores the bundle as
|
|
44
|
+
* next and lets that instance install it.
|
|
45
|
+
*/
|
|
46
|
+
public class HeadlessUpdateWorker extends Worker {
|
|
47
|
+
|
|
48
|
+
static final String UNIQUE_WORK_NAME = "capacitor_updater_headless_update_check";
|
|
49
|
+
private static final long FINISH_TIMEOUT_MINUTES = 9;
|
|
50
|
+
|
|
51
|
+
public HeadlessUpdateWorker(@NonNull final Context context, @NonNull final WorkerParameters params) {
|
|
52
|
+
super(context, params);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Queue a headless update check. Safe to call from any thread, including an FCM service. */
|
|
56
|
+
public static void enqueue(@NonNull final Context context) {
|
|
57
|
+
final OneTimeWorkRequest.Builder builder = new OneTimeWorkRequest.Builder(HeadlessUpdateWorker.class)
|
|
58
|
+
.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build())
|
|
59
|
+
.addTag(UNIQUE_WORK_NAME);
|
|
60
|
+
// Same as downloads: expedited work starts right away on Android 12+ without a foreground service.
|
|
61
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
62
|
+
builder.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST);
|
|
63
|
+
}
|
|
64
|
+
WorkManager.getInstance(context.getApplicationContext()).enqueueUniqueWork(
|
|
65
|
+
UNIQUE_WORK_NAME,
|
|
66
|
+
ExistingWorkPolicy.KEEP,
|
|
67
|
+
builder.build()
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
@NonNull
|
|
72
|
+
@Override
|
|
73
|
+
public Result doWork() {
|
|
74
|
+
final Logger logger = new Logger("CapgoUpdater");
|
|
75
|
+
try {
|
|
76
|
+
final String status = run(getApplicationContext(), logger);
|
|
77
|
+
logger.info("Headless update check finished: " + status);
|
|
78
|
+
} catch (final Exception e) {
|
|
79
|
+
logger.error("Headless update check failed: " + e.getMessage());
|
|
80
|
+
}
|
|
81
|
+
// Never retry: the next launch or push runs a fresh check.
|
|
82
|
+
return Result.success();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static String run(final Context context, final Logger logger) throws PackageManager.NameNotFoundException {
|
|
86
|
+
if (CapacitorUpdaterPlugin.instanceLoaded) {
|
|
87
|
+
// The running plugin instance owns downloads and installs in this process.
|
|
88
|
+
return "handed_over";
|
|
89
|
+
}
|
|
90
|
+
final CapConfig capConfig = CapConfig.loadDefault(context);
|
|
91
|
+
final PluginConfig config = capConfig.getPluginConfiguration("CapacitorUpdater");
|
|
92
|
+
final String serverUrl = capConfig.getServerUrl();
|
|
93
|
+
if (serverUrl != null && !serverUrl.isEmpty()) {
|
|
94
|
+
return "unavailable";
|
|
95
|
+
}
|
|
96
|
+
final String autoUpdateMode = CapacitorUpdaterPlugin.autoUpdateModeFromConfig(config, logger);
|
|
97
|
+
if (!CapacitorUpdaterPlugin.isAutoUpdateModeEnabled(autoUpdateMode)) {
|
|
98
|
+
return "unavailable";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
final SharedPreferences prefs = context.getSharedPreferences(WebView.WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE);
|
|
102
|
+
final boolean allowPreview = config.getBoolean("allowPreview", false);
|
|
103
|
+
if (allowPreview && prefs.getBoolean(CapacitorUpdaterPlugin.PREVIEW_SESSION_PREF_KEY, false)) {
|
|
104
|
+
return "preview_session";
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
final PackageInfo packageInfo = getPackageInfo(context);
|
|
108
|
+
final String versionCode = Long.toString(PackageInfoCompat.getLongVersionCode(packageInfo));
|
|
109
|
+
// After a native update the launch resets bundles first (resetWhenUpdate): leave it to the launch.
|
|
110
|
+
if (nativeBuildChanged(prefs, versionCode)) {
|
|
111
|
+
return "native_update_pending";
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
final boolean setNext = CapacitorUpdaterPlugin.shouldAutoUpdateModeSetNextBundle(autoUpdateMode);
|
|
115
|
+
final CountDownLatch finished = new CountDownLatch(1);
|
|
116
|
+
final AtomicReference<String> result = new AtomicReference<>("no_update");
|
|
117
|
+
final CapgoUpdater updater = new CapgoUpdater(logger) {
|
|
118
|
+
@Override
|
|
119
|
+
void nextBundleReady(final BundleInfo bundle) {
|
|
120
|
+
result.set(applyIfNoUi(this, bundle, logger) ? "installed" : "queued");
|
|
121
|
+
finished.countDown();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
@Override
|
|
125
|
+
void notifyListeners(final String id, final Map<String, Object> res) {
|
|
126
|
+
if ("downloadFailed".equals(id)) {
|
|
127
|
+
result.set("failed");
|
|
128
|
+
finished.countDown();
|
|
129
|
+
} else if ("updateAvailable".equals(id) && !setNext) {
|
|
130
|
+
result.set("downloaded");
|
|
131
|
+
finished.countDown();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
configure(updater, context, capConfig, config, prefs, packageInfo, versionCode);
|
|
136
|
+
try {
|
|
137
|
+
String updateUrl = config.getString("updateUrl", CapacitorUpdaterPlugin.updateUrlDefault);
|
|
138
|
+
if (config.getBoolean("persistModifyUrl", false) && prefs.contains(CapacitorUpdaterPlugin.UPDATE_URL_PREF_KEY)) {
|
|
139
|
+
updateUrl = prefs.getString(CapacitorUpdaterPlugin.UPDATE_URL_PREF_KEY, updateUrl);
|
|
140
|
+
}
|
|
141
|
+
if (updateUrl == null || updateUrl.isEmpty()) {
|
|
142
|
+
return "unavailable";
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
final AtomicReference<Map<String, Object>> latestRef = new AtomicReference<>();
|
|
146
|
+
final CountDownLatch latestDone = new CountDownLatch(1);
|
|
147
|
+
updater.getLatest(updateUrl, null, (res) -> {
|
|
148
|
+
latestRef.set(res);
|
|
149
|
+
latestDone.countDown();
|
|
150
|
+
});
|
|
151
|
+
try {
|
|
152
|
+
if (!latestDone.await(updater.timeout + 5000L, TimeUnit.MILLISECONDS)) {
|
|
153
|
+
return "failed";
|
|
154
|
+
}
|
|
155
|
+
} catch (final InterruptedException e) {
|
|
156
|
+
Thread.currentThread().interrupt();
|
|
157
|
+
return "failed";
|
|
158
|
+
}
|
|
159
|
+
final Map<String, Object> latest = latestRef.get();
|
|
160
|
+
if (latest == null || latest.containsKey("error") || latest.containsKey("kind")) {
|
|
161
|
+
return "no_update";
|
|
162
|
+
}
|
|
163
|
+
final String version = String.valueOf(latest.get("version"));
|
|
164
|
+
final Object url = latest.get("url");
|
|
165
|
+
final BundleInfo current = updater.getCurrentBundle();
|
|
166
|
+
if ("builtin".equals(version) || version.isEmpty() || version.equals(current.getVersionName()) || !(url instanceof String)) {
|
|
167
|
+
return "no_update";
|
|
168
|
+
}
|
|
169
|
+
final String sessionKey = latest.get("sessionKey") instanceof String ? (String) latest.get("sessionKey") : "";
|
|
170
|
+
final String checksum = latest.get("checksum") instanceof String ? (String) latest.get("checksum") : "";
|
|
171
|
+
|
|
172
|
+
final BundleInfo existing = updater.getBundleInfoByName(version);
|
|
173
|
+
if (existing != null && existing.isErrorStatus()) {
|
|
174
|
+
return "failed";
|
|
175
|
+
}
|
|
176
|
+
if (existing != null && existing.isDownloaded() && BundleStatus.DOWNLOADING != existing.getStatus()) {
|
|
177
|
+
if (setNext && updater.setNextBundle(existing.getId())) {
|
|
178
|
+
return applyIfNoUi(updater, existing, logger) ? "installed" : "queued";
|
|
179
|
+
}
|
|
180
|
+
return "downloaded";
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// downloadBackground returns null when it refuses the download (blocked encryption, gate,
|
|
184
|
+
// version already downloading): only wait for a download it started.
|
|
185
|
+
final String startedId = updater.downloadBackground((String) url, version, sessionKey, checksum, manifestOf(latest), setNext);
|
|
186
|
+
if (startedId == null) {
|
|
187
|
+
return "skipped";
|
|
188
|
+
}
|
|
189
|
+
final long deadline = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(FINISH_TIMEOUT_MINUTES);
|
|
190
|
+
try {
|
|
191
|
+
// Keep the worker (and so the process) alive until the bundle is stored. If the user opens
|
|
192
|
+
// the app meanwhile, its plugin instance takes over the download (it restarts downloads it
|
|
193
|
+
// does not observe), so stop waiting.
|
|
194
|
+
while (!finished.await(1, TimeUnit.SECONDS)) {
|
|
195
|
+
if (CapacitorUpdaterPlugin.instanceLoaded) {
|
|
196
|
+
return "handed_over";
|
|
197
|
+
}
|
|
198
|
+
if (System.currentTimeMillis() > deadline) {
|
|
199
|
+
return "timeout";
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
} catch (final InterruptedException e) {
|
|
203
|
+
Thread.currentThread().interrupt();
|
|
204
|
+
return "interrupted";
|
|
205
|
+
}
|
|
206
|
+
return result.get();
|
|
207
|
+
} finally {
|
|
208
|
+
updater.shutdown();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* With no plugin instance (no bridge, no WebView) nothing shows the current bundle, so switching
|
|
214
|
+
* it is the same as installing on a background transition. Delay conditions keep it as next.
|
|
215
|
+
*/
|
|
216
|
+
static boolean applyIfNoUi(final CapgoUpdater updater, final BundleInfo bundle, final Logger logger) {
|
|
217
|
+
if (CapacitorUpdaterPlugin.instanceLoaded) {
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
final String delays = updater.prefs.getString(DelayUpdateUtils.DELAY_CONDITION_PREFERENCES, "[]");
|
|
221
|
+
if (delays != null && !"[]".equals(delays.trim())) {
|
|
222
|
+
logger.info("Update delayed until delay conditions met");
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
if (updater.set(bundle)) {
|
|
226
|
+
updater.setNextBundle(null);
|
|
227
|
+
logger.info("Installed bundle " + bundle.getVersionName() + " for the next launch");
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
static JSONArray manifestOf(final Map<String, Object> latest) {
|
|
234
|
+
final Object manifest = latest.get("manifest");
|
|
235
|
+
if (manifest instanceof JSONArray) {
|
|
236
|
+
return (JSONArray) manifest;
|
|
237
|
+
}
|
|
238
|
+
if (manifest instanceof String) {
|
|
239
|
+
try {
|
|
240
|
+
return new JSONArray((String) manifest);
|
|
241
|
+
} catch (final JSONException e) {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
static boolean nativeBuildChanged(final SharedPreferences prefs, final String versionCode) {
|
|
249
|
+
String previous = prefs.getString("LatestNativeBuildVersion", "");
|
|
250
|
+
if (previous == null || previous.isEmpty()) {
|
|
251
|
+
previous = prefs.getString("LatestVersionNative", "");
|
|
252
|
+
}
|
|
253
|
+
return previous != null && !previous.isEmpty() && !previous.equals(versionCode);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
private static PackageInfo getPackageInfo(final Context context) throws PackageManager.NameNotFoundException {
|
|
257
|
+
final PackageManager packageManager = context.getPackageManager();
|
|
258
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
259
|
+
return packageManager.getPackageInfo(context.getPackageName(), PackageManager.PackageInfoFlags.of(0));
|
|
260
|
+
}
|
|
261
|
+
return packageManager.getPackageInfo(context.getPackageName(), 0);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Mirrors the identity and URL setup of CapacitorUpdaterPlugin#load. */
|
|
265
|
+
private static void configure(
|
|
266
|
+
final CapgoUpdater updater,
|
|
267
|
+
final Context context,
|
|
268
|
+
final CapConfig capConfig,
|
|
269
|
+
final PluginConfig config,
|
|
270
|
+
final SharedPreferences prefs,
|
|
271
|
+
final PackageInfo packageInfo,
|
|
272
|
+
final String versionCode
|
|
273
|
+
) {
|
|
274
|
+
updater.appContext = context;
|
|
275
|
+
updater.prefs = prefs;
|
|
276
|
+
updater.editor = prefs.edit();
|
|
277
|
+
updater.documentsDir = context.getFilesDir();
|
|
278
|
+
updater.noBackupDir = context.getNoBackupFilesDir();
|
|
279
|
+
// Shares the plugin's pending-stats file: load it first so a flush does not overwrite it.
|
|
280
|
+
updater.restorePendingStats();
|
|
281
|
+
updater.CAP_SERVER_PATH = WebView.CAP_SERVER_PATH;
|
|
282
|
+
updater.pluginVersion = CapacitorUpdaterPlugin.PLUGIN_VERSION;
|
|
283
|
+
updater.versionBuild = config.getString("version", packageInfo.versionName);
|
|
284
|
+
updater.versionCode = versionCode;
|
|
285
|
+
updater.versionOs = Build.VERSION.RELEASE;
|
|
286
|
+
|
|
287
|
+
String appId = InternalUtils.getPackageName(context.getPackageManager(), context.getPackageName());
|
|
288
|
+
appId = capConfig.getString("appId", appId);
|
|
289
|
+
updater.appId = config.getString("appId", appId);
|
|
290
|
+
updater.setPublicKey(config.getString("publicKey", ""));
|
|
291
|
+
|
|
292
|
+
final boolean persistModifyUrl = config.getBoolean("persistModifyUrl", false);
|
|
293
|
+
updater.statsUrl = config.getString("statsUrl", CapacitorUpdaterPlugin.statsUrlDefault);
|
|
294
|
+
updater.channelUrl = config.getString("channelUrl", CapacitorUpdaterPlugin.channelUrlDefault);
|
|
295
|
+
if (persistModifyUrl) {
|
|
296
|
+
updater.statsUrl = prefs.getString(CapacitorUpdaterPlugin.STATS_URL_PREF_KEY, updater.statsUrl);
|
|
297
|
+
updater.channelUrl = prefs.getString(CapacitorUpdaterPlugin.CHANNEL_URL_PREF_KEY, updater.channelUrl);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
final String storedDefaultChannel = prefs.getString(CapacitorUpdaterPlugin.DEFAULT_CHANNEL_PREF_KEY, "");
|
|
301
|
+
updater.defaultChannel =
|
|
302
|
+
storedDefaultChannel != null && !storedDefaultChannel.isEmpty() ? storedDefaultChannel : config.getString("defaultChannel", "");
|
|
303
|
+
|
|
304
|
+
if (config.getBoolean("persistCustomId", false)) {
|
|
305
|
+
final String storedCustomId = prefs.getString(CapacitorUpdaterPlugin.CUSTOM_ID_PREF_KEY, "");
|
|
306
|
+
if (storedCustomId != null && !storedCustomId.isEmpty()) {
|
|
307
|
+
updater.customId = storedCustomId;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
updater.deviceID = DeviceIdHelper.getOrCreateDeviceId(context, prefs);
|
|
311
|
+
|
|
312
|
+
final int responseTimeoutSeconds = config.getInt("responseTimeout", 20);
|
|
313
|
+
final long responseTimeoutMillis = responseTimeoutSeconds > 0 ? (long) responseTimeoutSeconds * 1000L : 20_000L;
|
|
314
|
+
updater.timeout = (int) Math.min(Integer.MAX_VALUE, responseTimeoutMillis);
|
|
315
|
+
DownloadService.applyHttpTimeouts(updater.timeout);
|
|
316
|
+
DownloadService.updateUserAgent(updater.appId, updater.pluginVersion, updater.versionOs);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
package ee.forgr.capacitor_updater;
|
|
2
|
+
|
|
3
|
+
import android.content.pm.PackageInfo;
|
|
4
|
+
import android.content.pm.PackageManager;
|
|
5
|
+
import android.os.Build;
|
|
6
|
+
import com.getcapacitor.JSObject;
|
|
7
|
+
import java.util.Map;
|
|
8
|
+
|
|
9
|
+
public class InternalUtils {
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Converts a Map to JSObject for proper bridge serialization.
|
|
13
|
+
*/
|
|
14
|
+
public static JSObject mapToJSObject(Map<String, Object> map) {
|
|
15
|
+
JSObject jsObject = new JSObject();
|
|
16
|
+
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
17
|
+
jsObject.put(entry.getKey(), entry.getValue());
|
|
18
|
+
}
|
|
19
|
+
return jsObject;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public static String getPackageName(PackageManager pm, String packageName) {
|
|
23
|
+
try {
|
|
24
|
+
PackageInfo pInfo = getPackageInfoInternal(pm, packageName);
|
|
25
|
+
return pInfo != null ? pInfo.packageName : null;
|
|
26
|
+
} catch (PackageManager.NameNotFoundException e) {
|
|
27
|
+
// Exception is handled internally, and null is returned to indicate the package name could not be retrieved
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
private static PackageInfo getPackageInfoInternal(PackageManager pm, String packageName) throws PackageManager.NameNotFoundException {
|
|
33
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
34
|
+
return pm.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0));
|
|
35
|
+
} else {
|
|
36
|
+
return getPackageInfoLegacy(pm, packageName, (int) (long) 0);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@SuppressWarnings("deprecation")
|
|
41
|
+
private static PackageInfo getPackageInfoLegacy(PackageManager pm, String packageName, int flags)
|
|
42
|
+
throws PackageManager.NameNotFoundException {
|
|
43
|
+
return pm.getPackageInfo(packageName, flags);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -89,10 +89,6 @@ public class Logger {
|
|
|
89
89
|
private final String kDefaultTimerLabel = "default";
|
|
90
90
|
private boolean useSystemLog;
|
|
91
91
|
|
|
92
|
-
boolean usesSystemLog() {
|
|
93
|
-
return this.useSystemLog;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
92
|
public void setBridge(Bridge bridge) {
|
|
97
93
|
this.bridge = bridge;
|
|
98
94
|
}
|
|
@@ -230,8 +226,7 @@ public class Logger {
|
|
|
230
226
|
// If the label is ASCII, surround it with []
|
|
231
227
|
String format;
|
|
232
228
|
|
|
233
|
-
|
|
234
|
-
if (label != null && !label.isEmpty()) {
|
|
229
|
+
if (label != null) {
|
|
235
230
|
format = label.charAt(0) <= 127 ? "[%s]: %s" : "%s %s";
|
|
236
231
|
} else {
|
|
237
232
|
label = "";
|