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