diff options
author | Vincent Breitmoser <valodim@mugenguild.com> | 2014-10-03 03:29:08 +0200 |
---|---|---|
committer | Vincent Breitmoser <valodim@mugenguild.com> | 2014-10-03 03:29:23 +0200 |
commit | a91468565ff600e082c29aec6433c51a19f26b07 (patch) | |
tree | cf362b9e8e09b6c7aa9375efc5c8fbbb0c70db9d | |
parent | 4b4e885e55f788bd2cf05f3c83f5a4bccb6cd0c0 (diff) | |
download | open-keychain-a91468565ff600e082c29aec6433c51a19f26b07.tar.gz open-keychain-a91468565ff600e082c29aec6433c51a19f26b07.tar.bz2 open-keychain-a91468565ff600e082c29aec6433c51a19f26b07.zip |
use iterator interface exclusively in ParcelableFileCache
5 files changed, 30 insertions, 24 deletions
diff --git a/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/util/ParcelableFileCacheTest.java b/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/util/ParcelableFileCacheTest.java index ba9ba7369..7f473c345 100644 --- a/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/util/ParcelableFileCacheTest.java +++ b/OpenKeychain-Test/src/test/java/org/sufficientlysecure/keychain/util/ParcelableFileCacheTest.java @@ -29,6 +29,7 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.shadows.ShadowLog; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; @RunWith(RobolectricTestRunner.class) @@ -55,16 +56,19 @@ public class ParcelableFileCacheTest { } // write to cache file - cache.writeCache(list); + cache.writeCache(list.size(), list.iterator()); // read back - List<Bundle> last = cache.readCacheIntoList(); + Iterator<Bundle> it = cache.readCache(); - for (int i = 0; i < list.size(); i++) { + Assert.assertEquals("number of entries must be correct", list.size(), cache.getNumEntries()); + + while (it.hasNext()) { + Bundle b = it.next(); Assert.assertEquals("input values should be equal to output values", - list.get(i).getInt("key1"), last.get(i).getInt("key1")); + b.getInt("key1"), b.getInt("key1")); Assert.assertEquals("input values should be equal to output values", - list.get(i).getString("key2"), last.get(i).getString("key2")); + b.getString("key2"), b.getString("key2")); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index f8600d9d4..d4aabc1b5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -929,7 +929,7 @@ public class ProviderHelper { ParcelableFileCache<ParcelableKeyRing> cache = new ParcelableFileCache<ParcelableKeyRing>(mContext, "consolidate_secret.pcl"); - cache.writeCache(new Iterator<ParcelableKeyRing>() { + cache.writeCache(cursor.getCount(), new Iterator<ParcelableKeyRing>() { ParcelableKeyRing ring; @Override @@ -991,7 +991,7 @@ public class ProviderHelper { ParcelableFileCache<ParcelableKeyRing> cache = new ParcelableFileCache<ParcelableKeyRing>(mContext, "consolidate_public.pcl"); - cache.writeCache(new Iterator<ParcelableKeyRing>() { + cache.writeCache(cursor.getCount(), new Iterator<ParcelableKeyRing>() { ParcelableKeyRing ring; @Override diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index 0ee3c7093..833dccde5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -78,6 +78,7 @@ import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.Date; +import java.util.Iterator; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; @@ -511,21 +512,25 @@ public class KeychainIntentService extends IntentService implements Progressable } else if (ACTION_IMPORT_KEYRING.equals(action)) { try { - List<ParcelableKeyRing> entries; + Iterator<ParcelableKeyRing> entries; + int numEntries; if (data.containsKey(IMPORT_KEY_LIST)) { // get entries from intent - entries = data.getParcelableArrayList(IMPORT_KEY_LIST); + ArrayList<ParcelableKeyRing> list = data.getParcelableArrayList(IMPORT_KEY_LIST); + entries = list.iterator(); + numEntries = list.size(); } else { // get entries from cached file ParcelableFileCache<ParcelableKeyRing> cache = new ParcelableFileCache<ParcelableKeyRing>(this, "key_import.pcl"); - entries = cache.readCacheIntoList(); + entries = cache.readCache(); + numEntries = cache.getNumEntries(); } ProviderHelper providerHelper = new ProviderHelper(this); PgpImportExport pgpImportExport = new PgpImportExport( this, providerHelper, this, mActionCanceled); - ImportKeyResult result = pgpImportExport.importKeyRings(entries); + ImportKeyResult result = pgpImportExport.importKeyRings(entries, numEntries); // we do this even on failure or cancellation! if (result.mSecret > 0) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index 6298f96c0..cabc5eb71 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -489,7 +489,7 @@ public class ImportKeysActivity extends ActionBarActivity { try { ParcelableFileCache<ParcelableKeyRing> cache = new ParcelableFileCache<ParcelableKeyRing>(this, "key_import.pcl"); - cache.writeCache(selectedEntries); + cache.writeCache(selectedEntries.size(), selectedEntries.iterator()); intent.putExtra(KeychainIntentService.EXTRA_DATA, data); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableFileCache.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableFileCache.java index 111bf0124..3b4facbd0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableFileCache.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableFileCache.java @@ -48,17 +48,18 @@ public class ParcelableFileCache<E extends Parcelable> { private Context mContext; private final String mFilename; + private int mNumEntries; public ParcelableFileCache(Context context, String filename) { mContext = context; mFilename = filename; } - public void writeCache(ArrayList<E> selectedEntries) throws IOException { - writeCache(selectedEntries.iterator()); + public int getNumEntries() { + return mNumEntries; } - public void writeCache(Iterator<E> it) throws IOException { + public void writeCache(int numEntries, Iterator<E> it) throws IOException { File cacheDir = mContext.getCacheDir(); if (cacheDir == null) { @@ -70,6 +71,8 @@ public class ParcelableFileCache<E extends Parcelable> { DataOutputStream oos = new DataOutputStream(new FileOutputStream(tempFile)); + oos.writeInt(numEntries); + while (it.hasNext()) { Parcel p = Parcel.obtain(); // creating empty parcel object p.writeParcelable(it.next(), 0); // saving bundle as parcel @@ -83,15 +86,6 @@ public class ParcelableFileCache<E extends Parcelable> { } - public List<E> readCacheIntoList() throws IOException { - ArrayList<E> result = new ArrayList<E>(); - Iterator<E> it = readCache(); - while (it.hasNext()) { - result.add(it.next()); - } - return result; - } - public Iterator<E> readCache() throws IOException { return readCache(true); } @@ -113,6 +107,9 @@ public class ParcelableFileCache<E extends Parcelable> { throw new IOException(e); } + // yes this is slightly sloppy data flow. WE WOULDN'T NEED THIS WITH TUPLE RETURN TYPES + mNumEntries = ois.readInt(); + return new Iterator<E>() { E mRing = null; |