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 forLocationManager. Note that the default state of location on Android devices is location on, gps provider enabled, network provider disabled.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classShadowLocationManager.ProviderPropertiesProviderProperties is not public prior to S, so a new class is required to represent it prior to that platform.static classShadowLocationManager.RoboLocationRequestLocationRequest 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
Constructors Constructor Description ShadowLocationManager()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected booleanaddGpsStatusListener(GpsStatus.Listener listener)protected booleanaddNmeaListener(OnNmeaMessageListener listener, Handler handler)protected booleanaddNmeaListener(Executor executor, OnNmeaMessageListener listener)protected List<String>getAllProviders()protected 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)List<GpsStatus.Listener>getGpsStatusListeners()Returns the list of currently registeredGpsStatus.Listeners.protected LocationgetLastKnownLocation(String provider)List<ShadowLocationManager.RoboLocationRequest>getLegacyLocationRequests(String provider)Returns the list ofShadowLocationManager.RoboLocationRequestcurrently registered under the given provider.List<LocationRequest>getLocationRequests(String provider)Returns the list ofLocationRequestcurrently registered under the given provider.List<LocationListener>getLocationUpdateListeners()Deprecated.Do not test listeners, instead usesimulateLocation(Location)and test the results of those listeners being invoked.List<LocationListener>getLocationUpdateListeners(String provider)Deprecated.Do not test listeners, instead usesimulateLocation(Location)and test the results of those listeners being invoked.List<PendingIntent>getLocationUpdatePendingIntents()Deprecated.Do not test pending intents, instead usesimulateLocation(Location)and test the results of those pending intent being invoked.List<PendingIntent>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(Object providerStr)protected List<String>getProviders(boolean enabledOnly)protected List<String>getProviders(Criteria criteria, boolean enabled)List<LocationListener>getRequestLocationUpdateListeners()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 booleanisLocationEnabled()protected booleanisLocationEnabledForUser(UserHandle userHandle)protected booleanisProviderEnabled(String provider)protected ObjectregisterAntennaInfoListener(Object executor, Object listener)protected booleanregisterGnssMeasurementsCallback(GnssMeasurementsEvent.Callback listener, Handler handler)protected booleanregisterGnssMeasurementsCallback(Executor executor, GnssMeasurementsEvent.Callback listener)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 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.voidsendGnssMeasurementsEvent(GnssMeasurementsEvent event)Deprecated.UsesimulateGnssMeasurementsEvent(GnssMeasurementsEvent)instead.voidsendGnssStatus(GnssStatus status)Deprecated.UsesimulateGnssStatus(GnssStatus)instead.voidsendNmeaMessage(String message, long timestamp)Deprecated.UsesimulateNmeaMessage(String, long)instead.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.voidsimulateGnssMeasurementsEvent(GnssMeasurementsEvent event)Simulates 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.voidsimulateGnssStatusStarted()Simulates a GNSS status started event.voidsimulateGnssStatusStopped()Simulates a GNSS status stopped event.voidsimulateLocation(Location location)A convenience function equivalent to invokingsimulateLocation(String, Location)with the provider of the given location.voidsimulateLocation(String provider, Location location)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 voidunregisterGnssMeasurementsCallback(GnssMeasurementsEvent.Callback listener)protected voidunregisterGnssStatusCallback(GnssStatus.Callback listener)
-
-
-
Method Detail
-
getAllProviders
@Implementation protected List<String> getAllProviders()
-
getProvider
@Implementation @Nullable protected LocationProvider getProvider(String name)
-
getProviders
@Implementation protected List<String> getProviders(boolean enabledOnly)
-
getProviders
@Implementation protected List<String> getProviders(@Nullable Criteria criteria, boolean enabled)
-
getBestProvider
@Implementation @Nullable protected String getBestProvider(Criteria criteria, boolean enabled)
-
getProviderProperties
@Implementation(minSdk=31) @Nullable protected Object getProviderProperties(Object providerStr)
-
hasProvider
@Implementation(minSdk=31) protected boolean hasProvider(String provider)
-
isProviderEnabled
@Implementation protected boolean isProviderEnabled(String provider)
-
removeProvider
public void removeProvider(String name)
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
public void setProviderEnabled(String name, boolean enabled)
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
@Implementation(minSdk=28) protected boolean isLocationEnabledForUser(UserHandle userHandle)
-
isLocationEnabled
@Implementation(minSdk=28) protected boolean 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
@Implementation @Nullable protected Location getLastKnownLocation(String provider)
-
setLastKnownLocation
@Deprecated public void setLastKnownLocation(String provider, @Nullable Location location)
Deprecated.UsesimulateLocation(Location)to update the last location for a provider.
-
getCurrentLocation
@Implementation(minSdk=30) protected void getCurrentLocation(String provider, @Nullable CancellationSignal cancellationSignal, Executor executor, Consumer<Location> consumer)
-
getCurrentLocation
@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
@Implementation protected void requestSingleUpdate(String provider, PendingIntent pendingIntent)
-
requestSingleUpdate
@Implementation protected void requestSingleUpdate(Criteria criteria, PendingIntent pendingIntent)
-
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=19) 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
@Implementation protected void removeUpdates(LocationListener listener)
-
removeUpdates
@Implementation protected void removeUpdates(PendingIntent pendingIntent)
-
getLocationRequests
public List<LocationRequest> getLocationRequests(String provider)
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 may only use this method on Kitkat+.Prior to Android S
LocationRequestequality is not well defined, so prefer usinggetLegacyLocationRequests(String)instead if equality is required for testing.
-
getLegacyLocationRequests
public List<ShadowLocationManager.RoboLocationRequest> getLegacyLocationRequests(String provider)
Returns the list ofShadowLocationManager.RoboLocationRequestcurrently registered under the given provider. SinceLocationRequestwas not publicly visible prior to S, and did not exist prior to Kitkat,ShadowLocationManager.RoboLocationRequestallows querying the location requests prior to those platforms, and also implements proper equality comparisons for testing.
-
injectLocation
@Implementation(minSdk=28) protected boolean injectLocation(Location location)
-
addGpsStatusListener
@Implementation protected boolean addGpsStatusListener(GpsStatus.Listener listener)
-
removeGpsStatusListener
@Implementation protected void removeGpsStatusListener(GpsStatus.Listener listener)
-
getGpsStatusListeners
public List<GpsStatus.Listener> 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
public void simulateGnssStatusStarted()
Simulates a GNSS status started event.
-
simulateGnssStatusFirstFix
public void simulateGnssStatusFirstFix(int ttff)
Simulates a GNSS status first fix event.
-
simulateGnssStatus
public void simulateGnssStatus(GnssStatus status)
Simulates a GNSS status event.
-
sendGnssStatus
@Deprecated public void sendGnssStatus(GnssStatus status)
Deprecated.UsesimulateGnssStatus(GnssStatus)instead.
-
simulateGnssStatusStopped
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
@Implementation(minSdk=24) protected void removeNmeaListener(OnNmeaMessageListener listener)
-
simulateNmeaMessage
public void simulateNmeaMessage(String message, long timestamp)
Simulates a NMEA message.
-
sendNmeaMessage
@Deprecated public void sendNmeaMessage(String message, long timestamp)
Deprecated.UsesimulateNmeaMessage(String, long)instead.
-
registerGnssMeasurementsCallback
@Implementation(minSdk=24) protected boolean registerGnssMeasurementsCallback(GnssMeasurementsEvent.Callback listener, Handler handler)
-
registerGnssMeasurementsCallback
@Implementation(minSdk=30) protected boolean registerGnssMeasurementsCallback(Executor executor, GnssMeasurementsEvent.Callback listener)
-
unregisterGnssMeasurementsCallback
@Implementation(minSdk=24) protected void unregisterGnssMeasurementsCallback(GnssMeasurementsEvent.Callback listener)
-
simulateGnssMeasurementsEvent
public void simulateGnssMeasurementsEvent(GnssMeasurementsEvent event)
Simulates a GNSS measurements event.
-
sendGnssMeasurementsEvent
@Deprecated public void sendGnssMeasurementsEvent(GnssMeasurementsEvent event)
Deprecated.UsesimulateGnssMeasurementsEvent(GnssMeasurementsEvent)instead.
-
simulateGnssMeasurementsStatus
public void simulateGnssMeasurementsStatus(int status)
Simulates a GNSS measurements status change.
-
registerAntennaInfoListener
@Implementation(minSdk=30) protected Object registerAntennaInfoListener(Object executor, Object listener)
-
unregisterAntennaInfoListener
@Implementation(minSdk=30) protected void unregisterAntennaInfoListener(Object listener)
-
simulateGnssAntennaInfo
public void simulateGnssAntennaInfo(List<GnssAntennaInfo> antennaInfos)
Simulates a GNSS antenna info event.
-
sendGnssAntennaInfo
@Deprecated public void sendGnssAntennaInfo(List<GnssAntennaInfo> antennaInfos)
Deprecated.UsesimulateGnssAntennaInfo(List)instead.
-
simulateLocation
public void simulateLocation(Location location)
A convenience function equivalent to invokingsimulateLocation(String, Location)with the provider of the given location.
-
simulateLocation
public void simulateLocation(String provider, Location location)
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 public List<LocationListener> getRequestLocationUpdateListeners()
Deprecated.Do not test listeners, instead usesimulateLocation(Location)and test the results of those listeners being invoked.
-
getLocationUpdateListeners
@Deprecated public List<LocationListener> getLocationUpdateListeners()
Deprecated.Do not test listeners, instead usesimulateLocation(Location)and test the results of those listeners being invoked.
-
getLocationUpdateListeners
@Deprecated public List<LocationListener> getLocationUpdateListeners(String provider)
Deprecated.Do not test listeners, instead usesimulateLocation(Location)and test the results of those listeners being invoked.
-
getLocationUpdatePendingIntents
@Deprecated public List<PendingIntent> getLocationUpdatePendingIntents()
Deprecated.Do not test pending intents, instead usesimulateLocation(Location)and test the results of those pending intent being invoked.
-
getLocationUpdatePendingIntents
@Deprecated public List<PendingIntent> getLocationUpdatePendingIntents(String provider)
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
@Resetter public static void reset()
-
-