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.kitchensink.diagnostic; 18 19 import android.annotation.Nullable; 20 import android.car.Car; 21 import android.car.diagnostic.CarDiagnosticEvent; 22 import android.car.diagnostic.CarDiagnosticManager; 23 import android.car.diagnostic.CarDiagnosticManager.OnDiagnosticEventListener; 24 import android.car.hardware.CarSensorManager; 25 import android.graphics.Color; 26 import android.os.Bundle; 27 import android.view.LayoutInflater; 28 import android.view.View; 29 import android.view.ViewGroup; 30 import android.widget.TextView; 31 32 import androidx.fragment.app.Fragment; 33 34 import com.google.android.car.kitchensink.KitchenSinkActivity; 35 import com.google.android.car.kitchensink.R; 36 37 import java.util.Objects; 38 39 40 public class DiagnosticTestFragment extends Fragment { 41 private KitchenSinkActivity mActivity; 42 private TextView mLiveDiagnosticInfo; 43 private TextView mFreezeDiagnosticInfo; 44 private CarDiagnosticManager mDiagnosticManager; 45 46 private final class TestListener implements OnDiagnosticEventListener { 47 private final TextView mTextView; 48 TestListener(TextView view)49 TestListener(TextView view) { 50 mTextView = Objects.requireNonNull(view); 51 } 52 53 @Override onDiagnosticEvent(CarDiagnosticEvent carDiagnosticEvent)54 public void onDiagnosticEvent(CarDiagnosticEvent carDiagnosticEvent) { 55 mTextView.post(() -> mTextView.setText(carDiagnosticEvent.toString())); 56 } 57 } 58 59 private OnDiagnosticEventListener mLiveListener; 60 private OnDiagnosticEventListener mFreezeListener; 61 62 @Nullable 63 @Override onCreateView( LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)64 public View onCreateView( 65 LayoutInflater inflater, 66 @Nullable ViewGroup container, 67 @Nullable Bundle savedInstanceState) { 68 View view = inflater.inflate(R.layout.diagnostic, container, false); 69 mActivity = (KitchenSinkActivity) getHost(); 70 71 mLiveDiagnosticInfo = (TextView) view.findViewById(R.id.live_diagnostic_info); 72 mLiveDiagnosticInfo.setTextColor(Color.RED); 73 mLiveListener = new TestListener(mLiveDiagnosticInfo); 74 75 mFreezeDiagnosticInfo = (TextView) view.findViewById(R.id.freeze_diagnostic_info); 76 mFreezeDiagnosticInfo.setTextColor(Color.RED); 77 mFreezeListener = new TestListener(mFreezeDiagnosticInfo); 78 79 return view; 80 } 81 82 @Override onResume()83 public void onResume() { 84 super.onResume(); 85 resumeDiagnosticManager(); 86 } 87 88 @Override onPause()89 public void onPause() { 90 super.onPause(); 91 pauseDiagnosticManager(); 92 } 93 resumeDiagnosticManager()94 private void resumeDiagnosticManager() { 95 Car car = mActivity.getCar(); 96 if (!car.isFeatureEnabled(Car.DIAGNOSTIC_SERVICE)) { 97 String notSupported = Car.DIAGNOSTIC_SERVICE + " not supported"; 98 mLiveDiagnosticInfo.setText(notSupported); 99 mFreezeDiagnosticInfo.setText(notSupported); 100 return; 101 } 102 mDiagnosticManager = 103 (CarDiagnosticManager) car.getCarManager(Car.DIAGNOSTIC_SERVICE); 104 if (mLiveListener != null) { 105 mDiagnosticManager.registerListener(mLiveListener, 106 CarDiagnosticManager.FRAME_TYPE_LIVE, 107 CarSensorManager.SENSOR_RATE_NORMAL); 108 } 109 if (mFreezeListener != null) { 110 mDiagnosticManager.registerListener(mFreezeListener, 111 CarDiagnosticManager.FRAME_TYPE_FREEZE, 112 CarSensorManager.SENSOR_RATE_NORMAL); 113 } 114 } 115 pauseDiagnosticManager()116 private void pauseDiagnosticManager() { 117 if (mDiagnosticManager != null) { 118 if (mLiveListener != null) { 119 mDiagnosticManager.unregisterListener(mLiveListener); 120 } 121 if (mFreezeListener != null) { 122 mDiagnosticManager.unregisterListener(mFreezeListener); 123 } 124 } 125 } 126 } 127