system_controller.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import 'dart:async';
  2. import 'package:flutter/foundation.dart';
  3. import '../../data/models/system_status.dart';
  4. import '../../data/services/native_bridge.dart';
  5. /// Polls the system for battery, Wi-Fi, media volume and media activity.
  6. ///
  7. /// Polling keeps the app free of long-lived broadcast receivers and works
  8. /// identically on Android 10 and 15. Timers are stopped while the launcher is
  9. /// in the background.
  10. class SystemController extends ChangeNotifier {
  11. SystemController(this._platform);
  12. final LauncherPlatform _platform;
  13. SystemStatus _status = const SystemStatus();
  14. VolumeInfo _volume = const VolumeInfo();
  15. Timer? _volumeTimer;
  16. Timer? _statusTimer;
  17. bool _active = false;
  18. bool _volumeBusy = false;
  19. bool _statusBusy = false;
  20. bool _musicBusy = false;
  21. bool _musicPlaying = false;
  22. bool _disposed = false;
  23. int _lastNonZeroVolume = 0;
  24. SystemStatus get status => _status;
  25. VolumeInfo get volume => _volume;
  26. int get volumePercent => _volume.percent;
  27. bool get isMuted => _volume.index <= 0;
  28. bool get isMusicPlaying => _musicPlaying;
  29. Future<void> start() async {
  30. if (_active) return;
  31. _active = true;
  32. await refreshAll();
  33. _volumeTimer = Timer.periodic(const Duration(milliseconds: 900), (_) {
  34. _pollVolume();
  35. _pollMusic();
  36. });
  37. _statusTimer = Timer.periodic(
  38. const Duration(seconds: 20),
  39. (_) => _pollStatus(),
  40. );
  41. }
  42. void stop() {
  43. _active = false;
  44. _volumeTimer?.cancel();
  45. _volumeTimer = null;
  46. _statusTimer?.cancel();
  47. _statusTimer = null;
  48. }
  49. Future<void> refreshAll() async {
  50. await Future.wait([_pollVolume(), _pollStatus(), _pollMusic()]);
  51. }
  52. Future<void> _pollVolume() async {
  53. if (_volumeBusy || !_active) return;
  54. _volumeBusy = true;
  55. try {
  56. final next = await _platform.getVolume();
  57. if (next.index != _volume.index || next.max != _volume.max) {
  58. _volume = next;
  59. if (next.index > 0) _lastNonZeroVolume = next.index;
  60. _notify();
  61. }
  62. } finally {
  63. _volumeBusy = false;
  64. }
  65. }
  66. Future<void> _pollMusic() async {
  67. if (_musicBusy || !_active) return;
  68. _musicBusy = true;
  69. try {
  70. final playing = await _platform.isMusicPlaying();
  71. if (playing != _musicPlaying) {
  72. _musicPlaying = playing;
  73. _notify();
  74. }
  75. } finally {
  76. _musicBusy = false;
  77. }
  78. }
  79. Future<void> _pollStatus() async {
  80. if (_statusBusy) return;
  81. _statusBusy = true;
  82. try {
  83. final next = await _platform.getStatus();
  84. if (next.batteryLevel != _status.batteryLevel ||
  85. next.charging != _status.charging ||
  86. next.wifi != _status.wifi) {
  87. _status = next;
  88. _notify();
  89. }
  90. } finally {
  91. _statusBusy = false;
  92. }
  93. }
  94. Future<void> setVolume(int index) async {
  95. final max = _volume.max <= 0 ? 15 : _volume.max;
  96. final clamped = index.clamp(0, max);
  97. _volume = VolumeInfo(index: clamped, max: max);
  98. if (clamped > 0) _lastNonZeroVolume = clamped;
  99. _notify();
  100. await _platform.setVolume(clamped);
  101. }
  102. Future<void> adjustVolume(int delta) => setVolume(_volume.index + delta);
  103. Future<void> toggleMute() async {
  104. if (_volume.index > 0) {
  105. _lastNonZeroVolume = _volume.index;
  106. await setVolume(0);
  107. } else {
  108. final restore = _lastNonZeroVolume > 0
  109. ? _lastNonZeroVolume
  110. : (_volume.max <= 0
  111. ? 1
  112. : (_volume.max / 2).round().clamp(1, _volume.max));
  113. await setVolume(restore);
  114. }
  115. }
  116. void _notify() {
  117. if (!_disposed) notifyListeners();
  118. }
  119. @override
  120. void dispose() {
  121. _disposed = true;
  122. stop();
  123. super.dispose();
  124. }
  125. }