move IteratorWithSize interface into ParcelableFileCache - much cleaner

This commit is contained in:
Vincent Breitmoser 2014-10-03 04:22:32 +02:00
parent 504064302b
commit 00bbf8c267
4 changed files with 25 additions and 20 deletions

View File

@ -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<ParcelableKeyRing> cache =
new ParcelableFileCache<ParcelableKeyRing>(this, "key_import.pcl");
entries = cache.readCache();
numEntries = cache.getNumEntries();
IteratorWithSize<ParcelableKeyRing> it = cache.readCache();
entries = it;
numEntries = it.getSize();
}
ProviderHelper providerHelper = new ProviderHelper(this);

View File

@ -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<ParcelableKeyRing> cache =
new ParcelableFileCache<ParcelableKeyRing>(this, "key_import.pcl");
cache.writeCache(selectedEntries.getSize(), selectedEntries);
cache.writeCache(selectedEntries);
intent.putExtra(KeychainIntentService.EXTRA_DATA, data);

View File

@ -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<E> extends Iterator<E> {
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

View File

@ -48,19 +48,14 @@ 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;
}
/** 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<E> it) throws IOException {
writeCache(it.getSize(), it);
}
public void writeCache(int numEntries, Iterator<E> it) throws IOException {
@ -90,11 +85,11 @@ public class ParcelableFileCache<E extends Parcelable> {
}
public Iterator<E> readCache() throws IOException {
public IteratorWithSize<E> readCache() throws IOException {
return readCache(true);
}
public Iterator<E> readCache(final boolean deleteAfterRead) throws IOException {
public IteratorWithSize<E> readCache(final boolean deleteAfterRead) throws IOException {
File cacheDir = mContext.getCacheDir();
if (cacheDir == null) {
@ -112,14 +107,18 @@ public class ParcelableFileCache<E extends Parcelable> {
}
// 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<E>() {
return new IteratorWithSize<E>() {
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<E extends Parcelable> {
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<E> extends Iterator<E> {
/** Returns the number of total entries in this iterator. */
int getSize();
}
}