limit GeneralActivity to non-Uri Intents or file:// Uri Intents, now handling key imports as well

This commit is contained in:
Thialfihar 2010-06-06 19:46:49 +00:00
parent edd755bd0e
commit 6d2a1edd17
4 changed files with 68 additions and 19 deletions

View File

@ -154,8 +154,15 @@
<action android:name="android.intent.action.VIEW" /> <action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="*/*"/> <data android:mimeType="*/*" android:scheme="file"/>
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity> </activity>
<activity <activity

View File

@ -108,6 +108,7 @@ public class Apg {
public static final String ENCRYPT_AND_RETURN = "org.thialfihar.android.apg.intent.ENCRYPT_AND_RETURN"; public static final String ENCRYPT_AND_RETURN = "org.thialfihar.android.apg.intent.ENCRYPT_AND_RETURN";
public static final String SELECT_PUBLIC_KEYS = "org.thialfihar.android.apg.intent.SELECT_PUBLIC_KEYS"; public static final String SELECT_PUBLIC_KEYS = "org.thialfihar.android.apg.intent.SELECT_PUBLIC_KEYS";
public static final String SELECT_SECRET_KEY = "org.thialfihar.android.apg.intent.SELECT_SECRET_KEY"; public static final String SELECT_SECRET_KEY = "org.thialfihar.android.apg.intent.SELECT_SECRET_KEY";
public static final String IMPORT = "org.thialfihar.android.apg.intent.IMPORT";
} }
public static final String EXTRA_TEXT = "text"; public static final String EXTRA_TEXT = "text";
@ -573,7 +574,8 @@ public class Apg {
progress.setProgress(R.string.progress_done, 100, 100); progress.setProgress(R.string.progress_done, 100, 100);
} }
public static Bundle importKeyRings(Activity context, int type, String filename, public static Bundle importKeyRings(Activity context, int type,
InputStream inStream, long dataLength,
ProgressDialogUpdater progress) ProgressDialogUpdater progress)
throws GeneralException, FileNotFoundException, PGPException, IOException { throws GeneralException, FileNotFoundException, PGPException, IOException {
Bundle returnData = new Bundle(); Bundle returnData = new Bundle();
@ -588,9 +590,7 @@ public class Apg {
throw new GeneralException(context.getString(R.string.error_externalStorageNotReady)); throw new GeneralException(context.getString(R.string.error_externalStorageNotReady));
} }
FileInputStream fileIn = new FileInputStream(filename); PositionAwareInputStream progressIn = new PositionAwareInputStream(inStream);
long fileSize = new File(filename).length();
PositionAwareInputStream progressIn = new PositionAwareInputStream(fileIn);
// need to have access to the bufferedInput, so we can reuse it for the possible // need to have access to the bufferedInput, so we can reuse it for the possible
// PGPObject chunks after the first one, e.g. files with several consecutive ASCII // PGPObject chunks after the first one, e.g. files with several consecutive ASCII
// armour blocks // armour blocks
@ -636,7 +636,7 @@ public class Apg {
} else if (retValue == Id.return_value.ok) { } else if (retValue == Id.return_value.ok) {
++newKeys; ++newKeys;
} }
progress.setProgress((int)(100 * progressIn.position() / fileSize), 100); progress.setProgress((int)(100 * progressIn.position() / dataLength), 100);
obj = objectFactory.nextObject(); obj = objectFactory.nextObject();
} }
} }
@ -653,7 +653,7 @@ public class Apg {
} }
public static Bundle exportKeyRings(Activity context, Vector<Integer> keyRingIds, public static Bundle exportKeyRings(Activity context, Vector<Integer> keyRingIds,
String filename, OutputStream outStream,
ProgressDialogUpdater progress) ProgressDialogUpdater progress)
throws GeneralException, FileNotFoundException, PGPException, IOException { throws GeneralException, FileNotFoundException, PGPException, IOException {
Bundle returnData = new Bundle(); Bundle returnData = new Bundle();
@ -667,8 +667,7 @@ public class Apg {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
throw new GeneralException(context.getString(R.string.error_externalStorageNotReady)); throw new GeneralException(context.getString(R.string.error_externalStorageNotReady));
} }
FileOutputStream fileOut = new FileOutputStream(new File(filename), false); ArmoredOutputStream out = new ArmoredOutputStream(outStream);
ArmoredOutputStream out = new ArmoredOutputStream(fileOut);
int numKeys = 0; int numKeys = 0;
for (int i = 0; i < keyRingIds.size(); ++i) { for (int i = 0; i < keyRingIds.size(); ++i) {
@ -689,7 +688,6 @@ public class Apg {
++numKeys; ++numKeys;
} }
out.close(); out.close();
fileOut.close();
returnData.putInt("exported", numKeys); returnData.putInt("exported", numKeys);
progress.setProgress(R.string.progress_done, 100, 100); progress.setProgress(R.string.progress_done, 100, 100);

View File

@ -144,10 +144,24 @@ public class GeneralActivity extends BaseActivity {
} }
case Id.choice.action.import_public: { 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; break;
} }
case Id.choice.action.import_secret: { 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; break;
} }

View File

@ -16,8 +16,14 @@
package org.thialfihar.android.apg; package org.thialfihar.android.apg;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector; import java.util.Vector;
import org.bouncycastle2.openpgp.PGPException; import org.bouncycastle2.openpgp.PGPException;
@ -65,6 +71,8 @@ public class KeyListActivity extends BaseActivity {
protected String mImportFilename = Constants.path.app_dir + "/"; protected String mImportFilename = Constants.path.app_dir + "/";
protected String mExportFilename = Constants.path.app_dir + "/"; protected String mExportFilename = Constants.path.app_dir + "/";
protected String mImportData;
protected int mKeyType = Id.type.public_key; protected int mKeyType = Id.type.public_key;
@Override @Override
@ -89,6 +97,16 @@ public class KeyListActivity extends BaseActivity {
}); });
handleIntent(getIntent()); 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 @Override
@ -291,16 +309,26 @@ public class KeyListActivity extends BaseActivity {
Bundle data = new Bundle(); Bundle data = new Bundle();
Message msg = new Message(); Message msg = new Message();
String filename = null; try {
InputStream importInputStream = null;
OutputStream exportOutputStream = null;
long size = 0;
if (mTask == Id.task.import_keys) { if (mTask == Id.task.import_keys) {
filename = mImportFilename; if (mImportData != null) {
byte[] bytes = mImportData.getBytes();
size = bytes.length;
importInputStream = new ByteArrayInputStream(bytes);
} else { } else {
filename = mExportFilename; File file = new File(mImportFilename);
size = file.length();
importInputStream = new FileInputStream(file);
}
} else {
exportOutputStream = new FileOutputStream(mExportFilename);
} }
try {
if (mTask == Id.task.import_keys) { if (mTask == Id.task.import_keys) {
data = Apg.importKeyRings(this, mKeyType, filename, this); data = Apg.importKeyRings(this, mKeyType, importInputStream, size, this);
} else { } else {
Vector<Integer> keyRingIds = new Vector<Integer>(); Vector<Integer> keyRingIds = new Vector<Integer>();
if (mSelectedItem == -1) { if (mSelectedItem == -1) {
@ -312,7 +340,7 @@ public class KeyListActivity extends BaseActivity {
keyRingIds.add(keyRingId); keyRingIds.add(keyRingId);
mSelectedItem = -1; mSelectedItem = -1;
} }
data = Apg.exportKeyRings(this, keyRingIds, filename, this); data = Apg.exportKeyRings(this, keyRingIds, exportOutputStream, this);
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
error = getString(R.string.error_fileNotFound); error = getString(R.string.error_fileNotFound);
@ -324,6 +352,8 @@ public class KeyListActivity extends BaseActivity {
error = "" + e; error = "" + e;
} }
mImportData = null;
if (mTask == Id.task.import_keys) { if (mTask == Id.task.import_keys) {
data.putInt(Apg.EXTRA_STATUS, Id.message.import_done); data.putInt(Apg.EXTRA_STATUS, Id.message.import_done);
} else { } else {