From 00bbf8c26787d97f5ef5eed86ffa58d0522a576b Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 3 Oct 2014 04:22:32 +0200 Subject: [PATCH] move IteratorWithSize interface into ParcelableFileCache - much cleaner --- .../service/KeychainIntentService.java | 6 ++-- .../keychain/ui/ImportKeysActivity.java | 4 +-- .../keychain/ui/ImportKeysListFragment.java | 6 +--- .../keychain/util/ParcelableFileCache.java | 29 ++++++++++++------- 4 files changed, 25 insertions(+), 20 deletions(-) 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 833dccde5..2101705bc 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -31,6 +31,7 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.provider.ProviderHelper.NotFoundException; import org.sufficientlysecure.keychain.util.FileHelper; +import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize; import org.sufficientlysecure.keychain.util.Preferences; import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; @@ -523,8 +524,9 @@ public class KeychainIntentService extends IntentService implements Progressable // get entries from cached file ParcelableFileCache cache = new ParcelableFileCache(this, "key_import.pcl"); - entries = cache.readCache(); - numEntries = cache.getNumEntries(); + IteratorWithSize it = cache.readCache(); + entries = it; + numEntries = it.getSize(); } ProviderHelper providerHelper = new ProviderHelper(this); 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 3df29e507..efad2e45d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -39,9 +39,9 @@ import android.view.ViewGroup; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.api.OpenKeychainIntents; -import org.sufficientlysecure.keychain.ui.ImportKeysListFragment.IteratorWithSize; import org.sufficientlysecure.keychain.ui.util.FormattingUtils; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; +import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize; import org.sufficientlysecure.keychain.util.Preferences; import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; @@ -492,7 +492,7 @@ public class ImportKeysActivity extends ActionBarActivity { // display here, we should be able to import. ParcelableFileCache cache = new ParcelableFileCache(this, "key_import.pcl"); - cache.writeCache(selectedEntries.getSize(), selectedEntries); + cache.writeCache(selectedEntries); intent.putExtra(KeychainIntentService.EXTRA_DATA, data); 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 1d9935aa1..608c0f032 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java @@ -31,6 +31,7 @@ import android.widget.ListView; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.util.ParcelableFileCache.IteratorWithSize; import org.sufficientlysecure.keychain.util.Preferences; import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.keyimport.Keyserver; @@ -75,11 +76,6 @@ public class ImportKeysListFragment extends ListFragment implements return mAdapter.getData(); } - // Tuples would make this easier... - public static interface IteratorWithSize extends Iterator { - int getSize(); - } - /** Returns an Iterator (with size) of the selected data items. * This iterator is sort of a tradeoff, it's slightly more complex than an * ArrayList would have been, but we save some memory by just returning 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 e6345b1c3..3081021cf 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableFileCache.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableFileCache.java @@ -48,19 +48,14 @@ public class ParcelableFileCache { private Context mContext; private final String mFilename; - private int mNumEntries; public ParcelableFileCache(Context context, String filename) { mContext = context; mFilename = filename; } - /** This method returns the number of entries as valid for the iterator - * received by the latest readCache operation. Yes, it is slightly - * peculiar. - */ - public int getNumEntries() { - return mNumEntries; + public void writeCache(IteratorWithSize it) throws IOException { + writeCache(it.getSize(), it); } public void writeCache(int numEntries, Iterator it) throws IOException { @@ -90,11 +85,11 @@ public class ParcelableFileCache { } - public Iterator readCache() throws IOException { + public IteratorWithSize readCache() throws IOException { return readCache(true); } - public Iterator readCache(final boolean deleteAfterRead) throws IOException { + public IteratorWithSize readCache(final boolean deleteAfterRead) throws IOException { File cacheDir = mContext.getCacheDir(); if (cacheDir == null) { @@ -112,14 +107,18 @@ public class ParcelableFileCache { } // yes this is sloppy data flow. WE WOULDN'T NEED THIS WITH TUPLE RETURN TYPES - mNumEntries = ois.readInt(); + final int numEntries = ois.readInt(); - return new Iterator() { + return new IteratorWithSize() { E mRing = null; boolean closed = false; byte[] buf = new byte[512]; + public int getSize() { + return numEntries; + } + private void readNext() { if (mRing != null || closed) { return; @@ -205,4 +204,12 @@ public class ParcelableFileCache { return tempFile.delete(); } + /** As the name implies, this is an extended iterator interface, which + * knows the total number of its entries beforehand. + */ + public static interface IteratorWithSize extends Iterator { + /** Returns the number of total entries in this iterator. */ + int getSize(); + } + }