simple_mtglife

diff main.cc @ 6:9e3e14ed98eb

android: keep from sleeping while the app is active
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 16 Feb 2015 05:46:07 +0200
parents 6321cfa2ad62
children
line diff
     1.1 --- a/main.cc	Mon Jan 26 12:04:33 2015 +0200
     1.2 +++ b/main.cc	Mon Feb 16 05:46:07 2015 +0200
     1.3 @@ -1,11 +1,37 @@
     1.4  #include "mainwin.h"
     1.5  #include <QApplication>
     1.6  
     1.7 +static void keep_screen_on();
     1.8 +
     1.9  int main(int argc, char *argv[])
    1.10  {
    1.11  	QApplication a(argc, argv);
    1.12  	MainWin w;
    1.13  	w.show();
    1.14  
    1.15 +    keep_screen_on();
    1.16 +
    1.17  	return a.exec();
    1.18  }
    1.19 +
    1.20 +#ifdef ANDROID
    1.21 +#include <QtAndroid>
    1.22 +#include <QAndroidJniObject>
    1.23 +
    1.24 +static void keep_screen_on()
    1.25 +{
    1.26 +    QAndroidJniObject activity = QtAndroid::androidActivity();
    1.27 +    if(activity.isValid()) {
    1.28 +        QAndroidJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
    1.29 +
    1.30 +        if(window.isValid()) {
    1.31 +            const int FLAG_KEEP_SCREEN_ON = 128;
    1.32 +            window.callObjectMethod("addFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
    1.33 +        }
    1.34 +    }
    1.35 +}
    1.36 +#else
    1.37 +static void keep_screen_on()
    1.38 +{
    1.39 +}
    1.40 +#endif