Fix export of keys

This commit is contained in:
Dominik 2012-11-22 12:47:20 +01:00
parent 8f686e67f3
commit a4ea3e65a7
5 changed files with 70 additions and 70 deletions

View File

@ -114,7 +114,6 @@ import java.security.SignatureException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.Vector;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
@ -608,12 +607,12 @@ public class PGPMain {
return returnData; return returnData;
} }
public static Bundle exportKeyRings(Context context, Vector<Integer> keyRingIds, public static Bundle exportKeyRings(Context context, ArrayList<Long> keyRingRowIds,
OutputStream outStream, ProgressDialogUpdater progress) throws ApgGeneralException, OutputStream outStream, ProgressDialogUpdater progress) throws ApgGeneralException,
FileNotFoundException, PGPException, IOException { FileNotFoundException, PGPException, IOException {
Bundle returnData = new Bundle(); Bundle returnData = new Bundle();
if (keyRingIds.size() == 1) { if (keyRingRowIds.size() == 1) {
updateProgress(progress, R.string.progress_exportingKey, 0, 100); updateProgress(progress, R.string.progress_exportingKey, 0, 100);
} else { } else {
updateProgress(progress, R.string.progress_exportingKeys, 0, 100); updateProgress(progress, R.string.progress_exportingKeys, 0, 100);
@ -626,17 +625,17 @@ public class PGPMain {
out.setHeader("Version", getFullVersion(context)); out.setHeader("Version", getFullVersion(context));
int numKeys = 0; int numKeys = 0;
for (int i = 0; i < keyRingIds.size(); ++i) { for (int i = 0; i < keyRingRowIds.size(); ++i) {
updateProgress(progress, i * 100 / keyRingIds.size(), 100); updateProgress(progress, i * 100 / keyRingRowIds.size(), 100);
// try to get it as a PGPPublicKeyRing, if that fails try to get it as a SecretKeyRing // try to get it as a PGPPublicKeyRing, if that fails try to get it as a SecretKeyRing
PGPPublicKeyRing publicKeyRing = ProviderHelper.getPGPPublicKeyRingByMasterKeyId( PGPPublicKeyRing publicKeyRing = ProviderHelper.getPGPPublicKeyRingByRowId(context,
context, keyRingIds.get(i)); keyRingRowIds.get(i));
if (publicKeyRing != null) { if (publicKeyRing != null) {
publicKeyRing.encode(out); publicKeyRing.encode(out);
} else { } else {
PGPSecretKeyRing secretKeyRing = ProviderHelper.getPGPSecretKeyRingByMasterKeyId( PGPSecretKeyRing secretKeyRing = ProviderHelper.getPGPSecretKeyRingByRowId(context,
context, keyRingIds.get(i)); keyRingRowIds.get(i));
if (secretKeyRing != null) { if (secretKeyRing != null) {
secretKeyRing.encode(out); secretKeyRing.encode(out);
} else { } else {

View File

@ -19,7 +19,6 @@ package org.thialfihar.android.apg.provider;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Vector;
import org.spongycastle.openpgp.PGPKeyRing; import org.spongycastle.openpgp.PGPKeyRing;
import org.spongycastle.openpgp.PGPPublicKey; import org.spongycastle.openpgp.PGPPublicKey;
@ -232,7 +231,8 @@ public class ProviderHelper {
} }
try { try {
context.getContentResolver().applyBatch(ApgContract.CONTENT_AUTHORITY_INTERNAL, operations); context.getContentResolver().applyBatch(ApgContract.CONTENT_AUTHORITY_INTERNAL,
operations);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(Constants.TAG, "applyBatch failed!", e); Log.e(Constants.TAG, "applyBatch failed!", e);
} catch (OperationApplicationException e) { } catch (OperationApplicationException e) {
@ -288,7 +288,8 @@ public class ProviderHelper {
} }
try { try {
context.getContentResolver().applyBatch(ApgContract.CONTENT_AUTHORITY_INTERNAL, operations); context.getContentResolver().applyBatch(ApgContract.CONTENT_AUTHORITY_INTERNAL,
operations);
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(Constants.TAG, "applyBatch failed!", e); Log.e(Constants.TAG, "applyBatch failed!", e);
} catch (OperationApplicationException e) { } catch (OperationApplicationException e) {
@ -415,16 +416,16 @@ public class ProviderHelper {
* @param queryUri * @param queryUri
* @return * @return
*/ */
private static Vector<Integer> getKeyRingsRowIds(Context context, Uri queryUri) { private static ArrayList<Long> getKeyRingsRowIds(Context context, Uri queryUri) {
Cursor cursor = context.getContentResolver().query(queryUri, new String[] { KeyRings._ID }, Cursor cursor = context.getContentResolver().query(queryUri, new String[] { KeyRings._ID },
null, null, null); null, null, null);
Vector<Integer> keyIds = new Vector<Integer>(); ArrayList<Long> keyIds = new ArrayList<Long>();
if (cursor != null) { if (cursor != null) {
int idCol = cursor.getColumnIndex(KeyRings._ID); int idCol = cursor.getColumnIndex(KeyRings._ID);
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
keyIds.add(cursor.getInt(idCol)); keyIds.add(cursor.getLong(idCol));
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
} }
@ -442,7 +443,7 @@ public class ProviderHelper {
* @param context * @param context
* @return * @return
*/ */
public static Vector<Integer> getSecretKeyRingsRowIds(Context context) { public static ArrayList<Long> getSecretKeyRingsRowIds(Context context) {
Uri queryUri = KeyRings.buildSecretKeyRingsUri(); Uri queryUri = KeyRings.buildSecretKeyRingsUri();
return getKeyRingsRowIds(context, queryUri); return getKeyRingsRowIds(context, queryUri);
} }
@ -453,7 +454,7 @@ public class ProviderHelper {
* @param context * @param context
* @return * @return
*/ */
public static Vector<Integer> getPublicKeyRingsRowIds(Context context) { public static ArrayList<Long> getPublicKeyRingsRowIds(Context context) {
Uri queryUri = KeyRings.buildPublicKeyRingsUri(); Uri queryUri = KeyRings.buildPublicKeyRingsUri();
return getKeyRingsRowIds(context, queryUri); return getKeyRingsRowIds(context, queryUri);
} }

View File

@ -26,7 +26,6 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Vector;
import org.spongycastle.openpgp.PGPPublicKeyRing; import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPSecretKey; import org.spongycastle.openpgp.PGPSecretKey;
@ -145,7 +144,7 @@ public class ApgIntentService extends IntentService implements ProgressDialogUpd
public static final String EXPORT_FILENAME = "exportFilename"; public static final String EXPORT_FILENAME = "exportFilename";
public static final String EXPORT_KEY_TYPE = "exportKeyType"; public static final String EXPORT_KEY_TYPE = "exportKeyType";
public static final String EXPORT_ALL = "exportAll"; public static final String EXPORT_ALL = "exportAll";
public static final String EXPORT_KEY_RING_ID = "exportKeyRingId"; public static final String EXPORT_KEY_RING_ROW_ID = "exportKeyRingId";
// upload key // upload key
public static final String UPLOAD_KEY_SERVER = "uploadKeyServer"; public static final String UPLOAD_KEY_SERVER = "uploadKeyServer";
@ -692,9 +691,9 @@ public class ApgIntentService extends IntentService implements ProgressDialogUpd
String outputFile = data.getString(EXPORT_FILENAME); String outputFile = data.getString(EXPORT_FILENAME);
boolean exportAll = data.getBoolean(EXPORT_ALL); boolean exportAll = data.getBoolean(EXPORT_ALL);
int keyRingId = -1; long keyRingRowId = -1;
if (!exportAll) { if (!exportAll) {
keyRingId = data.getInt(EXPORT_KEY_RING_ID); keyRingRowId = data.getLong(EXPORT_KEY_RING_ROW_ID);
} }
/* Operation */ /* Operation */
@ -707,19 +706,21 @@ public class ApgIntentService extends IntentService implements ProgressDialogUpd
// OutputStream // OutputStream
FileOutputStream outStream = new FileOutputStream(outputFile); FileOutputStream outStream = new FileOutputStream(outputFile);
Vector<Integer> keyRingIds = new Vector<Integer>(); ArrayList<Long> keyRingRowIds = new ArrayList<Long>();
if (exportAll) { if (exportAll) {
// get all key ring row ids based on export type
if (keyType == Id.type.public_key) { if (keyType == Id.type.public_key) {
keyRingIds = ProviderHelper.getPublicKeyRingsRowIds(this); keyRingRowIds = ProviderHelper.getPublicKeyRingsRowIds(this);
} else { } else {
keyRingIds = ProviderHelper.getSecretKeyRingsRowIds(this); keyRingRowIds = ProviderHelper.getSecretKeyRingsRowIds(this);
} }
} else { } else {
keyRingIds.add(keyRingId); keyRingRowIds.add(keyRingRowId);
} }
Bundle resultData = new Bundle(); Bundle resultData = new Bundle();
resultData = PGPMain.exportKeyRings(this, keyRingIds, outStream, this); resultData = PGPMain.exportKeyRings(this, keyRingRowIds, outStream, this);
sendMessageToHandler(ApgIntentServiceHandler.MESSAGE_OKAY, resultData); sendMessageToHandler(ApgIntentServiceHandler.MESSAGE_OKAY, resultData);
} catch (Exception e) { } catch (Exception e) {

View File

@ -69,22 +69,37 @@ public class KeyListActivity extends SherlockFragmentActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setHomeButtonEnabled(true);
handleIntent(getIntent()); handleActions(getIntent());
} }
@Override // TODO: needed?
protected void onNewIntent(Intent intent) { // @Override
super.onNewIntent(intent); // protected void onNewIntent(Intent intent) {
handleIntent(intent); // super.onNewIntent(intent);
} // handleActions(intent);
// }
protected void handleIntent(Intent intent) { protected void handleActions(Intent intent) {
String action = intent.getAction();
Bundle extras = intent.getExtras();
if (extras == null) {
extras = new Bundle();
}
/**
* Android Standard Actions
*/
String searchString = null; String searchString = null;
if (Intent.ACTION_SEARCH.equals(intent.getAction())) { if (Intent.ACTION_SEARCH.equals(action)) {
searchString = intent.getStringExtra(SearchManager.QUERY); searchString = extras.getString(SearchManager.QUERY);
if (searchString != null && searchString.trim().length() == 0) { if (searchString != null && searchString.trim().length() == 0) {
searchString = null; searchString = null;
} }
} else if (Intent.ACTION_VIEW.equals(action)) {
// Android's Action when opening file associated to APG (see AndroidManifest.xml)
// override action to delegate it to APGs ACTION_IMPORT
action = ACTION_IMPORT;
} }
// if (searchString == null) { // if (searchString == null) {
@ -100,35 +115,19 @@ public class KeyListActivity extends SherlockFragmentActivity {
// mListAdapter = new KeyListAdapter(this, searchString); // mListAdapter = new KeyListAdapter(this, searchString);
// mList.setAdapter(mListAdapter); // mList.setAdapter(mListAdapter);
// Get intent, action /**
// Intent intent = getIntent(); * APG's own Actions
String action = intent.getAction(); */
if (ACTION_IMPORT.equals(action)) {
if (Intent.ACTION_VIEW.equals(action)) { if ("file".equals(intent.getScheme()) && intent.getDataString() != null) {
// Android's Action when opening file associated to APG (see AndroidManifest.xml) mImportFilename = intent.getData().getPath();
} else {
handleActionImport(intent); mImportData = intent.getStringExtra(EXTRA_TEXT);
} else if (ACTION_IMPORT.equals(action)) { }
// APG's own Actions importKeys();
handleActionImport(intent);
} }
} }
/**
* Handles import action
*
* @param intent
*/
private void handleActionImport(Intent intent) {
if ("file".equals(intent.getScheme()) && intent.getDataString() != null) {
mImportFilename = intent.getData().getPath();
} else {
mImportData = intent.getStringExtra(EXTRA_TEXT);
}
importKeys();
}
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) { switch (requestCode) {
@ -230,12 +229,12 @@ public class KeyListActivity extends SherlockFragmentActivity {
/** /**
* Show dialog where to export keys * Show dialog where to export keys
* *
* @param keyRingId * @param keyRingRowId
* if -1 export all keys * if -1 export all keys
*/ */
public void showExportKeysDialog(final long keyRingId) { public void showExportKeysDialog(final long keyRingRowId) {
String title = null; String title = null;
if (keyRingId != -1) { if (keyRingRowId != -1) {
// single key export // single key export
title = getString(R.string.title_exportKey); title = getString(R.string.title_exportKey);
} else { } else {
@ -257,7 +256,7 @@ public class KeyListActivity extends SherlockFragmentActivity {
Bundle data = message.getData(); Bundle data = message.getData();
mExportFilename = data.getString(FileDialogFragment.MESSAGE_DATA_FILENAME); mExportFilename = data.getString(FileDialogFragment.MESSAGE_DATA_FILENAME);
exportKeys(keyRingId); exportKeys(keyRingRowId);
} }
} }
}; };
@ -389,10 +388,10 @@ public class KeyListActivity extends SherlockFragmentActivity {
/** /**
* Export keys * Export keys
* *
* @param keyRingId * @param keyRingRowId
* if -1 export all keys * if -1 export all keys
*/ */
public void exportKeys(long keyRingId) { public void exportKeys(long keyRingRowId) {
Log.d(Constants.TAG, "exportKeys started"); Log.d(Constants.TAG, "exportKeys started");
// Send all information needed to service to export key in other thread // Send all information needed to service to export key in other thread
@ -406,10 +405,10 @@ public class KeyListActivity extends SherlockFragmentActivity {
data.putString(ApgIntentService.EXPORT_FILENAME, mExportFilename); data.putString(ApgIntentService.EXPORT_FILENAME, mExportFilename);
data.putInt(ApgIntentService.EXPORT_KEY_TYPE, mKeyType); data.putInt(ApgIntentService.EXPORT_KEY_TYPE, mKeyType);
if (keyRingId == -1) { if (keyRingRowId == -1) {
data.putBoolean(ApgIntentService.EXPORT_ALL, true); data.putBoolean(ApgIntentService.EXPORT_ALL, true);
} else { } else {
data.putLong(ApgIntentService.EXPORT_KEY_RING_ID, keyRingId); data.putLong(ApgIntentService.EXPORT_KEY_RING_ROW_ID, keyRingRowId);
} }
intent.putExtra(ApgIntentService.EXTRA_DATA, data); intent.putExtra(ApgIntentService.EXTRA_DATA, data);

View File

@ -89,7 +89,7 @@ public class KeyListPublicActivity extends KeyListActivity {
intent.setAction(KeyListPublicActivity.ACTION_IMPORT); intent.setAction(KeyListPublicActivity.ACTION_IMPORT);
intent.putExtra(KeyListPublicActivity.EXTRA_TEXT, intent.putExtra(KeyListPublicActivity.EXTRA_TEXT,
data.getStringExtra(KeyListActivity.EXTRA_TEXT)); data.getStringExtra(KeyListActivity.EXTRA_TEXT));
handleIntent(intent); handleActions(intent);
break; break;
} }