- Snackbar.make(logcatTextView, getString(R.string.save_failed) + " " + exception.toString(), Snackbar.LENGTH_INDEFINITE).show();
- }
- }
-
- // The activity result is called after browsing for a file in the save alert dialog.
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- // Don't do anything if the user pressed back from the file picker.
- if (resultCode == Activity.RESULT_OK) {
- // Get a handle for the save dialog fragment.
- DialogFragment saveDialogFragment = (DialogFragment) getSupportFragmentManager().findFragmentByTag(getString(R.string.save_logcat));
-
- // Remove the incorrect lint error that the save dialog fragment might be null.
- assert saveDialogFragment != null;
-
- // Get a handle for the save dialog.
- Dialog saveDialog = saveDialogFragment.getDialog();
-
- // Get a handle for the file name edit text.
- EditText fileNameEditText = saveDialog.findViewById(R.id.file_name_edittext);
-
- // Get the file name URI.
- Uri fileNameUri = data.getData();
-
- // Remove the incorrect lint warning that the file name URI might be null.
- assert fileNameUri != null;
-
- // Get the raw file name path.
- String rawFileNamePath = fileNameUri.getPath();
-
- // Remove the incorrect lint warning that the file name path might be null.
- assert rawFileNamePath != null;
-
- // Check to see if the file name Path includes a valid storage location.
- if (rawFileNamePath.contains(":")) { // The path is valid.
- // Split the path into the initial content uri and the final path information.
- String fileNameContentPath = rawFileNamePath.substring(0, rawFileNamePath.indexOf(":"));
- String fileNameFinalPath = rawFileNamePath.substring(rawFileNamePath.indexOf(":") + 1);
-
- // Create the file name path string.
- String fileNamePath;
-
- // Construct the file name path.
- switch (fileNameContentPath) {
- // The documents home has a special content path.
- case "/document/home":
- fileNamePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/" + fileNameFinalPath;
- break;
-
- // Everything else for the primary user should be in `/document/primary`.
- case "/document/primary":
- fileNamePath = Environment.getExternalStorageDirectory() + "/" + fileNameFinalPath;
- break;
-
- // Just in case, catch everything else and place it in the external storage directory.
- default:
- fileNamePath = Environment.getExternalStorageDirectory() + "/" + fileNameFinalPath;
- break;
- }
-
- // Set the file name path as the text of the file name edit text.
- fileNameEditText.setText(fileNamePath);
- } else { // The path is invalid.
- // Close the alert dialog.
- saveDialog.dismiss();
-
- // Get a handle for the logcat text view.
- TextView logcatTextView = findViewById(R.id.logcat_textview);
-
- // Display a snackbar with the error message.
- Snackbar.make(logcatTextView, rawFileNamePath + " " + getString(R.string.invalid_location), Snackbar.LENGTH_INDEFINITE).show();
- }
- }
- }
-
- // `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);