diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 48883cde8..d1d4c33d4 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -154,8 +154,15 @@ - + + + + + + + + keyRingIds, - String filename, + OutputStream outStream, ProgressDialogUpdater progress) throws GeneralException, FileNotFoundException, PGPException, IOException { Bundle returnData = new Bundle(); @@ -667,8 +667,7 @@ public class Apg { if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { throw new GeneralException(context.getString(R.string.error_externalStorageNotReady)); } - FileOutputStream fileOut = new FileOutputStream(new File(filename), false); - ArmoredOutputStream out = new ArmoredOutputStream(fileOut); + ArmoredOutputStream out = new ArmoredOutputStream(outStream); int numKeys = 0; for (int i = 0; i < keyRingIds.size(); ++i) { @@ -689,7 +688,6 @@ public class Apg { ++numKeys; } out.close(); - fileOut.close(); returnData.putInt("exported", numKeys); progress.setProgress(R.string.progress_done, 100, 100); diff --git a/src/org/thialfihar/android/apg/GeneralActivity.java b/src/org/thialfihar/android/apg/GeneralActivity.java index a66a7c1b4..686d6fd39 100644 --- a/src/org/thialfihar/android/apg/GeneralActivity.java +++ b/src/org/thialfihar/android/apg/GeneralActivity.java @@ -144,10 +144,24 @@ public class GeneralActivity extends BaseActivity { } case Id.choice.action.import_public: { + intent.setClass(this, PublicKeyListActivity.class); + intent.setAction(Apg.Intent.IMPORT); + if (mDataString != null) { + intent.putExtra(Apg.EXTRA_TEXT, mDataString); + } else if (mDataUri != null) { + intent.setDataAndType(mDataUri, mIntent.getType()); + } break; } case Id.choice.action.import_secret: { + intent.setClass(this, SecretKeyListActivity.class); + intent.setAction(Apg.Intent.IMPORT); + if (mDataString != null) { + intent.putExtra(Apg.EXTRA_TEXT, mDataString); + } else if (mDataUri != null) { + intent.setDataAndType(mDataUri, mIntent.getType()); + } break; } diff --git a/src/org/thialfihar/android/apg/KeyListActivity.java b/src/org/thialfihar/android/apg/KeyListActivity.java index 4bb2c091d..1e531676b 100644 --- a/src/org/thialfihar/android/apg/KeyListActivity.java +++ b/src/org/thialfihar/android/apg/KeyListActivity.java @@ -16,8 +16,14 @@ package org.thialfihar.android.apg; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.Vector; import org.bouncycastle2.openpgp.PGPException; @@ -65,6 +71,8 @@ public class KeyListActivity extends BaseActivity { protected String mImportFilename = Constants.path.app_dir + "/"; protected String mExportFilename = Constants.path.app_dir + "/"; + protected String mImportData; + protected int mKeyType = Id.type.public_key; @Override @@ -89,6 +97,16 @@ public class KeyListActivity extends BaseActivity { }); handleIntent(getIntent()); + + Intent intent = getIntent(); + if (Apg.Intent.IMPORT.equals(intent.getAction())) { + if ("file".equals(intent.getScheme()) && intent.getDataString() != null) { + mImportFilename = intent.getDataString().replace("file://", ""); + } else { + mImportData = intent.getStringExtra(Apg.EXTRA_TEXT); + } + importKeys(); + } } @Override @@ -291,16 +309,26 @@ public class KeyListActivity extends BaseActivity { Bundle data = new Bundle(); Message msg = new Message(); - String filename = null; - if (mTask == Id.task.import_keys) { - filename = mImportFilename; - } else { - filename = mExportFilename; - } - try { + InputStream importInputStream = null; + OutputStream exportOutputStream = null; + long size = 0; if (mTask == Id.task.import_keys) { - data = Apg.importKeyRings(this, mKeyType, filename, this); + if (mImportData != null) { + byte[] bytes = mImportData.getBytes(); + size = bytes.length; + importInputStream = new ByteArrayInputStream(bytes); + } else { + File file = new File(mImportFilename); + size = file.length(); + importInputStream = new FileInputStream(file); + } + } else { + exportOutputStream = new FileOutputStream(mExportFilename); + } + + if (mTask == Id.task.import_keys) { + data = Apg.importKeyRings(this, mKeyType, importInputStream, size, this); } else { Vector keyRingIds = new Vector(); if (mSelectedItem == -1) { @@ -312,7 +340,7 @@ public class KeyListActivity extends BaseActivity { keyRingIds.add(keyRingId); mSelectedItem = -1; } - data = Apg.exportKeyRings(this, keyRingIds, filename, this); + data = Apg.exportKeyRings(this, keyRingIds, exportOutputStream, this); } } catch (FileNotFoundException e) { error = getString(R.string.error_fileNotFound); @@ -324,6 +352,8 @@ public class KeyListActivity extends BaseActivity { error = "" + e; } + mImportData = null; + if (mTask == Id.task.import_keys) { data.putInt(Apg.EXTRA_STATUS, Id.message.import_done); } else {