Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.car.vms; |
| 18 | |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 19 | import android.annotation.CallbackExecutor; |
| 20 | import android.annotation.NonNull; |
Asaf Rosenfeld | 39e4f03 | 2017-09-16 11:33:50 -0700 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 22 | import android.car.CarManagerBase; |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 23 | import android.os.Binder; |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 24 | import android.os.IBinder; |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 25 | import android.os.RemoteException; |
| 26 | import android.util.Log; |
Demyn Plantenberg | 3ca1289 | 2017-06-20 21:01:34 -0700 | [diff] [blame] | 27 | |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 28 | import com.android.internal.annotations.GuardedBy; |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 29 | import com.android.internal.util.Preconditions; |
Demyn Plantenberg | 3ca1289 | 2017-06-20 21:01:34 -0700 | [diff] [blame] | 30 | |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 31 | import java.util.concurrent.Executor; |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 32 | |
| 33 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 34 | * API implementation for use by Vehicle Map Service subscribers. |
| 35 | * |
| 36 | * Supports a single client callback that can subscribe and unsubscribe to different data layers. |
| 37 | * {@link #setVmsSubscriberClientCallback} must be called before any subscription operations. |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 38 | * |
| 39 | * @hide |
| 40 | */ |
Asaf Rosenfeld | 39e4f03 | 2017-09-16 11:33:50 -0700 | [diff] [blame] | 41 | @SystemApi |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 42 | public final class VmsSubscriberManager implements CarManagerBase { |
| 43 | private static final boolean DBG = true; |
| 44 | private static final String TAG = "VmsSubscriberManager"; |
| 45 | |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 46 | private final IVmsSubscriberService mVmsSubscriberService; |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 47 | private final IVmsSubscriberClient mSubscriberManagerClient; |
| 48 | private final Object mClientCallbackLock = new Object(); |
| 49 | @GuardedBy("mClientCallbackLock") |
| 50 | private VmsSubscriberClientCallback mClientCallback; |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 51 | @GuardedBy("mClientCallbackLock") |
| 52 | private Executor mExecutor; |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 53 | |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 54 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 55 | * Callback interface for Vehicle Map Service subscribers. |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 56 | */ |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 57 | public interface VmsSubscriberClientCallback { |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 58 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 59 | * Called when a data packet is received. |
| 60 | * |
| 61 | * @param layer subscribed layer that packet was received for |
| 62 | * @param payload data packet that was received |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 63 | */ |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 64 | void onVmsMessageReceived(@NonNull VmsLayer layer, byte[] payload); |
Asaf Rosenfeld | 2ca6f8c | 2017-03-20 12:59:33 -0700 | [diff] [blame] | 65 | |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 66 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 67 | * Called when set of available data layers changes. |
| 68 | * |
| 69 | * @param availableLayers set of available data layers |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 70 | */ |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 71 | void onLayersAvailabilityChanged(@NonNull VmsAvailableLayers availableLayers); |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 75 | * Hidden constructor - can only be used internally. |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 76 | * |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 77 | * @hide |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 78 | */ |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 79 | public VmsSubscriberManager(IBinder service) { |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 80 | mVmsSubscriberService = IVmsSubscriberService.Stub.asInterface(service); |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 81 | mSubscriberManagerClient = new IVmsSubscriberClient.Stub() { |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 82 | @Override |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 83 | public void onVmsMessageReceived(VmsLayer layer, byte[] payload) { |
| 84 | Executor executor; |
| 85 | synchronized (mClientCallbackLock) { |
| 86 | executor = mExecutor; |
| 87 | } |
| 88 | if (executor == null) { |
| 89 | if (DBG) { |
| 90 | Log.d(TAG, "Executor is null in onVmsMessageReceived"); |
| 91 | } |
| 92 | return; |
| 93 | } |
| 94 | Binder.clearCallingIdentity(); |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 95 | executor.execute(() -> { |
| 96 | dispatchOnReceiveMessage(layer, payload); |
| 97 | }); |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 98 | } |
Asaf Rosenfeld | 2ca6f8c | 2017-03-20 12:59:33 -0700 | [diff] [blame] | 99 | |
| 100 | @Override |
Asaf Rosenfeld | 908811e | 2018-01-22 09:13:47 -0800 | [diff] [blame] | 101 | public void onLayersAvailabilityChanged(VmsAvailableLayers availableLayers) { |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 102 | Executor executor; |
| 103 | synchronized (mClientCallbackLock) { |
| 104 | executor = mExecutor; |
| 105 | } |
| 106 | if (executor == null) { |
| 107 | if (DBG) { |
| 108 | Log.d(TAG, "Executor is null in onLayersAvailabilityChanged"); |
| 109 | } |
| 110 | return; |
| 111 | } |
| 112 | Binder.clearCallingIdentity(); |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 113 | executor.execute(() -> { |
| 114 | dispatchOnAvailabilityChangeMessage(availableLayers); |
| 115 | }); |
Asaf Rosenfeld | 2ca6f8c | 2017-03-20 12:59:33 -0700 | [diff] [blame] | 116 | } |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 117 | }; |
| 118 | } |
| 119 | |
| 120 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 121 | * Sets the subscriber client's callback, for receiving layer availability and data events. |
| 122 | * |
| 123 | * @param executor {@link Executor} to handle the callbacks |
| 124 | * @param clientCallback subscriber callback that will handle events |
| 125 | * @throws IllegalStateException if the client callback was already set |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 126 | */ |
Justin Paupore | ccc75a3 | 2019-02-12 18:20:48 -0800 | [diff] [blame] | 127 | public void setVmsSubscriberClientCallback( |
| 128 | @NonNull @CallbackExecutor Executor executor, |
| 129 | @NonNull VmsSubscriberClientCallback clientCallback) { |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 130 | synchronized (mClientCallbackLock) { |
| 131 | if (mClientCallback != null) { |
| 132 | throw new IllegalStateException("Client callback is already configured."); |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 133 | } |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 134 | mClientCallback = Preconditions.checkNotNull(clientCallback, |
| 135 | "clientCallback cannot be null"); |
| 136 | mExecutor = Preconditions.checkNotNull(executor, "executor cannot be null"); |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 137 | } |
| 138 | try { |
| 139 | mVmsSubscriberService.addVmsSubscriberToNotifications(mSubscriberManagerClient); |
| 140 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 141 | throw e.rethrowFromSystemServer(); |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 145 | |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 146 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 147 | * Clears the subscriber client's callback. |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 148 | */ |
Justin Paupore | ccc75a3 | 2019-02-12 18:20:48 -0800 | [diff] [blame] | 149 | public void clearVmsSubscriberClientCallback() { |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 150 | synchronized (mClientCallbackLock) { |
| 151 | if (mExecutor == null) return; |
| 152 | } |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 153 | try { |
| 154 | mVmsSubscriberService.removeVmsSubscriberToNotifications(mSubscriberManagerClient); |
| 155 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 156 | throw e.rethrowFromSystemServer(); |
rleix | 4589973 | 2018-10-18 16:36:56 +0800 | [diff] [blame] | 157 | } finally { |
| 158 | synchronized (mClientCallbackLock) { |
| 159 | mClientCallback = null; |
| 160 | mExecutor = null; |
| 161 | } |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 166 | * Gets a publisher's self-reported description information. |
| 167 | * |
| 168 | * @param publisherId publisher ID to retrieve information for |
| 169 | * @return serialized publisher information, in a vendor-specific format |
Asaf Rosenfeld | 6028489 | 2017-05-19 12:59:56 -0700 | [diff] [blame] | 170 | */ |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 171 | @NonNull |
Justin Paupore | ccc75a3 | 2019-02-12 18:20:48 -0800 | [diff] [blame] | 172 | public byte[] getPublisherInfo(int publisherId) { |
Asaf Rosenfeld | 6028489 | 2017-05-19 12:59:56 -0700 | [diff] [blame] | 173 | try { |
| 174 | return mVmsSubscriberService.getPublisherInfo(publisherId); |
| 175 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 176 | throw e.rethrowFromSystemServer(); |
Asaf Rosenfeld | 6028489 | 2017-05-19 12:59:56 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 181 | * Gets all layers available for subscription. |
| 182 | * |
| 183 | * @return available layers |
Asaf Rosenfeld | 908811e | 2018-01-22 09:13:47 -0800 | [diff] [blame] | 184 | */ |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 185 | @NonNull |
Justin Paupore | ccc75a3 | 2019-02-12 18:20:48 -0800 | [diff] [blame] | 186 | public VmsAvailableLayers getAvailableLayers() { |
Asaf Rosenfeld | 908811e | 2018-01-22 09:13:47 -0800 | [diff] [blame] | 187 | try { |
| 188 | return mVmsSubscriberService.getAvailableLayers(); |
| 189 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 190 | throw e.rethrowFromSystemServer(); |
Asaf Rosenfeld | 908811e | 2018-01-22 09:13:47 -0800 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 195 | * Subscribes to data packets for a specific layer. |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 196 | * |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 197 | * @param layer layer to subscribe to |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 198 | * @throws IllegalStateException if the client callback was not set via |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 199 | * {@link #setVmsSubscriberClientCallback}. |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 200 | */ |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 201 | public void subscribe(@NonNull VmsLayer layer) { |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 202 | verifySubscriptionIsAllowed(); |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 203 | try { |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 204 | mVmsSubscriberService.addVmsSubscriber(mSubscriberManagerClient, layer); |
Demyn Plantenberg | 3ca1289 | 2017-06-20 21:01:34 -0700 | [diff] [blame] | 205 | VmsOperationRecorder.get().subscribe(layer); |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 206 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 207 | throw e.rethrowFromSystemServer(); |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 211 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 212 | * Subscribes to data packets for a specific layer from a specific publisher. |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 213 | * |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 214 | * @param layer layer to subscribe to |
| 215 | * @param publisherId a publisher of the layer |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 216 | * @throws IllegalStateException if the client callback was not set via |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 217 | * {@link #setVmsSubscriberClientCallback}. |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 218 | */ |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 219 | public void subscribe(@NonNull VmsLayer layer, int publisherId) { |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 220 | verifySubscriptionIsAllowed(); |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 221 | try { |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 222 | mVmsSubscriberService.addVmsSubscriberToPublisher( |
| 223 | mSubscriberManagerClient, layer, publisherId); |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 224 | VmsOperationRecorder.get().subscribe(layer, publisherId); |
| 225 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 226 | throw e.rethrowFromSystemServer(); |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 230 | /** |
| 231 | * Start monitoring all messages for all layers, regardless of subscriptions. |
| 232 | */ |
Justin Paupore | ccc75a3 | 2019-02-12 18:20:48 -0800 | [diff] [blame] | 233 | public void startMonitoring() { |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 234 | verifySubscriptionIsAllowed(); |
| 235 | try { |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 236 | mVmsSubscriberService.addVmsSubscriberPassive(mSubscriberManagerClient); |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 237 | VmsOperationRecorder.get().startMonitoring(); |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 238 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 239 | throw e.rethrowFromSystemServer(); |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 244 | * Unsubscribes from data packets for a specific layer. |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 245 | * |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 246 | * @param layer layer to unsubscribe from |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 247 | * @throws IllegalStateException if the client callback was not set via |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 248 | * {@link #setVmsSubscriberClientCallback}. |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 249 | */ |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 250 | public void unsubscribe(@NonNull VmsLayer layer) { |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 251 | verifySubscriptionIsAllowed(); |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 252 | try { |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 253 | mVmsSubscriberService.removeVmsSubscriber(mSubscriberManagerClient, layer); |
Demyn Plantenberg | 3ca1289 | 2017-06-20 21:01:34 -0700 | [diff] [blame] | 254 | VmsOperationRecorder.get().unsubscribe(layer); |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 255 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 256 | throw e.rethrowFromSystemServer(); |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 260 | /** |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 261 | * Unsubscribes from data packets for a specific layer from a specific publisher. |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 262 | * |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 263 | * @param layer layer to unsubscribe from |
| 264 | * @param publisherId a publisher of the layer |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 265 | * @throws IllegalStateException if the client callback was not set via |
Anna Linetsky | 40f1ded | 2018-05-07 16:37:01 -0700 | [diff] [blame] | 266 | * {@link #setVmsSubscriberClientCallback}. |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 267 | */ |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 268 | public void unsubscribe(@NonNull VmsLayer layer, int publisherId) { |
Antonio Cortes | 2ec3e35 | 2017-03-29 16:20:33 -0700 | [diff] [blame] | 269 | try { |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 270 | mVmsSubscriberService.removeVmsSubscriberToPublisher( |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 271 | mSubscriberManagerClient, layer, publisherId); |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 272 | VmsOperationRecorder.get().unsubscribe(layer, publisherId); |
| 273 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 274 | throw e.rethrowFromSystemServer(); |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
Mark Tabry | 16318e2 | 2019-03-22 10:04:32 -0700 | [diff] [blame] | 278 | /** |
| 279 | * Stop monitoring. Only receive messages for layers which have been subscribed to." |
| 280 | */ |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 281 | public void stopMonitoring() { |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 282 | try { |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 283 | mVmsSubscriberService.removeVmsSubscriberPassive(mSubscriberManagerClient); |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 284 | VmsOperationRecorder.get().stopMonitoring(); |
Antonio Cortes | 2ec3e35 | 2017-03-29 16:20:33 -0700 | [diff] [blame] | 285 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 286 | throw e.rethrowFromSystemServer(); |
Antonio Cortes | 2ec3e35 | 2017-03-29 16:20:33 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
Antonio Cortes | 01a6039 | 2017-03-22 13:00:02 -0700 | [diff] [blame] | 290 | private void dispatchOnReceiveMessage(VmsLayer layer, byte[] payload) { |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 291 | VmsSubscriberClientCallback clientCallback = getClientCallbackThreadSafe(); |
| 292 | if (clientCallback == null) { |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 293 | Log.e(TAG, "Cannot dispatch received message."); |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 294 | return; |
| 295 | } |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 296 | clientCallback.onVmsMessageReceived(layer, payload); |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Asaf Rosenfeld | 908811e | 2018-01-22 09:13:47 -0800 | [diff] [blame] | 299 | private void dispatchOnAvailabilityChangeMessage(VmsAvailableLayers availableLayers) { |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 300 | VmsSubscriberClientCallback clientCallback = getClientCallbackThreadSafe(); |
| 301 | if (clientCallback == null) { |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 302 | Log.e(TAG, "Cannot dispatch availability change message."); |
Asaf Rosenfeld | 2ca6f8c | 2017-03-20 12:59:33 -0700 | [diff] [blame] | 303 | return; |
| 304 | } |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 305 | clientCallback.onLayersAvailabilityChanged(availableLayers); |
Asaf Rosenfeld | 2ca6f8c | 2017-03-20 12:59:33 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 308 | private VmsSubscriberClientCallback getClientCallbackThreadSafe() { |
| 309 | VmsSubscriberClientCallback clientCallback; |
| 310 | synchronized (mClientCallbackLock) { |
| 311 | clientCallback = mClientCallback; |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 312 | } |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 313 | if (clientCallback == null) { |
| 314 | Log.e(TAG, "client callback not set."); |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 315 | } |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 316 | return clientCallback; |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Verifies that the subscriber is in a state where it is allowed to subscribe. |
| 321 | */ |
| 322 | private void verifySubscriptionIsAllowed() { |
Asaf Rosenfeld | a0b665f | 2017-08-25 11:11:37 -0700 | [diff] [blame] | 323 | VmsSubscriberClientCallback clientCallback = getClientCallbackThreadSafe(); |
| 324 | if (clientCallback == null) { |
Asaf Rosenfeld | 1a56ab9 | 2017-07-23 15:48:09 -0700 | [diff] [blame] | 325 | throw new IllegalStateException("Cannot subscribe."); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * @hide |
| 331 | */ |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 332 | @Override |
| 333 | public void onCarDisconnected() { |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 334 | } |
Antonio Cortes | a7d8ed1 | 2017-02-02 11:50:01 -0800 | [diff] [blame] | 335 | } |