1 /*
2  * Copyright (C) 2016 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 
18 package com.example.android.common.logger;
19 
20 import android.graphics.Typeface;
21 import android.os.Bundle;
22 import android.support.v4.app.Fragment;
23 import android.text.Editable;
24 import android.text.TextWatcher;
25 import android.view.Gravity;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.ScrollView;
30 
31 /**
32  * Simple fraggment which contains a LogView and uses is to output log data it receives
33  * through the LogNode interface.
34  */
35 public class LogFragment extends Fragment {
36 
37     private LogView mLogView;
38     private ScrollView mScrollView;
39 
LogFragment()40     public LogFragment() {
41     }
42 
inflateViews()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.setTextAppearance(android.R.style.TextAppearance_Material_Medium);
54         mLogView.setLayoutParams(logParams);
55         mLogView.setClickable(true);
56         mLogView.setFocusable(true);
57         mLogView.setTypeface(Typeface.create("monospace", Typeface.NORMAL));
58 
59         // Want to set padding as 16 dips, setPadding takes pixels.  Hooray math!
60         int paddingDips = 16;
61         double scale = getResources().getDisplayMetrics().density;
62         int paddingPixels = (int) ((paddingDips * (scale)) + .5);
63         mLogView.setPadding(paddingPixels, paddingPixels, paddingPixels, paddingPixels);
64         mLogView.setCompoundDrawablePadding(paddingPixels);
65 
66         mLogView.setGravity(Gravity.BOTTOM);
67 
68         mScrollView.addView(mLogView);
69         return mScrollView;
70     }
71 
72     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)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 
90                 mScrollView.post(new Runnable() {
91                     @Override
92                     public void run() {
93                         mScrollView
94                                 .smoothScrollTo(0, mScrollView.getBottom() + mLogView.getHeight());
95                     }
96                 });
97             }
98         });
99         return result;
100     }
101 
getLogView()102     public LogView getLogView() {
103         return mLogView;
104     }
105 }