-
Notifications
You must be signed in to change notification settings - Fork 38
/
CellLocationController.java
289 lines (248 loc) · 11.5 KB
/
CellLocationController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
* @author Andy Gup
*
* Copyright 2016 Esri
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.esri.cordova.geolocation.controllers;
/**
* Provides information derived from the cellular service of the device. Not all functionality
* is available on all devices and it also depends on the cellular providers capabilities.
*
* IMPORTANT: This Class is only compatible with API Level 17 or greater
* Reference: https://developer.android.com/reference/android/telephony/CellInfo.html
*/
import android.content.Context;
import android.os.Build;
import android.os.Looper;
import android.telephony.CellInfo;
import android.telephony.CellInfoCdma;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellInfoWcdma;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import com.esri.cordova.geolocation.listeners.SignalStrengthListener;
import com.esri.cordova.geolocation.model.StrengthChange;
import com.esri.cordova.geolocation.utils.ErrorMessages;
import com.esri.cordova.geolocation.utils.JSONHelper;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.PluginResult;
import java.util.List;
public final class CellLocationController implements Runnable{
public static final String CELLINFO_PROVIDER = "cell";
private static final String TAG = "GeolocationPlugin";
private static CallbackContext _callbackContext; // Threadsafe
private static TelephonyManager _telephonyManager = null;
private static PhoneStateListener _phoneStateListener = null;
private static SignalStrengthListener _signalStrengthListener = null;
private static CordovaInterface _cordova;
private static boolean _isConnected = false;
private static boolean _returnSignalStrength = false;
public CellLocationController(
boolean isConnected,
boolean returnSignalStrength,
CordovaInterface cordova,
CallbackContext callbackContext
){
_isConnected = isConnected;
_cordova = cordova;
_callbackContext = callbackContext;
_returnSignalStrength = returnSignalStrength;
}
public void run(){
// There are minimum OS version requirements
if(versionCheck()){
// Reference: http://developer.android.com/reference/android/os/Process.html#THREAD_PRIORITY_BACKGROUND
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
// We are running a Looper to allow the Cordova CallbackContext to be passed within the Thread as a message.
if(Looper.myLooper() == null){
_telephonyManager = (TelephonyManager) _cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
Looper.prepare();
startLocation();
Looper.loop();
}
}
else {
Log.e(TAG, ErrorMessages.CELL_DATA_MIN_VERSION().message);
sendCallback(PluginResult.Status.ERROR,
JSONHelper.errorJSON(CELLINFO_PROVIDER, ErrorMessages.CELL_DATA_MIN_VERSION()));
}
}
public void startLocation(){
if(!Thread.currentThread().isInterrupted()){
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
Log.e(TAG, "Failing gracefully after detecting an uncaught exception on CellLocationController thread. "
+ throwable.getMessage());
sendCallback(PluginResult.Status.ERROR,
JSONHelper.errorJSON(CELLINFO_PROVIDER, ErrorMessages.UNCAUGHT_THREAD_EXCEPTION()));
stopLocation();
}
});
if(_isConnected){
// Set up a change listener
setPhoneStateListener();
// Return a snapshot of all cell info
getAllCellInfos();
Log.d(TAG, "Starting CellLocationController");
}
else {
Log.e(TAG, "Unable to start CellLocationController: no internet connection.");
sendCallback(PluginResult.Status.ERROR,
JSONHelper.errorJSON(CELLINFO_PROVIDER, ErrorMessages.CELL_DATA_NOT_AVAILABLE()));
}
}
else {
Log.e(TAG, "Not starting CellLocationController due to thread interrupt.");
}
}
/**
* Full stop using brute force. Works with many Android versions.
*/
public void stopLocation(){
if(_phoneStateListener != null && _telephonyManager != null){
_telephonyManager.listen(_phoneStateListener, PhoneStateListener.LISTEN_NONE);
}
if(_signalStrengthListener != null && _telephonyManager != null){
_telephonyManager.listen(_signalStrengthListener, PhoneStateListener.LISTEN_NONE);
}
_signalStrengthListener = null;
_phoneStateListener = null;
_telephonyManager = null;
try {
Thread.currentThread().interrupt();
}
catch(SecurityException exc){
Log.e(TAG, exc.getMessage());
sendCallback(PluginResult.Status.ERROR,
JSONHelper.errorJSON(CELLINFO_PROVIDER, ErrorMessages.FAILED_THREAD_INTERRUPT()));
}
Log.d(TAG, "Stopping cell location listeners");
}
/**
* Returns all observed cell information from all radios on the device including the primary and
* neighboring cells. Calling this method does not trigger a call to onCellInfoChanged(), or change
* the rate at which onCellInfoChanged() is called.
*/
private void getAllCellInfos(){
if(_telephonyManager != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final List<CellInfo> cellInfos = _telephonyManager.getAllCellInfo();
processCellInfos(cellInfos);
}
else {
Log.w(TAG, "Unable to provide cell info due to version restriction");
}
}
private void setPhoneStateListener(){
if(_returnSignalStrength){
_signalStrengthListener = new SignalStrengthListener();
_signalStrengthListener.setListener(new StrengthChange() {
@Override
public SignalStrength onSignalStrengthChanged(SignalStrength signalStrength) {
if(!Thread.currentThread().isInterrupted()){
sendCallback(PluginResult.Status.OK,
JSONHelper.signalStrengthJSON(signalStrength));
}
return null;
}
});
_telephonyManager.listen(_signalStrengthListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
_phoneStateListener = new PhoneStateListener(){
@Override
public void onCellLocationChanged(CellLocation location){
if(!Thread.currentThread().isInterrupted()){
if(location instanceof CdmaCellLocation){
final CdmaCellLocation cellLocationCdma = (CdmaCellLocation) location;
sendCallback(PluginResult.Status.OK,
JSONHelper.cdmaCellLocationJSON(cellLocationCdma));
}
if(location instanceof GsmCellLocation){
final GsmCellLocation cellLocationGsm = (GsmCellLocation) location;
sendCallback(PluginResult.Status.OK,
JSONHelper.gsmCellLocationJSON(cellLocationGsm));
}
}
}
@Override
public void onCellInfoChanged(List<CellInfo> cellInfo){
if(!Thread.currentThread().isInterrupted()){
processCellInfos(cellInfo);
}
}
};
_telephonyManager.listen(_phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION);
}
private static void processCellInfos(List<CellInfo> cellInfos){
if(cellInfos != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
for(CellInfo cellInfo : cellInfos){
if(cellInfo instanceof CellInfoWcdma){
final CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) cellInfo;
sendCallback(PluginResult.Status.OK,
JSONHelper.cellInfoWCDMAJSON(cellInfoWcdma, _returnSignalStrength));
}
if(cellInfo instanceof CellInfoGsm){
final CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
sendCallback(PluginResult.Status.OK,
JSONHelper.cellInfoGSMJSON(cellInfoGsm, _returnSignalStrength));
}
if(cellInfo instanceof CellInfoCdma){
final CellInfoCdma cellIdentityCdma = (CellInfoCdma) cellInfo;
sendCallback(PluginResult.Status.OK,
JSONHelper.cellInfoCDMAJSON(cellIdentityCdma, _returnSignalStrength));
}
if(cellInfo instanceof CellInfoLte){
final CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
sendCallback(PluginResult.Status.OK,
JSONHelper.cellInfoLTEJSON(cellInfoLte, _returnSignalStrength));
}
Log.d(TAG,cellInfo.toString());
}
}
else {
Log.e(TAG, "CellInfoLocation returning null. Is it supported on this phone?");
// There are several reasons as to why cell location data would be null.
// * could be an older device running an unsupported version of the Android OS
// * could be a device that doesn't support this capability.
// * could be incorrect permissions: ACCESS_COARSE_LOCATION
sendCallback(PluginResult.Status.ERROR,
JSONHelper.errorJSON(CELLINFO_PROVIDER, ErrorMessages.CELL_DATA_IS_NULL()));
}
}
/**
* This Class will not work correctly on older versions of the Android SDK
* Reference: http://developer.android.com/reference/android/telephony/TelephonyManager.html#getAllCellInfo()
*/
private static boolean versionCheck(){
boolean verified = true;
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
verified = false;
}
return verified;
}
private static void sendCallback(PluginResult.Status status, String message){
if(!Thread.currentThread().isInterrupted()){
final PluginResult result = new PluginResult(status, message);
result.setKeepCallback(true);
_callbackContext.sendPluginResult(result);
}
}
}