2 * Copyright © 2019-2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
6 * Privacy Browser Android is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * Privacy Browser Android is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Privacy Browser Android. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.activities
22 import android.content.ClipData
23 import android.content.ClipboardManager
24 import android.net.Uri
25 import android.os.Build
26 import android.os.Bundle
27 import android.provider.OpenableColumns
28 import android.util.TypedValue
29 import android.view.Menu
30 import android.view.MenuItem
31 import android.view.WindowManager
32 import android.widget.TextView
33 import android.widget.ScrollView
35 import androidx.activity.result.contract.ActivityResultContracts
36 import androidx.appcompat.app.AppCompatActivity
37 import androidx.appcompat.widget.Toolbar
38 import androidx.preference.PreferenceManager
39 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
41 import com.google.android.material.snackbar.Snackbar
42 import com.stoutner.privacybrowser.BuildConfig
44 import com.stoutner.privacybrowser.R
46 import java.io.BufferedReader
47 import java.io.IOException
48 import java.io.InputStreamReader
49 import java.lang.Exception
50 import java.nio.charset.StandardCharsets
52 // Define the class constants.
53 private const val SCROLLVIEW_POSITION = "scrollview_position"
55 class LogcatActivity : AppCompatActivity() {
56 // Define the class variables.
57 private var scrollViewYPositionInt = 0
59 // Define the class views.
60 private lateinit var swipeRefreshLayout: SwipeRefreshLayout
61 private lateinit var logcatScrollView: ScrollView
62 private lateinit var logcatTextView: TextView
64 // Define the save logcat activity result launcher. It must be defined before `onCreate()` is run or the app will crash.
65 private val saveLogcatActivityResultLauncher = registerForActivityResult(ActivityResultContracts.CreateDocument()) { fileUri: Uri? ->
66 // Only save the file if the URI is not null, which happens if the user exited the file picker by pressing back.
67 if (fileUri != null) {
69 // Get the logcat string.
70 val logcatString = logcatTextView.text.toString()
72 // Open an output stream.
73 val outputStream = contentResolver.openOutputStream(fileUri)!!
75 // Write the logcat string to the output stream.
76 outputStream.write(logcatString.toByteArray(StandardCharsets.UTF_8))
78 // Close the output stream.
81 // Initialize the file name string from the file URI last path segment.
82 var fileNameString = fileUri.lastPathSegment
84 // Query the exact file name if the API >= 26.
85 if (Build.VERSION.SDK_INT >= 26) {
86 // Get a cursor from the content resolver.
87 val contentResolverCursor = contentResolver.query(fileUri, null, null, null)!!
89 // Move to the fist row.
90 contentResolverCursor.moveToFirst()
92 // Get the file name from the cursor.
93 fileNameString = contentResolverCursor.getString(contentResolverCursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME))
96 contentResolverCursor.close()
99 // Display a snackbar with the saved logcat information.
100 Snackbar.make(logcatTextView, getString(R.string.saved, fileNameString), Snackbar.LENGTH_SHORT).show()
101 } catch (exception: Exception) {
102 // Display a snackbar with the error message.
103 Snackbar.make(logcatTextView, getString(R.string.error_saving_logcat, exception.toString()), Snackbar.LENGTH_INDEFINITE).show()
108 public override fun onCreate(savedInstanceState: Bundle?) {
109 // Get a handle for the shared preferences.
110 val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
112 // Get the preferences.
113 val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
114 val bottomAppBar = sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false)
116 // Disable screenshots if not allowed.
117 if (!allowScreenshots) {
118 window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
122 setTheme(R.style.PrivacyBrowser)
124 // Run the default commands.
125 super.onCreate(savedInstanceState)
127 // Set the content view.
129 setContentView(R.layout.logcat_bottom_appbar)
131 setContentView(R.layout.logcat_top_appbar)
134 // Get handles for the views.
135 val toolbar = findViewById<Toolbar>(R.id.toolbar)
136 swipeRefreshLayout = findViewById(R.id.swiperefreshlayout)
137 logcatScrollView = findViewById(R.id.scrollview)
138 logcatTextView = findViewById(R.id.logcat_textview)
140 // Set the toolbar as the action bar.
141 setSupportActionBar(toolbar)
143 // Get a handle for the action bar.
144 val actionBar = supportActionBar!!
146 // Display the back arrow in the action bar.
147 actionBar.setDisplayHomeAsUpEnabled(true)
149 // Implement swipe to refresh.
150 swipeRefreshLayout.setOnRefreshListener {
151 // Get the current logcat.
155 // Set the swipe refresh color scheme according to the theme.
156 swipeRefreshLayout.setColorSchemeResources(R.color.blue_text)
158 // Initialize a color background typed value.
159 val colorBackgroundTypedValue = TypedValue()
161 // Get the color background from the theme.
162 theme.resolveAttribute(android.R.attr.colorBackground, colorBackgroundTypedValue, true)
164 // Get the color background int from the typed value.
165 val colorBackgroundInt = colorBackgroundTypedValue.data
167 // Set the swipe refresh background color.
168 swipeRefreshLayout.setProgressBackgroundColorSchemeColor(colorBackgroundInt)
170 // Check to see if the activity has been restarted.
171 if (savedInstanceState != null) {
172 // Get the saved scrollview position.
173 scrollViewYPositionInt = savedInstanceState.getInt(SCROLLVIEW_POSITION)
180 override fun onCreateOptionsMenu(menu: Menu): Boolean {
181 // Inflate the menu. This adds items to the action bar.
182 menuInflater.inflate(R.menu.logcat_options_menu, menu)
188 override fun onOptionsItemSelected(menuItem: MenuItem): Boolean {
189 // Run the commands that correlate to the selected menu item.
190 return when (menuItem.itemId) {
191 R.id.copy -> { // Copy was selected.
192 // Get a handle for the clipboard manager.
193 val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
195 // Save the logcat in a clip data.
196 val logcatClipData = ClipData.newPlainText(getString(R.string.logcat), logcatTextView.text)
198 // Place the clip data on the clipboard.
199 clipboardManager.setPrimaryClip(logcatClipData)
201 // Display a snackbar.
202 Snackbar.make(logcatTextView, R.string.logcat_copied, Snackbar.LENGTH_SHORT).show()
204 // Consume the event.
208 R.id.save -> { // Save was selected.
209 // Open the file picker.
210 saveLogcatActivityResultLauncher.launch(getString(R.string.privacy_browser_logcat_txt, BuildConfig.VERSION_NAME))
212 // Consume the event.
216 R.id.clear -> { // Clear was selected.
218 // Clear the logcat. `-c` clears the logcat. `-b all` clears all the buffers (instead of just crash, main, and system).
219 val process = Runtime.getRuntime().exec("logcat -b all -c")
221 // Wait for the process to finish.
224 // Reset the scroll view Y position int.
225 scrollViewYPositionInt = 0
227 // Reload the logcat.
229 } catch (exception: Exception) {
233 // Consume the event.
237 else -> { // The home button was pushed.
238 // Do not consume the event. The system will process the home command.
239 super.onOptionsItemSelected(menuItem)
244 public override fun onSaveInstanceState(savedInstanceState: Bundle) {
245 // Run the default commands.
246 super.onSaveInstanceState(savedInstanceState)
248 // Get the scrollview Y position.
249 val scrollViewYPositionInt = logcatScrollView.scrollY
251 // Store the scrollview Y position in the bundle.
252 savedInstanceState.putInt(SCROLLVIEW_POSITION, scrollViewYPositionInt)
255 private fun getLogcat() {
257 // Get the logcat. `-b all` gets all the buffers (instead of just crash, main, and system). `-v long` produces more complete information. `-d` dumps the logcat and exits.
258 val getLogcatProcess = Runtime.getRuntime().exec("logcat -b all -v long -d")
260 // Wrap the logcat in a buffered reader.
261 val logcatBufferedReader = BufferedReader(InputStreamReader(getLogcatProcess.inputStream))
263 // Display the logcat.
264 logcatTextView.text = logcatBufferedReader.readText()
266 // Close the buffered reader.
267 logcatBufferedReader.close()
268 } catch (exception: IOException) {
272 // Update the scroll position after the text is populated.
273 logcatTextView.post {
274 // Set the scroll position.
275 logcatScrollView.scrollY = scrollViewYPositionInt
278 // Stop the swipe to refresh animation if it is displayed.
279 swipeRefreshLayout.isRefreshing = false