Selaa lähdekoodia

keep screen on

Laserlicht 1 kuukausi sitten
vanhempi
sitoutus
2048806753

+ 2 - 0
launcher/firstLaunch/firstlaunch_moc.cpp

@@ -443,10 +443,12 @@ void FirstLaunchView::extractGogDataAsync(QString filePathBin, QString filePathE
 	if(errorText.isEmpty())
 	{
 		logGlobal->info("Performing extraction using innoextract...");
+		Helper::keepScreenOn(true);
 		errorText = Innoextract::extract(tmpFileExe, tempDir.path(), [this](float progress) {
 			ui->progressBarGog->setValue(progress * 100);
 			qApp->processEvents();
 		});
+		Helper::keepScreenOn(false);
 		logGlobal->info("Extraction done!");
 	}
 

+ 29 - 0
launcher/helper.cpp

@@ -22,6 +22,7 @@
 #ifdef VCMI_ANDROID
 #include <QAndroidJniObject>
 #include <QtAndroid>
+#include <QAndroidJniEnvironment>
 #endif
 
 #ifdef VCMI_IOS
@@ -114,4 +115,32 @@ MainWindow * getMainWindow()
 			return mainWin;
 	return nullptr;
 }
+
+
+void keepScreenOn(bool on)
+{
+#if defined(VCMI_ANDROID)
+	// based on https://stackoverflow.com/a/38846485
+	QtAndroid::runOnAndroidThread([on]
+	{
+		QAndroidJniObject activity = QtAndroid::androidActivity();
+		if(activity.isValid())
+		{
+			QAndroidJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
+
+			if(window.isValid())
+			{
+				const int FLAG_KEEP_SCREEN_ON = 128;
+				if(on)
+					window.callMethod<void>("addFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
+				else
+					window.callMethod<void>("clearFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
+			}
+		}
+		QAndroidJniEnvironment env;
+		if (env->ExceptionCheck())
+			env->ExceptionClear();
+	});
+#endif
+}
 }

+ 1 - 0
launcher/helper.h

@@ -23,4 +23,5 @@ QString getRealPath(QString path);
 void performNativeCopy(QString src, QString dst);
 void revealDirectoryInFileBrowser(QString path);
 MainWindow * getMainWindow();
+void keepScreenOn(bool on);
 }

+ 3 - 0
launcher/modManager/cmodlistview_moc.cpp

@@ -798,6 +798,7 @@ void CModListView::downloadFile(QString file, QUrl url, QString description, qin
 
 void CModListView::downloadProgress(qint64 current, qint64 max)
 {
+	Helper::keepScreenOn(true);
 	// display progress, in megabytes
 	ui->progressBar->setVisible(true);
 	ui->progressBar->setMaximum(max / (1024 * 1024));
@@ -806,6 +807,7 @@ void CModListView::downloadProgress(qint64 current, qint64 max)
 
 void CModListView::extractionProgress(qint64 current, qint64 max)
 {
+	Helper::keepScreenOn(true);
 	// display progress, in extracted files
 	ui->progressBar->setVisible(true);
 	ui->progressBar->setMaximum(max);
@@ -857,6 +859,7 @@ void CModListView::downloadFinished(QStringList savedFiles, QStringList failedFi
 
 void CModListView::hideProgressBar()
 {
+	Helper::keepScreenOn(false);
 	if(dlManager == nullptr) // it was not recreated meanwhile
 	{
 		ui->progressWidget->setVisible(false);