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
Nested Classes Modifier and Type Class Description static class
ShadowLocationManager.ProviderProperties
ProviderProperties is not public prior to S, so a new class is required to represent it prior to that platform.static class
ShadowLocationManager.RoboLocationRequest
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
Constructors Constructor Description ShadowLocationManager()
-
Method Summary
Modifier and Type Method Description protected boolean
addGpsStatusListener(GpsStatus.Listener listener)
protected boolean
addNmeaListener(OnNmeaMessageListener listener, Handler handler)
protected boolean
addNmeaListener(Executor executor, OnNmeaMessageListener listener)
protected List<String>
getAllProviders()
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 String
getGnssHardwareModelName()
protected int
getGnssYearOfHardware()
List<GpsStatus.Listener>
getGpsStatusListeners()
Returns the list of currently registeredGpsStatus.Listener
s.protected Location
getLastKnownLocation(String provider)
List<ShadowLocationManager.RoboLocationRequest>
getLegacyLocationRequests(String provider)
Returns the list ofShadowLocationManager.RoboLocationRequest
currently registered under the given provider.List<LocationRequest>
getLocationRequests(String provider)
Returns the list ofLocationRequest
currently 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 LocationProvider
getProvider(String name)
protected Object
getProviderProperties(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 boolean
hasProvider(String provider)
protected boolean
injectLocation(Location location)
protected boolean
isLocationEnabled()
protected boolean
isLocationEnabledForUser(UserHandle userHandle)
protected boolean
isProviderEnabled(String provider)
protected Object
registerAntennaInfoListener(Object executor, Object listener)
protected boolean
registerGnssMeasurementsCallback(GnssMeasurementsEvent.Callback listener, Handler handler)
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
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
sendGnssMeasurementsEvent(GnssMeasurementsEvent event)
Deprecated.UsesimulateGnssMeasurementsEvent(GnssMeasurementsEvent)
instead.void
sendGnssStatus(GnssStatus status)
Deprecated.UsesimulateGnssStatus(GnssStatus)
instead.void
sendNmeaMessage(String message, long timestamp)
Deprecated.UsesimulateNmeaMessage(String, long)
instead.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
simulateGnssMeasurementsEvent(GnssMeasurementsEvent event)
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
simulateGnssStatusStarted()
Simulates a GNSS status started event.void
simulateGnssStatusStopped()
Simulates a GNSS status stopped event.void
simulateLocation(Location location)
A convenience function equivalent to invokingsimulateLocation(String, Location)
with the provider of the given location.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.void
simulateNmeaMessage(String message, long timestamp)
Simulates a NMEA message.protected void
unregisterAntennaInfoListener(Object listener)
protected void
unregisterGnssMeasurementsCallback(GnssMeasurementsEvent.Callback listener)
protected void
unregisterGnssStatusCallback(GnssStatus.Callback listener)
-
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
@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
-
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
-
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
-
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
public void simulateGnssStatusStarted()Simulates a GNSS status started event. -
simulateGnssStatusFirstFix
public void simulateGnssStatusFirstFix(int ttff)Simulates a GNSS status first fix event. -
simulateGnssStatus
Simulates a GNSS status event. -
sendGnssStatus
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
-
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) 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
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.UsesimulateGnssAntennaInfo(List)
instead. -
simulateLocation
A convenience function equivalent to invokingsimulateLocation(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
-