1 package org.robolectric.shadows; 2 3 import android.location.Address; 4 import android.location.Geocoder; 5 import java.io.IOException; 6 import java.util.ArrayList; 7 import java.util.List; 8 import org.robolectric.annotation.Implementation; 9 import org.robolectric.annotation.Implements; 10 import org.robolectric.annotation.Resetter; 11 12 /** Shadow for {@link Geocoder}. */ 13 @Implements(Geocoder.class) 14 public final class ShadowGeocoder { 15 private static boolean isPresent = true; 16 private List<Address> fromLocation = new ArrayList<>(); 17 18 /** Returns true by default, or the last value set by {@link #setIsPresent(boolean)}. */ 19 @Implementation 20 public static boolean isPresent() { 21 return isPresent; 22 } 23 24 /** 25 * Returns an empty list by default, or the last value set by {@link #setFromLocation(List)} 26 * @param latitude it's ignored by this implementation 27 * @param longitude it's ignored by this implementation 28 * @param maxResults max number of addresses to return 29 * @throws IOException - never thrown, only keeping the same signature 30 */ 31 @Implementation 32 public List<Address> getFromLocation(double latitude, double longitude, int maxResults) throws IOException { 33 return fromLocation.subList(0, Math.min(maxResults, fromLocation.size())); 34 } 35 36 /** Statically sets the value to be returned by {@link #isPresent()}. */ 37 public static void setIsPresent(boolean value) { 38 isPresent = value; 39 } 40 41 /** Sets the value to be returned by {@link #getFromLocation(double, double, int)} */ 42 public void setFromLocation(List<Address> list) { fromLocation = list; } 43 44 @Resetter 45 public static void reset() { 46 isPresent = true; 47 } 48 } 49