From ead14cd61da071ac10e7a83b9c1d05098cf2a679 Mon Sep 17 00:00:00 2001
From: Joona Petrell <joona.t.petrell@nokia.com>
Date: Mon, 6 Sep 2010 18:04:13 +1000
Subject: Pause orientation sensors in Qml Viewer when the application window
 is not active

Task-number: QTBUG-13347
Reviewed-by: Martin Jones
---
 tools/qml/deviceorientation.cpp         |  5 ++++
 tools/qml/deviceorientation.h           |  3 ++
 tools/qml/deviceorientation_maemo5.cpp  | 53 +++++++++++++++++++++++----------
 tools/qml/deviceorientation_symbian.cpp | 27 ++++++++++++++++-
 tools/qml/qmlruntime.cpp                |  2 ++
 5 files changed, 73 insertions(+), 17 deletions(-)

diff --git a/tools/qml/deviceorientation.cpp b/tools/qml/deviceorientation.cpp
index e7c70d5..d3014ad 100644
--- a/tools/qml/deviceorientation.cpp
+++ b/tools/qml/deviceorientation.cpp
@@ -53,6 +53,11 @@ public:
         return m_orientation;
     }
 
+    void pauseListening() {
+    }
+    void resumeListening() {
+    }
+
     void setOrientation(Orientation o) {
         if (o != m_orientation) {
             m_orientation = o;
diff --git a/tools/qml/deviceorientation.h b/tools/qml/deviceorientation.h
index 817bfc8..487ebd4 100644
--- a/tools/qml/deviceorientation.h
+++ b/tools/qml/deviceorientation.h
@@ -63,6 +63,9 @@ public:
     virtual Orientation orientation() const = 0;
     virtual void setOrientation(Orientation) = 0;
 
+    virtual void pauseListening() = 0;
+    virtual void resumeListening() = 0;
+
     static DeviceOrientation *instance();
 
 signals:
diff --git a/tools/qml/deviceorientation_maemo5.cpp b/tools/qml/deviceorientation_maemo5.cpp
index e942579..a324820 100644
--- a/tools/qml/deviceorientation_maemo5.cpp
+++ b/tools/qml/deviceorientation_maemo5.cpp
@@ -50,23 +50,9 @@ class MaemoOrientation : public DeviceOrientation
     Q_OBJECT
 public:
     MaemoOrientation()
-        : o(UnknownOrientation)
+        : o(UnknownOrientation), sensorEnabled(false)
     {
-        // enable the orientation sensor
-        QDBusConnection::systemBus().call(
-                QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,
-                                               MCE_REQUEST_IF, MCE_ACCELEROMETER_ENABLE_REQ));
-
-        // query the initial orientation
-        QDBusMessage reply = QDBusConnection::systemBus().call(
-                QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,
-                                               MCE_REQUEST_IF, MCE_DEVICE_ORIENTATION_GET));
-        if (reply.type() == QDBusMessage::ErrorMessage) {
-            qWarning("Unable to retrieve device orientation: %s", qPrintable(reply.errorMessage()));
-        } else {
-            o = toOrientation(reply.arguments().value(0).toString());
-        }
-
+        resumeListening();
         // connect to the orientation change signal
         QDBusConnection::systemBus().connect(QString(), MCE_SIGNAL_PATH, MCE_SIGNAL_IF,
                 MCE_DEVICE_ORIENTATION_SIG,
@@ -91,6 +77,40 @@ public:
     {
     }
 
+    void pauseListening() {
+        if (sensorEnabled) {
+            // disable the orientation sensor
+            QDBusConnection::systemBus().call(
+                    QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,
+                                                   MCE_REQUEST_IF, MCE_ACCELEROMETER_DISABLE_REQ));
+            sensorEnabled = false;
+        }
+    }
+
+    void resumeListening() {
+        if (!sensorEnabled) {
+            // enable the orientation sensor
+            QDBusConnection::systemBus().call(
+                    QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,
+                                                   MCE_REQUEST_IF, MCE_ACCELEROMETER_ENABLE_REQ));
+
+            QDBusMessage reply = QDBusConnection::systemBus().call(
+                    QDBusMessage::createMethodCall(MCE_SERVICE, MCE_REQUEST_PATH,
+                                                   MCE_REQUEST_IF, MCE_DEVICE_ORIENTATION_GET));
+
+            if (reply.type() == QDBusMessage::ErrorMessage) {
+                qWarning("Unable to retrieve device orientation: %s", qPrintable(reply.errorMessage()));
+            } else {
+                Orientation orientation = toOrientation(reply.arguments().value(0).toString());
+                if (o != orientation) {
+                    o = orientation;
+                    emit orientationChanged();
+                }
+                sensorEnabled = true;
+            }
+        }
+    }
+
 private Q_SLOTS:
     void deviceOrientationChanged(const QString &newOrientation)
     {
@@ -116,6 +136,7 @@ private:
 
 private:
     Orientation o;
+    bool sensorEnabled;
 };
 
 DeviceOrientation* DeviceOrientation::instance()
diff --git a/tools/qml/deviceorientation_symbian.cpp b/tools/qml/deviceorientation_symbian.cpp
index 307c417..7710cf9 100644
--- a/tools/qml/deviceorientation_symbian.cpp
+++ b/tools/qml/deviceorientation_symbian.cpp
@@ -52,7 +52,7 @@ class SymbianOrientation : public DeviceOrientation, public MSensrvDataListener
     Q_OBJECT
 public:
     SymbianOrientation()
-        : DeviceOrientation(), m_current(UnknownOrientation), m_sensorChannel(0)
+        : DeviceOrientation(), m_current(UnknownOrientation), m_sensorChannel(0), m_channelOpen(false)
     {
         TRAP_IGNORE(initL());
         if (!m_sensorChannel)
@@ -84,6 +84,7 @@ public:
                 TRAP(error, m_sensorChannel->OpenChannelL());
             if (!error) {
                 TRAP(error, m_sensorChannel->StartDataListeningL(this, 1, 1, 0));
+                m_channelOpen = true;
                 break;
            }
             if (error) {
@@ -107,6 +108,30 @@ public:
 private:
     DeviceOrientation::Orientation m_current;
     CSensrvChannel *m_sensorChannel;
+    bool m_channelOpen;
+    void pauseListening() {
+        if (m_sensorChannel && m_channelOpen) {
+            m_sensorChannel->StopDataListening();
+            m_sensorChannel->CloseChannel();
+            m_channelOpen = false;
+        }
+    }
+
+    void resumeListening() {
+        if (m_sensorChannel && !m_channelOpen) {
+            TRAPD(error, m_sensorChannel->OpenChannelL());
+            if (!error) {
+                TRAP(error, m_sensorChannel->StartDataListeningL(this, 1, 1, 0));
+                if (!error) {
+                    m_channelOpen = true;
+                }
+            }
+            if (error) {
+                delete m_sensorChannel;
+                m_sensorChannel = 0;
+            }
+        }
+    }
 
     void DataReceived(CSensrvChannel &channel, TInt count, TInt dataLost)
     {
diff --git a/tools/qml/qmlruntime.cpp b/tools/qml/qmlruntime.cpp
index 321b7fd..b38e80d 100644
--- a/tools/qml/qmlruntime.cpp
+++ b/tools/qml/qmlruntime.cpp
@@ -1208,8 +1208,10 @@ bool QDeclarativeViewer::event(QEvent *event)
 {
     if (event->type() == QEvent::WindowActivate) {
         Runtime::instance()->setActiveWindow(true);
+        DeviceOrientation::instance()->resumeListening();
     } else if (event->type() == QEvent::WindowDeactivate) {
         Runtime::instance()->setActiveWindow(false);
+        DeviceOrientation::instance()->pauseListening();
     }
     return QWidget::event(event);
 }
-- 
cgit v0.12