From 48dd3d09e6d78f965796bea5ad400e8e77f39d0d Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 19 Jan 2016 16:18:06 +0100 Subject: import: handle FileNotFoundException (fixes #1688) --- .../keychain/operations/results/GetKeyResult.java | 13 ++++--- .../operations/results/OperationResult.java | 14 +++---- .../keychain/pgp/UncachedKeyRing.java | 1 - .../keychain/ui/adapter/ImportKeysListLoader.java | 43 +++++++++++++--------- 4 files changed, 40 insertions(+), 31 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/GetKeyResult.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/GetKeyResult.java index bdc4d9a47..76ffaff4a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/GetKeyResult.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/GetKeyResult.java @@ -44,12 +44,13 @@ public class GetKeyResult extends InputPendingResult { super(log, requiredInput, cryptoInputParcel); } - public static final int RESULT_ERROR_NO_VALID_KEYS = RESULT_ERROR + 8; - public static final int RESULT_ERROR_NO_PGP_PARTS = RESULT_ERROR + 16; - public static final int RESULT_ERROR_QUERY_TOO_SHORT = RESULT_ERROR + 32; - public static final int RESULT_ERROR_TOO_MANY_RESPONSES = RESULT_ERROR + 64; - public static final int RESULT_ERROR_TOO_SHORT_OR_TOO_MANY_RESPONSES = RESULT_ERROR + 128; - public static final int RESULT_ERROR_QUERY_FAILED = RESULT_ERROR + 256; + public static final int RESULT_ERROR_NO_VALID_KEYS = RESULT_ERROR + (1<<4); + public static final int RESULT_ERROR_NO_PGP_PARTS = RESULT_ERROR + (2<<4); + public static final int RESULT_ERROR_QUERY_TOO_SHORT = RESULT_ERROR + (3<<4); + public static final int RESULT_ERROR_TOO_MANY_RESPONSES = RESULT_ERROR + (4<<4); + public static final int RESULT_ERROR_TOO_SHORT_OR_TOO_MANY_RESPONSES = RESULT_ERROR + (5<<4); + public static final int RESULT_ERROR_QUERY_FAILED = RESULT_ERROR + (6<<4); + public static final int RESULT_ERROR_FILE_NOT_FOUND = RESULT_ERROR + (7<<4); public GetKeyResult(Parcel source) { super(source); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java index 1e90c2cc3..f9c2db8e8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/results/OperationResult.java @@ -810,13 +810,13 @@ public abstract class OperationResult implements Parcelable { MSG_NO_VALID_ENC (LogLevel.ERROR, R.string.error_invalid_data), // get key - MSG_GET_SUCCESS(LogLevel.OK, R.string.msg_download_success), - MSG_GET_NO_VALID_KEYS(LogLevel.ERROR, R.string.msg_download_no_valid_keys), - MSG_GET_NO_PGP_PARTS(LogLevel.ERROR, R.string.msg_download_no_pgp_parts), - MSG_GET_QUERY_TOO_SHORT(LogLevel.ERROR, R.string.msg_download_query_too_short), - MSG_GET_TOO_MANY_RESPONSES(LogLevel.ERROR, R.string.msg_download_too_many_responses), - MSG_GET_QUERY_TOO_SHORT_OR_TOO_MANY_RESPONSES(LogLevel.ERROR, R.string.msg_download_query_too_short_or_too_many_responses), - MSG_GET_QUERY_FAILED(LogLevel.ERROR, R.string.msg_download_query_failed), + MSG_GET_SUCCESS (LogLevel.OK, R.string.msg_get_success), + MSG_GET_NO_VALID_KEYS (LogLevel.ERROR, R.string.msg_get_no_valid_keys), + MSG_GET_QUERY_TOO_SHORT (LogLevel.ERROR, R.string.msg_get_query_too_short), + MSG_GET_TOO_MANY_RESPONSES (LogLevel.ERROR, R.string.msg_get_too_many_responses), + MSG_GET_QUERY_TOO_SHORT_OR_TOO_MANY_RESPONSES (LogLevel.ERROR, R.string.msg_get_query_too_short_or_too_many_responses), + MSG_GET_QUERY_FAILED (LogLevel.ERROR, R.string.msg_download_query_failed), + MSG_GET_FILE_NOT_FOUND (LogLevel.ERROR, R.string.msg_get_file_not_found), MSG_DEL_ERROR_EMPTY (LogLevel.ERROR, R.string.msg_del_error_empty), MSG_DEL_ERROR_MULTI_SECRET (LogLevel.ERROR, R.string.msg_del_error_multi_secret), diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java index af09cf235..d1382f3dd 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -221,7 +221,6 @@ public class UncachedKeyRing { public boolean hasNext() throws IOException; public E next() throws IOException; } - public void encodeArmored(OutputStream out, String version) throws IOException { ArmoredOutputStream aos = new ArmoredOutputStream(out); if (version != null) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java index 038ebd5dd..0201318e8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java @@ -26,6 +26,7 @@ import java.io.InputStream; import java.util.ArrayList; import android.content.Context; +import android.support.annotation.NonNull; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.util.LongSparseArray; @@ -34,6 +35,8 @@ import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; import org.sufficientlysecure.keychain.operations.results.GetKeyResult; import org.sufficientlysecure.keychain.operations.results.OperationResult; +import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType; +import org.sufficientlysecure.keychain.operations.results.OperationResult.OperationLog; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing.IteratorWithIOThrow; import org.sufficientlysecure.keychain.ui.ImportKeysListFragment.BytesLoaderState; @@ -65,16 +68,25 @@ public class ImportKeysListLoader return mEntryListWrapper; } - GetKeyResult getKeyResult = new GetKeyResult(GetKeyResult.RESULT_OK, null); - mEntryListWrapper = new AsyncTaskResultWrapper<>(mData, getKeyResult); + { + GetKeyResult getKeyResult = new GetKeyResult(GetKeyResult.RESULT_OK, null); + mEntryListWrapper = new AsyncTaskResultWrapper<>(mData, getKeyResult); + } if (mLoaderState == null) { Log.e(Constants.TAG, "Input data is null!"); return mEntryListWrapper; } - InputData inputData = getInputData(getContext(), mLoaderState); - generateListOfKeyrings(inputData); + try { + InputData inputData = getInputData(getContext(), mLoaderState); + generateListOfKeyrings(inputData); + } catch (FileNotFoundException e) { + OperationLog log = new OperationLog(); + log.add(LogType.MSG_GET_FILE_NOT_FOUND, 0); + GetKeyResult getKeyResult = new GetKeyResult(GetKeyResult.RESULT_ERROR_FILE_NOT_FOUND, log); + mEntryListWrapper = new AsyncTaskResultWrapper<>(mData, getKeyResult); + } return mEntryListWrapper; } @@ -129,25 +141,22 @@ public class ImportKeysListLoader OperationResult.OperationLog log = new OperationResult.OperationLog(); log.add(OperationResult.LogType.MSG_GET_NO_VALID_KEYS, 0); GetKeyResult getKeyResult = new GetKeyResult(GetKeyResult.RESULT_ERROR_NO_VALID_KEYS, log); - mEntryListWrapper = new AsyncTaskResultWrapper<> - (mData, getKeyResult); + mEntryListWrapper = new AsyncTaskResultWrapper<>(mData, getKeyResult); } } - private static InputData getInputData(Context context, BytesLoaderState loaderState) { - InputData inputData = null; + @NonNull + private static InputData getInputData(Context context, BytesLoaderState loaderState) throws FileNotFoundException { + InputData inputData; if (loaderState.mKeyBytes != null) { inputData = new InputData(new ByteArrayInputStream(loaderState.mKeyBytes), loaderState.mKeyBytes.length); } else if (loaderState.mDataUri != null) { - try { - InputStream inputStream = context.getContentResolver().openInputStream(loaderState.mDataUri); - long length = FileHelper.getFileSize(context, loaderState.mDataUri, -1); - - inputData = new InputData(inputStream, length); - } catch (FileNotFoundException e) { - Log.e(Constants.TAG, "FileNotFoundException!", e); - return null; - } + InputStream inputStream = context.getContentResolver().openInputStream(loaderState.mDataUri); + long length = FileHelper.getFileSize(context, loaderState.mDataUri, -1); + + inputData = new InputData(inputStream, length); + } else { + throw new AssertionError("Loader state must contain bytes or a data URI. This is a bug!"); } return inputData; -- cgit v1.2.3