Skylar Ittner 10 months ago
commit 9bfaf229e2

1
.gitignore vendored

@ -0,0 +1 @@
.idea

@ -20,6 +20,11 @@ Install the plugin using the cordova command line utility:
`$ cordova plugin add https://github.com/boltex/cordova-plugin-powermanagement.git` `$ cordova plugin add https://github.com/boltex/cordova-plugin-powermanagement.git`
OR
---
`npm i cordova-plugin-powermanagement2`
Usage Usage
----- -----

@ -1,6 +1,6 @@
{ {
"name": "@netsyms/cordova-plugin-powermanagement", "name": "@netsyms/cordova-plugin-powermanagement",
"version": "1.1.2", "version": "1.1.3",
"description": "Plugin for managing the power state (i.e. idle switching) for cordova", "description": "Plugin for managing the power state (i.e. idle switching) for cordova",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
@ -9,7 +9,7 @@
"type": "git", "type": "git",
"url": "git+https://source.netsyms.com/Netsyms/cordova-plugin-powermanagement.git" "url": "git+https://source.netsyms.com/Netsyms/cordova-plugin-powermanagement.git"
}, },
"author": "Viras-", "contributors": ["Netsyms Technologies", "Viras-", "Sitronik"],
"license": "Apache-2.0", "license": "Apache-2.0",
"bugs": { "bugs": {
"url": "https://source.netsyms.com/Netsyms/cordova-plugin-powermanagement/issues" "url": "https://source.netsyms.com/Netsyms/cordova-plugin-powermanagement/issues"
@ -18,9 +18,7 @@
"keywords": [ "keywords": [
"ecosystem:cordova", "ecosystem:cordova",
"cordova-android", "cordova-android",
"cordova-ios", "cordova-ios"
"cordova-wp7",
"cordova-wp8"
], ],
"main": "index.js" "main": "index.js"
} }

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0" <plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
id="@netsyms/cordova-plugin-powermanagement" id="@netsyms/cordova-plugin-powermanagement"
version="1.1.2"> version="1.1.3">
<name>PowerManagement</name> <name>PowerManagement</name>
<description>PowerManagement plugin for Cordova</description> <description>PowerManagement plugin for Cordova</description>
<license>Apache 2.0</license> <license>Apache 2.0</license>

@ -55,33 +55,33 @@ public class PowerManagement extends CordovaPlugin {
private CordovaWebView webView; private CordovaWebView webView;
private final Runnable heartbeat = new Runnable() { private final Runnable heartbeat = new Runnable() {
public void run() { public void run() {
try { try {
//Log.d("PowerManagementPlugin", "About to declare ourselves VISIBLE"); //Log.d("PowerManagementPlugin", "About to declare ourselves VISIBLE");
webView.getEngine().getView().dispatchWindowVisibilityChanged(View.VISIBLE); webView.getView().dispatchWindowVisibilityChanged(View.VISIBLE);
// if sdk is 23 (android 6) or greater // if sdk is 23 (android 6) or greater
if(android.os.Build.VERSION.SDK_INT > 22){ if(android.os.Build.VERSION.SDK_INT > 22){
if (wakeLock != null && powerManager != null && powerManager.isDeviceIdleMode()) { if (wakeLock != null && powerManager != null && powerManager.isDeviceIdleMode()) {
//Log.d("PowerManagementPlugin", "Poking location service"); //Log.d("PowerManagementPlugin", "Poking location service");
try { try {
wakeupIntent.send(); wakeupIntent.send();
} catch (SecurityException e) { } catch (SecurityException e) {
Log.d("PowerManagementPlugin", "SecurityException : Heartbeat location manager keep-alive failed"); Log.d("PowerManagementPlugin", "SecurityException : Heartbeat location manager keep-alive failed");
} catch (PendingIntent.CanceledException e) { } catch (PendingIntent.CanceledException e) {
Log.d("PowerManagementPlugin", "PendingIntent.CanceledException : Heartbeat location manager keep-alive failed"); Log.d("PowerManagementPlugin", "PendingIntent.CanceledException : Heartbeat location manager keep-alive failed");
} }
} }
} }
} finally { } finally {
if (handler != null) { if (handler != null) {
handler.postDelayed(this, 10000); handler.postDelayed(this, 10000);
} }
} }
} }
}; };
/** /**
@ -99,14 +99,21 @@ public class PowerManagement extends CordovaPlugin {
this.powerManager = (PowerManager) cordova.getActivity().getSystemService(Context.POWER_SERVICE); this.powerManager = (PowerManager) cordova.getActivity().getSystemService(Context.POWER_SERVICE);
handler = new Handler(); handler = new Handler();
wakeupIntent = PendingIntent.getBroadcast( context , 0, wakeupIntent = PendingIntent.getBroadcast( context , 0,
new Intent("com.android.internal.location.ALARM_WAKEUP"), 0); new Intent("com.android.internal.location.ALARM_WAKEUP"), 0);
} }
public PluginResult partialWakeLock() {
Log.d("PowerManagementPlugin", "Partial wake lock" );
PluginResult result = this.acquire( PowerManager.PARTIAL_WAKE_LOCK );
handler.postDelayed(heartbeat, 10000);
return result;
}
@Override @Override
public boolean execute(String action, JSONArray args, public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException { CallbackContext callbackContext) throws JSONException {
PluginResult result = null; PluginResult result = null;
Log.d("PowerManagementPlugin", "Plugin execute called - " + this.toString() ); Log.d("PowerManagementPlugin", "Plugin execute called - " + this.toString() );
@ -115,12 +122,9 @@ public class PowerManagement extends CordovaPlugin {
try { try {
if( action.equals("acquire") ) { if( action.equals("acquire") ) {
if( args.length() > 0 && args.getBoolean(0) ) { if( args.length() > 0 && args.getBoolean(0) ) {
Log.d("PowerManagementPlugin", "Partial wake lock" ); result = partialWakeLock();
result = this.acquire( PowerManager.PARTIAL_WAKE_LOCK ); } else {
handler.postDelayed(heartbeat, 10000); result = partialWakeLock();
}
else {
result = this.acquire( PowerManager.FULL_WAKE_LOCK );
} }
} else if( action.equals("release") ) { } else if( action.equals("release") ) {
result = this.release(); result = this.release();

@ -1,12 +1,12 @@
/* /*
* Copyright 2013 Wolfgang Koller * Copyright 2013 Wolfgang Koller
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -25,48 +25,29 @@
@implementation PowerManagement @implementation PowerManagement
- (void) acquire:(CDVInvokedUrlCommand*)command - (void) acquire:(CDVInvokedUrlCommand*)command
{ {
CDVPluginResult* result = nil;
NSString* jsString = nil;
NSString* callbackId = command.callbackId;
// Acquire a reference to the local UIApplication singleton // Acquire a reference to the local UIApplication singleton
UIApplication* app = [UIApplication sharedApplication]; UIApplication* app = [UIApplication sharedApplication];
CDVPluginResult *pluginResult = nil;
if( ![app isIdleTimerDisabled] ) { if( ![app isIdleTimerDisabled] ) {
[app setIdleTimerDisabled:true]; [app setIdleTimerDisabled:true];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
jsString = [result toSuccessCallbackString:callbackId];
}
else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ILLEGAL_ACCESS_EXCEPTION messageAsString:@"IdleTimer already disabled"];
jsString = [result toErrorCallbackString:callbackId];
} }
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self writeJavascript:jsString]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} }
- (void) release:(CDVInvokedUrlCommand*)command - (void) release:(CDVInvokedUrlCommand*)command
{ {
CDVPluginResult* result = nil;
NSString* jsString = nil;
NSString* callbackId = command.callbackId;
// Acquire a reference to the local UIApplication singleton // Acquire a reference to the local UIApplication singleton
UIApplication* app = [UIApplication sharedApplication]; UIApplication* app = [UIApplication sharedApplication];
CDVPluginResult *pluginResult = nil;
if( [app isIdleTimerDisabled] ) { if( [app isIdleTimerDisabled] ) {
[app setIdleTimerDisabled:false]; [app setIdleTimerDisabled:false];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
jsString = [result toSuccessCallbackString:callbackId];
}
else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ILLEGAL_ACCESS_EXCEPTION messageAsString:@"IdleTimer not disabled"];
jsString = [result toErrorCallbackString:callbackId];
} }
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self writeJavascript:jsString]; [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
} }
@end @end

Loading…
Cancel
Save