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

View File

@ -28,6 +28,8 @@ import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Vector;
import org.spongycastle.openpgp.PGPKeyRing;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSecretKeyRing;
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.PGPConversionHelper;
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.ProgressDialogUpdater;
@ -124,6 +127,10 @@ public class ApgService extends IntentService implements ProgressDialogUpdater {
public static final String EXPORT_ALL = "exportAll";
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 */
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_EXPORT_KEY = 51;
public static final int ACTION_UPLOAD_KEY = 60;
/* possible data keys as result send over messenger */
// keys
public static final String RESULT_NEW_KEY = "newKey";
@ -700,6 +709,36 @@ public class ApgService extends IntentService implements ProgressDialogUpdater {
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:
break;
}

View File

@ -18,6 +18,7 @@
package org.thialfihar.android.apg.ui;
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.ApgService;
import org.thialfihar.android.apg.ui.dialog.ProgressDialogFragment;
@ -37,7 +38,9 @@ import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
@ -49,15 +52,50 @@ public class ImportFromQRCodeActivity extends SherlockFragmentActivity {
// public static final String EXTRA_KEY_ID = "keyId";
private TextView mContentView;
private String mScannedContent;
@Override
protected void onCreate(Bundle 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();
}
@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) {
// if (expectedFingerprint != null && expectedFingerprint.length() > 0) {
//
@ -127,6 +165,7 @@ public class ImportFromQRCodeActivity extends SherlockFragmentActivity {
public void importOnClick(View view) {
Log.d(Constants.TAG, "import key started");
if (mScannedContent != null) {
// Send all information needed to service to import key in other thread
Intent intent = new Intent(this, ApgService.class);
@ -135,7 +174,7 @@ public class ImportFromQRCodeActivity extends SherlockFragmentActivity {
// fill values for this action
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.putByteArray(ApgService.IMPORT_BYTES, mScannedContent.getBytes());
@ -169,8 +208,8 @@ public class ImportFromQRCodeActivity extends SherlockFragmentActivity {
} else {
toastMessage = getString(R.string.noKeysAddedOrUpdated);
}
Toast.makeText(ImportFromQRCodeActivity.this, toastMessage, Toast.LENGTH_SHORT)
.show();
Toast.makeText(ImportFromQRCodeActivity.this, toastMessage,
Toast.LENGTH_SHORT).show();
if (bad > 0) {
AlertDialog.Builder alert = new AlertDialog.Builder(
ImportFromQRCodeActivity.this);
@ -203,6 +242,7 @@ public class ImportFromQRCodeActivity extends SherlockFragmentActivity {
// start service with intent
startService(intent);
}
}
public void signAndUploadOnClick(View view) {
// first, import!
@ -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;

View File

@ -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<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);
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(this, R.string.keySendSuccess, Toast.LENGTH_SHORT).show();
Toast.makeText(KeyServerUploadActivity.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);
}
}

View File

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

View File

@ -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<KeyInfo> search(String query) throws QueryException, TooManyResponses,
InsufficientQuery {