creativecreatorormaybenot

0.1.1

## 0.1.1
* Renamed functions.
* Improved README.
## 0.1.0+3
* Added wakelock permission in Android Manifest.
... ...
... ... @@ -9,37 +9,41 @@ To use this plugin, follow the [installing guide](https://pub.dev/packages/wakel
### Implementation
Everything in this plugin is controlled via the [`Wakelock` class](https://pub.dev/documentation/wakelock/latest/wakelock/Wakelock-class.html).
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):
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):
```dart
import 'package:wakelock/wakelock.dart';
// ...
// The following line will enable the Android and iOS wakelock.
Wakelock.enableWakelock();
Wakelock.enable();
// The next line disables the wakelock again.
Wakelock.disableWakelock();
Wakelock.disable();
```
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):
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):
```dart
import 'package:wakelock/wakelock.dart';
// ...
// The following lines of code toggle the wakelock based on a bool value.
bool enableWakelock = true;
Wakelock.toggleWakelock(enableWakelock); // This statement enables the wakelock.
enableWakelock = false;
Wakelock.toggleWakelock(enableWakelock); // This statement disables the wakelock.
// 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.
bool isWakelockEnabled = await Wakelock.isWakelockEnabled;
bool enable = true;
// The following statement enables the wakelock.
Wakelock.toggle(enable);
enable = false;
// The following statement disables the wakelock.
Wakelock.toggle(enable);
// If you want to retrieve the current wakelock status,
// you will have to be in an async scope
// to await the Future returned by isEnabled.
bool isEnabled = await Wakelock.isEnabled;
```
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`.
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`.
## Note
... ...
... ... @@ -20,15 +20,15 @@ public class WakelockPlugin implements MethodCallHandler {
channel.setMethodCallHandler(new WakelockPlugin(registrar));
}
private boolean isWakelockEnabled() {
private boolean isEnabled() {
return (registrar.activity().getWindow().getAttributes().flags &
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) != 0;
}
@Override
public void onMethodCall(MethodCall call, Result result) {
if (call.method.equals("toggleWakelock")) {
final boolean enable = call.argument("enable"), enabled = isWakelockEnabled();
if (call.method.equals("toggle")) {
final boolean enable = call.argument("enable"), enabled = isEnabled();
if (enable) {
if (!enabled)
registrar.activity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
... ... @@ -36,8 +36,8 @@ public class WakelockPlugin implements MethodCallHandler {
registrar.activity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
result.success(null);
} else if (call.method.equals("isWakelockEnabled")) {
result.success(isWakelockEnabled());
} else if (call.method.equals("isEnabled")) {
result.success(isEnabled());
} else {
result.notImplemented();
}
... ...
... ... @@ -25,7 +25,7 @@ class _MyAppState extends State<MyApp> {
onPressed: () {
// The following code will enable the wakelock on Android or iOS using the wakelock plugin.
setState(() {
Wakelock.enableWakelock();
Wakelock.enable();
});
},
),
... ... @@ -34,14 +34,14 @@ class _MyAppState extends State<MyApp> {
onPressed: () {
// The following code will disable the wakelock on Android or iOS using the wakelock plugin.
setState(() {
Wakelock.disableWakelock();
Wakelock.disable();
});
},
),
FutureBuilder(
future: Wakelock.isWakelockEnabled,
future: Wakelock.isEnabled,
builder: (context, AsyncSnapshot<bool> snapshot) {
// The use of FutureBuilder is necessary here to await the bool value from isWakelockEnabled.
// The use of FutureBuilder is necessary here to await the bool value from isEnabled.
if (!snapshot.hasData)
return Container(); // The Future is retrieved so fast that you will not be able to see any loading indicator.
return Text(
... ...
... ... @@ -10,11 +10,11 @@
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"toggleWakelock" isEqualToString:call.method]) {
if ([@"toggle" isEqualToString:call.method]) {
NSNumber *enable = call.arguments[@"enable"];
[[UIApplication sharedApplication] setIdleTimerDisabled:enable.boolValue];
result(nil);
} else if ([@"isWakelockEnabled" isEqualToString:call.method]) {
} else if ([@"isEnabled" isEqualToString:call.method]) {
bool enabled = [[UIApplication sharedApplication] isIdleTimerDisabled];
result([NSNumber numberWithBool:enabled]);
} else {
... ...
... ... @@ -2,39 +2,38 @@ import 'dart:async';
import 'package:flutter/services.dart';
/// To enable the wakelock, you can use [Wakelock.enableWakelock] and to disable it,
/// you can call [Wakelock.disableWakelock].
/// You do not need to worry about making redundant calls, e.g. calling [Wakelock.enableWakelock]
/// 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.
/// If you want the flexibility to pass a [bool] to control whether the wakelock should be
/// enabled or disabled, you can use [Wakelock.toggleWakelock].
/// enabled or disabled, you can use [Wakelock.toggle].
///
/// The [Wakelock.isWakelockEnabled] function allows you to retrieve the current wakelock
/// The [Wakelock.isEnabled] function allows you to retrieve the current wakelock
/// status from Android or iOS.
class Wakelock {
static const MethodChannel _channel = const MethodChannel('wakelock');
/// This can simply be called using `Wakelock.enableWakelock()` and does not return anything.
/// 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.
static Future<void> enableWakelock() =>
_channel.invokeMethod('toggleWakelock', {'enable': true});
static Future<void> enable() =>
_channel.invokeMethod('toggle', {'enable': true});
/// This can simply be called using `Wakelock.disableWakelock()` and does not return anything.
/// 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.
static Future<void> disableWakelock() =>
_channel.invokeMethod('toggleWakelock', {'enable': false});
static Future<void> disable() =>
_channel.invokeMethod('toggle', {'enable': false});
/// You can simply use this function to toggle the wakelock using a [bool] value.
/// ```dart
/// bool enable = true;
/// Wakelock.toggleWakelock(enable);
/// Wakelock.toggle(enable);
/// ```
/// You can await the [Future] to wait for the operation to complete.
static Future<void> toggleWakelock(bool enable) =>
_channel.invokeMethod('toggleWakelock', {'enable': enable});
static Future<void> toggle(bool enable) =>
_channel.invokeMethod('toggle', {'enable': enable});
/// If you want to retrieve the current wakelock status, you will have to call [Wakelock.isWakelockEnabled]
/// and await its result: `bool isWakelockEnabled = await Wakelock.isWakelockEnabled()`
static Future<bool> get isWakelockEnabled =>
_channel.invokeMethod('isWakelockEnabled');
/// If you want to retrieve the current wakelock status, you will have to call [Wakelock.isEnabled]
/// and await its result: `bool isEnabled = await Wakelock.isEnabled()`
static Future<bool> get isEnabled => _channel.invokeMethod('isEnabled');
}
... ...