1 /*
2  * Copyright (C) 2020 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 com.android.server.location;
18 
19 import android.location.Location;
20 import android.location.LocationResult;
21 import android.os.SystemClock;
22 
23 import java.util.Arrays;
24 import java.util.Random;
25 
26 public final class LocationUtils {
27 
28     private static final double MIN_LATITUDE = -90D;
29     private static final double MAX_LATITUDE = 90D;
30     private static final double MIN_LONGITUDE = -180D;
31     private static final double MAX_LONGITUDE = 180D;
32 
33     private static final float MIN_ACCURACY = 1;
34     private static final float MAX_ACCURACY = 100;
35 
createLocation(String provider, Random random)36     public static Location createLocation(String provider, Random random) {
37         return createLocation(provider,
38                 MIN_LATITUDE + random.nextDouble() * (MAX_LATITUDE - MIN_LATITUDE),
39                 MIN_LONGITUDE + random.nextDouble() * (MAX_LONGITUDE - MIN_LONGITUDE),
40                 MIN_ACCURACY + random.nextFloat() * (MAX_ACCURACY - MIN_ACCURACY));
41     }
42 
createLocationResult(String provider, Random random)43     public static LocationResult createLocationResult(String provider, Random random) {
44         return LocationResult.wrap(createLocation(provider, random));
45     }
46 
createLocationResult(String provider, Random random, int numLocations)47     public static LocationResult createLocationResult(String provider, Random random,
48             int numLocations) {
49         Location[] locations = new Location[numLocations];
50         for (int i = 0; i < numLocations; i++) {
51             locations[i] = createLocation(provider, random);
52         }
53         return LocationResult.create(Arrays.asList(locations));
54     }
55 
createLocation(String provider, double latitude, double longitude, float accuracy)56     public static Location createLocation(String provider, double latitude, double longitude,
57             float accuracy) {
58         Location location = new Location(provider);
59         location.setLatitude(latitude);
60         location.setLongitude(longitude);
61         location.setAccuracy(accuracy);
62         location.setTime(System.currentTimeMillis());
63         location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
64         return location;
65     }
66 
createLocationResult(String provider, double latitude, double longitude, float accuracy)67     public static LocationResult createLocationResult(String provider, double latitude,
68             double longitude, float accuracy) {
69         return LocationResult.wrap(createLocation(provider, latitude, longitude, accuracy));
70     }
71 }
72