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 package com.android.multiwindowplayground.activities; 18 19 import com.android.multiwindowplayground.R; 20 import com.example.android.common.logger.Log; 21 import com.example.android.common.logger.LogFragment; 22 import com.example.android.common.logger.LogWrapper; 23 import com.example.android.common.logger.MessageOnlyLogFilter; 24 25 import android.content.res.Configuration; 26 import android.os.Bundle; 27 import android.os.PersistableBundle; 28 import android.support.annotation.ColorRes; 29 import android.support.annotation.StringRes; 30 import android.support.v7.app.AppCompatActivity; 31 import android.view.View; 32 import android.widget.TextView; 33 34 /** 35 * Activity that logs all key lifecycle callbacks to {@link Log}. 36 * Output is also logged to the UI into a {@link LogFragment} through {@link #initializeLogging()} 37 * and {@link #stopLogging()}. 38 */ 39 public abstract class LoggingActivity extends AppCompatActivity { 40 41 protected String mLogTag = getClass().getSimpleName(); 42 43 @Override onCreate(Bundle savedInstanceState)44 protected void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 Log.d(mLogTag, "onCreate"); 47 48 49 } 50 51 @Override onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState)52 public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) { 53 super.onPostCreate(savedInstanceState, persistentState); 54 Log.d(mLogTag, "onPostCreate"); 55 } 56 57 @Override onPause()58 protected void onPause() { 59 super.onPause(); 60 Log.d(mLogTag, "onPause"); 61 } 62 63 @Override onDestroy()64 protected void onDestroy() { 65 super.onDestroy(); 66 Log.d(mLogTag, "onDestroy"); 67 } 68 69 @Override onResume()70 protected void onResume() { 71 super.onResume(); 72 Log.d(mLogTag, "onResume"); 73 } 74 75 @Override onConfigurationChanged(Configuration newConfig)76 public void onConfigurationChanged(Configuration newConfig) { 77 super.onConfigurationChanged(newConfig); 78 Log.d(mLogTag, "onConfigurationChanged: " + newConfig.toString()); 79 } 80 81 @Override onPostCreate(Bundle savedInstanceState)82 protected void onPostCreate(Bundle savedInstanceState) { 83 super.onPostCreate(savedInstanceState); 84 Log.d(mLogTag, "onPostCreate"); 85 } 86 87 @Override onStart()88 protected void onStart() { 89 super.onStart(); 90 // Start logging to UI. 91 initializeLogging(); 92 93 Log.d(mLogTag, "onStart"); 94 } 95 96 @Override onStop()97 protected void onStop() { 98 super.onStop(); 99 // Stop logging to UI when this activity is stopped. 100 stopLogging(); 101 102 Log.d(mLogTag, "onStop"); 103 } 104 105 @Override onMultiWindowModeChanged(boolean isInMultiWindowMode)106 public void onMultiWindowModeChanged(boolean isInMultiWindowMode) { 107 super.onMultiWindowModeChanged(isInMultiWindowMode); 108 109 Log.d(mLogTag, "onMultiWindowModeChanged: " + isInMultiWindowMode); 110 } 111 112 // Logging and UI methods below. 113 114 /** Set up targets to receive log data */ initializeLogging()115 public void initializeLogging() { 116 // Using Log, front-end to the logging chain, emulates android.util.log method signatures. 117 // Wraps Android's native log framework 118 LogWrapper logWrapper = new LogWrapper(); 119 Log.setLogNode(logWrapper); 120 121 // Filter strips out everything except the message text. 122 MessageOnlyLogFilter msgFilter = new MessageOnlyLogFilter(); 123 logWrapper.setNext(msgFilter); 124 125 // On screen logging via a fragment with a TextView. 126 LogFragment logFragment = (LogFragment) getSupportFragmentManager() 127 .findFragmentById(R.id.log_fragment); 128 msgFilter.setNext(logFragment.getLogView()); 129 } 130 stopLogging()131 public void stopLogging() { 132 Log.setLogNode(null); 133 } 134 135 /** 136 * Set the description text if a TextView with the id <code>description</code> is available. 137 */ setDescription(@tringRes int textId)138 protected void setDescription(@StringRes int textId) { 139 // Set the text and background color 140 TextView description = (TextView) findViewById(R.id.description); 141 if (description != null) { 142 description.setText(textId); 143 } 144 } 145 146 /** 147 * Set the background color for the description text. 148 */ setBackgroundColor(@olorRes int colorId)149 protected void setBackgroundColor(@ColorRes int colorId) { 150 View scrollView = findViewById(R.id.scrollview); 151 if (scrollView != null) { 152 scrollView.setBackgroundResource(colorId); 153 } 154 } 155 156 } 157