2 * Copyright © 2020-2021 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser 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 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. If not, see <http://www.gnu.org/licenses/>.
20 package com.stoutner.privacybrowser.asynctasks;
22 import android.app.Activity;
23 import android.os.AsyncTask;
24 import android.widget.ScrollView;
25 import android.widget.TextView;
27 import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
29 import com.stoutner.privacybrowser.R;
31 import java.io.BufferedReader;
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34 import java.lang.ref.WeakReference;
36 // `Void` does not declare any parameters. `Void` does not declare progress units. `String` contains the results.
37 public class GetLogcat extends AsyncTask<Void, Void, String> {
38 // Define the class variables.
39 private final WeakReference<Activity> activityWeakReference;
40 private final int scrollViewYPositionInt;
42 // The public constructor.
43 public GetLogcat(Activity activity, int scrollViewYPositionInt) {
44 // Populate the weak reference to the calling activity.
45 activityWeakReference = new WeakReference<>(activity);
47 // Store the scrollview Y position.
48 this.scrollViewYPositionInt = scrollViewYPositionInt;
52 protected String doInBackground(Void... parameters) {
53 // Get a handle for the activity.
54 Activity activity = activityWeakReference.get();
56 // Abort if the activity is gone.
57 if ((activity == null) || activity.isFinishing()) {
61 // Create a log string builder.
62 StringBuilder logStringBuilder = new StringBuilder();
65 // 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.
66 Process process = Runtime.getRuntime().exec("logcat -b all -v long -d");
68 // Wrap the logcat in a buffered reader.
69 BufferedReader logBufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
71 // Create a log transfer string.
72 String logTransferString;
74 // Use the log transfer string to copy the logcat from the buffered reader to the string builder.
75 while ((logTransferString = logBufferedReader.readLine()) != null) {
77 logStringBuilder.append(logTransferString);
79 // Append a line break.
80 logStringBuilder.append("\n");
83 // Close the buffered reader.
84 logBufferedReader.close();
85 } catch (IOException exception) {
90 return logStringBuilder.toString();
93 // `onPostExecute()` operates on the UI thread.
95 protected void onPostExecute(String logcatString) {
96 // Get a handle for the activity.
97 Activity activity = activityWeakReference.get();
99 // Abort if the activity is gone.
100 if ((activity == null) || activity.isFinishing()) {
104 // Get handles for the views.
105 TextView logcatTextView = activity.findViewById(R.id.logcat_textview);
106 SwipeRefreshLayout swipeRefreshLayout = activity.findViewById(R.id.logcat_swiperefreshlayout);
107 ScrollView scrollView = activity.findViewById(R.id.logcat_scrollview);
109 // Display the logcat.
110 logcatTextView.setText(logcatString);
112 // Update the scroll position after the text is populated.
113 logcatTextView.post(() -> {
114 // Set the scroll position.
115 scrollView.setScrollY(scrollViewYPositionInt);
118 // Stop the swipe to refresh animation if it is displayed.
119 swipeRefreshLayout.setRefreshing(false);