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 com.android.car;
18 
19 import android.car.vms.IVmsSubscriberClient;
20 import android.car.vms.IVmsSubscriberService;
21 import android.car.vms.VmsAvailableLayers;
22 import android.car.vms.VmsLayer;
23 import android.content.Context;
24 import android.os.RemoteException;
25 import android.util.Log;
26 
27 import com.android.car.hal.VmsHalService;
28 import com.android.car.vms.VmsBrokerService;
29 import com.android.car.vms.VmsClientManager;
30 
31 import java.io.PrintWriter;
32 
33 /**
34  * Offers subscriber services by implementing IVmsSubscriberService.Stub.
35  */
36 public class VmsSubscriberService extends IVmsSubscriberService.Stub implements CarServiceBase,
37         VmsBrokerService.SubscriberListener {
38     private static final String TAG = "VmsSubscriberService";
39 
40     private final Context mContext;
41     private final VmsBrokerService mBrokerService;
42     private final VmsClientManager mClientManager;
43 
44     /**
45      * Constructor for client manager.
46      *
47      * @param context           Context to use for registering receivers and binding services.
48      * @param brokerService     Service managing the VMS publisher/subscriber state.
49      * @param clientManager     Service for monitoring VMS subscriber clients.
50      * @param hal               Service providing the HAL client interface
51      */
VmsSubscriberService(Context context, VmsBrokerService brokerService, VmsClientManager clientManager, VmsHalService hal)52     VmsSubscriberService(Context context, VmsBrokerService brokerService,
53             VmsClientManager clientManager, VmsHalService hal) {
54         mContext = context;
55         mBrokerService = brokerService;
56         mClientManager = clientManager;
57         mBrokerService.addSubscriberListener(this);
58         hal.setVmsSubscriberService(this);
59     }
60 
61     @Override
init()62     public void init() {}
63 
64     @Override
release()65     public void release() {}
66 
67     @Override
dump(PrintWriter writer)68     public void dump(PrintWriter writer) {
69     }
70 
71     @Override
addVmsSubscriberToNotifications(IVmsSubscriberClient subscriber)72     public void addVmsSubscriberToNotifications(IVmsSubscriberClient subscriber) {
73         ICarImpl.assertVmsSubscriberPermission(mContext);
74         mClientManager.addSubscriber(subscriber);
75     }
76 
77     @Override
removeVmsSubscriberToNotifications(IVmsSubscriberClient subscriber)78     public void removeVmsSubscriberToNotifications(IVmsSubscriberClient subscriber) {
79         ICarImpl.assertVmsSubscriberPermission(mContext);
80         mClientManager.removeSubscriber(subscriber);
81     }
82 
83     @Override
addVmsSubscriber(IVmsSubscriberClient subscriber, VmsLayer layer)84     public void addVmsSubscriber(IVmsSubscriberClient subscriber, VmsLayer layer) {
85         ICarImpl.assertVmsSubscriberPermission(mContext);
86         mClientManager.addSubscriber(subscriber);
87         mBrokerService.addSubscription(subscriber, layer);
88     }
89 
90     @Override
removeVmsSubscriber(IVmsSubscriberClient subscriber, VmsLayer layer)91     public void removeVmsSubscriber(IVmsSubscriberClient subscriber, VmsLayer layer) {
92         ICarImpl.assertVmsSubscriberPermission(mContext);
93         mBrokerService.removeSubscription(subscriber, layer);
94     }
95 
96     @Override
addVmsSubscriberToPublisher(IVmsSubscriberClient subscriber, VmsLayer layer, int publisherId)97     public void addVmsSubscriberToPublisher(IVmsSubscriberClient subscriber,
98             VmsLayer layer,
99             int publisherId) {
100         ICarImpl.assertVmsSubscriberPermission(mContext);
101         mClientManager.addSubscriber(subscriber);
102         mBrokerService.addSubscription(subscriber, layer, publisherId);
103     }
104 
105     @Override
removeVmsSubscriberToPublisher(IVmsSubscriberClient subscriber, VmsLayer layer, int publisherId)106     public void removeVmsSubscriberToPublisher(IVmsSubscriberClient subscriber,
107             VmsLayer layer,
108             int publisherId) {
109         ICarImpl.assertVmsSubscriberPermission(mContext);
110         mBrokerService.removeSubscription(subscriber, layer, publisherId);
111     }
112 
113     @Override
addVmsSubscriberPassive(IVmsSubscriberClient subscriber)114     public void addVmsSubscriberPassive(IVmsSubscriberClient subscriber) {
115         ICarImpl.assertVmsSubscriberPermission(mContext);
116         mClientManager.addSubscriber(subscriber);
117         mBrokerService.addSubscription(subscriber);
118     }
119 
120     @Override
removeVmsSubscriberPassive(IVmsSubscriberClient subscriber)121     public void removeVmsSubscriberPassive(IVmsSubscriberClient subscriber) {
122         ICarImpl.assertVmsSubscriberPermission(mContext);
123         mBrokerService.removeSubscription(subscriber);
124     }
125 
126     @Override
getPublisherInfo(int publisherId)127     public byte[] getPublisherInfo(int publisherId) {
128         ICarImpl.assertVmsSubscriberPermission(mContext);
129         return mBrokerService.getPublisherInfo(publisherId);
130     }
131 
132     @Override
getAvailableLayers()133     public VmsAvailableLayers getAvailableLayers() {
134         ICarImpl.assertVmsSubscriberPermission(mContext);
135         return mBrokerService.getAvailableLayers();
136     }
137 
138     @Override
onLayersAvailabilityChange(VmsAvailableLayers availableLayers)139     public void onLayersAvailabilityChange(VmsAvailableLayers availableLayers) {
140         for (IVmsSubscriberClient subscriber : mClientManager.getAllSubscribers()) {
141             try {
142                 subscriber.onLayersAvailabilityChanged(availableLayers);
143             } catch (RemoteException e) {
144                 Log.e(TAG, "onLayersAvailabilityChanged failed: "
145                         + mClientManager.getPackageName(subscriber), e);
146             }
147         }
148     }
149 }
150