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.
  • Constructor Details

    • ShadowLocationManager

      public ShadowLocationManager()
  • Method Details

    • 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(String provider)
    • 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 via setLocationEnabled(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 to Settings.Secure.LOCATION_MODE_HIGH_ACCURACY or Settings.Secure.LOCATION_MODE_OFF.
    • setLocationMode

      public void setLocationMode(int locationMode)
      On pre-P devices, sets the device location mode. For P and above, use setLocationEnabled(boolean) and setProviderEnabled(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.
      Use simulateLocation(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

      @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=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

      @Implementation protected void removeUpdates(LocationListener listener)
    • removeUpdates

      @Implementation protected void removeUpdates(PendingIntent pendingIntent)
    • 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

      public List<LocationRequest> getLocationRequests(String provider)
      Returns the list of LocationRequest 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 can use this method on any supported SDK.

      Prior to Android S LocationRequest equality is not well defined, so prefer using getLegacyLocationRequests(String) instead if equality is required for testing.

    • getLegacyLocationRequests

      public List<ShadowLocationManager.RoboLocationRequest> getLegacyLocationRequests(String provider)
      Returns the list of ShadowLocationManager.RoboLocationRequest currently registered under the given provider. Since LocationRequest was not publicly visible prior to S, ShadowLocationManager.RoboLocationRequest allows 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)
    • getGnssBatchSize

      @Implementation(minSdk=26) protected int 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

      @Implementation(minSdk=26) protected void flushGnssBatch()
    • unregisterGnssBatchedLocationCallback

      @Implementation(minSdk=26) protected boolean unregisterGnssBatchedLocationCallback(Object callback)
    • getGnssHardwareModelName

      @Implementation(minSdk=28) @Nullable protected String getGnssHardwareModelName()
    • setGnssHardwareModelName

      public void setGnssHardwareModelName(@Nullable String gnssHardwareModelName)
      Sets the GNSS hardware model name returned by LocationManager.getGnssHardwareModelName().
    • getGnssYearOfHardware

      @Implementation(minSdk=28) protected int getGnssYearOfHardware()
    • setGnssYearOfHardware

      public void setGnssYearOfHardware(int gnssYearOfHardware)
      Sets the GNSS year of hardware returned by LocationManager.getGnssYearOfHardware().
    • 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 registered GpsStatus.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

      @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

      @RequiresApi(24) public void simulateGnssStatus(GnssStatus status)
      Simulates a GNSS status event.
    • sendGnssStatus

      @Deprecated @RequiresApi(24) public void sendGnssStatus(GnssStatus status)
      Deprecated.
    • 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

      @Implementation(minSdk=24) protected void removeNmeaListener(OnNmeaMessageListener listener)
    • simulateNmeaMessage

      @RequiresApi(api=24) public void simulateNmeaMessage(String message, long timestamp)
      Simulates a NMEA message.
    • sendNmeaMessage

      @Deprecated @RequiresApi(api=24) public void sendNmeaMessage(String message, long timestamp)
      Deprecated.
    • 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

      @RequiresApi(api=24) public void simulateGnssMeasurementsEvent(GnssMeasurementsEvent event)
      Simulates a GNSS measurements event.
    • sendGnssMeasurementsEvent

      @Deprecated @RequiresApi(api=24) public void sendGnssMeasurementsEvent(GnssMeasurementsEvent event)
    • simulateGnssMeasurementsStatus

      @RequiresApi(api=24) public void simulateGnssMeasurementsStatus(int status)
      Simulates a GNSS measurements status change.
    • registerAntennaInfoListener

      @Implementation(minSdk=30) protected boolean registerAntennaInfoListener(Executor executor, Object listener)
    • unregisterAntennaInfoListener

      @Implementation(minSdk=30) protected void unregisterAntennaInfoListener(Object listener)
    • simulateGnssAntennaInfo

      @RequiresApi(api=30) public void simulateGnssAntennaInfo(List<GnssAntennaInfo> antennaInfos)
      Simulates a GNSS antenna info event.
    • sendGnssAntennaInfo

      @Deprecated @RequiresApi(api=30) public void sendGnssAntennaInfo(List<GnssAntennaInfo> antennaInfos)
      Deprecated.
    • simulateLocation

      public void simulateLocation(Location location)
      A convenience function equivalent to invoking simulateLocation(Location) with the provider of the given location.
    • simulateLocation

      public 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. 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 use simulateLocation(Location) and test the results of those listeners being invoked.
    • getLocationUpdateListeners

      @Deprecated public List<LocationListener> getLocationUpdateListeners()
      Deprecated.
      Do not test listeners, instead use simulateLocation(Location) and test the results of those listeners being invoked.
    • getLocationUpdateListeners

      @Deprecated public List<LocationListener> getLocationUpdateListeners(String provider)
      Deprecated.
      Do not test listeners, instead use simulateLocation(Location) and test the results of those listeners being invoked.
    • getLocationUpdatePendingIntents

      @Deprecated public List<PendingIntent> getLocationUpdatePendingIntents()
      Deprecated.
      Do not test pending intents, instead use simulateLocation(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 use simulateLocation(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()