@capgo/capacitor-updater 8.51.4 → 8.51.6

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.
@@ -6,10 +6,13 @@
6
6
 
7
7
  package ee.forgr.capacitor_updater;
8
8
 
9
+ import android.content.Context;
9
10
  import androidx.annotation.NonNull;
10
11
  import androidx.lifecycle.DefaultLifecycleObserver;
11
12
  import androidx.lifecycle.LifecycleOwner;
13
+ import androidx.lifecycle.ProcessLifecycleInitializer;
12
14
  import androidx.lifecycle.ProcessLifecycleOwner;
15
+ import androidx.startup.AppInitializer;
13
16
 
14
17
  /**
15
18
  * Observes app-level lifecycle events using ProcessLifecycleOwner.
@@ -41,16 +44,40 @@ public class AppLifecycleObserver implements DefaultLifecycleObserver {
41
44
  this.logger = logger;
42
45
  }
43
46
 
44
- public void register() {
47
+ public boolean isRegistered() {
48
+ return isRegistered;
49
+ }
50
+
51
+ public void register(Context context) {
45
52
  if (isRegistered) {
46
53
  return;
47
54
  }
55
+ if (context == null) {
56
+ logger.error("Cannot register AppLifecycleObserver without context");
57
+ return;
58
+ }
59
+ // get()/addObserver() succeed even when ProcessLifecycleOwner.init() never ran.
60
+ // Initialize first; if that fails, leave isRegistered false so activity fallback runs.
61
+ try {
62
+ AppInitializer.getInstance(context.getApplicationContext()).initializeComponent(ProcessLifecycleInitializer.class);
63
+ } catch (Exception e) {
64
+ logger.error("Failed to initialize ProcessLifecycleOwner: " + e.getMessage());
65
+ return;
66
+ }
67
+ if (!addObserver()) {
68
+ logger.error("Failed to register AppLifecycleObserver with ProcessLifecycleOwner");
69
+ }
70
+ }
71
+
72
+ private boolean addObserver() {
48
73
  try {
49
74
  ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
50
75
  isRegistered = true;
51
76
  logger.info("AppLifecycleObserver registered with ProcessLifecycleOwner");
77
+ return true;
52
78
  } catch (Exception e) {
53
- logger.error("Failed to register AppLifecycleObserver: " + e.getMessage());
79
+ logger.error("Failed to add AppLifecycleObserver: " + e.getMessage());
80
+ return false;
54
81
  }
55
82
  }
56
83
 
@@ -146,7 +146,7 @@ public class CapacitorUpdaterPlugin extends Plugin {
146
146
  static final int APPLICATION_EXIT_REASON_USER_REQUESTED = 10;
147
147
  static final int APPLICATION_EXIT_REASON_DEPENDENCY_DIED = 12;
148
148
 
149
- private final String pluginVersion = "8.51.4";
149
+ private final String pluginVersion = "8.51.6";
150
150
  private static final String DELAY_CONDITION_PREFERENCES = "";
151
151
 
152
152
  private SharedPreferences.Editor editor;
@@ -282,6 +282,10 @@ public class CapacitorUpdaterPlugin extends Plugin {
282
282
  // App lifecycle observer using ProcessLifecycleOwner for reliable foreground/background detection
283
283
  private AppLifecycleObserver appLifecycleObserver;
284
284
 
285
+ private boolean isProcessLifecycleObserverActive() {
286
+ return this.appLifecycleObserver != null && this.appLifecycleObserver.isRegistered();
287
+ }
288
+
285
289
  // Play Store In-App Updates
286
290
  private AppUpdateManager appUpdateManager;
287
291
  private AppUpdateInfo cachedAppUpdateInfo;
@@ -833,11 +837,13 @@ public class CapacitorUpdaterPlugin extends Plugin {
833
837
  this.periodCheckDelay = normalizedPeriodCheckDelayMs(this.getConfig().getInt("periodCheckDelay", 0));
834
838
 
835
839
  this.implementation.documentsDir = this.getContext().getFilesDir();
840
+ this.implementation.noBackupDir = this.getContext().getNoBackupFilesDir();
836
841
  this.implementation.prefs = this.prefs;
837
842
  this.implementation.editor = this.editor;
838
843
  this.implementation.versionOs = Build.VERSION.RELEASE;
839
844
  // Use DeviceIdHelper to get or create device ID that persists across reinstalls
840
845
  this.implementation.deviceID = DeviceIdHelper.getOrCreateDeviceId(this.getContext(), this.prefs);
846
+ this.implementation.restorePendingStats();
841
847
 
842
848
  // Update User-Agent for shared OkHttpClient with OS version
843
849
  DownloadService.updateUserAgent(this.implementation.appId, this.pluginVersion, this.implementation.versionOs);
@@ -945,8 +951,12 @@ public class CapacitorUpdaterPlugin extends Plugin {
945
951
  },
946
952
  logger
947
953
  );
948
- this.appLifecycleObserver.register();
949
- logger.info("Using ProcessLifecycleOwner for foreground/background detection (Android 14+)");
954
+ this.appLifecycleObserver.register(this.getContext());
955
+ if (!this.appLifecycleObserver.isRegistered()) {
956
+ logger.warn("ProcessLifecycleOwner unavailable; using activity lifecycle callbacks");
957
+ } else {
958
+ logger.info("Using ProcessLifecycleOwner for foreground/background detection (Android 14+)");
959
+ }
950
960
  } else {
951
961
  logger.info("Using activity lifecycle callbacks for foreground/background detection (Android <14)");
952
962
  }
@@ -5250,6 +5260,7 @@ public class CapacitorUpdaterPlugin extends Plugin {
5250
5260
 
5251
5261
  // Do other background work after splashscreen is shown
5252
5262
  CapacitorUpdaterPlugin.this.implementation.sendStats("app_moved_to_background", current.getVersionName());
5263
+ CapacitorUpdaterPlugin.this.implementation.persistPendingStats();
5253
5264
  logger.info("Checking for pending update");
5254
5265
 
5255
5266
  try {
@@ -5325,9 +5336,9 @@ public class CapacitorUpdaterPlugin extends Plugin {
5325
5336
 
5326
5337
  // On Android < 14, use activity lifecycle for foreground detection
5327
5338
  // On Android 14+, ProcessLifecycleOwner handles this via AppLifecycleObserver
5328
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
5329
- if (isPreviousMainActivity) {
5330
- logger.info("handleOnStart: appMovedToForeground (Android <14 path)");
5339
+ if (!this.isProcessLifecycleObserverActive()) {
5340
+ if (isPreviousMainActivity || Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
5341
+ logger.info("handleOnStart: appMovedToForeground");
5331
5342
  this.appMovedToForeground();
5332
5343
  }
5333
5344
  isPreviousMainActivity = true;
@@ -5346,11 +5357,16 @@ public class CapacitorUpdaterPlugin extends Plugin {
5346
5357
 
5347
5358
  // On Android < 14, use activity lifecycle for background detection
5348
5359
  // On Android 14+, ProcessLifecycleOwner handles this via AppLifecycleObserver
5349
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
5350
- isPreviousMainActivity = isMainActivity();
5351
- if (isPreviousMainActivity) {
5352
- logger.info("handleOnStop: appMovedToBackground (Android <14 path)");
5360
+ if (!this.isProcessLifecycleObserverActive()) {
5361
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
5362
+ logger.info("handleOnStop: appMovedToBackground");
5353
5363
  this.appMovedToBackground();
5364
+ } else {
5365
+ isPreviousMainActivity = isMainActivity();
5366
+ if (isPreviousMainActivity) {
5367
+ logger.info("handleOnStop: appMovedToBackground (Android <14 path)");
5368
+ this.appMovedToBackground();
5369
+ }
5354
5370
  }
5355
5371
  }
5356
5372
  } catch (Exception e) {