From 7bbe869c88c445b087e32a75572cf18efa2165b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 31 Jul 2014 20:38:06 +0200 Subject: Parcelable data over 1MB can not be send through binder, parcel into a cache file, fix #592 --- .../keychain/keyimport/FileImportCache.java | 101 +++++++++++++++++++++ .../keychain/service/KeychainIntentService.java | 13 ++- .../keychain/ui/ImportKeysActivity.java | 30 ++++-- 3 files changed, 128 insertions(+), 16 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java (limited to 'OpenKeychain/src/main/java') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java new file mode 100644 index 000000000..ff391af1a --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.keyimport; + +import android.content.Context; +import android.os.Bundle; +import android.os.Parcel; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.KeychainApplication; +import org.sufficientlysecure.keychain.util.Log; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * When sending large data (over 1MB) through Androids Binder IPC you get + * JavaBinder E !!! FAILED BINDER TRANSACTION !!! + *

+ * To overcome this problem, we cache large Parcelables into a file in our private cache directory + * instead of sending them through IPC. + */ +public class FileImportCache { + + private Context mContext; + + private static final String FILENAME = "key_import.pcl"; + private static final String BUNDLE_DATA = "data"; + + public FileImportCache(Context context) { + this.mContext = context; + } + + public void writeCache(ArrayList selectedEntries) throws IOException { + Bundle in = new Bundle(); + in.putParcelableArrayList(BUNDLE_DATA, selectedEntries); + File cacheDir = mContext.getCacheDir(); + if (cacheDir == null) { + // https://groups.google.com/forum/#!topic/android-developers/-694j87eXVU + throw new IOException("cache dir is null!"); + } + File tempFile = new File(mContext.getCacheDir(), FILENAME); + + FileOutputStream fos = new FileOutputStream(tempFile); + Parcel p = Parcel.obtain(); // creating empty parcel object + in.writeToParcel(p, 0); // saving bundle as parcel + fos.write(p.marshall()); // writing parcel to file + fos.flush(); + fos.close(); + } + + public List readCache() throws IOException { + Parcel parcel = Parcel.obtain(); // creating empty parcel object + Bundle out; + File cacheDir = mContext.getCacheDir(); + if (cacheDir == null) { + // https://groups.google.com/forum/#!topic/android-developers/-694j87eXVU + throw new IOException("cache dir is null!"); + } + + File tempFile = new File(cacheDir, FILENAME); + try { + + FileInputStream fis = new FileInputStream(tempFile); + byte[] array = new byte[(int) fis.getChannel().size()]; + fis.read(array, 0, array.length); + fis.close(); + + parcel.unmarshall(array, 0, array.length); + parcel.setDataPosition(0); + out = parcel.readBundle(KeychainApplication.class.getClassLoader()); + out.putAll(out); + + return out.getParcelableArrayList(BUNDLE_DATA); + } finally { + parcel.recycle(); + // delete temp file + tempFile.delete(); + } + } +} 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 f69961df6..77598e2b9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -31,11 +31,13 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.FileHelper; import org.sufficientlysecure.keychain.helper.OtherHelper; import org.sufficientlysecure.keychain.helper.Preferences; +import org.sufficientlysecure.keychain.keyimport.FileImportCache; import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; -import org.sufficientlysecure.keychain.keyimport.Keyserver; import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.keyimport.KeybaseKeyserver; +import org.sufficientlysecure.keychain.keyimport.Keyserver; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; +import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing; import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey; import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing; import org.sufficientlysecure.keychain.pgp.PgpDecryptVerify; @@ -46,7 +48,6 @@ import org.sufficientlysecure.keychain.pgp.PgpKeyOperation; import org.sufficientlysecure.keychain.pgp.PgpSignEncrypt; import org.sufficientlysecure.keychain.pgp.Progressable; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; -import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralMsgIdException; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; @@ -134,9 +135,6 @@ public class KeychainIntentService extends IntentService // delete file securely public static final String DELETE_FILE = "deleteFile"; - // import key - public static final String IMPORT_KEY_LIST = "import_key_list"; - // export key public static final String EXPORT_OUTPUT_STREAM = "export_output_stream"; public static final String EXPORT_FILENAME = "export_filename"; @@ -386,7 +384,9 @@ public class KeychainIntentService extends IntentService } } else if (ACTION_IMPORT_KEYRING.equals(action)) { try { - List entries = data.getParcelableArrayList(IMPORT_KEY_LIST); + // get entries from cached file + FileImportCache cache = new FileImportCache(this); + List entries = cache.readCache(); PgpImportExport pgpImportExport = new PgpImportExport(this, this); ImportKeyResult result = pgpImportExport.importKeyRings(entries); @@ -515,7 +515,6 @@ public class KeychainIntentService extends IntentService Intent importIntent = new Intent(this, KeychainIntentService.class); importIntent.setAction(ACTION_IMPORT_KEYRING); Bundle importData = new Bundle(); - importData.putParcelableArrayList(IMPORT_KEY_LIST, keyRings); importIntent.putExtra(EXTRA_DATA, importData); importIntent.putExtra(EXTRA_MESSENGER, mMessenger); 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 5f340019f..524fe2ef2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -40,6 +40,7 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.OtherHelper; import org.sufficientlysecure.keychain.helper.Preferences; +import org.sufficientlysecure.keychain.keyimport.FileImportCache; import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; @@ -51,6 +52,7 @@ import org.sufficientlysecure.keychain.ui.widget.SlidingTabLayout; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Notify; +import java.io.IOException; import java.util.ArrayList; import java.util.Locale; @@ -469,19 +471,29 @@ public class ImportKeysActivity extends ActionBarActivity { // get DATA from selected key entries ArrayList selectedEntries = mListFragment.getSelectedData(); - data.putParcelableArrayList(KeychainIntentService.IMPORT_KEY_LIST, selectedEntries); - intent.putExtra(KeychainIntentService.EXTRA_DATA, data); + // instead of given the entries by Intent extra, cache them into a file + // to prevent Java Binder problems on heavy imports + // read FileImportCache for more info. + try { + FileImportCache cache = new FileImportCache(this); + cache.writeCache(selectedEntries); - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(saveHandler); - intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); + intent.putExtra(KeychainIntentService.EXTRA_DATA, data); - // show progress dialog - saveHandler.showProgressDialog(this); + // Create a new Messenger for the communication back + Messenger messenger = new Messenger(saveHandler); + intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); - // start service with intent - startService(intent); + // show progress dialog + saveHandler.showProgressDialog(this); + + // start service with intent + startService(intent); + } catch (IOException e) { + Log.e(Constants.TAG, "Problem writing cache file", e); + Notify.showNotify(this, "Problem writing cache file!", Notify.Style.ERROR); + } } else if (ls instanceof ImportKeysListFragment.KeyserverLoaderState) { ImportKeysListFragment.KeyserverLoaderState sls = (ImportKeysListFragment.KeyserverLoaderState) ls; -- cgit v1.2.3