|
| 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 | +} |
0 commit comments