Home | History | Annotate | Download | only in walt
      1 /*
      2  * Copyright (C) 2015 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 org.chromium.latency.walt;
     18 
     19 
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.os.Bundle;
     24 import android.support.v4.app.Fragment;
     25 import android.text.method.ScrollingMovementMethod;
     26 import android.view.LayoutInflater;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 import android.widget.TextView;
     30 
     31 
     32 /**
     33  * This screen allows to perform different tasks useful for diagnostics.
     34  */
     35 public class DiagnosticsFragment extends Fragment {
     36 
     37     private SimpleLogger logger;
     38     private TextView logTextView;
     39 
     40 
     41     private BroadcastReceiver logReceiver = new BroadcastReceiver() {
     42         @Override
     43         public void onReceive(Context context, Intent intent) {
     44             String msg = intent.getStringExtra("message");
     45             DiagnosticsFragment.this.appendLogText(msg);
     46         }
     47     };
     48 
     49     public DiagnosticsFragment() {
     50         // Required empty public constructor
     51     }
     52 
     53 
     54     @Override
     55     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     56                              Bundle savedInstanceState) {
     57         logger = SimpleLogger.getInstance(getContext());
     58         // Inflate the layout for this fragment
     59         final View view = inflater.inflate(R.layout.fragment_diagnostics, container, false);
     60         logTextView = (TextView) view.findViewById(R.id.txt_log_diag);
     61         return view;
     62     }
     63 
     64     @Override
     65     public void onResume() {
     66         super.onResume();
     67         logTextView.setMovementMethod(new ScrollingMovementMethod());
     68         logTextView.setText(logger.getLogText());
     69         logger.registerReceiver(logReceiver);
     70     }
     71 
     72     @Override
     73     public void onPause() {
     74         logger.unregisterReceiver(logReceiver);
     75         super.onPause();
     76     }
     77 
     78 
     79     public void appendLogText(String msg) {
     80         logTextView.append(msg + "\n");
     81     }
     82 }
     83