CardEmulation / src / com.example.android.common / logger /

LogFragment.java

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
 * Copyright 2013 The Android Open Source Project
18
 *
19
 * Licensed under the Apache License, Version 2.0 (the "License");
20
 * you may not use this file except in compliance with the License.
21
 * You may obtain a copy of the License at
22
 *
23
 *     http://www.apache.org/licenses/LICENSE-2.0
24
 *
25
 * Unless required by applicable law or agreed to in writing, software
26
 * distributed under the License is distributed on an "AS IS" BASIS,
27
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28
 * See the License for the specific language governing permissions and
29
 * limitations under the License.
30
 */
31
 
32
package com.example.android.common.logger;
33
 
34
import android.graphics.Typeface;
35
import android.os.Bundle;
36
import android.support.v4.app.Fragment;
37
import android.text.Editable;
38
import android.text.TextWatcher;
39
import android.view.Gravity;
40
import android.view.LayoutInflater;
41
import android.view.View;
42
import android.view.ViewGroup;
43
import android.widget.ScrollView;
44
 
45
/**
46
 * Simple fraggment which contains a LogView and uses is to output log data it receives
47
 * through the LogNode interface.
48
 */
49
public class LogFragment extends Fragment {
50
 
51
    private LogView mLogView;
52
    private ScrollView mScrollView;
53
 
54
    public LogFragment() {}
55
 
56
    public View inflateViews() {
57
        mScrollView = new ScrollView(getActivity());
58
        ViewGroup.LayoutParams scrollParams = new ViewGroup.LayoutParams(
59
                ViewGroup.LayoutParams.MATCH_PARENT,
60
                ViewGroup.LayoutParams.MATCH_PARENT);
61
        mScrollView.setLayoutParams(scrollParams);
62
 
63
        mLogView = new LogView(getActivity());
64
        ViewGroup.LayoutParams logParams = new ViewGroup.LayoutParams(scrollParams);
65
        logParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
66
        mLogView.setLayoutParams(logParams);
67
        mLogView.setClickable(true);
68
        mLogView.setFocusable(true);
69
        mLogView.setTypeface(Typeface.MONOSPACE);
70
 
71
        // Want to set padding as 16 dips, setPadding takes pixels.  Hooray math!
72
        int paddingDips = 16;
73
        double scale = getResources().getDisplayMetrics().density;
74
        int paddingPixels = (int) ((paddingDips * (scale)) + .5);
75
        mLogView.setPadding(paddingPixels, paddingPixels, paddingPixels, paddingPixels);
76
        mLogView.setCompoundDrawablePadding(paddingPixels);
77
 
78
        mLogView.setGravity(Gravity.BOTTOM);
79
        mLogView.setTextAppearance(getActivity(), android.R.style.TextAppearance_Holo_Medium);
80
 
81
        mScrollView.addView(mLogView);
82
        return mScrollView;
83
    }
84
 
85
    @Override
86
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
87
                             Bundle savedInstanceState) {
88
 
89
        View result = inflateViews();
90
 
91
        mLogView.addTextChangedListener(new TextWatcher() {
92
            @Override
93
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
94
 
95
            @Override
96
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
97
 
98
            @Override
99
            public void afterTextChanged(Editable s) {
100
                mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
101
            }
102
        });
103
        return result;
104
    }
105
 
106
    public LogView getLogView() {
107
        return mLogView;
108
    }
109
}