From 61ae170f6cef761407216dcf545ebb80410ca023 Mon Sep 17 00:00:00 2001 From: Matthew Wong Date: Thu, 16 Jul 2015 03:16:25 -0400 Subject: Make the generated public key viewer a dialog instead of fragment and add a "preference" that opens the dialog. --- .../main/java/com/zeapo/pwdstore/SshKeyGen.java | 69 +++++++++++++++------- .../java/com/zeapo/pwdstore/UserPreference.java | 22 +++++++ .../com/zeapo/pwdstore/pwgenDialogFragment.java | 4 +- app/src/main/res/layout/fragment_show_ssh_key.xml | 22 ------- app/src/main/res/values/strings.xml | 5 +- app/src/main/res/xml/preference.xml | 3 + 6 files changed, 78 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java b/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java index 17d952ad..dd9ef5ce 100644 --- a/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java +++ b/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java @@ -1,20 +1,25 @@ package com.zeapo.pwdstore; -import android.app.AlertDialog; +import android.app.Dialog; +import android.app.DialogFragment; import android.app.Fragment; 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.os.AsyncTask; import android.os.Bundle; +import android.preference.PreferenceManager; +import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; import android.widget.ArrayAdapter; +import android.widget.Button; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; @@ -50,14 +55,16 @@ public class SshKeyGen extends AppCompatActivity { } // Displays the generated public key .ssh_key.pub - public static class ShowSshKeyFragment extends Fragment { + public static class ShowSshKeyFragment extends DialogFragment { public ShowSshKeyFragment() { } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.fragment_show_ssh_key, container, false); + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + LayoutInflater inflater = getActivity().getLayoutInflater(); + final View v = inflater.inflate(R.layout.fragment_show_ssh_key, null); + builder.setView(v); TextView textView = (TextView) v.findViewById(R.id.public_key); File file = new File(getActivity().getFilesDir() + "/.ssh_key.pub"); @@ -68,14 +75,40 @@ public class SshKeyGen extends AppCompatActivity { e.printStackTrace(); } - v.findViewById(R.id.ok_ssh_key).setOnClickListener(new View.OnClickListener() { + builder.setPositiveButton(getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { @Override - public void onClick(View v) { - getActivity().finish(); + public void onClick(DialogInterface dialog, int which) { + if (getActivity() instanceof SshKeyGen) + getActivity().finish(); } }); - return v; + builder.setNegativeButton(getResources().getString(R.string.dialog_cancel), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int 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 = (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); + } + }); + } + }); + return ad; } } @@ -145,8 +178,12 @@ public class SshKeyGen extends AppCompatActivity { pd.dismiss(); if (e == null) { Toast.makeText(SshKeyGen.this, "SSH-key generated", Toast.LENGTH_LONG).show(); - getFragmentManager().beginTransaction() - .replace(android.R.id.content, new ShowSshKeyFragment()).commit(); + DialogFragment df = new ShowSshKeyFragment(); + df.show(getFragmentManager(), "public_key"); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + SharedPreferences.Editor editor = prefs.edit(); + editor.putBoolean("use_generated_key", true); + editor.apply(); } else { new AlertDialog.Builder(SshKeyGen.this) .setTitle("Error while trying to generate the ssh-key") @@ -168,14 +205,4 @@ public class SshKeyGen extends AppCompatActivity { public void generate(View view) { new generateTask().execute(view); } - - // Invoked when 'Copy' button of ShowSshKeyFragment clicked. Copies the - // displayed public key to the clipboard. - public void copy (View view) { - TextView textView = (TextView) findViewById(R.id.public_key); - ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); - ClipData clip = ClipData.newPlainText("public key", textView.getText().toString()); - clipboard.setPrimaryClip(clip); - } - } diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.java b/app/src/main/java/com/zeapo/pwdstore/UserPreference.java index dc535990..8344e8b3 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.java +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.java @@ -1,6 +1,7 @@ package com.zeapo.pwdstore; import android.app.AlertDialog; +import android.app.DialogFragment; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; @@ -90,6 +91,16 @@ public class UserPreference extends AppCompatActivity { } }); + findPreference("ssh_see_key").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + DialogFragment df = new SshKeyGen.ShowSshKeyFragment(); + df.show(getFragmentManager(), "public_key"); + return true; + } + }); + + findPreference("git_server_info").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { @@ -171,6 +182,13 @@ public class UserPreference extends AppCompatActivity { findPreference("pref_select_external").setOnPreferenceChangeListener(resetRepo); findPreference("git_external").setOnPreferenceChangeListener(resetRepo); } + + @Override + public void onStart() { + super.onStart(); + final SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences(); + findPreference("ssh_see_key").setEnabled(sharedPreferences.getBoolean("use_generated_key", false)); + } } @Override @@ -256,6 +274,10 @@ public class UserPreference extends AppCompatActivity { } copySshKey(data.getData()); Toast.makeText(this, this.getResources().getString(R.string.ssh_key_success_dialog_title), Toast.LENGTH_LONG).show(); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + SharedPreferences.Editor editor = prefs.edit(); + editor.putBoolean("use_generated_key", false); + editor.apply(); setResult(RESULT_OK); finish(); } catch (IOException e) { diff --git a/app/src/main/java/com/zeapo/pwdstore/pwgenDialogFragment.java b/app/src/main/java/com/zeapo/pwdstore/pwgenDialogFragment.java index 0e2b96ef..5dae4d23 100644 --- a/app/src/main/java/com/zeapo/pwdstore/pwgenDialogFragment.java +++ b/app/src/main/java/com/zeapo/pwdstore/pwgenDialogFragment.java @@ -58,8 +58,8 @@ public class pwgenDialogFragment extends DialogFragment { builder.setPositiveButton(getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - TextView edit = (TextView) pwgenDialogFragment.this.getActivity().findViewById(R.id.crypto_password_edit); - TextView generate = (TextView) pwgenDialogFragment.this.getDialog().findViewById(R.id.passwordText); + TextView edit = (TextView) getActivity().findViewById(R.id.crypto_password_edit); + TextView generate = (TextView) getDialog().findViewById(R.id.passwordText); edit.append(generate.getText()); } }); diff --git a/app/src/main/res/layout/fragment_show_ssh_key.xml b/app/src/main/res/layout/fragment_show_ssh_key.xml index 6590a98f..e81ae8c4 100644 --- a/app/src/main/res/layout/fragment_show_ssh_key.xml +++ b/app/src/main/res/layout/fragment_show_ssh_key.xml @@ -24,27 +24,5 @@ android:text="@string/ssh_keygen_tip" android:textAppearance="?android:attr/textAppearanceMedium"/> - - -