summaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/b2qtdevice.cpp (renamed from src/utils/qdroidutils.cpp)372
-rw-r--r--src/utils/b2qtdevice.h59
-rw-r--r--src/utils/qdroidutils.h83
-rw-r--r--src/utils/utils.pro16
4 files changed, 290 insertions, 240 deletions
diff --git a/src/utils/qdroidutils.cpp b/src/utils/b2qtdevice.cpp
index 379fc20..c59859c 100644
--- a/src/utils/qdroidutils.cpp
+++ b/src/utils/b2qtdevice.cpp
@@ -16,7 +16,7 @@
** the contact form at http://qt.digia.com/
**
****************************************************************************/
-#include "qdroidutils.h"
+#include "b2qtdevice.h"
#include <unistd.h>
#include <QDebug>
#include <math.h>
@@ -32,61 +32,166 @@
#include <utils/String8.h>
#endif
+// When we can't query directly, at least remember what we have set it to
+static quint8 knownBrightness = 255;
+
+B2QtDevice::B2QtDevice(QObject *parent)
+ : QObject(parent)
+{
+}
+
+B2QtDevice::~B2QtDevice()
+{
+}
+
/*!
* Reboots the system. Does not return.
*
- * \sa powerOffSystem()
+ * \sa powerOff()
*/
-void QDroidUtils::rebootSystem()
+void B2QtDevice::reboot()
{
sync();
- reboot(RB_AUTOBOOT);
+ ::reboot(RB_AUTOBOOT);
qWarning("reboot returned");
}
+
/*!
* Shuts down the system. Does not return.
*
- * \sa rebootSystem()
+ * \sa reboot()
*/
-void QDroidUtils::powerOffSystem()
+void B2QtDevice::powerOff()
{
sync();
- reboot(RB_POWER_OFF);
+ ::reboot(RB_POWER_OFF);
qWarning("powerOff returned");
}
-void QDroidUtils::setOrientationForAudioSystem(AudioOrientation orientation)
+
+/*!
+ * Sets the display brightness (i.e. the intensity of the backlight)
+ * to \a value. A value of 255 requests maximum brightness, while 0 requests
+ * minimum (typically, the backlight turned off).
+ *
+ * Returns true on success.
+ */
+bool B2QtDevice::setDisplayBrightness(quint8 value)
{
#ifdef Q_OS_ANDROID_NO_SDK
- QString orientationString = QStringLiteral("undefined");
- switch (orientation) {
- case LandscapeAudioOrientation:
- orientationString = QStringLiteral("landscape");
- break;
- case PortraitAudioOrientation:
- orientationString = QStringLiteral("portrait");
- break;
- case SquareAudioOrientation:
- orientationString = QStringLiteral("square");
- break;
- default:
- break;
+ const struct hw_module_t* module = 0;
+ if (hw_get_module(LIGHTS_HARDWARE_MODULE_ID, &module))
+ return false;
+ if (!module || !module->methods || !module->methods->open)
+ return false;
+
+ struct light_device_t* device = 0;
+ if (module->methods->open(module, LIGHT_ID_BACKLIGHT, (struct hw_device_t**)&device))
+ return false;
+ if (!device || !device->set_light || !device->common.close)
+ return false;
+
+ struct light_state_t state;
+ memset(&state, 0, sizeof(light_state_t));
+ state.color = 0xff000000 | (value << 16) | (value << 8) | value;
+ if (!device->set_light(device, &state))
+ return false;
+
+ device->common.close(&device->common);
+ knownBrightness = value;
+ emit displayBrightnessChanged(value);
+ return true;
+#else
+ return false;
+#endif
+}
+
+
+/*!
+ * Returns the current backlight intensity.
+ * \sa setDisplayBrightness
+ */
+quint8 B2QtDevice::displayBrightness() const
+{
+ QFile sysFile(QStringLiteral("/sys/class/leds/lcd-backlight/brightness"));
+ if (sysFile.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) {
+ bool ok = false;
+ int sysVal = sysFile.read(3).simplified().toInt(&ok);
+ if (ok)
+ knownBrightness = qBound(0, sysVal, 255);
}
- android::AudioSystem::setParameters(0, android::String8(QStringLiteral("orientation=%2")
- .arg(orientationString).toLatin1().constData()));
+ return knownBrightness;
+}
+
+
+/*!
+ * Gets the current IP address(es) of the device
+ */
+QString B2QtDevice::getIPAddress() const
+{
+ QStringList addresses;
+ foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()) {
+ QNetworkInterface::InterfaceFlags flags = interface.flags();
+ if (flags.testFlag(QNetworkInterface::IsRunning) && !flags.testFlag(QNetworkInterface::IsLoopBack)) {
+ foreach (const QNetworkAddressEntry &entry, interface.addressEntries())
+ addresses.append(entry.ip().toString().split('%').first());
+ }
+ }
+ return addresses.join(QStringLiteral(", "));
+}
+
+
+/*!
+ * Gets the current hostname of the device
+ */
+QString B2QtDevice::hostname() const
+{
+ QString name;
+#ifdef Q_OS_ANDROID_NO_SDK
+ char prop_value[PROPERTY_VALUE_MAX];
+ int len = property_get("net.hostname", prop_value, 0);
+ if (len)
+ name = QString::fromLocal8Bit(prop_value, len);
+#else
+ name = QHostInfo::localHostName();
#endif
+ return name;
}
+
+/*!
+ * Sets new hostname for the device
+ */
+bool B2QtDevice::setHostname(const QString &name)
+{
+#ifdef Q_OS_ANDROID_NO_SDK
+ property_set("net.hostname", name.toLocal8Bit().constData());
+#else
+ QByteArray lname = name.toLocal8Bit();
+ if (::sethostname(lname.constData(), lname.length())) {
+ qWarning("Could not set system hostname");
+ return false;
+ }
+ // Also store it for next boot:
+ QFile file(QStringLiteral("/etc/hostname"));
+ if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
+ qWarning("Could not write to /etc/hostname");
+ return false;
+ }
+ file.write(lname.append('\n'));
+ file.close();
+#endif
+ emit hostnameChanged(name);
+ return true;
+}
+
+
/*!
* Sets the master volume to \a volume.
* The volume can range from 0 to 100 and is linear.
- * Changing the master volume will affect all audio streams.
- *
- * \sa setStreamVolume()
- * \sa setMasterMute()
*/
-void QDroidUtils::setMasterVolume(int volume)
+void B2QtDevice::setMasterVolume(int volume)
{
#ifdef Q_OS_ANDROID_NO_SDK
android::status_t rc;
@@ -94,28 +199,57 @@ void QDroidUtils::setMasterVolume(int volume)
rc = android::AudioSystem::setMasterVolume(android::AudioSystem::linearToLog(volume));
if (rc != android::NO_ERROR)
qWarning() << Q_FUNC_INFO << "Error while setting audio properties.";
+ else
+ emit masterVolumeChanged(volume);
+#else
+ Q_UNUSED(volume)
#endif
}
+
/*!
- * Sets the master mute to \a mute. Setting it to true will disable all
- * sounds on the device.
- *
- * \sa setMasterVolume()
- * \sa setStreamMute()
+ * Returns the current master volume.
+ * The volume can range from 0 to 100 and is linear.
*/
-void QDroidUtils::setMasterMute(bool mute)
+int B2QtDevice::masterVolume() const
{
+ float volume = 0;
#ifdef Q_OS_ANDROID_NO_SDK
android::status_t rc;
- rc = android::AudioSystem::setMasterMute(mute);
+ rc = android::AudioSystem::getMasterVolume(&volume);
if (rc != android::NO_ERROR)
- qWarning() << Q_FUNC_INFO << "Error while setting audio properties.";
+ qWarning() << Q_FUNC_INFO << "Error while getting audio properties.";
#endif
+ return qBound(0, qRound(volume), 100);
}
-/*!
- \enum QDroidUtils::AudioStreamType
+#ifdef Q_OS_ANDROID_NO_SDK
+// Android audio handling
+
+enum AudioOrientation {
+ LandscapeAudioOrientation,
+ PortraitAudioOrientation,
+ SquareAudioOrientation,
+ UndefinedAudioOrientation,
+};
+
+enum AudioStreamType {
+ DefaultAudioStream = -1,
+ VoiceCallAudioStream = 0,
+ SystemAudioStream = 1,
+ RingAudioStream = 2,
+ MusicAudioStream = 3,
+ AlarmAudioStream = 4,
+ NotificationAudioStream = 5,
+ BluetoothAudioStream = 6,
+ EnforcedAudibleAudioStream = 7,
+ DTMFAudioStream = 8,
+ TTSAudioStream = 9
+};
+
+
+/*
+ \enum AudioStreamType
\value DefaultAudioStream
The default audio stream
@@ -147,7 +281,7 @@ void QDroidUtils::setMasterMute(bool mute)
The audio stream for text-to-speech
*/
-/*!
+/*
* Sets the volume for a specific audio \a stream type to \a volume.
* The volume can range from 0 to 100 and is linear.
* All streams of the specified type will be affected.
@@ -155,172 +289,114 @@ void QDroidUtils::setMasterMute(bool mute)
* \sa setMasterVolume()
* \sa setStreamMute()
*/
-void QDroidUtils::setStreamVolume(AudioStreamType streamType, int volume)
+void setStreamVolume(AudioStreamType streamType, int volume)
{
-#ifdef Q_OS_ANDROID_NO_SDK
android::status_t rc;
volume = qBound(0, volume, 100);
rc = android::AudioSystem::setStreamVolume(audio_stream_type_t(streamType),
android::AudioSystem::linearToLog(volume), 0);
if (rc != android::NO_ERROR)
qWarning() << Q_FUNC_INFO << "Error while setting audio properties.";
-#endif
}
-/*!
+/*
* Mutes all audio \a streams of type \a streamType.
*
* \sa setStreamVolume()
* \sa setMasterMute()
*/
-void QDroidUtils::setStreamMute(AudioStreamType streamType, bool mute)
+void setStreamMute(AudioStreamType streamType, bool mute)
{
-#ifdef Q_OS_ANDROID_NO_SDK
android::status_t rc;
rc = android::AudioSystem::setStreamMute(audio_stream_type_t(streamType), mute);
if (rc != android::NO_ERROR)
qWarning() << Q_FUNC_INFO << "Error while setting audio properties.";
-#endif
}
-/*!
- * Sets the display brightness (i.e. the intensity of the backlight)
- * to \a value. A value of 255 requests maximum brightness, while 0 requests
- * minimum (typically, the backlight turned off).
- *
- * Returns true on success.
- */
-//### TBD: add the user/sensor setting as parameter!
-bool QDroidUtils::setDisplayBrightness(quint8 value)
+void setOrientationForAudioSystem(AudioOrientation orientation)
{
-#ifdef Q_OS_ANDROID_NO_SDK
- const struct hw_module_t* module = 0;
- if (hw_get_module(LIGHTS_HARDWARE_MODULE_ID, &module))
- return false;
- if (!module || !module->methods || !module->methods->open)
- return false;
-
- struct light_device_t* device = 0;
- if (module->methods->open(module, LIGHT_ID_BACKLIGHT, (struct hw_device_t**)&device))
- return false;
- if (!device || !device->set_light || !device->common.close)
- return false;
-
- struct light_state_t state;
- memset(&state, 0, sizeof(light_state_t));
- state.color = 0xff000000 | (value << 16) | (value << 8) | value;
- if (!device->set_light(device, &state))
- return false;
-
- device->common.close(&device->common);
-#else
- qDebug("QDroidUtils::setDisplayBrightness(%i)", value);
-#endif
- return true;
-}
-
-
-/*!
- * Gets the current IP address(es) of the device
- */
-QString QDroidUtils::getIPAddress()
-{
- QList<QNetworkInterface> availableInterfaces = QNetworkInterface::allInterfaces();
- if (availableInterfaces.length() > 0) {
- foreach (const QNetworkInterface &interface, availableInterfaces) {
- if (interface.flags() & QNetworkInterface::IsRunning
- && (interface.flags() & QNetworkInterface::IsLoopBack) == 0) {
- QList<QNetworkAddressEntry> entries = interface.addressEntries();
- QStringList addresses;
- foreach (const QNetworkAddressEntry &entry, entries)
- addresses.append(entry.ip().toString().split('%').first());
- return addresses.join(QStringLiteral(", "));
- }
- }
+ QString orientationString = QStringLiteral("undefined");
+ switch (orientation) {
+ case LandscapeAudioOrientation:
+ orientationString = QStringLiteral("landscape");
+ break;
+ case PortraitAudioOrientation:
+ orientationString = QStringLiteral("portrait");
+ break;
+ case SquareAudioOrientation:
+ orientationString = QStringLiteral("square");
+ break;
+ default:
+ break;
}
- return QString();
+ android::AudioSystem::setParameters(0, android::String8(QStringLiteral("orientation=%2")
+ .arg(orientationString).toLatin1().constData()));
}
-/*!
- * Gets the current hostname of the device
- */
-QString QDroidUtils::getHostname()
-{
- QString hostname;
-#ifdef Q_OS_ANDROID_NO_SDK
- char prop_value[PROPERTY_VALUE_MAX];
- int len = property_get("net.hostname", prop_value, 0);
- if (len)
- hostname = QString::fromLocal8Bit(prop_value, len);
-#else
- hostname = QHostInfo::localHostName();
-#endif
- return hostname;
-}
/*!
- * Sets new hostname for the device
+ * Sets the master mute to \a mute. Setting it to true will disable all
+ * sounds on the device.
+ *
+ * \sa setMasterVolume()
+ * \sa setStreamMute()
*/
-bool QDroidUtils::setHostname(QString hostname)
+void setMasterMute(bool mute)
{
-#ifdef Q_OS_ANDROID_NO_SDK
- property_set("net.hostname", hostname.toLocal8Bit().constData());
-#else
- QFile file("/etc/hostname");
- if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
- qWarning("Could not open hostname file");
- return false;
- }
- file.write(hostname.toLocal8Bit());
- file.close();
-#endif
- return true;
-}
-float QDroidUtils::masterVolume() const
-{
- float volume = NAN;
-#ifdef Q_OS_ANDROID_NO_SDK
android::status_t rc;
- rc = android::AudioSystem::getMasterVolume(&volume);
+ rc = android::AudioSystem::setMasterMute(mute);
if (rc != android::NO_ERROR)
- qWarning() << Q_FUNC_INFO << "Error while getting audio properties.";
-#endif
- return volume;
+ qWarning() << Q_FUNC_INFO << "Error while setting audio properties.";
}
-bool QDroidUtils::masterMute() const
+bool masterMute()
{
bool mute = false;
-#ifdef Q_OS_ANDROID_NO_SDK
android::status_t rc;
rc = android::AudioSystem::getMasterMute(&mute);
if (rc != android::NO_ERROR)
qWarning() << Q_FUNC_INFO << "Error while getting audio properties.";
-#endif
return mute;
}
-float QDroidUtils::streamVolume(AudioStreamType stream) const
+float streamVolume(AudioStreamType stream)
{
float volume = NAN;
-#ifdef Q_OS_ANDROID_NO_SDK
android::status_t rc;
rc = android::AudioSystem::getStreamVolume(audio_stream_type_t(stream), &volume, 0);
if (rc != android::NO_ERROR)
qWarning() << Q_FUNC_INFO << "Error while getting audio properties.";
-#endif
return volume;
}
-bool QDroidUtils::streamMute(AudioStreamType stream) const
+bool streamMute(AudioStreamType stream)
{
bool mute = false;
-#ifdef Q_OS_ANDROID_NO_SDK
android::status_t rc;
rc = android::AudioSystem::getStreamMute(audio_stream_type_t(stream), &mute);
if (rc != android::NO_ERROR)
qWarning() << Q_FUNC_INFO << "Error while getting audio properties.";
-#endif
return mute;
}
+
+#endif
+
+/*!
+ * Initializes the audio subsystem, setting the volume to max.
+ * This is done during system startup, so there is normally no need to call this function from applications.
+ */
+void B2QtDevice::initAudio()
+{
+#ifdef Q_OS_ANDROID_NO_SDK
+ // Set the audio orientation to something to force the HW driver to reconfigure
+ // audio routing (workaround for bug on Nexus 7)
+ setOrientationForAudioSystem(LandscapeAudioOrientation);
+ setMasterVolume(100);
+ setMasterMute(false);
+ setStreamVolume(SystemAudioStream, 100);
+ setStreamVolume(MusicAudioStream, 100);
+ setStreamVolume(NotificationAudioStream, 100);
+ setStreamVolume(EnforcedAudibleAudioStream, 100);
+#endif
+}
diff --git a/src/utils/b2qtdevice.h b/src/utils/b2qtdevice.h
new file mode 100644
index 0000000..fb3232a
--- /dev/null
+++ b/src/utils/b2qtdevice.h
@@ -0,0 +1,59 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc
+** All rights reserved.
+** For any questions to Digia, please use the contact form at
+** http://qt.digia.com/
+**
+** This file is part of Qt Enterprise Embedded.
+**
+** Licensees holding valid Qt Enterprise licenses may use this file in
+** accordance with the Qt Enterprise License Agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia.
+**
+** If you have questions regarding the use of this file, please use
+** the contact form at http://qt.digia.com/
+**
+****************************************************************************/
+#ifndef B2QTDEVICE_H
+#define B2QTDEVICE_H
+
+#include <qobject.h>
+
+class Q_DECL_EXPORT B2QtDevice : public QObject
+{
+ Q_OBJECT
+
+ Q_PROPERTY(quint8 displayBrightness READ displayBrightness WRITE setDisplayBrightness NOTIFY displayBrightnessChanged)
+ Q_PROPERTY(QString hostname READ hostname WRITE setHostname NOTIFY hostnameChanged)
+ Q_PROPERTY(QString ipAddress READ getIPAddress NOTIFY ipAddressChanged)
+ Q_PROPERTY(int masterVolume READ masterVolume WRITE setMasterVolume NOTIFY masterVolumeChanged)
+
+public:
+ B2QtDevice(QObject *parent = 0);
+ ~B2QtDevice();
+
+ quint8 displayBrightness() const;
+ QString hostname() const;
+ QString getIPAddress() const;
+ int masterVolume() const;
+
+ void initAudio();
+
+public Q_SLOTS:
+ void reboot();
+ void powerOff();
+
+ bool setDisplayBrightness(quint8 value);
+ bool setHostname(const QString &name);
+ void setMasterVolume(int volume);
+
+signals:
+ void displayBrightnessChanged(quint8 newValue);
+ void hostnameChanged(const QString &newName);
+ void ipAddressChanged(const QString &newAddress);
+ void masterVolumeChanged(int newVolume);
+};
+
+#endif // B2QTDEVICE_H
diff --git a/src/utils/qdroidutils.h b/src/utils/qdroidutils.h
deleted file mode 100644
index 8e70f19..0000000
--- a/src/utils/qdroidutils.h
+++ /dev/null
@@ -1,83 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2014 Digia Plc
-** All rights reserved.
-** For any questions to Digia, please use the contact form at
-** http://qt.digia.com/
-**
-** This file is part of Qt Enterprise Embedded.
-**
-** Licensees holding valid Qt Enterprise licenses may use this file in
-** accordance with the Qt Enterprise License Agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and Digia.
-**
-** If you have questions regarding the use of this file, please use
-** the contact form at http://qt.digia.com/
-**
-****************************************************************************/
-#ifndef QDROIDUTILS_H
-#define QDROIDUTILS_H
-
-#include <qobject.h>
-
-class Q_DECL_EXPORT QDroidUtils : public QObject
-{
- Q_OBJECT
- Q_ENUMS(AudioStreamType)
-public:
- enum AudioOrientation {
- LandscapeAudioOrientation,
- PortraitAudioOrientation,
- SquareAudioOrientation,
- UndefinedAudioOrientation,
- };
-
- enum AudioStreamType {
- DefaultAudioStream = -1,
- VoiceCallAudioStream = 0,
- SystemAudioStream = 1,
- RingAudioStream = 2,
- MusicAudioStream = 3,
- AlarmAudioStream = 4,
- NotificationAudioStream = 5,
- BluetoothAudioStream = 6,
- EnforcedAudibleAudioStream = 7,
- DTMFAudioStream = 8,
- TTSAudioStream = 9
- };
-
- QDroidUtils(QObject* parent = 0) : QObject(parent)
- {
- }
- ~QDroidUtils()
- {
- }
-
- //### TBD: make an instance() method, for singleton use from C++ ?
- //e.g. connect(myobj, mysig, QDroidUtils::instance(), slot(rebootSystem());
-
-public Q_SLOTS:
- void rebootSystem();
- void powerOffSystem();
-
- void setOrientationForAudioSystem(AudioOrientation orientation);
-
- void setMasterVolume(int volume);
- void setMasterMute(bool mute);
- void setStreamVolume(AudioStreamType stream, int volume);
- void setStreamMute(AudioStreamType stream, bool mute);
-
- float masterVolume() const;
- bool masterMute() const;
- float streamVolume(AudioStreamType stream) const;
- bool streamMute(AudioStreamType stream) const;
-
- bool setDisplayBrightness(quint8 value);
-
- QString getIPAddress();
- QString getHostname();
- bool setHostname(QString hostname);
-};
-
-#endif // QDROIDUTILS_H
diff --git a/src/utils/utils.pro b/src/utils/utils.pro
index e28b111..13287d5 100644
--- a/src/utils/utils.pro
+++ b/src/utils/utils.pro
@@ -1,20 +1,18 @@
-TARGET = QtDroidUtils
-VERSION = 5.2
+load(qt_build_config)
+
+TARGET = B2QtUtils
+VERSION = 1.0
CONFIG += dll warn_on
QT = core network
-#QT = core-private gui-private qml-private quick-private
-#QT_PRIVATE = v8-private
-
-#DEFINES += QT_NO_URL_CAST_FROM_STRING QT_NO_INTEGER_EVENT_COORDINATES
-MODULE = droidutils
+MODULE = b2qtutils
load(qt_module)
android: LIBS += -lmedia -lhardware -lcutils -lutils
HEADERS += \
- $$PWD/qdroidutils.h
+ $$PWD/b2qtdevice.h
SOURCES += \
- $$PWD/qdroidutils.cpp
+ $$PWD/b2qtdevice.cpp