diff options
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java')
-rw-r--r-- | OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java | 189 |
1 files changed, 121 insertions, 68 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java index b79e4454d..b92645777 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java @@ -17,19 +17,19 @@ package org.sufficientlysecure.keychain.ui; - -import java.util.Date; -import java.util.Iterator; - import android.app.Activity; import android.content.Intent; import android.database.Cursor; -import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; +import android.view.View.OnClickListener; import android.view.ViewGroup; +import android.widget.Button; import android.widget.CheckBox; import android.widget.TextView; @@ -55,6 +55,9 @@ import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Passphrase; import org.sufficientlysecure.keychain.util.Preferences; +import java.util.Date; +import java.util.Iterator; + public class CreateKeyFinalFragment extends Fragment { public static final int REQUEST_EDIT_KEY = 0x00008007; @@ -64,8 +67,8 @@ public class CreateKeyFinalFragment extends Fragment { CheckBox mUploadCheckbox; View mBackButton; View mCreateButton; - TextView mEditText; - View mEditButton; + View mCustomKeyLayout; + Button mCustomKeyRevertButton; SaveKeyringParcel mSaveKeyringParcel; @@ -97,8 +100,8 @@ public class CreateKeyFinalFragment extends Fragment { mUploadCheckbox = (CheckBox) view.findViewById(R.id.create_key_upload); mBackButton = view.findViewById(R.id.create_key_back_button); mCreateButton = view.findViewById(R.id.create_key_next_button); - mEditText = (TextView) view.findViewById(R.id.create_key_edit_text); - mEditButton = view.findViewById(R.id.create_key_edit_button); + mCustomKeyLayout = view.findViewById(R.id.custom_key_layout); + mCustomKeyRevertButton = (Button) view.findViewById(R.id.revert_key_configuration); CreateKeyActivity createKeyActivity = (CreateKeyActivity) getActivity(); @@ -126,22 +129,20 @@ public class CreateKeyFinalFragment extends Fragment { } }); - mBackButton.setOnClickListener(new View.OnClickListener() { + mCustomKeyRevertButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - CreateKeyActivity createKeyActivity = (CreateKeyActivity) getActivity(); - if (createKeyActivity != null) { - createKeyActivity.loadFragment(null, FragAction.TO_LEFT); - } + keyConfigRevertToDefault(); } }); - mEditButton.setOnClickListener(new View.OnClickListener() { + mBackButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Intent edit = new Intent(getActivity(), EditKeyActivity.class); - edit.putExtra(EditKeyActivity.EXTRA_SAVE_KEYRING_PARCEL, mSaveKeyringParcel); - startActivityForResult(edit, REQUEST_EDIT_KEY); + CreateKeyActivity createKeyActivity = (CreateKeyActivity) getActivity(); + if (createKeyActivity != null) { + createKeyActivity.loadFragment(null, FragAction.TO_LEFT); + } } }); @@ -154,6 +155,38 @@ public class CreateKeyFinalFragment extends Fragment { } @Override + public void onPrepareOptionsMenu(Menu menu) { + CreateKeyActivity createKeyActivity = (CreateKeyActivity) getActivity(); + + MenuItem editItem = menu.findItem(R.id.menu_create_key_edit); + editItem.setEnabled(!createKeyActivity.mCreateSecurityToken); + + super.onPrepareOptionsMenu(menu); + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + inflater.inflate(R.menu.create_key_final, menu); + + super.onCreateOptionsMenu(menu, inflater); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + + case R.id.menu_create_key_edit: + Intent edit = new Intent(getActivity(), EditKeyActivity.class); + edit.putExtra(EditKeyActivity.EXTRA_SAVE_KEYRING_PARCEL, mSaveKeyringParcel); + startActivityForResult(edit, REQUEST_EDIT_KEY); + return true; + + default: + return super.onOptionsItemSelected(item); + } + } + + @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (mCreateOpHelper != null) { mCreateOpHelper.handleActivityResult(requestCode, resultCode, data); @@ -168,8 +201,9 @@ public class CreateKeyFinalFragment extends Fragment { switch (requestCode) { case REQUEST_EDIT_KEY: { if (resultCode == Activity.RESULT_OK) { - mSaveKeyringParcel = data.getParcelableExtra(EditKeyActivity.EXTRA_SAVE_KEYRING_PARCEL); - mEditText.setText(R.string.create_key_custom); + SaveKeyringParcel customKeyConfiguration = + data.getParcelableExtra(EditKeyActivity.EXTRA_SAVE_KEYRING_PARCEL); + keyConfigUseCustom(customKeyConfiguration); } break; } @@ -179,53 +213,29 @@ public class CreateKeyFinalFragment extends Fragment { } } + public void keyConfigUseCustom(SaveKeyringParcel customKeyConfiguration) { + mSaveKeyringParcel = customKeyConfiguration; + mCustomKeyLayout.setVisibility(View.VISIBLE); + } + + public void keyConfigRevertToDefault() { + Activity activity = getActivity(); + if (activity == null) { + return; + } + mSaveKeyringParcel = createDefaultSaveKeyringParcel((CreateKeyActivity) activity); + mCustomKeyLayout.setVisibility(View.GONE); + } + @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - CreateKeyActivity createKeyActivity = (CreateKeyActivity) getActivity(); + // We have a menu item to show in action bar. + setHasOptionsMenu(true); if (mSaveKeyringParcel == null) { - mSaveKeyringParcel = new SaveKeyringParcel(); - - if (createKeyActivity.mCreateYubiKey) { - mSaveKeyringParcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Algorithm.RSA, - 2048, null, KeyFlags.SIGN_DATA | KeyFlags.CERTIFY_OTHER, 0L)); - mSaveKeyringParcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Algorithm.RSA, - 2048, null, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE, 0L)); - mSaveKeyringParcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Algorithm.RSA, - 2048, null, KeyFlags.AUTHENTICATION, 0L)); - mEditText.setText(R.string.create_key_custom); - mEditButton.setEnabled(false); - - // use empty passphrase - mSaveKeyringParcel.mNewUnlock = new ChangeUnlockParcel(new Passphrase(), null); - } else { - mSaveKeyringParcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Algorithm.RSA, - 4096, null, KeyFlags.CERTIFY_OTHER, 0L)); - mSaveKeyringParcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Algorithm.RSA, - 4096, null, KeyFlags.SIGN_DATA, 0L)); - mSaveKeyringParcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Algorithm.RSA, - 4096, null, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE, 0L)); - - mSaveKeyringParcel.mNewUnlock = createKeyActivity.mPassphrase != null - ? new ChangeUnlockParcel(createKeyActivity.mPassphrase, null) - : null; - } - String userId = KeyRing.createUserId( - new KeyRing.UserId(createKeyActivity.mName, createKeyActivity.mEmail, null) - ); - mSaveKeyringParcel.mAddUserIds.add(userId); - mSaveKeyringParcel.mChangePrimaryUserId = userId; - if (createKeyActivity.mAdditionalEmails != null - && createKeyActivity.mAdditionalEmails.size() > 0) { - for (String email : createKeyActivity.mAdditionalEmails) { - String thisUserId = KeyRing.createUserId( - new KeyRing.UserId(createKeyActivity.mName, email, null) - ); - mSaveKeyringParcel.mAddUserIds.add(thisUserId); - } - } + keyConfigRevertToDefault(); } // handle queued actions @@ -256,6 +266,49 @@ public class CreateKeyFinalFragment extends Fragment { } + private static SaveKeyringParcel createDefaultSaveKeyringParcel(CreateKeyActivity createKeyActivity) { + SaveKeyringParcel saveKeyringParcel = new SaveKeyringParcel(); + + if (createKeyActivity.mCreateSecurityToken) { + saveKeyringParcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Algorithm.RSA, + 2048, null, KeyFlags.SIGN_DATA | KeyFlags.CERTIFY_OTHER, 0L)); + saveKeyringParcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Algorithm.RSA, + 2048, null, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE, 0L)); + saveKeyringParcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Algorithm.RSA, + 2048, null, KeyFlags.AUTHENTICATION, 0L)); + + // use empty passphrase + saveKeyringParcel.mNewUnlock = new ChangeUnlockParcel(new Passphrase(), null); + } else { + saveKeyringParcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Algorithm.RSA, + 4096, null, KeyFlags.CERTIFY_OTHER, 0L)); + saveKeyringParcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Algorithm.RSA, + 4096, null, KeyFlags.SIGN_DATA, 0L)); + saveKeyringParcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Algorithm.RSA, + 4096, null, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE, 0L)); + + saveKeyringParcel.mNewUnlock = createKeyActivity.mPassphrase != null + ? new ChangeUnlockParcel(createKeyActivity.mPassphrase, null) + : null; + } + String userId = KeyRing.createUserId( + new KeyRing.UserId(createKeyActivity.mName, createKeyActivity.mEmail, null) + ); + saveKeyringParcel.mAddUserIds.add(userId); + saveKeyringParcel.mChangePrimaryUserId = userId; + if (createKeyActivity.mAdditionalEmails != null + && createKeyActivity.mAdditionalEmails.size() > 0) { + for (String email : createKeyActivity.mAdditionalEmails) { + String thisUserId = KeyRing.createUserId( + new KeyRing.UserId(createKeyActivity.mName, email, null) + ); + saveKeyringParcel.mAddUserIds.add(thisUserId); + } + } + + return saveKeyringParcel; + } + private void createKey() { CreateKeyActivity activity = (CreateKeyActivity) getActivity(); if (activity == null) { @@ -263,7 +316,7 @@ public class CreateKeyFinalFragment extends Fragment { return; } - final boolean createYubiKey = activity.mCreateYubiKey; + final boolean createSecurityToken = activity.mCreateSecurityToken; CryptoOperationHelper.Callback<SaveKeyringParcel, EditKeyResult> createKeyCallback = new CryptoOperationHelper.Callback<SaveKeyringParcel, EditKeyResult>() { @@ -275,7 +328,7 @@ public class CreateKeyFinalFragment extends Fragment { @Override public void onCryptoOperationSuccess(EditKeyResult result) { - if (createYubiKey) { + if (createSecurityToken) { moveToCard(result); return; } @@ -327,19 +380,19 @@ public class CreateKeyFinalFragment extends Fragment { try { changeKeyringParcel = new SaveKeyringParcel(key.getMasterKeyId(), key.getFingerprint()); } catch (PgpKeyNotFoundException e) { - Log.e(Constants.TAG, "Key that should be moved to YubiKey not found in database!"); + Log.e(Constants.TAG, "Key that should be moved to Security Token not found in database!"); return; } // define subkeys that should be moved to the card Cursor cursor = activity.getContentResolver().query( KeychainContract.Keys.buildKeysUri(changeKeyringParcel.mMasterKeyId), - new String[] { KeychainContract.Keys.KEY_ID, }, null, null, null + new String[]{KeychainContract.Keys.KEY_ID,}, null, null, null ); try { while (cursor != null && cursor.moveToNext()) { long subkeyId = cursor.getLong(0); - changeKeyringParcel.getOrCreateSubkeyChange(subkeyId).mMoveKeyToCard = true; + changeKeyringParcel.getOrCreateSubkeyChange(subkeyId).mMoveKeyToSecurityToken = true; } } finally { if (cursor != null) { @@ -348,8 +401,8 @@ public class CreateKeyFinalFragment extends Fragment { } // define new PIN and Admin PIN for the card - changeKeyringParcel.mCardPin = activity.mYubiKeyPin; - changeKeyringParcel.mCardAdminPin = activity.mYubiKeyAdminPin; + changeKeyringParcel.mSecurityTokenPin = activity.mSecurityTokenPin; + changeKeyringParcel.mSecurityTokenAdminPin = activity.mSecurityTokenAdminPin; CryptoOperationHelper.Callback<SaveKeyringParcel, EditKeyResult> callback = new CryptoOperationHelper.Callback<SaveKeyringParcel, EditKeyResult>() { |