Merge pull request #539 from gogowitczak/custom-key-length-info

Modified Spinner values for CreateKeyDialog. Added info for user about custom key length restrictions.
This commit is contained in:
Dominik Schürmann 2014-04-09 00:51:58 +02:00
commit e9efd0ec87
4 changed files with 112 additions and 42 deletions

View File

@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.ui.dialog;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
@ -37,6 +38,7 @@ import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.util.Choice; import org.sufficientlysecure.keychain.util.Choice;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
public class CreateKeyDialogFragment extends DialogFragment { public class CreateKeyDialogFragment extends DialogFragment {
@ -53,6 +55,7 @@ public class CreateKeyDialogFragment extends DialogFragment {
private Spinner mKeySizeSpinner; private Spinner mKeySizeSpinner;
private TextView mCustomKeyTextView; private TextView mCustomKeyTextView;
private EditText mCustomKeyEditText; private EditText mCustomKeyEditText;
private TextView mCustomKeyInfoTextView;
public void setOnAlgorithmSelectedListener(OnAlgorithmSelectedListener listener) { public void setOnAlgorithmSelectedListener(OnAlgorithmSelectedListener listener) {
mAlgorithmSelectedListener = listener; mAlgorithmSelectedListener = listener;
@ -110,32 +113,16 @@ public class CreateKeyDialogFragment extends DialogFragment {
} }
mKeySizeSpinner = (Spinner) view.findViewById(R.id.create_key_size); mKeySizeSpinner = (Spinner) view.findViewById(R.id.create_key_size);
ArrayAdapter<CharSequence> keySizeAdapter = ArrayAdapter.createFromResource( // dynamic ArrayAdapter must be created (instead of ArrayAdapter.getFromResource), because it's content may change
context, R.array.key_size_spinner_values, ArrayAdapter<CharSequence> keySizeAdapter = new ArrayAdapter<CharSequence>(context, android.R.layout.simple_spinner_item,
android.R.layout.simple_spinner_item); new ArrayList<CharSequence>(Arrays.asList(getResources().getStringArray(R.array.rsa_key_size_spinner_values))));
keySizeAdapter keySizeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mKeySizeSpinner.setAdapter(keySizeAdapter); mKeySizeSpinner.setAdapter(keySizeAdapter);
mKeySizeSpinner.setSelection(3); // Default to 4096 for the key length mKeySizeSpinner.setSelection(1); // Default to 4096 for the key length
mCustomKeyTextView = (TextView) view.findViewById(R.id.custom_key_size_label); mCustomKeyTextView = (TextView) view.findViewById(R.id.custom_key_size_label);
mCustomKeyEditText = (EditText) view.findViewById(R.id.custom_key_size_input); mCustomKeyEditText = (EditText) view.findViewById(R.id.custom_key_size_input);
mCustomKeyInfoTextView = (TextView) view.findViewById(R.id.custom_key_size_info);
final AdapterView.OnItemSelectedListener customKeySelectedLisener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final String selectedItemString = (String) parent.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);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
dialog.setPositiveButton(android.R.string.ok, dialog.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@ -158,20 +145,6 @@ public class CreateKeyDialogFragment extends DialogFragment {
final AlertDialog alertDialog = dialog.create(); final AlertDialog alertDialog = dialog.create();
final AdapterView.OnItemSelectedListener weakRsaListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (mKeySizeSpinner == parent) {
customKeySelectedLisener.onItemSelected(parent, view, position, id);
}
setOkButtonAvailability(alertDialog);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
mCustomKeyEditText.addTextChangedListener(new TextWatcher() { mCustomKeyEditText.addTextChangedListener(new TextWatcher() {
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { public void beforeTextChanged(CharSequence s, int start, int count, int after) {
@ -187,8 +160,31 @@ public class CreateKeyDialogFragment extends DialogFragment {
} }
}); });
mKeySizeSpinner.setOnItemSelectedListener(weakRsaListener); mKeySizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
mAlgorithmSpinner.setOnItemSelectedListener(weakRsaListener); @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; return alertDialog;
} }
@ -263,4 +259,55 @@ public class CreateKeyDialogFragment extends DialogFragment {
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(properKeyLength > 0); 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);
}
private void setKeyLengthSpinnerValuesForAlgorithm(int algorithmId) {
final ArrayAdapter<CharSequence> keySizeAdapter = (ArrayAdapter<CharSequence>) mKeySizeSpinner.getAdapter();
final Object selectedItem = mKeySizeSpinner.getSelectedItem();
keySizeAdapter.clear();
switch (algorithmId) {
case Id.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 Id.choice.algorithm.elgamal:
replaceArrayAdapterContent(keySizeAdapter, R.array.elgamal_key_size_spinner_values);
mCustomKeyInfoTextView.setText(""); // ElGamal does not support custom key length
break;
case Id.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;
}
}
}
private void replaceArrayAdapterContent(ArrayAdapter<CharSequence> 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);
}
}
}
} }

View File

@ -74,6 +74,13 @@
android:inputType="number" android:inputType="number"
android:visibility="gone"/> android:visibility="gone"/>
<TextView
android:id="@+id/custom_key_size_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="4dp" />
</TableLayout> </TableLayout>
</ScrollView> </ScrollView>

View File

@ -29,14 +29,25 @@
<item>28800</item> <item>28800</item>
<item>-1</item> <item>-1</item>
</string-array> </string-array>
<string-array name="key_size_spinner_values" translatable="false"> <string-array name="rsa_key_size_spinner_values" translatable="false">
<item>@string/key_size_512</item>
<item>@string/key_size_1024</item>
<item>@string/key_size_2048</item> <item>@string/key_size_2048</item>
<item>@string/key_size_4096</item> <item>@string/key_size_4096</item>
<item>@string/key_size_8192</item> <item>@string/key_size_8192</item>
<item>@string/key_size_custom</item> <item>@string/key_size_custom</item>
</string-array> </string-array>
<string-array name="elgamal_key_size_spinner_values" translatable="false">
<item>@string/key_size_1536</item>
<item>@string/key_size_2048</item>
<item>@string/key_size_3072</item>
<item>@string/key_size_4096</item>
<item>@string/key_size_8192</item>
</string-array>
<string-array name="dsa_key_size_spinner_values" translatable="false">
<item>@string/key_size_512</item>
<item>@string/key_size_768</item>
<item>@string/key_size_1024</item>
<item>@string/key_size_custom</item>
</string-array>
<string-array name="import_action_list" translatable="false"> <string-array name="import_action_list" translatable="false">
<item>@string/menu_import_from_key_server</item> <item>@string/menu_import_from_key_server</item>
<item>@string/menu_import_from_file</item> <item>@string/menu_import_from_file</item>

View File

@ -249,7 +249,7 @@
<string name="key_exported">Successfully exported 1 key.</string> <string name="key_exported">Successfully exported 1 key.</string>
<string name="keys_exported">Successfully exported %d keys.</string> <string name="keys_exported">Successfully exported %d keys.</string>
<string name="no_keys_exported">No keys exported.</string> <string name="no_keys_exported">No keys exported.</string>
<string name="key_creation_el_gamal_info">Note: only subkeys support ElGamal, and for ElGamal the nearest keysize of 1536, 2048, 3072, 4096, or 8192 will be used.</string> <string name="key_creation_el_gamal_info">Note: only subkeys support ElGamal.</string>
<string name="key_creation_weak_rsa_info">Note: generating RSA key with length 1024-bit and less is considered unsafe and it\'s disabled for generating new keys.</string> <string name="key_creation_weak_rsa_info">Note: generating RSA key with length 1024-bit and less is considered unsafe and it\'s disabled for generating new keys.</string>
<string name="key_not_found">Couldn\'t find key %08X.</string> <string name="key_not_found">Couldn\'t find key %08X.</string>
@ -375,12 +375,17 @@
<!-- key bit length selections --> <!-- key bit length selections -->
<string name="key_size_512">512</string> <string name="key_size_512">512</string>
<string name="key_size_768">768</string>
<string name="key_size_1024">1024</string> <string name="key_size_1024">1024</string>
<string name="key_size_1536">1536</string>
<string name="key_size_2048">2048</string> <string name="key_size_2048">2048</string>
<string name="key_size_3072">3072</string>
<string name="key_size_4096">4096</string> <string name="key_size_4096">4096</string>
<string name="key_size_8192">8192</string> <string name="key_size_8192">8192</string>
<string name="key_size_custom">Custom key size</string> <string name="key_size_custom">Custom key size</string>
<string name="key_size_custom_info">Type custom key length (in bits):</string> <string name="key_size_custom_info">Type custom key length (in bits):</string>
<string name="key_size_custom_info_rsa">RSA key length must be greater than 1024 and at most 8192. Also it must be multiplicity of 8.</string>
<string name="key_size_custom_info_dsa">DSA key length must be at least 512 and at most 1024. Also it must be multiplicity of 64.</string>
<!-- compression --> <!-- compression -->
<string name="compression_fast">fast</string> <string name="compression_fast">fast</string>