- public void importExport(View view) {
- // Get a handle for the views.
- Spinner encryptionSpinner = findViewById(R.id.encryption_spinner);
- RadioButton importRadioButton = findViewById(R.id.import_radiobutton);
- RadioButton exportRadioButton = findViewById(R.id.export_radiobutton);
-
- // Check to see if the storage permission is needed.
- if ((encryptionSpinner.getSelectedItemPosition() == OPENPGP_ENCRYPTION) && exportRadioButton.isChecked()) { // Permission not needed to export via OpenKeychain.
- // Export the settings.
- exportSettings();
- } else if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { // The storage permission has been granted.
- // Check to see if import or export is selected.
- if (importRadioButton.isChecked()) { // Import is selected.
- // Import the settings.
- importSettings();
- } else { // Export is selected.
- // Export the settings.
- exportSettings();
- }
- } else { // The storage permission has not been granted.
- // Get a handle for the file name EditText.
- EditText fileNameEditText = findViewById(R.id.file_name_edittext);
-
- // Get the file name string.
- String fileNameString = fileNameEditText.getText().toString();
-
- // 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 (fileNameString.startsWith(externalPrivateDirectory)) { // The file path is in the external private directory.
- // Check to see if import or export is selected.
- if (importRadioButton.isChecked()) { // Import is selected.
- // Import the settings.
- importSettings();
- } else { // Export is selected.
- // Export the settings.
- exportSettings();
- }
- } 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.
- // Instantiate the storage permission alert dialog.
- DialogFragment storagePermissionDialogFragment = StoragePermissionDialog.displayDialog(0);
-
- // 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.
- // Request the storage permission. The export will be run when it finishes.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
- }
- }
- }
- }
-
- @Override
- public void onCloseStoragePermissionDialog(int type) {
- // Request the write external storage permission. The import/export will be run when it finishes.
- ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
- }
-
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- // Get a handle for the import radiobutton.
- RadioButton importRadioButton = findViewById(R.id.import_radiobutton);
-
- // Check to see if the storage permission was granted. If the dialog was canceled the grant results will be empty.
- if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) { // The storage permission was granted.
- // Run the import or export methods according to which radio button is selected.
- if (importRadioButton.isChecked()) { // Import is selected.
- // Import the settings.
- importSettings();
- } else { // Export is selected.
- // Export the settings.
- exportSettings();
- }
- } else { // The storage permission was not granted.
- // Display an error snackbar.
- Snackbar.make(importRadioButton, getString(R.string.cannot_use_location), Snackbar.LENGTH_LONG).show();
- }
- }
-