|
| 1 | +/* |
| 2 | + * PhoneGap is available under *either* the terms of the modified BSD license *or* the |
| 3 | + * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text. |
| 4 | + * |
| 5 | + * Copyright (c) 2011, Wolfgang Koller - http://www.gofg.at/ |
| 6 | + */ |
| 7 | + |
| 8 | +function PowermanagementError() { |
| 9 | +}; |
| 10 | + |
| 11 | +PowermanagementError.cast = function( p_code, p_message ) { |
| 12 | + var powerManagementError = new PowermanagementError(); |
| 13 | + powerManagementError.code = p_code; |
| 14 | + powerManagementError.message = p_message; |
| 15 | + |
| 16 | + return powerManagementError; |
| 17 | +}; |
| 18 | + |
| 19 | +PowermanagementError.ALREADY_ACTIVE = 1; |
| 20 | +PowermanagementError.NOT_SUPPORTED = 2; |
| 21 | +PowermanagementError.NOT_ACTIVE = 3; |
| 22 | + |
| 23 | +PowermanagementError.prototype.code = 0; |
| 24 | +PowermanagementError.prototype.message = ""; |
| 25 | + |
| 26 | +function Powermanagement() { |
| 27 | +}; |
| 28 | + |
| 29 | +Powermanagement.prototype.acquired = false; |
| 30 | + |
| 31 | +Powermanagement.prototype.acquire = function( successCallback, errorCallback ) { |
| 32 | + if( this.acquired ) { |
| 33 | + errorCallback( PowermanagementError.cast( PowermanagementError.ALREADY_ACTIVE, "Powermanagement lock already active." ) ); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + var me = this; |
| 38 | + PhoneGap.exec( function() { |
| 39 | + me.acquired = true; |
| 40 | + successCallback(); |
| 41 | + }, function() { |
| 42 | + me.acquired = false; |
| 43 | + errorCallback( PowermanagementError.cast( PowermanagementError.NOT_SUPPORTED, "Powermanagement not supported." ) ); |
| 44 | + }, "com.phonegap.plugin.Powermanagement", "acquire", [] ); |
| 45 | +}; |
| 46 | + |
| 47 | +Powermanagement.prototype.release = function( successCallback, errorCallback ) { |
| 48 | + if( !this.acquired ) { |
| 49 | + errorCallback( PowermanagementError.cast( PowermanagementError.NOT_ACTIVE, "Powermanagement lock not active." ) ); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + var me = this; |
| 54 | + PhoneGap.exec( function() { |
| 55 | + me.acquired = false; |
| 56 | + successCallback(); |
| 57 | + }, function() { |
| 58 | + me.acquired = false; |
| 59 | + errorCallback( PowermanagementError.cast( PowermanagementError.NOT_SUPPORTED, "Powermanagement not supported." ) ); |
| 60 | + }, "com.phonegap.plugin.Powermanagement", "release", [] ); |
| 61 | +}; |
| 62 | + |
| 63 | +Powermanagement.prototype.dim = function( successCallback, errorCallback ) { |
| 64 | + if( this.acquired ) { |
| 65 | + errorCallback( PowermanagementError.cast( PowermanagementError.ALREADY_ACTIVE, "Powermanagement lock already active." ) ); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + var me = this; |
| 70 | + if( !PhoneGap.exec( function() { |
| 71 | + me.acquired = true; |
| 72 | + successCallback(); |
| 73 | + }, function() { |
| 74 | + me.acquired = false; |
| 75 | + errorCallback( PowermanagementError.cast( PowermanagementError.NOT_SUPPORTED, "Powermanagement not supported." ) ); |
| 76 | + }, "com.phonegap.plugin.Powermanagement", "dim", [] ) ) { |
| 77 | + |
| 78 | + // If dimming doesn't work, try to acquire a full wake lock |
| 79 | + this.acquire( successCallback, errorCallback ); |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +/** |
| 84 | + * Create Powermanagement instance |
| 85 | + */ |
| 86 | +PhoneGap.addConstructor( "com.phonegap.plugin.Powermanagement", function () { |
| 87 | + window.Powermanagement = new Powermanagement(); |
| 88 | + window.PowerManagement = window.Powermanagement; // Alias for backwards compatibility |
| 89 | +} ); |
0 commit comments