diff options
author | Vincent Breitmoser <valodim@mugenguild.com> | 2015-10-26 15:28:53 +0100 |
---|---|---|
committer | Vincent Breitmoser <valodim@mugenguild.com> | 2015-10-26 15:28:53 +0100 |
commit | c55ca09a0658e3602a1f349f2d1b58a54c7f95b4 (patch) | |
tree | 53008af5c21c7a5698fe9fddd414e6c8fcb141ea /OpenKeychain/src/main/java/org/sufficientlysecure/keychain | |
parent | 10407d34de7a0aa466470dd402ee10be6249d800 (diff) | |
download | open-keychain-c55ca09a0658e3602a1f349f2d1b58a54c7f95b4.tar.gz open-keychain-c55ca09a0658e3602a1f349f2d1b58a54c7f95b4.tar.bz2 open-keychain-c55ca09a0658e3602a1f349f2d1b58a54c7f95b4.zip |
import: move uri resolution into AsyncTask, prevent NetworkOnMainThreadException
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain')
2 files changed, 44 insertions, 49 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java index 864283b0a..7aed3176c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java @@ -17,6 +17,11 @@ package org.sufficientlysecure.keychain.ui; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + import android.app.Activity; import android.net.Uri; import android.os.Bundle; @@ -40,22 +45,12 @@ import org.sufficientlysecure.keychain.ui.adapter.AsyncTaskResultWrapper; import org.sufficientlysecure.keychain.ui.adapter.ImportKeysAdapter; import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListCloudLoader; import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListLoader; -import org.sufficientlysecure.keychain.util.FileHelper; -import org.sufficientlysecure.keychain.util.InputData; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize; import org.sufficientlysecure.keychain.util.ParcelableProxy; import org.sufficientlysecure.keychain.util.Preferences; import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; -import java.io.ByteArrayInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - public class ImportKeysListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>> { @@ -180,8 +175,8 @@ public class ImportKeysListFragment extends ListFragment implements } static public class BytesLoaderState extends LoaderState { - byte[] mKeyBytes; - Uri mDataUri; + public byte[] mKeyBytes; + public Uri mDataUri; BytesLoaderState(byte[] keyBytes, Uri dataUri) { mKeyBytes = keyBytes; @@ -305,9 +300,7 @@ public class ImportKeysListFragment extends ListFragment implements onCreateLoader(int id, Bundle args) { switch (id) { case LOADER_ID_BYTES: { - BytesLoaderState ls = (BytesLoaderState) mLoaderState; - InputData inputData = getInputData(ls.mKeyBytes, ls.mDataUri); - return new ImportKeysListLoader(mActivity, inputData); + return new ImportKeysListLoader(mActivity, (BytesLoaderState) mLoaderState); } case LOADER_ID_CLOUD: { CloudLoaderState ls = (CloudLoaderState) mLoaderState; @@ -432,23 +425,4 @@ public class ImportKeysListFragment extends ListFragment implements } } - private InputData getInputData(byte[] importBytes, Uri dataUri) { - InputData inputData = null; - if (importBytes != null) { - inputData = new InputData(new ByteArrayInputStream(importBytes), importBytes.length); - } else if (dataUri != null) { - try { - InputStream inputStream = getActivity().getContentResolver().openInputStream(dataUri); - long length = FileHelper.getFileSize(getActivity(), dataUri, -1); - - inputData = new InputData(inputStream, length); - } catch (FileNotFoundException e) { - Log.e(Constants.TAG, "FileNotFoundException!", e); - return null; - } - } - - return inputData; - } - } 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 139512ba9..038ebd5dd 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 @@ -17,6 +17,14 @@ package org.sufficientlysecure.keychain.ui.adapter; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; + import android.content.Context; import android.support.v4.content.AsyncTaskLoader; import android.support.v4.util.LongSparseArray; @@ -28,28 +36,26 @@ import org.sufficientlysecure.keychain.operations.results.GetKeyResult; import org.sufficientlysecure.keychain.operations.results.OperationResult; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing.IteratorWithIOThrow; +import org.sufficientlysecure.keychain.ui.ImportKeysListFragment.BytesLoaderState; +import org.sufficientlysecure.keychain.util.FileHelper; import org.sufficientlysecure.keychain.util.InputData; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.PositionAwareInputStream; -import java.io.BufferedInputStream; -import java.io.IOException; -import java.util.ArrayList; - public class ImportKeysListLoader extends AsyncTaskLoader<AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>>> { final Context mContext; - final InputData mInputData; + final BytesLoaderState mLoaderState; ArrayList<ImportKeysListEntry> mData = new ArrayList<>(); LongSparseArray<ParcelableKeyRing> mParcelableRings = new LongSparseArray<>(); AsyncTaskResultWrapper<ArrayList<ImportKeysListEntry>> mEntryListWrapper; - public ImportKeysListLoader(Context context, InputData inputData) { + public ImportKeysListLoader(Context context, BytesLoaderState inputData) { super(context); this.mContext = context; - this.mInputData = inputData; + this.mLoaderState = inputData; } @Override @@ -62,12 +68,13 @@ public class ImportKeysListLoader GetKeyResult getKeyResult = new GetKeyResult(GetKeyResult.RESULT_OK, null); mEntryListWrapper = new AsyncTaskResultWrapper<>(mData, getKeyResult); - if (mInputData == null) { + if (mLoaderState == null) { Log.e(Constants.TAG, "Input data is null!"); return mEntryListWrapper; } - generateListOfKeyrings(mInputData); + InputData inputData = getInputData(getContext(), mLoaderState); + generateListOfKeyrings(inputData); return mEntryListWrapper; } @@ -99,12 +106,7 @@ public class ImportKeysListLoader return mParcelableRings; } - /** - * Reads all PGPKeyRing objects from input - * - * @param inputData - * @return - */ + /** Reads all PGPKeyRing objects from the bytes of an InputData object. */ private void generateListOfKeyrings(InputData inputData) { PositionAwareInputStream progressIn = new PositionAwareInputStream( inputData.getInputStream()); @@ -132,4 +134,23 @@ public class ImportKeysListLoader } } + private static InputData getInputData(Context context, BytesLoaderState loaderState) { + InputData inputData = null; + 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; + } + } + + return inputData; + } + } |