@capgo/capacitor-updater 8.3.0 → 8.4.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.
@@ -9,10 +9,12 @@ package ee.forgr.capacitor_updater;
9
9
  import android.app.Activity;
10
10
  import android.app.ActivityManager;
11
11
  import android.content.Context;
12
+ import android.content.Intent;
12
13
  import android.content.SharedPreferences;
13
14
  import android.content.pm.PackageInfo;
14
15
  import android.content.pm.PackageManager;
15
16
  import android.graphics.Color;
17
+ import android.net.Uri;
16
18
  import android.os.Build;
17
19
  import android.os.Handler;
18
20
  import android.os.Looper;
@@ -30,6 +32,17 @@ import com.getcapacitor.PluginHandle;
30
32
  import com.getcapacitor.PluginMethod;
31
33
  import com.getcapacitor.annotation.CapacitorPlugin;
32
34
  import com.getcapacitor.plugin.WebView;
35
+ import com.google.android.gms.tasks.Task;
36
+ // Play Store In-App Updates
37
+ import com.google.android.play.core.appupdate.AppUpdateInfo;
38
+ import com.google.android.play.core.appupdate.AppUpdateManager;
39
+ import com.google.android.play.core.appupdate.AppUpdateManagerFactory;
40
+ import com.google.android.play.core.appupdate.AppUpdateOptions;
41
+ import com.google.android.play.core.install.InstallState;
42
+ import com.google.android.play.core.install.InstallStateUpdatedListener;
43
+ import com.google.android.play.core.install.model.AppUpdateType;
44
+ import com.google.android.play.core.install.model.InstallStatus;
45
+ import com.google.android.play.core.install.model.UpdateAvailability;
33
46
  import io.github.g00fy2.versioncompare.Version;
34
47
  import java.io.IOException;
35
48
  import java.net.MalformedURLException;
@@ -72,7 +85,7 @@ public class CapacitorUpdaterPlugin extends Plugin {
72
85
  private static final String[] BREAKING_EVENT_NAMES = { "breakingAvailable", "majorAvailable" };
73
86
  private static final String LAST_FAILED_BUNDLE_PREF_KEY = "CapacitorUpdater.lastFailedBundle";
74
87
 
75
- private final String pluginVersion = "8.3.0";
88
+ private final String pluginVersion = "8.4.1";
76
89
  private static final String DELAY_CONDITION_PREFERENCES = "";
77
90
 
78
91
  private SharedPreferences.Editor editor;
@@ -125,6 +138,12 @@ public class CapacitorUpdaterPlugin extends Plugin {
125
138
  private FrameLayout splashscreenLoaderOverlay;
126
139
  private Runnable splashscreenTimeoutRunnable;
127
140
 
141
+ // Play Store In-App Updates
142
+ private AppUpdateManager appUpdateManager;
143
+ private AppUpdateInfo cachedAppUpdateInfo;
144
+ private static final int APP_UPDATE_REQUEST_CODE = 9001;
145
+ private InstallStateUpdatedListener installStateUpdatedListener;
146
+
128
147
  private void notifyBreakingEvents(final String version) {
129
148
  if (version == null || version.isEmpty()) {
130
149
  return;
@@ -2191,27 +2210,6 @@ public class CapacitorUpdaterPlugin extends Plugin {
2191
2210
  }
2192
2211
  }
2193
2212
 
2194
- @Override
2195
- public void handleOnDestroy() {
2196
- try {
2197
- logger.info("onActivityDestroyed " + getActivity().getClass().getName());
2198
- this.implementation.activity = getActivity();
2199
-
2200
- // Clean up shake menu
2201
- if (shakeMenu != null) {
2202
- try {
2203
- shakeMenu.stop();
2204
- shakeMenu = null;
2205
- logger.info("Shake menu cleaned up");
2206
- } catch (Exception e) {
2207
- logger.error("Failed to clean up shake menu: " + e.getMessage());
2208
- }
2209
- }
2210
- } catch (Exception e) {
2211
- logger.error("Failed to run handleOnDestroy: " + e.getMessage());
2212
- }
2213
- }
2214
-
2215
2213
  @PluginMethod
2216
2214
  public void setShakeMenu(final PluginCall call) {
2217
2215
  final Boolean enabled = call.getBoolean("enabled");
@@ -2285,4 +2283,332 @@ public class CapacitorUpdaterPlugin extends Plugin {
2285
2283
  this.implementation.appId = appId;
2286
2284
  call.resolve();
2287
2285
  }
2286
+
2287
+ // ============================================================================
2288
+ // Play Store In-App Update Methods
2289
+ // ============================================================================
2290
+
2291
+ // AppUpdateAvailability enum values matching TypeScript definitions
2292
+ private static final int UPDATE_AVAILABILITY_UNKNOWN = 0;
2293
+ private static final int UPDATE_AVAILABILITY_NOT_AVAILABLE = 1;
2294
+ private static final int UPDATE_AVAILABILITY_AVAILABLE = 2;
2295
+ private static final int UPDATE_AVAILABILITY_IN_PROGRESS = 3;
2296
+
2297
+ // AppUpdateResultCode enum values matching TypeScript definitions
2298
+ private static final int RESULT_OK = 0;
2299
+ private static final int RESULT_CANCELED = 1;
2300
+ private static final int RESULT_FAILED = 2;
2301
+ private static final int RESULT_NOT_AVAILABLE = 3;
2302
+ private static final int RESULT_NOT_ALLOWED = 4;
2303
+ private static final int RESULT_INFO_MISSING = 5;
2304
+
2305
+ private AppUpdateManager getAppUpdateManager() {
2306
+ if (appUpdateManager == null) {
2307
+ appUpdateManager = AppUpdateManagerFactory.create(getContext());
2308
+ }
2309
+ return appUpdateManager;
2310
+ }
2311
+
2312
+ private int mapUpdateAvailability(int playStoreAvailability) {
2313
+ switch (playStoreAvailability) {
2314
+ case UpdateAvailability.UPDATE_AVAILABLE:
2315
+ return UPDATE_AVAILABILITY_AVAILABLE;
2316
+ case UpdateAvailability.UPDATE_NOT_AVAILABLE:
2317
+ return UPDATE_AVAILABILITY_NOT_AVAILABLE;
2318
+ case UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS:
2319
+ return UPDATE_AVAILABILITY_IN_PROGRESS;
2320
+ default:
2321
+ return UPDATE_AVAILABILITY_UNKNOWN;
2322
+ }
2323
+ }
2324
+
2325
+ @PluginMethod
2326
+ public void getAppUpdateInfo(final PluginCall call) {
2327
+ logger.info("Getting Play Store update info");
2328
+
2329
+ try {
2330
+ AppUpdateManager manager = getAppUpdateManager();
2331
+ Task<AppUpdateInfo> appUpdateInfoTask = manager.getAppUpdateInfo();
2332
+
2333
+ appUpdateInfoTask
2334
+ .addOnSuccessListener((appUpdateInfo) -> {
2335
+ cachedAppUpdateInfo = appUpdateInfo;
2336
+
2337
+ JSObject result = new JSObject();
2338
+ try {
2339
+ PackageInfo pInfo = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0);
2340
+ result.put("currentVersionName", pInfo.versionName);
2341
+ result.put("currentVersionCode", String.valueOf(pInfo.versionCode));
2342
+ } catch (PackageManager.NameNotFoundException e) {
2343
+ result.put("currentVersionName", "0.0.0");
2344
+ result.put("currentVersionCode", "0");
2345
+ }
2346
+
2347
+ result.put("updateAvailability", mapUpdateAvailability(appUpdateInfo.updateAvailability()));
2348
+
2349
+ if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
2350
+ result.put("availableVersionCode", String.valueOf(appUpdateInfo.availableVersionCode()));
2351
+ // Play Store doesn't provide version name, only version code
2352
+ result.put("availableVersionName", String.valueOf(appUpdateInfo.availableVersionCode()));
2353
+ result.put("updatePriority", appUpdateInfo.updatePriority());
2354
+ result.put("immediateUpdateAllowed", appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE));
2355
+ result.put("flexibleUpdateAllowed", appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE));
2356
+
2357
+ Integer stalenessDays = appUpdateInfo.clientVersionStalenessDays();
2358
+ if (stalenessDays != null) {
2359
+ result.put("clientVersionStalenessDays", stalenessDays);
2360
+ }
2361
+ } else {
2362
+ result.put("immediateUpdateAllowed", false);
2363
+ result.put("flexibleUpdateAllowed", false);
2364
+ }
2365
+
2366
+ result.put("installStatus", appUpdateInfo.installStatus());
2367
+
2368
+ call.resolve(result);
2369
+ })
2370
+ .addOnFailureListener((e) -> {
2371
+ logger.error("Failed to get app update info: " + e.getMessage());
2372
+ call.reject("Failed to get app update info: " + e.getMessage());
2373
+ });
2374
+ } catch (Exception e) {
2375
+ logger.error("Error getting app update info: " + e.getMessage());
2376
+ call.reject("Error getting app update info: " + e.getMessage());
2377
+ }
2378
+ }
2379
+
2380
+ @PluginMethod
2381
+ public void openAppStore(final PluginCall call) {
2382
+ String packageName = call.getString("packageName");
2383
+ if (packageName == null || packageName.isEmpty()) {
2384
+ packageName = getContext().getPackageName();
2385
+ }
2386
+
2387
+ try {
2388
+ // Try to open Play Store app first
2389
+ Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName));
2390
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2391
+ getContext().startActivity(intent);
2392
+ call.resolve();
2393
+ } catch (android.content.ActivityNotFoundException e) {
2394
+ // Fall back to browser
2395
+ try {
2396
+ Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName));
2397
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2398
+ getContext().startActivity(intent);
2399
+ call.resolve();
2400
+ } catch (Exception ex) {
2401
+ logger.error("Failed to open Play Store: " + ex.getMessage());
2402
+ call.reject("Failed to open Play Store: " + ex.getMessage());
2403
+ }
2404
+ }
2405
+ }
2406
+
2407
+ @PluginMethod
2408
+ public void performImmediateUpdate(final PluginCall call) {
2409
+ if (cachedAppUpdateInfo == null) {
2410
+ logger.error("No update info available. Call getAppUpdateInfo first.");
2411
+ JSObject result = new JSObject();
2412
+ result.put("code", RESULT_INFO_MISSING);
2413
+ call.resolve(result);
2414
+ return;
2415
+ }
2416
+
2417
+ if (cachedAppUpdateInfo.updateAvailability() != UpdateAvailability.UPDATE_AVAILABLE) {
2418
+ logger.info("No update available");
2419
+ JSObject result = new JSObject();
2420
+ result.put("code", RESULT_NOT_AVAILABLE);
2421
+ call.resolve(result);
2422
+ return;
2423
+ }
2424
+
2425
+ if (!cachedAppUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
2426
+ logger.info("Immediate update not allowed");
2427
+ JSObject result = new JSObject();
2428
+ result.put("code", RESULT_NOT_ALLOWED);
2429
+ call.resolve(result);
2430
+ return;
2431
+ }
2432
+
2433
+ try {
2434
+ Activity activity = getActivity();
2435
+ if (activity == null) {
2436
+ call.reject("Activity not available");
2437
+ return;
2438
+ }
2439
+
2440
+ // Save the call for later resolution
2441
+ bridge.saveCall(call);
2442
+
2443
+ AppUpdateManager manager = getAppUpdateManager();
2444
+ manager.startUpdateFlowForResult(
2445
+ cachedAppUpdateInfo,
2446
+ activity,
2447
+ AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build(),
2448
+ APP_UPDATE_REQUEST_CODE
2449
+ );
2450
+ } catch (Exception e) {
2451
+ logger.error("Failed to start immediate update: " + e.getMessage());
2452
+ JSObject result = new JSObject();
2453
+ result.put("code", RESULT_FAILED);
2454
+ call.resolve(result);
2455
+ }
2456
+ }
2457
+
2458
+ @PluginMethod
2459
+ public void startFlexibleUpdate(final PluginCall call) {
2460
+ if (cachedAppUpdateInfo == null) {
2461
+ logger.error("No update info available. Call getAppUpdateInfo first.");
2462
+ JSObject result = new JSObject();
2463
+ result.put("code", RESULT_INFO_MISSING);
2464
+ call.resolve(result);
2465
+ return;
2466
+ }
2467
+
2468
+ if (cachedAppUpdateInfo.updateAvailability() != UpdateAvailability.UPDATE_AVAILABLE) {
2469
+ logger.info("No update available");
2470
+ JSObject result = new JSObject();
2471
+ result.put("code", RESULT_NOT_AVAILABLE);
2472
+ call.resolve(result);
2473
+ return;
2474
+ }
2475
+
2476
+ if (!cachedAppUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
2477
+ logger.info("Flexible update not allowed");
2478
+ JSObject result = new JSObject();
2479
+ result.put("code", RESULT_NOT_ALLOWED);
2480
+ call.resolve(result);
2481
+ return;
2482
+ }
2483
+
2484
+ try {
2485
+ Activity activity = getActivity();
2486
+ if (activity == null) {
2487
+ call.reject("Activity not available");
2488
+ return;
2489
+ }
2490
+
2491
+ // Register listener for flexible update state changes
2492
+ AppUpdateManager manager = getAppUpdateManager();
2493
+
2494
+ // Remove any existing listener
2495
+ if (installStateUpdatedListener != null) {
2496
+ manager.unregisterListener(installStateUpdatedListener);
2497
+ }
2498
+
2499
+ installStateUpdatedListener = (state) -> {
2500
+ JSObject eventData = new JSObject();
2501
+ eventData.put("installStatus", state.installStatus());
2502
+
2503
+ if (state.installStatus() == InstallStatus.DOWNLOADING) {
2504
+ eventData.put("bytesDownloaded", state.bytesDownloaded());
2505
+ eventData.put("totalBytesToDownload", state.totalBytesToDownload());
2506
+ }
2507
+
2508
+ notifyListeners("onFlexibleUpdateStateChange", eventData);
2509
+ };
2510
+
2511
+ manager.registerListener(installStateUpdatedListener);
2512
+
2513
+ // Save the call for later resolution
2514
+ bridge.saveCall(call);
2515
+
2516
+ manager.startUpdateFlowForResult(
2517
+ cachedAppUpdateInfo,
2518
+ activity,
2519
+ AppUpdateOptions.newBuilder(AppUpdateType.FLEXIBLE).build(),
2520
+ APP_UPDATE_REQUEST_CODE
2521
+ );
2522
+ } catch (Exception e) {
2523
+ logger.error("Failed to start flexible update: " + e.getMessage());
2524
+ JSObject result = new JSObject();
2525
+ result.put("code", RESULT_FAILED);
2526
+ call.resolve(result);
2527
+ }
2528
+ }
2529
+
2530
+ @PluginMethod
2531
+ public void completeFlexibleUpdate(final PluginCall call) {
2532
+ try {
2533
+ AppUpdateManager manager = getAppUpdateManager();
2534
+ manager
2535
+ .completeUpdate()
2536
+ .addOnSuccessListener((aVoid) -> {
2537
+ // The app will restart, so this may not be called
2538
+ call.resolve();
2539
+ })
2540
+ .addOnFailureListener((e) -> {
2541
+ logger.error("Failed to complete flexible update: " + e.getMessage());
2542
+ call.reject("Failed to complete flexible update: " + e.getMessage());
2543
+ });
2544
+ } catch (Exception e) {
2545
+ logger.error("Error completing flexible update: " + e.getMessage());
2546
+ call.reject("Error completing flexible update: " + e.getMessage());
2547
+ }
2548
+ }
2549
+
2550
+ @Override
2551
+ protected void handleOnActivityResult(int requestCode, int resultCode, Intent data) {
2552
+ super.handleOnActivityResult(requestCode, resultCode, data);
2553
+
2554
+ if (requestCode == APP_UPDATE_REQUEST_CODE) {
2555
+ PluginCall savedCall = bridge.getSavedCall("com.getcapacitor.PluginCall");
2556
+ if (savedCall == null) {
2557
+ // Try to get any saved call (for backward compatibility)
2558
+ return;
2559
+ }
2560
+
2561
+ JSObject result = new JSObject();
2562
+ if (resultCode == Activity.RESULT_OK) {
2563
+ result.put("code", RESULT_OK);
2564
+ } else if (resultCode == Activity.RESULT_CANCELED) {
2565
+ result.put("code", RESULT_CANCELED);
2566
+ } else {
2567
+ result.put("code", RESULT_FAILED);
2568
+ }
2569
+ savedCall.resolve(result);
2570
+ bridge.releaseCall(savedCall);
2571
+ }
2572
+ }
2573
+
2574
+ @Override
2575
+ protected void handleOnDestroy() {
2576
+ // Clean up the install state listener
2577
+ if (installStateUpdatedListener != null && appUpdateManager != null) {
2578
+ try {
2579
+ appUpdateManager.unregisterListener(installStateUpdatedListener);
2580
+ installStateUpdatedListener = null;
2581
+ } catch (Exception e) {
2582
+ logger.error("Failed to unregister install state listener: " + e.getMessage());
2583
+ }
2584
+ }
2585
+
2586
+ handleOnDestroyInternal();
2587
+ }
2588
+
2589
+ private void handleOnDestroyInternal() {
2590
+ // Original handleOnDestroy code
2591
+ try {
2592
+ logger.info("onActivityDestroyed " + getActivity().getClass().getName());
2593
+ this.implementation.activity = getActivity();
2594
+
2595
+ // Check for 'kill' delay condition on activity destroy
2596
+ // Note: onDestroy is not reliably called - also check on next app launch
2597
+ this.delayUpdateUtils.checkCancelDelay(DelayUpdateUtils.CancelDelaySource.KILLED);
2598
+ this.delayUpdateUtils.setBackgroundTimestamp(0);
2599
+
2600
+ // Clean up shake menu
2601
+ if (shakeMenu != null) {
2602
+ try {
2603
+ shakeMenu.stop();
2604
+ shakeMenu = null;
2605
+ logger.info("Shake menu cleaned up");
2606
+ } catch (Exception e) {
2607
+ logger.error("Failed to clean up shake menu: " + e.getMessage());
2608
+ }
2609
+ }
2610
+ } catch (Exception e) {
2611
+ logger.error("Failed to run handleOnDestroy: " + e.getMessage());
2612
+ }
2613
+ }
2288
2614
  }