From 7f463ae0dfcc3c0299c8ff943f8a021bfa5476f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 4 Aug 2014 02:26:34 +0200 Subject: Edit key: subkey adding, not finished --- .../keychain/ui/EditKeyFragment.java | 19 +- .../keychain/ui/adapter/ImportKeysAdapter.java | 2 +- .../ui/adapter/SelectKeyCursorAdapter.java | 2 +- .../keychain/ui/adapter/SubkeysAdapter.java | 36 ++- .../keychain/ui/adapter/SubkeysAddedAdapter.java | 323 ++++---------------- .../ui/dialog/AddSubkeyDialogFragment.java | 328 +++++++++++++++++++++ .../ui/dialog/CreateKeyDialogFragment.java | 322 -------------------- .../keychain/ui/widget/KeyEditor.java | 2 +- .../keychain/ui/widget/SectionView.java | 36 +-- .../src/main/res/layout/create_key_dialog.xml | 130 ++++++-- .../src/main/res/layout/edit_key_key_item.xml | 2 +- .../main/res/layout/edit_key_subkey_added_item.xml | 179 ----------- .../src/main/res/layout/import_keys_list_entry.xml | 2 +- .../src/main/res/layout/select_key_item.xml | 2 +- .../src/main/res/layout/view_key_subkey_item.xml | 57 ++-- 15 files changed, 570 insertions(+), 872 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddSubkeyDialogFragment.java delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/CreateKeyDialogFragment.java delete mode 100644 OpenKeychain/src/main/res/layout/edit_key_subkey_added_item.xml (limited to 'OpenKeychain/src/main') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index b76755bb2..2d8bdad27 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -57,6 +57,7 @@ import org.sufficientlysecure.keychain.ui.adapter.SubkeysAdapter; import org.sufficientlysecure.keychain.ui.adapter.SubkeysAddedAdapter; import org.sufficientlysecure.keychain.ui.adapter.UserIdsAdapter; import org.sufficientlysecure.keychain.ui.adapter.UserIdsAddedAdapter; +import org.sufficientlysecure.keychain.ui.dialog.AddSubkeyDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.AddUserIdDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.ChangeExpiryDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.EditSubkeyDialogFragment; @@ -65,7 +66,6 @@ import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.SetPassphraseDialogFragment; import org.sufficientlysecure.keychain.util.Log; -import java.util.ArrayList; import java.util.Date; public class EditKeyFragment extends LoaderFragment implements @@ -453,8 +453,21 @@ public class EditKeyFragment extends LoaderFragment implements } private void addSubkey() { - // default values - mSubkeysAddedAdapter.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.SIGN_DATA, null)); + boolean willBeMasterKey = mSubkeysAdapter.getCount() == 0 + && mSubkeysAddedAdapter.getCount() == 0; + + AddSubkeyDialogFragment addSubkeyDialogFragment = + AddSubkeyDialogFragment.newInstance(willBeMasterKey); + addSubkeyDialogFragment + .setOnAlgorithmSelectedListener( + new AddSubkeyDialogFragment.OnAlgorithmSelectedListener() { + @Override + public void onAlgorithmSelected(SaveKeyringParcel.SubkeyAdd newSubkey) { + mSubkeysAddedAdapter.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.SIGN_DATA, null)); + } + } + ); + addSubkeyDialogFragment.show(getActivity().getSupportFragmentManager(), "addSubkeyDialog"); } private void cachePassphraseForEdit() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java index 7a55f9aaa..1f809cc51 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java @@ -111,7 +111,7 @@ public class ImportKeysAdapter extends ArrayAdapter { convertView = mInflater.inflate(R.layout.import_keys_list_entry, null); holder.mainUserId = (TextView) convertView.findViewById(R.id.mainUserId); holder.mainUserIdRest = (TextView) convertView.findViewById(R.id.mainUserIdRest); - holder.keyId = (TextView) convertView.findViewById(R.id.keyId); + holder.keyId = (TextView) convertView.findViewById(R.id.subkey_item_key_id); holder.fingerprint = (TextView) convertView.findViewById(R.id.view_key_fingerprint); holder.algorithm = (TextView) convertView.findViewById(R.id.algorithm); holder.status = (TextView) convertView.findViewById(R.id.status); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java index e69a63c63..0e2177568 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java @@ -152,7 +152,7 @@ abstract public class SelectKeyCursorAdapter extends CursorAdapter { holder.view = view; holder.mainUserId = (TextView) view.findViewById(R.id.mainUserId); holder.mainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest); - holder.keyId = (TextView) view.findViewById(R.id.keyId); + holder.keyId = (TextView) view.findViewById(R.id.subkey_item_key_id); holder.status = (TextView) view.findViewById(R.id.status); holder.selected = (CheckBox) view.findViewById(R.id.selected); view.setTag(holder); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAdapter.java index c2a882fdb..0d8c7240d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAdapter.java @@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.ui.adapter; import android.content.Context; import android.content.res.ColorStateList; import android.database.Cursor; +import android.graphics.Typeface; import android.support.v4.widget.CursorAdapter; import android.text.format.DateFormat; import android.view.LayoutInflater; @@ -106,15 +107,18 @@ public class SubkeysAdapter extends CursorAdapter { @Override public void bindView(View view, Context context, Cursor cursor) { - TextView vKeyId = (TextView) view.findViewById(R.id.keyId); - TextView vKeyDetails = (TextView) view.findViewById(R.id.keyDetails); - TextView vKeyExpiry = (TextView) view.findViewById(R.id.keyExpiry); - ImageView vMasterKeyIcon = (ImageView) view.findViewById(R.id.ic_masterKey); - ImageView vCertifyIcon = (ImageView) view.findViewById(R.id.ic_certifyKey); - ImageView vEncryptIcon = (ImageView) view.findViewById(R.id.ic_encryptKey); - ImageView vSignIcon = (ImageView) view.findViewById(R.id.ic_signKey); - ImageView vRevokedKeyIcon = (ImageView) view.findViewById(R.id.ic_revokedKey); - ImageView vEditImage = (ImageView) view.findViewById(R.id.user_id_item_edit_image); + TextView vKeyId = (TextView) view.findViewById(R.id.subkey_item_key_id); + TextView vKeyDetails = (TextView) view.findViewById(R.id.subkey_item_details); + TextView vKeyExpiry = (TextView) view.findViewById(R.id.subkey_item_expiry); + ImageView vCertifyIcon = (ImageView) view.findViewById(R.id.subkey_item_ic_certify); + ImageView vEncryptIcon = (ImageView) view.findViewById(R.id.subkey_item_ic_encrypt); + ImageView vSignIcon = (ImageView) view.findViewById(R.id.subkey_item_ic_sign); + ImageView vRevokedIcon = (ImageView) view.findViewById(R.id.subkey_item_ic_revoked); + ImageView vEditImage = (ImageView) view.findViewById(R.id.subkey_item_edit_image); + + // not used: + ImageView deleteImage = (ImageView) view.findViewById(R.id.subkey_item_delete_button); + deleteImage.setVisibility(View.GONE); long keyId = cursor.getLong(INDEX_KEY_ID); String keyIdStr = PgpKeyHelper.convertKeyIdToHex(keyId); @@ -133,8 +137,14 @@ public class SubkeysAdapter extends CursorAdapter { vKeyDetails.setText(algorithmStr); } + boolean isMasterKey = cursor.getInt(INDEX_RANK) == 0; + if (isMasterKey) { + vKeyId.setTypeface(null, Typeface.BOLD); + } else { + vKeyId.setTypeface(null, Typeface.NORMAL); + } + // Set icons according to properties - vMasterKeyIcon.setVisibility(cursor.getInt(INDEX_RANK) == 0 ? View.VISIBLE : View.INVISIBLE); vCertifyIcon.setVisibility(cursor.getInt(INDEX_CAN_CERTIFY) != 0 ? View.VISIBLE : View.GONE); vEncryptIcon.setVisibility(cursor.getInt(INDEX_CAN_ENCRYPT) != 0 ? View.VISIBLE : View.GONE); vSignIcon.setVisibility(cursor.getInt(INDEX_CAN_SIGN) != 0 ? View.VISIBLE : View.GONE); @@ -157,13 +167,13 @@ public class SubkeysAdapter extends CursorAdapter { } if (isRevoked) { - vRevokedKeyIcon.setVisibility(View.VISIBLE); + vRevokedIcon.setVisibility(View.VISIBLE); } else { vKeyId.setTextColor(mDefaultTextColor); vKeyDetails.setTextColor(mDefaultTextColor); vKeyExpiry.setTextColor(mDefaultTextColor); - vRevokedKeyIcon.setVisibility(View.GONE); + vRevokedIcon.setVisibility(View.GONE); } boolean isExpired; @@ -195,7 +205,7 @@ public class SubkeysAdapter extends CursorAdapter { public View newView(Context context, Cursor cursor, ViewGroup parent) { View view = mInflater.inflate(R.layout.view_key_subkey_item, null); if (mDefaultTextColor == null) { - TextView keyId = (TextView) view.findViewById(R.id.keyId); + TextView keyId = (TextView) view.findViewById(R.id.subkey_item_key_id); mDefaultTextColor = keyId.getTextColors(); } return view; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAddedAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAddedAdapter.java index 25509fee5..1ec9add68 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAddedAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAddedAdapter.java @@ -17,48 +17,28 @@ package org.sufficientlysecure.keychain.ui.adapter; -import android.annotation.TargetApi; import android.app.Activity; -import android.app.AlertDialog; import android.content.Context; -import android.content.DialogInterface; -import android.os.Build; -import android.text.Editable; -import android.text.TextUtils; -import android.text.TextWatcher; -import android.util.Patterns; +import android.text.format.DateFormat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.inputmethod.InputMethodManager; -import android.widget.AdapterView; import android.widget.ArrayAdapter; -import android.widget.AutoCompleteTextView; -import android.widget.EditText; import android.widget.ImageButton; -import android.widget.Spinner; +import android.widget.ImageView; import android.widget.TextView; -import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.helper.ContactHelper; +import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.service.SaveKeyringParcel; -import org.sufficientlysecure.keychain.ui.dialog.CreateKeyDialogFragment; -import org.sufficientlysecure.keychain.util.Choice; -import java.util.ArrayList; -import java.util.Arrays; +import java.util.Date; import java.util.List; -import java.util.regex.Matcher; public class SubkeysAddedAdapter extends ArrayAdapter { private LayoutInflater mInflater; private Activity mActivity; - public interface OnAlgorithmSelectedListener { - public void onAlgorithmSelected(Choice algorithmChoice, int keySize); - } - // hold a private reference to the underlying data List private List mData; @@ -70,12 +50,13 @@ public class SubkeysAddedAdapter extends ArrayAdapter parent, View view, int position, long id) { - Choice newKeyAlgorithmChoice = (Choice) holder.mAlgorithmSpinner.getSelectedItem(); - // update referenced model item - holder.mModel.mAlgorithm = newKeyAlgorithmChoice.getId(); - } + holder.vKeyId = (TextView) convertView.findViewById(R.id.subkey_item_key_id); + holder.vKeyDetails = (TextView) convertView.findViewById(R.id.subkey_item_details); + holder.vKeyExpiry = (TextView) convertView.findViewById(R.id.subkey_item_expiry); + holder.vCertifyIcon = (ImageView) convertView.findViewById(R.id.subkey_item_ic_certify); + holder.vEncryptIcon = (ImageView) convertView.findViewById(R.id.subkey_item_ic_encrypt); + holder.vSignIcon = (ImageView) convertView.findViewById(R.id.subkey_item_ic_sign); + holder.vRevokedIcon = (ImageView) convertView.findViewById(R.id.subkey_item_ic_revoked); + holder.vDelete = (ImageButton) convertView.findViewById(R.id.subkey_item_delete_button); + holder.vDelete.setVisibility(View.VISIBLE); // always visible + + // not used: + ImageView editImage = (ImageView) convertView.findViewById(R.id.subkey_item_edit_image); + editImage.setVisibility(View.GONE); - @Override - public void onNothingSelected(AdapterView parent) { - } - }); - - holder.mKeySizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { - Choice newKeyAlgorithmChoice = (Choice) holder.mAlgorithmSpinner.getSelectedItem(); - int newKeySize = getProperKeyLength(newKeyAlgorithmChoice.getId(), - getSelectedKeyLength(holder.mKeySizeSpinner, holder.mCustomKeyEditText)); - // update referenced model item - holder.mModel.mKeysize = newKeySize; - } - - @Override - public void onNothingSelected(AdapterView parent) { - } - }); + convertView.setTag(holder); holder.vDelete.setOnClickListener(new View.OnClickListener() { @Override @@ -136,83 +97,30 @@ public class SubkeysAddedAdapter extends ArrayAdapter choices = new ArrayList(); - choices.add(new Choice(Constants.choice.algorithm.dsa, mActivity.getResources().getString( - R.string.dsa))); - if (!wouldBeMasterKey) { - choices.add(new Choice(Constants.choice.algorithm.elgamal, mActivity.getResources().getString( - R.string.elgamal))); - } - - choices.add(new Choice(Constants.choice.algorithm.rsa, mActivity.getResources().getString( - R.string.rsa))); - - ArrayAdapter adapter = new ArrayAdapter(mActivity, - android.R.layout.simple_spinner_item, choices); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - holder.mAlgorithmSpinner.setAdapter(adapter); - // make RSA the default - for (int i = 0; i < choices.size(); ++i) { - if (choices.get(i).getId() == Constants.choice.algorithm.rsa) { - holder.mAlgorithmSpinner.setSelection(i); - break; - } - } - - // dynamic ArrayAdapter must be created (instead of ArrayAdapter.getFromResource), because it's content may change - ArrayAdapter keySizeAdapter = new ArrayAdapter(mActivity, android.R.layout.simple_spinner_item, - new ArrayList(Arrays.asList(mActivity.getResources().getStringArray(R.array.rsa_key_size_spinner_values)))); - keySizeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - holder.mKeySizeSpinner.setAdapter(keySizeAdapter); - holder.mKeySizeSpinner.setSelection(1); // Default to 4096 for the key length - - holder.mCustomKeyEditText.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - - @Override - public void afterTextChanged(Editable s) { -// setOkButtonAvailability(alertDialog); - } - }); - - holder.mKeySizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { - setCustomKeyVisibility(holder.mKeySizeSpinner, holder.mCustomKeyEditText, - holder.mCustomKeyTextView, holder.mCustomKeyInfoTextView); -// setOkButtonAvailability(alertDialog); - } - - @Override - public void onNothingSelected(AdapterView parent) { - } - }); - - holder.mAlgorithmSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { - setKeyLengthSpinnerValuesForAlgorithm(((Choice) parent.getSelectedItem()).getId(), - holder.mKeySizeSpinner, holder.mCustomKeyInfoTextView); - - setCustomKeyVisibility(holder.mKeySizeSpinner, holder.mCustomKeyEditText, - holder.mCustomKeyTextView, holder.mCustomKeyInfoTextView); -// setOkButtonAvailability(alertDialog); - } - - @Override - public void onNothingSelected(AdapterView parent) { - } - }); + String algorithmStr = PgpKeyHelper.getAlgorithmInfo( + mActivity, + holder.mModel.mAlgorithm, + holder.mModel.mKeysize + ); + holder.vKeyId.setText("new"); + holder.vKeyDetails.setText(algorithmStr); + + // Set icons according to properties +// holder.vMasterIcon.setVisibility(cursor.getInt(INDEX_RANK) == 0 ? View.VISIBLE : View.INVISIBLE); +// holder.vCertifyIcon.setVisibility(cursor.getInt(INDEX_CAN_CERTIFY) != 0 ? View.VISIBLE : View.GONE); +// holder.vEncryptIcon.setVisibility(cursor.getInt(INDEX_CAN_ENCRYPT) != 0 ? View.VISIBLE : View.GONE); +// holder.vSignIcon.setVisibility(cursor.getInt(INDEX_CAN_SIGN) != 0 ? View.VISIBLE : View.GONE); +// if (!cursor.isNull(INDEX_EXPIRY)) { +// Date expiryDate = new Date(cursor.getLong(INDEX_EXPIRY) * 1000); +// isExpired = expiryDate.before(new Date()); +// +// holder.vKeyExpiry.setText(context.getString(R.string.label_expiry) + ": " +// + DateFormat.getDateFormat(context).format(expiryDate)); +// } else { +// isExpired = false; +// +// holder.vKeyExpiry.setText(context.getString(R.string.label_expiry) + ": " + context.getString(R.string.none)); +// } // // holder.vAddress.setText(holder.mModel.address); // holder.vAddress.setThreshold(1); // Start working from first character @@ -227,135 +135,4 @@ public class SubkeysAddedAdapter extends ArrayAdapterRSA - *

for RSA algorithm, key length must be greater than 1024 (according to - * #102). Possibility to generate keys bigger - * than 8192 bits is currently disabled, because it's almost impossible to generate them on a mobile device (check - * RSA key length plot and - * Cryptographic Key Length Recommendation). Also, key length must be a - * multiplicity of 8.

- *

ElGamal

- *

For ElGamal algorithm, supported key lengths are 1536, 2048, 3072, 4096 or 8192 bits.

- *

DSA

- *

For DSA algorithm key length must be between 512 and 1024. Also, it must me dividable by 64.

- * - * @return correct key length, according to SpongyCastle specification. Returns -1, if key length is - * inappropriate. - */ - private int getProperKeyLength(int algorithmId, int currentKeyLength) { - final int[] elGamalSupportedLengths = {1536, 2048, 3072, 4096, 8192}; - int properKeyLength = -1; - switch (algorithmId) { - case Constants.choice.algorithm.rsa: - if (currentKeyLength > 1024 && currentKeyLength <= 8192) { - properKeyLength = currentKeyLength + ((8 - (currentKeyLength % 8)) % 8); - } - break; - case Constants.choice.algorithm.elgamal: - int[] elGammalKeyDiff = new int[elGamalSupportedLengths.length]; - for (int i = 0; i < elGamalSupportedLengths.length; i++) { - elGammalKeyDiff[i] = Math.abs(elGamalSupportedLengths[i] - currentKeyLength); - } - int minimalValue = Integer.MAX_VALUE; - int minimalIndex = -1; - for (int i = 0; i < elGammalKeyDiff.length; i++) { - if (elGammalKeyDiff[i] <= minimalValue) { - minimalValue = elGammalKeyDiff[i]; - minimalIndex = i; - } - } - properKeyLength = elGamalSupportedLengths[minimalIndex]; - break; - case Constants.choice.algorithm.dsa: - if (currentKeyLength >= 512 && currentKeyLength <= 1024) { - properKeyLength = currentKeyLength + ((64 - (currentKeyLength % 64)) % 64); - } - break; - } - return properKeyLength; - } - - // TODO: make this an error message on the field -// private boolean setOkButtonAvailability(AlertDialog alertDialog) { -// final Choice selectedAlgorithm = (Choice) mAlgorithmSpinner.getSelectedItem(); -// final int selectedKeySize = getSelectedKeyLength(); //Integer.parseInt((String) mKeySizeSpinner.getSelectedItem()); -// final int properKeyLength = getProperKeyLength(selectedAlgorithm.getId(), selectedKeySize); -// alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(properKeyLength > 0); -// } - - private void setCustomKeyVisibility(Spinner keySizeSpinner, EditText customkeyedittext, TextView customKeyTextView, TextView customKeyInfoTextView) { - final String selectedItemString = (String) keySizeSpinner.getSelectedItem(); - final String customLengthString = mActivity.getResources().getString(R.string.key_size_custom); - final boolean customSelected = customLengthString.equals(selectedItemString); - final int visibility = customSelected ? View.VISIBLE : View.GONE; - - customkeyedittext.setVisibility(visibility); - customKeyTextView.setVisibility(visibility); - customKeyInfoTextView.setVisibility(visibility); - - // hide keyboard after setting visibility to gone - if (visibility == View.GONE) { - InputMethodManager imm = (InputMethodManager) - mActivity.getSystemService(mActivity.INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(customkeyedittext.getWindowToken(), 0); - } - } - - private void setKeyLengthSpinnerValuesForAlgorithm(int algorithmId, Spinner keySizeSpinner, TextView customKeyInfoTextView) { - final ArrayAdapter keySizeAdapter = (ArrayAdapter) keySizeSpinner.getAdapter(); - final Object selectedItem = keySizeSpinner.getSelectedItem(); - keySizeAdapter.clear(); - switch (algorithmId) { - case Constants.choice.algorithm.rsa: - replaceArrayAdapterContent(keySizeAdapter, R.array.rsa_key_size_spinner_values); - customKeyInfoTextView.setText(mActivity.getResources().getString(R.string.key_size_custom_info_rsa)); - break; - case Constants.choice.algorithm.elgamal: - replaceArrayAdapterContent(keySizeAdapter, R.array.elgamal_key_size_spinner_values); - customKeyInfoTextView.setText(""); // ElGamal does not support custom key length - break; - case Constants.choice.algorithm.dsa: - replaceArrayAdapterContent(keySizeAdapter, R.array.dsa_key_size_spinner_values); - customKeyInfoTextView.setText(mActivity.getResources().getString(R.string.key_size_custom_info_dsa)); - break; - } - keySizeAdapter.notifyDataSetChanged(); - - // when switching algorithm, try to select same key length as before - for (int i = 0; i < keySizeAdapter.getCount(); i++) { - if (selectedItem.equals(keySizeAdapter.getItem(i))) { - keySizeSpinner.setSelection(i); - break; - } - } - } - - @TargetApi(Build.VERSION_CODES.HONEYCOMB) - private void replaceArrayAdapterContent(ArrayAdapter arrayAdapter, int stringArrayResourceId) { - final String[] spinnerValuesStringArray = mActivity.getResources().getStringArray(stringArrayResourceId); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { - arrayAdapter.addAll(spinnerValuesStringArray); - } else { - for (final String value : spinnerValuesStringArray) { - arrayAdapter.add(value); - } - } - } - } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddSubkeyDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddSubkeyDialogFragment.java new file mode 100644 index 000000000..2031eae2a --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/AddSubkeyDialogFragment.java @@ -0,0 +1,328 @@ +/* + * Copyright (C) 2012-2013 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui.dialog; + +import android.annotation.TargetApi; +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.DialogInterface; +import android.os.Build; +import android.os.Bundle; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.FragmentActivity; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.LayoutInflater; +import android.view.View; +import android.view.inputmethod.InputMethodManager; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.EditText; +import android.widget.Spinner; +import android.widget.TextView; + +import org.spongycastle.bcpg.sig.KeyFlags; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.service.SaveKeyringParcel; +import org.sufficientlysecure.keychain.util.Choice; + +import java.util.ArrayList; +import java.util.Arrays; + +public class AddSubkeyDialogFragment extends DialogFragment { + + public interface OnAlgorithmSelectedListener { + public void onAlgorithmSelected(SaveKeyringParcel.SubkeyAdd newSubkey); + } + + private static final String ARG_WILL_BE_MASTER_KEY = "will_be_master_key"; + + private OnAlgorithmSelectedListener mAlgorithmSelectedListener; + private Spinner mAlgorithmSpinner; + private Spinner mKeySizeSpinner; + private TextView mCustomKeyTextView; + private EditText mCustomKeyEditText; + private TextView mCustomKeyInfoTextView; + + public void setOnAlgorithmSelectedListener(OnAlgorithmSelectedListener listener) { + mAlgorithmSelectedListener = listener; + } + + public static AddSubkeyDialogFragment newInstance(boolean willBeMasterKey) { + AddSubkeyDialogFragment frag = new AddSubkeyDialogFragment(); + Bundle args = new Bundle(); + + args.putBoolean(ARG_WILL_BE_MASTER_KEY, willBeMasterKey); + + frag.setArguments(args); + + return frag; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + final FragmentActivity context = getActivity(); + final LayoutInflater mInflater; + + final boolean willBeMasterKey = getArguments().getBoolean(ARG_WILL_BE_MASTER_KEY); + mInflater = context.getLayoutInflater(); + + CustomAlertDialogBuilder dialog = new CustomAlertDialogBuilder(context); + + View view = mInflater.inflate(R.layout.create_key_dialog, null); + dialog.setView(view); + dialog.setTitle(R.string.title_create_key); + + mAlgorithmSpinner = (Spinner) view.findViewById(R.id.create_key_algorithm); + ArrayList choices = new ArrayList(); + choices.add(new Choice(Constants.choice.algorithm.dsa, getResources().getString( + R.string.dsa))); + if (!willBeMasterKey) { + choices.add(new Choice(Constants.choice.algorithm.elgamal, getResources().getString( + R.string.elgamal))); + } + + choices.add(new Choice(Constants.choice.algorithm.rsa, getResources().getString( + R.string.rsa))); + + ArrayAdapter adapter = new ArrayAdapter(context, + android.R.layout.simple_spinner_item, choices); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + mAlgorithmSpinner.setAdapter(adapter); + // make RSA the default + for (int i = 0; i < choices.size(); ++i) { + if (choices.get(i).getId() == Constants.choice.algorithm.rsa) { + mAlgorithmSpinner.setSelection(i); + break; + } + } + + mKeySizeSpinner = (Spinner) view.findViewById(R.id.create_key_size); + // dynamic ArrayAdapter must be created (instead of ArrayAdapter.getFromResource), because it's content may change + ArrayAdapter keySizeAdapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item, + new ArrayList(Arrays.asList(getResources().getStringArray(R.array.rsa_key_size_spinner_values)))); + keySizeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + mKeySizeSpinner.setAdapter(keySizeAdapter); + mKeySizeSpinner.setSelection(1); // Default to 4096 for the key length + + mCustomKeyTextView = (TextView) view.findViewById(R.id.custom_key_size_label); + mCustomKeyEditText = (EditText) view.findViewById(R.id.custom_key_size_input); + mCustomKeyInfoTextView = (TextView) view.findViewById(R.id.custom_key_size_info); + + dialog.setPositiveButton(android.R.string.ok, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface di, int id) { + di.dismiss(); + Choice newKeyAlgorithmChoice = (Choice) mAlgorithmSpinner.getSelectedItem(); + int newKeySize = getProperKeyLength(newKeyAlgorithmChoice.getId(), getSelectedKeyLength()); + SaveKeyringParcel.SubkeyAdd newSubkey = new SaveKeyringParcel.SubkeyAdd( + newKeyAlgorithmChoice.getId(), + newKeySize, + KeyFlags.SIGN_DATA, //TODO + null + ); + mAlgorithmSelectedListener.onAlgorithmSelected(newSubkey); + } + } + ); + + dialog.setCancelable(true); + dialog.setNegativeButton(android.R.string.cancel, + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface di, int id) { + di.dismiss(); + } + }); + + final AlertDialog alertDialog = dialog.show(); + + mCustomKeyEditText.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + + @Override + public void afterTextChanged(Editable s) { + setOkButtonAvailability(alertDialog); + } + }); + + mKeySizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + setCustomKeyVisibility(); + setOkButtonAvailability(alertDialog); + } + + @Override + public void onNothingSelected(AdapterView parent) { + } + }); + + mAlgorithmSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + setKeyLengthSpinnerValuesForAlgorithm(((Choice) parent.getSelectedItem()).getId()); + + setCustomKeyVisibility(); + setOkButtonAvailability(alertDialog); + } + + @Override + public void onNothingSelected(AdapterView parent) { + } + }); + + return alertDialog; + } + + private int getSelectedKeyLength() { + final String selectedItemString = (String) mKeySizeSpinner.getSelectedItem(); + final String customLengthString = getResources().getString(R.string.key_size_custom); + final boolean customSelected = customLengthString.equals(selectedItemString); + String keyLengthString = customSelected ? mCustomKeyEditText.getText().toString() : selectedItemString; + int keySize; + try { + keySize = Integer.parseInt(keyLengthString); + } catch (NumberFormatException e) { + keySize = 0; + } + return keySize; + } + + /** + *

RSA

+ *

for RSA algorithm, key length must be greater than 1024 (according to + * #102). Possibility to generate keys bigger + * than 8192 bits is currently disabled, because it's almost impossible to generate them on a mobile device (check + * RSA key length plot and + * Cryptographic Key Length Recommendation). Also, key length must be a + * multiplicity of 8.

+ *

ElGamal

+ *

For ElGamal algorithm, supported key lengths are 1536, 2048, 3072, 4096 or 8192 bits.

+ *

DSA

+ *

For DSA algorithm key length must be between 512 and 1024. Also, it must me dividable by 64.

+ * + * @return correct key length, according to SpongyCastle specification. Returns -1, if key length is + * inappropriate. + */ + private int getProperKeyLength(int algorithmId, int currentKeyLength) { + final int[] elGamalSupportedLengths = {1536, 2048, 3072, 4096, 8192}; + int properKeyLength = -1; + switch (algorithmId) { + case Constants.choice.algorithm.rsa: + if (currentKeyLength > 1024 && currentKeyLength <= 8192) { + properKeyLength = currentKeyLength + ((8 - (currentKeyLength % 8)) % 8); + } + break; + case Constants.choice.algorithm.elgamal: + int[] elGammalKeyDiff = new int[elGamalSupportedLengths.length]; + for (int i = 0; i < elGamalSupportedLengths.length; i++) { + elGammalKeyDiff[i] = Math.abs(elGamalSupportedLengths[i] - currentKeyLength); + } + int minimalValue = Integer.MAX_VALUE; + int minimalIndex = -1; + for (int i = 0; i < elGammalKeyDiff.length; i++) { + if (elGammalKeyDiff[i] <= minimalValue) { + minimalValue = elGammalKeyDiff[i]; + minimalIndex = i; + } + } + properKeyLength = elGamalSupportedLengths[minimalIndex]; + break; + case Constants.choice.algorithm.dsa: + if (currentKeyLength >= 512 && currentKeyLength <= 1024) { + properKeyLength = currentKeyLength + ((64 - (currentKeyLength % 64)) % 64); + } + break; + } + return properKeyLength; + } + + private void setOkButtonAvailability(AlertDialog alertDialog) { + final Choice selectedAlgorithm = (Choice) mAlgorithmSpinner.getSelectedItem(); + final int selectedKeySize = getSelectedKeyLength(); //Integer.parseInt((String) mKeySizeSpinner.getSelectedItem()); + final int properKeyLength = getProperKeyLength(selectedAlgorithm.getId(), selectedKeySize); + alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(properKeyLength > 0); + } + + private void setCustomKeyVisibility() { + final String selectedItemString = (String) mKeySizeSpinner.getSelectedItem(); + final String customLengthString = getResources().getString(R.string.key_size_custom); + final boolean customSelected = customLengthString.equals(selectedItemString); + final int visibility = customSelected ? View.VISIBLE : View.GONE; + + mCustomKeyEditText.setVisibility(visibility); + mCustomKeyTextView.setVisibility(visibility); + mCustomKeyInfoTextView.setVisibility(visibility); + + // hide keyboard after setting visibility to gone + if (visibility == View.GONE) { + InputMethodManager imm = (InputMethodManager) + getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(mCustomKeyEditText.getWindowToken(), 0); + } + } + + private void setKeyLengthSpinnerValuesForAlgorithm(int algorithmId) { + final ArrayAdapter keySizeAdapter = (ArrayAdapter) mKeySizeSpinner.getAdapter(); + final Object selectedItem = mKeySizeSpinner.getSelectedItem(); + keySizeAdapter.clear(); + switch (algorithmId) { + case Constants.choice.algorithm.rsa: + replaceArrayAdapterContent(keySizeAdapter, R.array.rsa_key_size_spinner_values); + mCustomKeyInfoTextView.setText(getResources().getString(R.string.key_size_custom_info_rsa)); + break; + case Constants.choice.algorithm.elgamal: + replaceArrayAdapterContent(keySizeAdapter, R.array.elgamal_key_size_spinner_values); + mCustomKeyInfoTextView.setText(""); // ElGamal does not support custom key length + break; + case Constants.choice.algorithm.dsa: + replaceArrayAdapterContent(keySizeAdapter, R.array.dsa_key_size_spinner_values); + mCustomKeyInfoTextView.setText(getResources().getString(R.string.key_size_custom_info_dsa)); + break; + } + keySizeAdapter.notifyDataSetChanged(); + + // when switching algorithm, try to select same key length as before + for (int i = 0; i < keySizeAdapter.getCount(); i++) { + if (selectedItem.equals(keySizeAdapter.getItem(i))) { + mKeySizeSpinner.setSelection(i); + break; + } + } + } + + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + private void replaceArrayAdapterContent(ArrayAdapter arrayAdapter, int stringArrayResourceId) { + final String[] spinnerValuesStringArray = getResources().getStringArray(stringArrayResourceId); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + arrayAdapter.addAll(spinnerValuesStringArray); + } else { + for (final String value : spinnerValuesStringArray) { + arrayAdapter.add(value); + } + } + } + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/CreateKeyDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/CreateKeyDialogFragment.java deleted file mode 100644 index 920743a9b..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/CreateKeyDialogFragment.java +++ /dev/null @@ -1,322 +0,0 @@ -/* - * Copyright (C) 2012-2013 Dominik Schürmann - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.sufficientlysecure.keychain.ui.dialog; - -import android.annotation.TargetApi; -import android.app.AlertDialog; -import android.app.Dialog; -import android.content.DialogInterface; -import android.os.Build; -import android.os.Bundle; -import android.support.v4.app.DialogFragment; -import android.support.v4.app.FragmentActivity; -import android.text.Editable; -import android.text.TextWatcher; -import android.view.LayoutInflater; -import android.view.View; -import android.view.inputmethod.InputMethodManager; -import android.widget.AdapterView; -import android.widget.ArrayAdapter; -import android.widget.EditText; -import android.widget.Spinner; -import android.widget.TextView; - -import org.sufficientlysecure.keychain.Constants; -import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.util.Choice; - -import java.util.ArrayList; -import java.util.Arrays; - -public class CreateKeyDialogFragment extends DialogFragment { - - public interface OnAlgorithmSelectedListener { - public void onAlgorithmSelected(Choice algorithmChoice, int keySize); - } - - private static final String ARG_EDITOR_CHILD_COUNT = "child_count"; - - private OnAlgorithmSelectedListener mAlgorithmSelectedListener; - private Spinner mAlgorithmSpinner; - private Spinner mKeySizeSpinner; - private TextView mCustomKeyTextView; - private EditText mCustomKeyEditText; - private TextView mCustomKeyInfoTextView; - - public void setOnAlgorithmSelectedListener(OnAlgorithmSelectedListener listener) { - mAlgorithmSelectedListener = listener; - } - - public static CreateKeyDialogFragment newInstance(int mEditorChildCount) { - CreateKeyDialogFragment frag = new CreateKeyDialogFragment(); - Bundle args = new Bundle(); - - args.putInt(ARG_EDITOR_CHILD_COUNT, mEditorChildCount); - - frag.setArguments(args); - - return frag; - } - - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - final FragmentActivity context = getActivity(); - final LayoutInflater mInflater; - - final int childCount = getArguments().getInt(ARG_EDITOR_CHILD_COUNT); - mInflater = context.getLayoutInflater(); - - CustomAlertDialogBuilder dialog = new CustomAlertDialogBuilder(context); - - View view = mInflater.inflate(R.layout.create_key_dialog, null); - dialog.setView(view); - dialog.setTitle(R.string.title_create_key); - - boolean wouldBeMasterKey = (childCount == 0); - - mAlgorithmSpinner = (Spinner) view.findViewById(R.id.create_key_algorithm); - ArrayList choices = new ArrayList(); - choices.add(new Choice(Constants.choice.algorithm.dsa, getResources().getString( - R.string.dsa))); - if (!wouldBeMasterKey) { - choices.add(new Choice(Constants.choice.algorithm.elgamal, getResources().getString( - R.string.elgamal))); - } - - choices.add(new Choice(Constants.choice.algorithm.rsa, getResources().getString( - R.string.rsa))); - - ArrayAdapter adapter = new ArrayAdapter(context, - android.R.layout.simple_spinner_item, choices); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - mAlgorithmSpinner.setAdapter(adapter); - // make RSA the default - for (int i = 0; i < choices.size(); ++i) { - if (choices.get(i).getId() == Constants.choice.algorithm.rsa) { - mAlgorithmSpinner.setSelection(i); - break; - } - } - - mKeySizeSpinner = (Spinner) view.findViewById(R.id.create_key_size); - // dynamic ArrayAdapter must be created (instead of ArrayAdapter.getFromResource), because it's content may change - ArrayAdapter keySizeAdapter = new ArrayAdapter(context, android.R.layout.simple_spinner_item, - new ArrayList(Arrays.asList(getResources().getStringArray(R.array.rsa_key_size_spinner_values)))); - keySizeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - mKeySizeSpinner.setAdapter(keySizeAdapter); - mKeySizeSpinner.setSelection(1); // Default to 4096 for the key length - - mCustomKeyTextView = (TextView) view.findViewById(R.id.custom_key_size_label); - mCustomKeyEditText = (EditText) view.findViewById(R.id.custom_key_size_input); - mCustomKeyInfoTextView = (TextView) view.findViewById(R.id.custom_key_size_info); - - dialog.setPositiveButton(android.R.string.ok, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface di, int id) { - di.dismiss(); - Choice newKeyAlgorithmChoice = (Choice) mAlgorithmSpinner.getSelectedItem(); - int newKeySize = getProperKeyLength(newKeyAlgorithmChoice.getId(), getSelectedKeyLength()); - mAlgorithmSelectedListener.onAlgorithmSelected(newKeyAlgorithmChoice, newKeySize); - } - } - ); - - dialog.setCancelable(true); - dialog.setNegativeButton(android.R.string.cancel, - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface di, int id) { - di.dismiss(); - } - }); - - final AlertDialog alertDialog = dialog.show(); - - mCustomKeyEditText.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - - @Override - public void afterTextChanged(Editable s) { - setOkButtonAvailability(alertDialog); - } - }); - - mKeySizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { - setCustomKeyVisibility(); - setOkButtonAvailability(alertDialog); - } - - @Override - public void onNothingSelected(AdapterView parent) { - } - }); - - mAlgorithmSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView parent, View view, int position, long id) { - setKeyLengthSpinnerValuesForAlgorithm(((Choice) parent.getSelectedItem()).getId()); - - setCustomKeyVisibility(); - setOkButtonAvailability(alertDialog); - } - - @Override - public void onNothingSelected(AdapterView parent) { - } - }); - - return alertDialog; - } - - private int getSelectedKeyLength() { - final String selectedItemString = (String) mKeySizeSpinner.getSelectedItem(); - final String customLengthString = getResources().getString(R.string.key_size_custom); - final boolean customSelected = customLengthString.equals(selectedItemString); - String keyLengthString = customSelected ? mCustomKeyEditText.getText().toString() : selectedItemString; - int keySize; - try { - keySize = Integer.parseInt(keyLengthString); - } catch (NumberFormatException e) { - keySize = 0; - } - return keySize; - } - - /** - *

RSA

- *

for RSA algorithm, key length must be greater than 1024 (according to - * #102). Possibility to generate keys bigger - * than 8192 bits is currently disabled, because it's almost impossible to generate them on a mobile device (check - * RSA key length plot and - * Cryptographic Key Length Recommendation). Also, key length must be a - * multiplicity of 8.

- *

ElGamal

- *

For ElGamal algorithm, supported key lengths are 1536, 2048, 3072, 4096 or 8192 bits.

- *

DSA

- *

For DSA algorithm key length must be between 512 and 1024. Also, it must me dividable by 64.

- * - * @return correct key length, according to SpongyCastle specification. Returns -1, if key length is - * inappropriate. - */ - private int getProperKeyLength(int algorithmId, int currentKeyLength) { - final int[] elGamalSupportedLengths = {1536, 2048, 3072, 4096, 8192}; - int properKeyLength = -1; - switch (algorithmId) { - case Constants.choice.algorithm.rsa: - if (currentKeyLength > 1024 && currentKeyLength <= 8192) { - properKeyLength = currentKeyLength + ((8 - (currentKeyLength % 8)) % 8); - } - break; - case Constants.choice.algorithm.elgamal: - int[] elGammalKeyDiff = new int[elGamalSupportedLengths.length]; - for (int i = 0; i < elGamalSupportedLengths.length; i++) { - elGammalKeyDiff[i] = Math.abs(elGamalSupportedLengths[i] - currentKeyLength); - } - int minimalValue = Integer.MAX_VALUE; - int minimalIndex = -1; - for (int i = 0; i < elGammalKeyDiff.length; i++) { - if (elGammalKeyDiff[i] <= minimalValue) { - minimalValue = elGammalKeyDiff[i]; - minimalIndex = i; - } - } - properKeyLength = elGamalSupportedLengths[minimalIndex]; - break; - case Constants.choice.algorithm.dsa: - if (currentKeyLength >= 512 && currentKeyLength <= 1024) { - properKeyLength = currentKeyLength + ((64 - (currentKeyLength % 64)) % 64); - } - break; - } - return properKeyLength; - } - - private void setOkButtonAvailability(AlertDialog alertDialog) { - final Choice selectedAlgorithm = (Choice) mAlgorithmSpinner.getSelectedItem(); - final int selectedKeySize = getSelectedKeyLength(); //Integer.parseInt((String) mKeySizeSpinner.getSelectedItem()); - final int properKeyLength = getProperKeyLength(selectedAlgorithm.getId(), selectedKeySize); - alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(properKeyLength > 0); - } - - private void setCustomKeyVisibility() { - final String selectedItemString = (String) mKeySizeSpinner.getSelectedItem(); - final String customLengthString = getResources().getString(R.string.key_size_custom); - final boolean customSelected = customLengthString.equals(selectedItemString); - final int visibility = customSelected ? View.VISIBLE : View.GONE; - - mCustomKeyEditText.setVisibility(visibility); - mCustomKeyTextView.setVisibility(visibility); - mCustomKeyInfoTextView.setVisibility(visibility); - - // hide keyboard after setting visibility to gone - if (visibility == View.GONE) { - InputMethodManager imm = (InputMethodManager) - getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(mCustomKeyEditText.getWindowToken(), 0); - } - } - - private void setKeyLengthSpinnerValuesForAlgorithm(int algorithmId) { - final ArrayAdapter keySizeAdapter = (ArrayAdapter) mKeySizeSpinner.getAdapter(); - final Object selectedItem = mKeySizeSpinner.getSelectedItem(); - keySizeAdapter.clear(); - switch (algorithmId) { - case Constants.choice.algorithm.rsa: - replaceArrayAdapterContent(keySizeAdapter, R.array.rsa_key_size_spinner_values); - mCustomKeyInfoTextView.setText(getResources().getString(R.string.key_size_custom_info_rsa)); - break; - case Constants.choice.algorithm.elgamal: - replaceArrayAdapterContent(keySizeAdapter, R.array.elgamal_key_size_spinner_values); - mCustomKeyInfoTextView.setText(""); // ElGamal does not support custom key length - break; - case Constants.choice.algorithm.dsa: - replaceArrayAdapterContent(keySizeAdapter, R.array.dsa_key_size_spinner_values); - mCustomKeyInfoTextView.setText(getResources().getString(R.string.key_size_custom_info_dsa)); - break; - } - keySizeAdapter.notifyDataSetChanged(); - - // when switching algorithm, try to select same key length as before - for (int i = 0; i < keySizeAdapter.getCount(); i++) { - if (selectedItem.equals(keySizeAdapter.getItem(i))) { - mKeySizeSpinner.setSelection(i); - break; - } - } - } - - @TargetApi(Build.VERSION_CODES.HONEYCOMB) - private void replaceArrayAdapterContent(ArrayAdapter arrayAdapter, int stringArrayResourceId) { - final String[] spinnerValuesStringArray = getResources().getStringArray(stringArrayResourceId); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { - arrayAdapter.addAll(spinnerValuesStringArray); - } else { - for (final String value : spinnerValuesStringArray) { - arrayAdapter.add(value); - } - } - } - -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/KeyEditor.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/KeyEditor.java index c23b4c3ff..484ce8e47 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/KeyEditor.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/KeyEditor.java @@ -120,7 +120,7 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener { setAlwaysDrawnWithCacheEnabled(true); mAlgorithm = (TextView) findViewById(R.id.algorithm); - mKeyId = (TextView) findViewById(R.id.keyId); + mKeyId = (TextView) findViewById(R.id.subkey_item_key_id); mCreationDate = (TextView) findViewById(R.id.creation); mExpiryDateButton = (Button) findViewById(R.id.expiry); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java index 35170fc37..190db1fe3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/SectionView.java @@ -17,13 +17,7 @@ package org.sufficientlysecure.keychain.ui.widget; -import android.app.ProgressDialog; import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.os.Bundle; -import android.os.Message; -import android.os.Messenger; import android.support.v7.app.ActionBarActivity; import android.util.AttributeSet; import android.view.LayoutInflater; @@ -36,10 +30,8 @@ import android.widget.ImageButton; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.UncachedSecretKey; -import org.sufficientlysecure.keychain.service.KeychainIntentService; -import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; import org.sufficientlysecure.keychain.service.PassphraseCacheService; -import org.sufficientlysecure.keychain.ui.dialog.CreateKeyDialogFragment; +import org.sufficientlysecure.keychain.ui.dialog.AddSubkeyDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment; import org.sufficientlysecure.keychain.ui.widget.Editor.EditorListener; import org.sufficientlysecure.keychain.util.Choice; @@ -280,19 +272,19 @@ public class SectionView extends LinearLayout implements OnClickListener, Editor } case TYPE_KEY: { - CreateKeyDialogFragment mCreateKeyDialogFragment = - CreateKeyDialogFragment.newInstance(mEditors.getChildCount()); - mCreateKeyDialogFragment - .setOnAlgorithmSelectedListener( - new CreateKeyDialogFragment.OnAlgorithmSelectedListener() { - @Override - public void onAlgorithmSelected(Choice algorithmChoice, int keySize) { - mNewKeyAlgorithmChoice = algorithmChoice; - mNewKeySize = keySize; - createKey(); - } - }); - mCreateKeyDialogFragment.show(mActivity.getSupportFragmentManager(), "createKeyDialog"); +// AddSubkeyDialogFragment mCreateKeyDialogFragment = +// AddSubkeyDialogFragment.newInstance(mEditors.getChildCount()); +// mCreateKeyDialogFragment +// .setOnAlgorithmSelectedListener( +// new AddSubkeyDialogFragment.OnAlgorithmSelectedListener() { +// @Override +// public void onAlgorithmSelected(Choice algorithmChoice, int keySize) { +// mNewKeyAlgorithmChoice = algorithmChoice; +// mNewKeySize = keySize; +// createKey(); +// } +// }); +// mCreateKeyDialogFragment.show(mActivity.getSupportFragmentManager(), "createKeyDialog"); break; } diff --git a/OpenKeychain/src/main/res/layout/create_key_dialog.xml b/OpenKeychain/src/main/res/layout/create_key_dialog.xml index 16eef08c4..31c5fbefc 100644 --- a/OpenKeychain/src/main/res/layout/create_key_dialog.xml +++ b/OpenKeychain/src/main/res/layout/create_key_dialog.xml @@ -1,28 +1,104 @@ + android:layout_height="match_parent"> + android:stretchColumns="1"> - + + + + +