Home | History | Annotate | Download | only in testingcamera2
      1 /*
      2  * Copyright (C) 2014 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.android.testingcamera2;
     18 
     19 import java.util.Date;
     20 import java.text.SimpleDateFormat;
     21 
     22 import android.annotation.SuppressLint;
     23 import android.content.Context;
     24 import android.graphics.Color;
     25 import android.os.Handler;
     26 import android.os.Looper;
     27 import android.text.Spannable;
     28 import android.text.SpannableString;
     29 import android.text.method.ScrollingMovementMethod;
     30 import android.text.style.ForegroundColorSpan;
     31 import android.util.AttributeSet;
     32 import android.util.TypedValue;
     33 import android.view.Gravity;
     34 import android.view.View;
     35 import android.view.MotionEvent;
     36 import android.widget.LinearLayout;
     37 import android.widget.TextView;
     38 
     39 public class LogPane extends LinearLayout implements TLog.Logger {
     40 
     41     private final TextView mLogTextView;
     42     private final Handler mHandler;
     43 
     44     @SuppressLint("SimpleDateFormat")
     45     private SimpleDateFormat mDateFormatter = new SimpleDateFormat("HH:mm:ss.SSS : ");
     46 
     47     public LogPane(Context context, AttributeSet attrs) {
     48         super(context, attrs);
     49 
     50         mHandler = new Handler(Looper.getMainLooper());
     51 
     52         this.setOrientation(VERTICAL);
     53 
     54         TextView titleText = new TextView(context);
     55         titleText.setText(R.string.log_pane_label);
     56         titleText.setTextSize(TypedValue.COMPLEX_UNIT_PX,
     57                 context.getResources().getDimension(R.dimen.pane_title_text));
     58         LinearLayout.LayoutParams titleTextLayoutParams =
     59                 new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
     60 
     61         this.addView(titleText, titleTextLayoutParams);
     62 
     63         mLogTextView = new TextView(context);
     64         mLogTextView.setFreezesText(true);
     65         LinearLayout.LayoutParams logTextLayoutParams =
     66                 new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
     67         logTextLayoutParams.leftMargin = 40;
     68         mLogTextView.setLayoutParams(logTextLayoutParams);
     69         mLogTextView.setLines(10);
     70         mLogTextView.setMovementMethod(new ScrollingMovementMethod());
     71         mLogTextView.setOnTouchListener(new View.OnTouchListener() {
     72             @Override
     73             public boolean onTouch(View view, MotionEvent event) {
     74                 // Allow scrolling of text while inside a scroll view
     75                 view.getParent().requestDisallowInterceptTouchEvent(true);
     76                 return false;
     77             }
     78         });
     79         mLogTextView.setGravity(Gravity.BOTTOM);
     80 
     81         this.addView(mLogTextView);
     82     }
     83 
     84     @Override
     85     public void addToLog(final String text, boolean error) {
     86         StringBuffer logEntry = new StringBuffer(32);
     87         logEntry.append("\n").append(mDateFormatter.format(new Date()));
     88         logEntry.append(text);
     89         final Spannable logLine = new SpannableString(logEntry.toString());
     90 
     91         int lineColor = error ? Color.RED : Color.WHITE;
     92         logLine.setSpan(new ForegroundColorSpan(lineColor), 0, logLine.length(),
     93                 Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
     94 
     95         mHandler.post(new Runnable() {
     96             public void run() {
     97                 mLogTextView.append(logLine);
     98             }
     99         });
    100     }
    101 }
    102