@capgo/capacitor-updater 6.7.5 → 6.7.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.
@@ -483,6 +483,17 @@ public class CapacitorUpdater {
483
483
  }
484
484
 
485
485
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
486
+ // Check if app is in foreground using activity lifecycle state
487
+ boolean isAppForeground =
488
+ this.activity != null &&
489
+ !this.activity.isFinishing() &&
490
+ this.activity.hasWindowFocus();
491
+ if (!isAppForeground) {
492
+ // If app is not in foreground, use regular startService instead of startForegroundService
493
+ Log.d(TAG, "App is in background, aborting downloadFileBackground");
494
+ return;
495
+ }
496
+ Log.d(TAG, "App is in foreground, using startForegroundService");
486
497
  this.activity.startForegroundService(intent);
487
498
  } else {
488
499
  this.activity.startService(intent);
@@ -57,7 +57,7 @@ public class CapacitorUpdaterPlugin extends Plugin {
57
57
  private static final String channelUrlDefault =
58
58
  "https://plugin.capgo.app/channel_self";
59
59
 
60
- private final String PLUGIN_VERSION = "6.7.5";
60
+ private final String PLUGIN_VERSION = "6.7.6";
61
61
  private static final String DELAY_CONDITION_PREFERENCES = "";
62
62
 
63
63
  private SharedPreferences.Editor editor;
@@ -10,9 +10,11 @@ import android.app.Notification;
10
10
  import android.app.NotificationChannel;
11
11
  import android.app.NotificationManager;
12
12
  import android.content.Intent;
13
+ import android.content.pm.ServiceInfo;
13
14
  import android.os.Build;
14
15
  import android.os.Handler;
15
16
  import android.os.Looper;
17
+ import android.os.PowerManager;
16
18
  import android.util.Log;
17
19
  import java.io.*;
18
20
  import java.io.FileInputStream;
@@ -23,7 +25,6 @@ import java.security.MessageDigest;
23
25
  import java.util.ArrayList;
24
26
  import java.util.Arrays;
25
27
  import java.util.List;
26
- import java.util.concurrent.CompletableFuture;
27
28
  import java.util.concurrent.ExecutorService;
28
29
  import java.util.concurrent.Executors;
29
30
  import java.util.concurrent.Future;
@@ -58,6 +59,10 @@ public class DownloadService extends IntentService {
58
59
  private static final String UPDATE_FILE = "update.dat";
59
60
  private static final int NOTIFICATION_ID = 1;
60
61
  private static final long NOTIFICATION_DELAY_MS = 4000; // 4 seconds
62
+ private static final String CHANNEL_ID = "CapacitorUpdaterChannel";
63
+ private static final String CHANNEL_NAME = "Capacitor Updater";
64
+ private static final String CHANNEL_DESCRIPTION =
65
+ "Notifications for app updates";
61
66
 
62
67
  private final OkHttpClient client = new OkHttpClient.Builder()
63
68
  .protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1))
@@ -65,6 +70,7 @@ public class DownloadService extends IntentService {
65
70
  private Handler handler = new Handler(Looper.getMainLooper());
66
71
  private Runnable notificationRunnable;
67
72
  private boolean isNotificationShown = false;
73
+ private PowerManager.WakeLock wakeLock;
68
74
 
69
75
  public DownloadService() {
70
76
  super("Background DownloadService");
@@ -73,35 +79,47 @@ public class DownloadService extends IntentService {
73
79
  @Override
74
80
  public void onCreate() {
75
81
  super.onCreate();
76
- notificationRunnable = () -> startForeground();
77
- handler.postDelayed(notificationRunnable, NOTIFICATION_DELAY_MS);
82
+ this.startForegroundService();
78
83
  }
79
84
 
80
85
  @Override
81
86
  public void onDestroy() {
82
87
  super.onDestroy();
83
88
  handler.removeCallbacks(notificationRunnable);
89
+ Log.w(TAG + " DownloadService", "DownloadService killed/destroyed");
84
90
  }
85
91
 
86
- private void startForeground() {
92
+ private void startForegroundService() {
87
93
  isNotificationShown = true;
88
- String channelId = createNotificationChannel();
94
+ String channelId = createNotificationChannelForDownload();
95
+
89
96
  Notification.Builder builder = new Notification.Builder(this, channelId)
90
97
  .setContentTitle("Downloading Update")
91
98
  .setContentText("Download in progress")
92
99
  .setSmallIcon(android.R.drawable.stat_sys_download)
93
- .setOngoing(true);
94
-
95
- startForeground(NOTIFICATION_ID, builder.build());
100
+ .setOngoing(true)
101
+ .setPriority(Notification.PRIORITY_MIN)
102
+ .setCategory(Notification.CATEGORY_SERVICE)
103
+ .setAutoCancel(false);
104
+
105
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
106
+ startForeground(
107
+ NOTIFICATION_ID,
108
+ builder.build(),
109
+ ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
110
+ );
111
+ } else {
112
+ startForeground(NOTIFICATION_ID, builder.build());
113
+ }
96
114
  }
97
115
 
98
- private String createNotificationChannel() {
116
+ private String createNotificationChannelForDownload() {
99
117
  String channelId = "capacitor_updater_channel";
100
118
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
101
119
  NotificationChannel channel = new NotificationChannel(
102
120
  channelId,
103
121
  "Capacitor Updater Downloads",
104
- NotificationManager.IMPORTANCE_LOW
122
+ NotificationManager.IMPORTANCE_MIN // // High importance to keep service alive
105
123
  );
106
124
  NotificationManager manager = getSystemService(NotificationManager.class);
107
125
  manager.createNotificationChannel(channel);
@@ -385,6 +403,8 @@ public class DownloadService extends IntentService {
385
403
  int percent = calcTotalPercent(downloadedBytes, contentLength);
386
404
  while (lastNotifiedPercent + 10 <= percent) {
387
405
  lastNotifiedPercent += 10;
406
+ // Artificial delay using CPU-bound calculation to take ~5 seconds
407
+ double result = 0;
388
408
  notifyDownload(id, lastNotifiedPercent);
389
409
  }
390
410
  }
@@ -592,4 +612,45 @@ public class DownloadService extends IntentService {
592
612
  }
593
613
  }
594
614
  }
615
+
616
+ private void createNotificationChannel() {
617
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
618
+ NotificationChannel channel = new NotificationChannel(
619
+ CHANNEL_ID,
620
+ CHANNEL_NAME,
621
+ NotificationManager.IMPORTANCE_MAX
622
+ );
623
+ channel.setDescription(CHANNEL_DESCRIPTION);
624
+ NotificationManager notificationManager = getSystemService(
625
+ NotificationManager.class
626
+ );
627
+ notificationManager.createNotificationChannel(channel);
628
+ }
629
+ }
630
+
631
+ private void showNotification(String text, int progress) {
632
+ Notification.Builder builder = new Notification.Builder(this, CHANNEL_ID)
633
+ .setSmallIcon(android.R.drawable.stat_sys_download)
634
+ .setContentTitle("App Update")
635
+ .setContentText(text)
636
+ // .setPriority(Notification.PRIORITY_LOW)
637
+ .setOngoing(true);
638
+
639
+ if (progress > 0) {
640
+ builder.setProgress(100, progress, false);
641
+ }
642
+
643
+ startForeground(NOTIFICATION_ID, builder.build());
644
+ }
645
+
646
+ // Update the notification progress
647
+ private void updateNotificationProgress(int progress) {
648
+ showNotification("Downloading OTA update... " + progress + "%", progress);
649
+ }
650
+
651
+ // When download completes or fails
652
+ private void stopForegroundService() {
653
+ stopForeground(true);
654
+ stopSelf();
655
+ }
595
656
  }
@@ -44,7 +44,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
44
44
  CAPPluginMethod(name: "isAutoUpdateAvailable", returnType: CAPPluginReturnPromise)
45
45
  ]
46
46
  public var implementation = CapacitorUpdater()
47
- private let PLUGIN_VERSION: String = "6.7.5"
47
+ private let PLUGIN_VERSION: String = "6.7.6"
48
48
  static let updateUrlDefault = "https://plugin.capgo.app/updates"
49
49
  static let statsUrlDefault = "https://plugin.capgo.app/stats"
50
50
  static let channelUrlDefault = "https://plugin.capgo.app/channel_self"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-updater",
3
- "version": "6.7.5",
3
+ "version": "6.7.6",
4
4
  "license": "MPL-2.0",
5
5
  "description": "Live update for capacitor apps",
6
6
  "main": "dist/plugin.cjs.js",