Showing
6 changed files
with
50 additions
and
42 deletions
| @@ -9,37 +9,41 @@ To use this plugin, follow the [installing guide](https://pub.dev/packages/wakel | @@ -9,37 +9,41 @@ To use this plugin, follow the [installing guide](https://pub.dev/packages/wakel | ||
| 9 | ### Implementation | 9 | ### Implementation |
| 10 | 10 | ||
| 11 | Everything in this plugin is controlled via the [`Wakelock` class](https://pub.dev/documentation/wakelock/latest/wakelock/Wakelock-class.html). | 11 | Everything in this plugin is controlled via the [`Wakelock` class](https://pub.dev/documentation/wakelock/latest/wakelock/Wakelock-class.html). |
| 12 | -If you want to enable the wakelock, you can simply call [`Wakelock.enableWakelock`](https://pub.dev/documentation/wakelock/latest/wakelock/Wakelock/enableWakelock.html) and to disable it, you can use [`Wakelock.disableWakelock`](https://pub.dev/documentation/wakelock/latest/wakelock/Wakelock/disableWakelock.html): | 12 | +If you want to enable the wakelock, you can simply call [`Wakelock.enable`](https://pub.dev/documentation/wakelock/latest/wakelock/Wakelock/enable.html) and to disable it, you can use [`Wakelock.disable`](https://pub.dev/documentation/wakelock/latest/wakelock/Wakelock/disable.html): |
| 13 | 13 | ||
| 14 | ```dart | 14 | ```dart |
| 15 | import 'package:wakelock/wakelock.dart'; | 15 | import 'package:wakelock/wakelock.dart'; |
| 16 | // ... | 16 | // ... |
| 17 | 17 | ||
| 18 | // The following line will enable the Android and iOS wakelock. | 18 | // The following line will enable the Android and iOS wakelock. |
| 19 | -Wakelock.enableWakelock(); | 19 | +Wakelock.enable(); |
| 20 | 20 | ||
| 21 | // The next line disables the wakelock again. | 21 | // The next line disables the wakelock again. |
| 22 | -Wakelock.disableWakelock(); | 22 | +Wakelock.disable(); |
| 23 | ``` | 23 | ``` |
| 24 | 24 | ||
| 25 | -For more advanced usage, you can pass a `bool` to [`Wakelock.toggleWakelock`](https://pub.dev/documentation/wakelock/latest/wakelock/Wakelock/toggleWakelock.html) to enable or disable the wakelock and also retrieve the current wakelock status using [`Wakelock.isWakelockEnabled`](https://pub.dev/documentation/wakelock/latest/wakelock/Wakelock/isWakelockEnabled.html): | 25 | +For more advanced usage, you can pass a `bool` to [`Wakelock.toggle`](https://pub.dev/documentation/wakelock/latest/wakelock/Wakelock/toggle.html) to enable or disable the wakelock and also retrieve the current wakelock status using [`Wakelock.isEnabled`](https://pub.dev/documentation/wakelock/latest/wakelock/Wakelock/isEnabled.html): |
| 26 | 26 | ||
| 27 | ```dart | 27 | ```dart |
| 28 | import 'package:wakelock/wakelock.dart'; | 28 | import 'package:wakelock/wakelock.dart'; |
| 29 | // ... | 29 | // ... |
| 30 | 30 | ||
| 31 | // The following lines of code toggle the wakelock based on a bool value. | 31 | // The following lines of code toggle the wakelock based on a bool value. |
| 32 | -bool enableWakelock = true; | ||
| 33 | -Wakelock.toggleWakelock(enableWakelock); // This statement enables the wakelock. | ||
| 34 | - | ||
| 35 | -enableWakelock = false; | ||
| 36 | -Wakelock.toggleWakelock(enableWakelock); // This statement disables the wakelock. | ||
| 37 | - | ||
| 38 | -// If you want to retrieve the current wakelock status, you will have to be in an async scope to await the Future returned by isWakelockEnabled. | ||
| 39 | -bool isWakelockEnabled = await Wakelock.isWakelockEnabled; | 32 | +bool enable = true; |
| 33 | +// The following statement enables the wakelock. | ||
| 34 | +Wakelock.toggle(enable); | ||
| 35 | + | ||
| 36 | +enable = false; | ||
| 37 | +// The following statement disables the wakelock. | ||
| 38 | +Wakelock.toggle(enable); | ||
| 39 | + | ||
| 40 | +// If you want to retrieve the current wakelock status, | ||
| 41 | +// you will have to be in an async scope | ||
| 42 | +// to await the Future returned by isEnabled. | ||
| 43 | +bool isEnabled = await Wakelock.isEnabled; | ||
| 40 | ``` | 44 | ``` |
| 41 | 45 | ||
| 42 | -If you want to wait for the wakelock toggle on Android or iOS to complete (which takes an insignificant amount of time), you can also await either of `Wakelock.enableWakelock`, `Wakelock.disableWakelock`, and `Wakelock.toggleWakelock`. | 46 | +If you want to wait for the wakelock toggle on Android or iOS to complete (which takes an insignificant amount of time), you can also await either of `Wakelock.enable`, `Wakelock.disable`, and `Wakelock.toggle`. |
| 43 | 47 | ||
| 44 | ## Note | 48 | ## Note |
| 45 | 49 |
| @@ -20,15 +20,15 @@ public class WakelockPlugin implements MethodCallHandler { | @@ -20,15 +20,15 @@ public class WakelockPlugin implements MethodCallHandler { | ||
| 20 | channel.setMethodCallHandler(new WakelockPlugin(registrar)); | 20 | channel.setMethodCallHandler(new WakelockPlugin(registrar)); |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | - private boolean isWakelockEnabled() { | 23 | + private boolean isEnabled() { |
| 24 | return (registrar.activity().getWindow().getAttributes().flags & | 24 | return (registrar.activity().getWindow().getAttributes().flags & |
| 25 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) != 0; | 25 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) != 0; |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | @Override | 28 | @Override |
| 29 | public void onMethodCall(MethodCall call, Result result) { | 29 | public void onMethodCall(MethodCall call, Result result) { |
| 30 | - if (call.method.equals("toggleWakelock")) { | ||
| 31 | - final boolean enable = call.argument("enable"), enabled = isWakelockEnabled(); | 30 | + if (call.method.equals("toggle")) { |
| 31 | + final boolean enable = call.argument("enable"), enabled = isEnabled(); | ||
| 32 | if (enable) { | 32 | if (enable) { |
| 33 | if (!enabled) | 33 | if (!enabled) |
| 34 | registrar.activity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); | 34 | registrar.activity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| @@ -36,8 +36,8 @@ public class WakelockPlugin implements MethodCallHandler { | @@ -36,8 +36,8 @@ public class WakelockPlugin implements MethodCallHandler { | ||
| 36 | registrar.activity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); | 36 | registrar.activity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 37 | } | 37 | } |
| 38 | result.success(null); | 38 | result.success(null); |
| 39 | - } else if (call.method.equals("isWakelockEnabled")) { | ||
| 40 | - result.success(isWakelockEnabled()); | 39 | + } else if (call.method.equals("isEnabled")) { |
| 40 | + result.success(isEnabled()); | ||
| 41 | } else { | 41 | } else { |
| 42 | result.notImplemented(); | 42 | result.notImplemented(); |
| 43 | } | 43 | } |
| @@ -25,7 +25,7 @@ class _MyAppState extends State<MyApp> { | @@ -25,7 +25,7 @@ class _MyAppState extends State<MyApp> { | ||
| 25 | onPressed: () { | 25 | onPressed: () { |
| 26 | // The following code will enable the wakelock on Android or iOS using the wakelock plugin. | 26 | // The following code will enable the wakelock on Android or iOS using the wakelock plugin. |
| 27 | setState(() { | 27 | setState(() { |
| 28 | - Wakelock.enableWakelock(); | 28 | + Wakelock.enable(); |
| 29 | }); | 29 | }); |
| 30 | }, | 30 | }, |
| 31 | ), | 31 | ), |
| @@ -34,14 +34,14 @@ class _MyAppState extends State<MyApp> { | @@ -34,14 +34,14 @@ class _MyAppState extends State<MyApp> { | ||
| 34 | onPressed: () { | 34 | onPressed: () { |
| 35 | // The following code will disable the wakelock on Android or iOS using the wakelock plugin. | 35 | // The following code will disable the wakelock on Android or iOS using the wakelock plugin. |
| 36 | setState(() { | 36 | setState(() { |
| 37 | - Wakelock.disableWakelock(); | 37 | + Wakelock.disable(); |
| 38 | }); | 38 | }); |
| 39 | }, | 39 | }, |
| 40 | ), | 40 | ), |
| 41 | FutureBuilder( | 41 | FutureBuilder( |
| 42 | - future: Wakelock.isWakelockEnabled, | 42 | + future: Wakelock.isEnabled, |
| 43 | builder: (context, AsyncSnapshot<bool> snapshot) { | 43 | builder: (context, AsyncSnapshot<bool> snapshot) { |
| 44 | - // The use of FutureBuilder is necessary here to await the bool value from isWakelockEnabled. | 44 | + // The use of FutureBuilder is necessary here to await the bool value from isEnabled. |
| 45 | if (!snapshot.hasData) | 45 | if (!snapshot.hasData) |
| 46 | return Container(); // The Future is retrieved so fast that you will not be able to see any loading indicator. | 46 | return Container(); // The Future is retrieved so fast that you will not be able to see any loading indicator. |
| 47 | return Text( | 47 | return Text( |
| @@ -10,11 +10,11 @@ | @@ -10,11 +10,11 @@ | ||
| 10 | } | 10 | } |
| 11 | 11 | ||
| 12 | - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { | 12 | - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { |
| 13 | - if ([@"toggleWakelock" isEqualToString:call.method]) { | 13 | + if ([@"toggle" isEqualToString:call.method]) { |
| 14 | NSNumber *enable = call.arguments[@"enable"]; | 14 | NSNumber *enable = call.arguments[@"enable"]; |
| 15 | [[UIApplication sharedApplication] setIdleTimerDisabled:enable.boolValue]; | 15 | [[UIApplication sharedApplication] setIdleTimerDisabled:enable.boolValue]; |
| 16 | result(nil); | 16 | result(nil); |
| 17 | - } else if ([@"isWakelockEnabled" isEqualToString:call.method]) { | 17 | + } else if ([@"isEnabled" isEqualToString:call.method]) { |
| 18 | bool enabled = [[UIApplication sharedApplication] isIdleTimerDisabled]; | 18 | bool enabled = [[UIApplication sharedApplication] isIdleTimerDisabled]; |
| 19 | result([NSNumber numberWithBool:enabled]); | 19 | result([NSNumber numberWithBool:enabled]); |
| 20 | } else { | 20 | } else { |
| @@ -2,39 +2,38 @@ import 'dart:async'; | @@ -2,39 +2,38 @@ import 'dart:async'; | ||
| 2 | 2 | ||
| 3 | import 'package:flutter/services.dart'; | 3 | import 'package:flutter/services.dart'; |
| 4 | 4 | ||
| 5 | -/// To enable the wakelock, you can use [Wakelock.enableWakelock] and to disable it, | ||
| 6 | -/// you can call [Wakelock.disableWakelock]. | ||
| 7 | -/// You do not need to worry about making redundant calls, e.g. calling [Wakelock.enableWakelock] | 5 | +/// To enable the wakelock, you can use [Wakelock.enable] and to disable it, |
| 6 | +/// you can call [Wakelock.disable]. | ||
| 7 | +/// You do not need to worry about making redundant calls, e.g. calling [Wakelock.enable] | ||
| 8 | /// when the wakelock is already enabled as the plugin handles this for you. | 8 | /// when the wakelock is already enabled as the plugin handles this for you. |
| 9 | /// If you want the flexibility to pass a [bool] to control whether the wakelock should be | 9 | /// If you want the flexibility to pass a [bool] to control whether the wakelock should be |
| 10 | -/// enabled or disabled, you can use [Wakelock.toggleWakelock]. | 10 | +/// enabled or disabled, you can use [Wakelock.toggle]. |
| 11 | /// | 11 | /// |
| 12 | -/// The [Wakelock.isWakelockEnabled] function allows you to retrieve the current wakelock | 12 | +/// The [Wakelock.isEnabled] function allows you to retrieve the current wakelock |
| 13 | /// status from Android or iOS. | 13 | /// status from Android or iOS. |
| 14 | class Wakelock { | 14 | class Wakelock { |
| 15 | static const MethodChannel _channel = const MethodChannel('wakelock'); | 15 | static const MethodChannel _channel = const MethodChannel('wakelock'); |
| 16 | 16 | ||
| 17 | - /// This can simply be called using `Wakelock.enableWakelock()` and does not return anything. | 17 | + /// This can simply be called using `Wakelock.enable()` and does not return anything. |
| 18 | /// You can await the [Future] to wait for the operation to complete. | 18 | /// You can await the [Future] to wait for the operation to complete. |
| 19 | - static Future<void> enableWakelock() => | ||
| 20 | - _channel.invokeMethod('toggleWakelock', {'enable': true}); | 19 | + static Future<void> enable() => |
| 20 | + _channel.invokeMethod('toggle', {'enable': true}); | ||
| 21 | 21 | ||
| 22 | - /// This can simply be called using `Wakelock.disableWakelock()` and does not return anything. | 22 | + /// This can simply be called using `Wakelock.disable()` and does not return anything. |
| 23 | /// You can await the [Future] to wait for the operation to complete. | 23 | /// You can await the [Future] to wait for the operation to complete. |
| 24 | - static Future<void> disableWakelock() => | ||
| 25 | - _channel.invokeMethod('toggleWakelock', {'enable': false}); | 24 | + static Future<void> disable() => |
| 25 | + _channel.invokeMethod('toggle', {'enable': false}); | ||
| 26 | 26 | ||
| 27 | /// You can simply use this function to toggle the wakelock using a [bool] value. | 27 | /// You can simply use this function to toggle the wakelock using a [bool] value. |
| 28 | /// ```dart | 28 | /// ```dart |
| 29 | /// bool enable = true; | 29 | /// bool enable = true; |
| 30 | - /// Wakelock.toggleWakelock(enable); | 30 | + /// Wakelock.toggle(enable); |
| 31 | /// ``` | 31 | /// ``` |
| 32 | /// You can await the [Future] to wait for the operation to complete. | 32 | /// You can await the [Future] to wait for the operation to complete. |
| 33 | - static Future<void> toggleWakelock(bool enable) => | ||
| 34 | - _channel.invokeMethod('toggleWakelock', {'enable': enable}); | 33 | + static Future<void> toggle(bool enable) => |
| 34 | + _channel.invokeMethod('toggle', {'enable': enable}); | ||
| 35 | 35 | ||
| 36 | - /// If you want to retrieve the current wakelock status, you will have to call [Wakelock.isWakelockEnabled] | ||
| 37 | - /// and await its result: `bool isWakelockEnabled = await Wakelock.isWakelockEnabled()` | ||
| 38 | - static Future<bool> get isWakelockEnabled => | ||
| 39 | - _channel.invokeMethod('isWakelockEnabled'); | 36 | + /// If you want to retrieve the current wakelock status, you will have to call [Wakelock.isEnabled] |
| 37 | + /// and await its result: `bool isEnabled = await Wakelock.isEnabled()` | ||
| 38 | + static Future<bool> get isEnabled => _channel.invokeMethod('isEnabled'); | ||
| 40 | } | 39 | } |
-
Please register or login to post a comment