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 |
Anil Admal | efd9dc6 | 2019-03-12 17:39:20 -0700 | [diff] [blame] | 14 | * limitations under the License. |
destradaa | 4b3e393 | 2014-07-21 18:01:47 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.server.location; |
| 18 | |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Lifu Tang | e8abe8e | 2016-04-01 10:32:05 -0700 | [diff] [blame] | 20 | import android.location.GnssNavigationMessage; |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 21 | import android.location.IGnssNavigationMessageListener; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 22 | import android.os.Handler; |
destradaa | 4b3e393 | 2014-07-21 18:01:47 -0700 | [diff] [blame] | 23 | import android.os.RemoteException; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 24 | import android.util.Log; |
destradaa | 4b3e393 | 2014-07-21 18:01:47 -0700 | [diff] [blame] | 25 | |
Yu-Han Yang | 23d9216 | 2018-04-19 06:03:00 -0700 | [diff] [blame] | 26 | import com.android.internal.annotations.VisibleForTesting; |
| 27 | |
destradaa | 4b3e393 | 2014-07-21 18:01:47 -0700 | [diff] [blame] | 28 | /** |
| 29 | * An base implementation for GPS navigation messages provider. |
| 30 | * It abstracts out the responsibility of handling listeners, while still allowing technology |
| 31 | * specific implementations to be built. |
| 32 | * |
| 33 | * @hide |
| 34 | */ |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 35 | public abstract class GnssNavigationMessageProvider |
Yu-Han Yang | 519694b | 2020-01-14 10:51:41 -0800 | [diff] [blame^] | 36 | extends RemoteListenerHelper<Void, IGnssNavigationMessageListener> { |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 37 | private static final String TAG = "GnssNavigationMessageProvider"; |
Yu-Han Yang | 23d9216 | 2018-04-19 06:03:00 -0700 | [diff] [blame] | 38 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 39 | |
| 40 | private final GnssNavigationMessageProviderNative mNative; |
| 41 | private boolean mCollectionStarted; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 42 | |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 43 | protected GnssNavigationMessageProvider(Context context, Handler handler) { |
| 44 | this(context, handler, new GnssNavigationMessageProviderNative()); |
Yu-Han Yang | 23d9216 | 2018-04-19 06:03:00 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @VisibleForTesting |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 48 | public GnssNavigationMessageProvider(Context context, Handler handler, |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 49 | GnssNavigationMessageProviderNative aNative) { |
| 50 | super(context, handler, TAG); |
Yu-Han Yang | 23d9216 | 2018-04-19 06:03:00 -0700 | [diff] [blame] | 51 | mNative = aNative; |
| 52 | } |
| 53 | |
Yu-Han Yang | 23d9216 | 2018-04-19 06:03:00 -0700 | [diff] [blame] | 54 | void resumeIfStarted() { |
| 55 | if (DEBUG) { |
| 56 | Log.d(TAG, "resumeIfStarted"); |
| 57 | } |
| 58 | if (mCollectionStarted) { |
| 59 | mNative.startNavigationMessageCollection(); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected boolean isAvailableInPlatform() { |
| 65 | return mNative.isNavigationMessageSupported(); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected int registerWithService() { |
| 70 | boolean result = mNative.startNavigationMessageCollection(); |
| 71 | if (result) { |
| 72 | mCollectionStarted = true; |
| 73 | return RemoteListenerHelper.RESULT_SUCCESS; |
| 74 | } else { |
| 75 | return RemoteListenerHelper.RESULT_INTERNAL_ERROR; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected void unregisterFromService() { |
| 81 | boolean stopped = mNative.stopNavigationMessageCollection(); |
| 82 | if (stopped) { |
| 83 | mCollectionStarted = false; |
| 84 | } |
destradaa | 4b3e393 | 2014-07-21 18:01:47 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Lifu Tang | e8abe8e | 2016-04-01 10:32:05 -0700 | [diff] [blame] | 87 | public void onNavigationMessageAvailable(final GnssNavigationMessage event) { |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 88 | foreach((IGnssNavigationMessageListener listener, CallerIdentity callerIdentity) -> { |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 89 | listener.onGnssNavigationMessageReceived(event); |
| 90 | } |
| 91 | ); |
destradaa | 4b3e393 | 2014-07-21 18:01:47 -0700 | [diff] [blame] | 92 | } |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 93 | |
Anil Admal | efd9dc6 | 2019-03-12 17:39:20 -0700 | [diff] [blame] | 94 | /** Handle GNSS capabilities update from the GNSS HAL implementation */ |
Anil Admal | 312fddb | 2019-03-25 12:15:43 -0700 | [diff] [blame] | 95 | public void onCapabilitiesUpdated(boolean isGnssNavigationMessageSupported) { |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 96 | setSupported(isGnssNavigationMessageSupported); |
destradaa | 13a60b0 | 2015-01-15 18:36:01 -0800 | [diff] [blame] | 97 | updateResult(); |
| 98 | } |
| 99 | |
| 100 | public void onGpsEnabledChanged() { |
Wyatt Riley | aa420d5 | 2017-07-03 15:14:42 -0700 | [diff] [blame] | 101 | tryUpdateRegistrationWithService(); |
| 102 | updateResult(); |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | @Override |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 106 | protected ListenerOperation<IGnssNavigationMessageListener> getHandlerOperation(int result) { |
destradaa | 13a60b0 | 2015-01-15 18:36:01 -0800 | [diff] [blame] | 107 | int status; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 108 | switch (result) { |
| 109 | case RESULT_SUCCESS: |
Lifu Tang | e8abe8e | 2016-04-01 10:32:05 -0700 | [diff] [blame] | 110 | status = GnssNavigationMessage.Callback.STATUS_READY; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 111 | break; |
| 112 | case RESULT_NOT_AVAILABLE: |
| 113 | case RESULT_NOT_SUPPORTED: |
| 114 | case RESULT_INTERNAL_ERROR: |
Lifu Tang | e8abe8e | 2016-04-01 10:32:05 -0700 | [diff] [blame] | 115 | status = GnssNavigationMessage.Callback.STATUS_NOT_SUPPORTED; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 116 | break; |
| 117 | case RESULT_GPS_LOCATION_DISABLED: |
Lifu Tang | e8abe8e | 2016-04-01 10:32:05 -0700 | [diff] [blame] | 118 | status = GnssNavigationMessage.Callback.STATUS_LOCATION_DISABLED; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 119 | break; |
destradaa | 13a60b0 | 2015-01-15 18:36:01 -0800 | [diff] [blame] | 120 | case RESULT_UNKNOWN: |
| 121 | return null; |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 122 | default: |
| 123 | Log.v(TAG, "Unhandled addListener result: " + result); |
| 124 | return null; |
| 125 | } |
| 126 | return new StatusChangedOperation(status); |
| 127 | } |
| 128 | |
destradaa | 13a60b0 | 2015-01-15 18:36:01 -0800 | [diff] [blame] | 129 | private static class StatusChangedOperation |
Lifu Tang | 818aa2c | 2016-02-01 01:52:00 -0800 | [diff] [blame] | 130 | implements ListenerOperation<IGnssNavigationMessageListener> { |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 131 | private final int mStatus; |
| 132 | |
| 133 | public StatusChangedOperation(int status) { |
| 134 | mStatus = status; |
| 135 | } |
| 136 | |
| 137 | @Override |
Anil Admal | 75b9fd6 | 2018-11-28 11:22:50 -0800 | [diff] [blame] | 138 | public void execute(IGnssNavigationMessageListener listener, |
Anil Admal | 08b9612 | 2019-01-30 16:55:05 -0800 | [diff] [blame] | 139 | CallerIdentity callerIdentity) throws RemoteException { |
destradaa | 6568d70 | 2014-10-27 12:47:41 -0700 | [diff] [blame] | 140 | listener.onStatusChanged(mStatus); |
| 141 | } |
| 142 | } |
Yu-Han Yang | 23d9216 | 2018-04-19 06:03:00 -0700 | [diff] [blame] | 143 | |
| 144 | @VisibleForTesting |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 145 | public static class GnssNavigationMessageProviderNative { |
Yu-Han Yang | 23d9216 | 2018-04-19 06:03:00 -0700 | [diff] [blame] | 146 | public boolean isNavigationMessageSupported() { |
| 147 | return native_is_navigation_message_supported(); |
| 148 | } |
| 149 | |
| 150 | public boolean startNavigationMessageCollection() { |
| 151 | return native_start_navigation_message_collection(); |
| 152 | } |
| 153 | |
| 154 | public boolean stopNavigationMessageCollection() { |
| 155 | return native_stop_navigation_message_collection(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private static native boolean native_is_navigation_message_supported(); |
| 160 | |
| 161 | private static native boolean native_start_navigation_message_collection(); |
| 162 | |
| 163 | private static native boolean native_stop_navigation_message_collection(); |
destradaa | 4b3e393 | 2014-07-21 18:01:47 -0700 | [diff] [blame] | 164 | } |