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