Home | History | Annotate | Download | only in logger
      1 /*
      2 * Copyright 2013 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.example.android.common.logger;
     18 
     19 import android.graphics.Typeface;
     20 import android.os.Bundle;
     21 import android.support.v4.app.Fragment;
     22 import android.text.Editable;
     23 import android.text.TextWatcher;
     24 import android.view.Gravity;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 import android.widget.ScrollView;
     29 
     30 /**
     31  * Simple fragment which contains a LogView and uses is to output log data it receives
     32  * through the LogNode interface.
     33  */
     34 public class LogFragment extends Fragment {
     35 
     36     private LogView mLogView;
     37 
     38     private ScrollView mScrollView;
     39 
     40     public LogFragment() {
     41     }
     42 
     43     public View inflateViews() {
     44         mScrollView = new ScrollView(getActivity());
     45         ViewGroup.LayoutParams scrollParams = new ViewGroup.LayoutParams(
     46                 ViewGroup.LayoutParams.MATCH_PARENT,
     47                 ViewGroup.LayoutParams.MATCH_PARENT);
     48         mScrollView.setLayoutParams(scrollParams);
     49 
     50         mLogView = new LogView(getActivity());
     51         ViewGroup.LayoutParams logParams = new ViewGroup.LayoutParams(scrollParams);
     52         logParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
     53         mLogView.setLayoutParams(logParams);
     54         mLogView.setClickable(true);
     55         mLogView.setFocusable(true);
     56         mLogView.setTypeface(Typeface.MONOSPACE);
     57 
     58         // Want to set padding as 16 dips, setPadding takes pixels.  Hooray math!
     59         int paddingDips = 16;
     60         double scale = getResources().getDisplayMetrics().density;
     61         int paddingPixels = (int) ((paddingDips * (scale)) + .5);
     62         mLogView.setPadding(paddingPixels, paddingPixels, paddingPixels, paddingPixels);
     63         mLogView.setCompoundDrawablePadding(paddingPixels);
     64 
     65         mLogView.setGravity(Gravity.BOTTOM);
     66         mLogView.setTextAppearance(getActivity(), android.R.style.TextAppearance_Holo_Medium);
     67 
     68         mScrollView.addView(mLogView);
     69         return mScrollView;
     70     }
     71 
     72     @Override
     73     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     74             Bundle savedInstanceState) {
     75 
     76         View result = inflateViews();
     77 
     78         mLogView.addTextChangedListener(new TextWatcher() {
     79             @Override
     80             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
     81             }
     82 
     83             @Override
     84             public void onTextChanged(CharSequence s, int start, int before, int count) {
     85             }
     86 
     87             @Override
     88             public void afterTextChanged(Editable s) {
     89                 mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
     90             }
     91         });
     92         return result;
     93     }
     94 
     95     public LogView getLogView() {
     96         return mLogView;
     97     }
     98 }