use iterator interface exclusively in ParcelableFileCache

This commit is contained in:
Vincent Breitmoser 2014-10-03 03:29:08 +02:00
parent 4b4e885e55
commit a91468565f
5 changed files with 30 additions and 24 deletions

View File

@ -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"));
}
}

View File

@ -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

View File

@ -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) {

View File

@ -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);

View File

@ -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;