From 6096f7e8ca37f47f6ee110991770d0451cecc575 Mon Sep 17 00:00:00 2001 From: Dominik Date: Mon, 10 Sep 2012 15:02:52 +0200 Subject: [PATCH] key server upload activity refactored --- org_apg/AndroidManifest.xml | 2 +- .../android/apg/service/ApgService.java | 39 ++++ .../apg/ui/ImportFromQRCodeActivity.java | 169 +++++++++++------- ...vity.java => KeyServerUploadActivity.java} | 89 ++++----- .../android/apg/ui/PublicKeyListActivity.java | 6 +- .../android/apg/util/HkpKeyServer.java | 2 - 6 files changed, 191 insertions(+), 116 deletions(-) rename org_apg/src/org/thialfihar/android/apg/ui/{KeyServerExportActivity.java => KeyServerUploadActivity.java} (56%) diff --git a/org_apg/AndroidManifest.xml b/org_apg/AndroidManifest.xml index 4b38560ae..2a1e0bc4f 100644 --- a/org_apg/AndroidManifest.xml +++ b/org_apg/AndroidManifest.xml @@ -209,7 +209,7 @@ android:configChanges="keyboardHidden|orientation|keyboard" android:label="@string/title_keyServerQuery" /> 0 && updated > 0) { - toastMessage = getString(R.string.keysAddedAndUpdated, added, updated); - } else if (added > 0) { - toastMessage = getString(R.string.keysAdded, added); - } else if (updated > 0) { - toastMessage = getString(R.string.keysUpdated, updated); - } else { - toastMessage = getString(R.string.noKeysAddedOrUpdated); + int added = returnData.getInt(ApgService.RESULT_IMPORT_ADDED); + int updated = returnData.getInt(ApgService.RESULT_IMPORT_UPDATED); + int bad = returnData.getInt(ApgService.RESULT_IMPORT_BAD); + String toastMessage; + if (added > 0 && updated > 0) { + toastMessage = getString(R.string.keysAddedAndUpdated, added, updated); + } else if (added > 0) { + toastMessage = getString(R.string.keysAdded, added); + } else if (updated > 0) { + toastMessage = getString(R.string.keysUpdated, updated); + } else { + 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 - Messenger messenger = new Messenger(saveHandler); - intent.putExtra(ApgService.EXTRA_MESSENGER, messenger); + // Create a new Messenger for the communication back + Messenger messenger = new Messenger(saveHandler); + intent.putExtra(ApgService.EXTRA_MESSENGER, messenger); - // show progress dialog - importingDialog.show(getSupportFragmentManager(), "importingDialog"); + // show progress dialog + importingDialog.show(getSupportFragmentManager(), "importingDialog"); - // start service with intent - startService(intent); + // start service with intent + startService(intent); + } } public void signAndUploadOnClick(View view) { @@ -221,13 +261,9 @@ public class ImportFromQRCodeActivity extends SherlockFragmentActivity { data); 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(); - contentView.setText(mScannedContent); + mContentView.setText(mScannedContent); // String[] bits = scanResult.getContents().split(","); // if (bits.length != 2) { // return; // dont know how to handle this. Not a valid code @@ -237,7 +273,6 @@ public class ImportFromQRCodeActivity extends SherlockFragmentActivity { // String expectedFingerprint = bits[1]; // importAndSign(keyId, expectedFingerprint); - } break; diff --git a/org_apg/src/org/thialfihar/android/apg/ui/KeyServerExportActivity.java b/org_apg/src/org/thialfihar/android/apg/ui/KeyServerUploadActivity.java similarity index 56% rename from org_apg/src/org/thialfihar/android/apg/ui/KeyServerExportActivity.java rename to org_apg/src/org/thialfihar/android/apg/ui/KeyServerUploadActivity.java index 33f222644..4957480b7 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/KeyServerExportActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/KeyServerUploadActivity.java @@ -17,19 +17,21 @@ 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.Id; import org.thialfihar.android.apg.R; -import org.thialfihar.android.apg.helper.PGPMain; -import org.thialfihar.android.apg.util.HkpKeyServer; +import org.thialfihar.android.apg.helper.Preferences; +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 android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.os.Message; +import android.os.Messenger; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; @@ -42,7 +44,7 @@ import android.widget.Toast; * * 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! 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"; - // TODO: remove when using new intentservice: - public static final String EXTRA_ERROR = "error"; - private Button export; private Spinner keyServer; @@ -84,7 +83,8 @@ public class KeyServerExportActivity extends BaseActivity { keyServer = (Spinner) findViewById(R.id.keyServer); ArrayAdapter adapter = new ArrayAdapter(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); keyServer.setAdapter(adapter); if (adapter.getCount() > 0) { @@ -96,52 +96,55 @@ public class KeyServerExportActivity extends BaseActivity { export.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - startThread(); + uploadKey(); } }); } - @Override - public void run() { - String error = null; - Bundle data = new Bundle(); - Message msg = new Message(); + private void uploadKey() { + // Send all information needed to service to upload key in other thread + Intent intent = new Intent(this, ApgService.class); - 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); + data.putInt(ApgService.UPLOAD_KEY_KEYRING_ID, keyRingId); - PGPKeyRing keyring = PGPMain.getKeyRing(keyRingId); - if (keyring != null && keyring instanceof PGPPublicKeyRing) { - boolean uploaded = PGPMain.uploadKeyRingToServer(server, (PGPPublicKeyRing) keyring); - if (!uploaded) { - error = "Unable to export key to selected server"; - } - } + String server = (String) keyServer.getSelectedItem(); + data.putString(ApgService.UPLOAD_KEY_SERVER, server); - data.putInt(Constants.extras.STATUS, Id.message.export_done); + intent.putExtra(ApgService.EXTRA_DATA, data); - if (error != null) { - data.putString(EXTRA_ERROR, error); - } + // create progress dialog + ProgressDialogFragment uploadingDialog = ProgressDialogFragment.newInstance( + R.string.progress_importing, ProgressDialog.STYLE_HORIZONTAL); - msg.setData(data); - sendMessage(msg); - } + // Message is received after uploading is done in ApgService + ApgHandler saveHandler = new ApgHandler(this, uploadingDialog) { + public void handleMessage(Message message) { + // handle messages by standard ApgHandler first + super.handleMessage(message); - @Override - public void doneCallback(Message msg) { - super.doneCallback(msg); + if (message.arg1 == ApgHandler.MESSAGE_OKAY) { - Bundle data = msg.getData(); - String error = data.getString(EXTRA_ERROR); - if (error != null) { - Toast.makeText(this, getString(R.string.errorMessage, error), Toast.LENGTH_SHORT) - .show(); - return; - } + Toast.makeText(KeyServerUploadActivity.this, R.string.keySendSuccess, + Toast.LENGTH_SHORT).show(); + finish(); + } + }; + }; - Toast.makeText(this, R.string.keySendSuccess, Toast.LENGTH_SHORT).show(); - finish(); + // Create a new Messenger for the communication back + Messenger messenger = new Messenger(saveHandler); + intent.putExtra(ApgService.EXTRA_MESSENGER, messenger); + + // show progress dialog + uploadingDialog.show(getSupportFragmentManager(), "uploadingDialog"); + + // start service with intent + startService(intent); } } diff --git a/org_apg/src/org/thialfihar/android/apg/ui/PublicKeyListActivity.java b/org_apg/src/org/thialfihar/android/apg/ui/PublicKeyListActivity.java index d5b05b64e..464706191 100644 --- a/org_apg/src/org/thialfihar/android/apg/ui/PublicKeyListActivity.java +++ b/org_apg/src/org/thialfihar/android/apg/ui/PublicKeyListActivity.java @@ -136,9 +136,9 @@ public class PublicKeyListActivity extends KeyListActivity { mSelectedItem = groupPosition; final int keyRingId = mListAdapter.getKeyRingId(groupPosition); - Intent intent = new Intent(this, KeyServerExportActivity.class); - intent.setAction(KeyServerExportActivity.ACTION_EXPORT_KEY_TO_SERVER); - intent.putExtra(KeyServerExportActivity.EXTRA_KEY_ID, keyRingId); + Intent intent = new Intent(this, KeyServerUploadActivity.class); + intent.setAction(KeyServerUploadActivity.ACTION_EXPORT_KEY_TO_SERVER); + intent.putExtra(KeyServerUploadActivity.EXTRA_KEY_ID, keyRingId); startActivityForResult(intent, Id.request.export_to_server); return true; diff --git a/org_apg/src/org/thialfihar/android/apg/util/HkpKeyServer.java b/org_apg/src/org/thialfihar/android/apg/util/HkpKeyServer.java index bdef7b59e..0f267850c 100644 --- a/org_apg/src/org/thialfihar/android/apg/util/HkpKeyServer.java +++ b/org_apg/src/org/thialfihar/android/apg/util/HkpKeyServer.java @@ -108,7 +108,6 @@ public class HkpKeyServer extends KeyServer { return raw.toString(encoding); } - // TODO: replace this with httpclient private String query(String request) throws QueryException, HttpError { InetAddress ips[]; try { @@ -141,7 +140,6 @@ public class HkpKeyServer extends KeyServer { throw new QueryException("querying server(s) for '" + mHost + "' failed"); } - // TODO: replace this with httpclient @Override public List search(String query) throws QueryException, TooManyResponses, InsufficientQuery {