tools/qml/deviceorientation_maemo5.cpp
changeset 37 758a864f9613
parent 33 3e2da88830cd
--- a/tools/qml/deviceorientation_maemo5.cpp	Fri Sep 17 08:34:18 2010 +0300
+++ b/tools/qml/deviceorientation_maemo5.cpp	Mon Oct 04 01:19:32 2010 +0300
@@ -50,23 +50,9 @@
     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 @@
     {
     }
 
+    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:
     Orientation o;
+    bool sensorEnabled;
 };
 
 DeviceOrientation* DeviceOrientation::instance()