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 android.location;
18 
19 import android.content.Context;
20 import android.os.RemoteException;
21 
22 import com.android.internal.util.Preconditions;
23 
24 /**
25  * A handler class to manage transport callbacks for {@link GnssMeasurementsEvent.Callback}.
26  *
27  * @hide
28  */
29 class GnssMeasurementCallbackTransport
30         extends LocalListenerHelper<GnssMeasurementsEvent.Callback> {
31     private static final String TAG = "GnssMeasCbTransport";
32     private final ILocationManager mLocationManager;
33 
34     private final IGnssMeasurementsListener mListenerTransport = new ListenerTransport();
35 
GnssMeasurementCallbackTransport(Context context, ILocationManager locationManager)36     public GnssMeasurementCallbackTransport(Context context, ILocationManager locationManager) {
37         super(context, TAG);
38         mLocationManager = locationManager;
39     }
40 
41     @Override
registerWithServer()42     protected boolean registerWithServer() throws RemoteException {
43         return mLocationManager.addGnssMeasurementsListener(
44                 mListenerTransport,
45                 getContext().getPackageName());
46     }
47 
48     @Override
unregisterFromServer()49     protected void unregisterFromServer() throws RemoteException {
50         mLocationManager.removeGnssMeasurementsListener(mListenerTransport);
51     }
52 
53     /**
54      * Injects GNSS measurement corrections into the GNSS chipset.
55      *
56      * @param measurementCorrections a {@link GnssMeasurementCorrections} object with the GNSS
57      *     measurement corrections to be injected into the GNSS chipset.
58      */
injectGnssMeasurementCorrections( GnssMeasurementCorrections measurementCorrections)59     protected void injectGnssMeasurementCorrections(
60             GnssMeasurementCorrections measurementCorrections) throws RemoteException {
61         Preconditions.checkNotNull(measurementCorrections);
62         mLocationManager.injectGnssMeasurementCorrections(
63                 measurementCorrections, getContext().getPackageName());
64     }
65 
getGnssCapabilities()66     protected long getGnssCapabilities() throws RemoteException {
67         return mLocationManager.getGnssCapabilities(getContext().getPackageName());
68     }
69 
70     private class ListenerTransport extends IGnssMeasurementsListener.Stub {
71         @Override
onGnssMeasurementsReceived(final GnssMeasurementsEvent event)72         public void onGnssMeasurementsReceived(final GnssMeasurementsEvent event) {
73             ListenerOperation<GnssMeasurementsEvent.Callback> operation =
74                     new ListenerOperation<GnssMeasurementsEvent.Callback>() {
75                         @Override
76                         public void execute(GnssMeasurementsEvent.Callback callback)
77                                 throws RemoteException {
78                             callback.onGnssMeasurementsReceived(event);
79                         }
80                     };
81             foreach(operation);
82         }
83 
84         @Override
onStatusChanged(final int status)85         public void onStatusChanged(final int status) {
86             ListenerOperation<GnssMeasurementsEvent.Callback> operation =
87                     new ListenerOperation<GnssMeasurementsEvent.Callback>() {
88                 @Override
89                 public void execute(GnssMeasurementsEvent.Callback callback)
90                         throws RemoteException {
91                     callback.onStatusChanged(status);
92                 }
93             };
94             foreach(operation);
95         }
96     }
97 }
98