2 * Copyright © 2021 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Cell <https://www.stoutner.com/privacy-cell>.
6 * Privacy Cell 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 Cell 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 Cell. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacycell.activities
22 import android.content.ClipData
23 import android.content.ClipboardManager
24 import android.net.Uri
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.widget.ScrollView
31 import android.widget.TextView
33 import androidx.activity.result.contract.ActivityResultContracts
34 import androidx.appcompat.app.AppCompatActivity
35 import androidx.appcompat.widget.Toolbar
36 import androidx.preference.PreferenceManager
37 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
39 import com.google.android.material.snackbar.Snackbar
41 import com.stoutner.privacycell.R
43 import java.io.BufferedReader
44 import java.io.IOException
45 import java.io.InputStreamReader
46 import java.lang.Exception
47 import java.nio.charset.StandardCharsets
49 // Define the class constants.
50 private const val SCROLLVIEW_POSITION = "scrollview_position"
52 class LogcatActivity : AppCompatActivity() {
53 // Define the class variables.
54 private var scrollViewYPositionInt = 0
56 // Define the class views.
57 private lateinit var swipeRefreshLayout: SwipeRefreshLayout
58 private lateinit var logcatScrollView: ScrollView
59 private lateinit var logcatTextView: TextView
61 // Define the save logcat activity result launcher.
62 private val saveLogcatActivityResultLauncher = registerForActivityResult(ActivityResultContracts.CreateDocument()) { fileNameUri: Uri? ->
63 // Only save the file if the URI is not null, which happens if the user exited the file picker by pressing back.
64 if (fileNameUri != null) {
66 // Get the logcat as a string.
67 val logcatString = logcatTextView.text.toString()
69 // Open an output stream.
70 val outputStream = contentResolver.openOutputStream(fileNameUri)!!
72 // Write the logcat string to the output stream.
73 outputStream.write(logcatString.toByteArray(StandardCharsets.UTF_8))
75 // Close the output stream.
78 // Get a cursor from the content resolver.
79 val contentResolverCursor = contentResolver.query(fileNameUri, null, null, null)!!
81 // Move to the first row.
82 contentResolverCursor.moveToFirst()
84 // Get the file name from the cursor.
85 val fileNameString = contentResolverCursor.getString(contentResolverCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
88 contentResolverCursor.close()
90 // Display a snackbar with the saved logcat information.
91 Snackbar.make(logcatTextView, getString(R.string.logcat_saved, fileNameString), Snackbar.LENGTH_SHORT).show()
92 } catch (exception: Exception) {
93 // Display a snackbar with the error message.
94 Snackbar.make(logcatTextView, getString(R.string.error_saving_logcat, exception.toString()), Snackbar.LENGTH_INDEFINITE).show()
99 public override fun onCreate(savedInstanceState: Bundle?) {
100 // Run the default commands.
101 super.onCreate(savedInstanceState)
103 // Get a handle for the shared preferences.
104 val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
106 // Get the bottom app bar preference.
107 val bottomAppBar = sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false)
109 // Set the content view.
111 setContentView(R.layout.logcat_bottom_appbar)
113 setContentView(R.layout.logcat_top_appbar)
116 // Get handles for the views.
117 val toolbar = findViewById<Toolbar>(R.id.toolbar)
118 swipeRefreshLayout = findViewById(R.id.swiperefreshlayout)
119 logcatScrollView = findViewById(R.id.scrollview)
120 logcatTextView = findViewById(R.id.logcat_textview)
122 // Set the toolbar as the action bar.
123 setSupportActionBar(toolbar)
125 // Get a handle for the action bar.
126 val actionBar = supportActionBar!!
128 // Display the back arrow in the action bar.
129 actionBar.setDisplayHomeAsUpEnabled(true)
131 // Implement swipe to refresh.
132 swipeRefreshLayout.setOnRefreshListener {
133 // Get the current logcat.
137 // Set the swipe refresh color scheme according to the theme.
138 swipeRefreshLayout.setColorSchemeResources(R.color.blue_text)
140 // Initialize a color background typed value.
141 val colorBackgroundTypedValue = TypedValue()
143 // Get the color background from the theme.
144 theme.resolveAttribute(android.R.attr.colorBackground, colorBackgroundTypedValue, true)
146 // Get the color background int from the typed value.
147 val colorBackgroundInt = colorBackgroundTypedValue.data
149 // Set the swipe refresh background color.
150 swipeRefreshLayout.setProgressBackgroundColorSchemeColor(colorBackgroundInt)
152 // Check to see if the activity has been restarted.
153 if (savedInstanceState != null) {
154 // Get the saved scrollview position.
155 scrollViewYPositionInt = savedInstanceState.getInt(SCROLLVIEW_POSITION)
162 override fun onCreateOptionsMenu(menu: Menu): Boolean {
163 // Inflate the menu. This adds items to the action bar.
164 menuInflater.inflate(R.menu.logcat_options_menu, menu)
170 override fun onOptionsItemSelected(menuItem: MenuItem): Boolean {
171 // Run the commands that correlate to the selected menu item.
172 return when (menuItem.itemId) {
173 R.id.copy -> { // Copy was selected.
174 // Get a handle for the clipboard manager.
175 val clipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
177 // Save the logcat in a clip data.
178 val logcatClipData = ClipData.newPlainText(getString(R.string.logcat), logcatTextView.text)
180 // Place the clip data on the clipboard.
181 clipboardManager.setPrimaryClip(logcatClipData)
183 // Display a snackbar.
184 Snackbar.make(logcatTextView, R.string.logcat_copied, Snackbar.LENGTH_SHORT).show()
186 // Consume the event.
190 R.id.save -> { // Save was selected.
191 // Open the file picker.
192 saveLogcatActivityResultLauncher.launch(getString(R.string.privacy_cell_logcat_txt))
194 // Consume the event.
198 R.id.clear -> { // Clear was selected.
200 // Clear the logcat. `-c` clears the logcat. `-b all` clears all the buffers (instead of just crash, main, and system).
201 val process = Runtime.getRuntime().exec("logcat -b all -c")
203 // Wait for the process to finish.
206 // Reset the scroll view Y position int.
207 scrollViewYPositionInt = 0
209 // Reload the logcat.
211 } catch (exception: Exception) {
215 // Consume the event.
219 else -> { // The home button was pushed.
220 // Do not consume the event. The system will process the home command.
221 super.onOptionsItemSelected(menuItem)
226 public override fun onSaveInstanceState(savedInstanceState: Bundle) {
227 // Run the default commands.
228 super.onSaveInstanceState(savedInstanceState)
230 // Get the scrollview Y position.
231 val scrollViewYPositionInt = logcatScrollView.scrollY
233 // Store the scrollview Y position in the bundle.
234 savedInstanceState.putInt(SCROLLVIEW_POSITION, scrollViewYPositionInt)
237 private fun getLogcat() {
239 // 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.
240 val getLogcatProcess = Runtime.getRuntime().exec("logcat -b all -v long -d")
242 // Wrap the logcat in a buffered reader.
243 val logcatBufferedReader = BufferedReader(InputStreamReader(getLogcatProcess.inputStream))
245 // Display the logcat.
246 logcatTextView.text = logcatBufferedReader.readText()
248 // Close the buffered reader.
249 logcatBufferedReader.close()
250 } catch (exception: IOException) {
254 // Update the scroll position after the text is populated.
255 logcatTextView.post {
256 // Set the scroll position.
257 logcatScrollView.scrollY = scrollViewYPositionInt
260 // Stop the swipe to refresh animation if it is displayed.
261 swipeRefreshLayout.isRefreshing = false