-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathScreenLocker.cpp
47 lines (37 loc) · 1.48 KB
/
ScreenLocker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <QAndroidJniObject>
#include <QtDebug>
#include "jni.h"
#include "ScreenLocker.h"
ScreenLocker::ScreenLocker()
{
QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod( "org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;" );
if( activity.isValid() )
{
QAndroidJniObject serviceName = QAndroidJniObject::getStaticObjectField<jstring>( "android/content/Context", "POWER_SERVICE" );
if( serviceName.isValid() )
{
QAndroidJniObject powerMgr = activity.callObjectMethod( "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;", serviceName.object<jobject>() );
if ( powerMgr.isValid() )
{
jint levelAndFlags = QAndroidJniObject::getStaticField<jint>( "android/os/PowerManager", "SCREEN_DIM_WAKE_LOCK" );
QAndroidJniObject tag = QAndroidJniObject::fromString( "My Tag" );
m_screenLock = powerMgr.callObjectMethod( "newWakeLock", "(ILjava/lang/String;)Landroid/os/PowerManager$WakeLock;", levelAndFlags, tag.object<jstring>() );
}
}
}
if( m_screenLock.isValid() )
{
m_screenLock.callMethod<void>( "acquire", "()V" );
qInfo() << "Screen stay on enabled";
}
else
assert( false );
}
ScreenLocker::~ScreenLocker()
{
if ( m_screenLock.isValid() )
{
m_screenLock.callMethod<void>( "release", "()V" );
qInfo() << "Screen stay on disabled";
}
}