@capgo/capacitor-updater 8.52.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/README.md +198 -66
- package/android/build.gradle +2 -1
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +409 -148
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +521 -197
- 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 -105
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +30 -10
- package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +318 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +4 -4
- package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +1 -1
- package/dist/docs.json +378 -27
- package/dist/esm/definitions.d.ts +206 -7
- 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 +104 -52
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +292 -94
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +156 -10
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +2 -1
- package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +202 -90
- package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +5 -2
- package/package.json +7 -3
|
@@ -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,303 @@
|
|
|
1
|
+
package ee.forgr.capacitor_updater;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import android.app.AlertDialog;
|
|
5
|
+
import android.content.Intent;
|
|
6
|
+
import android.content.SharedPreferences;
|
|
7
|
+
import android.net.Uri;
|
|
8
|
+
import android.os.Handler;
|
|
9
|
+
import android.os.Looper;
|
|
10
|
+
import com.getcapacitor.JSObject;
|
|
11
|
+
import com.getcapacitor.PluginConfig;
|
|
12
|
+
import com.google.android.play.core.appupdate.AppUpdateInfo;
|
|
13
|
+
import com.google.android.play.core.appupdate.AppUpdateManager;
|
|
14
|
+
import com.google.android.play.core.appupdate.AppUpdateOptions;
|
|
15
|
+
import com.google.android.play.core.install.model.AppUpdateType;
|
|
16
|
+
import com.google.android.play.core.install.model.UpdateAvailability;
|
|
17
|
+
import java.util.HashSet;
|
|
18
|
+
import java.util.Locale;
|
|
19
|
+
import java.util.Set;
|
|
20
|
+
|
|
21
|
+
final class NativeUpdatePrompt {
|
|
22
|
+
|
|
23
|
+
static final String PREF_LAST_NATIVE_VERSION = "CapacitorUpdater.nativeUpdatePromptLastNativeVersion";
|
|
24
|
+
static final String PREF_LAST_SHOWN_MS = "CapacitorUpdater.nativeUpdatePromptLastShownMs";
|
|
25
|
+
|
|
26
|
+
private static final Set<String> SESSION_SHOWN_NATIVE_VERSIONS = new HashSet<>();
|
|
27
|
+
|
|
28
|
+
static void resetSessionStateForTesting() {
|
|
29
|
+
SESSION_SHOWN_NATIVE_VERSIONS.clear();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
void resetPresentationStateForTesting() {
|
|
33
|
+
this.promptPresentationActive = false;
|
|
34
|
+
if (this.pendingShowRunnable != null) {
|
|
35
|
+
this.mainHandler.removeCallbacks(this.pendingShowRunnable);
|
|
36
|
+
this.pendingShowRunnable = null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
boolean isPromptPresentationActiveForTesting() {
|
|
41
|
+
return this.promptPresentationActive;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private final boolean enabled;
|
|
45
|
+
private final boolean onStoreCheck;
|
|
46
|
+
private final String title;
|
|
47
|
+
private final String message;
|
|
48
|
+
private final String updateButton;
|
|
49
|
+
private final String laterButton;
|
|
50
|
+
private final String countryFilter;
|
|
51
|
+
private final long cooldownMs;
|
|
52
|
+
private final SharedPreferences prefs;
|
|
53
|
+
private final Logger logger;
|
|
54
|
+
private final NativeUpdatePromptHost host;
|
|
55
|
+
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
56
|
+
private volatile boolean promptPresentationActive = false;
|
|
57
|
+
private Runnable pendingShowRunnable;
|
|
58
|
+
|
|
59
|
+
interface NativeUpdatePromptHost {
|
|
60
|
+
Activity getActivity();
|
|
61
|
+
|
|
62
|
+
String getNativeBuildVersion();
|
|
63
|
+
|
|
64
|
+
AppUpdateManager getAppUpdateManager();
|
|
65
|
+
|
|
66
|
+
AppUpdateInfo getCachedAppUpdateInfo();
|
|
67
|
+
|
|
68
|
+
void setCachedAppUpdateInfo(AppUpdateInfo info);
|
|
69
|
+
|
|
70
|
+
String getPackageNameForStore();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
NativeUpdatePrompt(final PluginConfig config, final SharedPreferences prefs, final Logger logger, final NativeUpdatePromptHost host) {
|
|
74
|
+
this.prefs = prefs;
|
|
75
|
+
this.logger = logger;
|
|
76
|
+
this.host = host;
|
|
77
|
+
this.enabled = config.getBoolean("nativeUpdatePrompt", false);
|
|
78
|
+
this.onStoreCheck = config.getBoolean("nativeUpdatePromptOnStoreCheck", false);
|
|
79
|
+
this.title = config.getString("nativeUpdatePromptTitle", null);
|
|
80
|
+
this.message = config.getString("nativeUpdatePromptMessage", null);
|
|
81
|
+
this.updateButton = config.getString("nativeUpdatePromptUpdateButton", null);
|
|
82
|
+
this.laterButton = config.getString("nativeUpdatePromptLaterButton", null);
|
|
83
|
+
this.countryFilter = config.getString("nativeUpdatePromptCountry", null);
|
|
84
|
+
final int cooldownHours = Math.max(1, config.getInt("nativeUpdatePromptCooldownHours", 24));
|
|
85
|
+
this.cooldownMs = (long) cooldownHours * 60L * 60L * 1000L;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
boolean isOnStoreCheckEnabled() {
|
|
89
|
+
return this.enabled && this.onStoreCheck;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static boolean isNativeStoreUpdateBlock(final JSObject response) {
|
|
93
|
+
if (response == null) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
if (response.optBoolean("breaking", false)) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
if ("store_update_required".equals(response.optString("message", ""))) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
final String error = response.optString("error", "");
|
|
103
|
+
return error.startsWith("disable_auto_update");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
void maybeShowForUpdateBlock(final JSObject response) {
|
|
107
|
+
if (!this.enabled || !isNativeStoreUpdateBlock(response)) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
this.scheduleShow();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
void maybeShowForStoreUpdateAvailable() {
|
|
114
|
+
if (!this.isOnStoreCheckEnabled()) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
this.scheduleShow();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private void scheduleShow() {
|
|
121
|
+
if (!this.matchesCountryFilter()) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
final String nativeVersion = this.host.getNativeBuildVersion();
|
|
125
|
+
if (nativeVersion == null || nativeVersion.isEmpty()) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (!this.shouldShowForNativeVersion(nativeVersion)) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (this.promptPresentationActive) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
this.promptPresentationActive = true;
|
|
135
|
+
if (this.pendingShowRunnable != null) {
|
|
136
|
+
this.mainHandler.removeCallbacks(this.pendingShowRunnable);
|
|
137
|
+
}
|
|
138
|
+
this.pendingShowRunnable = () -> this.showDialog(nativeVersion);
|
|
139
|
+
this.mainHandler.postDelayed(this.pendingShowRunnable, 600);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private void clearPresentationGuard() {
|
|
143
|
+
this.promptPresentationActive = false;
|
|
144
|
+
this.pendingShowRunnable = null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
boolean shouldShowForNativeVersion(final String nativeVersion) {
|
|
148
|
+
if (SESSION_SHOWN_NATIVE_VERSIONS.contains(nativeVersion)) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
final String lastVersion = this.prefs.getString(PREF_LAST_NATIVE_VERSION, "");
|
|
152
|
+
final long lastShown = this.prefs.getLong(PREF_LAST_SHOWN_MS, 0L);
|
|
153
|
+
if (nativeVersion.equals(lastVersion) && lastShown > 0L) {
|
|
154
|
+
final long elapsed = System.currentTimeMillis() - lastShown;
|
|
155
|
+
if (elapsed < this.cooldownMs) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
boolean matchesCountryFilter() {
|
|
163
|
+
if (this.countryFilter == null || this.countryFilter.trim().isEmpty()) {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
final String deviceCountry = Locale.getDefault().getCountry();
|
|
167
|
+
if (deviceCountry == null || deviceCountry.isEmpty()) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
final String normalizedDevice = deviceCountry.toUpperCase(Locale.ROOT);
|
|
171
|
+
final String[] allowed = this.countryFilter.split(",");
|
|
172
|
+
for (final String entry : allowed) {
|
|
173
|
+
if (entry != null && normalizedDevice.equals(entry.trim().toUpperCase(Locale.ROOT))) {
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private void showDialog(final String nativeVersion) {
|
|
181
|
+
final Activity activity = this.host.getActivity();
|
|
182
|
+
if (activity == null || activity.isFinishing()) {
|
|
183
|
+
this.clearPresentationGuard();
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
final String dialogTitle = this.title != null && !this.title.isEmpty() ? this.title : "Update available";
|
|
188
|
+
final String dialogMessage =
|
|
189
|
+
this.message != null && !this.message.isEmpty()
|
|
190
|
+
? this.message
|
|
191
|
+
: "A new version of this app is available in the store. Update now to continue receiving live updates.";
|
|
192
|
+
final String positive = this.updateButton != null && !this.updateButton.isEmpty() ? this.updateButton : "Update";
|
|
193
|
+
final String negative = this.laterButton != null && !this.laterButton.isEmpty() ? this.laterButton : "Not now";
|
|
194
|
+
|
|
195
|
+
try {
|
|
196
|
+
final AlertDialog dialog = new AlertDialog.Builder(activity)
|
|
197
|
+
.setTitle(dialogTitle)
|
|
198
|
+
.setMessage(dialogMessage)
|
|
199
|
+
.setCancelable(true)
|
|
200
|
+
.setPositiveButton(positive, (d, which) -> this.onUpdateSelected())
|
|
201
|
+
.setNegativeButton(negative, (d, which) -> d.dismiss())
|
|
202
|
+
.create();
|
|
203
|
+
final boolean[] recorded = { false };
|
|
204
|
+
final Runnable markRecorded = () -> {
|
|
205
|
+
if (!recorded[0]) {
|
|
206
|
+
recorded[0] = true;
|
|
207
|
+
this.recordShown(nativeVersion);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
dialog.setOnShowListener((d) -> markRecorded.run());
|
|
211
|
+
dialog.setOnCancelListener((d) -> markRecorded.run());
|
|
212
|
+
dialog.setOnDismissListener((d) -> this.clearPresentationGuard());
|
|
213
|
+
dialog.show();
|
|
214
|
+
} catch (final Exception e) {
|
|
215
|
+
this.logger.warn("Could not show native update prompt: " + e.getMessage());
|
|
216
|
+
this.clearPresentationGuard();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
void recordShown(final String nativeVersion) {
|
|
221
|
+
SESSION_SHOWN_NATIVE_VERSIONS.add(nativeVersion);
|
|
222
|
+
this.prefs
|
|
223
|
+
.edit()
|
|
224
|
+
.putString(PREF_LAST_NATIVE_VERSION, nativeVersion)
|
|
225
|
+
.putLong(PREF_LAST_SHOWN_MS, System.currentTimeMillis())
|
|
226
|
+
.apply();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private void onUpdateSelected() {
|
|
230
|
+
final AppUpdateInfo cached = this.host.getCachedAppUpdateInfo();
|
|
231
|
+
if (
|
|
232
|
+
cached != null &&
|
|
233
|
+
cached.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
|
|
234
|
+
cached.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
|
|
235
|
+
) {
|
|
236
|
+
this.startImmediateUpdate(cached);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
final AppUpdateManager manager = this.host.getAppUpdateManager();
|
|
241
|
+
manager
|
|
242
|
+
.getAppUpdateInfo()
|
|
243
|
+
.addOnSuccessListener((appUpdateInfo) -> {
|
|
244
|
+
this.host.setCachedAppUpdateInfo(appUpdateInfo);
|
|
245
|
+
if (
|
|
246
|
+
appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
|
|
247
|
+
appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
|
|
248
|
+
) {
|
|
249
|
+
this.startImmediateUpdate(appUpdateInfo);
|
|
250
|
+
} else {
|
|
251
|
+
this.openPlayStore();
|
|
252
|
+
}
|
|
253
|
+
})
|
|
254
|
+
.addOnFailureListener((e) -> this.openPlayStore());
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private void startImmediateUpdate(final AppUpdateInfo appUpdateInfo) {
|
|
258
|
+
final Activity activity = this.host.getActivity();
|
|
259
|
+
if (activity == null || activity.isFinishing()) {
|
|
260
|
+
this.openPlayStore();
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
try {
|
|
264
|
+
final AppUpdateManager manager = this.host.getAppUpdateManager();
|
|
265
|
+
final boolean started = manager.startUpdateFlowForResult(
|
|
266
|
+
appUpdateInfo,
|
|
267
|
+
activity,
|
|
268
|
+
AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build(),
|
|
269
|
+
CapacitorUpdaterPlugin.APP_UPDATE_REQUEST_CODE
|
|
270
|
+
);
|
|
271
|
+
if (!started) {
|
|
272
|
+
this.openPlayStore();
|
|
273
|
+
}
|
|
274
|
+
} catch (final Exception e) {
|
|
275
|
+
this.logger.warn("Immediate update from prompt failed: " + e.getMessage());
|
|
276
|
+
this.openPlayStore();
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
private void openPlayStore() {
|
|
281
|
+
final String packageName = this.host.getPackageNameForStore();
|
|
282
|
+
final Activity activity = this.host.getActivity();
|
|
283
|
+
if (activity == null) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName));
|
|
288
|
+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
289
|
+
activity.startActivity(intent);
|
|
290
|
+
} catch (final android.content.ActivityNotFoundException e) {
|
|
291
|
+
try {
|
|
292
|
+
final Intent intent = new Intent(
|
|
293
|
+
Intent.ACTION_VIEW,
|
|
294
|
+
Uri.parse("https://play.google.com/store/apps/details?id=" + packageName)
|
|
295
|
+
);
|
|
296
|
+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
297
|
+
activity.startActivity(intent);
|
|
298
|
+
} catch (final Exception ex) {
|
|
299
|
+
this.logger.error("Failed to open Play Store from native update prompt: " + ex.getMessage());
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
@@ -766,10 +766,10 @@ public class ShakeMenu implements ShakeDetector.Listener, ThreeFingerPinchDetect
|
|
|
766
766
|
latestMessage != null && !latestMessage.isEmpty()
|
|
767
767
|
? latestMessage
|
|
768
768
|
: latestError != null && !latestError.isEmpty()
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
769
|
+
? latestError
|
|
770
|
+
: latestKind != null && !latestKind.isEmpty()
|
|
771
|
+
? latestKind
|
|
772
|
+
: "server did not provide a message";
|
|
773
773
|
|
|
774
774
|
// Handle update errors first (before "no new version" check)
|
|
775
775
|
if (
|
|
@@ -82,7 +82,7 @@ public class Version implements Comparable<Version> {
|
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
}
|
|
85
|
-
suffix =
|
|
85
|
+
suffix = suffixSb != null ? suffixSb.toString() : "";
|
|
86
86
|
trimmedSubversionNumbers.addAll(subversionNumbers);
|
|
87
87
|
while (!trimmedSubversionNumbers.isEmpty() && trimmedSubversionNumbers.get(trimmedSubversionNumbers.size() - 1) == 0L) {
|
|
88
88
|
trimmedSubversionNumbers.remove(trimmedSubversionNumbers.size() - 1);
|