diff options
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java | 127 |
1 files changed, 52 insertions, 75 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java b/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java index 72d1c024..476ca6b1 100644 --- a/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java +++ b/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java @@ -8,14 +8,11 @@ import android.app.ProgressDialog; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; -import android.content.DialogInterface; import android.content.SharedPreferences; import android.graphics.Typeface; import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatActivity; import android.text.InputType; import android.view.LayoutInflater; import android.view.View; @@ -24,15 +21,14 @@ import android.view.inputmethod.InputMethodManager; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; -import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; - +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; import com.jcraft.jsch.JSch; import com.jcraft.jsch.KeyPair; - import org.apache.commons.io.FileUtils; import java.io.File; @@ -41,6 +37,34 @@ import java.lang.ref.WeakReference; public class SshKeyGen extends AppCompatActivity { + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + if (getSupportActionBar() != null) + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + setTitle("Generate SSH Key"); + + if (savedInstanceState == null) { + getFragmentManager().beginTransaction() + .replace(android.R.id.content, new SshKeyGenFragment()).commit(); + } + } + + // Invoked when 'Generate' button of SshKeyGenFragment clicked. Generates a + // private and public key, then replaces the SshKeyGenFragment with a + // ShowSshKeyFragment which displays the public key. + public void generate(View view) { + String length = Integer.toString((Integer) ((Spinner) findViewById(R.id.length)).getSelectedItem()); + String passphrase = ((EditText) findViewById(R.id.passphrase)).getText().toString(); + String comment = ((EditText) findViewById(R.id.comment)).getText().toString(); + new KeyGenerateTask(this).execute(length, passphrase, comment); + + InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(view.getWindowToken(), 0); + } + // SSH key generation UI public static class SshKeyGenFragment extends Fragment { public SshKeyGenFragment() { @@ -61,18 +85,15 @@ public class SshKeyGen extends AppCompatActivity { ((EditText) v.findViewById(R.id.passphrase)).setTypeface(monoTypeface); CheckBox checkbox = v.findViewById(R.id.show_passphrase); - checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - EditText editText = v.findViewById(R.id.passphrase); - int selection = editText.getSelectionEnd(); - if (isChecked) { - editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); - } else { - editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); - } - editText.setSelection(selection); + checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> { + EditText editText = v.findViewById(R.id.passphrase); + int selection = editText.getSelectionEnd(); + if (isChecked) { + editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); + } else { + editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); } + editText.setSelection(selection); }); return v; @@ -100,59 +121,31 @@ public class SshKeyGen extends AppCompatActivity { e.printStackTrace(); } - builder.setPositiveButton(getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - if (getActivity() instanceof SshKeyGen) - getActivity().finish(); - } + builder.setPositiveButton(getResources().getString(R.string.dialog_ok), (dialog, which) -> { + if (getActivity() instanceof SshKeyGen) + getActivity().finish(); }); - builder.setNegativeButton(getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { + builder.setNegativeButton(getResources().getString(R.string.dialog_cancel), (dialog, which) -> { - } }); builder.setNeutralButton(getResources().getString(R.string.ssh_keygen_copy), null); final AlertDialog ad = builder.setTitle("Your public key").create(); - ad.setOnShowListener(new DialogInterface.OnShowListener() { - @Override - public void onShow(DialogInterface dialog) { - Button b = ad.getButton(AlertDialog.BUTTON_NEUTRAL); - b.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - TextView textView = getDialog().findViewById(R.id.public_key); - ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE); - ClipData clip = ClipData.newPlainText("public key", textView.getText().toString()); - clipboard.setPrimaryClip(clip); - } - }); - } + ad.setOnShowListener(dialog -> { + Button b = ad.getButton(AlertDialog.BUTTON_NEUTRAL); + b.setOnClickListener(v1 -> { + TextView textView1 = getDialog().findViewById(R.id.public_key); + ClipboardManager clipboard = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE); + ClipData clip = ClipData.newPlainText("public key", textView1.getText().toString()); + clipboard.setPrimaryClip(clip); + }); }); return ad; } } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - if (getSupportActionBar() != null) - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - - setTitle("Generate SSH Key"); - - if (savedInstanceState == null) { - getFragmentManager().beginTransaction() - .replace(android.R.id.content, new SshKeyGenFragment()).commit(); - } - } - private static class KeyGenerateTask extends AsyncTask<String, Void, Exception> { private ProgressDialog pd; private WeakReference<SshKeyGen> weakReference; @@ -211,27 +204,11 @@ public class SshKeyGen extends AppCompatActivity { new AlertDialog.Builder(weakReference.get()) .setTitle("Error while trying to generate the ssh-key") .setMessage(weakReference.get().getResources().getString(R.string.ssh_key_error_dialog_text) + e.getMessage()) - .setPositiveButton(weakReference.get().getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - // pass - } + .setPositiveButton(weakReference.get().getResources().getString(R.string.dialog_ok), (dialogInterface, i) -> { + // pass }).show(); } } } - - // Invoked when 'Generate' button of SshKeyGenFragment clicked. Generates a - // private and public key, then replaces the SshKeyGenFragment with a - // ShowSshKeyFragment which displays the public key. - public void generate(View view) { - String length = Integer.toString((Integer) ((Spinner) findViewById(R.id.length)).getSelectedItem()); - String passphrase = ((EditText) findViewById(R.id.passphrase)).getText().toString(); - String comment = ((EditText) findViewById(R.id.comment)).getText().toString(); - new KeyGenerateTask(this).execute(length, passphrase, comment); - - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(view.getWindowToken(), 0); - } } |
