Home | History | Annotate | Download | only in location
      1 /*
      2  * Copyright (C) 2014 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 com.android.server.location;
     18 
     19 import android.location.GpsMeasurementsEvent;
     20 import android.location.IGpsMeasurementsListener;
     21 import android.os.Handler;
     22 import android.os.RemoteException;
     23 import android.util.Log;
     24 
     25 /**
     26  * An base implementation for GPS measurements provider.
     27  * It abstracts out the responsibility of handling listeners, while still allowing technology
     28  * specific implementations to be built.
     29  *
     30  * @hide
     31  */
     32 public abstract class GpsMeasurementsProvider
     33         extends RemoteListenerHelper<IGpsMeasurementsListener> {
     34     private static final String TAG = "GpsMeasurementsProvider";
     35 
     36     public GpsMeasurementsProvider(Handler handler) {
     37         super(handler, TAG);
     38     }
     39 
     40     public void onMeasurementsAvailable(final GpsMeasurementsEvent event) {
     41         ListenerOperation<IGpsMeasurementsListener> operation =
     42                 new ListenerOperation<IGpsMeasurementsListener>() {
     43             @Override
     44             public void execute(IGpsMeasurementsListener listener) throws RemoteException {
     45                 listener.onGpsMeasurementsReceived(event);
     46             }
     47         };
     48         foreach(operation);
     49     }
     50 
     51     public void onCapabilitiesUpdated(boolean isGpsMeasurementsSupported) {
     52         int status = isGpsMeasurementsSupported ?
     53                 GpsMeasurementsEvent.STATUS_READY :
     54                 GpsMeasurementsEvent.STATUS_NOT_SUPPORTED;
     55         setSupported(isGpsMeasurementsSupported, new StatusChangedOperation(status));
     56     }
     57 
     58     @Override
     59     protected ListenerOperation<IGpsMeasurementsListener> getHandlerOperation(int result) {
     60         final int status;
     61         switch (result) {
     62             case RESULT_SUCCESS:
     63                 status = GpsMeasurementsEvent.STATUS_READY;
     64                 break;
     65             case RESULT_NOT_AVAILABLE:
     66             case RESULT_NOT_SUPPORTED:
     67             case RESULT_INTERNAL_ERROR:
     68                 status = GpsMeasurementsEvent.STATUS_NOT_SUPPORTED;
     69                 break;
     70             case RESULT_GPS_LOCATION_DISABLED:
     71                 status = GpsMeasurementsEvent.STATUS_GPS_LOCATION_DISABLED;
     72                 break;
     73             default:
     74                 Log.v(TAG, "Unhandled addListener result: " + result);
     75                 return null;
     76         }
     77         return new StatusChangedOperation(status);
     78     }
     79 
     80     @Override
     81     protected void handleGpsEnabledChanged(boolean enabled) {
     82         int status = enabled ?
     83                 GpsMeasurementsEvent.STATUS_READY :
     84                 GpsMeasurementsEvent.STATUS_GPS_LOCATION_DISABLED;
     85         foreach(new StatusChangedOperation(status));
     86     }
     87 
     88     private class StatusChangedOperation implements ListenerOperation<IGpsMeasurementsListener> {
     89         private final int mStatus;
     90 
     91         public StatusChangedOperation(int status) {
     92             mStatus = status;
     93         }
     94 
     95         @Override
     96         public void execute(IGpsMeasurementsListener listener) throws RemoteException {
     97             listener.onStatusChanged(mStatus);
     98         }
     99     }
    100 }
    101