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 cabc5eb71..3df29e507 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -39,6 +39,7 @@ 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.Preferences; @@ -481,15 +482,17 @@ public class ImportKeysActivity extends ActionBarActivity { Bundle data = new Bundle(); // get DATA from selected key entries - ArrayList selectedEntries = mListFragment.getSelectedData(); + IteratorWithSize selectedEntries = mListFragment.getSelectedData(); - // instead of given the entries by Intent extra, cache them into a file - // to prevent Java Binder problems on heavy imports + // instead of giving the entries by Intent extra, cache them into a + // file to prevent Java Binder problems on heavy imports // read FileImportCache for more info. try { + // We parcel this iteratively into a file - anything we can + // display here, we should be able to import. ParcelableFileCache cache = new ParcelableFileCache(this, "key_import.pcl"); - cache.writeCache(selectedEntries.size(), selectedEntries.iterator()); + cache.writeCache(selectedEntries.getSize(), 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 e30af6b29..1d9935aa1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java @@ -48,6 +48,7 @@ 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 @@ -74,12 +75,42 @@ public class ImportKeysListFragment extends ListFragment implements return mAdapter.getData(); } - public ArrayList getSelectedData() { - ArrayList result = new ArrayList(); - for (ImportKeysListEntry entry : getSelectedEntries()) { - result.add(mCachedKeyData.get(entry.hashCode())); - } - return result; + // 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 + * relevant elements on demand. + */ + public IteratorWithSize getSelectedData() { + final ArrayList entries = getSelectedEntries(); + final Iterator it = entries.iterator(); + return new IteratorWithSize() { + + @Override + public int getSize() { + return entries.size(); + } + + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public ParcelableKeyRing next() { + // throws NoSuchElementException if it doesn't exist, but that's not our problem + return mCachedKeyData.get(it.next().hashCode()); + } + + @Override + public void remove() { + it.remove(); + } + }; } public ArrayList getSelectedEntries() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java index 7b378ae79..b9eb4c8d4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java @@ -41,7 +41,6 @@ import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.List; import java.util.Map; @@ -91,17 +90,13 @@ public class ImportKeysAdapter extends ArrayAdapter { } public ArrayList getSelectedEntries() { - ArrayList selectedData = new ArrayList(); - // Nothing to select, nvm. - if (mData == null) { - return selectedData; - } + ArrayList result = new ArrayList(); for (ImportKeysListEntry entry : mData) { if (entry.isSelected()) { - selectedData.add(entry); + result.add(entry); } } - return selectedData; + return result; } @Override 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 3b4facbd0..e6345b1c3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableFileCache.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableFileCache.java @@ -55,6 +55,10 @@ public class ParcelableFileCache { 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; } @@ -107,7 +111,7 @@ public class ParcelableFileCache { throw new IOException(e); } - // yes this is slightly sloppy data flow. WE WOULDN'T NEED THIS WITH TUPLE RETURN TYPES + // yes this is sloppy data flow. WE WOULDN'T NEED THIS WITH TUPLE RETURN TYPES mNumEntries = ois.readInt(); return new Iterator() {