blob: 9509493881b8ebf39dbeaf6019ec2c17385581d5 (
plain)
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
|
package nl.xservices.plugins;
import android.view.WindowManager;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
public class Insomnia extends CordovaPlugin {
private static final String ACTION_KEEP_AWAKE = "keepAwake";
private static final String ACTION_ALLOW_SLEEP_AGAIN = "allowSleepAgain";
@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
try {
if (ACTION_KEEP_AWAKE.equals(action)) {
cordova.getActivity().runOnUiThread(
new Runnable() {
public void run() {
cordova.getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
}
});
return true;
} else if (ACTION_ALLOW_SLEEP_AGAIN.equals(action)) {
cordova.getActivity().runOnUiThread(
new Runnable() {
public void run() {
cordova.getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));
}
});
return true;
} else {
callbackContext.error("insomnia." + action + " is not a supported function. Did you mean '" + ACTION_KEEP_AWAKE + "'?");
return false;
}
} catch (Exception e) {
callbackContext.error(e.getMessage());
return false;
}
}
}
|