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.GnssStatus; 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 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 */ 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 */ 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 */ 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 */ 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 */ 107 public void requestLocationUpdates(LocationListener locationListener, int minTimeMsec) { 108 if (mLocationManager.getProvider(LocationManager.GPS_PROVIDER) != null) { 109 Log.i(TAG, "Request Location updates."); 110 mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 111 minTimeMsec, 112 0 /* minDistance */, 113 locationListener, 114 Looper.getMainLooper()); 115 } 116 } 117 118 /** 119 * See {@code LocationManager#requestLocationUpdates}. 120 * 121 * @param locationListener location listener for request 122 */ 123 public void requestLocationUpdates(LocationListener locationListener) { 124 requestLocationUpdates(locationListener, 0 /* minTimeMsec */); 125 } 126 127 /** 128 * See {@code LocationManager#requestNetworkLocationUpdates}. 129 * 130 * @param locationListener location listener for request 131 */ 132 public void requestNetworkLocationUpdates(LocationListener locationListener) { 133 if (mLocationManager.getProvider(LocationManager.NETWORK_PROVIDER) != null) { 134 Log.i(TAG, "Request Network Location updates."); 135 mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 136 0 /* minTime*/, 137 0 /* minDistance */, 138 locationListener, 139 Looper.getMainLooper()); 140 } 141 } 142 143 /** 144 * See {@code LocationManager#requestLocationUpdates}. 145 * 146 * @param locationListener location listener for request 147 */ 148 public void requestPassiveLocationUpdates(LocationListener locationListener, int minTimeMsec) { 149 if (mLocationManager.getProvider(LocationManager.PASSIVE_PROVIDER) != null) { 150 Log.i(TAG, "Request Passive Location updates."); 151 mLocationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 152 minTimeMsec, 153 0 /* minDistance */, 154 locationListener, 155 Looper.getMainLooper()); 156 } 157 } 158 159 /** 160 * See {@link android.location.LocationManager#sendExtraCommand}. 161 * 162 * @param command name of the command to send to the provider. 163 * 164 * @return true if the command succeeds. 165 */ 166 public boolean sendExtraCommand(String command) { 167 Log.i(TAG, "Send Extra Command = " + command); 168 boolean extraCommandStatus = mLocationManager.sendExtraCommand(LocationManager.GPS_PROVIDER, 169 command, null); 170 Log.i(TAG, "Sent extra command (" + command + ") status = " + extraCommandStatus); 171 return extraCommandStatus; 172 } 173 174 /** 175 * Add a GNSS Navigation Message callback. 176 * 177 * @param callback a {@link GnssNavigationMessage.Callback} object to register. 178 * @return {@code true} if the listener was added successfully, {@code false} otherwise. 179 */ 180 public boolean registerGnssNavigationMessageCallback( 181 GnssNavigationMessage.Callback callback) { 182 Log.i(TAG, "Add Gnss Navigation Message Callback."); 183 return mLocationManager.registerGnssNavigationMessageCallback(callback); 184 } 185 186 /** 187 * Add a GNSS Navigation Message callback. 188 * 189 * @param callback a {@link GnssNavigationMessage.Callback} object to register. 190 * @param handler the handler that the callback runs at. 191 * @return {@code true} if the listener was added successfully, {@code false} otherwise. 192 */ 193 public boolean registerGnssNavigationMessageCallback( 194 GnssNavigationMessage.Callback callback, Handler handler) { 195 Log.i(TAG, "Add Gnss Navigation Message Callback."); 196 return mLocationManager.registerGnssNavigationMessageCallback(callback, handler); 197 } 198 199 /** 200 * Removes a GNSS Navigation Message callback. 201 * 202 * @param callback a {@link GnssNavigationMessage.Callback} object to remove. 203 */ 204 public void unregisterGnssNavigationMessageCallback(GnssNavigationMessage.Callback callback) { 205 Log.i(TAG, "Remove Gnss Navigation Message Callback."); 206 mLocationManager.unregisterGnssNavigationMessageCallback(callback); 207 } 208 209 /** 210 * Add a GNSS Status callback. 211 * 212 * @param callback a {@link GnssStatus.Callback} object to register. 213 * @return {@code true} if the listener was added successfully, {@code false} otherwise. 214 */ 215 public boolean registerGnssStatusCallback(GnssStatus.Callback callback) { 216 Log.i(TAG, "Add Gnss Status Callback."); 217 return mLocationManager.registerGnssStatusCallback( 218 callback, new Handler(Looper.getMainLooper())); 219 } 220 221 /** 222 * Removes a GNSS Status callback. 223 * 224 * @param callback a {@link GnssStatus.Callback} object to remove. 225 */ 226 public void unregisterGnssStatusCallback(GnssStatus.Callback callback) { 227 Log.i(TAG, "Remove Gnss Status Callback."); 228 mLocationManager.unregisterGnssStatusCallback(callback); 229 } 230 231 /** 232 * Get LocationManager 233 * 234 * @return locationManager 235 */ 236 public LocationManager getLocationManager() { 237 return mLocationManager; 238 } 239 /** 240 * Get Context 241 * 242 * @return context 243 */ 244 public Context getContext() { 245 return mContext; 246 } 247 } 248