blob: 806bac0c32c9c4d863d96093fe78e6b5ac280e57 [file] [log] [blame]
Pengquan Menge7bc13f2019-08-14 17:57:33 -07001/*
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 Liub3591332019-10-21 12:31:59 -070017package android.telephony;
Pengquan Menge7bc13f2019-08-14 17:57:33 -070018
19import android.annotation.NonNull;
Jordan Liub3591332019-10-21 12:31:59 -070020import android.annotation.SystemApi;
Pengquan Menge7bc13f2019-08-14 17:57:33 -070021import android.text.TextUtils;
22
Sarah Chin5ea6d8a2019-12-19 12:37:41 -080023import com.android.internal.telephony.util.TelephonyUtils;
Meng Wang1dbea2f2020-01-30 12:25:08 -080024import com.android.telephony.Rlog;
Sarah Chin5ea6d8a2019-12-19 12:37:41 -080025
Pengquan Menge7bc13f2019-08-14 17:57:33 -070026import java.util.ArrayList;
27import java.util.List;
28import java.util.stream.Collectors;
29
Pengquan Menge7bc13f2019-08-14 17:57:33 -070030/**
Jordan Liu8cd7a4c2019-12-17 12:58:48 -080031 * This utils class is used for geo-fencing of CellBroadcast messages and is used by the cell
32 * broadcast module.
33 *
Pengquan Menge7bc13f2019-08-14 17:57:33 -070034 * 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 Liu8cd7a4c2019-12-17 12:58:48 -080037 *
38 * More information regarding cell broadcast geo-fencing logic is laid out in 3GPP TS 23.041 and
39 * ATIS-0700041.
Jordan Liub3591332019-10-21 12:31:59 -070040 * @hide
Pengquan Menge7bc13f2019-08-14 17:57:33 -070041 */
Jordan Liub3591332019-10-21 12:31:59 -070042@SystemApi
Pengquan Menge7bc13f2019-08-14 17:57:33 -070043public class CbGeoUtils {
Jordan Liub3591332019-10-21 12:31:59 -070044
45 /**
46 * This class is never instantiated
47 * @hide
48 */
49 private CbGeoUtils() {}
50
Pengquan Menge7bc13f2019-08-14 17:57:33 -070051 /** 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 Liub3591332019-10-21 12:31:59 -070058 boolean contains(@NonNull LatLng p);
Pengquan Menge7bc13f2019-08-14 17:57:33 -070059 }
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 Liub3591332019-10-21 12:31:59 -070064 * @hide
Pengquan Menge7bc13f2019-08-14 17:57:33 -070065 */
66 public static final double EPS = 1e-7;
67
Jordan Liub3591332019-10-21 12:31:59 -070068 /**
69 * The radius of earth.
70 * @hide
71 */
Pengquan Menge7bc13f2019-08-14 17:57:33 -070072 public static final int EARTH_RADIUS_METER = 6371 * 1000;
73
74 private static final String TAG = "CbGeoUtils";
75
Jordan Liub3591332019-10-21 12:31:59 -070076 // The TLV tags of WAC, defined in ATIS-0700041 5.2.3 WAC tag coding.
77 /** @hide */
Pengquan Mengc3427c02019-08-12 23:09:34 -070078 public static final int GEO_FENCING_MAXIMUM_WAIT_TIME = 0x01;
Jordan Liub3591332019-10-21 12:31:59 -070079 /** @hide */
Pengquan Mengc3427c02019-08-12 23:09:34 -070080 public static final int GEOMETRY_TYPE_POLYGON = 0x02;
Jordan Liub3591332019-10-21 12:31:59 -070081 /** @hide */
Pengquan Mengc3427c02019-08-12 23:09:34 -070082 public static final int GEOMETRY_TYPE_CIRCLE = 0x03;
83
Jordan Liub3591332019-10-21 12:31:59 -070084 // The identifier of geometry in the encoded string.
85 /** @hide */
Pengquan Menge7bc13f2019-08-14 17:57:33 -070086 private static final String CIRCLE_SYMBOL = "circle";
Jordan Liub3591332019-10-21 12:31:59 -070087 /** @hide */
Pengquan Menge7bc13f2019-08-14 17:57:33 -070088 private static final String POLYGON_SYMBOL = "polygon";
89
Jordan Liu8cd7a4c2019-12-17 12:58:48 -080090 /** A point represented by (latitude, longitude). */
Pengquan Menge7bc13f2019-08-14 17:57:33 -070091 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 Liu8cd7a4c2019-12-17 12:58:48 -0800106 * @param p the point to subtract
107 * @return the result of the subtraction
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700108 */
Jordan Liub3591332019-10-21 12:31:59 -0700109 @NonNull
110 public LatLng subtract(@NonNull LatLng p) {
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700111 return new LatLng(lat - p.lat, lng - p.lng);
112 }
113
114 /**
Jordan Liu8cd7a4c2019-12-17 12:58:48 -0800115 * 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 Menge7bc13f2019-08-14 17:57:33 -0700118 */
Jordan Liub3591332019-10-21 12:31:59 -0700119 public double distance(@NonNull LatLng p) {
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700120 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 Mengc3427c02019-08-12 23:09:34 -0700126
127 @Override
128 public String toString() {
129 return "(" + lat + "," + lng + ")";
130 }
Jordan Liu14864372020-04-08 14:42:02 -0700131
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 Menge7bc13f2019-08-14 17:57:33 -0700148 }
149
150 /**
Jordan Liu8cd7a4c2019-12-17 12:58:48 -0800151 * 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 Menge7bc13f2019-08-14 17:57:33 -0700154 */
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 Liu8cd7a4c2019-12-17 12:58:48 -0800172 * The longitude difference in the vertices should be less than 180 degrees.
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700173 */
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 Liu8cd7a4c2019-12-17 12:58:48 -0800191 /**
192 * Return the list of vertices which compose the polygon.
193 */
194 public @NonNull List<LatLng> getVertices() {
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700195 return mVertices;
196 }
197
198 /**
Jordan Liu8cd7a4c2019-12-17 12:58:48 -0800199 * Check if the given LatLng is inside the polygon.
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700200 *
Jordan Liu8cd7a4c2019-12-17 12:58:48 -0800201 * If a LatLng is on the edge of the polygon, it is also considered to be inside the
202 * polygon.
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700203 */
204 @Override
Jordan Liu8cd7a4c2019-12-17 12:58:48 -0800205 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 Menge7bc13f2019-08-14 17:57:33 -0700209 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 Liu8cd7a4c2019-12-17 12:58:48 -0800277 /** @hide */
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700278 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 Yu35eed632019-11-08 09:32:17 -0800291
292 @Override
293 public String toString() {
294 String str = "Polygon: ";
Sarah Chin5ea6d8a2019-12-19 12:37:41 -0800295 if (TelephonyUtils.IS_DEBUGGABLE) {
Jack Yu35eed632019-11-08 09:32:17 -0800296 str += mVertices;
297 }
298 return str;
299 }
Jordan Liu14864372020-04-08 14:42:02 -0700300
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 Menge7bc13f2019-08-14 17:57:33 -0700326 }
327
Jordan Liub3591332019-10-21 12:31:59 -0700328 /**
Jordan Liu8cd7a4c2019-12-17 12:58:48 -0800329 * 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 Liub3591332019-10-21 12:31:59 -0700332 */
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700333 public static class Circle implements Geometry {
334 private final LatLng mCenter;
335 private final double mRadiusMeter;
336
Jordan Liu8cd7a4c2019-12-17 12:58:48 -0800337 /**
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 Menge7bc13f2019-08-14 17:57:33 -0700344 this.mCenter = center;
Jordan Liu8cd7a4c2019-12-17 12:58:48 -0800345 this.mRadiusMeter = radiusInMeters;
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700346 }
347
Jordan Liu8cd7a4c2019-12-17 12:58:48 -0800348 /**
349 * Return the latitude and longitude of the center of the circle;
350 */
351 public @NonNull LatLng getCenter() {
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700352 return mCenter;
353 }
354
Jordan Liu8cd7a4c2019-12-17 12:58:48 -0800355 /**
356 * Return the radius of the circle in meters.
357 */
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700358 public double getRadius() {
359 return mRadiusMeter;
360 }
361
Jordan Liu8cd7a4c2019-12-17 12:58:48 -0800362 /**
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 Menge7bc13f2019-08-14 17:57:33 -0700367 @Override
Jordan Liu8cd7a4c2019-12-17 12:58:48 -0800368 public boolean contains(@NonNull LatLng latLng) {
369 return mCenter.distance(latLng) <= mRadiusMeter;
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700370 }
Jack Yu35eed632019-11-08 09:32:17 -0800371
372 @Override
373 public String toString() {
374 String str = "Circle: ";
Sarah Chin5ea6d8a2019-12-19 12:37:41 -0800375 if (TelephonyUtils.IS_DEBUGGABLE) {
Jack Yu35eed632019-11-08 09:32:17 -0800376 str += mCenter + ", radius = " + mRadiusMeter;
377 }
378
379 return str;
380 }
Jordan Liu14864372020-04-08 14:42:02 -0700381
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 Menge7bc13f2019-08-14 17:57:33 -0700399 }
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 Liub3591332019-10-21 12:31:59 -0700404 * @hide
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700405 */
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 Liub3591332019-10-21 12:31:59 -0700436 * @hide
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700437 */
438 @NonNull
Pengquan Meng9f677a02019-08-27 11:20:17 -0700439 public static String encodeGeometriesToString(List<Geometry> geometries) {
440 if (geometries == null || geometries.isEmpty()) return "";
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700441 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 Liub3591332019-10-21 12:31:59 -0700453 * @hide
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700454 */
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 Liub3591332019-10-21 12:31:59 -0700492 * @hide
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700493 */
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 Liub3591332019-10-21 12:31:59 -0700503 * @hide
Pengquan Menge7bc13f2019-08-14 17:57:33 -0700504 */
505 public static int sign(double value) {
506 if (value > EPS) return 1;
507 if (value < -EPS) return -1;
508 return 0;
509 }
510}