import: handle FileNotFoundException (fixes #1688)
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
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;
|
||||
}
|
||||
inputData = new InputData(inputStream, length);
|
||||
} else {
|
||||
throw new AssertionError("Loader state must contain bytes or a data URI. This is a bug!");
|
||||
}
|
||||
|
||||
return inputData;
|
||||
|
||||
Reference in New Issue
Block a user