key server upload activity refactored

This commit is contained in:
Dominik 2012-09-10 15:02:52 +02:00
parent 018a84c8d6
commit 6096f7e8ca
6 changed files with 191 additions and 116 deletions

View File

@ -209,7 +209,7 @@
android:configChanges="keyboardHidden|orientation|keyboard" android:configChanges="keyboardHidden|orientation|keyboard"
android:label="@string/title_keyServerQuery" /> android:label="@string/title_keyServerQuery" />
<activity <activity
android:name=".ui.KeyServerExportActivity" android:name=".ui.KeyServerUploadActivity"
android:configChanges="keyboardHidden|orientation|keyboard" android:configChanges="keyboardHidden|orientation|keyboard"
android:label="@string/title_sendKey" /> android:label="@string/title_sendKey" />
<activity <activity

View File

@ -28,6 +28,8 @@ import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Vector; import java.util.Vector;
import org.spongycastle.openpgp.PGPKeyRing;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPSecretKey; import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSecretKeyRing; import org.spongycastle.openpgp.PGPSecretKeyRing;
import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Constants;
@ -40,6 +42,7 @@ import org.thialfihar.android.apg.helper.Preferences;
import org.thialfihar.android.apg.helper.PGPMain.GeneralException; import org.thialfihar.android.apg.helper.PGPMain.GeneralException;
import org.thialfihar.android.apg.helper.PGPConversionHelper; import org.thialfihar.android.apg.helper.PGPConversionHelper;
import org.thialfihar.android.apg.provider.DataProvider; import org.thialfihar.android.apg.provider.DataProvider;
import org.thialfihar.android.apg.util.HkpKeyServer;
import org.thialfihar.android.apg.util.InputData; import org.thialfihar.android.apg.util.InputData;
import org.thialfihar.android.apg.util.ProgressDialogUpdater; import org.thialfihar.android.apg.util.ProgressDialogUpdater;
@ -124,6 +127,10 @@ public class ApgService extends IntentService implements ProgressDialogUpdater {
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_ID = "exportKeyRingId";
// upload key
public static final String UPLOAD_KEY_SERVER = "uploadKeyServer";
public static final String UPLOAD_KEY_KEYRING_ID = "uploadKeyRingId";
/* possible EXTRA_ACTIONs */ /* possible EXTRA_ACTIONs */
public static final int ACTION_ENCRYPT_SIGN = 10; public static final int ACTION_ENCRYPT_SIGN = 10;
@ -138,6 +145,8 @@ public class ApgService extends IntentService implements ProgressDialogUpdater {
public static final int ACTION_IMPORT_KEY = 50; public static final int ACTION_IMPORT_KEY = 50;
public static final int ACTION_EXPORT_KEY = 51; public static final int ACTION_EXPORT_KEY = 51;
public static final int ACTION_UPLOAD_KEY = 60;
/* possible data keys as result send over messenger */ /* possible data keys as result send over messenger */
// keys // keys
public static final String RESULT_NEW_KEY = "newKey"; public static final String RESULT_NEW_KEY = "newKey";
@ -700,6 +709,36 @@ public class ApgService extends IntentService implements ProgressDialogUpdater {
break; break;
case ACTION_UPLOAD_KEY:
try {
/* Input */
int keyRingId = data.getInt(UPLOAD_KEY_KEYRING_ID);
String keyServer = data.getString(UPLOAD_KEY_SERVER);
/* Operation */
HkpKeyServer server = new HkpKeyServer(keyServer);
PGPKeyRing keyring = PGPMain.getKeyRing(keyRingId);
if (keyring != null && keyring instanceof PGPPublicKeyRing) {
boolean uploaded = PGPMain.uploadKeyRingToServer(server,
(PGPPublicKeyRing) keyring);
if (!uploaded) {
sendErrorToHandler(new GeneralException(
"Unable to export key to selected server"));
return;
}
}
sendMessageToHandler(ApgHandler.MESSAGE_OKAY);
} catch (Exception e) {
sendErrorToHandler(e);
}
break;
default: default:
break; break;
} }

View File

@ -18,6 +18,7 @@
package org.thialfihar.android.apg.ui; package org.thialfihar.android.apg.ui;
import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.Id;
import org.thialfihar.android.apg.service.ApgHandler; import org.thialfihar.android.apg.service.ApgHandler;
import org.thialfihar.android.apg.service.ApgService; import org.thialfihar.android.apg.service.ApgService;
import org.thialfihar.android.apg.ui.dialog.ProgressDialogFragment; import org.thialfihar.android.apg.ui.dialog.ProgressDialogFragment;
@ -37,7 +38,9 @@ import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult; import com.google.zxing.integration.android.IntentResult;
@ -49,15 +52,50 @@ public class ImportFromQRCodeActivity extends SherlockFragmentActivity {
// public static final String EXTRA_KEY_ID = "keyId"; // public static final String EXTRA_KEY_ID = "keyId";
private TextView mContentView;
private String mScannedContent; private String mScannedContent;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.import_from_qr_code);
mContentView = (TextView) findViewById(R.id.import_from_qr_code_content);
// set actionbar without home button if called from another app
final ActionBar actionBar = getSupportActionBar();
Log.d(Constants.TAG, "calling package (only set when using startActivityForResult)="
+ getCallingPackage());
if (getCallingPackage() != null && getCallingPackage().equals(Constants.PACKAGE_NAME)) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
} else {
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(false);
}
// start scanning
new IntentIntegrator(this).initiateScan(); new IntentIntegrator(this).initiateScan();
} }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in Action Bar clicked; go home
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default: {
return super.onOptionsItemSelected(item);
}
}
}
// private void importAndSignOld(final long keyId, final String expectedFingerprint) { // private void importAndSignOld(final long keyId, final String expectedFingerprint) {
// if (expectedFingerprint != null && expectedFingerprint.length() > 0) { // if (expectedFingerprint != null && expectedFingerprint.length() > 0) {
// //
@ -127,81 +165,83 @@ public class ImportFromQRCodeActivity extends SherlockFragmentActivity {
public void importOnClick(View view) { public void importOnClick(View view) {
Log.d(Constants.TAG, "import key started"); Log.d(Constants.TAG, "import key started");
// Send all information needed to service to import key in other thread if (mScannedContent != null) {
Intent intent = new Intent(this, ApgService.class); // Send all information needed to service to import key in other thread
Intent intent = new Intent(this, ApgService.class);
intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_IMPORT_KEY); intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_IMPORT_KEY);
// fill values for this action // fill values for this action
Bundle data = new Bundle(); Bundle data = new Bundle();
// data.putInt(ApgService.IMPORT_KEY_TYPE, Id.type.public_key); data.putInt(ApgService.IMPORT_KEY_TYPE, Id.type.public_key);
data.putInt(ApgService.TARGET, ApgService.TARGET_BYTES); data.putInt(ApgService.TARGET, ApgService.TARGET_BYTES);
data.putByteArray(ApgService.IMPORT_BYTES, mScannedContent.getBytes()); data.putByteArray(ApgService.IMPORT_BYTES, mScannedContent.getBytes());
intent.putExtra(ApgService.EXTRA_DATA, data); intent.putExtra(ApgService.EXTRA_DATA, data);
// create progress dialog // create progress dialog
ProgressDialogFragment importingDialog = ProgressDialogFragment.newInstance( ProgressDialogFragment importingDialog = ProgressDialogFragment.newInstance(
R.string.progress_importing, ProgressDialog.STYLE_HORIZONTAL); R.string.progress_importing, ProgressDialog.STYLE_HORIZONTAL);
// Message is received after importing is done in ApgService // Message is received after importing is done in ApgService
ApgHandler saveHandler = new ApgHandler(this, importingDialog) { ApgHandler saveHandler = new ApgHandler(this, importingDialog) {
public void handleMessage(Message message) { public void handleMessage(Message message) {
// handle messages by standard ApgHandler first // handle messages by standard ApgHandler first
super.handleMessage(message); super.handleMessage(message);
if (message.arg1 == ApgHandler.MESSAGE_OKAY) { if (message.arg1 == ApgHandler.MESSAGE_OKAY) {
// get returned data bundle // get returned data bundle
Bundle returnData = message.getData(); Bundle returnData = message.getData();
int added = returnData.getInt(ApgService.RESULT_IMPORT_ADDED); int added = returnData.getInt(ApgService.RESULT_IMPORT_ADDED);
int updated = returnData.getInt(ApgService.RESULT_IMPORT_UPDATED); int updated = returnData.getInt(ApgService.RESULT_IMPORT_UPDATED);
int bad = returnData.getInt(ApgService.RESULT_IMPORT_BAD); int bad = returnData.getInt(ApgService.RESULT_IMPORT_BAD);
String toastMessage; String toastMessage;
if (added > 0 && updated > 0) { if (added > 0 && updated > 0) {
toastMessage = getString(R.string.keysAddedAndUpdated, added, updated); toastMessage = getString(R.string.keysAddedAndUpdated, added, updated);
} else if (added > 0) { } else if (added > 0) {
toastMessage = getString(R.string.keysAdded, added); toastMessage = getString(R.string.keysAdded, added);
} else if (updated > 0) { } else if (updated > 0) {
toastMessage = getString(R.string.keysUpdated, updated); toastMessage = getString(R.string.keysUpdated, updated);
} else { } else {
toastMessage = getString(R.string.noKeysAddedOrUpdated); toastMessage = getString(R.string.noKeysAddedOrUpdated);
}
Toast.makeText(ImportFromQRCodeActivity.this, toastMessage,
Toast.LENGTH_SHORT).show();
if (bad > 0) {
AlertDialog.Builder alert = new AlertDialog.Builder(
ImportFromQRCodeActivity.this);
alert.setIcon(android.R.drawable.ic_dialog_alert);
alert.setTitle(R.string.warning);
alert.setMessage(ImportFromQRCodeActivity.this.getString(
R.string.badKeysEncountered, bad));
alert.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alert.setCancelable(true);
alert.create().show();
}
} }
Toast.makeText(ImportFromQRCodeActivity.this, toastMessage, Toast.LENGTH_SHORT) };
.show();
if (bad > 0) {
AlertDialog.Builder alert = new AlertDialog.Builder(
ImportFromQRCodeActivity.this);
alert.setIcon(android.R.drawable.ic_dialog_alert);
alert.setTitle(R.string.warning);
alert.setMessage(ImportFromQRCodeActivity.this.getString(
R.string.badKeysEncountered, bad));
alert.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alert.setCancelable(true);
alert.create().show();
}
}
}; };
};
// Create a new Messenger for the communication back // Create a new Messenger for the communication back
Messenger messenger = new Messenger(saveHandler); Messenger messenger = new Messenger(saveHandler);
intent.putExtra(ApgService.EXTRA_MESSENGER, messenger); intent.putExtra(ApgService.EXTRA_MESSENGER, messenger);
// show progress dialog // show progress dialog
importingDialog.show(getSupportFragmentManager(), "importingDialog"); importingDialog.show(getSupportFragmentManager(), "importingDialog");
// start service with intent // start service with intent
startService(intent); startService(intent);
}
} }
public void signAndUploadOnClick(View view) { public void signAndUploadOnClick(View view) {
@ -221,13 +261,9 @@ public class ImportFromQRCodeActivity extends SherlockFragmentActivity {
data); data);
if (scanResult != null && scanResult.getFormatName() != null) { if (scanResult != null && scanResult.getFormatName() != null) {
// show layout
setContentView(R.layout.import_from_qr_code);
TextView contentView = (TextView) findViewById(R.id.import_from_qr_code_content);
mScannedContent = scanResult.getContents(); mScannedContent = scanResult.getContents();
contentView.setText(mScannedContent); mContentView.setText(mScannedContent);
// String[] bits = scanResult.getContents().split(","); // String[] bits = scanResult.getContents().split(",");
// if (bits.length != 2) { // if (bits.length != 2) {
// return; // dont know how to handle this. Not a valid code // return; // dont know how to handle this. Not a valid code
@ -237,7 +273,6 @@ public class ImportFromQRCodeActivity extends SherlockFragmentActivity {
// String expectedFingerprint = bits[1]; // String expectedFingerprint = bits[1];
// importAndSign(keyId, expectedFingerprint); // importAndSign(keyId, expectedFingerprint);
} }
break; break;

View File

@ -17,19 +17,21 @@
package org.thialfihar.android.apg.ui; package org.thialfihar.android.apg.ui;
import org.spongycastle.openpgp.PGPKeyRing;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.thialfihar.android.apg.Constants; import org.thialfihar.android.apg.Constants;
import org.thialfihar.android.apg.Id;
import org.thialfihar.android.apg.R; import org.thialfihar.android.apg.R;
import org.thialfihar.android.apg.helper.PGPMain; import org.thialfihar.android.apg.helper.Preferences;
import org.thialfihar.android.apg.util.HkpKeyServer; import org.thialfihar.android.apg.service.ApgHandler;
import org.thialfihar.android.apg.service.ApgService;
import org.thialfihar.android.apg.ui.dialog.ProgressDialogFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
import android.app.ProgressDialog;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Message; import android.os.Message;
import android.os.Messenger;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
@ -42,7 +44,7 @@ import android.widget.Toast;
* *
* Sends the selected public key to a key server * Sends the selected public key to a key server
*/ */
public class KeyServerExportActivity extends BaseActivity { public class KeyServerUploadActivity extends SherlockFragmentActivity {
// Not used in sourcode, but listed in AndroidManifest! // Not used in sourcode, but listed in AndroidManifest!
public static final String ACTION_EXPORT_KEY_TO_SERVER = Constants.INTENT_PREFIX public static final String ACTION_EXPORT_KEY_TO_SERVER = Constants.INTENT_PREFIX
@ -50,9 +52,6 @@ public class KeyServerExportActivity extends BaseActivity {
public static final String EXTRA_KEY_ID = "keyId"; public static final String EXTRA_KEY_ID = "keyId";
// TODO: remove when using new intentservice:
public static final String EXTRA_ERROR = "error";
private Button export; private Button export;
private Spinner keyServer; private Spinner keyServer;
@ -84,7 +83,8 @@ public class KeyServerExportActivity extends BaseActivity {
keyServer = (Spinner) findViewById(R.id.keyServer); keyServer = (Spinner) findViewById(R.id.keyServer);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, mPreferences.getKeyServers()); android.R.layout.simple_spinner_item, Preferences.getPreferences(this)
.getKeyServers());
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
keyServer.setAdapter(adapter); keyServer.setAdapter(adapter);
if (adapter.getCount() > 0) { if (adapter.getCount() > 0) {
@ -96,52 +96,55 @@ public class KeyServerExportActivity extends BaseActivity {
export.setOnClickListener(new OnClickListener() { export.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
startThread(); uploadKey();
} }
}); });
} }
@Override private void uploadKey() {
public void run() { // Send all information needed to service to upload key in other thread
String error = null; Intent intent = new Intent(this, ApgService.class);
Bundle data = new Bundle();
Message msg = new Message();
HkpKeyServer server = new HkpKeyServer((String) keyServer.getSelectedItem()); intent.putExtra(ApgService.EXTRA_ACTION, ApgService.ACTION_UPLOAD_KEY);
// fill values for this action
Bundle data = new Bundle();
int keyRingId = getIntent().getIntExtra(EXTRA_KEY_ID, -1); int keyRingId = getIntent().getIntExtra(EXTRA_KEY_ID, -1);
data.putInt(ApgService.UPLOAD_KEY_KEYRING_ID, keyRingId);
PGPKeyRing keyring = PGPMain.getKeyRing(keyRingId); String server = (String) keyServer.getSelectedItem();
if (keyring != null && keyring instanceof PGPPublicKeyRing) { data.putString(ApgService.UPLOAD_KEY_SERVER, server);
boolean uploaded = PGPMain.uploadKeyRingToServer(server, (PGPPublicKeyRing) keyring);
if (!uploaded) {
error = "Unable to export key to selected server";
}
}
data.putInt(Constants.extras.STATUS, Id.message.export_done); intent.putExtra(ApgService.EXTRA_DATA, data);
if (error != null) { // create progress dialog
data.putString(EXTRA_ERROR, error); ProgressDialogFragment uploadingDialog = ProgressDialogFragment.newInstance(
} R.string.progress_importing, ProgressDialog.STYLE_HORIZONTAL);
msg.setData(data); // Message is received after uploading is done in ApgService
sendMessage(msg); ApgHandler saveHandler = new ApgHandler(this, uploadingDialog) {
} public void handleMessage(Message message) {
// handle messages by standard ApgHandler first
super.handleMessage(message);
@Override if (message.arg1 == ApgHandler.MESSAGE_OKAY) {
public void doneCallback(Message msg) {
super.doneCallback(msg);
Bundle data = msg.getData(); Toast.makeText(KeyServerUploadActivity.this, R.string.keySendSuccess,
String error = data.getString(EXTRA_ERROR); Toast.LENGTH_SHORT).show();
if (error != null) { finish();
Toast.makeText(this, getString(R.string.errorMessage, error), Toast.LENGTH_SHORT) }
.show(); };
return; };
}
Toast.makeText(this, R.string.keySendSuccess, Toast.LENGTH_SHORT).show(); // Create a new Messenger for the communication back
finish(); Messenger messenger = new Messenger(saveHandler);
intent.putExtra(ApgService.EXTRA_MESSENGER, messenger);
// show progress dialog
uploadingDialog.show(getSupportFragmentManager(), "uploadingDialog");
// start service with intent
startService(intent);
} }
} }

View File

@ -136,9 +136,9 @@ public class PublicKeyListActivity extends KeyListActivity {
mSelectedItem = groupPosition; mSelectedItem = groupPosition;
final int keyRingId = mListAdapter.getKeyRingId(groupPosition); final int keyRingId = mListAdapter.getKeyRingId(groupPosition);
Intent intent = new Intent(this, KeyServerExportActivity.class); Intent intent = new Intent(this, KeyServerUploadActivity.class);
intent.setAction(KeyServerExportActivity.ACTION_EXPORT_KEY_TO_SERVER); intent.setAction(KeyServerUploadActivity.ACTION_EXPORT_KEY_TO_SERVER);
intent.putExtra(KeyServerExportActivity.EXTRA_KEY_ID, keyRingId); intent.putExtra(KeyServerUploadActivity.EXTRA_KEY_ID, keyRingId);
startActivityForResult(intent, Id.request.export_to_server); startActivityForResult(intent, Id.request.export_to_server);
return true; return true;

View File

@ -108,7 +108,6 @@ public class HkpKeyServer extends KeyServer {
return raw.toString(encoding); return raw.toString(encoding);
} }
// TODO: replace this with httpclient
private String query(String request) throws QueryException, HttpError { private String query(String request) throws QueryException, HttpError {
InetAddress ips[]; InetAddress ips[];
try { try {
@ -141,7 +140,6 @@ public class HkpKeyServer extends KeyServer {
throw new QueryException("querying server(s) for '" + mHost + "' failed"); throw new QueryException("querying server(s) for '" + mHost + "' failed");
} }
// TODO: replace this with httpclient
@Override @Override
public List<KeyInfo> search(String query) throws QueryException, TooManyResponses, public List<KeyInfo> search(String query) throws QueryException, TooManyResponses,
InsufficientQuery { InsufficientQuery {