- @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 path string.
- filePathString = fileNameEditText.getText().toString();
-
- // Get a handle for the about version linear layout.
- aboutVersionLinearLayout = findViewById(R.id.about_version_linearlayout);
-
- // check to see if the storage permission is needed.
- if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // The storage permission has been granted.
- // Save the file according to the type.
- switch (saveType) {
- case SaveDialog.SAVE_ABOUT_VERSION_TEXT:
- // Save the about version text.
- saveAsText(filePathString);
- break;
-
- case SaveDialog.SAVE_ABOUT_VERSION_IMAGE:
- // Save the about version image.
- new SaveAboutVersionImage(this, this, filePathString, aboutVersionLinearLayout).execute();
- break;
- }
-
- // Reset the file path string.
- filePathString = "";
- } else { // The storage permission has not been granted.
- // Get the external private directory file.
- File externalPrivateDirectoryFile = getExternalFilesDir(null);
-
- // Remove the incorrect lint error below that the file might be null.
- assert externalPrivateDirectoryFile != null;
-
- // Get the external private directory string.
- String externalPrivateDirectory = externalPrivateDirectoryFile.toString();
-
- // Check to see if the file path is in the external private directory.
- if (filePathString.startsWith(externalPrivateDirectory)) { // The file path is in the external private directory.
- // Save the webpage according to the type.
- switch (saveType) {
- case SaveDialog.SAVE_ABOUT_VERSION_TEXT:
- // Save the about version text.
- saveAsText(filePathString);
- break;
-
- case SaveDialog.SAVE_ABOUT_VERSION_IMAGE:
- // Save the about version image.
- new SaveAboutVersionImage(this, this, filePathString, aboutVersionLinearLayout).execute();
- break;
- }
-
- // Reset the file path string.
- filePathString = "";
- } else { // The file path is in a public directory.
- // Check if the user has previously denied the storage permission.
- if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { // Show a dialog explaining the request first.
- // Declare a storage permission dialog fragment.
- DialogFragment storagePermissionDialogFragment;
-
- // Instantiate the storage permission alert dialog according to the type.
- if (saveType == SaveDialog.SAVE_ABOUT_VERSION_TEXT) {
- storagePermissionDialogFragment = StoragePermissionDialog.displayDialog(StoragePermissionDialog.SAVE_TEXT);
- } else {
- storagePermissionDialogFragment = StoragePermissionDialog.displayDialog(StoragePermissionDialog.SAVE_IMAGE);
- }
-
- // Show the storage permission alert dialog. The permission will be requested when the dialog is closed.
- storagePermissionDialogFragment.show(getSupportFragmentManager(), getString(R.string.storage_permission));
- } else { // Show the permission request directly.
- switch (saveType) {
- case SaveDialog.SAVE_ABOUT_VERSION_TEXT:
- // Request the write external storage permission. The text will be saved when it finishes.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, StoragePermissionDialog.SAVE_TEXT);
- break;
-
- case SaveDialog.SAVE_ABOUT_VERSION_IMAGE:
- // Request the write external storage permission. The image will be saved when it finishes.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, StoragePermissionDialog.SAVE_IMAGE);
- break;
- }
-
- }
- }
- }
- }
-
- @Override
- public void onCloseStoragePermissionDialog(int requestType) {
- // Request the write external storage permission according to the request type. About version will be saved when it finishes.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestType);
- }
-
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- //Only process the results if they exist (this method is triggered when a dialog is presented the first time for an app, but no grant results are included).
- if (grantResults.length > 0) {
- // Check to see if the storage permission was granted. If the dialog was canceled the grant results will be empty.
- if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // The storage permission was granted.
- switch (requestCode) {
- case StoragePermissionDialog.SAVE_TEXT:
- // Save the about version text.
- saveAsText(filePathString);
- break;
-
- case StoragePermissionDialog.SAVE_IMAGE:
- // Save the about version image.
- new SaveAboutVersionImage(this, this, filePathString, aboutVersionLinearLayout).execute();
- break;
- }
- } else{ // the storage permission was not granted.
- // Display an error snackbar.
- Snackbar.make(aboutVersionLinearLayout, getString(R.string.cannot_use_location), Snackbar.LENGTH_LONG).show();
- }
-
- // Reset the file path string.
- filePathString = "";
- }
- }
-