2 * Copyright 2019-2023 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.os.Build
25 import android.os.Bundle
26 import android.provider.OpenableColumns
27 import android.util.TypedValue
28 import android.view.Menu
29 import android.view.MenuItem
30 import android.view.WindowManager
31 import android.widget.TextView
32 import android.widget.ScrollView
34 import androidx.activity.result.contract.ActivityResultContracts
35 import androidx.appcompat.app.AppCompatActivity
36 import androidx.appcompat.widget.Toolbar
37 import androidx.preference.PreferenceManager
38 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
40 import com.google.android.material.snackbar.Snackbar
41 import com.stoutner.privacybrowser.BuildConfig
43 import com.stoutner.privacybrowser.R
45 import kotlinx.coroutines.CoroutineScope
46 import kotlinx.coroutines.Dispatchers
47 import kotlinx.coroutines.launch
48 import kotlinx.coroutines.withContext
50 import java.io.BufferedReader
51 import java.io.IOException
52 import java.io.InputStreamReader
53 import java.lang.Exception
54 import java.nio.charset.StandardCharsets
56 // Define the class constants.
57 private const val SCROLLVIEW_POSITION = "scrollview_position"
59 class LogcatActivity : AppCompatActivity() {
60 // Define the class variables.
61 private var scrollViewYPositionInt = 0
63 // Define the class views.
64 private lateinit var swipeRefreshLayout: SwipeRefreshLayout
65 private lateinit var logcatScrollView: ScrollView
66 private lateinit var logcatTextView: TextView
68 // Define the save logcat activity result launcher. It must be defined before `onCreate()` is run or the app will crash.
69 private val saveLogcatActivityResultLauncher = registerForActivityResult(ActivityResultContracts.CreateDocument("text/plain")) { fileUri ->
70 // Only save the file if the URI is not null, which happens if the user exited the file picker by pressing back.
71 if (fileUri != null) {
73 // Get the logcat string.
74 val logcatString = logcatTextView.text.toString()
76 // Open an output stream.
77 val outputStream = contentResolver.openOutputStream(fileUri)!!
79 // Save the logcat using a coroutine with Dispatchers.IO.
80 CoroutineScope(Dispatchers.Main).launch {
81 withContext(Dispatchers.IO) {
82 // Write the logcat string to the output stream.
83 outputStream.write(logcatString.toByteArray(StandardCharsets.UTF_8))
85 // Close the output stream.
90 // Initialize the file name string from the file URI last path segment.
91 var fileNameString = fileUri.lastPathSegment
93 // Query the exact file name if the API >= 26.
94 if (Build.VERSION.SDK_INT >= 26) {
95 // Get a cursor from the content resolver.
96 val contentResolverCursor = contentResolver.query(fileUri, null, null, null)!!
98 // Move to the fist row.
99 contentResolverCursor.moveToFirst()
101 // Get the file name from the cursor.
102 fileNameString = contentResolverCursor.getString(contentResolverCursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME))
105 contentResolverCursor.close()
108 // Display a snackbar with the saved logcat information.
109 Snackbar.make(logcatTextView, getString(R.string.saved, fileNameString), Snackbar.LENGTH_SHORT).show()
110 } catch (exception: Exception) {
111 // Display a snackbar with the error message.
112 Snackbar.make(logcatTextView, getString(R.string.error_saving_logcat, exception.toString()), Snackbar.LENGTH_INDEFINITE).show()
117 public override fun onCreate(savedInstanceState: Bundle?) {
118 // Get a handle for the shared preferences.
119 val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
121 // Get the preferences.
122 val allowScreenshots = sharedPreferences.getBoolean(getString(R.string.allow_screenshots_key), false)
123 val bottomAppBar = sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false)
125 // Disable screenshots if not allowed.
126 if (!allowScreenshots) {
127 window.addFlags(WindowManager.LayoutParams.FLAG_SECURE)
130 // Run the default commands.
131 super.onCreate(savedInstanceState)
133 // Set the content view.
135 setContentView(R.layout.logcat_bottom_appbar)
137 setContentView(R.layout.logcat_top_appbar)
140 // Get handles for the views.
141 val toolbar = findViewById<Toolbar>(R.id.toolbar)
142 swipeRefreshLayout = findViewById(R.id.swiperefreshlayout)
143 logcatScrollView = findViewById(R.id.scrollview)
144 logcatTextView = findViewById(R.id.logcat_textview)
146 // Set the toolbar as the action bar.
147 setSupportActionBar(toolbar)
149 // Get a handle for the action bar.
150 val actionBar = supportActionBar!!
152 // Display the back arrow in the action bar.
153 actionBar.setDisplayHomeAsUpEnabled(true)
155 // Implement swipe to refresh.
156 swipeRefreshLayout.setOnRefreshListener {
157 // Get the current logcat.
161 // Set the swipe refresh color scheme according to the theme.
162 swipeRefreshLayout.setColorSchemeResources(R.color.blue_text)
164 // Initialize a color background typed value.
165 val colorBackgroundTypedValue = TypedValue()
167 // Get the color background from the theme.
168 theme.resolveAttribute(android.R.attr.colorBackground, colorBackgroundTypedValue, true)
170 // Get the color background int from the typed value.
171 val colorBackgroundInt = colorBackgroundTypedValue.data
173 // Set the swipe refresh background color.
174 swipeRefreshLayout.setProgressBackgroundColorSchemeColor(colorBackgroundInt)
176 // Check to see if the activity has been restarted.
177 if (savedInstanceState != null) {
178 // Get the saved scrollview position.
179 scrollViewYPositionInt = savedInstanceState.getInt(SCROLLVIEW_POSITION)
186 override fun onCreateOptionsMenu(menu: Menu): Boolean {
187 // Inflate the menu. This adds items to the action bar.
188 menuInflater.inflate(R.menu.logcat_options_menu, menu)
194 override fun onOptionsItemSelected(menuItem: MenuItem): Boolean {
195 // Run the commands that correlate to the selected menu item.
196 return when (menuItem.itemId) {
197 R.id.copy -> { // Copy was selected.
198 // Get a handle for the clipboard manager.
199 val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
201 // Save the logcat in a clip data.
202 val logcatClipData = ClipData.newPlainText(getString(R.string.logcat), logcatTextView.text)
204 // Place the clip data on the clipboard.
205 clipboardManager.setPrimaryClip(logcatClipData)
207 // Display a snackbar.
208 Snackbar.make(logcatTextView, R.string.logcat_copied, Snackbar.LENGTH_SHORT).show()
210 // Consume the event.
214 R.id.save -> { // Save was selected.
215 // Open the file picker.
216 saveLogcatActivityResultLauncher.launch(getString(R.string.privacy_browser_logcat_txt, BuildConfig.VERSION_NAME))
218 // Consume the event.
222 R.id.clear -> { // Clear was selected.
224 // Clear the logcat. `-c` clears the logcat. `-b all` clears all the buffers (instead of just crash, main, and system).
225 val process = Runtime.getRuntime().exec("logcat -b all -c")
227 // Wait for the process to finish.
230 // Reset the scroll view Y position int.
231 scrollViewYPositionInt = 0
233 // Reload the logcat.
235 } catch (exception: Exception) {
239 // Consume the event.
243 else -> { // The home button was pushed.
244 // Do not consume the event. The system will process the home command.
245 super.onOptionsItemSelected(menuItem)
250 public override fun onSaveInstanceState(savedInstanceState: Bundle) {
251 // Run the default commands.
252 super.onSaveInstanceState(savedInstanceState)
254 // Get the scrollview Y position.
255 val scrollViewYPositionInt = logcatScrollView.scrollY
257 // Store the scrollview Y position in the bundle.
258 savedInstanceState.putInt(SCROLLVIEW_POSITION, scrollViewYPositionInt)
261 private fun getLogcat() {
263 // 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.
264 val getLogcatProcess = Runtime.getRuntime().exec("logcat -b all -v long -d")
266 // Wrap the logcat in a buffered reader.
267 val logcatBufferedReader = BufferedReader(InputStreamReader(getLogcatProcess.inputStream))
269 // Display the logcat.
270 logcatTextView.text = logcatBufferedReader.readText()
272 // Close the buffered reader.
273 logcatBufferedReader.close()
274 } catch (exception: IOException) {
278 // Update the scroll position after the text is populated.
279 logcatTextView.post {
280 // Set the scroll position.
281 logcatScrollView.scrollY = scrollViewYPositionInt
284 // Stop the swipe to refresh animation if it is displayed.
285 swipeRefreshLayout.isRefreshing = false