WakelockPlugin.java
1.5 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
package wakelock.wakelock;
import android.view.WindowManager;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;
public class WakelockPlugin implements MethodCallHandler {
private Registrar registrar;
private WakelockPlugin(Registrar registrar) {
this.registrar = registrar;
}
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "wakelock");
channel.setMethodCallHandler(new WakelockPlugin(registrar));
}
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("toggle")) {
final boolean enable = call.argument("enable"), enabled = isEnabled();
if (enable) {
if (!enabled)
registrar.activity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else if (enabled) {
registrar.activity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
result.success(null);
} else if (call.method.equals("isEnabled")) {
result.success(isEnabled());
} else {
result.notImplemented();
}
}
}