Home | History | Annotate | Download | only in diagnostic
      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.graphics.Color;
     25 import android.os.Bundle;
     26 import android.support.car.hardware.CarSensorManager;
     27 import android.support.v4.app.Fragment;
     28 import android.util.Log;
     29 import android.view.LayoutInflater;
     30 import android.view.View;
     31 import android.view.ViewGroup;
     32 import android.widget.TextView;
     33 import com.google.android.car.kitchensink.KitchenSinkActivity;
     34 import com.google.android.car.kitchensink.R;
     35 import java.util.Objects;
     36 
     37 
     38 public class DiagnosticTestFragment extends Fragment {
     39     private static final String TAG = "CAR.DIAGNOSTIC.KS";
     40 
     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 
     49         TestListener(TextView view) {
     50             mTextView = Objects.requireNonNull(view);
     51         }
     52 
     53         @Override
     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
     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
     83     public void onResume() {
     84         super.onResume();
     85         resumeDiagnosticManager();
     86     }
     87 
     88     @Override
     89     public void onPause() {
     90         super.onPause();
     91         pauseDiagnosticManager();
     92     }
     93 
     94     private void resumeDiagnosticManager() {
     95         try {
     96             mDiagnosticManager =
     97                     (CarDiagnosticManager)mActivity.getCar().getCarManager(Car.DIAGNOSTIC_SERVICE);
     98             if (mLiveListener != null) {
     99                 mDiagnosticManager.registerListener(mLiveListener,
    100                     CarDiagnosticManager.FRAME_TYPE_LIVE,
    101                     CarSensorManager.SENSOR_RATE_NORMAL);
    102             }
    103             if (mFreezeListener != null) {
    104                 mDiagnosticManager.registerListener(mFreezeListener,
    105                     CarDiagnosticManager.FRAME_TYPE_FREEZE,
    106                     CarSensorManager.SENSOR_RATE_NORMAL);
    107             }
    108         } catch (android.car.CarNotConnectedException|android.support.car.CarNotConnectedException e) {
    109             Log.e(TAG, "Car not connected or not supported", e);
    110         }
    111     }
    112 
    113     private void pauseDiagnosticManager() {
    114         if (mDiagnosticManager != null) {
    115             if (mLiveListener != null) {
    116                 mDiagnosticManager.unregisterListener(mLiveListener);
    117             }
    118             if (mFreezeListener != null) {
    119                 mDiagnosticManager.unregisterListener(mFreezeListener);
    120             }
    121         }
    122     }
    123 }
    124