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