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.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-03-09 16:27:29 +01:00
|
|
|
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;
|
2011-11-03 22:15:31 +01:00
|
|
|
import org.spongycastle.openpgp.PGPException;
|
|
|
|
|
import org.spongycastle.openpgp.PGPSecretKey;
|
|
|
|
|
import org.spongycastle.openpgp.PGPSecretKeyRing;
|
2012-03-09 16:27:29 +01:00
|
|
|
import org.apg.R;
|
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.Menu;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
|
import android.widget.Button;
|
|
|
|
|
import android.widget.EditText;
|
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
2011-11-03 22:15:31 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
import java.security.NoSuchProviderException;
|
|
|
|
|
import java.security.SignatureException;
|
|
|
|
|
import java.util.Vector;
|
|
|
|
|
|
major restructuring, moving dialog, message, menu, option menu, task, type IDs into Id in a similar structure as the generated R, also introducing a BaseActivity class that almost all activities derive from, which generates some common dialogs, handles the progress update, thread management, and thread communication
also adding first draft of encrypt file activity, not very functional yet
2010-04-19 02:12:13 +00:00
|
|
|
public class EditKeyActivity extends BaseActivity implements OnClickListener {
|
2010-04-06 19:54:51 +00:00
|
|
|
|
|
|
|
|
private PGPSecretKeyRing mKeyRing = null;
|
|
|
|
|
|
|
|
|
|
private SectionView mUserIds;
|
|
|
|
|
private SectionView mKeys;
|
|
|
|
|
|
|
|
|
|
private Button mSaveButton;
|
|
|
|
|
private Button mDiscardButton;
|
|
|
|
|
|
2010-05-16 13:35:16 +00:00
|
|
|
private String mCurrentPassPhrase = null;
|
2010-04-06 19:54:51 +00:00
|
|
|
private String mNewPassPhrase = null;
|
|
|
|
|
|
2010-07-23 12:57:02 +00:00
|
|
|
private Button mChangePassPhrase;
|
|
|
|
|
|
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) {
|
2010-05-31 23:15:20 +00:00
|
|
|
keyId = intent.getExtras().getLong(Apg.EXTRA_KEY_ID);
|
2010-04-06 19:54:51 +00:00
|
|
|
}
|
2010-04-24 17:40:09 +00:00
|
|
|
|
2010-05-15 15:19:56 +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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-23 12:57:02 +00:00
|
|
|
mChangePassPhrase = (Button) findViewById(R.id.btn_change_pass_phrase);
|
|
|
|
|
mChangePassPhrase.setOnClickListener(new OnClickListener() {
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
showDialog(Id.dialog.new_pass_phrase);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2010-04-06 19:54:51 +00:00
|
|
|
mSaveButton = (Button) findViewById(R.id.btn_save);
|
|
|
|
|
mDiscardButton = (Button) findViewById(R.id.btn_discard);
|
|
|
|
|
|
|
|
|
|
mSaveButton.setOnClickListener(this);
|
|
|
|
|
mDiscardButton.setOnClickListener(this);
|
|
|
|
|
|
2012-03-09 16:27:29 +01:00
|
|
|
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
2010-04-06 19:54:51 +00:00
|
|
|
|
|
|
|
|
LinearLayout container = (LinearLayout) findViewById(R.id.container);
|
|
|
|
|
mUserIds = (SectionView) inflater.inflate(R.layout.edit_key_section, container, false);
|
2010-05-13 20:41:32 +00:00
|
|
|
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);
|
2010-05-13 20:41:32 +00:00
|
|
|
mKeys.setType(Id.type.key);
|
2010-04-06 19:54:51 +00:00
|
|
|
mKeys.setKeys(keys);
|
|
|
|
|
container.addView(mKeys);
|
|
|
|
|
|
2010-05-16 13:35:16 +00:00
|
|
|
mCurrentPassPhrase = Apg.getEditPassPhrase();
|
|
|
|
|
if (mCurrentPassPhrase == null) {
|
|
|
|
|
mCurrentPassPhrase = "";
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-23 12:57:02 +00:00
|
|
|
updatePassPhraseButtonText();
|
|
|
|
|
|
2012-03-09 16:27:29 +01:00
|
|
|
Toast.makeText(this,
|
|
|
|
|
getString(R.string.warningMessage, getString(R.string.keyEditingIsBeta)),
|
|
|
|
|
Toast.LENGTH_LONG).show();
|
2010-04-06 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
2010-05-19 14:51:58 +00:00
|
|
|
private long getMasterKeyId() {
|
2010-05-15 15:19:56 +00:00
|
|
|
if (mKeys.getEditors().getChildCount() == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return ((KeyEditor) mKeys.getEditors().getChildAt(0)).getValue().getKeyID();
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-06 19:54:51 +00:00
|
|
|
public boolean havePassPhrase() {
|
2012-03-09 16:27:29 +01:00
|
|
|
return (!mCurrentPassPhrase.equals(""))
|
|
|
|
|
|| (mNewPassPhrase != null && !mNewPassPhrase.equals(""));
|
2010-04-06 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
2012-03-09 16:27:29 +01:00
|
|
|
menu.add(0, Id.menu.option.preferences, 0, R.string.menu_preferences).setIcon(
|
|
|
|
|
android.R.drawable.ic_menu_preferences);
|
|
|
|
|
menu.add(0, Id.menu.option.about, 1, R.string.menu_about).setIcon(
|
|
|
|
|
android.R.drawable.ic_menu_info_details);
|
2010-04-06 19:54:51 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected Dialog onCreateDialog(int id) {
|
|
|
|
|
switch (id) {
|
2012-03-09 16:27:29 +01:00
|
|
|
case Id.dialog.new_pass_phrase: {
|
|
|
|
|
AlertDialog.Builder alert = new AlertDialog.Builder(this);
|
2010-04-06 19:54:51 +00:00
|
|
|
|
2012-03-09 16:27:29 +01:00
|
|
|
if (havePassPhrase()) {
|
|
|
|
|
alert.setTitle(R.string.title_changePassPhrase);
|
|
|
|
|
} else {
|
|
|
|
|
alert.setTitle(R.string.title_setPassPhrase);
|
2010-04-06 19:54:51 +00:00
|
|
|
}
|
2012-03-09 16:27:29 +01:00
|
|
|
alert.setMessage(R.string.enterPassPhraseTwice);
|
2010-04-06 19:54:51 +00:00
|
|
|
|
2012-03-09 16:27:29 +01:00
|
|
|
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
|
View view = inflater.inflate(R.layout.pass_phrase, null);
|
|
|
|
|
final EditText input1 = (EditText) view.findViewById(R.id.passPhrase);
|
|
|
|
|
final EditText input2 = (EditText) view.findViewById(R.id.passPhraseAgain);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
if (v == mSaveButton) {
|
|
|
|
|
// TODO: some warning
|
|
|
|
|
saveClicked();
|
|
|
|
|
} else if (v == mDiscardButton) {
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void saveClicked() {
|
major restructuring, moving dialog, message, menu, option menu, task, type IDs into Id in a similar structure as the generated R, also introducing a BaseActivity class that almost all activities derive from, which generates some common dialogs, handles the progress update, thread management, and thread communication
also adding first draft of encrypt file activity, not very functional yet
2010-04-19 02:12:13 +00:00
|
|
|
if (!havePassPhrase()) {
|
2010-05-13 20:41:32 +00:00
|
|
|
Toast.makeText(this, R.string.setAPassPhrase, Toast.LENGTH_SHORT).show();
|
2010-04-06 19:54:51 +00:00
|
|
|
return;
|
|
|
|
|
}
|
major restructuring, moving dialog, message, menu, option menu, task, type IDs into Id in a similar structure as the generated R, also introducing a BaseActivity class that almost all activities derive from, which generates some common dialogs, handles the progress update, thread management, and thread communication
also adding first draft of encrypt file activity, not very functional yet
2010-04-19 02:12:13 +00:00
|
|
|
showDialog(Id.dialog.saving);
|
|
|
|
|
startThread();
|
2010-04-06 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
major restructuring, moving dialog, message, menu, option menu, task, type IDs into Id in a similar structure as the generated R, also introducing a BaseActivity class that almost all activities derive from, which generates some common dialogs, handles the progress update, thread management, and thread communication
also adding first draft of encrypt file activity, not very functional yet
2010-04-19 02:12:13 +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 {
|
2010-05-16 13:35:16 +00:00
|
|
|
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);
|
2010-05-19 14:51:58 +00:00
|
|
|
Apg.setCachedPassPhrase(getMasterKeyId(), newPassPhrase);
|
2010-04-06 19:54:51 +00:00
|
|
|
} catch (NoSuchProviderException e) {
|
2010-05-21 02:47:40 +00:00
|
|
|
error = "" + e;
|
2010-04-06 19:54:51 +00:00
|
|
|
} catch (NoSuchAlgorithmException e) {
|
2010-05-21 02:47:40 +00:00
|
|
|
error = "" + e;
|
2010-04-06 19:54:51 +00:00
|
|
|
} catch (PGPException e) {
|
2010-05-21 02:47:40 +00:00
|
|
|
error = "" + e;
|
2010-04-06 19:54:51 +00:00
|
|
|
} catch (SignatureException e) {
|
2010-05-21 02:47:40 +00:00
|
|
|
error = "" + e;
|
2010-04-06 19:54:51 +00:00
|
|
|
} catch (Apg.GeneralException e) {
|
2010-05-21 02:47:40 +00:00
|
|
|
error = "" + e;
|
2010-05-26 15:25:14 +00:00
|
|
|
} catch (Database.GeneralException e) {
|
|
|
|
|
error = "" + e;
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
error = "" + e;
|
2010-04-06 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-09 18:43:10 +01:00
|
|
|
data.putInt(Constants.extras.STATUS, Id.message.done);
|
2010-04-06 19:54:51 +00:00
|
|
|
|
|
|
|
|
if (error != null) {
|
2010-05-31 23:15:20 +00:00
|
|
|
data.putString(Apg.EXTRA_ERROR, error);
|
2010-04-06 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg.setData(data);
|
major restructuring, moving dialog, message, menu, option menu, task, type IDs into Id in a similar structure as the generated R, also introducing a BaseActivity class that almost all activities derive from, which generates some common dialogs, handles the progress update, thread management, and thread communication
also adding first draft of encrypt file activity, not very functional yet
2010-04-19 02:12:13 +00:00
|
|
|
sendMessage(msg);
|
2010-04-06 19:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
major restructuring, moving dialog, message, menu, option menu, task, type IDs into Id in a similar structure as the generated R, also introducing a BaseActivity class that almost all activities derive from, which generates some common dialogs, handles the progress update, thread management, and thread communication
also adding first draft of encrypt file activity, not very functional yet
2010-04-19 02:12:13 +00:00
|
|
|
@Override
|
|
|
|
|
public void doneCallback(Message msg) {
|
|
|
|
|
super.doneCallback(msg);
|
2010-04-06 19:54:51 +00:00
|
|
|
|
major restructuring, moving dialog, message, menu, option menu, task, type IDs into Id in a similar structure as the generated R, also introducing a BaseActivity class that almost all activities derive from, which generates some common dialogs, handles the progress update, thread management, and thread communication
also adding first draft of encrypt file activity, not very functional yet
2010-04-19 02:12:13 +00:00
|
|
|
Bundle data = msg.getData();
|
|
|
|
|
removeDialog(Id.dialog.saving);
|
|
|
|
|
|
2010-05-31 23:15:20 +00:00
|
|
|
String error = data.getString(Apg.EXTRA_ERROR);
|
major restructuring, moving dialog, message, menu, option menu, task, type IDs into Id in a similar structure as the generated R, also introducing a BaseActivity class that almost all activities derive from, which generates some common dialogs, handles the progress update, thread management, and thread communication
also adding first draft of encrypt file activity, not very functional yet
2010-04-19 02:12:13 +00:00
|
|
|
if (error != null) {
|
2012-03-09 16:27:29 +01:00
|
|
|
Toast.makeText(EditKeyActivity.this, getString(R.string.errorMessage, error),
|
|
|
|
|
Toast.LENGTH_SHORT).show();
|
major restructuring, moving dialog, message, menu, option menu, task, type IDs into Id in a similar structure as the generated R, also introducing a BaseActivity class that almost all activities derive from, which generates some common dialogs, handles the progress update, thread management, and thread communication
also adding first draft of encrypt file activity, not very functional yet
2010-04-19 02:12:13 +00:00
|
|
|
} else {
|
2010-05-13 20:41:32 +00:00
|
|
|
Toast.makeText(EditKeyActivity.this, R.string.keySaved, Toast.LENGTH_SHORT).show();
|
major restructuring, moving dialog, message, menu, option menu, task, type IDs into Id in a similar structure as the generated R, also introducing a BaseActivity class that almost all activities derive from, which generates some common dialogs, handles the progress update, thread management, and thread communication
also adding first draft of encrypt file activity, not very functional yet
2010-04-19 02:12:13 +00:00
|
|
|
setResult(RESULT_OK);
|
|
|
|
|
finish();
|
|
|
|
|
}
|
2010-04-06 19:54:51 +00:00
|
|
|
}
|
2010-07-23 12:57:02 +00:00
|
|
|
|
|
|
|
|
private void updatePassPhraseButtonText() {
|
2012-03-09 16:27:29 +01:00
|
|
|
mChangePassPhrase.setText(havePassPhrase() ? R.string.btn_changePassPhrase
|
|
|
|
|
: R.string.btn_setPassPhrase);
|
2010-07-23 12:57:02 +00:00
|
|
|
}
|
2012-03-09 11:08:22 +01:00
|
|
|
}
|