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.google.android.car.vms.publisher;
18 
19 import android.car.vms.VmsLayer;
20 import android.car.vms.VmsPublisherClientService;
21 import android.car.vms.VmsSubscriptionState;
22 import android.os.Handler;
23 import android.os.Message;
24 
25 import java.util.concurrent.atomic.AtomicBoolean;
26 
27 /**
28  * This service is launched during the initialization of the VMS publisher service.
29  * Once onVmsPublisherServiceReady is invoked, it starts publishing a single byte every second.
30  */
31 public class VmsPublisherClientSampleService extends VmsPublisherClientService {
32     public static final int PUBLISH_EVENT = 0;
33     public static final VmsLayer TEST_LAYER = new VmsLayer(0, 0, 0);
34     public static final int PUBLISHER_ID = 1;
35 
36     private byte mCounter = 0;
37     private AtomicBoolean mInitialized = new AtomicBoolean(false);
38 
39     private final Handler mHandler = new Handler() {
40         @Override
41         public void handleMessage(Message msg) {
42             if (msg.what == PUBLISH_EVENT && mInitialized.get()) {
43                 periodicPublish();
44             }
45         }
46     };
47 
48     /**
49      * Notifies that the publisher services are ready to be used: {@link #publish(VmsLayer, byte[])}
50      * and {@link #getSubscriptions()}.
51      */
52     @Override
onVmsPublisherServiceReady()53     public void onVmsPublisherServiceReady() {
54         VmsSubscriptionState subscriptionState = getSubscriptions();
55         onVmsSubscriptionChange(subscriptionState);
56     }
57 
58     @Override
onVmsSubscriptionChange(VmsSubscriptionState subscriptionState)59     public void onVmsSubscriptionChange(VmsSubscriptionState subscriptionState) {
60         if (mInitialized.compareAndSet(false, true)) {
61             for (VmsLayer layer : subscriptionState.getLayers()) {
62                 if (layer.equals(TEST_LAYER)) {
63                     mHandler.sendEmptyMessage(PUBLISH_EVENT);
64                 }
65             }
66         }
67     }
68 
69     @Override
onDestroy()70     public void onDestroy() {
71         super.onDestroy();
72         mInitialized.set(false);
73         mHandler.removeMessages(PUBLISH_EVENT);
74     }
75 
periodicPublish()76     private void periodicPublish() {
77         publish(TEST_LAYER, PUBLISHER_ID, new byte[]{mCounter});
78         ++mCounter;
79         mHandler.sendEmptyMessageDelayed(PUBLISH_EVENT, 1000);
80     }
81 }
82