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