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