From 3d18fde9cf4934560d99f2f69477caa04153186b Mon Sep 17 00:00:00 2001 From: Jakob Nixdorf Date: Sat, 1 Jul 2017 15:11:09 +0200 Subject: [PATCH] Check if the external storage is accessible before writing --- .../shadowice/flocke/andotp/MainActivity.java | 12 +++-- .../flocke/andotp/StorageHelper.java | 46 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/org/shadowice/flocke/andotp/StorageHelper.java diff --git a/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java b/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java index 4d2d98a5..f9a7c932 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/MainActivity.java @@ -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() { diff --git a/app/src/main/java/org/shadowice/flocke/andotp/StorageHelper.java b/app/src/main/java/org/shadowice/flocke/andotp/StorageHelper.java new file mode 100644 index 00000000..d46fdb34 --- /dev/null +++ b/app/src/main/java/org/shadowice/flocke/andotp/StorageHelper.java @@ -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; + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d298b864..a94e7609 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -27,6 +27,8 @@ This file contains all your secret keys, please keep it safe! Export to external storage successful + Export to external storage failed + External storage currently not accessible An open-source two-factor authentication App for Android 4.3+