1 /*
2  * Copyright (C) 2015 Google Inc.
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;
18 
19 import android.content.Context;
20 import android.location.GnssMeasurementsEvent;
21 import android.location.GnssNavigationMessage;
22 import android.location.GpsStatus;
23 import android.location.LocationListener;
24 import android.location.LocationManager;
25 import android.os.Handler;
26 import android.os.Looper;
27 import android.util.Log;
28 
29 import junit.framework.Assert;
30 
31 /**
32  * A {@code LocationManager} wrapper that logs GNSS turn-on and turn-off.
33  */
34 public class TestLocationManager {
35 
36     private static final String TAG = "TestLocationManager";
37     private LocationManager mLocationManager;
38     private Context mContext;
39 
TestLocationManager(Context context)40     public TestLocationManager(Context context) {
41         mContext = context;
42         mLocationManager =
43                 (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
44     }
45 
46     /**
47      * See {@code LocationManager#removeUpdates(LocationListener)}.
48      *
49      * @param listener the listener to remove
50      */
removeLocationUpdates(LocationListener listener)51     public void removeLocationUpdates(LocationListener listener) {
52         Log.i(TAG, "Remove Location updates.");
53         mLocationManager.removeUpdates(listener);
54     }
55 
56     /**
57      * See {@link android.location.LocationManager#registerGnssMeasurementsCallback
58      * (GnssMeasurementsEvent.Callback callback)}
59      *
60      * @param callback the listener to add
61      */
registerGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback)62     public void registerGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback) {
63         Log.i(TAG, "Add Gnss Measurement Callback.");
64         boolean measurementListenerAdded =
65                 mLocationManager.registerGnssMeasurementsCallback(callback);
66         if (!measurementListenerAdded) {
67             // Registration of GnssMeasurements listener has failed, this indicates a platform bug.
68             Log.i(TAG, TestMeasurementUtil.REGISTRATION_ERROR_MESSAGE);
69             Assert.fail(TestMeasurementUtil.REGISTRATION_ERROR_MESSAGE);
70         }
71     }
72 
73     /**
74      * See {@link android.location.LocationManager#registerGnssMeasurementsCallback(GnssMeasurementsEvent.Callback callback)}
75      *
76      * @param callback the listener to add
77      * @param handler the handler that the callback runs at.
78      */
registerGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback, Handler handler)79     public void registerGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback,
80             Handler handler) {
81         Log.i(TAG, "Add Gnss Measurement Callback.");
82         boolean measurementListenerAdded =
83                 mLocationManager.registerGnssMeasurementsCallback(callback, handler);
84         if (!measurementListenerAdded) {
85             // Registration of GnssMeasurements listener has failed, this indicates a platform bug.
86             Log.i(TAG, TestMeasurementUtil.REGISTRATION_ERROR_MESSAGE);
87             Assert.fail(TestMeasurementUtil.REGISTRATION_ERROR_MESSAGE);
88         }
89     }
90 
91     /**
92      * See {@link android.location.LocationManager#unregisterGnssMeasurementsCallback
93      * (GnssMeasurementsEvent.Callback)}.
94      *
95      * @param callback the listener to remove
96      */
unregisterGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback)97     public void unregisterGnssMeasurementCallback(GnssMeasurementsEvent.Callback callback) {
98         Log.i(TAG, "Remove Gnss Measurement Callback.");
99         mLocationManager.unregisterGnssMeasurementsCallback(callback);
100     }
101 
102     /**
103      * See {@code LocationManager#requestLocationUpdates}.
104      *
105      * @param locationListener location listener for request
106      */
requestLocationUpdates(LocationListener locationListener)107     public void requestLocationUpdates(LocationListener locationListener) {
108         if (mLocationManager.getProvider(LocationManager.GPS_PROVIDER) != null) {
109             Log.i(TAG, "Request Location updates.");
110             mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
111                     0 /* minTime*/,
112                     0 /* minDistance */,
113                     locationListener,
114                     Looper.getMainLooper());
115         }
116     }
117 
118     /**
119      * See {@link android.location.LocationManager#addGpsStatusListener (GpsStatus.Listener)}.
120      * @param listener the GpsStatus.Listener to add
121      */
addGpsStatusListener(final GpsStatus.Listener listener)122     public void addGpsStatusListener(final GpsStatus.Listener listener) {
123         Log.i(TAG, "Add Gps Status Listener.");
124         Handler mainThreadHandler = new Handler(Looper.getMainLooper());
125         // Add Gps status listener to the main thread, since the Gps Status updates will go to
126         // the main thread while the test thread is blocked by mGpsStatusListener.await()
127         mainThreadHandler.post(new Runnable() {
128             @Override
129             public void run() {
130                 mLocationManager.addGpsStatusListener(listener);
131             }
132         });
133     }
134 
135     /**
136      * See {@link android.location.LocationManager#removeGpsStatusListener (GpsStatus.Listener)}.
137      *
138      * @param listener the listener to remove
139      */
removeGpsStatusListener(GpsStatus.Listener listener)140     public void removeGpsStatusListener(GpsStatus.Listener listener) {
141         Log.i(TAG, "Remove Gps Status Listener.");
142         mLocationManager.removeGpsStatusListener(listener);
143     }
144 
145     /**
146      * See {@link android.location.LocationManager#sendExtraCommand}.
147      *
148      * @param command name of the command to send to the provider.
149      *
150      * @return true if the command succeeds.
151      */
sendExtraCommand(String command)152     public boolean sendExtraCommand(String command) {
153         Log.i(TAG, "Send Extra Command = " + command);
154         boolean extraCommandStatus = mLocationManager.sendExtraCommand(LocationManager.GPS_PROVIDER,
155                 command, null);
156         Log.i(TAG, "Sent extra command (" + command + ") status = " + extraCommandStatus);
157         return extraCommandStatus;
158     }
159 
160     /**
161      * Add a GNSS Navigation Message callback.
162      *
163      * @param callback a {@link GnssNavigationMessage.Callback} object to register.
164      * @return {@code true} if the listener was added successfully, {@code false} otherwise.
165      */
registerGnssNavigationMessageCallback( GnssNavigationMessage.Callback callback)166     public boolean registerGnssNavigationMessageCallback(
167             GnssNavigationMessage.Callback callback) {
168         Log.i(TAG, "Add Gnss Navigation Message Callback.");
169         return mLocationManager.registerGnssNavigationMessageCallback(callback);
170     }
171 
172     /**
173      * Add a GNSS Navigation Message callback.
174      *
175      * @param callback a {@link GnssNavigationMessage.Callback} object to register.
176      * @param handler the handler that the callback runs at.
177      * @return {@code true} if the listener was added successfully, {@code false} otherwise.
178      */
registerGnssNavigationMessageCallback( GnssNavigationMessage.Callback callback, Handler handler)179     public boolean registerGnssNavigationMessageCallback(
180             GnssNavigationMessage.Callback callback, Handler handler) {
181         Log.i(TAG, "Add Gnss Navigation Message Callback.");
182         return mLocationManager.registerGnssNavigationMessageCallback(callback, handler);
183     }
184 
185     /**
186      * Removes a GNSS Navigation Message callback.
187      *
188      * @param callback a {@link GnssNavigationMessage.Callback} object to remove.
189      */
unregisterGnssNavigationMessageCallback(GnssNavigationMessage.Callback callback)190     public void unregisterGnssNavigationMessageCallback(GnssNavigationMessage.Callback callback) {
191         Log.i(TAG, "Remove Gnss Navigation Message Callback.");
192         mLocationManager.unregisterGnssNavigationMessageCallback(callback);
193     }
194 
195     /**
196      * Get LocationManager
197      *
198      * @return locationManager
199      */
getLocationManager()200     public LocationManager getLocationManager() {
201         return mLocationManager;
202     }
203     /**
204      * Get Context
205      *
206      * @return context
207      */
getContext()208     public Context getContext() {
209         return mContext;
210     }
211 }
212