aboutsummaryrefslogtreecommitdiff
path: root/service/src/com/android/car/VmsPublisherService.java
blob: a240e171de0d7178f431ddcc29f10b017a9b16c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.car;

import android.car.vms.IVmsPublisherClient;
import android.car.vms.IVmsPublisherService;
import android.car.vms.IVmsSubscriberClient;
import android.car.vms.VmsLayer;
import android.car.vms.VmsLayersOffering;
import android.car.vms.VmsSubscriptionState;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;

import com.android.car.hal.VmsHalService;
import com.android.car.hal.VmsHalService.VmsHalPublisherListener;

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

/**
 * Receives HAL updates by implementing VmsHalService.VmsHalListener.
 * Binds to publishers and configures them to use this service.
 * Notifies publishers of subscription changes.
 */
public class VmsPublisherService extends IVmsPublisherService.Stub implements CarServiceBase {
    private static final boolean DBG = true;
    private static final String TAG = "VmsPublisherService";

    private static final int MSG_HAL_SUBSCRIPTION_CHANGED = 1;

    private final Context mContext;
    private final VmsHalService mHal;
    private final Map<String, PublisherConnection> mPublisherConnectionMap = new ArrayMap<>();
    private final Map<String, IVmsPublisherClient> mPublisherMap = new ArrayMap<>();
    private final Set<String> mSafePermissions;
    private final Handler mHandler = new EventHandler();
    private final VmsHalPublisherListener mHalPublisherListener;

    private BroadcastReceiver mBootCompleteReceiver;

    public VmsPublisherService(Context context, VmsHalService hal) {
        mContext = context;
        mHal = hal;

        mHalPublisherListener = subscriptionState -> mHandler.sendMessage(
                mHandler.obtainMessage(MSG_HAL_SUBSCRIPTION_CHANGED, subscriptionState));

        // Load permissions that can be granted to publishers.
        mSafePermissions = new ArraySet<>(
                Arrays.asList(mContext.getResources().getStringArray(R.array.vmsSafePermissions)));
    }

    // Implements CarServiceBase interface.
    @Override
    public void init() {
        mHal.addPublisherListener(mHalPublisherListener);

        if (isTestEnvironment()) {
            Log.d(TAG, "Running under test environment");
            bindToAllPublishers();
        } else {
            mBootCompleteReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(intent.getAction())) {
                        onLockedBootCompleted();
                    } else {
                        Log.e(TAG, "Unexpected action received: " + intent);
                    }
                }
            };

            mContext.registerReceiver(mBootCompleteReceiver,
                    new IntentFilter(Intent.ACTION_LOCKED_BOOT_COMPLETED));
        }
    }

    private void bindToAllPublishers() {
        String[] publisherNames = mContext.getResources().getStringArray(
                R.array.vmsPublisherClients);
        if (DBG) Log.d(TAG, "Publishers found: " + publisherNames.length);

        for (String publisherName : publisherNames) {
            if (TextUtils.isEmpty(publisherName)) {
                Log.e(TAG, "empty publisher name");
                continue;
            }
            ComponentName name = ComponentName.unflattenFromString(publisherName);
            if (name == null) {
                Log.e(TAG, "invalid publisher name: " + publisherName);
                continue;
            }

            if (!mContext.getPackageManager().isPackageAvailable(name.getPackageName())) {
                Log.w(TAG, "VMS publisher not installed: " + publisherName);
                continue;
            }

            bind(name);
        }
    }

    @Override
    public void release() {
        if (mBootCompleteReceiver != null) {
            mContext.unregisterReceiver(mBootCompleteReceiver);
            mBootCompleteReceiver = null;
        }
        mHal.removePublisherListener(mHalPublisherListener);

        for (PublisherConnection connection : mPublisherConnectionMap.values()) {
            mContext.unbindService(connection);
        }
        mPublisherConnectionMap.clear();
        mPublisherMap.clear();
    }

    @Override
    public void dump(PrintWriter writer) {
        writer.println("*" + getClass().getSimpleName() + "*");
        writer.println("mSafePermissions: " + mSafePermissions);
        writer.println("mPublisherMap:" + mPublisherMap);
        writer.println("mPublisherConnectionMap:" + mPublisherConnectionMap);
    }

    /* Called in arbitrary binder thread */
    @Override
    public void setLayersOffering(IBinder token, VmsLayersOffering offering) {
        mHal.setPublisherLayersOffering(token, offering);
    }

    /* Called in arbitrary binder thread */
    @Override
    public void publish(IBinder token, VmsLayer layer, int publisherId, byte[] payload) {
        if (DBG) {
            Log.d(TAG, "Publishing for layer: " + layer);
        }
        ICarImpl.assertVmsPublisherPermission(mContext);

        // Send the message to application listeners.
        Set<IVmsSubscriberClient> listeners =
                mHal.getSubscribersForLayerFromPublisher(layer, publisherId);

        if (DBG) {
            Log.d(TAG, "Number of subscribed apps: " + listeners.size());
        }
        for (IVmsSubscriberClient listener : listeners) {
            try {
                listener.onVmsMessageReceived(layer, payload);
            } catch (RemoteException ex) {
                Log.e(TAG, "unable to publish to listener: " + listener);
            }
        }

        // Send the message to HAL
        if (mHal.isHalSubscribed(layer)) {
            Log.d(TAG, "HAL is subscribed");
            mHal.setDataMessage(layer, payload);
        } else {
            Log.d(TAG, "HAL is NOT subscribed");
        }
    }

    /* Called in arbitrary binder thread */
    @Override
    public VmsSubscriptionState getSubscriptions() {
        ICarImpl.assertVmsPublisherPermission(mContext);
        return mHal.getSubscriptionState();
    }

    /* Called in arbitrary binder thread */
    @Override
    public int getPublisherId(byte[] publisherInfo) {
        ICarImpl.assertVmsPublisherPermission(mContext);
        return mHal.getPublisherId(publisherInfo);
    }

    private void onLockedBootCompleted() {
        if (DBG) Log.i(TAG, "onLockedBootCompleted");

        bindToAllPublishers();
    }

    /**
     * This method is only invoked by VmsHalService.notifyPublishers which is synchronized.
     * Therefore this method only sees a non-decreasing sequence.
     */
    private void handleHalSubscriptionChanged(VmsSubscriptionState subscriptionState) {
        // Send the message to application listeners.
        for (IVmsPublisherClient client : mPublisherMap.values()) {
            try {
                client.onVmsSubscriptionChange(subscriptionState);
            } catch (RemoteException ex) {
                Log.e(TAG, "unable to send notification to: " + client, ex);
            }
        }
    }

    /**
     * Tries to bind to a publisher.
     *
     * @param name publisher component name (e.g. android.car.vms.logger/.LoggingService).
     */
    private void bind(ComponentName name) {
        String publisherName = name.flattenToString();
        if (DBG) {
            Log.d(TAG, "binding to: " + publisherName);
        }

        if (mPublisherConnectionMap.containsKey(publisherName)) {
            // Already registered, nothing to do.
            return;
        }
        grantPermissions(name);
        Intent intent = new Intent();
        intent.setComponent(name);
        PublisherConnection connection = new PublisherConnection(name);
        if (mContext.bindServiceAsUser(intent, connection,
                Context.BIND_AUTO_CREATE, UserHandle.SYSTEM)) {
            mPublisherConnectionMap.put(publisherName, connection);
        } else {
            Log.e(TAG, "unable to bind to: " + publisherName);
        }
    }

    /**
     * Removes the publisher and associated connection.
     *
     * @param name publisher component name (e.g. android.car.vms.Logger).
     */
    private void unbind(ComponentName name) {
        String publisherName = name.flattenToString();
        if (DBG) {
            Log.d(TAG, "unbinding from: " + publisherName);
        }

        boolean found = mPublisherMap.remove(publisherName) != null;
        if (found) {
            PublisherConnection connection = mPublisherConnectionMap.get(publisherName);
            mContext.unbindService(connection);
            mPublisherConnectionMap.remove(publisherName);
        } else {
            Log.e(TAG, "unbind: unknown publisher." + publisherName);
        }
    }

    private void grantPermissions(ComponentName component) {
        final PackageManager packageManager = mContext.getPackageManager();
        final String packageName = component.getPackageName();
        PackageInfo packageInfo;
        try {
            packageInfo = packageManager.getPackageInfo(packageName,
                    PackageManager.GET_PERMISSIONS);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Error getting package info for " + packageName, e);
            return;
        }
        if (packageInfo.requestedPermissions == null) return;
        for (String permission : packageInfo.requestedPermissions) {
            if (!mSafePermissions.contains(permission)) {
                continue;
            }
            if (packageManager.checkPermission(permission, packageName)
                    == PackageManager.PERMISSION_GRANTED) {
                continue;
            }
            try {
                packageManager.grantRuntimePermission(packageName, permission,
                        UserHandle.SYSTEM);
                Log.d(TAG, "Permission " + permission + " granted to " + packageName);
            } catch (SecurityException | IllegalArgumentException e) {
                Log.e(TAG, "Error while trying to grant " + permission + " to " + packageName,
                        e);
            }
        }
    }

    private boolean isTestEnvironment() {
        // If the context is derived from other package it means we're running under
        // environment.
        return !TextUtils.equals(mContext.getBasePackageName(), mContext.getPackageName());
    }

    class PublisherConnection implements ServiceConnection {
        private final IBinder mToken = new Binder();
        private final ComponentName mName;

        PublisherConnection(ComponentName name) {
            mName = name;
        }

        private final Runnable mBindRunnable = new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "delayed binding for: " + mName);
                bind(mName);
            }
        };

        /**
         * Once the service binds to a publisher service, the publisher binder is added to
         * mPublisherMap
         * and the publisher is configured to use this service.
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            if (DBG) {
                Log.d(TAG, "onServiceConnected, name: " + name + ", binder: " + binder);
            }
            IVmsPublisherClient service = IVmsPublisherClient.Stub.asInterface(binder);
            mPublisherMap.put(name.flattenToString(), service);
            try {
                service.setVmsPublisherService(mToken, VmsPublisherService.this);
            } catch (RemoteException e) {
                Log.e(TAG, "unable to configure publisher: " + name, e);
            }
        }

        /**
         * Tries to rebind to the publisher service.
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {
            String publisherName = name.flattenToString();
            Log.d(TAG, "onServiceDisconnected, name: " + publisherName);

            int millisecondsToWait = mContext.getResources().getInteger(
                    com.android.car.R.integer.millisecondsBeforeRebindToVmsPublisher);
            if (!mName.flattenToString().equals(name.flattenToString())) {
                throw new IllegalArgumentException(
                    "Mismatch on publisherConnection. Expected: " + mName + " Got: " + name);
            }
            mHandler.postDelayed(mBindRunnable, millisecondsToWait);

            unbind(name);
        }
    }

    private class EventHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_HAL_SUBSCRIPTION_CHANGED:
                    handleHalSubscriptionChanged((VmsSubscriptionState) msg.obj);
                    return;
            }
            super.handleMessage(msg);
        }
    }
}