From 9ad79c102ae8429503981551f8cfb482b8595e91 Mon Sep 17 00:00:00 2001 From: Richard Moe Gustavsen Date: Tue, 9 Apr 2013 11:16:03 +0200 Subject: iOS: implement proximity sensor Change-Id: Ia4c2dbe4f81ceda1600f9f642effbc3377cfa94d Reviewed-by: Thomas McGuire --- src/plugins/sensors/ios/ios.pro | 6 +- src/plugins/sensors/ios/iosproximitysensor.h | 73 ++++++++++++++ src/plugins/sensors/ios/iosproximitysensor.mm | 133 ++++++++++++++++++++++++++ src/plugins/sensors/ios/main.mm | 5 + src/sensors/doc/src/compatmap.qdoc | 2 +- 5 files changed, 216 insertions(+), 3 deletions(-) create mode 100644 src/plugins/sensors/ios/iosproximitysensor.h create mode 100644 src/plugins/sensors/ios/iosproximitysensor.mm diff --git a/src/plugins/sensors/ios/ios.pro b/src/plugins/sensors/ios/ios.pro index 2cb0bc9d..4197b14e 100644 --- a/src/plugins/sensors/ios/ios.pro +++ b/src/plugins/sensors/ios/ios.pro @@ -11,13 +11,15 @@ HEADERS += iosaccelerometer.h \ iosmotionmanager.h \ iosgyroscope.h \ iosmagnetometer.h \ - ioscompass.h + ioscompass.h \ + iosproximitysensor.h OBJECTIVE_SOURCES += main.mm \ iosaccelerometer.mm \ iosmotionmanager.mm \ iosgyroscope.mm \ iosmagnetometer.mm \ - ioscompass.mm + ioscompass.mm \ + iosproximitysensor.mm LIBS += -framework UIKit -framework CoreMotion -framework CoreLocation diff --git a/src/plugins/sensors/ios/iosproximitysensor.h b/src/plugins/sensors/ios/iosproximitysensor.h new file mode 100644 index 00000000..67885126 --- /dev/null +++ b/src/plugins/sensors/ios/iosproximitysensor.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef IOSPROXIMITYSENSOR_H +#define IOSPROXIMITYSENSOR_H + +#include +#include + +QT_BEGIN_NAMESPACE + +@class ProximitySensorCallback; + +class IOSProximitySensor : public QSensorBackend +{ +public: + static char const * const id; + + explicit IOSProximitySensor(QSensor *sensor); + ~IOSProximitySensor(); + + void start(); + void stop(); + + void proximityChanged(bool close); + static bool available(); + +private: + ProximitySensorCallback *m_proximitySensorCallback; + QProximityReading m_reading; +}; +QT_END_NAMESPACE + +#endif // IOSPROXIMITYSENSOR_H + diff --git a/src/plugins/sensors/ios/iosproximitysensor.mm b/src/plugins/sensors/ios/iosproximitysensor.mm new file mode 100644 index 00000000..5ba832e3 --- /dev/null +++ b/src/plugins/sensors/ios/iosproximitysensor.mm @@ -0,0 +1,133 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#import + +#include "iosproximitysensor.h" + +char const * const IOSProximitySensor::id("ios.proximitysensor"); + +QT_BEGIN_NAMESPACE + +@interface ProximitySensorCallback : NSObject +{ + IOSProximitySensor *m_iosProximitySensor; +} +@end + +@implementation ProximitySensorCallback + +- (id)initWithQIOSProximitySensor:(IOSProximitySensor *)iosProximitySensor +{ + self = [super init]; + if (self) { + m_iosProximitySensor = iosProximitySensor; + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(proximityChanged:) + name:@"UIDeviceProximityStateDidChangeNotification" object:nil]; + } + return self; +} + +-(void) dealloc +{ + [[NSNotificationCenter defaultCenter] + removeObserver:self + name:@"UIDeviceProximityStateDidChangeNotification" object:nil]; + [super dealloc]; +} + +- (void)proximityChanged:(NSNotificationCenter *)notification +{ + Q_UNUSED(notification); + bool close = [[UIDevice currentDevice] proximityState] == YES; + m_iosProximitySensor->proximityChanged(close); +} + +@end + +bool IOSProximitySensor::available() +{ + UIDevice *device = [UIDevice currentDevice]; + if (device.proximityMonitoringEnabled) + return true; + // According to the docs, you need to switch it on and + // re-read the property to check if it is available: + device.proximityMonitoringEnabled = YES; + bool available = device.proximityMonitoringEnabled; + device.proximityMonitoringEnabled = NO; + return available; +} + +IOSProximitySensor::IOSProximitySensor(QSensor *sensor) + : QSensorBackend(sensor) + , m_proximitySensorCallback(0) +{ + setReading(&m_reading); +} + +IOSProximitySensor::~IOSProximitySensor() +{ + [m_proximitySensorCallback release]; +} + +void IOSProximitySensor::start() +{ + m_proximitySensorCallback = [[ProximitySensorCallback alloc] initWithQIOSProximitySensor:this]; + [UIDevice currentDevice].proximityMonitoringEnabled = YES; +} + +void IOSProximitySensor::proximityChanged(bool close) +{ + m_reading.setClose(close); + m_reading.setTimestamp(quint64([[NSDate date] timeIntervalSinceReferenceDate] * 1e6)); + newReadingAvailable(); +} + +void IOSProximitySensor::stop() +{ + [UIDevice currentDevice].proximityMonitoringEnabled = NO; + [m_proximitySensorCallback release]; + m_proximitySensorCallback = 0; +} + +QT_END_NAMESPACE diff --git a/src/plugins/sensors/ios/main.mm b/src/plugins/sensors/ios/main.mm index 8797b8ee..e4a0364e 100644 --- a/src/plugins/sensors/ios/main.mm +++ b/src/plugins/sensors/ios/main.mm @@ -48,6 +48,7 @@ #include "iosgyroscope.h" #include "iosmagnetometer.h" #include "ioscompass.h" +#include "iosproximitysensor.h" class IOSSensorPlugin : public QObject, public QSensorPluginInterface, public QSensorBackendFactory { @@ -64,6 +65,8 @@ public: QSensorManager::registerBackend(QMagnetometer::type, IOSMagnetometer::id, this); if ([CLLocationManager headingAvailable]) QSensorManager::registerBackend(QCompass::type, IOSCompass::id, this); + if (IOSProximitySensor::available()) + QSensorManager::registerBackend(QProximitySensor::type, IOSProximitySensor::id, this); } QSensorBackend *createBackend(QSensor *sensor) @@ -76,6 +79,8 @@ public: return new IOSMagnetometer(sensor); if (sensor->identifier() == IOSCompass::id) return new IOSCompass(sensor); + if (sensor->identifier() == IOSProximitySensor::id) + return new IOSProximitySensor(sensor); return 0; } diff --git a/src/sensors/doc/src/compatmap.qdoc b/src/sensors/doc/src/compatmap.qdoc index 4eeff238..5e541def 100644 --- a/src/sensors/doc/src/compatmap.qdoc +++ b/src/sensors/doc/src/compatmap.qdoc @@ -173,7 +173,7 @@ - + -- cgit v1.2.3