destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 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 | |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame^] | 19 | import android.location.GnssMeasurementsEvent; |
| 20 | import android.location.IGnssMeasurementsListener; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 21 | import android.os.Handler; |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 22 | import android.os.RemoteException; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 23 | import android.util.Log; |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 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 | */ |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame^] | 32 | public abstract class GnssMeasurementsProvider |
| 33 | extends RemoteListenerHelper<IGnssMeasurementsListener> { |
| 34 | private static final String TAG = "GnssMeasurementsProvider"; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 35 | |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame^] | 36 | protected GnssMeasurementsProvider(Handler handler) { |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 37 | super(handler, TAG); |
destradaa | 4b3e393 | 2014-07-21 18:01:47 -0700 | [diff] [blame] | 38 | } |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 39 | |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame^] | 40 | public void onMeasurementsAvailable(final GnssMeasurementsEvent event) { |
| 41 | ListenerOperation<IGnssMeasurementsListener> operation = |
| 42 | new ListenerOperation<IGnssMeasurementsListener>() { |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 43 | @Override |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame^] | 44 | public void execute(IGnssMeasurementsListener listener) throws RemoteException { |
| 45 | listener.onGnssMeasurementsReceived(event); |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 46 | } |
| 47 | }; |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 48 | foreach(operation); |
| 49 | } |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 50 | |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame^] | 51 | public void onCapabilitiesUpdated(boolean isGnssMeasurementsSupported) { |
| 52 | setSupported(isGnssMeasurementsSupported); |
destradaa | 13a60b0 | 2015-01-15 18:36:01 -0800 | [diff] [blame] | 53 | updateResult(); |
| 54 | } |
| 55 | |
| 56 | public void onGpsEnabledChanged() { |
| 57 | if (tryUpdateRegistrationWithService()) { |
| 58 | updateResult(); |
| 59 | } |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame^] | 63 | protected ListenerOperation<IGnssMeasurementsListener> getHandlerOperation(int result) { |
destradaa | 13a60b0 | 2015-01-15 18:36:01 -0800 | [diff] [blame] | 64 | int status; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 65 | switch (result) { |
| 66 | case RESULT_SUCCESS: |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame^] | 67 | status = GnssMeasurementsEvent.STATUS_READY; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 68 | break; |
| 69 | case RESULT_NOT_AVAILABLE: |
| 70 | case RESULT_NOT_SUPPORTED: |
| 71 | case RESULT_INTERNAL_ERROR: |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame^] | 72 | status = GnssMeasurementsEvent.STATUS_NOT_SUPPORTED; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 73 | break; |
| 74 | case RESULT_GPS_LOCATION_DISABLED: |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame^] | 75 | status = GnssMeasurementsEvent.STATUS_GPS_LOCATION_DISABLED; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 76 | break; |
destradaa | 13a60b0 | 2015-01-15 18:36:01 -0800 | [diff] [blame] | 77 | case RESULT_UNKNOWN: |
| 78 | return null; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 79 | default: |
| 80 | Log.v(TAG, "Unhandled addListener result: " + result); |
| 81 | return null; |
| 82 | } |
| 83 | return new StatusChangedOperation(status); |
| 84 | } |
| 85 | |
destradaa | 13a60b0 | 2015-01-15 18:36:01 -0800 | [diff] [blame] | 86 | private static class StatusChangedOperation |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame^] | 87 | implements ListenerOperation<IGnssMeasurementsListener> { |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 88 | private final int mStatus; |
| 89 | |
| 90 | public StatusChangedOperation(int status) { |
| 91 | mStatus = status; |
| 92 | } |
| 93 | |
| 94 | @Override |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame^] | 95 | public void execute(IGnssMeasurementsListener listener) throws RemoteException { |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 96 | listener.onStatusChanged(mStatus); |
| 97 | } |
| 98 | } |
destradaa | ea8a8a6 | 2014-06-23 18:19:03 -0700 | [diff] [blame] | 99 | } |