Skip to content

Commit bf3fb19

Browse files
author
Viras-
committed
- Added PowerManagement plugin for Qt port of PhoneGap
1 parent 4415249 commit bf3fb19

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "powermanagement.h"
2+
3+
PowerManagement::PowerManagement(QWebFrame *p_webFrame) :
4+
PGPlugin(p_webFrame)
5+
{
6+
m_systemScreenSaver = new QSystemScreenSaver();
7+
}
8+
9+
void PowerManagement::release( int scId, int ecId ) {
10+
m_systemScreenSaver->setScreenSaverInhibited(false);
11+
12+
// Check if everything went fine
13+
if( !m_systemScreenSaver->screenSaverInhibited() ) {
14+
this->callback( scId, "" );
15+
}
16+
else {
17+
this->callback( ecId, "" );
18+
}
19+
}
20+
21+
void PowerManagement::acquire( int scId, int ecId ) {
22+
m_systemScreenSaver->setScreenSaverInhibited(true);
23+
24+
// Check if everything went fine
25+
if( m_systemScreenSaver->screenSaverInhibited() ) {
26+
this->callback( scId, "" );
27+
}
28+
else {
29+
this->callback( ecId, "" );
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef POWERMANAGEMENT_H
2+
#define POWERMANAGEMENT_H
3+
4+
#include "../pgplugin.h"
5+
6+
#include <QSystemScreenSaver>
7+
8+
QTM_USE_NAMESPACE
9+
10+
class PowerManagement : public PGPlugin
11+
{
12+
Q_OBJECT
13+
public:
14+
explicit PowerManagement(QWebFrame *p_webFrame);
15+
16+
signals:
17+
18+
public slots:
19+
void release( int scId, int ecId );
20+
void acquire( int scId, int ecId );
21+
22+
private:
23+
QSystemScreenSaver *m_systemScreenSaver;
24+
};
25+
26+
#endif // POWERMANAGEMENT_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)