Home | History | Annotate | Download | only in subscriber
      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.subscriber;
     18 
     19 import android.app.Activity;
     20 import android.car.vms.VmsAvailableLayers;
     21 import android.car.vms.VmsLayer;
     22 import android.car.vms.VmsSubscriberManager;
     23 import android.content.pm.PackageManager;
     24 import android.os.Bundle;
     25 import android.support.car.Car;
     26 import android.support.car.CarConnectionCallback;
     27 import android.util.Log;
     28 import android.widget.TextView;
     29 import java.util.concurrent.Executor;
     30 
     31 /**
     32  * Connects to the Car service during onCreate. CarConnectionCallback.onConnected is invoked when
     33  * the connection is ready. Then, it subscribes to a VMS layer/version and updates the TextView when
     34  * a message is received.
     35  */
     36 public class VmsSubscriberClientSampleActivity extends Activity {
     37     private static final String TAG = "VmsSampleActivity";
     38     // The layer id and version should match the ones defined in
     39     // com.google.android.car.vms.publisher.VmsPublisherClientSampleService
     40     public static final VmsLayer TEST_LAYER = new VmsLayer(0, 0, 0);
     41 
     42     private Car mCarApi;
     43     private TextView mTextView;
     44     private VmsSubscriberManager mVmsSubscriberManager;
     45     private Executor mExecutor;
     46 
     47     private class ThreadPerTaskExecutor implements Executor {
     48         public void execute(Runnable r) {
     49             new Thread(r).start();
     50         }
     51     }
     52 
     53     @Override
     54     protected void onCreate(Bundle savedInstanceState) {
     55         mExecutor = new ThreadPerTaskExecutor();
     56         super.onCreate(savedInstanceState);
     57         setContentView(R.layout.activity_main);
     58         mTextView = (TextView) findViewById(R.id.textview);
     59         if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
     60             mCarApi = Car.createCar(this, mCarConnectionCallback);
     61             mCarApi.connect();
     62         } else {
     63             Log.d(TAG, "No automotive feature.");
     64         }
     65     }
     66 
     67     @Override
     68     protected void onDestroy() {
     69         super.onDestroy();
     70         if (mCarApi != null) {
     71             mCarApi.disconnect();
     72         }
     73         Log.i(TAG, "onDestroy");
     74     }
     75 
     76     private final CarConnectionCallback mCarConnectionCallback = new CarConnectionCallback() {
     77         @Override
     78         public void onConnected(Car car) {
     79             Log.d(TAG, "Connected to Car Service");
     80             mVmsSubscriberManager = getVmsSubscriberManager();
     81             configureSubscriptions(mVmsSubscriberManager);
     82         }
     83 
     84         @Override
     85         public void onDisconnected(Car car) {
     86             Log.d(TAG, "Disconnect from Car Service");
     87         }
     88 
     89         private VmsSubscriberManager getVmsSubscriberManager() {
     90             try {
     91                 return (VmsSubscriberManager) mCarApi.getCarManager(
     92                         android.car.Car.VMS_SUBSCRIBER_SERVICE);
     93             } catch (android.support.car.CarNotConnectedException e) {
     94                 Log.e(TAG, "Car is not connected!", e);
     95             }
     96             return null;
     97         }
     98 
     99         private void configureSubscriptions(VmsSubscriberManager vmsSubscriberManager) {
    100             try {
    101                 vmsSubscriberManager.setVmsSubscriberClientCallback(mExecutor, mClientCallback);
    102                 vmsSubscriberManager.subscribe(TEST_LAYER);
    103             } catch (android.car.CarNotConnectedException e) {
    104                 Log.e(TAG, "Car is not connected!", e);
    105             }
    106         }
    107 
    108     };
    109 
    110     private final VmsSubscriberManager.VmsSubscriberClientCallback mClientCallback =
    111         new VmsSubscriberManager.VmsSubscriberClientCallback() {
    112             @Override
    113             public void onVmsMessageReceived(VmsLayer layer, byte[] payload) {
    114                 mTextView.setText(String.valueOf(payload[0]));
    115             }
    116 
    117             @Override
    118             public void onLayersAvailabilityChanged(VmsAvailableLayers availableLayers) {
    119                 mTextView.setText(String.valueOf(availableLayers));
    120             }
    121         };
    122 }
    123