From 562c0546c6e89f0720ab8ec88194be4357afb134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 19 Nov 2015 18:26:06 +0100 Subject: Handle denied storage permission, use ContentResolver.SCHEME_FILE constant --- .../keychain/ui/DecryptListFragment.java | 7 ++++--- .../keychain/ui/EncryptFilesFragment.java | 9 ++++++--- .../sufficientlysecure/keychain/util/FileHelper.java | 4 ++-- .../keychain/util/FileHelperLollipop.java | 17 +++++++++++++++++ OpenKeychain/src/main/res/values/strings.xml | 1 + 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java index b60b06df5..922dd7307 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java @@ -29,6 +29,7 @@ import android.Manifest; import android.annotation.TargetApi; import android.app.Activity; import android.content.ClipDescription; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.pm.LabeledIntent; @@ -635,7 +636,7 @@ public class DecryptListFragment * see https://commonsware.com/blog/2015/10/07/runtime-permissions-files-action-send.html */ private boolean checkAndRequestReadPermission(Activity activity, final Uri uri) { - if ( ! "file".equals(uri.getScheme())) { + if ( ! ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { return true; } @@ -676,7 +677,7 @@ public class DecryptListFragment Iterator it = mCancelledInputUris.iterator(); while (it.hasNext()) { Uri uri = it.next(); - if ( ! "file".equals(uri.getScheme())) { + if ( ! ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { continue; } it.remove(); @@ -694,7 +695,7 @@ public class DecryptListFragment Iterator it = mPendingInputUris.iterator(); while (it.hasNext()) { Uri uri = it.next(); - if ( ! "file".equals(uri.getScheme())) { + if ( ! ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { continue; } it.remove(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFilesFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFilesFragment.java index 7f5e4bd3d..be08f6a53 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFilesFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFilesFragment.java @@ -31,6 +31,7 @@ import android.annotation.TargetApi; import android.app.Activity; import android.content.ClipData; import android.content.ClipboardManager; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; @@ -55,6 +56,7 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; +import android.widget.Toast; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; @@ -267,7 +269,7 @@ public class EncryptFilesFragment * see https://commonsware.com/blog/2015/10/07/runtime-permissions-files-action-send.html */ private boolean checkAndRequestReadPermission(final Uri uri) { - if ( ! "file".equals(uri.getScheme())) { + if ( ! ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { return true; } @@ -305,8 +307,9 @@ public class EncryptFilesFragment // permission granted -> restart processing uris processPendingInputUris(); } else { - // permission denied -> - + Toast.makeText(getActivity(), R.string.error_denied_storage_permission, Toast.LENGTH_LONG).show(); + getActivity().setResult(Activity.RESULT_CANCELED); + getActivity().finish(); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java index bae119700..1c413034e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelper.java @@ -161,7 +161,7 @@ public class FileHelper { } public static long getFileSize(Context context, Uri uri, long def) { - if ("file".equals(uri.getScheme())) { + if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { long size = new File(uri.getPath()).length(); if (size == 0) { size = def; @@ -293,7 +293,7 @@ public class FileHelper { } out.close(); - if ("file".equals(uri.getScheme())) { + if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) { return new File(uri.getPath()).delete() ? 1 : 0; } else { return resolver.delete(uri, null, null); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelperLollipop.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelperLollipop.java index f89d679bc..ab2841ed5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelperLollipop.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/FileHelperLollipop.java @@ -1,3 +1,20 @@ +/* + * Copyright (C) 2015 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + package org.sufficientlysecure.keychain.util; diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index ff148b0c7..a8af687ac 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -375,6 +375,7 @@ "No keys selected for import!" "Retrieving the key ID from contacts failed!" "A generic error occurred, please create a new bug report for OpenKeychain." + "Can not read files from storage because access has been denied!" "Not Signed" -- cgit v1.2.3