Check if the external storage is accessible before writing

This commit is contained in:
Jakob Nixdorf 2017-07-01 15:11:09 +02:00
parent afd96e6242
commit 3d18fde9cf
No known key found for this signature in database
GPG key ID: BE99BF86574A7DBC
3 changed files with 57 additions and 3 deletions

View file

@ -106,10 +106,16 @@ public class MainActivity extends AppCompatActivity {
}
private void doExportJSON() {
boolean success = SettingsHelper.exportAsJSON(this);
if (StorageHelper.isExternalStorageWritable()) {
boolean success = SettingsHelper.exportAsJSON(this);
if (success)
showSimpleSnackbar(R.string.msg_export_success);
if (success)
showSimpleSnackbar(R.string.msg_export_success);
else
showSimpleSnackbar(R.string.msg_export_failed);
} else {
showSimpleSnackbar(R.string.msg_storage_not_accessible);
}
}
private void exportJSON() {

View file

@ -0,0 +1,46 @@
/*
* Copyright (C) 2017 Jakob Nixdorf
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.shadowice.flocke.andotp;
import android.os.Environment;
public class StorageHelper {
/* Checks if external storage is available for read and write */
public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public static boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
}

View file

@ -27,6 +27,8 @@
This file contains all your secret keys, please keep it safe!
</string>
<string name="msg_export_success">Export to external storage successful</string>
<string name="msg_export_failed">Export to external storage failed</string>
<string name="msg_storage_not_accessible">External storage currently not accessible</string>
<!-- About dialog -->
<string name="about_description">An open-source two-factor authentication App for Android 4.3+</string>