Package org.robolectric.shadows
Class ShadowLocationManager
java.lang.Object
org.robolectric.shadows.ShadowLocationManager
@Implements(android.location.LocationManager.class)
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
Nested ClassesModifier and TypeClassDescriptionstatic classProviderProperties is not public prior to S, so a new class is required to represent it prior to that platform.static final classLocationRequest is not public prior to S, so a new class is required to represent it prior to those platforms. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanaddGpsStatusListener(GpsStatus.Listener listener) protected booleanaddNmeaListener(OnNmeaMessageListener listener, Handler handler) protected booleanaddNmeaListener(Executor executor, OnNmeaMessageListener listener) protected voidprotected StringgetBestProvider(Criteria criteria, boolean enabled) protected voidgetCurrentLocation(String provider, LocationRequest request, CancellationSignal cancellationSignal, Executor executor, Consumer<Location> consumer) protected voidgetCurrentLocation(String provider, CancellationSignal cancellationSignal, Executor executor, Consumer<Location> consumer) protected intprotected Stringprotected intReturns the list of currently registeredGpsStatus.Listeners.protected LocationgetLastKnownLocation(String provider) getLegacyLocationRequests(String provider) Returns the list ofShadowLocationManager.RoboLocationRequestcurrently registered under the given provider.getLocationRequests(String provider) Returns the list ofLocationRequestcurrently 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 LocationProvidergetProvider(String name) protected ObjectgetProviderProperties(String provider) 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 booleanhasProvider(String provider) protected booleaninjectLocation(Location location) protected booleanprotected booleanisLocationEnabledForUser(UserHandle userHandle) protected booleanisProviderEnabled(String provider) protected booleanregisterAntennaInfoListener(Executor executor, Object listener) protected booleanregisterGnssBatchedLocationCallback(long periodNanos, boolean wakeOnFifoFull, Object callback, Handler handler) protected booleanregisterGnssMeasurementsCallback(GnssMeasurementsEvent.Callback listener, Handler handler) protected booleanregisterGnssMeasurementsCallback(Object request, Executor executor, Object callback) protected booleanregisterGnssMeasurementsCallback(Executor executor, GnssMeasurementsEvent.Callback listener) protected booleanregisterGnssMeasurementsCallbackFromS(Object request, Executor executor, Object callback) protected booleanregisterGnssStatusCallback(GnssStatus.Callback callback, Handler handler) protected booleanregisterGnssStatusCallback(Executor executor, GnssStatus.Callback listener) protected voidremoveGpsStatusListener(GpsStatus.Listener listener) protected voidremoveNmeaListener(OnNmeaMessageListener listener) voidremoveProvider(String name) Completely removes a provider.protected voidremoveUpdates(PendingIntent pendingIntent) protected voidremoveUpdates(LocationListener listener) protected voidrequestFlush(String provider, PendingIntent pendingIntent, int requestCode) protected voidrequestFlush(String provider, LocationListener listener, int requestCode) protected voidrequestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent pendingIntent) protected voidrequestLocationUpdates(long minTime, float minDistance, Criteria criteria, LocationListener listener, Looper looper) protected voidrequestLocationUpdates(long minTime, float minDistance, Criteria criteria, Executor executor, LocationListener listener) protected voidrequestLocationUpdates(LocationRequest request, PendingIntent pendingIntent) protected voidrequestLocationUpdates(LocationRequest request, LocationListener listener, Looper looper) protected voidrequestLocationUpdates(LocationRequest request, Executor executor, LocationListener listener) protected voidrequestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent pendingIntent) protected voidrequestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener) protected voidrequestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener, Looper looper) protected voidrequestLocationUpdates(String provider, long minTime, float minDistance, Executor executor, LocationListener listener) protected voidrequestLocationUpdates(String provider, LocationRequest request, PendingIntent pendingIntent) protected voidrequestLocationUpdates(String provider, LocationRequest request, Executor executor, LocationListener listener) protected voidrequestSingleUpdate(Criteria criteria, PendingIntent pendingIntent) protected voidrequestSingleUpdate(Criteria criteria, LocationListener listener, Looper looper) protected voidrequestSingleUpdate(String provider, PendingIntent pendingIntent) protected voidrequestSingleUpdate(String provider, LocationListener listener, Looper looper) static voidreset()voidsendGnssAntennaInfo(List<GnssAntennaInfo> antennaInfos) Deprecated.UsesimulateGnssAntennaInfo(List)instead.voidDeprecated.UsesimulateGnssMeasurementsEvent(GnssMeasurementsEvent)instead.voidsendGnssStatus(GnssStatus status) Deprecated.UsesimulateGnssStatus(GnssStatus)instead.voidsendNmeaMessage(String message, long timestamp) Deprecated.UsesimulateNmeaMessage(String, long)instead.voidsetGnssBatchSize(int gnssBatchSize) Sets the GNSS hardware batch size.voidsetGnssHardwareModelName(String gnssHardwareModelName) Sets the GNSS hardware model name returned byLocationManager.getGnssHardwareModelName().voidsetGnssYearOfHardware(int gnssYearOfHardware) Sets the GNSS year of hardware returned byLocationManager.getGnssYearOfHardware().voidsetLastKnownLocation(String provider, Location location) Deprecated.UsesimulateLocation(Location)to update the last location for a provider.voidsetLocationEnabled(boolean enabled) On P and above, turns location on or off.protected voidsetLocationEnabledForUser(boolean enabled, UserHandle userHandle) voidsetLocationMode(int locationMode) On pre-P devices, sets the device location mode.voidsetProviderEnabled(String name, boolean enabled) Sets the given provider enabled or disabled.voidsetProviderProperties(String name, ShadowLocationManager.ProviderProperties properties) Sets the properties of the given provider.voidsimulateGnssAntennaInfo(List<GnssAntennaInfo> antennaInfos) Simulates a GNSS antenna info event.voidSimulates a GNSS measurements event.voidsimulateGnssMeasurementsStatus(int status) Simulates a GNSS measurements status change.voidsimulateGnssStatus(GnssStatus status) Simulates a GNSS status event.voidsimulateGnssStatusFirstFix(int ttff) Simulates a GNSS status first fix event.voidSimulates a GNSS status started event.voidSimulates a GNSS status stopped event.voidsimulateLocation(Location location) A convenience function equivalent to invokingsimulateLocation(Location)with the provider of the given location.voidsimulateLocation(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.voidsimulateNmeaMessage(String message, long timestamp) Simulates a NMEA message.protected voidunregisterAntennaInfoListener(Object listener) protected booleanunregisterGnssBatchedLocationCallback(Object callback) protected voidprotected 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_ACCURACYorSettings.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 protected void requestLocationUpdates(@Nullable LocationRequest request, LocationListener listener, Looper looper) -
requestLocationUpdates
@Implementation 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 ofLocationRequestcurrently 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 can use this method on any supported SDK.Prior to Android S
LocationRequestequality is not well defined, so prefer usinggetLegacyLocationRequests(String)instead if equality is required for testing. -
getLegacyLocationRequests
Returns the list ofShadowLocationManager.RoboLocationRequestcurrently registered under the given provider. SinceLocationRequestwas not publicly visible prior to S,ShadowLocationManager.RoboLocationRequestallows 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
@Implementation(minSdk=26) protected boolean registerGnssBatchedLocationCallback(long periodNanos, boolean wakeOnFifoFull, Object callback, Handler handler) -
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.Listeners. -
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
Simulates a GNSS status started event. -
simulateGnssStatusFirstFix
Simulates a GNSS status first fix event. -
simulateGnssStatus
Simulates a GNSS status event. -
sendGnssStatus
Deprecated.UsesimulateGnssStatus(GnssStatus)instead. -
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, Executor executor, Object callback) -
registerGnssMeasurementsCallbackFromS
@Implementation(minSdk=31, methodName="registerGnssMeasurementsCallback") @RequiresApi(api=31) protected boolean registerGnssMeasurementsCallbackFromS(Object request, Executor 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
Simulates a GNSS measurements status change. -
registerAntennaInfoListener
@Implementation(minSdk=30) protected boolean registerAntennaInfoListener(Executor 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 invokingsimulateLocation(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.