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 
17 package android.telephony;
18 
19 import android.annotation.NonNull;
20 import android.annotation.SystemApi;
21 import android.text.TextUtils;
22 
23 import com.android.internal.telephony.util.TelephonyUtils;
24 import com.android.telephony.Rlog;
25 
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 
30 /**
31  * This utils class is used for geo-fencing of CellBroadcast messages and is used by the cell
32  * broadcast module.
33  *
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.
37  *
38  * More information regarding cell broadcast geo-fencing logic is laid out in 3GPP TS 23.041 and
39  * ATIS-0700041.
40  * @hide
41  */
42 @SystemApi
43 public class CbGeoUtils {
44 
45     /**
46      * This class is never instantiated
47      * @hide
48      */
CbGeoUtils()49     private CbGeoUtils() {}
50 
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          */
contains(@onNull LatLng p)58         boolean contains(@NonNull LatLng p);
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.
64      * @hide
65      */
66     public static final double EPS = 1e-7;
67 
68     /**
69      * The radius of earth.
70      * @hide
71      */
72     public static final int EARTH_RADIUS_METER = 6371 * 1000;
73 
74     private static final String TAG = "CbGeoUtils";
75 
76     // The TLV tags of WAC, defined in ATIS-0700041 5.2.3 WAC tag coding.
77     /** @hide */
78     public static final int GEO_FENCING_MAXIMUM_WAIT_TIME = 0x01;
79     /** @hide */
80     public static final int GEOMETRY_TYPE_POLYGON = 0x02;
81     /** @hide */
82     public static final int GEOMETRY_TYPE_CIRCLE = 0x03;
83 
84     // The identifier of geometry in the encoded string.
85     /** @hide */
86     private static final String CIRCLE_SYMBOL = "circle";
87     /** @hide */
88     private static final String POLYGON_SYMBOL = "polygon";
89 
90     /** A point represented by (latitude, longitude). */
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          */
LatLng(double lat, double lng)100         public LatLng(double lat, double lng) {
101             this.lat = lat;
102             this.lng = lng;
103         }
104 
105         /**
106          * @param p the point to subtract
107          * @return the result of the subtraction
108          */
109         @NonNull
subtract(@onNull LatLng p)110         public LatLng subtract(@NonNull LatLng p) {
111             return new LatLng(lat - p.lat, lng - p.lng);
112         }
113 
114         /**
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.
118          */
distance(@onNull LatLng p)119         public double distance(@NonNull LatLng p) {
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         }
126 
127         @Override
toString()128         public String toString() {
129             return "(" + lat + "," + lng + ")";
130         }
131 
132         /**
133          * @hide
134          */
135         @Override
equals(Object o)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         }
148     }
149 
150     /**
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.
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          *
172          * The longitude difference in the vertices should be less than 180 degrees.
173          */
Polygon(@onNull List<LatLng> vertices)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 
191         /**
192          * Return the list of vertices which compose the polygon.
193          */
getVertices()194         public @NonNull List<LatLng> getVertices() {
195             return mVertices;
196         }
197 
198         /**
199          * Check if the given LatLng is inside the polygon.
200          *
201          * If a LatLng is on the edge of the polygon, it is also considered to be inside the
202          * polygon.
203          */
204         @Override
contains(@onNull LatLng latLng)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 
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          */
convertAndScaleLatLng(LatLng latLng)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 
crossProduct(Point a, Point b)273         private static double crossProduct(Point a, Point b) {
274             return a.x * b.y - a.y * b.x;
275         }
276 
277         /** @hide */
278         static final class Point {
279             public final double x;
280             public final double y;
281 
Point(double x, double y)282             Point(double x, double y) {
283                 this.x = x;
284                 this.y = y;
285             }
286 
subtract(Point p)287             public Point subtract(Point p) {
288                 return new Point(x - p.x, y - p.y);
289             }
290         }
291 
292         @Override
toString()293         public String toString() {
294             String str = "Polygon: ";
295             if (TelephonyUtils.IS_DEBUGGABLE) {
296                 str += mVertices;
297             }
298             return str;
299         }
300 
301         /**
302          * @hide
303          */
304         @Override
equals(Object o)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         }
326     }
327 
328     /**
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.
332      */
333     public static class Circle implements Geometry {
334         private final LatLng mCenter;
335         private final double mRadiusMeter;
336 
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          */
Circle(@onNull LatLng center, double radiusInMeters)343         public Circle(@NonNull LatLng center, double radiusInMeters) {
344             this.mCenter = center;
345             this.mRadiusMeter = radiusInMeters;
346         }
347 
348         /**
349          * Return the latitude and longitude of the center of the circle;
350          */
getCenter()351         public @NonNull LatLng getCenter() {
352             return mCenter;
353         }
354 
355         /**
356          * Return the radius of the circle in meters.
357          */
getRadius()358         public double getRadius() {
359             return mRadiusMeter;
360         }
361 
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          */
367         @Override
contains(@onNull LatLng latLng)368         public boolean contains(@NonNull LatLng latLng) {
369             return mCenter.distance(latLng) <= mRadiusMeter;
370         }
371 
372         @Override
toString()373         public String toString() {
374             String str = "Circle: ";
375             if (TelephonyUtils.IS_DEBUGGABLE) {
376                 str += mCenter + ", radius = " + mRadiusMeter;
377             }
378 
379             return str;
380         }
381 
382         /**
383          * @hide
384          */
385         @Override
equals(Object o)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         }
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}.
404      * @hide
405      */
406     @NonNull
parseGeometriesFromString(@onNull String str)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.
436      * @hide
437      */
438     @NonNull
encodeGeometriesToString(List<Geometry> geometries)439     public static String encodeGeometriesToString(List<Geometry> geometries) {
440         if (geometries == null || geometries.isEmpty()) return "";
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.
453      * @hide
454      */
455     @NonNull
encodeGeometryToString(@onNull Geometry geometry)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.
492      * @hide
493      */
494     @NonNull
parseLatLngFromString(@onNull String str)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.
503      * @hide
504      */
sign(double value)505     public static int sign(double value) {
506         if (value > EPS) return 1;
507         if (value < -EPS) return -1;
508         return 0;
509     }
510 }
511