1 /* 2 * Copyright (C) 2017 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.multiwindowplayground.logger 18 19 import android.graphics.Typeface 20 import android.os.Bundle 21 import android.support.v4.app.Fragment 22 import android.support.v4.app.FragmentActivity 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.view.ViewGroup.LayoutParams.MATCH_PARENT 30 import android.view.ViewGroup.LayoutParams.WRAP_CONTENT 31 import android.widget.ScrollView 32 33 /** 34 * Simple fragment which contains a [LogView] and is used to output log data it receives through the 35 * [LogNode] interface. 36 */ 37 class LogFragment : Fragment() { 38 39 internal lateinit var logView: LogView 40 private lateinit var scrollView: ScrollView 41 42 private fun inflateViews(): View { 43 val scrollParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT) 44 val logParams = ViewGroup.LayoutParams(scrollParams).apply { 45 height = WRAP_CONTENT 46 } 47 48 // Want to set padding as 16 dips, setPadding takes pixels. Hooray math! 49 val paddingDips = 16 50 val scale = resources.displayMetrics.density.toDouble() 51 val paddingPixels = (paddingDips * scale + .5).toInt() 52 53 logView = LogView(activity as FragmentActivity).apply { 54 setTextAppearance(android.R.style.TextAppearance_Material_Medium) 55 layoutParams = logParams 56 isClickable = true 57 isFocusable = true 58 typeface = Typeface.create("monospace", Typeface.NORMAL) 59 setPadding(paddingPixels, paddingPixels, paddingPixels, paddingPixels) 60 compoundDrawablePadding = paddingPixels 61 gravity = Gravity.BOTTOM 62 } 63 64 scrollView = ScrollView(activity).apply { 65 layoutParams = scrollParams 66 addView(logView) 67 } 68 return scrollView 69 } 70 71 override fun onCreateView( 72 inflater: LayoutInflater, 73 container: ViewGroup?, 74 savedInstanceState: Bundle? 75 ): View? { 76 val result = inflateViews() 77 78 logView.addTextChangedListener(object : TextWatcher { 79 override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} 80 81 override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} 82 83 override fun afterTextChanged(s: Editable) { 84 scrollView.run { post { smoothScrollTo(0, scrollView.bottom + logView.height) }} 85 } 86 }) 87 return result 88 } 89 90 }