Skip to content

Commit a1e1d1d

Browse files
feat: Add support of extended Android geolocation (#1492)
1 parent 93a95fc commit a1e1d1d

File tree

3 files changed

+167
-1
lines changed

3 files changed

+167
-1
lines changed

src/main/java/io/appium/java_client/android/AndroidDriver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.appium.java_client.HasOnScreenKeyboard;
3434
import io.appium.java_client.LocksDevice;
3535
import io.appium.java_client.android.connection.HasNetworkConnection;
36+
import io.appium.java_client.android.geolocation.SupportsExtendedGeolocationCommands;
3637
import io.appium.java_client.android.nativekey.PressesKey;
3738
import io.appium.java_client.battery.HasBattery;
3839
import io.appium.java_client.remote.MobilePlatform;
@@ -68,7 +69,7 @@ public class AndroidDriver<T extends WebElement>
6869
HasSupportedPerformanceDataType, AuthenticatesByFinger, HasOnScreenKeyboard,
6970
CanRecordScreen, SupportsSpecialEmulatorCommands,
7071
SupportsNetworkStateManagement, ListensToLogcatMessages, HasAndroidClipboard,
71-
HasBattery<AndroidBatteryInfo>, ExecuteCDPCommand {
72+
HasBattery<AndroidBatteryInfo>, ExecuteCDPCommand, SupportsExtendedGeolocationCommands {
7273

7374
private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;
7475

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.android.geolocation;
18+
19+
import com.google.common.collect.ImmutableMap;
20+
21+
import java.util.Map;
22+
23+
import static java.util.Optional.ofNullable;
24+
25+
public class AndroidGeoLocation {
26+
private Double longitude;
27+
private Double latitude;
28+
private Double altitude;
29+
private Integer satellites;
30+
private Double speed;
31+
32+
/**
33+
* Initializes AndroidLocation instance.
34+
*/
35+
public AndroidGeoLocation() {
36+
37+
}
38+
39+
/**
40+
* Initializes AndroidLocation instance with longitude and latitude values.
41+
*
42+
* @param longitude longitude value
43+
* @param latitude latitude value
44+
*/
45+
public AndroidGeoLocation(double longitude, double latitude) {
46+
this.longitude = longitude;
47+
this.latitude = latitude;
48+
}
49+
50+
/**
51+
* Sets geo longitude value. This value is required to set.
52+
*
53+
* @param longitude geo longitude
54+
* @return self instance for chaining
55+
*/
56+
public AndroidGeoLocation withLongitude(double longitude) {
57+
this.longitude = longitude;
58+
return this;
59+
}
60+
61+
/**
62+
* Sets geo latitude value. This value is required to set.
63+
*
64+
* @param latitude geo latitude
65+
* @return self instance for chaining
66+
*/
67+
public AndroidGeoLocation withLatitude(double latitude) {
68+
this.latitude = latitude;
69+
return this;
70+
}
71+
72+
/**
73+
* Sets geo altitude value.
74+
*
75+
* @param altitude geo altitude
76+
* @return self instance for chaining
77+
*/
78+
public AndroidGeoLocation withAltitude(double altitude) {
79+
this.altitude = altitude;
80+
return this;
81+
}
82+
83+
/**
84+
* Sets the number of geo satellites being tracked.
85+
* This number is respected on Emulators.
86+
*
87+
* @param satellites the count of satellites in range 1..12
88+
* @return self instance for chaining
89+
*/
90+
public AndroidGeoLocation withSatellites(int satellites) {
91+
this.satellites = satellites;
92+
return this;
93+
}
94+
95+
/**
96+
* Sets the movement speed. It is measured in meters/second
97+
* for real devices and in knots for emulators.
98+
*
99+
* @param speed the actual speed, which should be greater than zero
100+
* @return self instance for chaining
101+
*/
102+
public AndroidGeoLocation withSpeed(double speed) {
103+
this.speed = speed;
104+
return this;
105+
}
106+
107+
/**
108+
* Builds parameters map suitable for passing to the downstream API.
109+
*
110+
* @return Parameters mapping
111+
*/
112+
public Map<String, ?> build() {
113+
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
114+
ofNullable(longitude).map(x -> builder.put("longitude", x))
115+
.orElseThrow(() -> new IllegalArgumentException(
116+
"A valid 'longitude' must be provided"));
117+
ofNullable(latitude).map(x -> builder.put("latitude", x))
118+
.orElseThrow(() -> new IllegalArgumentException(
119+
"A valid 'latitude' must be provided"));
120+
ofNullable(altitude).map(x -> builder.put("altitude", x));
121+
ofNullable(satellites).map(x -> builder.put("satellites", x));
122+
ofNullable(speed).map(x -> builder.put("speed", x));
123+
return builder.build();
124+
}
125+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.android.geolocation;
18+
19+
import com.google.common.collect.ImmutableMap;
20+
import io.appium.java_client.CommandExecutionHelper;
21+
import io.appium.java_client.ExecutesMethod;
22+
import org.openqa.selenium.remote.DriverCommand;
23+
24+
import java.util.AbstractMap;
25+
26+
public interface SupportsExtendedGeolocationCommands extends ExecutesMethod {
27+
28+
/**
29+
* Allows to set geo location with extended parameters
30+
* available for Android platform.
31+
*
32+
* @param location The location object to set.
33+
*/
34+
default void setLocation(AndroidGeoLocation location) {
35+
ImmutableMap<String, ?> parameters = ImmutableMap
36+
.of("location", location.build());
37+
CommandExecutionHelper.execute(this,
38+
new AbstractMap.SimpleEntry<>(DriverCommand.SET_LOCATION, parameters));
39+
}
40+
}

0 commit comments

Comments
 (0)