wakelock.dart
3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:wakelock_macos/wakelock_macos.dart';
import 'package:wakelock_windows/wakelock_windows.dart';
import 'package:wakelock_platform_interface/wakelock_platform_interface.dart';
/// The [WakelockPlatformInterface] that is used by [Wakelock].
///
/// This needs to be exposed for testing as unit tests might run on macOS.
/// In that case, the "hacky" instance override that we use here would be
/// triggered for the unit tests, even though the unit tests should actually
/// test the `pigeon` method channel implementation. Therefore, we want to
/// override this in tests that run on macOS (where there is no actual device).
@visibleForTesting
var wakelockPlatformInstance = !kIsWeb &&
// Assigning the macOS platform instance like this is not optimal.
// Ideally, we would use the default method channel instance on macOS,
// however, it is not yet entirely clear how to integrate with pigeon.
// This should just work fine and the io reference should be tree shaken
// on web.
Platform.isMacOS
? WakelockMacOS()
// This doesn't feel like the correct way to assign the windows implementation,
// but platform channels aren't used due to the win32 package.
// See this issue for details: https://github.com/flutter/flutter/issues/52267.
: (!kIsWeb && Platform.isWindows
? WakelockWindows()
: WakelockPlatformInterface.instance);
/// Class providing all wakelock functionality using static members.
///
/// To enable the wakelock, you can use [Wakelock.enable] and to disable it,
/// you can call [Wakelock.disable].
/// You do not need to worry about making redundant calls, e.g. calling
/// [Wakelock.enable] when the wakelock is already enabled as the plugin handles
/// this for you, i.e. it checks the status to determine if the wakelock is
/// already enabled or disabled.
/// If you want the flexibility to pass a [bool] to control whether the wakelock
/// should be enabled or disabled, you can use [Wakelock.toggle].
///
/// The [Wakelock.enabled] getter allows you to retrieve the current wakelock
/// status of the device..
class Wakelock {
/// Enables the wakelock.
///
/// This can simply be called using `Wakelock.enable()` and does not return
/// anything.
/// You can await the [Future] to wait for the operation to complete.
///
/// See also:
/// * [toggle], which allows to enable or disable using a [bool] parameter.
static Future<void> enable() => toggle(enable: true);
/// Disables the wakelock.
///
/// This can simply be called using `Wakelock.disable()` and does not return
/// anything.
/// You can await the [Future] to wait for the operation to complete.
///
/// See also:
/// * [toggle], which allows to enable or disable using a [bool] parameter.
static Future<void> disable() => toggle(enable: false);
/// Toggles the wakelock on or off.
///
/// You can simply use this function to toggle the wakelock using a [bool]
/// value (for the [enable] parameter).
///
/// ```dart
/// // This line keeps the screen on.
/// Wakelock.toggle(enable: true);
///
/// bool enableWakelock = false;
/// // The following line disables the wakelock.
/// Wakelock.toggle(enable: enableWakelock);
/// ```
///
/// You can await the [Future] to wait for the operation to complete.
static Future<void> toggle({
required bool enable,
}) {
return wakelockPlatformInstance.toggle(enable: enable);
}
/// Returns whether the wakelock is currently enabled or not.
///
/// If you want to retrieve the current wakelock status, you will have to call
/// [Wakelock.enabled] and await its result:
///
/// ```dart
/// bool wakelockEnabled = await Wakelock.enabled;
/// ```
static Future<bool> get enabled => wakelockPlatformInstance.enabled;
}