+
+ // The activity result is called after browsing for a file in the save alert dialog.
+ @Override
+ public void onActivityResult(int requestCode, int resultCode, Intent returnedIntent) {
+ // Run the default commands.
+ super.onActivityResult(requestCode, resultCode, returnedIntent);
+
+ // Only do something if the user didn't press 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_dialog));
+
+ // Only update the file name if the dialog still exists.
+ if (saveDialogFragment != null) {
+ // Get a handle for the save dialog.
+ Dialog saveDialog = saveDialogFragment.getDialog();
+
+ // Remove the lint warning below that the dialog might be null.
+ assert saveDialog != null;
+
+ // Get a handle for the dialog view.
+ EditText fileNameEditText = saveDialog.findViewById(R.id.file_name_edittext);
+
+ // Get the file name URI from the intent.
+ Uri fileNameUri = returnedIntent.getData();
+
+ // Get the file name string from the URI.
+ String fileNameString = fileNameUri.toString();
+
+ // Set the file name text.
+ fileNameEditText.setText(fileNameString);
+
+ // Move the cursor to the end of the file name edit text.
+ fileNameEditText.setSelection(fileNameString.length());
+ }
+ }
+ }
+
+ @Override
+ public void onSave(int saveType, DialogFragment dialogFragment) {
+ // Get a handle for the dialog.
+ Dialog dialog = dialogFragment.getDialog();
+
+ // Remove the lint warning below that the dialog might be null.
+ assert dialog != null;
+
+ // Get a handle for the file name edit text.
+ EditText fileNameEditText = dialog.findViewById(R.id.file_name_edittext);
+
+ // Get the file name string.
+ String fileNameString = fileNameEditText.getText().toString();
+
+ // Get a handle for the about version linear layout.
+ LinearLayout aboutVersionLinearLayout = findViewById(R.id.about_version_linearlayout);
+
+ // Save the file according to the type.
+ switch (saveType) {
+ case SaveDialog.SAVE_ABOUT_VERSION_TEXT:
+ try {
+ // Get a handle for the about version fragment.
+ AboutVersionFragment aboutVersionFragment = (AboutVersionFragment) aboutPagerAdapter.getTabFragment(0);
+
+ // Get the about version text.
+ String aboutVersionString = aboutVersionFragment.getAboutVersionString();
+
+ // Create an input stream with the contents of about version.
+ InputStream aboutVersionInputStream = new ByteArrayInputStream(aboutVersionString.getBytes(StandardCharsets.UTF_8));
+
+ // Create an about version buffered reader.
+ BufferedReader aboutVersionBufferedReader = new BufferedReader(new InputStreamReader(aboutVersionInputStream));
+
+ // Open an output stream.
+ OutputStream outputStream = getContentResolver().openOutputStream(Uri.parse(fileNameString));
+
+ // Create a file buffered writer.
+ BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
+
+ // Create a transfer string.
+ String transferString;
+
+ // Use the transfer string to copy the about version text from the buffered reader to the buffered writer.
+ while ((transferString = aboutVersionBufferedReader.readLine()) != null) {
+ // Append the line to the buffered writer.
+ bufferedWriter.append(transferString);
+
+ // Append a line break.
+ bufferedWriter.append("\n");
+ }
+
+ // Flush the buffered writer.
+ bufferedWriter.flush();
+
+ // Close the inputs and outputs.
+ aboutVersionBufferedReader.close();
+ aboutVersionInputStream.close();
+ bufferedWriter.close();
+ outputStream.close();
+
+ // Display a snackbar with the saved about version information.
+ Snackbar.make(aboutVersionLinearLayout, getString(R.string.file_saved) + " " + fileNameString, Snackbar.LENGTH_SHORT).show();
+ } catch (Exception exception) {
+ // Display a snackbar with the error message.
+ Snackbar.make(aboutVersionLinearLayout, getString(R.string.error_saving_file) + " " + exception.toString(), Snackbar.LENGTH_INDEFINITE).show();
+ }
+ break;
+
+ case SaveDialog.SAVE_ABOUT_VERSION_IMAGE:
+ // Save the about version image.
+ new SaveAboutVersionImage(this, fileNameString, aboutVersionLinearLayout).execute();
+ break;
+ }
+ }