Skip to content

Commit fa5866a

Browse files
garymathewslokeshchdhry
authored andcommitted
fix(android): getCurrentPosition() compatibility with some Samsung devices (#11355)
* fix(android): getCurrentPosition() compatibility with Samsung devices * fix(android): only register location provider when necessary * fix(android): return error when provider is unavailable * fix(android): clone callback array
1 parent 5bb63c5 commit fa5866a

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

android/modules/geolocation/src/java/ti/modules/titanium/geolocation/GeolocationModule.java

+45-24
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public class GeolocationModule extends KrollModule implements Handler.Callback,
119119
privatestaticfinaldoubleSIMPLE_LOCATION_PASSIVE_DISTANCE = 0.0;
120120
privatestaticfinaldoubleSIMPLE_LOCATION_PASSIVE_TIME = 0;
121121
privatestaticfinaldoubleSIMPLE_LOCATION_NETWORK_DISTANCE = 10.0;
122-
privatestaticfinaldoubleSIMPLE_LOCATION_NETWORK_TIME = 10000;
122+
privatestaticfinaldoubleSIMPLE_LOCATION_NETWORK_TIME = 3000;
123123
privatestaticfinaldoubleSIMPLE_LOCATION_GPS_DISTANCE = 3.0;
124124
privatestaticfinaldoubleSIMPLE_LOCATION_GPS_TIME = 3000;
125125
privatestaticfinaldoubleSIMPLE_LOCATION_NETWORK_DISTANCE_RULE = 200;
@@ -136,6 +136,7 @@ public class GeolocationModule extends KrollModule implements Handler.Callback,
136136
//currentLocation is conditionally updated. lastLocation is unconditionally updated
137137
//since currentLocation determines when to send out updates, and lastLocation is passive
138138
privateLocationlastLocation;
139+
privateArrayList<KrollFunction> currentPositionCallback = newArrayList<>();
139140

140141
privateFusedLocationProviderfusedLocationProvider;
141142

@@ -200,6 +201,20 @@ public boolean handleMessage(Message message)
200201
publicvoidonLocationChanged(Locationlocation)
201202
{
202203
lastLocation = location;
204+
205+
// Execute current position callbacks.
206+
if (currentPositionCallback.size() > 0) {
207+
ArrayList<KrollFunction> currentPositionCallbackClone =
208+
(ArrayList<KrollFunction>) currentPositionCallback.clone();
209+
currentPositionCallback.clear();
210+
for (KrollFunctioncallback : currentPositionCallbackClone) {
211+
callback.call(this.getKrollObject(),
212+
newObject[] { buildLocationEvent(
213+
lastLocation, tiLocation.locationManager.getProvider(lastLocation.getProvider())) });
214+
}
215+
}
216+
217+
// Fire 'location' event listeners.
203218
if (shouldUseUpdate(location)) {
204219
fireEvent(TiC.EVENT_LOCATION,
205220
buildLocationEvent(location, tiLocation.locationManager.getProvider(location.getProvider())));
@@ -225,50 +240,42 @@ public void onProviderStateChanged(String providerName, int state)
225240
switch (state) {
226241
caseLocationProviderProxy.STATE_DISABLED:
227242
message += " is disabled";
228-
Log.i(TAG, message, Log.DEBUG_MODE);
229-
fireEvent(TiC.EVENT_LOCATION, buildLocationErrorEvent(state, message));
230-
231243
break;
232244

233245
caseLocationProviderProxy.STATE_ENABLED:
234246
message += " is enabled";
235-
Log.d(TAG, message, Log.DEBUG_MODE);
236-
237247
break;
238248

239249
caseLocationProviderProxy.STATE_OUT_OF_SERVICE:
240250
message += " is out of service";
241-
Log.d(TAG, message, Log.DEBUG_MODE);
242-
fireEvent(TiC.EVENT_LOCATION, buildLocationErrorEvent(state, message));
243-
244251
break;
245252

246253
caseLocationProviderProxy.STATE_UNAVAILABLE:
247254
message += " is unavailable";
248-
Log.d(TAG, message, Log.DEBUG_MODE);
249-
fireEvent(TiC.EVENT_LOCATION, buildLocationErrorEvent(state, message));
250-
251255
break;
252256

253257
caseLocationProviderProxy.STATE_AVAILABLE:
254258
message += " is available";
255-
Log.d(TAG, message, Log.DEBUG_MODE);
256-
257259
break;
258260

259261
caseLocationProviderProxy.STATE_UNKNOWN:
260-
message += " is in a unknown state [" + state + "]";
261-
Log.d(TAG, message, Log.DEBUG_MODE);
262-
fireEvent(TiC.EVENT_LOCATION, buildLocationErrorEvent(state, message));
263-
264-
break;
265-
266262
default:
267263
message += " is in a unknown state [" + state + "]";
268-
Log.d(TAG, message, Log.DEBUG_MODE);
269-
fireEvent(TiC.EVENT_LOCATION, buildLocationErrorEvent(state, message));
270-
271-
break;
264+
}
265+
Log.d(TAG, message, Log.DEBUG_MODE);
266+
267+
if (state != LocationProviderProxy.STATE_ENABLED && state != LocationProviderProxy.STATE_AVAILABLE) {
268+
fireEvent(TiC.EVENT_LOCATION, buildLocationErrorEvent(state, message));
269+
270+
// Execute current position callbacks.
271+
if (currentPositionCallback.size() > 0) {
272+
ArrayList<KrollFunction> currentPositionCallbackClone =
273+
(ArrayList<KrollFunction>) currentPositionCallback.clone();
274+
currentPositionCallback.clear();
275+
for (KrollFunctioncallback : currentPositionCallbackClone) {
276+
callback.call(this.getKrollObject(), newObject[] { buildLocationErrorEvent(state, message) });
277+
}
278+
}
272279
}
273280
}
274281

@@ -670,6 +677,20 @@ public void getCurrentPosition(KrollFunction callback)
670677
}
671678
if (callback != null) {
672679
LocationlatestKnownLocation = tiLocation.getLastKnownLocation();
680+
if (latestKnownLocation == null) {
681+
latestKnownLocation = lastLocation;
682+
}
683+
684+
// TIMOB-27572: Samsung devices require a location provider to be registered
685+
// in order to obtain last known location.
686+
if (latestKnownLocation == null) {
687+
if (numLocationListeners == 0) {
688+
numLocationListeners++;
689+
enableLocationProviders(simpleLocationProviders);
690+
}
691+
currentPositionCallback.add(callback);
692+
return;
693+
}
673694

674695
if (latestKnownLocation != null) {
675696
callback.call(

0 commit comments

Comments
 (0)
close