Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -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 | |
| 19 | |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Asaf Rosenfeld | 39e4f03 | 2017-09-16 11:33:50 -0700 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 22 | import android.app.Service; |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 23 | import android.content.Intent; |
| 24 | import android.os.Handler; |
| 25 | import android.os.IBinder; |
| 26 | import android.os.Looper; |
| 27 | import android.os.Message; |
| 28 | import android.os.RemoteException; |
| 29 | import android.util.Log; |
| 30 | |
Antonio Cortes | 9db963a | 2017-03-10 15:15:28 -0800 | [diff] [blame] | 31 | import com.android.internal.annotations.GuardedBy; |
| 32 | |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 33 | import java.lang.ref.WeakReference; |
| 34 | |
| 35 | /** |
| 36 | * Services that need VMS publisher services need to inherit from this class and also need to be |
| 37 | * declared in the array vmsPublisherClients located in |
| 38 | * packages/services/Car/service/res/values/config.xml (most likely, this file will be in an overlay |
| 39 | * of the target product. |
| 40 | * |
| 41 | * The {@link com.android.car.VmsPublisherService} will start this service. The callback |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 42 | * {@link #onVmsPublisherServiceReady()} notifies when VMS publisher services can be used, and the |
| 43 | * publisher can request a publisher ID in order to start publishing. |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 44 | * |
| 45 | * SystemApi candidate. |
| 46 | * |
| 47 | * @hide |
| 48 | */ |
Asaf Rosenfeld | 39e4f03 | 2017-09-16 11:33:50 -0700 | [diff] [blame] | 49 | @SystemApi |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 50 | public abstract class VmsPublisherClientService extends Service { |
| 51 | private static final boolean DBG = true; |
| 52 | private static final String TAG = "VmsPublisherClient"; |
| 53 | |
Asaf Rosenfeld | 7bd684e | 2017-03-13 16:24:27 -0700 | [diff] [blame] | 54 | private final Object mLock = new Object(); |
| 55 | |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 56 | private Handler mHandler = new VmsEventHandler(this); |
| 57 | private final VmsPublisherClientBinder mVmsPublisherClient = new VmsPublisherClientBinder(this); |
| 58 | private volatile IVmsPublisherService mVmsPublisherService = null; |
Asaf Rosenfeld | 7bd684e | 2017-03-13 16:24:27 -0700 | [diff] [blame] | 59 | @GuardedBy("mLock") |
Asaf Rosenfeld | e8369b6 | 2017-03-17 14:11:56 -0700 | [diff] [blame] | 60 | private IBinder mToken = null; |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 61 | |
| 62 | @Override |
Asaf Rosenfeld | c2faf15 | 2018-01-31 13:59:06 -0800 | [diff] [blame] | 63 | public IBinder onBind(Intent intent) { |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 64 | if (DBG) { |
| 65 | Log.d(TAG, "onBind, intent: " + intent); |
| 66 | } |
| 67 | return mVmsPublisherClient.asBinder(); |
| 68 | } |
| 69 | |
| 70 | @Override |
Asaf Rosenfeld | c2faf15 | 2018-01-31 13:59:06 -0800 | [diff] [blame] | 71 | public boolean onUnbind(Intent intent) { |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 72 | if (DBG) { |
| 73 | Log.d(TAG, "onUnbind, intent: " + intent); |
| 74 | } |
| 75 | stopSelf(); |
| 76 | return super.onUnbind(intent); |
| 77 | } |
| 78 | |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 79 | private void setToken(IBinder token) { |
Asaf Rosenfeld | 7bd684e | 2017-03-13 16:24:27 -0700 | [diff] [blame] | 80 | synchronized (mLock) { |
| 81 | mToken = token; |
| 82 | } |
| 83 | } |
| 84 | |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Notifies that the publisher services are ready. |
| 87 | */ |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 88 | protected abstract void onVmsPublisherServiceReady(); |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Publishers need to implement this method to receive notifications of subscription changes. |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 92 | * |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 93 | * @param subscriptionState the state of the subscriptions. |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 94 | */ |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 95 | public abstract void onVmsSubscriptionChange(VmsSubscriptionState subscriptionState); |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Uses the VmsPublisherService binder to publish messages. |
| 99 | * |
Antonio Cortes | 01a6039 | 2017-03-22 13:00:02 -0700 | [diff] [blame] | 100 | * @param layer the layer to publish to. |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 101 | * @param payload the message to be sent. |
Asaf Rosenfeld | 800cb07 | 2017-07-18 10:56:11 -0700 | [diff] [blame] | 102 | * @param publisherId the ID that got assigned to the publisher that published the message by |
| 103 | * VMS core. |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 104 | * @return if the call to the method VmsPublisherService.publish was successful. |
| 105 | */ |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 106 | public final void publish(VmsLayer layer, int publisherId, byte[] payload) { |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 107 | if (DBG) { |
Antonio Cortes | 01a6039 | 2017-03-22 13:00:02 -0700 | [diff] [blame] | 108 | Log.d(TAG, "Publishing for layer : " + layer); |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 109 | } |
Asaf Rosenfeld | eb541d4 | 2017-03-31 14:32:10 -0700 | [diff] [blame] | 110 | |
| 111 | IBinder token = getTokenForPublisherServiceThreadSafe(); |
| 112 | |
| 113 | try { |
Asaf Rosenfeld | 800cb07 | 2017-07-18 10:56:11 -0700 | [diff] [blame] | 114 | mVmsPublisherService.publish(token, layer, publisherId, payload); |
Asaf Rosenfeld | eb541d4 | 2017-03-31 14:32:10 -0700 | [diff] [blame] | 115 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 116 | throw e.rethrowFromSystemServer(); |
Asaf Rosenfeld | eb541d4 | 2017-03-31 14:32:10 -0700 | [diff] [blame] | 117 | } |
Asaf Rosenfeld | eb541d4 | 2017-03-31 14:32:10 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Uses the VmsPublisherService binder to set the layers offering. |
| 122 | * |
| 123 | * @param offering the layers that the publisher may publish. |
| 124 | * @return if the call to VmsPublisherService.setLayersOffering was successful. |
| 125 | */ |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 126 | public final void setLayersOffering(VmsLayersOffering offering) { |
Asaf Rosenfeld | eb541d4 | 2017-03-31 14:32:10 -0700 | [diff] [blame] | 127 | if (DBG) { |
| 128 | Log.d(TAG, "Setting layers offering : " + offering); |
| 129 | } |
| 130 | |
| 131 | IBinder token = getTokenForPublisherServiceThreadSafe(); |
| 132 | |
| 133 | try { |
| 134 | mVmsPublisherService.setLayersOffering(token, offering); |
Demyn Plantenberg | 3ca1289 | 2017-06-20 21:01:34 -0700 | [diff] [blame] | 135 | VmsOperationRecorder.get().setLayersOffering(offering); |
Asaf Rosenfeld | eb541d4 | 2017-03-31 14:32:10 -0700 | [diff] [blame] | 136 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 137 | throw e.rethrowFromSystemServer(); |
Asaf Rosenfeld | eb541d4 | 2017-03-31 14:32:10 -0700 | [diff] [blame] | 138 | } |
Asaf Rosenfeld | eb541d4 | 2017-03-31 14:32:10 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | private IBinder getTokenForPublisherServiceThreadSafe() { |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 142 | if (mVmsPublisherService == null) { |
| 143 | throw new IllegalStateException("VmsPublisherService not set."); |
| 144 | } |
Asaf Rosenfeld | 7bd684e | 2017-03-13 16:24:27 -0700 | [diff] [blame] | 145 | |
| 146 | IBinder token; |
| 147 | synchronized (mLock) { |
| 148 | token = mToken; |
| 149 | } |
| 150 | if (token == null) { |
| 151 | throw new IllegalStateException("VmsPublisherService does not have a valid token."); |
| 152 | } |
Asaf Rosenfeld | eb541d4 | 2017-03-31 14:32:10 -0700 | [diff] [blame] | 153 | return token; |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 156 | public final int getPublisherId(byte[] publisherInfo) { |
Asaf Rosenfeld | 6028489 | 2017-05-19 12:59:56 -0700 | [diff] [blame] | 157 | if (mVmsPublisherService == null) { |
| 158 | throw new IllegalStateException("VmsPublisherService not set."); |
| 159 | } |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 160 | Integer publisherId = null; |
Asaf Rosenfeld | 6028489 | 2017-05-19 12:59:56 -0700 | [diff] [blame] | 161 | try { |
| 162 | Log.i(TAG, "Getting publisher static ID"); |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 163 | publisherId = mVmsPublisherService.getPublisherId(publisherInfo); |
Asaf Rosenfeld | 6028489 | 2017-05-19 12:59:56 -0700 | [diff] [blame] | 164 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 165 | throw e.rethrowFromSystemServer(); |
Asaf Rosenfeld | 6028489 | 2017-05-19 12:59:56 -0700 | [diff] [blame] | 166 | } |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 167 | if (publisherId == null) { |
Asaf Rosenfeld | 6028489 | 2017-05-19 12:59:56 -0700 | [diff] [blame] | 168 | throw new IllegalStateException("VmsPublisherService cannot get a publisher static ID."); |
Demyn Plantenberg | 3ca1289 | 2017-06-20 21:01:34 -0700 | [diff] [blame] | 169 | } else { |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 170 | VmsOperationRecorder.get().getPublisherId(publisherId); |
Asaf Rosenfeld | 6028489 | 2017-05-19 12:59:56 -0700 | [diff] [blame] | 171 | } |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 172 | return publisherId; |
Asaf Rosenfeld | 6028489 | 2017-05-19 12:59:56 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 175 | /** |
Asaf Rosenfeld | 2e45d81 | 2017-08-31 23:51:12 -0700 | [diff] [blame] | 176 | * Uses the VmsPublisherService binder to get the state of the subscriptions. |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 177 | * |
Antonio Cortes | 9db963a | 2017-03-10 15:15:28 -0800 | [diff] [blame] | 178 | * @return list of layer/version or null in case of error. |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 179 | */ |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 180 | public final @Nullable VmsSubscriptionState getSubscriptions() { |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 181 | if (mVmsPublisherService == null) { |
| 182 | throw new IllegalStateException("VmsPublisherService not set."); |
| 183 | } |
| 184 | try { |
Asaf Rosenfeld | 7bd684e | 2017-03-13 16:24:27 -0700 | [diff] [blame] | 185 | return mVmsPublisherService.getSubscriptions(); |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 186 | } catch (RemoteException e) { |
Justin Paupore | 0dd3177 | 2019-02-07 21:22:34 -0800 | [diff] [blame] | 187 | throw e.rethrowFromSystemServer(); |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 188 | } |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | private void setVmsPublisherService(IVmsPublisherService service) { |
| 192 | mVmsPublisherService = service; |
| 193 | onVmsPublisherServiceReady(); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Implements the interface that the VMS service uses to communicate with this client. |
| 198 | */ |
| 199 | private static class VmsPublisherClientBinder extends IVmsPublisherClient.Stub { |
| 200 | private final WeakReference<VmsPublisherClientService> mVmsPublisherClientService; |
Antonio Cortes | 9db963a | 2017-03-10 15:15:28 -0800 | [diff] [blame] | 201 | @GuardedBy("mSequenceLock") |
| 202 | private long mSequence = -1; |
| 203 | private final Object mSequenceLock = new Object(); |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 204 | |
| 205 | public VmsPublisherClientBinder(VmsPublisherClientService vmsPublisherClientService) { |
| 206 | mVmsPublisherClientService = new WeakReference<>(vmsPublisherClientService); |
| 207 | } |
| 208 | |
| 209 | @Override |
Asaf Rosenfeld | 7bd684e | 2017-03-13 16:24:27 -0700 | [diff] [blame] | 210 | public void setVmsPublisherService(IBinder token, IVmsPublisherService service) |
| 211 | throws RemoteException { |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 212 | VmsPublisherClientService vmsPublisherClientService = mVmsPublisherClientService.get(); |
| 213 | if (vmsPublisherClientService == null) return; |
| 214 | if (DBG) { |
| 215 | Log.d(TAG, "setting VmsPublisherService."); |
| 216 | } |
| 217 | Handler handler = vmsPublisherClientService.mHandler; |
| 218 | handler.sendMessage( |
| 219 | handler.obtainMessage(VmsEventHandler.SET_SERVICE_CALLBACK, service)); |
Asaf Rosenfeld | 7bd684e | 2017-03-13 16:24:27 -0700 | [diff] [blame] | 220 | vmsPublisherClientService.setToken(token); |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 224 | public void onVmsSubscriptionChange(VmsSubscriptionState subscriptionState) |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 225 | throws RemoteException { |
| 226 | VmsPublisherClientService vmsPublisherClientService = mVmsPublisherClientService.get(); |
| 227 | if (vmsPublisherClientService == null) return; |
| 228 | if (DBG) { |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 229 | Log.d(TAG, "subscription event: " + subscriptionState); |
Antonio Cortes | 9db963a | 2017-03-10 15:15:28 -0800 | [diff] [blame] | 230 | } |
| 231 | synchronized (mSequenceLock) { |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 232 | if (subscriptionState.getSequenceNumber() <= mSequence) { |
Antonio Cortes | 9db963a | 2017-03-10 15:15:28 -0800 | [diff] [blame] | 233 | Log.w(TAG, "Sequence out of order. Current sequence = " + mSequence |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 234 | + "; expected new sequence = " + subscriptionState.getSequenceNumber()); |
Antonio Cortes | 9db963a | 2017-03-10 15:15:28 -0800 | [diff] [blame] | 235 | // Do not propagate old notifications. |
| 236 | return; |
| 237 | } else { |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 238 | mSequence = subscriptionState.getSequenceNumber(); |
Antonio Cortes | 9db963a | 2017-03-10 15:15:28 -0800 | [diff] [blame] | 239 | } |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 240 | } |
| 241 | Handler handler = vmsPublisherClientService.mHandler; |
| 242 | handler.sendMessage( |
| 243 | handler.obtainMessage(VmsEventHandler.ON_SUBSCRIPTION_CHANGE_EVENT, |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 244 | subscriptionState)); |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Receives events from the binder thread and dispatches them. |
| 250 | */ |
| 251 | private final static class VmsEventHandler extends Handler { |
| 252 | /** Constants handled in the handler */ |
| 253 | private static final int ON_SUBSCRIPTION_CHANGE_EVENT = 0; |
| 254 | private static final int SET_SERVICE_CALLBACK = 1; |
| 255 | |
| 256 | private final WeakReference<VmsPublisherClientService> mVmsPublisherClientService; |
| 257 | |
| 258 | VmsEventHandler(VmsPublisherClientService service) { |
| 259 | super(Looper.getMainLooper()); |
| 260 | mVmsPublisherClientService = new WeakReference<>(service); |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public void handleMessage(Message msg) { |
| 265 | VmsPublisherClientService service = mVmsPublisherClientService.get(); |
| 266 | if (service == null) return; |
| 267 | switch (msg.what) { |
| 268 | case ON_SUBSCRIPTION_CHANGE_EVENT: |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame] | 269 | VmsSubscriptionState subscriptionState = (VmsSubscriptionState) msg.obj; |
| 270 | service.onVmsSubscriptionChange(subscriptionState); |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 271 | break; |
| 272 | case SET_SERVICE_CALLBACK: |
| 273 | service.setVmsPublisherService((IVmsPublisherService) msg.obj); |
| 274 | break; |
| 275 | default: |
| 276 | Log.e(TAG, "Event type not handled: " + msg.what); |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | } |
Antonio Cortes | 9db04df | 2017-02-07 08:38:05 -0800 | [diff] [blame] | 281 | } |