diff options
author | Dominik Schürmann <dominik@dominikschuermann.de> | 2015-11-13 23:17:34 +0100 |
---|---|---|
committer | Dominik Schürmann <dominik@dominikschuermann.de> | 2015-11-13 23:17:34 +0100 |
commit | 1963ca9c2d88f70fdab1c3a696bc832cfc578aba (patch) | |
tree | e117e6f06407d26df2bc9f4b2455bd7d7ed29c29 /OpenKeychain/src/main/java/org/sufficientlysecure/keychain | |
parent | 0eade61cd4202897de6727650bd3f6d60b399705 (diff) | |
download | open-keychain-1963ca9c2d88f70fdab1c3a696bc832cfc578aba.tar.gz open-keychain-1963ca9c2d88f70fdab1c3a696bc832cfc578aba.tar.bz2 open-keychain-1963ca9c2d88f70fdab1c3a696bc832cfc578aba.zip |
Move permission check to createOperationInput
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain')
-rw-r--r-- | OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java | 187 |
1 files changed, 96 insertions, 91 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 d4190ea3e..dbee564b1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptListFragment.java @@ -224,12 +224,10 @@ public class DecryptListFragment continue; } - if (readPermissionGranted(uri)) { - if (results != null && results.containsKey(uri)) { - processResult(uri); - } else { - mPendingInputUris.add(uri); - } + if (results != null && results.containsKey(uri)) { + processResult(uri); + } else { + mPendingInputUris.add(uri); } } @@ -237,82 +235,6 @@ public class DecryptListFragment cryptoOperation(); } - /** - * Request READ_EXTERNAL_STORAGE permission on Android >= 6.0 to read content from "file" Uris - * - * see - * https://commonsware.com/blog/2015/10/07/runtime-permissions-files-action-send.html - */ - private boolean readPermissionGranted(Uri uri) { - if (Build.VERSION.SDK_INT < VERSION_CODES.M) { - return true; - } - if (! "file".equals(uri.getScheme())) { - return true; - } - - // Build check due to https://commonsware.com/blog/2015/11/09/you-cannot-hold-nonexistent-permissions.html - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN || - ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) - == PackageManager.PERMISSION_GRANTED) { - return true; - } else { - requestPermissions( - new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, - MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); - - if (! mCancelledInputUris.contains(uri)) { - mCancelledInputUris.add(uri); - } - return false; - } - } - - @Override - public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, - @NonNull int[] grantResults) { - switch (requestCode) { - case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: { - if (grantResults.length > 0 - && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - - // permission granted -> retry all cancelled uris! - for (Iterator<Uri> iterator = mCancelledInputUris.iterator(); iterator.hasNext(); ) { - Uri uri = iterator.next(); - - if ("file".equals(uri.getScheme())) { - iterator.remove(); - mPendingInputUris.add(uri); - mAdapter.setCancelled(uri, null); - } - } - - // check if there are any pending input uris - cryptoOperation(); - } else { - - // permission denied -> cancel all file uris - for (final Uri uri : mInputUris) { - if ("file".equals(uri.getScheme())) { - if (! mCancelledInputUris.contains(uri)) { - mCancelledInputUris.add(uri); - } - mAdapter.setCancelled(uri, new OnClickListener() { - @Override - public void onClick(View v) { - retryUri(uri); - } - }); - } - } - } - } - default: { - super.onRequestPermissionsResult(requestCode, permissions, grantResults); - } - } - } - @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { @@ -534,11 +456,9 @@ public class DecryptListFragment } // un-cancel this one - if (readPermissionGranted(uri)) { - mCancelledInputUris.remove(uri); - mPendingInputUris.add(uri); - mAdapter.setCancelled(uri, null); - } + mCancelledInputUris.remove(uri); + mPendingInputUris.add(uri); + mAdapter.setCancelled(uri, null); // check if there are any pending input uris cryptoOperation(); @@ -671,12 +591,97 @@ public class DecryptListFragment mCurrentInputUri = mPendingInputUris.remove(0); } - Log.d(Constants.TAG, "mInputUri=" + mCurrentInputUri); + Log.d(Constants.TAG, "mCurrentInputUri=" + mCurrentInputUri); - PgpDecryptVerifyInputParcel decryptInput = new PgpDecryptVerifyInputParcel() - .setAllowSymmetricDecryption(true); - return new InputDataParcel(mCurrentInputUri, decryptInput); + if (readPermissionGranted(mCurrentInputUri)) { + PgpDecryptVerifyInputParcel decryptInput = new PgpDecryptVerifyInputParcel() + .setAllowSymmetricDecryption(true); + return new InputDataParcel(mCurrentInputUri, decryptInput); + } else { + return null; + } + } + + /** + * Request READ_EXTERNAL_STORAGE permission on Android >= 6.0 to read content from "file" Uris + * + * see + * https://commonsware.com/blog/2015/10/07/runtime-permissions-files-action-send.html + */ + private boolean readPermissionGranted(final Uri uri) { + if (Build.VERSION.SDK_INT < VERSION_CODES.M) { + return true; + } + if (! "file".equals(uri.getScheme())) { + return true; + } + + // Build check due to https://commonsware.com/blog/2015/11/09/you-cannot-hold-nonexistent-permissions.html + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN || + ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE) + == PackageManager.PERMISSION_GRANTED) { + return true; + } else { + requestPermissions( + new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, + MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); + + mCurrentInputUri = null; + mCancelledInputUris.add(uri); + mAdapter.setCancelled(uri, new OnClickListener() { + @Override + public void onClick(View v) { + retryUri(uri); + } + }); + return false; + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, + @NonNull int[] grantResults) { + switch (requestCode) { + case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: { + if (grantResults.length > 0 + && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + + // permission granted -> retry all cancelled file uris! + for (Iterator<Uri> iterator = mCancelledInputUris.iterator(); iterator.hasNext(); ) { + Uri uri = iterator.next(); + if ("file".equals(uri.getScheme())) { + iterator.remove(); + mPendingInputUris.add(uri); + mAdapter.setCancelled(uri, null); + } + } + + // check if there are any pending input uris + cryptoOperation(); + } else { + + // permission denied -> cancel all pending file uris + mCurrentInputUri = null; + for (final Uri uri : mPendingInputUris) { + if ("file".equals(uri.getScheme())) { + if (! mCancelledInputUris.contains(uri)) { + mCancelledInputUris.add(uri); + } + mAdapter.setCancelled(uri, new OnClickListener() { + @Override + public void onClick(View v) { + retryUri(uri); + } + }); + } + } + } + } + default: { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + } + } } @Override |