diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/processing/ImportKeysListener.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/processing/ImportKeysListener.java index 499d749e6..c4228c4de 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/processing/ImportKeysListener.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/processing/ImportKeysListener.java @@ -1,11 +1,16 @@ package org.sufficientlysecure.keychain.keyimport.processing; +import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; import org.sufficientlysecure.keychain.operations.results.ImportKeyResult; public interface ImportKeysListener { void loadKeys(LoaderState loaderState); + + void importKey(ParcelableKeyRing keyRing); + void importKeys(); + void handleResult(ImportKeyResult result); } 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 beac04dec..c11b116cf 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -336,6 +336,41 @@ public class ImportKeysActivity extends BaseActivity implements ImportKeysListen listFragment.loadState(loaderState); } + @Override + public void importKey(ParcelableKeyRing keyRing) { + FragmentManager fM = getSupportFragmentManager(); + ImportKeysListFragment listFragment = (ImportKeysListFragment) fM.findFragmentByTag(TAG_FRAG_LIST); + + String keyserver = null; + ArrayList keyList = null; + LoaderState loaderState = listFragment.getState(); + + Log.d(Constants.TAG, "importKey started"); + if (loaderState instanceof BytesLoaderState) { + // 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, ImportOperation.CACHE_FILE_NAME); + cache.writeCache(keyRing); + } catch (IOException e) { + Log.e(Constants.TAG, "Problem writing cache file", e); + Notify.create(this, "Problem writing cache file!", Notify.Style.ERROR).show(); + return; + } + } else if (loaderState instanceof CloudLoaderState) { + //TODO + } + + ImportKeysOperationCallback callback = new ImportKeysOperationCallback(this, keyserver, keyList); + mOperationHelper = new CryptoOperationHelper(1, this, callback, R.string.progress_importing); + + mOperationHelper.cryptoOperation(); + } + @Override public void importKeys() { FragmentManager fM = getSupportFragmentManager(); @@ -367,6 +402,7 @@ public class ImportKeysActivity extends BaseActivity implements ImportKeysListen return; } } else if (loaderState instanceof CloudLoaderState) { + //TODO This need to be removed because it makes no sense to import "all" from cloud CloudLoaderState sls = (CloudLoaderState) loaderState; // get selected key entries 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 511e1047a..2ad309429 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableFileCache.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ParcelableFileCache.java @@ -57,7 +57,39 @@ public class ParcelableFileCache { } public void writeCache(int numEntries, Iterator it) throws IOException { + DataOutputStream oos = getOutputStream(); + try { + oos.writeInt(numEntries); + while (it.hasNext()) { + writeParcelable(it.next(), oos); + } + } finally { + oos.close(); + } + } + + public void writeCache(E obj) throws IOException { + DataOutputStream oos = getOutputStream(); + + try { + oos.writeInt(1); + writeParcelable(obj, oos); + } finally { + oos.close(); + } + } + + private void writeParcelable(E obj, DataOutputStream oos) throws IOException { + Parcel p = Parcel.obtain(); // creating empty parcel object + p.writeParcelable(obj, 0); // saving bundle as parcel + byte[] buf = p.marshall(); + oos.writeInt(buf.length); + oos.write(buf); + p.recycle(); + } + + private DataOutputStream getOutputStream() throws IOException { File cacheDir = mContext.getCacheDir(); if (cacheDir == null) { // https://groups.google.com/forum/#!topic/android-developers/-694j87eXVU @@ -65,29 +97,12 @@ public class ParcelableFileCache { } File tempFile = new File(mContext.getCacheDir(), mFilename); - - - DataOutputStream oos = new DataOutputStream(new FileOutputStream(tempFile)); - - try { - oos.writeInt(numEntries); - - while (it.hasNext()) { - Parcel p = Parcel.obtain(); // creating empty parcel object - p.writeParcelable(it.next(), 0); // saving bundle as parcel - byte[] buf = p.marshall(); - oos.writeInt(buf.length); - oos.write(buf); - p.recycle(); - } - } finally { - oos.close(); - } - + return new DataOutputStream(new FileOutputStream(tempFile)); } /** * Reads from cache file and deletes it afterward. Convenience function for readCache(boolean). + * * @return an IteratorWithSize object containing entries read from the cache file * @throws IOException */ @@ -97,10 +112,11 @@ public class ParcelableFileCache { /** * Reads entries from a cache file and returns an IteratorWithSize object containing the entries + * * @param deleteAfterRead if true, the cache file will be deleted after being read * @return an IteratorWithSize object containing entries read from the cache file * @throws IOException if cache directory/parcel import file does not exist, or a read error - * occurs + * occurs */ public IteratorWithSize readCache(final boolean deleteAfterRead) throws IOException { @@ -205,7 +221,6 @@ public class ParcelableFileCache { } public boolean delete() throws IOException { - File cacheDir = mContext.getCacheDir(); if (cacheDir == null) { // https://groups.google.com/forum/#!topic/android-developers/-694j87eXVU