-
- // `Void` does not declare any parameters. `Void` does not declare progress units. `String` contains the results.
- private static class GetLogcat extends AsyncTask<Void, Void, String> {
- // Create a weak reference to the calling activity.
- private final WeakReference<Activity> activityWeakReference;
-
- // Populate the weak reference to the calling activity.
- GetLogcat(Activity activity) {
- activityWeakReference = new WeakReference<>(activity);
- }
-
- @Override
- protected String doInBackground(Void... parameters) {
- // Get a handle for the activity.
- Activity activity = activityWeakReference.get();
-
- // Abort if the activity is gone.
- if ((activity == null) || activity.isFinishing()) {
- return "";
- }
-
- // Create a log string builder.
- StringBuilder logStringBuilder = new StringBuilder();
-
- try {
- // 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.
- Process process = Runtime.getRuntime().exec("logcat -b all -v long -d");
-
- // Wrap the logcat in a buffered reader.
- BufferedReader logBufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
-
- // Create a log transfer string.
- String logTransferString;
-
- // Use the log transfer string to copy the logcat from the buffered reader to the string builder.
- while ((logTransferString = logBufferedReader.readLine()) != null) {
- // Append a line.
- logStringBuilder.append(logTransferString);
-
- // Append a line break.
- logStringBuilder.append("\n");
- }
-
- // Close the buffered reader.
- logBufferedReader.close();
- } catch (IOException exception) {
- // Do nothing.
- }
-
- // Return the logcat.
- return logStringBuilder.toString();
- }
-
- // `onPostExecute()` operates on the UI thread.
- @Override
- protected void onPostExecute(String logcatString) {
- // Get a handle for the activity.
- Activity activity = activityWeakReference.get();
-
- // Abort if the activity is gone.
- if ((activity == null) || activity.isFinishing()) {
- return;
- }
-
- // Get handles for the views.
- TextView logcatTextView = activity.findViewById(R.id.logcat_textview);
- SwipeRefreshLayout swipeRefreshLayout = activity.findViewById(R.id.logcat_swiperefreshlayout);
-
- // Display the logcat.
- logcatTextView.setText(logcatString);
-
- // Stop the swipe to refresh animation if it is displayed.
- swipeRefreshLayout.setRefreshing(false);
- }
- }