launcher_controller.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import 'dart:async';
  2. import 'package:flutter/foundation.dart';
  3. import '../../data/models/app_entry.dart';
  4. import '../../data/models/slot_assignment.dart';
  5. import '../../data/models/slot_type.dart';
  6. import '../../data/repositories/settings_repository.dart';
  7. import '../../data/services/native_bridge.dart';
  8. /// Owns installed apps, shortcut assignments and the default-home state.
  9. class LauncherController extends ChangeNotifier {
  10. LauncherController(this._platform, this._settings);
  11. final LauncherPlatform _platform;
  12. final SettingsRepository _settings;
  13. final Map<SlotType, SlotAssignment> _slots = {};
  14. List<AppEntry> _apps = const [];
  15. bool _appsLoaded = false;
  16. bool _loadingApps = false;
  17. Object? _appsError;
  18. DateTime? _lastAppsLoad;
  19. bool _defaultHome = false;
  20. bool _defaultHomeKnown = false;
  21. Timer? _stateTimer;
  22. bool _disposed = false;
  23. List<AppEntry> get apps => _apps;
  24. bool get isLoadingApps => _loadingApps;
  25. bool get appsLoaded => _appsLoaded;
  26. Object? get appsError => _appsError;
  27. bool get isDefaultHome => _defaultHome;
  28. bool get defaultHomeKnown => _defaultHomeKnown;
  29. SlotAssignment? assignmentFor(SlotType slot) => _slots[slot];
  30. /// Loads persisted choices synchronously, then refreshes platform state in
  31. /// the background so the first frame is not blocked by package enumeration.
  32. void initialize() {
  33. for (final slot in SlotType.values) {
  34. final assignment = _settings.slot(slot);
  35. if (assignment != null) _slots[slot] = assignment;
  36. }
  37. _notify();
  38. unawaited(refreshDefaultHome());
  39. unawaited(loadApps());
  40. }
  41. Future<void> loadApps({bool force = false}) async {
  42. if (_loadingApps) return;
  43. if (_appsLoaded && !force) return;
  44. _loadingApps = true;
  45. _appsError = null;
  46. _notify();
  47. try {
  48. final loaded = await _platform.listApps();
  49. _apps = List<AppEntry>.unmodifiable(loaded);
  50. _appsLoaded = true;
  51. _lastAppsLoad = DateTime.now();
  52. await _validateAssignments();
  53. } catch (error) {
  54. _appsError = error;
  55. } finally {
  56. _loadingApps = false;
  57. _notify();
  58. }
  59. }
  60. /// Called when the launcher returns to the foreground. The package list is
  61. /// refreshed at most once a minute unless [forceApps] is set.
  62. Future<void> refresh({bool forceApps = false}) async {
  63. await refreshDefaultHome();
  64. final last = _lastAppsLoad;
  65. if (forceApps ||
  66. last == null ||
  67. DateTime.now().difference(last).inSeconds > 60) {
  68. await loadApps(force: true);
  69. }
  70. }
  71. Future<void> _validateAssignments() async {
  72. final installed = _apps
  73. .map((app) => '${app.packageName}/${app.activityName}')
  74. .toSet();
  75. final missing = <SlotType>[];
  76. _slots.forEach((slot, assignment) {
  77. if (!installed.contains(
  78. '${assignment.packageName}/${assignment.activityName}',
  79. )) {
  80. missing.add(slot);
  81. }
  82. });
  83. for (final slot in missing) {
  84. _slots.remove(slot);
  85. await _settings.clearSlot(slot);
  86. }
  87. }
  88. Future<void> assign(SlotType slot, AppEntry app) async {
  89. final assignment = app.toAssignment();
  90. _slots[slot] = assignment;
  91. await _settings.setSlot(slot, assignment);
  92. _notify();
  93. }
  94. Future<void> clearSlot(SlotType slot) async {
  95. _slots.remove(slot);
  96. await _settings.clearSlot(slot);
  97. _notify();
  98. }
  99. Future<String?> launchSlot(SlotType slot) async {
  100. final assignment = _slots[slot];
  101. if (assignment == null) return null;
  102. return launchAssignment(assignment);
  103. }
  104. Future<String?> launchAssignment(SlotAssignment assignment) async {
  105. try {
  106. await _platform.launchApp(
  107. packageName: assignment.packageName,
  108. activityName: assignment.activityName,
  109. );
  110. return null;
  111. } on LauncherPlatformException catch (error) {
  112. return error.message;
  113. } catch (_) {
  114. return 'Could not open ${assignment.label.isEmpty ? 'this app' : assignment.label}.';
  115. }
  116. }
  117. Future<String?> launchAppEntry(AppEntry app) async {
  118. try {
  119. await _platform.launchApp(
  120. packageName: app.packageName,
  121. activityName: app.activityName,
  122. );
  123. return null;
  124. } on LauncherPlatformException catch (error) {
  125. return error.message;
  126. } catch (_) {
  127. return 'Could not open ${app.label}.';
  128. }
  129. }
  130. Future<void> refreshDefaultHome() async {
  131. try {
  132. _defaultHome = await _platform.isDefaultHome();
  133. } catch (_) {
  134. _defaultHome = false;
  135. }
  136. _defaultHomeKnown = true;
  137. _notify();
  138. }
  139. Future<bool> requestDefaultHome() async {
  140. final launched = await _platform.requestDefaultHome();
  141. if (launched) {
  142. // The system dialog will pause us; refresh again after it closes.
  143. _stateTimer?.cancel();
  144. _stateTimer = Timer(const Duration(seconds: 2), () {
  145. unawaited(refreshDefaultHome());
  146. });
  147. }
  148. return launched;
  149. }
  150. void _notify() {
  151. if (!_disposed) notifyListeners();
  152. }
  153. @override
  154. void dispose() {
  155. _disposed = true;
  156. _stateTimer?.cancel();
  157. super.dispose();
  158. }
  159. }