diff options
Diffstat (limited to 'org_apg/src/org/apg/ui')
-rw-r--r-- | org_apg/src/org/apg/ui/EditKeyActivity.java | 154 | ||||
-rw-r--r-- | org_apg/src/org/apg/ui/SecretKeyListActivity.java | 4 | ||||
-rw-r--r-- | org_apg/src/org/apg/ui/widget/KeyEditor.java | 76 | ||||
-rw-r--r-- | org_apg/src/org/apg/ui/widget/SectionView.java | 28 |
4 files changed, 150 insertions, 112 deletions
diff --git a/org_apg/src/org/apg/ui/EditKeyActivity.java b/org_apg/src/org/apg/ui/EditKeyActivity.java index 3809f160d..27a94fd1f 100644 --- a/org_apg/src/org/apg/ui/EditKeyActivity.java +++ b/org_apg/src/org/apg/ui/EditKeyActivity.java @@ -40,8 +40,10 @@ import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.os.Message; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; +import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; @@ -52,6 +54,8 @@ import android.widget.Toast; import android.widget.CompoundButton.OnCheckedChangeListener; import java.io.IOException; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidParameterException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.SignatureException; @@ -59,6 +63,7 @@ import java.util.Vector; public class EditKeyActivity extends BaseActivity { private Intent mIntent = null; + private ActionBar mActionBar; private PGPSecretKeyRing mKeyRing = null; @@ -109,60 +114,119 @@ public class EditKeyActivity extends BaseActivity { super.onCreate(savedInstanceState); setContentView(R.layout.edit_key); + mActionBar = getSupportActionBar(); + + // find views + mChangePassPhrase = (Button) findViewById(R.id.edit_key_btn_change_pass_phrase); + mNoPassphrase = (CheckBox) findViewById(R.id.edit_key_no_passphrase); + Vector<String> userIds = new Vector<String>(); Vector<PGPSecretKey> keys = new Vector<PGPSecretKey>(); + Vector<Integer> keysUsages = new Vector<Integer>(); - Intent intent = getIntent(); - long keyId = 0; - if (intent.getExtras() != null) { - keyId = intent.getExtras().getLong(Apg.EXTRA_KEY_ID); - } + // Catch Intents opened from other apps + mIntent = getIntent(); + Bundle extras = mIntent.getExtras(); + if (Apg.Intent.CREATE_KEY.equals(mIntent.getAction())) { + + mActionBar.setTitle(R.string.title_createKey); + + mCurrentPassPhrase = ""; - if (keyId != 0) { - 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 (extras != null) { + + // disable home button on actionbar because this activity is run from another app + mActionBar.setDisplayShowTitleEnabled(true); + mActionBar.setDisplayHomeAsUpEnabled(false); + mActionBar.setHomeButtonEnabled(false); + + // if userId is given, prefill the fields + if (extras.containsKey(Apg.EXTRA_USER_IDS)) { + Log.d(Constants.TAG, "UserIds are given!"); + userIds.add(extras.getString(Apg.EXTRA_USER_IDS)); } - } - if (masterKey != null) { - for (String userId : new IterableIterator<String>(masterKey.getUserIDs())) { - userIds.add(userId); + + // if no passphrase is given + if (extras.containsKey(Apg.EXTRA_NO_PASSPHRASE)) { + boolean noPassphrase = extras.getBoolean(Apg.EXTRA_NO_PASSPHRASE); + if (noPassphrase) { + // check "no passphrase" checkbox and remove button + mNoPassphrase.setChecked(true); + mChangePassPhrase.setVisibility(View.GONE); + } } - } - } - // 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(); + // generate key + if (extras.containsKey(Apg.EXTRA_GENERATE_DEFAULT_KEYS)) { + boolean generateDefaultKeys = extras + .getBoolean(Apg.EXTRA_GENERATE_DEFAULT_KEYS); + if (generateDefaultKeys) { + + // generate a RSA 2048 key for encryption and signing! + try { + PGPSecretKey masterKey = Apg.createKey(this, Id.choice.algorithm.rsa, + 2048, mCurrentPassPhrase, null); + + // add new masterKey to keys array, which is then added to view + keys.add(masterKey); + keysUsages.add(Id.choice.usage.sign_only); + + PGPSecretKey subKey = Apg.createKey(this, Id.choice.algorithm.rsa, + 2048, mCurrentPassPhrase, masterKey); + + keys.add(subKey); + keysUsages.add(Id.choice.usage.encrypt_only); + + // define usage of this key + } catch (Exception e) { + Log.e(Constants.TAG, "Creating initial key failed: +" + e); + } + } + + } } + } else if (Apg.Intent.EDIT_KEY.equals(mIntent.getAction())) { + + mActionBar.setTitle(R.string.title_editKey); - // 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); + mCurrentPassPhrase = Apg.getEditPassPhrase(); + if (mCurrentPassPhrase == null) { + mCurrentPassPhrase = ""; + } - // if userId is given, prefill the fields - if (extras.containsKey(Apg.EXTRA_USER_IDS)) { - userIds.add(extras.getString(Apg.EXTRA_USER_IDS)); + if (mCurrentPassPhrase.equals("")) { + // check "no passphrase" checkbox and remove button + mNoPassphrase.setChecked(true); + mChangePassPhrase.setVisibility(View.GONE); } - // 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 = ""; + if (extras != null) { + + if (extras.containsKey(Apg.EXTRA_KEY_ID)) { + long keyId = mIntent.getExtras().getLong(Apg.EXTRA_KEY_ID); + + if (keyId != 0) { + 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); + keysUsages.add(-1); // get usage when view is created + } + } + if (masterKey != null) { + for (String userId : new IterableIterator<String>( + masterKey.getUserIDs())) { + userIds.add(userId); + } + } + } } } } - 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); @@ -170,7 +234,6 @@ public class EditKeyActivity extends BaseActivity { }); // disable passphrase when no passphrase checkobox is checked! - mNoPassphrase = (CheckBox) findViewById(R.id.edit_key_no_passphrase); mNoPassphrase.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override @@ -197,20 +260,9 @@ public class EditKeyActivity extends BaseActivity { container.addView(mUserIds); mKeys = (SectionView) inflater.inflate(R.layout.edit_key_section, container, false); mKeys.setType(Id.type.key); - mKeys.setKeys(keys); + mKeys.setKeys(keys, keysUsages); 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(); } diff --git a/org_apg/src/org/apg/ui/SecretKeyListActivity.java b/org_apg/src/org/apg/ui/SecretKeyListActivity.java index 0e6d78c03..23ec694ce 100644 --- a/org_apg/src/org/apg/ui/SecretKeyListActivity.java +++ b/org_apg/src/org/apg/ui/SecretKeyListActivity.java @@ -164,13 +164,13 @@ public class SecretKeyListActivity extends KeyListActivity implements OnChildCli private void createKey() { Apg.setEditPassPhrase(""); - Intent intent = new Intent(this, EditKeyActivity.class); + Intent intent = new Intent(Apg.Intent.CREATE_KEY); startActivityForResult(intent, Id.message.create_key); } private void editKey() { long keyId = ((KeyListAdapter) mList.getExpandableListAdapter()).getGroupId(mSelectedItem); - Intent intent = new Intent(this, EditKeyActivity.class); + Intent intent = new Intent(Apg.Intent.EDIT_KEY); intent.putExtra(Apg.EXTRA_KEY_ID, keyId); startActivityForResult(intent, Id.message.edit_key); } diff --git a/org_apg/src/org/apg/ui/widget/KeyEditor.java b/org_apg/src/org/apg/ui/widget/KeyEditor.java index ef98f794a..0e2bd145f 100644 --- a/org_apg/src/org/apg/ui/widget/KeyEditor.java +++ b/org_apg/src/org/apg/ui/widget/KeyEditor.java @@ -59,13 +59,12 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener { Button mExpiryDateButton; GregorianCalendar mExpiryDate; - private DatePickerDialog.OnDateSetListener mExpiryDateSetListener = - new DatePickerDialog.OnDateSetListener() { - public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { - GregorianCalendar date = new GregorianCalendar(year, monthOfYear, dayOfMonth); - setExpiryDate(date); - } - }; + private DatePickerDialog.OnDateSetListener mExpiryDateSetListener = new DatePickerDialog.OnDateSetListener() { + public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { + GregorianCalendar date = new GregorianCalendar(year, monthOfYear, dayOfMonth); + setExpiryDate(date); + } + }; public KeyEditor(Context context) { super(context); @@ -86,16 +85,14 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener { mExpiryDateButton = (Button) findViewById(R.id.expiry); mUsage = (Spinner) findViewById(R.id.usage); Choice choices[] = { - new Choice(Id.choice.usage.sign_only, - getResources().getString(R.string.choice_signOnly)), - new Choice(Id.choice.usage.encrypt_only, - getResources().getString(R.string.choice_encryptOnly)), - new Choice(Id.choice.usage.sign_and_encrypt, - getResources().getString(R.string.choice_signAndEncrypt)), - }; - ArrayAdapter<Choice> adapter = - new ArrayAdapter<Choice>(getContext(), - android.R.layout.simple_spinner_item, choices); + new Choice(Id.choice.usage.sign_only, getResources().getString( + R.string.choice_signOnly)), + new Choice(Id.choice.usage.encrypt_only, getResources().getString( + R.string.choice_encryptOnly)), + new Choice(Id.choice.usage.sign_and_encrypt, getResources().getString( + R.string.choice_signAndEncrypt)), }; + ArrayAdapter<Choice> adapter = new ArrayAdapter<Choice>(getContext(), + android.R.layout.simple_spinner_item, choices); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mUsage.setAdapter(adapter); @@ -111,15 +108,12 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener { date = new GregorianCalendar(); } - DatePickerDialog dialog = - new DatePickerDialog(getContext(), mExpiryDateSetListener, - date.get(Calendar.YEAR), - date.get(Calendar.MONTH), - date.get(Calendar.DAY_OF_MONTH)); + DatePickerDialog dialog = new DatePickerDialog(getContext(), + mExpiryDateSetListener, date.get(Calendar.YEAR), date.get(Calendar.MONTH), + date.get(Calendar.DAY_OF_MONTH)); dialog.setCancelable(true); - dialog.setButton(Dialog.BUTTON_NEGATIVE, - getContext().getString(R.string.btn_noDate), - new DialogInterface.OnClickListener() { + dialog.setButton(Dialog.BUTTON_NEGATIVE, getContext() + .getString(R.string.btn_noDate), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { setExpiryDate(null); } @@ -131,7 +125,7 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener { super.onFinishInflate(); } - public void setValue(PGPSecretKey key, boolean isMasterKey) { + public void setValue(PGPSecretKey key, boolean isMasterKey, int usage) { mKey = key; mIsMasterKey = isMasterKey; @@ -147,24 +141,24 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener { Vector<Choice> choices = new Vector<Choice>(); boolean isElGamalKey = (key.getPublicKey().getAlgorithm() == PGPPublicKey.ELGAMAL_ENCRYPT); if (!isElGamalKey) { - choices.add(new Choice(Id.choice.usage.sign_only, - getResources().getString(R.string.choice_signOnly))); + choices.add(new Choice(Id.choice.usage.sign_only, getResources().getString( + R.string.choice_signOnly))); } if (!mIsMasterKey) { - choices.add(new Choice(Id.choice.usage.encrypt_only, - getResources().getString(R.string.choice_encryptOnly))); + choices.add(new Choice(Id.choice.usage.encrypt_only, getResources().getString( + R.string.choice_encryptOnly))); } if (!isElGamalKey) { - choices.add(new Choice(Id.choice.usage.sign_and_encrypt, - getResources().getString(R.string.choice_signAndEncrypt))); + choices.add(new Choice(Id.choice.usage.sign_and_encrypt, getResources().getString( + R.string.choice_signAndEncrypt))); } - ArrayAdapter<Choice> adapter = - new ArrayAdapter<Choice>(getContext(), - android.R.layout.simple_spinner_item, choices); + ArrayAdapter<Choice> adapter = new ArrayAdapter<Choice>(getContext(), + android.R.layout.simple_spinner_item, choices); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mUsage.setAdapter(adapter); + // Set value in choice dropdown to key int selectId = 0; if (Apg.isEncryptionKey(key)) { if (Apg.isSigningKey(key)) { @@ -173,7 +167,13 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener { selectId = Id.choice.usage.encrypt_only; } } else { - selectId = Id.choice.usage.sign_only; + // set usage if it is predefined + if (usage != -1) { + selectId = usage; + } else { + selectId = Id.choice.usage.sign_only; + } + } for (int i = 0; i < choices.size(); ++i) { @@ -194,6 +194,7 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener { cal.setTime(Apg.getExpiryDate(key)); setExpiryDate(cal); } + } public PGPSecretKey getValue() { @@ -201,7 +202,7 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener { } public void onClick(View v) { - final ViewGroup parent = (ViewGroup)getParent(); + final ViewGroup parent = (ViewGroup) getParent(); if (v == mDeleteButton) { parent.removeView(this); if (mEditorListener != null) { @@ -230,4 +231,5 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener { public int getUsage() { return ((Choice) mUsage.getSelectedItem()).getId(); } + } diff --git a/org_apg/src/org/apg/ui/widget/SectionView.java b/org_apg/src/org/apg/ui/widget/SectionView.java index c62e84c03..556164ca8 100644 --- a/org_apg/src/org/apg/ui/widget/SectionView.java +++ b/org_apg/src/org/apg/ui/widget/SectionView.java @@ -93,7 +93,7 @@ public class SectionView extends LinearLayout implements OnClickListener, Editor mEditors, false); view.setEditorListener(SectionView.this); boolean isMasterKey = (mEditors.getChildCount() == 0); - view.setValue(mNewKey, isMasterKey); + view.setValue(mNewKey, isMasterKey, -1); mEditors.addView(view); SectionView.this.updateEditorsVisible(); } @@ -260,42 +260,26 @@ public class SectionView extends LinearLayout implements OnClickListener, Editor this.updateEditorsVisible(); } - public void setKeys(Vector<PGPSecretKey> list) { + public void setKeys(Vector<PGPSecretKey> list, Vector<Integer> usages) { if (mType != Id.type.key) { return; } mEditors.removeAllViews(); - for (PGPSecretKey key : list) { + + // go through all keys and set view based on them + for (int i = 0; i < list.size(); i++) { KeyEditor view = (KeyEditor) mInflater.inflate(R.layout.edit_key_key_item, mEditors, false); view.setEditorListener(this); boolean isMasterKey = (mEditors.getChildCount() == 0); - view.setValue(key, isMasterKey); + view.setValue(list.get(i), isMasterKey, usages.get(i)); mEditors.addView(view); } this.updateEditorsVisible(); } - // TODO !!! - public void createInitialKey() { - mEditors.removeAllViews(); - - // create initial key - // PGPSecretKey key = new PGPSecretKey(); - - // for (PGPSecretKey key : list) { - KeyEditor view = (KeyEditor) mInflater.inflate(R.layout.edit_key_key_item, mEditors, false); - view.setEditorListener(this); - boolean isMasterKey = (mEditors.getChildCount() == 0); - // view.setValue(key, isMasterKey); - mEditors.addView(view); - // } - - this.updateEditorsVisible(); - } - private void createKey() { mProgressDialog = new ProgressDialog(getContext()); mProgressDialog.setMessage(getContext().getString(R.string.progress_generating)); |