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 android.location.cts.common;
18 
19 import android.content.Context;
20 import android.location.GnssMeasurementRequest;
21 import android.location.GnssMeasurementsEvent;
22 import android.location.GnssNavigationMessage;
23 import android.location.GnssRequest;
24 import android.location.GnssStatus;
25 import android.location.LocationListener;
26 import android.location.LocationManager;
27 import android.location.LocationRequest;
28 import android.os.Handler;
29 import android.os.Looper;
30 import android.util.Log;
31 
32 import junit.framework.Assert;
33 
34 /**
35  * A {@code LocationManager} wrapper that logs GNSS turn-on and turn-off.
36  */
37 public class TestLocationManager {
38 
39     private static final String TAG = "TestLocationManager";
40     private LocationManager mLocationManager;
41     private Context mContext;
42 
TestLocationManager(Context context)43     public TestLocationManager(Context context) {
44         mContext = context;
45         mLocationManager =
46                 (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
47     }
48 
49     /**
50      * See {@code LocationManager#removeUpdates(LocationListener)}.
51      *
52      * @param listener the listener to remove
53      */
removeLocationUpdates(LocationListener listener)54     public void removeLocationUpdates(LocationListener listener) {
55         Log.i(TAG, "Remove Location updates.");
56         mLocationManager.removeUpdates(listener);
57     }
58 
59     /**
60      * See {@link android.location.LocationManager#registerGnssMeasurementsCallback
61      * (GnssMeasurementsEvent.Callback callback)}
62      *
63      * @param callback the listener to add
64      */
registerGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback)65     public void registerGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback) {
66         Log.i(TAG, "Add Gnss Measurement Callback.");
67         boolean measurementListenerAdded =
68                 mLocationManager.registerGnssMeasurementsCallback(callback);
69         if (!measurementListenerAdded) {
70             // Registration of GnssMeasurements listener has failed, this indicates a platform bug.
71             Log.i(TAG, TestMeasurementUtil.REGISTRATION_ERROR_MESSAGE);
72             Assert.fail(TestMeasurementUtil.REGISTRATION_ERROR_MESSAGE);
73         }
74     }
75 
76     /**
77      * See {@link android.location.LocationManager#registerGnssMeasurementsCallback(GnssMeasurementsEvent.Callback callback)}
78      *
79      * @param callback the listener to add
80      * @param handler the handler that the callback runs at.
81      */
registerGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback, Handler handler)82     public void registerGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback,
83             Handler handler) {
84         Log.i(TAG, "Add Gnss Measurement Callback.");
85         boolean measurementListenerAdded =
86                 mLocationManager.registerGnssMeasurementsCallback(callback, handler);
87         if (!measurementListenerAdded) {
88             // Registration of GnssMeasurements listener has failed, this indicates a platform bug.
89             Log.i(TAG, TestMeasurementUtil.REGISTRATION_ERROR_MESSAGE);
90             Assert.fail(TestMeasurementUtil.REGISTRATION_ERROR_MESSAGE);
91         }
92     }
93 
94     /**
95      * See {@link android.location.LocationManager#registerGnssMeasurementsCallback
96      * (GnssMeasurementsEvent.Callback callback)}
97      *
98      * @param callback the listener to add
99      */
registerGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback, GnssRequest request)100     public void registerGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback,
101             GnssRequest request) {
102         Log.i(TAG, "Add Gnss Measurement Callback. enableFullTracking=" + request);
103         boolean measurementListenerAdded =
104                 mLocationManager.registerGnssMeasurementsCallback(request, Runnable::run, callback);
105         if (!measurementListenerAdded) {
106             // Registration of GnssMeasurements listener has failed, this indicates a platform bug.
107             Log.i(TAG, TestMeasurementUtil.REGISTRATION_ERROR_MESSAGE);
108             Assert.fail(TestMeasurementUtil.REGISTRATION_ERROR_MESSAGE);
109         }
110     }
111 
112     /**
113      * See {@link android.location.LocationManager#registerGnssMeasurementsCallback
114      * (GnssMeasurementsEvent.Callback callback)}
115      *
116      * @param callback the listener to add
117      */
registerGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback, GnssMeasurementRequest request)118     public void registerGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback,
119             GnssMeasurementRequest request) {
120         Log.i(TAG, "Add Gnss Measurement Callback. enableFullTracking=" + request);
121         boolean measurementListenerAdded =
122                 mLocationManager.registerGnssMeasurementsCallback(request, Runnable::run, callback);
123         if (!measurementListenerAdded) {
124             // Registration of GnssMeasurements listener has failed, this indicates a platform bug.
125             Log.i(TAG, TestMeasurementUtil.REGISTRATION_ERROR_MESSAGE);
126             Assert.fail(TestMeasurementUtil.REGISTRATION_ERROR_MESSAGE);
127         }
128     }
129 
130     /**
131      * Request GNSS location updates with {@code LocationRequest#setLowPowerMode()} enabled.
132      *
133      * See {@code LocationManager#requestLocationUpdates}.
134      *
135      * @param locationListener location listener for request
136      */
requestLowPowerModeGnssLocationUpdates(int minTimeMillis, LocationListener locationListener)137     public void requestLowPowerModeGnssLocationUpdates(int minTimeMillis,
138             LocationListener locationListener) {
139         if (mLocationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
140             Log.i(TAG, "Request Location updates.");
141             mLocationManager.requestLocationUpdates(
142                     LocationManager.GPS_PROVIDER,
143                     new LocationRequest.Builder(minTimeMillis).setLowPower(true).build(),
144                     mContext.getMainExecutor(),
145                     locationListener);
146         }
147     }
148 
149     /**
150      * See {@link android.location.LocationManager#unregisterGnssMeasurementsCallback
151      * (GnssMeasurementsEvent.Callback)}.
152      *
153      * @param callback the listener to remove
154      */
unregisterGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback)155     public void unregisterGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback) {
156         Log.i(TAG, "Remove Gnss Measurement Callback.");
157         mLocationManager.unregisterGnssMeasurementsCallback(callback);
158     }
159 
160     /**
161      * See {@code LocationManager#requestLocationUpdates}.
162      *
163      * @param locationListener location listener for request
164      */
requestLocationUpdates(LocationListener locationListener, int minTimeMsec)165     public void requestLocationUpdates(LocationListener locationListener, int minTimeMsec) {
166         if (mLocationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
167             Log.i(TAG, "Request Location updates.");
168             mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
169                     minTimeMsec,
170                     0 /* minDistance */,
171                     locationListener,
172                     Looper.getMainLooper());
173         }
174     }
175 
176     /**
177      * See {@code LocationManager#requestLocationUpdates}.
178      *
179      * @param locationListener location listener for request
180      */
requestLocationUpdates(LocationListener locationListener)181     public void requestLocationUpdates(LocationListener locationListener) {
182         requestLocationUpdates(locationListener, 0 /* minTimeMsec */);
183     }
184 
185     /**
186      * See {@code LocationManager#requestNetworkLocationUpdates}.
187      *
188      * @param locationListener location listener for request
189      */
requestNetworkLocationUpdates(LocationListener locationListener)190     public void requestNetworkLocationUpdates(LocationListener locationListener) {
191         if (mLocationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) {
192             Log.i(TAG, "Request Network Location updates.");
193             mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
194                 0 /* minTime*/,
195                 0 /* minDistance */,
196                 locationListener,
197                 Looper.getMainLooper());
198         }
199     }
200 
201     /**
202      * See {@code LocationManager#requestLocationUpdates}.
203      *
204      * @param locationListener location listener for request
205      */
requestPassiveLocationUpdates(LocationListener locationListener, int minTimeMsec)206     public void requestPassiveLocationUpdates(LocationListener locationListener, int minTimeMsec) {
207         if (mLocationManager.getProvider(LocationManager.PASSIVE_PROVIDER) != null) {
208             Log.i(TAG, "Request Passive Location updates.");
209             mLocationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,
210                     minTimeMsec,
211                     0 /* minDistance */,
212                     locationListener,
213                     Looper.getMainLooper());
214         }
215     }
216 
217     /**
218      * See {@link android.location.LocationManager#sendExtraCommand}.
219      *
220      * @param command name of the command to send to the provider.
221      *
222      * @return true if the command succeeds.
223      */
sendExtraCommand(String command)224     public boolean sendExtraCommand(String command) {
225         Log.i(TAG, "Send Extra Command = " + command);
226         boolean extraCommandStatus = mLocationManager.sendExtraCommand(LocationManager.GPS_PROVIDER,
227                 command, null);
228         Log.i(TAG, "Sent extra command (" + command + ") status = " + extraCommandStatus);
229         return extraCommandStatus;
230     }
231 
232     /**
233      * Add a GNSS Navigation Message callback.
234      *
235      * @param callback a {@link GnssNavigationMessage.Callback} object to register.
236      * @return {@code true} if the listener was added successfully, {@code false} otherwise.
237      */
registerGnssNavigationMessageCallback( GnssNavigationMessage.Callback callback)238     public boolean registerGnssNavigationMessageCallback(
239             GnssNavigationMessage.Callback callback) {
240         Log.i(TAG, "Add Gnss Navigation Message Callback.");
241         return mLocationManager.registerGnssNavigationMessageCallback(callback);
242     }
243 
244     /**
245      * Add a GNSS Navigation Message callback.
246      *
247      * @param callback a {@link GnssNavigationMessage.Callback} object to register.
248      * @param handler the handler that the callback runs at.
249      * @return {@code true} if the listener was added successfully, {@code false} otherwise.
250      */
registerGnssNavigationMessageCallback( GnssNavigationMessage.Callback callback, Handler handler)251     public boolean registerGnssNavigationMessageCallback(
252             GnssNavigationMessage.Callback callback, Handler handler) {
253         Log.i(TAG, "Add Gnss Navigation Message Callback.");
254         return mLocationManager.registerGnssNavigationMessageCallback(callback, handler);
255     }
256 
257     /**
258      * Removes a GNSS Navigation Message callback.
259      *
260      * @param callback a {@link GnssNavigationMessage.Callback} object to remove.
261      */
unregisterGnssNavigationMessageCallback(GnssNavigationMessage.Callback callback)262     public void unregisterGnssNavigationMessageCallback(GnssNavigationMessage.Callback callback) {
263         Log.i(TAG, "Remove Gnss Navigation Message Callback.");
264         mLocationManager.unregisterGnssNavigationMessageCallback(callback);
265     }
266 
267     /**
268      * Add a GNSS Status callback.
269      *
270      * @param callback a {@link GnssStatus.Callback} object to register.
271      * @return {@code true} if the listener was added successfully, {@code false} otherwise.
272      */
registerGnssStatusCallback(GnssStatus.Callback callback)273     public boolean registerGnssStatusCallback(GnssStatus.Callback callback) {
274         Log.i(TAG, "Add Gnss Status Callback.");
275         return mLocationManager.registerGnssStatusCallback(
276             callback, new Handler(Looper.getMainLooper()));
277     }
278 
279     /**
280      * Removes a GNSS Status callback.
281      *
282      * @param callback a {@link GnssStatus.Callback} object to remove.
283      */
unregisterGnssStatusCallback(GnssStatus.Callback callback)284     public void unregisterGnssStatusCallback(GnssStatus.Callback callback) {
285         Log.i(TAG, "Remove Gnss Status Callback.");
286         mLocationManager.unregisterGnssStatusCallback(callback);
287     }
288 
289     /**
290      * Get LocationManager
291      *
292      * @return locationManager
293      */
getLocationManager()294     public LocationManager getLocationManager() {
295         return mLocationManager;
296     }
297     /**
298      * Get Context
299      *
300      * @return context
301      */
getContext()302     public Context getContext() {
303         return mContext;
304     }
305 }
306