1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtPositioning module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include <QDebug>
41
42 #include "qgeosatelliteinfosource_android_p.h"
43 #include "jnipositioning.h"
44
45 Q_DECLARE_METATYPE(QGeoSatelliteInfo)
Q_DECLARE_METATYPE(QList<QGeoSatelliteInfo>)46 Q_DECLARE_METATYPE(QList<QGeoSatelliteInfo>)
47
48 #define UPDATE_FROM_COLD_START 2*60*1000
49
50 QGeoSatelliteInfoSourceAndroid::QGeoSatelliteInfoSourceAndroid(QObject *parent) :
51 QGeoSatelliteInfoSource(parent), m_error(NoError), updatesRunning(false)
52 {
53 qRegisterMetaType< QGeoSatelliteInfo >();
54 qRegisterMetaType< QList<QGeoSatelliteInfo> >();
55 androidClassKeyForUpdate = AndroidPositioning::registerPositionInfoSource(this);
56 androidClassKeyForSingleRequest = AndroidPositioning::registerPositionInfoSource(this);
57
58 requestTimer.setSingleShot(true);
59 QObject::connect(&requestTimer, SIGNAL(timeout()),
60 this, SLOT(requestTimeout()));
61 }
62
~QGeoSatelliteInfoSourceAndroid()63 QGeoSatelliteInfoSourceAndroid::~QGeoSatelliteInfoSourceAndroid()
64 {
65 stopUpdates();
66
67 if (requestTimer.isActive()) {
68 requestTimer.stop();
69 AndroidPositioning::stopUpdates(androidClassKeyForSingleRequest);
70 }
71
72 AndroidPositioning::unregisterPositionInfoSource(androidClassKeyForUpdate);
73 AndroidPositioning::unregisterPositionInfoSource(androidClassKeyForSingleRequest);
74 }
75
76
setUpdateInterval(int msec)77 void QGeoSatelliteInfoSourceAndroid::setUpdateInterval(int msec)
78 {
79 int previousInterval = updateInterval();
80 msec = (((msec > 0) && (msec < minimumUpdateInterval())) || msec < 0)? minimumUpdateInterval() : msec;
81
82 if (msec == previousInterval)
83 return;
84
85 QGeoSatelliteInfoSource::setUpdateInterval(msec);
86
87 if (updatesRunning)
88 reconfigureRunningSystem();
89 }
90
minimumUpdateInterval() const91 int QGeoSatelliteInfoSourceAndroid::minimumUpdateInterval() const
92 {
93 return 50;
94 }
95
error() const96 QGeoSatelliteInfoSource::Error QGeoSatelliteInfoSourceAndroid::error() const
97 {
98 return m_error;
99 }
100
startUpdates()101 void QGeoSatelliteInfoSourceAndroid::startUpdates()
102 {
103 if (updatesRunning)
104 return;
105
106 updatesRunning = true;
107
108 QGeoSatelliteInfoSource::Error error = AndroidPositioning::startSatelliteUpdates(
109 androidClassKeyForUpdate, false, updateInterval());
110 if (error != QGeoSatelliteInfoSource::NoError) {
111 updatesRunning = false;
112 m_error = error;
113 emit QGeoSatelliteInfoSource::error(m_error);
114 }
115 }
116
stopUpdates()117 void QGeoSatelliteInfoSourceAndroid::stopUpdates()
118 {
119 if (!updatesRunning)
120 return;
121
122 updatesRunning = false;
123 AndroidPositioning::stopUpdates(androidClassKeyForUpdate);
124 }
125
requestUpdate(int timeout)126 void QGeoSatelliteInfoSourceAndroid::requestUpdate(int timeout)
127 {
128 if (requestTimer.isActive())
129 return;
130
131 if (timeout != 0 && timeout < minimumUpdateInterval()) {
132 emit requestTimeout();
133 return;
134 }
135
136 if (timeout == 0)
137 timeout = UPDATE_FROM_COLD_START;
138
139 requestTimer.start(timeout);
140
141 // if updates already running with interval equal or less then timeout
142 // then we wait for next update coming through
143 // assume that a single update will not be quicker than regular updates anyway
144 if (updatesRunning && updateInterval() <= timeout)
145 return;
146
147 QGeoSatelliteInfoSource::Error error = AndroidPositioning::startSatelliteUpdates(
148 androidClassKeyForSingleRequest, true, timeout);
149 if (error != QGeoSatelliteInfoSource::NoError) {
150 requestTimer.stop();
151 m_error = error;
152 emit QGeoSatelliteInfoSource::error(m_error);
153 }
154 }
155
processSatelliteUpdateInView(const QList<QGeoSatelliteInfo> & satsInView,bool isSingleUpdate)156 void QGeoSatelliteInfoSourceAndroid::processSatelliteUpdateInView(const QList<QGeoSatelliteInfo> &satsInView, bool isSingleUpdate)
157 {
158 if (!isSingleUpdate) {
159 //if requested while regular updates were running
160 if (requestTimer.isActive())
161 requestTimer.stop();
162 emit QGeoSatelliteInfoSource::satellitesInViewUpdated(satsInView);
163 return;
164 }
165
166 m_satsInView = satsInView;
167 }
168
processSatelliteUpdateInUse(const QList<QGeoSatelliteInfo> & satsInUse,bool isSingleUpdate)169 void QGeoSatelliteInfoSourceAndroid::processSatelliteUpdateInUse(const QList<QGeoSatelliteInfo> &satsInUse, bool isSingleUpdate)
170 {
171 if (!isSingleUpdate) {
172 //if requested while regular updates were running
173 if (requestTimer.isActive())
174 requestTimer.stop();
175 emit QGeoSatelliteInfoSource::satellitesInUseUpdated(satsInUse);
176 return;
177 }
178
179 m_satsInUse = satsInUse;
180 }
181
requestTimeout()182 void QGeoSatelliteInfoSourceAndroid::requestTimeout()
183 {
184 AndroidPositioning::stopUpdates(androidClassKeyForSingleRequest);
185
186 const int count = m_satsInView.count();
187 if (!count) {
188 emit requestTimeout();
189 return;
190 }
191
192 emit QGeoSatelliteInfoSource::satellitesInViewUpdated(m_satsInView);
193 emit QGeoSatelliteInfoSource::satellitesInUseUpdated(m_satsInUse);
194
195 m_satsInUse.clear();
196 m_satsInView.clear();
197 }
198
199 /*
200 Updates the system assuming that updateInterval
201 and/or preferredPositioningMethod have changed.
202 */
reconfigureRunningSystem()203 void QGeoSatelliteInfoSourceAndroid::reconfigureRunningSystem()
204 {
205 if (!updatesRunning)
206 return;
207
208 stopUpdates();
209 startUpdates();
210 }
211
locationProviderDisabled()212 void QGeoSatelliteInfoSourceAndroid::locationProviderDisabled()
213 {
214 m_error = QGeoSatelliteInfoSource::ClosedError;
215 emit QGeoSatelliteInfoSource::error(m_error);
216 }
217