flet-android-background 0.0.3__tar.gz → 0.0.4__tar.gz

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.
Files changed (14) hide show
  1. {flet_android_background-0.0.3 → flet_android_background-0.0.4}/PKG-INFO +2 -2
  2. {flet_android_background-0.0.3 → flet_android_background-0.0.4}/README.md +1 -1
  3. {flet_android_background-0.0.3 → flet_android_background-0.0.4}/pyproject.toml +1 -1
  4. {flet_android_background-0.0.3 → flet_android_background-0.0.4}/src/flet_android_background/service.py +9 -1
  5. {flet_android_background-0.0.3 → flet_android_background-0.0.4}/src/flutter/flet_android_background/android/src/main/java/dev/alexstoica/background/BackgroundPlugin.java +4 -1
  6. {flet_android_background-0.0.3 → flet_android_background-0.0.4}/src/flutter/flet_android_background/android/src/main/java/dev/alexstoica/background/TaskService.java +27 -3
  7. {flet_android_background-0.0.3 → flet_android_background-0.0.4}/src/flutter/flet_android_background/pubspec.yaml +1 -1
  8. {flet_android_background-0.0.3 → flet_android_background-0.0.4}/.gitignore +0 -0
  9. {flet_android_background-0.0.3 → flet_android_background-0.0.4}/LICENSE +0 -0
  10. {flet_android_background-0.0.3 → flet_android_background-0.0.4}/src/flet_android_background/__init__.py +0 -0
  11. {flet_android_background-0.0.3 → flet_android_background-0.0.4}/src/flet_android_background/android.py +0 -0
  12. {flet_android_background-0.0.3 → flet_android_background-0.0.4}/src/flutter/flet_android_background/android/build.gradle +0 -0
  13. {flet_android_background-0.0.3 → flet_android_background-0.0.4}/src/flutter/flet_android_background/android/src/main/AndroidManifest.xml +0 -0
  14. {flet_android_background-0.0.3 → flet_android_background-0.0.4}/src/flutter/flet_android_background/lib/flet_android_background.dart +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: flet-android-background
3
- Version: 0.0.3
3
+ Version: 0.0.4
4
4
  Summary: Experimental foreground tasks for Flet on Android
5
5
  Project-URL: Homepage, https://github.com/alex-stoica/flet-android-background
6
6
  Project-URL: Repository, https://github.com/alex-stoica/flet-android-background
@@ -20,7 +20,7 @@ Experimental; no process-death recovery. Pip install.
20
20
  Create `BackgroundTask` per page; set `page.on_close = task.on_session_close`.
21
21
  Use `await task.start(work, enable_wifi_lock=True)` and `await task.stop()`.
22
22
  WifiLock: default off; released on exit. Android 14+: foreground/screen-on only;
23
- Doze applies. Bundles `WAKE_LOCK`; no CPU lock.
23
+ Power policies apply. `WAKE_LOCK` bundled.
24
24
 
25
25
  Before building:
26
26
 
@@ -5,7 +5,7 @@ Experimental; no process-death recovery. Pip install.
5
5
  Create `BackgroundTask` per page; set `page.on_close = task.on_session_close`.
6
6
  Use `await task.start(work, enable_wifi_lock=True)` and `await task.stop()`.
7
7
  WifiLock: default off; released on exit. Android 14+: foreground/screen-on only;
8
- Doze applies. Bundles `WAKE_LOCK`; no CPU lock.
8
+ Power policies apply. `WAKE_LOCK` bundled.
9
9
 
10
10
  Before building:
11
11
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "flet-android-background"
3
- version = "0.0.3"
3
+ version = "0.0.4"
4
4
  description = "Experimental foreground tasks for Flet on Android"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -17,14 +17,22 @@ class ForegroundService(ft.Service):
17
17
 
18
18
  async def start_foreground_service(self, *, notification_id=1,
19
19
  title="Background task", body="Running",
20
- enable_wifi_lock: bool = False):
20
+ enable_wifi_lock: bool = False,
21
+ enable_cpu_lock: bool = False,
22
+ cpu_lock_timeout: int = 3600):
23
+ """CPU lock expires after 1–21600 seconds and stops the service."""
21
24
  if notification_id <= 0:
22
25
  raise ValueError("notification_id must be positive")
23
26
  if not isinstance(enable_wifi_lock, bool):
24
27
  raise TypeError("enable_wifi_lock must be a bool")
28
+ if not isinstance(enable_cpu_lock, bool):
29
+ raise TypeError("enable_cpu_lock must be a bool")
30
+ if type(cpu_lock_timeout) is not int or not 1 <= cpu_lock_timeout <= 21600:
31
+ raise ValueError("cpu_lock_timeout must be an integer from 1 to 21600 seconds")
25
32
  self._owner = await self._call(
26
33
  "start", id=notification_id, title=title, body=body,
27
34
  enable_wifi_lock=enable_wifi_lock,
35
+ enable_cpu_lock=enable_cpu_lock, cpu_lock_timeout=cpu_lock_timeout,
28
36
  )
29
37
 
30
38
  async def stop_foreground_service(self):
@@ -54,7 +54,10 @@ public class BackgroundPlugin implements FlutterPlugin, ActivityAware,
54
54
  .putExtra("id", ((Number) call.argument("id")).intValue())
55
55
  .putExtra("title", (String) call.argument("title"))
56
56
  .putExtra("body", (String) call.argument("body"))
57
- .putExtra("enable_wifi_lock", Boolean.TRUE.equals(call.argument("enable_wifi_lock")));
57
+ .putExtra("enable_wifi_lock", Boolean.TRUE.equals(call.argument("enable_wifi_lock")))
58
+ .putExtra("enable_cpu_lock", Boolean.TRUE.equals(call.argument("enable_cpu_lock")))
59
+ .putExtra("cpu_lock_timeout", call.argument("cpu_lock_timeout") == null ? 3600L
60
+ : ((Number) call.argument("cpu_lock_timeout")).longValue());
58
61
  token = call.argument("owner");
59
62
  if (token == null) throw new IllegalArgumentException("Missing service owner");
60
63
  TaskService.owner = this;
@@ -9,11 +9,17 @@ import android.content.Intent;
9
9
  import android.os.Build;
10
10
  import android.net.wifi.WifiManager;
11
11
  import android.os.IBinder;
12
+ import android.os.Handler;
13
+ import android.os.Looper;
14
+ import android.os.PowerManager;
12
15
 
13
16
  public class TaskService extends Service {
14
17
  static BackgroundPlugin owner;
15
18
  private static final String CHANNEL = "python_background";
16
19
  private WifiManager.WifiLock wifiLock;
20
+ private PowerManager.WakeLock cpuLock;
21
+ private final Handler handler = new Handler(Looper.getMainLooper());
22
+ private final Runnable expireCpuLock = this::stopSelf;
17
23
 
18
24
  @Override public int onStartCommand(Intent intent, int flags, int startId) {
19
25
  if (owner == null || intent == null || "stop".equals(intent.getAction())) {
@@ -40,6 +46,19 @@ public class TaskService extends Service {
40
46
  if (launch != null) builder.setContentIntent(PendingIntent.getActivity(
41
47
  this, 0, launch, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT));
42
48
  startForeground(intent.getIntExtra("id", 1), builder.build());
49
+ if (intent.getBooleanExtra("enable_cpu_lock", false) && cpuLock == null) {
50
+ long seconds = intent.getLongExtra("cpu_lock_timeout", 3600L);
51
+ if (seconds < 1 || seconds > 21600) {
52
+ throw new IllegalArgumentException("CPU lock timeout must be 1–21600 seconds");
53
+ }
54
+ PowerManager power = getSystemService(PowerManager.class);
55
+ if (power == null) throw new IllegalStateException("Power service unavailable");
56
+ cpuLock = power.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
57
+ "flet_android_background:cpu");
58
+ cpuLock.setReferenceCounted(false);
59
+ cpuLock.acquire(seconds * 1000L);
60
+ handler.postDelayed(expireCpuLock, seconds * 1000L);
61
+ }
43
62
  if (intent.getBooleanExtra("enable_wifi_lock", false) && wifiLock == null) {
44
63
  WifiManager wifi = getApplicationContext().getSystemService(WifiManager.class);
45
64
  if (wifi == null) throw new IllegalStateException("Wi-Fi service unavailable");
@@ -50,14 +69,19 @@ public class TaskService extends Service {
50
69
  }
51
70
  } catch (Exception error) {
52
71
  failure = error;
53
- releaseWifiLock();
72
+ releaseLocks();
54
73
  stopSelf();
55
74
  }
56
75
  owner.onStarted(failure);
57
76
  return START_NOT_STICKY;
58
77
  }
59
78
 
60
- private void releaseWifiLock() {
79
+ private void releaseLocks() {
80
+ handler.removeCallbacks(expireCpuLock);
81
+ if (cpuLock != null) {
82
+ if (cpuLock.isHeld()) cpuLock.release();
83
+ cpuLock = null;
84
+ }
61
85
  if (wifiLock != null) {
62
86
  if (wifiLock.isHeld()) wifiLock.release();
63
87
  wifiLock = null;
@@ -71,7 +95,7 @@ public class TaskService extends Service {
71
95
  @Override public void onDestroy() {
72
96
  BackgroundPlugin previous = owner;
73
97
  owner = null;
74
- releaseWifiLock();
98
+ releaseLocks();
75
99
  stopForeground(STOP_FOREGROUND_REMOVE);
76
100
  if (previous != null) previous.onStopped();
77
101
  super.onDestroy();
@@ -1,6 +1,6 @@
1
1
  name: flet_android_background
2
2
  description: Foreground task lifecycle for Flet Android apps
3
- version: 0.0.3
3
+ version: 0.0.4
4
4
  publish_to: none
5
5
  environment:
6
6
  sdk: '>=3.11.0 <4.0.0'