Package org.robolectric.shadows
Class ShadowLocationManager
java.lang.Object
org.robolectric.shadows.ShadowLocationManager
@Implements(value=android.location.LocationManager.class,
looseSignatures=true)
public class ShadowLocationManager
extends Object
Shadow for
LocationManager
. Note that the default state of location on Android devices is
location on, gps provider enabled, network provider disabled.-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
ProviderProperties is not public prior to S, so a new class is required to represent it prior to that platform.static final class
LocationRequest doesn't exist prior to Kitkat, and is not public prior to S, so a new class is required to represent it prior to those platforms. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
addGpsStatusListener
(GpsStatus.Listener listener) protected boolean
addNmeaListener
(OnNmeaMessageListener listener, Handler handler) protected boolean
addNmeaListener
(Executor executor, OnNmeaMessageListener listener) protected void
protected String
getBestProvider
(Criteria criteria, boolean enabled) protected void
getCurrentLocation
(String provider, LocationRequest request, CancellationSignal cancellationSignal, Executor executor, Consumer<Location> consumer) protected void
getCurrentLocation
(String provider, CancellationSignal cancellationSignal, Executor executor, Consumer<Location> consumer) protected int
protected String
protected int
Returns the list of currently registeredGpsStatus.Listener
s.protected Location
getLastKnownLocation
(String provider) getLegacyLocationRequests
(String provider) Returns the list ofShadowLocationManager.RoboLocationRequest
currently registered under the given provider.getLocationRequests
(String provider) Returns the list ofLocationRequest
currently registered under the given provider.Deprecated.getLocationUpdateListeners
(String provider) Deprecated.Do not test listeners, instead usesimulateLocation(Location)
and test the results of those listeners being invoked.Deprecated.Do not test pending intents, instead usesimulateLocation(Location)
and test the results of those pending intent being invoked.getLocationUpdatePendingIntents
(String provider) Deprecated.Do not test pending intents, instead usesimulateLocation(Location)
and test the results of those pending intent being invoked.protected LocationProvider
getProvider
(String name) protected Object
getProviderProperties
(Object providerStr) getProviders
(boolean enabledOnly) getProviders
(Criteria criteria, boolean enabled) Deprecated.Do not test listeners, instead usesimulateLocation(Location)
and test the results of those listeners being invoked.protected boolean
hasProvider
(String provider) protected boolean
injectLocation
(Location location) protected boolean
protected boolean
isLocationEnabledForUser
(UserHandle userHandle) protected boolean
isProviderEnabled
(String provider) protected Object
registerAntennaInfoListener
(Object executor, Object listener) protected boolean
registerGnssBatchedLocationCallback
(Object periodNanos, Object wakeOnFifoFull, Object callback, Object handler) protected boolean
registerGnssMeasurementsCallback
(GnssMeasurementsEvent.Callback listener, Handler handler) protected boolean
registerGnssMeasurementsCallback
(Object request, Object executor, Object callback) protected boolean
registerGnssMeasurementsCallback
(Executor executor, GnssMeasurementsEvent.Callback listener) protected boolean
registerGnssStatusCallback
(GnssStatus.Callback callback, Handler handler) protected boolean
registerGnssStatusCallback
(Executor executor, GnssStatus.Callback listener) protected void
removeGpsStatusListener
(GpsStatus.Listener listener) protected void
removeNmeaListener
(OnNmeaMessageListener listener) void
removeProvider
(String name) Completely removes a provider.protected void
removeUpdates
(PendingIntent pendingIntent) protected void
removeUpdates
(LocationListener listener) protected void
requestFlush
(String provider, PendingIntent pendingIntent, int requestCode) protected void
requestFlush
(String provider, LocationListener listener, int requestCode) protected void
requestLocationUpdates
(long minTime, float minDistance, Criteria criteria, PendingIntent pendingIntent) protected void
requestLocationUpdates
(long minTime, float minDistance, Criteria criteria, LocationListener listener, Looper looper) protected void
requestLocationUpdates
(long minTime, float minDistance, Criteria criteria, Executor executor, LocationListener listener) protected void
requestLocationUpdates
(LocationRequest request, PendingIntent pendingIntent) protected void
requestLocationUpdates
(LocationRequest request, LocationListener listener, Looper looper) protected void
requestLocationUpdates
(LocationRequest request, Executor executor, LocationListener listener) protected void
requestLocationUpdates
(String provider, long minTime, float minDistance, PendingIntent pendingIntent) protected void
requestLocationUpdates
(String provider, long minTime, float minDistance, LocationListener listener) protected void
requestLocationUpdates
(String provider, long minTime, float minDistance, LocationListener listener, Looper looper) protected void
requestLocationUpdates
(String provider, long minTime, float minDistance, Executor executor, LocationListener listener) protected void
requestLocationUpdates
(String provider, LocationRequest request, PendingIntent pendingIntent) protected void
requestLocationUpdates
(String provider, LocationRequest request, Executor executor, LocationListener listener) protected void
requestSingleUpdate
(Criteria criteria, PendingIntent pendingIntent) protected void
requestSingleUpdate
(Criteria criteria, LocationListener listener, Looper looper) protected void
requestSingleUpdate
(String provider, PendingIntent pendingIntent) protected void
requestSingleUpdate
(String provider, LocationListener listener, Looper looper) static void
reset()
void
sendGnssAntennaInfo
(List<GnssAntennaInfo> antennaInfos) Deprecated.UsesimulateGnssAntennaInfo(List)
instead.void
Deprecated.UsesimulateGnssMeasurementsEvent(GnssMeasurementsEvent)
instead.void
sendGnssStatus
(GnssStatus status) Deprecated.UsesimulateGnssStatus(GnssStatus)
instead.void
sendNmeaMessage
(String message, long timestamp) Deprecated.UsesimulateNmeaMessage(String, long)
instead.void
setGnssBatchSize
(int gnssBatchSize) Sets the GNSS hardware batch size.void
setGnssHardwareModelName
(String gnssHardwareModelName) Sets the GNSS hardware model name returned byLocationManager.getGnssHardwareModelName()
.void
setGnssYearOfHardware
(int gnssYearOfHardware) Sets the GNSS year of hardware returned byLocationManager.getGnssYearOfHardware()
.void
setLastKnownLocation
(String provider, Location location) Deprecated.UsesimulateLocation(Location)
to update the last location for a provider.void
setLocationEnabled
(boolean enabled) On P and above, turns location on or off.protected void
setLocationEnabledForUser
(boolean enabled, UserHandle userHandle) void
setLocationMode
(int locationMode) On pre-P devices, sets the device location mode.void
setProviderEnabled
(String name, boolean enabled) Sets the given provider enabled or disabled.void
setProviderProperties
(String name, ShadowLocationManager.ProviderProperties properties) Sets the properties of the given provider.void
simulateGnssAntennaInfo
(List<GnssAntennaInfo> antennaInfos) Simulates a GNSS antenna info event.void
Simulates a GNSS measurements event.void
simulateGnssMeasurementsStatus
(int status) Simulates a GNSS measurements status change.void
simulateGnssStatus
(GnssStatus status) Simulates a GNSS status event.void
simulateGnssStatusFirstFix
(int ttff) Simulates a GNSS status first fix event.void
Simulates a GNSS status started event.void
Simulates a GNSS status stopped event.void
simulateLocation
(Location location) A convenience function equivalent to invoking#simulateLocation(String, Location)
with the provider of the given location.void
simulateLocation
(String provider, Location... locations) Delivers to the given provider (which will be created if necessary) a new location which will be delivered to appropriate listeners and updates state accordingly.void
simulateNmeaMessage
(String message, long timestamp) Simulates a NMEA message.protected void
unregisterAntennaInfoListener
(Object listener) protected boolean
unregisterGnssBatchedLocationCallback
(Object callback) protected void
protected void
-
Constructor Details
-
ShadowLocationManager
public ShadowLocationManager()
-
-
Method Details
-
getAllProviders
-
getProvider
-
getProviders
-
getProviders
-
getBestProvider
-
getProviderProperties
-
hasProvider
-
isProviderEnabled
-
removeProvider
Completely removes a provider. -
setProviderProperties
public void setProviderProperties(String name, @Nullable ShadowLocationManager.ProviderProperties properties) Sets the properties of the given provider. The provider will be created if it doesn't exist already. This overload functions for all Android SDK levels. -
setProviderEnabled
Sets the given provider enabled or disabled. The provider will be created if it doesn't exist already. On P and above, location must also be enabled viasetLocationEnabled(boolean)
in order for a provider to be considered enabled. -
isLocationEnabledForUser
-
isLocationEnabled
-
setLocationEnabledForUser
@Implementation(minSdk=28) protected void setLocationEnabledForUser(boolean enabled, UserHandle userHandle) -
setLocationEnabled
public void setLocationEnabled(boolean enabled) On P and above, turns location on or off. On pre-P devices, sets the location mode toSettings.Secure.LOCATION_MODE_HIGH_ACCURACY
orSettings.Secure.LOCATION_MODE_OFF
. -
setLocationMode
public void setLocationMode(int locationMode) On pre-P devices, sets the device location mode. For P and above, usesetLocationEnabled(boolean)
andsetProviderEnabled(String, boolean)
in combination to achieve the desired effect. -
getLastKnownLocation
-
setLastKnownLocation
Deprecated.UsesimulateLocation(Location)
to update the last location for a provider. -
getCurrentLocation
@RequiresApi(api=30) @Implementation(minSdk=30) protected void getCurrentLocation(String provider, @Nullable CancellationSignal cancellationSignal, Executor executor, Consumer<Location> consumer) -
getCurrentLocation
@RequiresApi(api=31) @Implementation(minSdk=31) protected void getCurrentLocation(String provider, LocationRequest request, @Nullable CancellationSignal cancellationSignal, Executor executor, Consumer<Location> consumer) -
requestSingleUpdate
@Implementation protected void requestSingleUpdate(String provider, LocationListener listener, @Nullable Looper looper) -
requestSingleUpdate
@Implementation protected void requestSingleUpdate(Criteria criteria, LocationListener listener, @Nullable Looper looper) -
requestSingleUpdate
-
requestSingleUpdate
-
requestLocationUpdates
@Implementation protected void requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener) -
requestLocationUpdates
@Implementation protected void requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener, @Nullable Looper looper) -
requestLocationUpdates
@Implementation(minSdk=30) protected void requestLocationUpdates(String provider, long minTime, float minDistance, Executor executor, LocationListener listener) -
requestLocationUpdates
@Implementation protected void requestLocationUpdates(long minTime, float minDistance, Criteria criteria, LocationListener listener, @Nullable Looper looper) -
requestLocationUpdates
@Implementation(minSdk=30) protected void requestLocationUpdates(long minTime, float minDistance, Criteria criteria, Executor executor, LocationListener listener) -
requestLocationUpdates
@Implementation protected void requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent pendingIntent) -
requestLocationUpdates
@Implementation protected void requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent pendingIntent) -
requestLocationUpdates
@Implementation(minSdk=30) protected void requestLocationUpdates(@Nullable LocationRequest request, Executor executor, LocationListener listener) -
requestLocationUpdates
@Implementation(minSdk=19) protected void requestLocationUpdates(@Nullable LocationRequest request, LocationListener listener, Looper looper) -
requestLocationUpdates
@Implementation(minSdk=19) protected void requestLocationUpdates(@Nullable LocationRequest request, PendingIntent pendingIntent) -
requestLocationUpdates
@Implementation(minSdk=31) protected void requestLocationUpdates(String provider, LocationRequest request, Executor executor, LocationListener listener) -
requestLocationUpdates
@Implementation(minSdk=31) protected void requestLocationUpdates(String provider, LocationRequest request, PendingIntent pendingIntent) -
removeUpdates
-
removeUpdates
-
requestFlush
@Implementation(minSdk=31) protected void requestFlush(String provider, LocationListener listener, int requestCode) -
requestFlush
@Implementation(minSdk=31) protected void requestFlush(String provider, PendingIntent pendingIntent, int requestCode) -
getLocationRequests
Returns the list ofLocationRequest
currently registered under the given provider. Clients compiled against the public Android SDK should only use this method on S+, clients compiled against the system Android SDK may only use this method on Kitkat+.Prior to Android S
LocationRequest
equality is not well defined, so prefer usinggetLegacyLocationRequests(String)
instead if equality is required for testing. -
getLegacyLocationRequests
Returns the list ofShadowLocationManager.RoboLocationRequest
currently registered under the given provider. SinceLocationRequest
was not publicly visible prior to S, and did not exist prior to Kitkat,ShadowLocationManager.RoboLocationRequest
allows querying the location requests prior to those platforms, and also implements proper equality comparisons for testing. -
injectLocation
-
getGnssBatchSize
-
setGnssBatchSize
public void setGnssBatchSize(int gnssBatchSize) Sets the GNSS hardware batch size. Values greater than 0 enables hardware GNSS batching APIs. -
registerGnssBatchedLocationCallback
-
flushGnssBatch
-
unregisterGnssBatchedLocationCallback
-
getGnssHardwareModelName
-
setGnssHardwareModelName
Sets the GNSS hardware model name returned byLocationManager.getGnssHardwareModelName()
. -
getGnssYearOfHardware
-
setGnssYearOfHardware
public void setGnssYearOfHardware(int gnssYearOfHardware) Sets the GNSS year of hardware returned byLocationManager.getGnssYearOfHardware()
. -
addGpsStatusListener
-
removeGpsStatusListener
-
getGpsStatusListeners
Returns the list of currently registeredGpsStatus.Listener
s. -
registerGnssStatusCallback
@Implementation(minSdk=24) protected boolean registerGnssStatusCallback(GnssStatus.Callback callback, Handler handler) -
registerGnssStatusCallback
@Implementation(minSdk=30) protected boolean registerGnssStatusCallback(Executor executor, GnssStatus.Callback listener) -
unregisterGnssStatusCallback
@Implementation(minSdk=24) protected void unregisterGnssStatusCallback(GnssStatus.Callback listener) -
simulateGnssStatusStarted
@RequiresApi(24) public void simulateGnssStatusStarted()Simulates a GNSS status started event. -
simulateGnssStatusFirstFix
@RequiresApi(24) public void simulateGnssStatusFirstFix(int ttff) Simulates a GNSS status first fix event. -
simulateGnssStatus
Simulates a GNSS status event. -
sendGnssStatus
Deprecated.UsesimulateGnssStatus(GnssStatus)
instead. -
simulateGnssStatusStopped
@RequiresApi(24) public void simulateGnssStatusStopped()Simulates a GNSS status stopped event. -
addNmeaListener
@Implementation(minSdk=24) protected boolean addNmeaListener(OnNmeaMessageListener listener, Handler handler) -
addNmeaListener
@Implementation(minSdk=30) protected boolean addNmeaListener(Executor executor, OnNmeaMessageListener listener) -
removeNmeaListener
-
simulateNmeaMessage
Simulates a NMEA message. -
sendNmeaMessage
Deprecated.UsesimulateNmeaMessage(String, long)
instead. -
registerGnssMeasurementsCallback
@Implementation(minSdk=24) protected boolean registerGnssMeasurementsCallback(GnssMeasurementsEvent.Callback listener, Handler handler) -
registerGnssMeasurementsCallback
@Implementation(minSdk=30) @RequiresApi(api=30) protected boolean registerGnssMeasurementsCallback(Object request, Object executor, Object callback) -
registerGnssMeasurementsCallback
@Implementation(minSdk=30) protected boolean registerGnssMeasurementsCallback(Executor executor, GnssMeasurementsEvent.Callback listener) -
unregisterGnssMeasurementsCallback
@Implementation(minSdk=24) protected void unregisterGnssMeasurementsCallback(GnssMeasurementsEvent.Callback listener) -
simulateGnssMeasurementsEvent
Simulates a GNSS measurements event. -
sendGnssMeasurementsEvent
Deprecated.UsesimulateGnssMeasurementsEvent(GnssMeasurementsEvent)
instead. -
simulateGnssMeasurementsStatus
@RequiresApi(api=24) public void simulateGnssMeasurementsStatus(int status) Simulates a GNSS measurements status change. -
registerAntennaInfoListener
@Implementation(minSdk=30) protected Object registerAntennaInfoListener(Object executor, Object listener) -
unregisterAntennaInfoListener
-
simulateGnssAntennaInfo
Simulates a GNSS antenna info event. -
sendGnssAntennaInfo
@Deprecated @RequiresApi(api=30) public void sendGnssAntennaInfo(List<GnssAntennaInfo> antennaInfos) Deprecated.UsesimulateGnssAntennaInfo(List)
instead. -
simulateLocation
A convenience function equivalent to invoking#simulateLocation(String, Location)
with the provider of the given location. -
simulateLocation
Delivers to the given provider (which will be created if necessary) a new location which will be delivered to appropriate listeners and updates state accordingly. Delivery will ignore the enabled/disabled state of providers, unlike location on a real device.The location will also be delivered to the passive provider.
-
getRequestLocationUpdateListeners
Deprecated.Do not test listeners, instead usesimulateLocation(Location)
and test the results of those listeners being invoked. -
getLocationUpdateListeners
Deprecated.Do not test listeners, instead usesimulateLocation(Location)
and test the results of those listeners being invoked. -
getLocationUpdateListeners
Deprecated.Do not test listeners, instead usesimulateLocation(Location)
and test the results of those listeners being invoked. -
getLocationUpdatePendingIntents
Deprecated.Do not test pending intents, instead usesimulateLocation(Location)
and test the results of those pending intent being invoked. -
getLocationUpdatePendingIntents
Deprecated.Do not test pending intents, instead usesimulateLocation(Location)
and test the results of those pending intent being invoked.Retrieves a list of all currently registered pending intents for the given provider. -
reset
-
simulateLocation(Location)
and test the results of those listeners being invoked.