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     private ScrollView mScrollView;
     38 
     39     public LogFragment() {}
     40 
     41     public View inflateViews() {
     42         mScrollView = new ScrollView(getActivity());
     43         ViewGroup.LayoutParams scrollParams = new ViewGroup.LayoutParams(
     44                 ViewGroup.LayoutParams.MATCH_PARENT,
     45                 ViewGroup.LayoutParams.MATCH_PARENT);
     46         mScrollView.setLayoutParams(scrollParams);
     47 
     48         mLogView = new LogView(getActivity());
     49         ViewGroup.LayoutParams logParams = new ViewGroup.LayoutParams(scrollParams);
     50         logParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
     51         mLogView.setLayoutParams(logParams);
     52         mLogView.setClickable(true);
     53         mLogView.setFocusable(true);
     54         mLogView.setTypeface(Typeface.MONOSPACE);
     55 
     56         // Want to set padding as 16 dips, setPadding takes pixels.  Hooray math!
     57         int paddingDips = 16;
     58         double scale = getResources().getDisplayMetrics().density;
     59         int paddingPixels = (int) ((paddingDips * (scale)) + .5);
     60         mLogView.setPadding(paddingPixels, paddingPixels, paddingPixels, paddingPixels);
     61         mLogView.setCompoundDrawablePadding(paddingPixels);
     62 
     63         mLogView.setGravity(Gravity.BOTTOM);
     64         mLogView.setTextAppearance(getActivity(), android.R.style.TextAppearance_Holo_Medium);
     65 
     66         mScrollView.addView(mLogView);
     67         return mScrollView;
     68     }
     69 
     70     @Override
     71     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     72                              Bundle savedInstanceState) {
     73 
     74         View result = inflateViews();
     75 
     76         mLogView.addTextChangedListener(new TextWatcher() {
     77             @Override
     78             public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
     79 
     80             @Override
     81             public void onTextChanged(CharSequence s, int start, int before, int count) {}
     82 
     83             @Override
     84             public void afterTextChanged(Editable s) {
     85                 mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
     86             }
     87         });
     88         return result;
     89     }
     90 
     91     public LogView getLogView() {
     92         return mLogView;
     93     }
     94 }