Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 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 | |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 17 | package android.telephony; |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 18 | |
| 19 | import android.annotation.NonNull; |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 20 | import android.annotation.SystemApi; |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 21 | import android.text.TextUtils; |
| 22 | |
Sarah Chin | 5ea6d8a | 2019-12-19 12:37:41 -0800 | [diff] [blame] | 23 | import com.android.internal.telephony.util.TelephonyUtils; |
Meng Wang | 1dbea2f | 2020-01-30 12:25:08 -0800 | [diff] [blame] | 24 | import com.android.telephony.Rlog; |
Sarah Chin | 5ea6d8a | 2019-12-19 12:37:41 -0800 | [diff] [blame] | 25 | |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 26 | import java.util.ArrayList; |
| 27 | import java.util.List; |
| 28 | import java.util.stream.Collectors; |
| 29 | |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 30 | /** |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 31 | * This utils class is used for geo-fencing of CellBroadcast messages and is used by the cell |
| 32 | * broadcast module. |
| 33 | * |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 34 | * The coordinates used by this utils class are latitude and longitude, but some algorithms in this |
| 35 | * class only use them as coordinates on plane, so the calculation will be inaccurate. So don't use |
| 36 | * this class for anything other then geo-targeting of cellbroadcast messages. |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 37 | * |
| 38 | * More information regarding cell broadcast geo-fencing logic is laid out in 3GPP TS 23.041 and |
| 39 | * ATIS-0700041. |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 40 | * @hide |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 41 | */ |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 42 | @SystemApi |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 43 | public class CbGeoUtils { |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * This class is never instantiated |
| 47 | * @hide |
| 48 | */ |
| 49 | private CbGeoUtils() {} |
| 50 | |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 51 | /** Geometric interface. */ |
| 52 | public interface Geometry { |
| 53 | /** |
| 54 | * Determines if the given point {@code p} is inside the geometry. |
| 55 | * @param p point in latitude, longitude format. |
| 56 | * @return {@code True} if the given point is inside the geometry. |
| 57 | */ |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 58 | boolean contains(@NonNull LatLng p); |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Tolerance for determining if the value is 0. If the absolute value of a value is less than |
| 63 | * this tolerance, it will be treated as 0. |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 64 | * @hide |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 65 | */ |
| 66 | public static final double EPS = 1e-7; |
| 67 | |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 68 | /** |
| 69 | * The radius of earth. |
| 70 | * @hide |
| 71 | */ |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 72 | public static final int EARTH_RADIUS_METER = 6371 * 1000; |
| 73 | |
| 74 | private static final String TAG = "CbGeoUtils"; |
| 75 | |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 76 | // The TLV tags of WAC, defined in ATIS-0700041 5.2.3 WAC tag coding. |
| 77 | /** @hide */ |
Pengquan Meng | c3427c0 | 2019-08-12 23:09:34 -0700 | [diff] [blame] | 78 | public static final int GEO_FENCING_MAXIMUM_WAIT_TIME = 0x01; |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 79 | /** @hide */ |
Pengquan Meng | c3427c0 | 2019-08-12 23:09:34 -0700 | [diff] [blame] | 80 | public static final int GEOMETRY_TYPE_POLYGON = 0x02; |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 81 | /** @hide */ |
Pengquan Meng | c3427c0 | 2019-08-12 23:09:34 -0700 | [diff] [blame] | 82 | public static final int GEOMETRY_TYPE_CIRCLE = 0x03; |
| 83 | |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 84 | // The identifier of geometry in the encoded string. |
| 85 | /** @hide */ |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 86 | private static final String CIRCLE_SYMBOL = "circle"; |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 87 | /** @hide */ |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 88 | private static final String POLYGON_SYMBOL = "polygon"; |
| 89 | |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 90 | /** A point represented by (latitude, longitude). */ |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 91 | public static class LatLng { |
| 92 | public final double lat; |
| 93 | public final double lng; |
| 94 | |
| 95 | /** |
| 96 | * Constructor. |
| 97 | * @param lat latitude, range [-90, 90] |
| 98 | * @param lng longitude, range [-180, 180] |
| 99 | */ |
| 100 | public LatLng(double lat, double lng) { |
| 101 | this.lat = lat; |
| 102 | this.lng = lng; |
| 103 | } |
| 104 | |
| 105 | /** |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 106 | * @param p the point to subtract |
| 107 | * @return the result of the subtraction |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 108 | */ |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 109 | @NonNull |
| 110 | public LatLng subtract(@NonNull LatLng p) { |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 111 | return new LatLng(lat - p.lat, lng - p.lng); |
| 112 | } |
| 113 | |
| 114 | /** |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 115 | * Calculate the distance in meters between this point and the given point {@code p}. |
| 116 | * @param p the point used to calculate the distance. |
| 117 | * @return the distance in meters. |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 118 | */ |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 119 | public double distance(@NonNull LatLng p) { |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 120 | double dlat = Math.sin(0.5 * Math.toRadians(lat - p.lat)); |
| 121 | double dlng = Math.sin(0.5 * Math.toRadians(lng - p.lng)); |
| 122 | double x = dlat * dlat |
| 123 | + dlng * dlng * Math.cos(Math.toRadians(lat)) * Math.cos(Math.toRadians(p.lat)); |
| 124 | return 2 * Math.atan2(Math.sqrt(x), Math.sqrt(1 - x)) * EARTH_RADIUS_METER; |
| 125 | } |
Pengquan Meng | c3427c0 | 2019-08-12 23:09:34 -0700 | [diff] [blame] | 126 | |
| 127 | @Override |
| 128 | public String toString() { |
| 129 | return "(" + lat + "," + lng + ")"; |
| 130 | } |
Jordan Liu | 1486437 | 2020-04-08 14:42:02 -0700 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * @hide |
| 134 | */ |
| 135 | @Override |
| 136 | public boolean equals(Object o) { |
| 137 | if (o == this) { |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | if (!(o instanceof LatLng)) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | LatLng l = (LatLng) o; |
| 146 | return lat == l.lat && lng == l.lng; |
| 147 | } |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 151 | * A class representing a simple polygon with at least 3 points. This is used for geo-fencing |
| 152 | * logic with cell broadcasts. More information regarding cell broadcast geo-fencing logic is |
| 153 | * laid out in 3GPP TS 23.041 and ATIS-0700041. |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 154 | */ |
| 155 | public static class Polygon implements Geometry { |
| 156 | /** |
| 157 | * In order to reduce the loss of precision in floating point calculations, all vertices |
| 158 | * of the polygon are scaled. Set the value of scale to 1000 can take into account the |
| 159 | * actual distance accuracy of 1 meter if the EPS is 1e-7 during the calculation. |
| 160 | */ |
| 161 | private static final double SCALE = 1000.0; |
| 162 | |
| 163 | private final List<LatLng> mVertices; |
| 164 | private final List<Point> mScaledVertices; |
| 165 | private final LatLng mOrigin; |
| 166 | |
| 167 | /** |
| 168 | * Constructs a simple polygon from the given vertices. The adjacent two vertices are |
| 169 | * connected to form an edge of the polygon. The polygon has at least 3 vertices, and the |
| 170 | * last vertices and the first vertices must be adjacent. |
| 171 | * |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 172 | * The longitude difference in the vertices should be less than 180 degrees. |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 173 | */ |
| 174 | public Polygon(@NonNull List<LatLng> vertices) { |
| 175 | mVertices = vertices; |
| 176 | |
| 177 | // Find the point with smallest longitude as the mOrigin point. |
| 178 | int idx = 0; |
| 179 | for (int i = 1; i < vertices.size(); i++) { |
| 180 | if (vertices.get(i).lng < vertices.get(idx).lng) { |
| 181 | idx = i; |
| 182 | } |
| 183 | } |
| 184 | mOrigin = vertices.get(idx); |
| 185 | |
| 186 | mScaledVertices = vertices.stream() |
| 187 | .map(latLng -> convertAndScaleLatLng(latLng)) |
| 188 | .collect(Collectors.toList()); |
| 189 | } |
| 190 | |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 191 | /** |
| 192 | * Return the list of vertices which compose the polygon. |
| 193 | */ |
| 194 | public @NonNull List<LatLng> getVertices() { |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 195 | return mVertices; |
| 196 | } |
| 197 | |
| 198 | /** |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 199 | * Check if the given LatLng is inside the polygon. |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 200 | * |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 201 | * If a LatLng is on the edge of the polygon, it is also considered to be inside the |
| 202 | * polygon. |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 203 | */ |
| 204 | @Override |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 205 | public boolean contains(@NonNull LatLng latLng) { |
| 206 | // This method counts the number of times the polygon winds around the point P, A.K.A |
| 207 | // "winding number". The point is outside only when this "winding number" is 0. |
| 208 | |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 209 | Point p = convertAndScaleLatLng(latLng); |
| 210 | |
| 211 | int n = mScaledVertices.size(); |
| 212 | int windingNumber = 0; |
| 213 | for (int i = 0; i < n; i++) { |
| 214 | Point a = mScaledVertices.get(i); |
| 215 | Point b = mScaledVertices.get((i + 1) % n); |
| 216 | |
| 217 | // CCW is counterclockwise |
| 218 | // CCW = ab x ap |
| 219 | // CCW > 0 -> ap is on the left side of ab |
| 220 | // CCW == 0 -> ap is on the same line of ab |
| 221 | // CCW < 0 -> ap is on the right side of ab |
| 222 | int ccw = sign(crossProduct(b.subtract(a), p.subtract(a))); |
| 223 | |
| 224 | if (ccw == 0) { |
| 225 | if (Math.min(a.x, b.x) <= p.x && p.x <= Math.max(a.x, b.x) |
| 226 | && Math.min(a.y, b.y) <= p.y && p.y <= Math.max(a.y, b.y)) { |
| 227 | return true; |
| 228 | } |
| 229 | } else { |
| 230 | if (sign(a.y - p.y) <= 0) { |
| 231 | // upward crossing |
| 232 | if (ccw > 0 && sign(b.y - p.y) > 0) { |
| 233 | ++windingNumber; |
| 234 | } |
| 235 | } else { |
| 236 | // downward crossing |
| 237 | if (ccw < 0 && sign(b.y - p.y) <= 0) { |
| 238 | --windingNumber; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | return windingNumber != 0; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Move the given point {@code latLng} to the coordinate system with {@code mOrigin} as the |
| 248 | * origin and scale it. {@code mOrigin} is selected from the vertices of a polygon, it has |
| 249 | * the smallest longitude value among all of the polygon vertices. |
| 250 | * |
| 251 | * @param latLng the point need to be converted and scaled. |
| 252 | * @Return a {@link Point} object. |
| 253 | */ |
| 254 | private Point convertAndScaleLatLng(LatLng latLng) { |
| 255 | double x = latLng.lat - mOrigin.lat; |
| 256 | double y = latLng.lng - mOrigin.lng; |
| 257 | |
| 258 | // If the point is in different hemispheres(western/eastern) than the mOrigin, and the |
| 259 | // edge between them cross the 180th meridian, then its relative coordinates will be |
| 260 | // extended. |
| 261 | // For example, suppose the longitude of the mOrigin is -178, and the longitude of the |
| 262 | // point to be converted is 175, then the longitude after the conversion is -8. |
| 263 | // calculation: (-178 - 8) - (-178). |
| 264 | if (sign(mOrigin.lng) != 0 && sign(mOrigin.lng) != sign(latLng.lng)) { |
| 265 | double distCross0thMeridian = Math.abs(mOrigin.lng) + Math.abs(latLng.lng); |
| 266 | if (sign(distCross0thMeridian * 2 - 360) > 0) { |
| 267 | y = sign(mOrigin.lng) * (360 - distCross0thMeridian); |
| 268 | } |
| 269 | } |
| 270 | return new Point(x * SCALE, y * SCALE); |
| 271 | } |
| 272 | |
| 273 | private static double crossProduct(Point a, Point b) { |
| 274 | return a.x * b.y - a.y * b.x; |
| 275 | } |
| 276 | |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 277 | /** @hide */ |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 278 | static final class Point { |
| 279 | public final double x; |
| 280 | public final double y; |
| 281 | |
| 282 | Point(double x, double y) { |
| 283 | this.x = x; |
| 284 | this.y = y; |
| 285 | } |
| 286 | |
| 287 | public Point subtract(Point p) { |
| 288 | return new Point(x - p.x, y - p.y); |
| 289 | } |
| 290 | } |
Jack Yu | 35eed63 | 2019-11-08 09:32:17 -0800 | [diff] [blame] | 291 | |
| 292 | @Override |
| 293 | public String toString() { |
| 294 | String str = "Polygon: "; |
Sarah Chin | 5ea6d8a | 2019-12-19 12:37:41 -0800 | [diff] [blame] | 295 | if (TelephonyUtils.IS_DEBUGGABLE) { |
Jack Yu | 35eed63 | 2019-11-08 09:32:17 -0800 | [diff] [blame] | 296 | str += mVertices; |
| 297 | } |
| 298 | return str; |
| 299 | } |
Jordan Liu | 1486437 | 2020-04-08 14:42:02 -0700 | [diff] [blame] | 300 | |
| 301 | /** |
| 302 | * @hide |
| 303 | */ |
| 304 | @Override |
| 305 | public boolean equals(Object o) { |
| 306 | if (o == this) { |
| 307 | return true; |
| 308 | } |
| 309 | |
| 310 | if (!(o instanceof Polygon)) { |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | Polygon p = (Polygon) o; |
| 315 | if (mVertices.size() != p.mVertices.size()) { |
| 316 | return false; |
| 317 | } |
| 318 | for (int i = 0; i < mVertices.size(); i++) { |
| 319 | if (!mVertices.get(i).equals(p.mVertices.get(i))) { |
| 320 | return false; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | return true; |
| 325 | } |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 328 | /** |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 329 | * A class represents a {@link Geometry} in the shape of a Circle. This is used for handling |
| 330 | * geo-fenced cell broadcasts. More information regarding cell broadcast geo-fencing logic is |
| 331 | * laid out in 3GPP TS 23.041 and ATIS-0700041. |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 332 | */ |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 333 | public static class Circle implements Geometry { |
| 334 | private final LatLng mCenter; |
| 335 | private final double mRadiusMeter; |
| 336 | |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 337 | /** |
| 338 | * Construct a Circle given a center point and a radius in meters. |
| 339 | * |
| 340 | * @param center the latitude and longitude of the center of the circle |
| 341 | * @param radiusInMeters the radius of the circle in meters |
| 342 | */ |
| 343 | public Circle(@NonNull LatLng center, double radiusInMeters) { |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 344 | this.mCenter = center; |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 345 | this.mRadiusMeter = radiusInMeters; |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 348 | /** |
| 349 | * Return the latitude and longitude of the center of the circle; |
| 350 | */ |
| 351 | public @NonNull LatLng getCenter() { |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 352 | return mCenter; |
| 353 | } |
| 354 | |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 355 | /** |
| 356 | * Return the radius of the circle in meters. |
| 357 | */ |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 358 | public double getRadius() { |
| 359 | return mRadiusMeter; |
| 360 | } |
| 361 | |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 362 | /** |
| 363 | * Check if the given LatLng is inside the circle. |
| 364 | * |
| 365 | * If a LatLng is on the edge of the circle, it is also considered to be inside the circle. |
| 366 | */ |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 367 | @Override |
Jordan Liu | 8cd7a4c | 2019-12-17 12:58:48 -0800 | [diff] [blame] | 368 | public boolean contains(@NonNull LatLng latLng) { |
| 369 | return mCenter.distance(latLng) <= mRadiusMeter; |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 370 | } |
Jack Yu | 35eed63 | 2019-11-08 09:32:17 -0800 | [diff] [blame] | 371 | |
| 372 | @Override |
| 373 | public String toString() { |
| 374 | String str = "Circle: "; |
Sarah Chin | 5ea6d8a | 2019-12-19 12:37:41 -0800 | [diff] [blame] | 375 | if (TelephonyUtils.IS_DEBUGGABLE) { |
Jack Yu | 35eed63 | 2019-11-08 09:32:17 -0800 | [diff] [blame] | 376 | str += mCenter + ", radius = " + mRadiusMeter; |
| 377 | } |
| 378 | |
| 379 | return str; |
| 380 | } |
Jordan Liu | 1486437 | 2020-04-08 14:42:02 -0700 | [diff] [blame] | 381 | |
| 382 | /** |
| 383 | * @hide |
| 384 | */ |
| 385 | @Override |
| 386 | public boolean equals(Object o) { |
| 387 | if (o == this) { |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | if (!(o instanceof Circle)) { |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | Circle c = (Circle) o; |
| 396 | return mCenter.equals(c.mCenter) |
| 397 | && Double.compare(mRadiusMeter, c.mRadiusMeter) == 0; |
| 398 | } |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Parse the geometries from the encoded string {@code str}. The string must follow the |
| 403 | * geometry encoding specified by {@link android.provider.Telephony.CellBroadcasts#GEOMETRIES}. |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 404 | * @hide |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 405 | */ |
| 406 | @NonNull |
| 407 | public static List<Geometry> parseGeometriesFromString(@NonNull String str) { |
| 408 | List<Geometry> geometries = new ArrayList<>(); |
| 409 | for (String geometryStr : str.split("\\s*;\\s*")) { |
| 410 | String[] geoParameters = geometryStr.split("\\s*\\|\\s*"); |
| 411 | switch (geoParameters[0]) { |
| 412 | case CIRCLE_SYMBOL: |
| 413 | geometries.add(new Circle(parseLatLngFromString(geoParameters[1]), |
| 414 | Double.parseDouble(geoParameters[2]))); |
| 415 | break; |
| 416 | case POLYGON_SYMBOL: |
| 417 | List<LatLng> vertices = new ArrayList<>(geoParameters.length - 1); |
| 418 | for (int i = 1; i < geoParameters.length; i++) { |
| 419 | vertices.add(parseLatLngFromString(geoParameters[i])); |
| 420 | } |
| 421 | geometries.add(new Polygon(vertices)); |
| 422 | break; |
| 423 | default: |
| 424 | Rlog.e(TAG, "Invalid geometry format " + geometryStr); |
| 425 | } |
| 426 | } |
| 427 | return geometries; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Encode a list of geometry objects to string. The encoding format is specified by |
| 432 | * {@link android.provider.Telephony.CellBroadcasts#GEOMETRIES}. |
| 433 | * |
| 434 | * @param geometries the list of geometry objects need to be encoded. |
| 435 | * @return the encoded string. |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 436 | * @hide |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 437 | */ |
| 438 | @NonNull |
Pengquan Meng | 9f677a0 | 2019-08-27 11:20:17 -0700 | [diff] [blame] | 439 | public static String encodeGeometriesToString(List<Geometry> geometries) { |
| 440 | if (geometries == null || geometries.isEmpty()) return ""; |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 441 | return geometries.stream() |
| 442 | .map(geometry -> encodeGeometryToString(geometry)) |
| 443 | .filter(encodedStr -> !TextUtils.isEmpty(encodedStr)) |
| 444 | .collect(Collectors.joining(";")); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | /** |
| 449 | * Encode the geometry object to string. The encoding format is specified by |
| 450 | * {@link android.provider.Telephony.CellBroadcasts#GEOMETRIES}. |
| 451 | * @param geometry the geometry object need to be encoded. |
| 452 | * @return the encoded string. |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 453 | * @hide |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 454 | */ |
| 455 | @NonNull |
| 456 | private static String encodeGeometryToString(@NonNull Geometry geometry) { |
| 457 | StringBuilder sb = new StringBuilder(); |
| 458 | if (geometry instanceof Polygon) { |
| 459 | sb.append(POLYGON_SYMBOL); |
| 460 | for (LatLng latLng : ((Polygon) geometry).getVertices()) { |
| 461 | sb.append("|"); |
| 462 | sb.append(latLng.lat); |
| 463 | sb.append(","); |
| 464 | sb.append(latLng.lng); |
| 465 | } |
| 466 | } else if (geometry instanceof Circle) { |
| 467 | sb.append(CIRCLE_SYMBOL); |
| 468 | Circle circle = (Circle) geometry; |
| 469 | |
| 470 | // Center |
| 471 | sb.append("|"); |
| 472 | sb.append(circle.getCenter().lat); |
| 473 | sb.append(","); |
| 474 | sb.append(circle.getCenter().lng); |
| 475 | |
| 476 | // Radius |
| 477 | sb.append("|"); |
| 478 | sb.append(circle.getRadius()); |
| 479 | } else { |
| 480 | Rlog.e(TAG, "Unsupported geometry object " + geometry); |
| 481 | return null; |
| 482 | } |
| 483 | return sb.toString(); |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Parse {@link LatLng} from {@link String}. Latitude and longitude are separated by ",". |
| 488 | * Example: "13.56,-55.447". |
| 489 | * |
| 490 | * @param str encoded lat/lng string. |
| 491 | * @Return {@link LatLng} object. |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 492 | * @hide |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 493 | */ |
| 494 | @NonNull |
| 495 | public static LatLng parseLatLngFromString(@NonNull String str) { |
| 496 | String[] latLng = str.split("\\s*,\\s*"); |
| 497 | return new LatLng(Double.parseDouble(latLng[0]), Double.parseDouble(latLng[1])); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * @Return the sign of the given value {@code value} with the specified tolerance. Return 1 |
| 502 | * means the sign is positive, -1 means negative, 0 means the value will be treated as 0. |
Jordan Liu | b359133 | 2019-10-21 12:31:59 -0700 | [diff] [blame] | 503 | * @hide |
Pengquan Meng | e7bc13f | 2019-08-14 17:57:33 -0700 | [diff] [blame] | 504 | */ |
| 505 | public static int sign(double value) { |
| 506 | if (value > EPS) return 1; |
| 507 | if (value < -EPS) return -1; |
| 508 | return 0; |
| 509 | } |
| 510 | } |