mirror of
https://github.com/moparisthebest/open-keychain
synced 2025-03-03 02:42:00 -05:00
Parcelable data over 1MB can not be send through binder, parcel into a cache file, fix #592
This commit is contained in:
parent
d48e980946
commit
7bbe869c88
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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 !!!
|
||||
* <p/>
|
||||
* 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<ParcelableKeyRing> 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<ParcelableKeyRing> 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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<ParcelableKeyRing> entries = data.getParcelableArrayList(IMPORT_KEY_LIST);
|
||||
// get entries from cached file
|
||||
FileImportCache cache = new FileImportCache(this);
|
||||
List<ParcelableKeyRing> 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);
|
||||
|
||||
|
@ -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<ParcelableKeyRing> 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;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user