system_controller.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import 'dart:async';
  2. import 'package:flutter/foundation.dart';
  3. import '../../data/models/media_playback.dart';
  4. import '../../data/models/system_status.dart';
  5. import '../../data/services/native_bridge.dart';
  6. /// Polls the system for battery, Wi-Fi, media volume and media activity.
  7. ///
  8. /// Polling keeps the app free of long-lived broadcast receivers and works
  9. /// identically on Android 10 and 15. Timers are stopped while the launcher is
  10. /// in the background.
  11. class SystemController extends ChangeNotifier {
  12. SystemController(this._platform);
  13. final LauncherPlatform _platform;
  14. SystemStatus _status = const SystemStatus();
  15. VolumeInfo _volume = const VolumeInfo();
  16. Timer? _volumeTimer;
  17. Timer? _statusTimer;
  18. bool _active = false;
  19. bool _volumeBusy = false;
  20. bool _statusBusy = false;
  21. bool _musicBusy = false;
  22. bool _musicPlaying = false;
  23. MediaPlayback _media = const MediaPlayback();
  24. bool _disposed = false;
  25. int _lastNonZeroVolume = 0;
  26. SystemStatus get status => _status;
  27. VolumeInfo get volume => _volume;
  28. int get volumePercent => _volume.percent;
  29. bool get isMuted => _volume.index <= 0;
  30. bool get isMusicPlaying => _musicPlaying;
  31. MediaPlayback get media => _media;
  32. bool get hasMediaAccess => _media.hasAccess;
  33. Future<void> start() async {
  34. if (_active) return;
  35. _active = true;
  36. await refreshAll();
  37. _volumeTimer = Timer.periodic(const Duration(milliseconds: 900), (_) {
  38. _pollVolume();
  39. _pollMusic();
  40. });
  41. _statusTimer = Timer.periodic(
  42. const Duration(seconds: 20),
  43. (_) => _pollStatus(),
  44. );
  45. }
  46. void stop() {
  47. _active = false;
  48. _volumeTimer?.cancel();
  49. _volumeTimer = null;
  50. _statusTimer?.cancel();
  51. _statusTimer = null;
  52. }
  53. Future<void> refreshAll() async {
  54. await Future.wait([_pollVolume(), _pollStatus(), _pollMusic()]);
  55. }
  56. Future<void> _pollVolume() async {
  57. if (_volumeBusy || !_active) return;
  58. _volumeBusy = true;
  59. try {
  60. final next = await _platform.getVolume();
  61. if (next.index != _volume.index || next.max != _volume.max) {
  62. _volume = next;
  63. if (next.index > 0) _lastNonZeroVolume = next.index;
  64. _notify();
  65. }
  66. } finally {
  67. _volumeBusy = false;
  68. }
  69. }
  70. Future<void> _pollMusic() async {
  71. if (_musicBusy || !_active) return;
  72. _musicBusy = true;
  73. try {
  74. final media = await _platform.getMediaState();
  75. final fallback = await _platform.isMusicPlaying();
  76. final playing = media.hasAccess && media.hasSession
  77. ? media.playing
  78. : fallback;
  79. final changed = media != _media || playing != _musicPlaying;
  80. _media = media;
  81. _musicPlaying = playing;
  82. if (changed) _notify();
  83. } finally {
  84. _musicBusy = false;
  85. }
  86. }
  87. Future<void> _pollStatus() async {
  88. if (_statusBusy) return;
  89. _statusBusy = true;
  90. try {
  91. final next = await _platform.getStatus();
  92. if (next.batteryLevel != _status.batteryLevel ||
  93. next.charging != _status.charging ||
  94. next.wifi != _status.wifi) {
  95. _status = next;
  96. _notify();
  97. }
  98. } finally {
  99. _statusBusy = false;
  100. }
  101. }
  102. Future<void> setVolume(int index) async {
  103. final max = _volume.max <= 0 ? 15 : _volume.max;
  104. final clamped = index.clamp(0, max);
  105. _volume = VolumeInfo(index: clamped, max: max);
  106. if (clamped > 0) _lastNonZeroVolume = clamped;
  107. _notify();
  108. await _platform.setVolume(clamped);
  109. }
  110. Future<void> adjustVolume(int delta) => setVolume(_volume.index + delta);
  111. Future<bool> openMediaAccessSettings() {
  112. return _platform.openNotificationAccessSettings();
  113. }
  114. /// Sends a pause key immediately. The next poll reconciles the real state.
  115. Future<void> pauseMusic() async {
  116. try {
  117. await _platform.sendMediaKey('pause');
  118. } catch (_) {
  119. // Media keys are best-effort; the next poll still reflects reality.
  120. }
  121. if (_musicPlaying) {
  122. _musicPlaying = false;
  123. _notify();
  124. }
  125. }
  126. /// Tries to resume a paused session. Returns false when nothing started, so
  127. /// the caller can open the configured music app instead.
  128. Future<bool> playMusic() async {
  129. try {
  130. final sent = await _platform.sendMediaKey('play');
  131. if (!sent) return false;
  132. await Future<void>.delayed(const Duration(milliseconds: 450));
  133. final media = await _platform.getMediaState();
  134. final fallback = await _platform.isMusicPlaying();
  135. final playing = media.hasAccess && media.hasSession
  136. ? media.playing
  137. : fallback;
  138. final changed = media != _media || playing != _musicPlaying;
  139. _media = media;
  140. _musicPlaying = playing;
  141. if (changed) _notify();
  142. return playing;
  143. } catch (_) {
  144. return false;
  145. }
  146. }
  147. Future<void> toggleMute() async {
  148. if (_volume.index > 0) {
  149. _lastNonZeroVolume = _volume.index;
  150. await setVolume(0);
  151. } else {
  152. final restore = _lastNonZeroVolume > 0
  153. ? _lastNonZeroVolume
  154. : (_volume.max <= 0
  155. ? 1
  156. : (_volume.max / 2).round().clamp(1, _volume.max));
  157. await setVolume(restore);
  158. }
  159. }
  160. void _notify() {
  161. if (!_disposed) notifyListeners();
  162. }
  163. @override
  164. void dispose() {
  165. _disposed = true;
  166. stop();
  167. super.dispose();
  168. }
  169. }