|
16 | 16 |
|
17 | 17 | package io.appium.java_client.remote;
|
18 | 18 |
|
| 19 | +import com.google.common.collect.ImmutableMap; |
| 20 | +import io.appium.java_client.CommandExecutionHelper; |
| 21 | +import io.appium.java_client.ExecutesMethod; |
| 22 | +import io.appium.java_client.MobileCommand; |
19 | 23 | import org.openqa.selenium.WebDriver;
|
| 24 | +import org.openqa.selenium.WebDriverException; |
20 | 25 | import org.openqa.selenium.html5.Location;
|
21 | 26 | import org.openqa.selenium.html5.LocationContext;
|
22 | 27 | import org.openqa.selenium.remote.html5.RemoteLocationContext;
|
23 | 28 |
|
24 |
| -public interface SupportsLocation extends WebDriver, LocationContext { |
25 |
| - public RemoteLocationContext getLocationContext(); |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Optional; |
26 | 31 |
|
| 32 | +public interface SupportsLocation extends WebDriver, ExecutesMethod, LocationContext { |
| 33 | + |
| 34 | + /** |
| 35 | + * Provides the location context. |
| 36 | + * |
| 37 | + * @return instance of {@link RemoteLocationContext} |
| 38 | + * @deprecated This method, {@link LocationContext} and {@link RemoteLocationContext} interface are deprecated, use |
| 39 | + * {@link #getLocation()} and {@link #setLocation(io.appium.java_client.Location)} instead. |
| 40 | + */ |
| 41 | + @Deprecated(forRemoval = true) |
| 42 | + RemoteLocationContext getLocationContext(); |
| 43 | + |
| 44 | + /** |
| 45 | + * Gets the current device's geolocation coordinates. |
| 46 | + * |
| 47 | + * @return A {@link Location} containing the location information. Returns null if the location is not available |
| 48 | + * @deprecated This method and whole {@link LocationContext} interface are deprecated, use {@link #getLocation()} |
| 49 | + * instead. |
| 50 | + */ |
| 51 | + @Deprecated(forRemoval = true) |
27 | 52 | default Location location() {
|
28 | 53 | return getLocationContext().location();
|
29 | 54 | }
|
30 | 55 |
|
| 56 | + /** |
| 57 | + * Gets the current device's geolocation coordinates. |
| 58 | + * |
| 59 | + * @return A {@link Location} containing the location information. Throws {@link WebDriverException} if the |
| 60 | + * location is not available. |
| 61 | + */ |
| 62 | + default io.appium.java_client.Location getLocation() { |
| 63 | + Map<String, Number> result = CommandExecutionHelper.execute(this, MobileCommand.GET_LOCATION); |
| 64 | + return new io.appium.java_client.Location( |
| 65 | + result.get("latitude").doubleValue(), |
| 66 | + result.get("longitude").doubleValue(), |
| 67 | + Optional.ofNullable(result.get("altitude")).map(Number::doubleValue).orElse(null) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Sets the current device's geolocation coordinates. |
| 73 | + * |
| 74 | + * @param location A {@link Location} containing the new location information. |
| 75 | + * @deprecated This method and whole {@link LocationContext} interface are deprecated, use |
| 76 | + * {@link #setLocation(io.appium.java_client.Location)} instead. |
| 77 | + */ |
| 78 | + @Deprecated(forRemoval = true) |
31 | 79 | default void setLocation(Location location) {
|
32 | 80 | getLocationContext().setLocation(location);
|
33 | 81 | }
|
| 82 | + |
| 83 | + /** |
| 84 | + * Sets the current device's geolocation coordinates. |
| 85 | + * |
| 86 | + * @param location A {@link Location} containing the new location information. |
| 87 | + */ |
| 88 | + default void setLocation(io.appium.java_client.Location location) { |
| 89 | + ImmutableMap.Builder<String, Object> locationParameters = ImmutableMap.builder(); |
| 90 | + locationParameters.put("latitude", location.getLatitude()); |
| 91 | + locationParameters.put("longitude", location.getLongitude()); |
| 92 | + Optional.ofNullable(location.getAltitude()).ifPresent(altitude -> locationParameters.put("altitude", altitude)); |
| 93 | + execute(MobileCommand.SET_LOCATION, Map.of("location", locationParameters)); |
| 94 | + } |
34 | 95 | }
|
0 commit comments