diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/README.md b/README.md index d514947..b84f9a8 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,11 @@ Install the plugin using the cordova command line utility: `$ cordova plugin add https://github.com/boltex/cordova-plugin-powermanagement.git` +OR +--- + +`npm i cordova-plugin-powermanagement2` + Usage ----- diff --git a/package.json b/package.json index c6901eb..88bc65d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "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", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" @@ -9,7 +9,7 @@ "type": "git", "url": "git+https://source.netsyms.com/Netsyms/cordova-plugin-powermanagement.git" }, - "author": "Viras-", + "contributors": ["Netsyms Technologies", "Viras-", "Sitronik"], "license": "Apache-2.0", "bugs": { "url": "https://source.netsyms.com/Netsyms/cordova-plugin-powermanagement/issues" @@ -18,9 +18,7 @@ "keywords": [ "ecosystem:cordova", "cordova-android", - "cordova-ios", - "cordova-wp7", - "cordova-wp8" + "cordova-ios" ], "main": "index.js" } diff --git a/plugin.xml b/plugin.xml index 37e6a43..31bf2a5 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ + version="1.1.3"> PowerManagement PowerManagement plugin for Cordova Apache 2.0 diff --git a/src/android/PowerManagement.java b/src/android/PowerManagement.java index 7bda6d7..d98ddf3 100644 --- a/src/android/PowerManagement.java +++ b/src/android/PowerManagement.java @@ -55,33 +55,33 @@ public class PowerManagement extends CordovaPlugin { private CordovaWebView webView; private final Runnable heartbeat = new Runnable() { - public void run() { - try { - //Log.d("PowerManagementPlugin", "About to declare ourselves VISIBLE"); - webView.getEngine().getView().dispatchWindowVisibilityChanged(View.VISIBLE); + public void run() { + try { + //Log.d("PowerManagementPlugin", "About to declare ourselves 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 (wakeLock != null && powerManager != null && powerManager.isDeviceIdleMode()) { - //Log.d("PowerManagementPlugin", "Poking location service"); - try { - wakeupIntent.send(); - } catch (SecurityException e) { - Log.d("PowerManagementPlugin", "SecurityException : Heartbeat location manager keep-alive failed"); - } catch (PendingIntent.CanceledException e) { - Log.d("PowerManagementPlugin", "PendingIntent.CanceledException : Heartbeat location manager keep-alive failed"); - } - } - - } - - } finally { - if (handler != null) { - handler.postDelayed(this, 10000); - } - } - } + if (wakeLock != null && powerManager != null && powerManager.isDeviceIdleMode()) { + //Log.d("PowerManagementPlugin", "Poking location service"); + try { + wakeupIntent.send(); + } catch (SecurityException e) { + Log.d("PowerManagementPlugin", "SecurityException : Heartbeat location manager keep-alive failed"); + } catch (PendingIntent.CanceledException e) { + Log.d("PowerManagementPlugin", "PendingIntent.CanceledException : Heartbeat location manager keep-alive failed"); + } + } + + } + + } finally { + if (handler != null) { + handler.postDelayed(this, 10000); + } + } + } }; /** @@ -99,14 +99,21 @@ public class PowerManagement extends CordovaPlugin { this.powerManager = (PowerManager) cordova.getActivity().getSystemService(Context.POWER_SERVICE); handler = new Handler(); - wakeupIntent = PendingIntent.getBroadcast( context , 0, - new Intent("com.android.internal.location.ALARM_WAKEUP"), 0); + wakeupIntent = PendingIntent.getBroadcast( context , 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 public boolean execute(String action, JSONArray args, - CallbackContext callbackContext) throws JSONException { + CallbackContext callbackContext) throws JSONException { PluginResult result = null; Log.d("PowerManagementPlugin", "Plugin execute called - " + this.toString() ); @@ -115,12 +122,9 @@ public class PowerManagement extends CordovaPlugin { try { if( action.equals("acquire") ) { if( args.length() > 0 && args.getBoolean(0) ) { - Log.d("PowerManagementPlugin", "Partial wake lock" ); - result = this.acquire( PowerManager.PARTIAL_WAKE_LOCK ); - handler.postDelayed(heartbeat, 10000); - } - else { - result = this.acquire( PowerManager.FULL_WAKE_LOCK ); + result = partialWakeLock(); + } else { + result = partialWakeLock(); } } else if( action.equals("release") ) { result = this.release(); diff --git a/src/ios/PowerManagement.m b/src/ios/PowerManagement.m index 576238f..62ce952 100644 --- a/src/ios/PowerManagement.m +++ b/src/ios/PowerManagement.m @@ -1,12 +1,12 @@ /* * Copyright 2013 Wolfgang Koller - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -25,48 +25,29 @@ @implementation PowerManagement - (void) acquire:(CDVInvokedUrlCommand*)command { - CDVPluginResult* result = nil; - NSString* jsString = nil; - NSString* callbackId = command.callbackId; - // Acquire a reference to the local UIApplication singleton UIApplication* app = [UIApplication sharedApplication]; - + + CDVPluginResult *pluginResult = nil; if( ![app isIdleTimerDisabled] ) { [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]; } - - [self writeJavascript:jsString]; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } - (void) release:(CDVInvokedUrlCommand*)command -{ - CDVPluginResult* result = nil; - NSString* jsString = nil; - NSString* callbackId = command.callbackId; - +{ // Acquire a reference to the local UIApplication singleton UIApplication* app = [UIApplication sharedApplication]; - + + + CDVPluginResult *pluginResult = nil; if( [app isIdleTimerDisabled] ) { [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]; } - - [self writeJavascript:jsString]; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; } @end