@capgo/capacitor-updater 9.0.0 → 9.0.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/CapgoCapacitorUpdater.podspec +3 -6
- package/Package.swift +30 -0
- package/README.md +2382 -292
- package/android/build.gradle +37 -15
- package/android/consumer-rules.pro +48 -0
- package/android/proguard-rules.pro +23 -0
- package/android/src/main/AndroidManifest.xml +0 -1
- package/android/src/main/java/ee/forgr/capacitor_updater/AndroidAppExitReporter.java +92 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/AppLifecycleObserver.java +115 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java +227 -195
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleStatus.java +24 -23
- package/android/src/main/java/ee/forgr/capacitor_updater/Callback.java +13 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +7435 -1243
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +5144 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +564 -121
- package/android/src/main/java/ee/forgr/capacitor_updater/DataManager.java +44 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayCondition.java +43 -49
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUntilNext.java +4 -4
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +427 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DeviceIdHelper.java +230 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +1969 -118
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +278 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +385 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Http3.java +356 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/HttpEngineTransport.java +553 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +79 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +419 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeDetector.java +72 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +959 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/StatsBatchFailureDiagnostics.java +40 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ThreeFingerPinchDetector.java +323 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +306 -0
- package/dist/docs.json +4431 -840
- package/dist/esm/definitions.d.ts +2607 -225
- package/dist/esm/definitions.js +103 -1
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/history.d.ts +1 -0
- package/dist/esm/history.js +283 -0
- package/dist/esm/history.js.map +1 -0
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.js +5 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +53 -42
- package/dist/esm/web.js +180 -37
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +570 -37
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +570 -37
- 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 +5642 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoRawRsa.swift +36 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoSemanticVersion.swift +162 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +4572 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +380 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +343 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DeviceIdHelper.swift +120 -0
- package/ios/Sources/CapacitorUpdaterPlugin/InternalUtils.swift +452 -0
- package/ios/Sources/CapacitorUpdaterPlugin/Logger.swift +380 -0
- package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
- package/ios/Sources/CapacitorUpdaterPlugin/RSA.swift +96 -0
- package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +97 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +871 -0
- package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/UserDefaultsExtension.swift +0 -2
- package/ios/Sources/CapacitorUpdaterPlugin/WKWebView+CapgoObjcBridge.swift +20 -0
- package/ios/Sources/CapacitorUpdaterPlugin/WebViewStatsReporter.swift +304 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ZipArchiveReader.swift +247 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ZipCentralDirectory.swift +280 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/BuiltinHashesTests.swift +210 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/BundleHardeningTests.swift +292 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CapacitorUpdaterPluginTests.swift +3905 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CapgoSemanticVersionTests.swift +64 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ChecksumRequiredTests.swift +151 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CliCryptoFixtureTests.swift +249 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/HttpsOnlyBuiltinMinimumTests.swift +528 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/InstallPerfTests.swift +174 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ListChannelsTests.swift +74 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/LoggerSecurityTests.swift +149 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/NativeContractTests.swift +160 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/NetworkTransportTests.swift +1095 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/PopulateDeltaCacheTests.swift +573 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/RedirectPolicyTests.swift +63 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/RsaContractTests.swift +281 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/SessionKeyRequiredTests.swift +192 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/TestZipWriter.swift +235 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/UpdateLifecycleTests.swift +454 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/WebKitOverlayGuardTests.swift +32 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ZipArchiveReaderTests.swift +302 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ZipBundleExtractionTests.swift +122 -0
- package/package.json +80 -43
- package/scripts/builtin-hashes-lib.mjs +148 -0
- package/scripts/capacitor-builtin-hashes-hook.mjs +65 -0
- package/scripts/resolve-real-path.mjs +25 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java +0 -1130
- package/ios/Plugin/BundleInfo.swift +0 -113
- package/ios/Plugin/BundleStatus.swift +0 -48
- package/ios/Plugin/CapacitorUpdater.swift +0 -858
- package/ios/Plugin/CapacitorUpdaterPlugin.h +0 -10
- package/ios/Plugin/CapacitorUpdaterPlugin.m +0 -27
- package/ios/Plugin/CapacitorUpdaterPlugin.swift +0 -677
- package/ios/Plugin/CryptoCipher.swift +0 -240
- /package/{LICENCE → LICENSE} +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayCondition.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayUntilNext.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/Info.plist +0 -0
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
package ee.forgr.capacitor_updater;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import android.app.AlertDialog;
|
|
5
|
+
import android.content.Intent;
|
|
6
|
+
import android.content.SharedPreferences;
|
|
7
|
+
import android.net.Uri;
|
|
8
|
+
import android.os.Handler;
|
|
9
|
+
import android.os.Looper;
|
|
10
|
+
import com.getcapacitor.JSObject;
|
|
11
|
+
import com.getcapacitor.PluginConfig;
|
|
12
|
+
import com.google.android.play.core.appupdate.AppUpdateInfo;
|
|
13
|
+
import com.google.android.play.core.appupdate.AppUpdateManager;
|
|
14
|
+
import com.google.android.play.core.appupdate.AppUpdateOptions;
|
|
15
|
+
import com.google.android.play.core.install.model.AppUpdateType;
|
|
16
|
+
import com.google.android.play.core.install.model.UpdateAvailability;
|
|
17
|
+
import java.util.HashSet;
|
|
18
|
+
import java.util.Locale;
|
|
19
|
+
import java.util.Set;
|
|
20
|
+
|
|
21
|
+
final class NativeUpdatePrompt {
|
|
22
|
+
|
|
23
|
+
static final String PREF_LAST_NATIVE_VERSION = "CapacitorUpdater.nativeUpdatePromptLastNativeVersion";
|
|
24
|
+
static final String PREF_LAST_SHOWN_MS = "CapacitorUpdater.nativeUpdatePromptLastShownMs";
|
|
25
|
+
|
|
26
|
+
private static final Set<String> SESSION_SHOWN_NATIVE_VERSIONS = new HashSet<>();
|
|
27
|
+
|
|
28
|
+
static void resetSessionStateForTesting() {
|
|
29
|
+
SESSION_SHOWN_NATIVE_VERSIONS.clear();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
void resetPresentationStateForTesting() {
|
|
33
|
+
this.promptPresentationActive = false;
|
|
34
|
+
if (this.pendingShowRunnable != null) {
|
|
35
|
+
this.mainHandler.removeCallbacks(this.pendingShowRunnable);
|
|
36
|
+
this.pendingShowRunnable = null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
boolean isPromptPresentationActiveForTesting() {
|
|
41
|
+
return this.promptPresentationActive;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
private final boolean enabled;
|
|
45
|
+
private final boolean onStoreCheck;
|
|
46
|
+
private final String title;
|
|
47
|
+
private final String message;
|
|
48
|
+
private final String updateButton;
|
|
49
|
+
private final String laterButton;
|
|
50
|
+
private final String countryFilter;
|
|
51
|
+
private final long cooldownMs;
|
|
52
|
+
private final SharedPreferences prefs;
|
|
53
|
+
private final Logger logger;
|
|
54
|
+
private final NativeUpdatePromptHost host;
|
|
55
|
+
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
56
|
+
private volatile boolean promptPresentationActive = false;
|
|
57
|
+
private Runnable pendingShowRunnable;
|
|
58
|
+
|
|
59
|
+
interface NativeUpdatePromptHost {
|
|
60
|
+
Activity getActivity();
|
|
61
|
+
|
|
62
|
+
String getNativeBuildVersion();
|
|
63
|
+
|
|
64
|
+
AppUpdateManager getAppUpdateManager();
|
|
65
|
+
|
|
66
|
+
AppUpdateInfo getCachedAppUpdateInfo();
|
|
67
|
+
|
|
68
|
+
void setCachedAppUpdateInfo(AppUpdateInfo info);
|
|
69
|
+
|
|
70
|
+
String getPackageNameForStore();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
NativeUpdatePrompt(final PluginConfig config, final SharedPreferences prefs, final Logger logger, final NativeUpdatePromptHost host) {
|
|
74
|
+
this.prefs = prefs;
|
|
75
|
+
this.logger = logger;
|
|
76
|
+
this.host = host;
|
|
77
|
+
this.enabled = config.getBoolean("nativeUpdatePrompt", false);
|
|
78
|
+
this.onStoreCheck = config.getBoolean("nativeUpdatePromptOnStoreCheck", false);
|
|
79
|
+
this.title = config.getString("nativeUpdatePromptTitle", null);
|
|
80
|
+
this.message = config.getString("nativeUpdatePromptMessage", null);
|
|
81
|
+
this.updateButton = config.getString("nativeUpdatePromptUpdateButton", null);
|
|
82
|
+
this.laterButton = config.getString("nativeUpdatePromptLaterButton", null);
|
|
83
|
+
this.countryFilter = config.getString("nativeUpdatePromptCountry", null);
|
|
84
|
+
final int cooldownHours = Math.max(1, config.getInt("nativeUpdatePromptCooldownHours", 24));
|
|
85
|
+
this.cooldownMs = (long) cooldownHours * 60L * 60L * 1000L;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
boolean isOnStoreCheckEnabled() {
|
|
89
|
+
return this.enabled && this.onStoreCheck;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static boolean isNativeStoreUpdateBlock(final JSObject response) {
|
|
93
|
+
if (response == null) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
if (response.optBoolean("breaking", false)) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
if ("store_update_required".equals(response.optString("message", ""))) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
final String error = response.optString("error", "");
|
|
103
|
+
return error.startsWith("disable_auto_update");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
void maybeShowForUpdateBlock(final JSObject response) {
|
|
107
|
+
if (!this.enabled || !isNativeStoreUpdateBlock(response)) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
this.scheduleShow();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
void maybeShowForStoreUpdateAvailable() {
|
|
114
|
+
if (!this.isOnStoreCheckEnabled()) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
this.scheduleShow();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private void scheduleShow() {
|
|
121
|
+
if (!this.matchesCountryFilter()) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
final String nativeVersion = this.host.getNativeBuildVersion();
|
|
125
|
+
if (nativeVersion == null || nativeVersion.isEmpty()) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (!this.shouldShowForNativeVersion(nativeVersion)) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (this.promptPresentationActive) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
this.promptPresentationActive = true;
|
|
135
|
+
if (this.pendingShowRunnable != null) {
|
|
136
|
+
this.mainHandler.removeCallbacks(this.pendingShowRunnable);
|
|
137
|
+
}
|
|
138
|
+
this.pendingShowRunnable = () -> this.showDialog(nativeVersion);
|
|
139
|
+
this.mainHandler.postDelayed(this.pendingShowRunnable, 600);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private void clearPresentationGuard() {
|
|
143
|
+
this.promptPresentationActive = false;
|
|
144
|
+
this.pendingShowRunnable = null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
boolean shouldShowForNativeVersion(final String nativeVersion) {
|
|
148
|
+
if (SESSION_SHOWN_NATIVE_VERSIONS.contains(nativeVersion)) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
final String lastVersion = this.prefs.getString(PREF_LAST_NATIVE_VERSION, "");
|
|
152
|
+
final long lastShown = this.prefs.getLong(PREF_LAST_SHOWN_MS, 0L);
|
|
153
|
+
if (nativeVersion.equals(lastVersion) && lastShown > 0L) {
|
|
154
|
+
final long elapsed = System.currentTimeMillis() - lastShown;
|
|
155
|
+
if (elapsed < this.cooldownMs) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
boolean matchesCountryFilter() {
|
|
163
|
+
if (this.countryFilter == null || this.countryFilter.trim().isEmpty()) {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
final String deviceCountry = Locale.getDefault().getCountry();
|
|
167
|
+
if (deviceCountry == null || deviceCountry.isEmpty()) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
final String normalizedDevice = deviceCountry.toUpperCase(Locale.ROOT);
|
|
171
|
+
final String[] allowed = this.countryFilter.split(",");
|
|
172
|
+
for (final String entry : allowed) {
|
|
173
|
+
if (entry != null && normalizedDevice.equals(entry.trim().toUpperCase(Locale.ROOT))) {
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private void showDialog(final String nativeVersion) {
|
|
181
|
+
final Activity activity = this.host.getActivity();
|
|
182
|
+
if (activity == null || activity.isFinishing()) {
|
|
183
|
+
this.clearPresentationGuard();
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
final String dialogTitle = this.title != null && !this.title.isEmpty() ? this.title : "Update available";
|
|
188
|
+
final String dialogMessage =
|
|
189
|
+
this.message != null && !this.message.isEmpty()
|
|
190
|
+
? this.message
|
|
191
|
+
: "A new version of this app is available in the store. Update now to continue receiving live updates.";
|
|
192
|
+
final String positive = this.updateButton != null && !this.updateButton.isEmpty() ? this.updateButton : "Update";
|
|
193
|
+
final String negative = this.laterButton != null && !this.laterButton.isEmpty() ? this.laterButton : "Not now";
|
|
194
|
+
|
|
195
|
+
try {
|
|
196
|
+
final AlertDialog dialog = new AlertDialog.Builder(activity)
|
|
197
|
+
.setTitle(dialogTitle)
|
|
198
|
+
.setMessage(dialogMessage)
|
|
199
|
+
.setCancelable(true)
|
|
200
|
+
.setPositiveButton(positive, (d, which) -> this.onUpdateSelected())
|
|
201
|
+
.setNegativeButton(negative, (d, which) -> d.dismiss())
|
|
202
|
+
.create();
|
|
203
|
+
final boolean[] recorded = { false };
|
|
204
|
+
final Runnable markRecorded = () -> {
|
|
205
|
+
if (!recorded[0]) {
|
|
206
|
+
recorded[0] = true;
|
|
207
|
+
this.recordShown(nativeVersion);
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
dialog.setOnShowListener((d) -> markRecorded.run());
|
|
211
|
+
dialog.setOnCancelListener((d) -> markRecorded.run());
|
|
212
|
+
dialog.setOnDismissListener((d) -> this.clearPresentationGuard());
|
|
213
|
+
dialog.show();
|
|
214
|
+
} catch (final Exception e) {
|
|
215
|
+
this.logger.warn("Could not show native update prompt: " + e.getMessage());
|
|
216
|
+
this.clearPresentationGuard();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
void recordShown(final String nativeVersion) {
|
|
221
|
+
SESSION_SHOWN_NATIVE_VERSIONS.add(nativeVersion);
|
|
222
|
+
this.prefs
|
|
223
|
+
.edit()
|
|
224
|
+
.putString(PREF_LAST_NATIVE_VERSION, nativeVersion)
|
|
225
|
+
.putLong(PREF_LAST_SHOWN_MS, System.currentTimeMillis())
|
|
226
|
+
.apply();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private void onUpdateSelected() {
|
|
230
|
+
final AppUpdateInfo cached = this.host.getCachedAppUpdateInfo();
|
|
231
|
+
if (
|
|
232
|
+
cached != null &&
|
|
233
|
+
cached.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
|
|
234
|
+
cached.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
|
|
235
|
+
) {
|
|
236
|
+
this.startImmediateUpdate(cached);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
final AppUpdateManager manager = this.host.getAppUpdateManager();
|
|
241
|
+
manager
|
|
242
|
+
.getAppUpdateInfo()
|
|
243
|
+
.addOnSuccessListener((appUpdateInfo) -> {
|
|
244
|
+
this.host.setCachedAppUpdateInfo(appUpdateInfo);
|
|
245
|
+
if (
|
|
246
|
+
appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE &&
|
|
247
|
+
appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)
|
|
248
|
+
) {
|
|
249
|
+
this.startImmediateUpdate(appUpdateInfo);
|
|
250
|
+
} else {
|
|
251
|
+
this.openPlayStore();
|
|
252
|
+
}
|
|
253
|
+
})
|
|
254
|
+
.addOnFailureListener((e) -> this.openPlayStore());
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
private void startImmediateUpdate(final AppUpdateInfo appUpdateInfo) {
|
|
258
|
+
final Activity activity = this.host.getActivity();
|
|
259
|
+
if (activity == null || activity.isFinishing()) {
|
|
260
|
+
this.openPlayStore();
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
try {
|
|
264
|
+
final AppUpdateManager manager = this.host.getAppUpdateManager();
|
|
265
|
+
final boolean started = manager.startUpdateFlowForResult(
|
|
266
|
+
appUpdateInfo,
|
|
267
|
+
activity,
|
|
268
|
+
AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build(),
|
|
269
|
+
CapacitorUpdaterPlugin.APP_UPDATE_REQUEST_CODE
|
|
270
|
+
);
|
|
271
|
+
if (!started) {
|
|
272
|
+
this.openPlayStore();
|
|
273
|
+
}
|
|
274
|
+
} catch (final Exception e) {
|
|
275
|
+
this.logger.warn("Immediate update from prompt failed: " + e.getMessage());
|
|
276
|
+
this.openPlayStore();
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
private void openPlayStore() {
|
|
281
|
+
final String packageName = this.host.getPackageNameForStore();
|
|
282
|
+
final Activity activity = this.host.getActivity();
|
|
283
|
+
if (activity == null) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
try {
|
|
287
|
+
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName));
|
|
288
|
+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
289
|
+
activity.startActivity(intent);
|
|
290
|
+
} catch (final android.content.ActivityNotFoundException e) {
|
|
291
|
+
try {
|
|
292
|
+
final Intent intent = new Intent(
|
|
293
|
+
Intent.ACTION_VIEW,
|
|
294
|
+
Uri.parse("https://play.google.com/store/apps/details?id=" + packageName)
|
|
295
|
+
);
|
|
296
|
+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
297
|
+
activity.startActivity(intent);
|
|
298
|
+
} catch (final Exception ex) {
|
|
299
|
+
this.logger.error("Failed to open Play Store from native update prompt: " + ex.getMessage());
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
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.hardware.Sensor;
|
|
10
|
+
import android.hardware.SensorEvent;
|
|
11
|
+
import android.hardware.SensorEventListener;
|
|
12
|
+
import android.hardware.SensorManager;
|
|
13
|
+
|
|
14
|
+
public class ShakeDetector implements SensorEventListener {
|
|
15
|
+
|
|
16
|
+
public interface Listener {
|
|
17
|
+
void onShakeDetected();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private static final float SHAKE_THRESHOLD = 16.0f; // Acceleration threshold for shake detection
|
|
21
|
+
private static final int SHAKE_TIMEOUT = 1000; // Minimum time between shake events (ms)
|
|
22
|
+
|
|
23
|
+
private Listener listener;
|
|
24
|
+
private SensorManager sensorManager;
|
|
25
|
+
private Sensor accelerometer;
|
|
26
|
+
private long lastShakeTime = 0;
|
|
27
|
+
|
|
28
|
+
public ShakeDetector(Listener listener) {
|
|
29
|
+
this.listener = listener;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public void start(SensorManager sensorManager) {
|
|
33
|
+
this.sensorManager = sensorManager;
|
|
34
|
+
this.accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
|
35
|
+
|
|
36
|
+
if (accelerometer != null) {
|
|
37
|
+
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public void stop() {
|
|
42
|
+
if (sensorManager != null) {
|
|
43
|
+
sensorManager.unregisterListener(this);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@Override
|
|
48
|
+
public void onSensorChanged(SensorEvent event) {
|
|
49
|
+
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
|
|
50
|
+
float x = event.values[0];
|
|
51
|
+
float y = event.values[1];
|
|
52
|
+
float z = event.values[2];
|
|
53
|
+
|
|
54
|
+
// Calculate the acceleration magnitude (excluding gravity)
|
|
55
|
+
float acceleration = (float) Math.sqrt(x * x + y * y + z * z) - SensorManager.GRAVITY_EARTH;
|
|
56
|
+
|
|
57
|
+
// Check if acceleration exceeds threshold and enough time has passed
|
|
58
|
+
long currentTime = System.currentTimeMillis();
|
|
59
|
+
if (Math.abs(acceleration) > SHAKE_THRESHOLD && currentTime - lastShakeTime > SHAKE_TIMEOUT) {
|
|
60
|
+
lastShakeTime = currentTime;
|
|
61
|
+
if (listener != null) {
|
|
62
|
+
listener.onShakeDetected();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@Override
|
|
69
|
+
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
|
70
|
+
// Not needed for shake detection
|
|
71
|
+
}
|
|
72
|
+
}
|