blob: caf1d6ca5e35d919add49804403367791a6e040d [file] [log] [blame]
destradaaea8a8a62014-06-23 18:19:03 -07001/*
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
17package com.android.server.location;
18
Lifu Tang818aa2c2016-02-01 01:52:00 -080019import android.location.GnssMeasurementsEvent;
20import android.location.IGnssMeasurementsListener;
destradaa6568d702014-10-27 12:47:41 -070021import android.os.Handler;
destradaaea8a8a62014-06-23 18:19:03 -070022import android.os.RemoteException;
destradaa6568d702014-10-27 12:47:41 -070023import android.util.Log;
destradaaea8a8a62014-06-23 18:19:03 -070024
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 */
Lifu Tang818aa2c2016-02-01 01:52:00 -080032public abstract class GnssMeasurementsProvider
33 extends RemoteListenerHelper<IGnssMeasurementsListener> {
34 private static final String TAG = "GnssMeasurementsProvider";
destradaa6568d702014-10-27 12:47:41 -070035
Lifu Tang818aa2c2016-02-01 01:52:00 -080036 protected GnssMeasurementsProvider(Handler handler) {
destradaa6568d702014-10-27 12:47:41 -070037 super(handler, TAG);
destradaa4b3e3932014-07-21 18:01:47 -070038 }
destradaaea8a8a62014-06-23 18:19:03 -070039
Lifu Tang818aa2c2016-02-01 01:52:00 -080040 public void onMeasurementsAvailable(final GnssMeasurementsEvent event) {
41 ListenerOperation<IGnssMeasurementsListener> operation =
42 new ListenerOperation<IGnssMeasurementsListener>() {
destradaaea8a8a62014-06-23 18:19:03 -070043 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -080044 public void execute(IGnssMeasurementsListener listener) throws RemoteException {
45 listener.onGnssMeasurementsReceived(event);
destradaaea8a8a62014-06-23 18:19:03 -070046 }
47 };
destradaaea8a8a62014-06-23 18:19:03 -070048 foreach(operation);
49 }
destradaa6568d702014-10-27 12:47:41 -070050
Lifu Tang818aa2c2016-02-01 01:52:00 -080051 public void onCapabilitiesUpdated(boolean isGnssMeasurementsSupported) {
52 setSupported(isGnssMeasurementsSupported);
destradaa13a60b02015-01-15 18:36:01 -080053 updateResult();
54 }
55
56 public void onGpsEnabledChanged() {
57 if (tryUpdateRegistrationWithService()) {
58 updateResult();
59 }
destradaa6568d702014-10-27 12:47:41 -070060 }
61
62 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -080063 protected ListenerOperation<IGnssMeasurementsListener> getHandlerOperation(int result) {
destradaa13a60b02015-01-15 18:36:01 -080064 int status;
destradaa6568d702014-10-27 12:47:41 -070065 switch (result) {
66 case RESULT_SUCCESS:
Lifu Tange8abe8e2016-04-01 10:32:05 -070067 status = GnssMeasurementsEvent.Callback.STATUS_READY;
destradaa6568d702014-10-27 12:47:41 -070068 break;
69 case RESULT_NOT_AVAILABLE:
70 case RESULT_NOT_SUPPORTED:
71 case RESULT_INTERNAL_ERROR:
Lifu Tange8abe8e2016-04-01 10:32:05 -070072 status = GnssMeasurementsEvent.Callback.STATUS_NOT_SUPPORTED;
destradaa6568d702014-10-27 12:47:41 -070073 break;
74 case RESULT_GPS_LOCATION_DISABLED:
Lifu Tange8abe8e2016-04-01 10:32:05 -070075 status = GnssMeasurementsEvent.Callback.STATUS_LOCATION_DISABLED;
destradaa6568d702014-10-27 12:47:41 -070076 break;
destradaa13a60b02015-01-15 18:36:01 -080077 case RESULT_UNKNOWN:
78 return null;
destradaa6568d702014-10-27 12:47:41 -070079 default:
80 Log.v(TAG, "Unhandled addListener result: " + result);
81 return null;
82 }
83 return new StatusChangedOperation(status);
84 }
85
destradaa13a60b02015-01-15 18:36:01 -080086 private static class StatusChangedOperation
Lifu Tang818aa2c2016-02-01 01:52:00 -080087 implements ListenerOperation<IGnssMeasurementsListener> {
destradaa6568d702014-10-27 12:47:41 -070088 private final int mStatus;
89
90 public StatusChangedOperation(int status) {
91 mStatus = status;
92 }
93
94 @Override
Lifu Tang818aa2c2016-02-01 01:52:00 -080095 public void execute(IGnssMeasurementsListener listener) throws RemoteException {
destradaa6568d702014-10-27 12:47:41 -070096 listener.onStatusChanged(mStatus);
97 }
98 }
destradaaea8a8a62014-06-23 18:19:03 -070099}