|
1 | 1 | package dev.fluttercommunity.plus.wakelock |
2 | 2 |
|
3 | | -import IsEnabledMessage |
4 | | -import ToggleMessage |
5 | | -import WakelockPlusApi |
6 | | - |
7 | 3 | import io.flutter.embedding.engine.plugins.FlutterPlugin |
8 | 4 | import io.flutter.embedding.engine.plugins.activity.ActivityAware |
9 | 5 | import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding |
10 | 6 |
|
11 | | -/** WakelockPlusPlugin */ |
12 | | -class WakelockPlusPlugin: FlutterPlugin, WakelockPlusApi, ActivityAware { |
13 | | - private var wakelock: Wakelock? = null |
14 | | - |
15 | | - override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) { |
16 | | - WakelockPlusApi.setUp(flutterPluginBinding.binaryMessenger, this) |
17 | | - wakelock = Wakelock() |
18 | | - } |
19 | | - |
20 | | - override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { |
21 | | - WakelockPlusApi.setUp(binding.binaryMessenger, null) |
22 | | - wakelock = null |
23 | | - } |
24 | | - |
25 | | - override fun onAttachedToActivity(binding: ActivityPluginBinding) { |
26 | | - wakelock?.activity = binding.activity |
27 | | - } |
28 | | - |
29 | | - override fun onDetachedFromActivity() { |
30 | | - wakelock?.activity = null |
31 | | - } |
32 | | - |
33 | | - override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { |
34 | | - onAttachedToActivity(binding) |
35 | | - } |
36 | | - |
37 | | - override fun onDetachedFromActivityForConfigChanges() { |
38 | | - onDetachedFromActivity() |
39 | | - } |
40 | | - |
41 | | - override fun toggle(msg: ToggleMessage) { |
42 | | - wakelock!!.toggle(msg) |
43 | | - } |
44 | | - |
45 | | - override fun isEnabled(): IsEnabledMessage { |
46 | | - return wakelock!!.isEnabled() |
47 | | - } |
| 7 | +/** |
| 8 | + * A Flutter plugin for managing wakelock functionality on Android. |
| 9 | + * |
| 10 | + * This class implements [WakelockPlusApi] for handling wakelock toggle and status queries, |
| 11 | + * and [ActivityAware] for managing the Android activity lifecycle. |
| 12 | + */ |
| 13 | +class WakelockPlusPlugin : FlutterPlugin, WakelockPlusApi, ActivityAware { |
| 14 | + private var wakelock: Wakelock? = null |
| 15 | + |
| 16 | + override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) { |
| 17 | + wakelock = Wakelock() |
| 18 | + WakelockPlusApi.setUp(binding.binaryMessenger, this) |
| 19 | + } |
| 20 | + |
| 21 | + override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) { |
| 22 | + WakelockPlusApi.setUp(binding.binaryMessenger, null) |
| 23 | + wakelock = null |
| 24 | + } |
| 25 | + |
| 26 | + override fun onAttachedToActivity(binding: ActivityPluginBinding) { |
| 27 | + wakelock?.activity = binding.activity |
| 28 | + } |
| 29 | + |
| 30 | + override fun onDetachedFromActivity() { |
| 31 | + wakelock?.activity = null |
| 32 | + } |
| 33 | + |
| 34 | + override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) { |
| 35 | + onAttachedToActivity(binding) |
| 36 | + } |
| 37 | + |
| 38 | + override fun onDetachedFromActivityForConfigChanges() { |
| 39 | + onDetachedFromActivity() |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Toggles the wakelock state based on the provided message. |
| 44 | + * |
| 45 | + * @param message The [ToggleMessage] containing the enable state. |
| 46 | + * @throws NoActivityException If no activity is available for wakelock operations. |
| 47 | + */ |
| 48 | + override fun toggle(message: ToggleMessage) { |
| 49 | + wakelock?.toggle(message) ?: throw NoActivityException() |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Queries the current wakelock state. |
| 54 | + * |
| 55 | + * @return An [IsEnabledMessage] indicating whether the wakelock is enabled. |
| 56 | + * @throws NoActivityException If no activity is available for wakelock operations. |
| 57 | + */ |
| 58 | + override fun isEnabled(): IsEnabledMessage { |
| 59 | + return wakelock?.isEnabled() ?: throw NoActivityException() |
| 60 | + } |
48 | 61 | } |
0 commit comments