Giter Club home page Giter Club logo

cordova-plugin-powermanagement's Introduction

PowerManagement

Plugin for Cordova (3.0+)

The PowerManagement plugin offers access to the devices power-management functionality. It should be used for applications which keep running for a long time without any user interaction.

For details on power functionality see:

Installation

Install using npm:

$ npm i cordova-plugin-powermanagement-orig

Install the plugin using the cordova command line utility:

$ cordova plugin add https://github.com/Viras-/cordova-plugin-powermanagement.git

Usage

window.powerManagement.acquire(successCallback, failureCallback)

Acquire a wakelock by calling this.

window.powerManagement.acquire(function() {
	console.log('Wakelock acquired');
}, function() {
	console.log('Failed to acquire wakelock');
});

window.powerManagement.dim(successCallback, failureCallback)

This acquires a partial wakelock, allowing the screen to be dimmed.

window.powerManagement.dim(function() {
	console.log('Wakelock acquired');
}, function() {
	console.log('Failed to acquire wakelock');
});

This function is nort supported on windows platform and will invoke the successCallback.

window.powerManagement.release(successCallback, failureCallback)

Release the wakelock. It's important to do this when you're finished with the wakelock, to avoid unnecessary battery drain.

window.powerManagement.release(function() {
	console.log('Wakelock released');
}, function() {
	console.log('Failed to release wakelock');
});

[Android Only] window.powerManagement.setReleaseOnPause(enabled, successCallback, failureCallback)

By default, the plugin will automatically release a wakelock when your app is paused (e.g. when the screen is turned off, or the user switches to another app). It will reacquire the wakelock upon app resume. If you would prefer to disable this behaviour, you can use this function.

window.powerManagement.setReleaseOnPause(false, function() {
	console.log('Set successfully');
}, function() {
	console.log('Failed to set');
});

Note that in all the above examples, all callbacks are optional.

License

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. See the License for the specific language governing permissions and limitations under the License.

cordova-plugin-powermanagement's People

Contributors

kubkov avatar marcoeidinger avatar menardi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cordova-plugin-powermanagement's Issues

question about partial wakelock

Hi,
thanks for the useful plugin, and also for the enhancement by Boltex.

My Android app fetches data periodically from a server. In order to keep the app running at all times I'm using Katzer's background plugin along with your powermanagemen plugin.
This works very well, however there is still one problem: the screen remain on all the time (unless I manually switch it off with the power button). Even when I switch to another app the screen remains on all the time.
I would like to have the screen go off automatically after some time of inactivity, just like any other app.
When I use window.powerManagement.acquire, the screen remains on with full brightness.
When I use window.powerManagement.dim, the screen will go dim after a while, but it will never go off completely.
How can I have the screen go completely off automatically as usual (while still keeping the app running)?

Plugin doesn't work in iOS

@Viras- I want Screen always wake-up in iOS, so I'm integrating power management Cordova plugin for iOS.

After Create cordova project for iOS, I added this plugin.
(cordova plugin add https://github.com/Viras-/cordova-plugin-powermanagement.git)

and put this code in my script but plugin code is not works for me.

Here is my code:

<script type="text/javascript"> 

document.addEventListener("deviceready", onDeviceReady, false);

//function will be called when device ready
function onDeviceReady()
 {

     var isDeviceiOS = /iPad|iPhone|iPod/.test(navigator.userAgent); 
     if(isDeviceiOS) 
     { 
          if(window.localStorage.getItem('keepscreenonchkboxlocal') == "true")
          {
                isIdleTimerDisabled = true;
          }
          else
          {
                isIdleTimerDisabled = false;
          }
     }
     else
     {         
          if(window.localStorage.getItem('keepscreenonchkboxlocal') == "true")
          {
               window.powerManagement.acquire(function() { 
                   console.log('Wakelock acquired');            
                         }, function() { 
                   console.log('Failed to acquire wakelock');               
               });
          }
          else
          {
               window.powerManagement.release(function() {      
                   console.log('Wakelock released');        
                       }, function() {   
                   console.log('Failed to release wakelock');           
               });
          }
      }
}
</script> 

This cordova plugin works in android but doesn't works in ios.

Thanks, Kaushik.

Failed to require lock on Android device

I have below code but print Failed to acquire wakelock. How do I inspect what happens? Whether is it a permission issue or device issue? I have added android.permission.WAKE_LOCK permission on android manifest file.

const win = window;
export const requireLock = () => {
  win.powerManagement && win.powerManagement.acquire(function() {
    console.log('Wakelock acquired');
  }, function() {
    console.log('Failed to acquire wakelock');
  });
};

I am using "cordova-android": "^6.4.0",

IOS compile errors

Hello. I'm using the latest version of ionic framework (1.2) and this plugin does not compile for IOS. Here are the errors:

/Users/vincentcrepin/ionicProjects/BlitzClock/platforms/ios/BlitzClock/Plugins/at.gofg.sportscomputer.powermanagement/PowerManagement.m:39:28: error: no visible @interface for 'CDVPluginResult' declares the selector 'toSuccessCallbackString:'
jsString = [result toSuccessCallbackString:callbackId];

/Users/vincentcrepin/ionicProjects/BlitzClock/platforms/ios/BlitzClock/Plugins/at.gofg.sportscomputer.powermanagement/PowerManagement.m:43:28: error: no visible @interface for 'CDVPluginResult' declares the selector 'toErrorCallbackString:'
jsString = [result toErrorCallbackString:callbackId];
~~~~~~ ^~~~~~~~~~~~~~~~~~~~~
/Users/vincentcrepin/ionicProjects/BlitzClock/platforms/ios/BlitzClock/Plugins/at.gofg.sportscomputer.powermanagement/PowerManagement.m:46:11: error: no visible @interface for 'PowerManagement' declares the selector 'writeJavascript:'
[self writeJavascript:jsString];
~~~~ ^~~~~~~~~~~~~~~
/Users/vincentcrepin/ionicProjects/BlitzClock/platforms/ios/BlitzClock/Plugins/at.gofg.sportscomputer.powermanagement/PowerManagement.m:63:28: error: no visible @interface for 'CDVPluginResult' declares the selector 'toSuccessCallbackString:'
jsString = [result toSuccessCallbackString:callbackId];
~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~
/Users/vincentcrepin/ionicProjects/BlitzClock/platforms/ios/BlitzClock/Plugins/at.gofg.sportscomputer.powermanagement/PowerManagement.m:67:28: error: no visible @interface for 'CDVPluginResult' declares the selector 'toErrorCallbackString:'
jsString = [result toErrorCallbackString:callbackId];
~~~~~~ ^~~~~~~~~~~~~~~~~~~~~
/Users/vincentcrepin/ionicProjects/BlitzClock/platforms/ios/BlitzClock/Plugins/at.gofg.sportscomputer.powermanagement/PowerManagement.m:70:11: error: no visible @interface for 'PowerManagement' declares the selector 'writeJavascript:'
[self writeJavascript:jsString];

No visible interface for CDVPluginResult declares the selector toSuccessCallBackString

i use ionic v4 and im trying to build an app in ios but im getting 4 errors in this file:

@implementation PowerManagement
- (void) acquire:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* result = nil;
    
    // Acquire a reference to the local UIApplication singleton
    UIApplication* app = [UIApplication sharedApplication];
    
    if( ![app isIdleTimerDisabled] ) {
        [app setIdleTimerDisabled:true];
        
        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    }
    else {
        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ILLEGAL_ACCESS_EXCEPTION messageAsString:@"IdleTimer already disabled"];
    }
    
	[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}


- (void) release:(CDVInvokedUrlCommand*)command
{    
    CDVPluginResult* result = nil;
    
    // Acquire a reference to the local UIApplication singleton
    UIApplication* app = [UIApplication sharedApplication];
    
    if( [app isIdleTimerDisabled] ) {
        [app setIdleTimerDisabled:false];
        
        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
    }
    else {
        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ILLEGAL_ACCESS_EXCEPTION messageAsString:@"IdleTimer not disabled"];
    }
    
	[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
@end

I try:

ionic cordova plugin rm at.gofg.sportscomputer.powermanagement
ionic cordova plugin add https://github.com/Viras-/cordova-plugin-powermanagement.git

but im still receiving this problem

Captura de Tela 2019-03-22 aฬ€s 09 26 59

iOS powerManagement.dim

Is the Partial Wake Lock (dim only) supposed to work on iOS? It acquires the lock but the screen never diims. The full wake lock works perfectly. Am I doing something wrong?

Thanks

npm ERR! 404 'at.gofg.sportscomputer.powermanagement' is not in the npm registry.

Hello there,
while trying to build my project for IOS, I get the following error. Did someone had the similar issue?
Error:
code E404
npm ERR! 404 Registry returned 404 for GET on https://registry.npmjs.org/at.gofg.sportscomputer.powermanagement
npm ERR! 404
npm ERR! 404 'at.gofg.sportscomputer.powermanagement' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

Screen dims after 3 minutes on android (Samsung Galaxy S9)

I installed the plugin with

cordova plugin add cordova-plugin-powermanagement

I added this to my code:

    document.addEventListener("deviceready", onDeviceReady, false);

    function onDeviceReady() {
        window.powerManagement.acquire(function(){}, function(){});
    }

When running this app, the screen dims after 3 minutes.

Issue : ClassNotFound Exception

If I add "
<script type="text/javascript" charset="utf-8" src="js/powermanagement.js"></script>
"
I can call PowerManager.prototype.acquire(success, fail, false)
But fail(error) was called and alert(error) says "ClassNotFoundException".

If i dont add this, with this code :
try {
alert(window.plugin);
PowerManagement.prototype.acquire(successWakeAcquired, errorWakeAcquired, false);
}
catch (e) {
alert(e);
}

I have this : "ReferenceError: PowerManager is not defined.
What's the problem ? please make a tuto/doc ?
thanks

Coco

Question: is it also possible to keep WIFI always on with this plugin?

Hi,
on some devices the WIFI is stopped after some time of inactivity.
My app fetches data periodically from a server so it is important to keep the connection alive.
Would it be possible to use this plugin to keep the WIFI 'wakelocked' so that the connection will be kept alive at all times?

usage example?

I'm quite new to Cordova - Your plugin could be very useful. I successfully installed the plugin but I don't understand how to use it. Could you give some usage instructions (some examples how to call it from JS) ? Many thanks.

I modified your plugin to enable android's PARTIAL_WAKELOCK

PARTIAL_WAKE_LOCK Wake lock level: Ensures that the CPU is running; the screen and keyboard backlight will be allowed to go off : If the user presses the power button, then the screen will be turned off but the CPU will be kept on until all partial wake locks have been released.

I was wondering if there was a reason you did not provide for this mode in the first place.... I tought this was a very useful in some instances... Is there an issue I might not be aware of? Something that made you not to include this choice with your powermanagement plugin?

Please write a doc !

Hi,
Can you write a doc please, because I dont know how to use it.
Thanks

Coco

Error: exec proxy not found for :: PowerManagement

Great work!

On windows, when I launch it via cordova-electron build, i get Error: exec proxy not found for :: PowerManagement error.

The powermanagmentproxy.js exist in the plugin folder

any help would be appreciated.

thanks

Phone Wake Up

I have successfully integrated the plugin in my cordova app and am able to capture the acquire and release events but the issue I am facing is this. When I acquire the wakelock my phone is not active , its locked but the app is running so when from another phone I try to send the socket.io event , the acquire even is called but the phone does not wake up. How can I make sure that the phone wakes up when the acquire event is fired.

windows support

Great plugin but without windows support (only windows phone). I'd like to change that. See my pull request

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.