File indexing completed on 2024-04-21 03:48:35

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2015 Dennis Nienhüser <nienhueser@kde.org>
0004 //
0005 
0006 #include "MarbleMaps.h"
0007 #include <FileManager.h>
0008 
0009 #include <MarbleModel.h>
0010 #include <QGuiApplication>
0011 
0012 #ifdef Q_OS_ANDROID
0013 #include <QtAndroid>
0014 #include <QAndroidJniObject>
0015 #include <qandroidfunctions.h>
0016 #endif
0017 
0018 namespace Marble {
0019 
0020 MarbleMaps::MarbleMaps(QQuickItem *parent) :
0021     MarbleQuickItem(parent),
0022     m_suspended(false),
0023     m_keepScreenOn(false)
0024 {
0025     QGuiApplication* application = qobject_cast<QGuiApplication*>(QGuiApplication::instance());
0026     if (application) {
0027         connect(application, SIGNAL(applicationStateChanged(Qt::ApplicationState)),
0028                 this, SLOT(handleApplicationStateChange(Qt::ApplicationState)));
0029     }
0030 
0031 #ifdef Q_OS_ANDROID
0032     QAndroidJniObject const activity = QtAndroid::androidActivity();
0033     if (activity.isValid()) {
0034         // Control music volume
0035         int const STREAM_MUSIC = 3;
0036         activity.callMethod<void>("setVolumeControlStream", "(I)V", STREAM_MUSIC);
0037 
0038         // If a file is passed, open it. Possible file types are registered in package/AndroidManifest.xml
0039         QAndroidJniObject const intent = activity.callObjectMethod("getIntent", "()Landroid/content/Intent;");
0040         if (intent.isValid()) {
0041             QAndroidJniObject const data = intent.callObjectMethod("getData", "()Landroid/net/Uri;");
0042             if (data.isValid()) {
0043                 QAndroidJniObject const path = data.callObjectMethod("getPath", "()Ljava/lang/String;");
0044                 if (path.isValid()) {
0045                     model()->addGeoDataFile(path.toString());
0046                     connect( model()->fileManager(), SIGNAL(centeredDocument(GeoDataLatLonBox)), this, SLOT(centerOn(GeoDataLatLonBox)) );
0047                 }
0048             }
0049         }
0050     }
0051 #endif
0052 }
0053 
0054 bool MarbleMaps::isSuspended() const
0055 {
0056     return m_suspended;
0057 }
0058 
0059 bool MarbleMaps::keepScreenOn() const
0060 {
0061     return m_keepScreenOn;
0062 }
0063 
0064 void MarbleMaps::setKeepScreenOn(bool screenOn)
0065 {
0066     if (m_keepScreenOn == screenOn) {
0067         return;
0068     }
0069     m_keepScreenOn = screenOn;
0070     char const * const action = m_keepScreenOn ? "addFlags" : "clearFlags";
0071 #ifdef Q_OS_ANDROID
0072     QtAndroid::runOnAndroidThread([action](){
0073     QAndroidJniObject activity = QtAndroid::androidActivity();
0074     if (activity.isValid()) {
0075         QAndroidJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
0076         if (window.isValid()) {
0077             const int FLAG_KEEP_SCREEN_ON = 128;
0078             window.callMethod<void>(action, "(I)V", FLAG_KEEP_SCREEN_ON);
0079         }
0080     }});
0081 #else
0082     Q_UNUSED(action);
0083 #endif
0084     emit keepScreenOnChanged(screenOn);
0085 }
0086 
0087 void MarbleMaps::handleApplicationStateChange(Qt::ApplicationState state)
0088 {
0089     if (state == Qt::ApplicationSuspended) {
0090         m_suspended = true;
0091         emit isSuspendedChanged(m_suspended);
0092     } else if (state == Qt::ApplicationActive) {
0093         m_suspended = false;
0094         emit isSuspendedChanged(m_suspended);
0095     }
0096 }
0097 
0098 }
0099 
0100 #include "moc_MarbleMaps.cpp"