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 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.annotation.SystemApi; 23 import android.app.Service; 24 import android.car.Car; 25 import android.car.annotation.AddedInOrBefore; 26 import android.car.annotation.RequiredFeature; 27 import android.car.vms.VmsClientManager.VmsClientCallback; 28 import android.content.Intent; 29 import android.os.Binder; 30 import android.os.Handler; 31 import android.os.IBinder; 32 import android.os.Looper; 33 import android.util.Log; 34 35 import com.android.car.internal.os.HandlerExecutor; 36 import com.android.internal.annotations.GuardedBy; 37 import com.android.internal.annotations.VisibleForTesting; 38 39 /** 40 * API implementation of a Vehicle Map Service publisher client. 41 * 42 * All publisher clients must inherit from this class and export it as a service, and the service 43 * be added to either the {@code vmsPublisherSystemClients} or {@code vmsPublisherUserClients} 44 * arrays in the Car service configuration, depending on which user the client will run as. 45 * 46 * The {@link com.android.car.VmsPublisherService} will then bind to this service, with the 47 * {@link #onVmsPublisherServiceReady()} callback notifying the client implementation when the 48 * connection is established and publisher operations can be called. 49 * 50 * Publishers must also register a publisher ID by calling {@link #getPublisherId(byte[])}. 51 * 52 * @deprecated Use {@link VmsClientManager} instead 53 * @hide 54 */ 55 @RequiredFeature(Car.VEHICLE_MAP_SERVICE) 56 @Deprecated 57 @SystemApi 58 public abstract class VmsPublisherClientService extends Service { 59 private static final boolean DBG = false; 60 private static final String TAG = "VmsPublisherClientService"; 61 62 63 private final Handler mHandler = new Handler(Looper.getMainLooper()); 64 private final VmsClientCallback mClientCallback = new PublisherClientCallback(); 65 66 private final Object mLock = new Object(); 67 @GuardedBy("mLock") 68 private @Nullable Car mCar; 69 @GuardedBy("mLock") 70 private @Nullable VmsClient mClient; 71 72 @Override 73 @AddedInOrBefore(majorVersion = 33) onCreate()74 public void onCreate() { 75 if (DBG) Log.d(TAG, "Connecting to Car service"); 76 synchronized (mLock) { 77 mCar = Car.createCar(this, mHandler, Car.CAR_WAIT_TIMEOUT_DO_NOT_WAIT, 78 this::onCarLifecycleChanged); 79 } 80 } 81 82 @Override 83 @AddedInOrBefore(majorVersion = 33) onDestroy()84 public void onDestroy() { 85 if (DBG) Log.d(TAG, "Disconnecting from Car service"); 86 synchronized (mLock) { 87 if (mCar != null) { 88 mCar.disconnect(); 89 mCar = null; 90 } 91 } 92 } 93 94 @Override 95 @AddedInOrBefore(majorVersion = 33) onBind(Intent intent)96 public IBinder onBind(Intent intent) { 97 if (DBG) Log.d(TAG, "onBind, intent: " + intent); 98 return new Binder(); 99 } 100 101 /** 102 * @hide 103 */ 104 @VisibleForTesting 105 @AddedInOrBefore(majorVersion = 33) onCarLifecycleChanged(Car car, boolean ready)106 protected void onCarLifecycleChanged(Car car, boolean ready) { 107 if (DBG) Log.d(TAG, "Car service ready: " + ready); 108 if (ready) { 109 VmsClientManager clientManager = 110 (VmsClientManager) car.getCarManager(Car.VEHICLE_MAP_SERVICE); 111 if (DBG) Log.d(TAG, "VmsClientManager: " + clientManager); 112 if (clientManager == null) { 113 Log.e(TAG, "VmsClientManager is not available"); 114 return; 115 } 116 clientManager.registerVmsClientCallback(new HandlerExecutor(mHandler), mClientCallback, 117 /* legacyClient= */ true); 118 } 119 } 120 121 /** 122 * Notifies the client that publisher services are ready. 123 */ 124 @AddedInOrBefore(majorVersion = 33) onVmsPublisherServiceReady()125 protected abstract void onVmsPublisherServiceReady(); 126 127 /** 128 * Notifies the client of changes in layer subscriptions. 129 * 130 * @param subscriptionState state of layer subscriptions 131 */ 132 @AddedInOrBefore(majorVersion = 33) onVmsSubscriptionChange(@onNull VmsSubscriptionState subscriptionState)133 public abstract void onVmsSubscriptionChange(@NonNull VmsSubscriptionState subscriptionState); 134 135 /** 136 * Publishes a data packet to subscribers. 137 * 138 * Publishers must only publish packets for the layers that they have made offerings for. 139 * 140 * @param layer layer to publish to 141 * @param publisherId ID of the publisher publishing the message 142 * @param payload data packet to be sent 143 * @throws IllegalStateException if publisher services are not available 144 */ 145 @AddedInOrBefore(majorVersion = 33) publish(@onNull VmsLayer layer, int publisherId, byte[] payload)146 public final void publish(@NonNull VmsLayer layer, int publisherId, byte[] payload) { 147 getVmsClient().publishPacket(publisherId, layer, payload); 148 } 149 150 /** 151 * Sets the layers offered by a specific publisher. 152 * 153 * @param offering layers being offered for subscription by the publisher 154 * @throws IllegalStateException if publisher services are not available 155 */ 156 @AddedInOrBefore(majorVersion = 33) setLayersOffering(@onNull VmsLayersOffering offering)157 public final void setLayersOffering(@NonNull VmsLayersOffering offering) { 158 getVmsClient().setProviderOfferings(offering.getPublisherId(), offering.getDependencies()); 159 } 160 161 /** 162 * Acquires a publisher ID for a serialized publisher description. 163 * 164 * Multiple calls to this method with the same information will return the same publisher ID. 165 * 166 * @param publisherInfo serialized publisher description information, in a vendor-specific 167 * format 168 * @return a publisher ID for the given publisher description 169 * @throws IllegalStateException if publisher services are not available 170 */ 171 @AddedInOrBefore(majorVersion = 33) getPublisherId(byte[] publisherInfo)172 public final int getPublisherId(byte[] publisherInfo) { 173 return getVmsClient().registerProvider(publisherInfo); 174 } 175 176 /** 177 * Gets the state of layer subscriptions. 178 * 179 * @return state of layer subscriptions 180 * @throws IllegalStateException if publisher services are not available 181 */ 182 @AddedInOrBefore(majorVersion = 33) getSubscriptions()183 public final VmsSubscriptionState getSubscriptions() { 184 return getVmsClient().getSubscriptionState(); 185 } 186 getVmsClient()187 private VmsClient getVmsClient() { 188 synchronized (mLock) { 189 if (mClient == null) { 190 throw new IllegalStateException("VMS client connection is not ready"); 191 } 192 return mClient; 193 } 194 } 195 196 private class PublisherClientCallback implements VmsClientCallback { 197 @Override onClientConnected(VmsClient client)198 public void onClientConnected(VmsClient client) { 199 synchronized (mLock) { 200 mClient = client; 201 } 202 onVmsPublisherServiceReady(); 203 } 204 205 @Override onSubscriptionStateChanged(VmsSubscriptionState subscriptionState)206 public void onSubscriptionStateChanged(VmsSubscriptionState subscriptionState) { 207 onVmsSubscriptionChange(subscriptionState); 208 } 209 210 @Override onLayerAvailabilityChanged(VmsAvailableLayers availableLayers)211 public void onLayerAvailabilityChanged(VmsAvailableLayers availableLayers) { 212 // Ignored 213 } 214 215 @Override onPacketReceived(int providerId, VmsLayer layer, byte[] packet)216 public void onPacketReceived(int providerId, VmsLayer layer, byte[] packet) { 217 // Does not subscribe to packets 218 } 219 } 220 } 221