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.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="*/*"/>
<data android:mimeType="*/*" android:scheme="file"/>
</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

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 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 IMPORT = "org.thialfihar.android.apg.intent.IMPORT";
}
public static final String EXTRA_TEXT = "text";
@ -573,7 +574,8 @@ public class Apg {
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)
throws GeneralException, FileNotFoundException, PGPException, IOException {
Bundle returnData = new Bundle();
@ -588,9 +590,7 @@ public class Apg {
throw new GeneralException(context.getString(R.string.error_externalStorageNotReady));
}
FileInputStream fileIn = new FileInputStream(filename);
long fileSize = new File(filename).length();
PositionAwareInputStream progressIn = new PositionAwareInputStream(fileIn);
PositionAwareInputStream progressIn = new PositionAwareInputStream(inStream);
// 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
// armour blocks
@ -636,7 +636,7 @@ public class Apg {
} else if (retValue == Id.return_value.ok) {
++newKeys;
}
progress.setProgress((int)(100 * progressIn.position() / fileSize), 100);
progress.setProgress((int)(100 * progressIn.position() / dataLength), 100);
obj = objectFactory.nextObject();
}
}
@ -653,7 +653,7 @@ public class Apg {
}
public static Bundle exportKeyRings(Activity context, Vector<Integer> 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);

View File

@ -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;
}

View File

@ -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;
try {
InputStream importInputStream = null;
OutputStream exportOutputStream = null;
long size = 0;
if (mTask == Id.task.import_keys) {
filename = mImportFilename;
if (mImportData != null) {
byte[] bytes = mImportData.getBytes();
size = bytes.length;
importInputStream = new ByteArrayInputStream(bytes);
} 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) {
data = Apg.importKeyRings(this, mKeyType, filename, this);
data = Apg.importKeyRings(this, mKeyType, importInputStream, size, this);
} else {
Vector<Integer> keyRingIds = new Vector<Integer>();
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 {