Files
open-keychain/org_apg/src/org/apg/ui/EditKeyActivity.java

363 lines
12 KiB
Java
Raw Normal View History

2010-04-06 19:54:51 +00:00
/*
2012-04-12 19:44:00 +02:00
* Copyright (C) 2012 Dominik Schürmann <dominik@dominikschuermann.de>
2010-04-06 19:54:51 +00:00
* Copyright (C) 2010 Thialfihar <thi@thialfihar.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apg.ui;
import org.apg.Apg;
import org.apg.Constants;
import org.apg.Id;
import org.apg.provider.Database;
import org.apg.ui.widget.KeyEditor;
import org.apg.ui.widget.SectionView;
import org.apg.util.IterableIterator;
import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSecretKeyRing;
import org.apg.R;
2010-04-06 19:54:51 +00:00
2012-04-12 15:23:00 +02:00
import com.actionbarsherlock.app.ActionBar;
2012-03-11 17:33:40 +01:00
import com.actionbarsherlock.view.Menu;
2012-03-12 01:58:24 +01:00
import com.actionbarsherlock.view.MenuItem;
2012-03-11 17:33:40 +01:00
2010-04-06 19:54:51 +00:00
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
2012-04-12 19:44:00 +02:00
import android.widget.CheckBox;
import android.widget.CompoundButton;
2010-04-06 19:54:51 +00:00
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
2012-04-12 19:44:00 +02:00
import android.widget.CompoundButton.OnCheckedChangeListener;
2010-04-06 19:54:51 +00:00
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SignatureException;
import java.util.Vector;
2012-03-12 01:58:24 +01:00
public class EditKeyActivity extends BaseActivity {
2012-04-12 15:23:00 +02:00
private Intent mIntent = null;
2010-04-06 19:54:51 +00:00
private PGPSecretKeyRing mKeyRing = null;
private SectionView mUserIds;
private SectionView mKeys;
private String mCurrentPassPhrase = null;
2010-04-06 19:54:51 +00:00
private String mNewPassPhrase = null;
private Button mChangePassPhrase;
private CheckBox mNoPassphrase;
2012-03-12 01:58:24 +01:00
@Override
public boolean onCreateOptionsMenu(Menu menu) {
2012-04-12 15:23:00 +02:00
menu.add(1, Id.menu.option.cancel, 0, R.string.btn_doNotSave).setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(1, Id.menu.option.save, 1, R.string.btn_save).setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
2012-03-12 01:58:24 +01:00
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
startActivity(new Intent(this, SecretKeyListActivity.class));
return true;
case Id.menu.option.save:
saveClicked();
return true;
case Id.menu.option.cancel:
finish();
return true;
default:
break;
}
return false;
}
2010-04-06 19:54:51 +00:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_key);
Vector<String> userIds = new Vector<String>();
Vector<PGPSecretKey> keys = new Vector<PGPSecretKey>();
Intent intent = getIntent();
long keyId = 0;
if (intent.getExtras() != null) {
keyId = intent.getExtras().getLong(Apg.EXTRA_KEY_ID);
2010-04-06 19:54:51 +00:00
}
if (keyId != 0) {
2010-04-06 19:54:51 +00:00
PGPSecretKey masterKey = null;
mKeyRing = Apg.getSecretKeyRing(keyId);
if (mKeyRing != null) {
masterKey = Apg.getMasterKey(mKeyRing);
for (PGPSecretKey key : new IterableIterator<PGPSecretKey>(mKeyRing.getSecretKeys())) {
keys.add(key);
}
}
if (masterKey != null) {
for (String userId : new IterableIterator<String>(masterKey.getUserIDs())) {
userIds.add(userId);
}
}
}
2012-04-12 15:23:00 +02:00
// Catch Intents opened from other apps
mIntent = getIntent();
if (Apg.Intent.EDIT_KEY.equals(mIntent.getAction())) {
Bundle extras = mIntent.getExtras();
if (extras == null) {
extras = new Bundle();
}
// disable home button on actionbar because this activity is run from another app
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(false);
// if userId is given, prefill the fields
if (extras.containsKey(Apg.EXTRA_USER_IDS)) {
userIds.add(extras.getString(Apg.EXTRA_USER_IDS));
}
// if userId is given, prefill the fields
if (extras.containsKey(Apg.EXTRA_NO_PASSPHRASE)) {
boolean noPassphrase = extras.getBoolean(Apg.EXTRA_NO_PASSPHRASE);
if (noPassphrase) {
mCurrentPassPhrase = "";
}
}
2012-04-12 15:23:00 +02:00
}
2012-04-12 19:44:00 +02:00
mChangePassPhrase = (Button) findViewById(R.id.edit_key_btn_change_pass_phrase);
mChangePassPhrase.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(Id.dialog.new_pass_phrase);
}
});
2012-04-12 19:44:00 +02:00
// disable passphrase when no passphrase checkobox is checked!
mNoPassphrase = (CheckBox) findViewById(R.id.edit_key_no_passphrase);
mNoPassphrase.setOnCheckedChangeListener(new OnCheckedChangeListener() {
2012-04-12 19:44:00 +02:00
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// remove passphrase
mNewPassPhrase = null;
2012-04-12 19:44:00 +02:00
mChangePassPhrase.setVisibility(View.GONE);
} else {
mChangePassPhrase.setVisibility(View.VISIBLE);
}
}
});
2012-04-12 15:23:00 +02:00
// Build layout based on given userIds and keys
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2010-04-06 19:54:51 +00:00
2012-04-12 19:44:00 +02:00
LinearLayout container = (LinearLayout) findViewById(R.id.edit_key_container);
2010-04-06 19:54:51 +00:00
mUserIds = (SectionView) inflater.inflate(R.layout.edit_key_section, container, false);
mUserIds.setType(Id.type.user_id);
2010-04-06 19:54:51 +00:00
mUserIds.setUserIds(userIds);
container.addView(mUserIds);
mKeys = (SectionView) inflater.inflate(R.layout.edit_key_section, container, false);
mKeys.setType(Id.type.key);
2010-04-06 19:54:51 +00:00
mKeys.setKeys(keys);
container.addView(mKeys);
mCurrentPassPhrase = Apg.getEditPassPhrase();
if (mCurrentPassPhrase == null) {
mCurrentPassPhrase = "";
}
if (mCurrentPassPhrase.equals("")) {
// check "no passphrase" checkbox and remove button
mNoPassphrase.setChecked(true);
mChangePassPhrase.setVisibility(View.GONE);
}
updatePassPhraseButtonText();
2010-04-06 19:54:51 +00:00
}
private long getMasterKeyId() {
if (mKeys.getEditors().getChildCount() == 0) {
return 0;
}
return ((KeyEditor) mKeys.getEditors().getChildAt(0)).getValue().getKeyID();
}
public boolean isPassphraseSet() {
if (mNoPassphrase.isChecked()) {
return true;
} else if ((!mCurrentPassPhrase.equals(""))
|| (mNewPassPhrase != null && !mNewPassPhrase.equals(""))) {
return true;
} else {
return false;
}
2010-04-06 19:54:51 +00:00
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case Id.dialog.new_pass_phrase: {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
2010-04-06 19:54:51 +00:00
if (isPassphraseSet()) {
alert.setTitle(R.string.title_changePassPhrase);
} else {
alert.setTitle(R.string.title_setPassPhrase);
2010-04-06 19:54:51 +00:00
}
alert.setMessage(R.string.enterPassPhraseTwice);
2010-04-06 19:54:51 +00:00
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
2012-04-12 19:44:00 +02:00
View view = inflater.inflate(R.layout.passphrase, null);
final EditText input1 = (EditText) view.findViewById(R.id.passphrase_passphrase);
final EditText input2 = (EditText) view.findViewById(R.id.passphrase_passphrase_again);
alert.setView(view);
alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
removeDialog(Id.dialog.new_pass_phrase);
String passPhrase1 = "" + input1.getText();
String passPhrase2 = "" + input2.getText();
if (!passPhrase1.equals(passPhrase2)) {
showDialog(Id.dialog.pass_phrases_do_not_match);
return;
}
if (passPhrase1.equals("")) {
showDialog(Id.dialog.no_pass_phrase);
return;
}
mNewPassPhrase = passPhrase1;
updatePassPhraseButtonText();
}
});
alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
removeDialog(Id.dialog.new_pass_phrase);
}
});
return alert.create();
}
default: {
return super.onCreateDialog(id);
}
2010-04-06 19:54:51 +00:00
}
}
private void saveClicked() {
if (!isPassphraseSet()) {
Toast.makeText(this, R.string.setAPassPhrase, Toast.LENGTH_SHORT).show();
2010-04-06 19:54:51 +00:00
return;
}
showDialog(Id.dialog.saving);
startThread();
2010-04-06 19:54:51 +00:00
}
@Override
2010-04-06 19:54:51 +00:00
public void run() {
String error = null;
Bundle data = new Bundle();
Message msg = new Message();
try {
String oldPassPhrase = mCurrentPassPhrase;
2010-04-06 19:54:51 +00:00
String newPassPhrase = mNewPassPhrase;
if (newPassPhrase == null) {
newPassPhrase = oldPassPhrase;
}
Apg.buildSecretKey(this, mUserIds, mKeys, oldPassPhrase, newPassPhrase, this);
Apg.setCachedPassPhrase(getMasterKeyId(), newPassPhrase);
2010-04-06 19:54:51 +00:00
} catch (NoSuchProviderException e) {
error = "" + e;
2010-04-06 19:54:51 +00:00
} catch (NoSuchAlgorithmException e) {
error = "" + e;
2010-04-06 19:54:51 +00:00
} catch (PGPException e) {
error = "" + e;
2010-04-06 19:54:51 +00:00
} catch (SignatureException e) {
error = "" + e;
2010-04-06 19:54:51 +00:00
} catch (Apg.GeneralException e) {
error = "" + e;
} catch (Database.GeneralException e) {
error = "" + e;
} catch (IOException e) {
error = "" + e;
2010-04-06 19:54:51 +00:00
}
data.putInt(Constants.extras.STATUS, Id.message.done);
2010-04-06 19:54:51 +00:00
if (error != null) {
data.putString(Apg.EXTRA_ERROR, error);
2010-04-06 19:54:51 +00:00
}
msg.setData(data);
sendMessage(msg);
2010-04-06 19:54:51 +00:00
}
@Override
public void doneCallback(Message msg) {
super.doneCallback(msg);
2010-04-06 19:54:51 +00:00
Bundle data = msg.getData();
removeDialog(Id.dialog.saving);
String error = data.getString(Apg.EXTRA_ERROR);
if (error != null) {
Toast.makeText(EditKeyActivity.this, getString(R.string.errorMessage, error),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(EditKeyActivity.this, R.string.keySaved, Toast.LENGTH_SHORT).show();
setResult(RESULT_OK);
finish();
}
2010-04-06 19:54:51 +00:00
}
private void updatePassPhraseButtonText() {
mChangePassPhrase.setText(isPassphraseSet() ? R.string.btn_changePassPhrase
: R.string.btn_setPassPhrase);
}
2012-03-09 11:08:22 +01:00
}