Make the generated public key viewer a dialog instead of fragment and add a "preference" that opens the dialog.

This commit is contained in:
Matthew Wong 2015-07-16 03:16:25 -04:00
parent d03ab8ee3c
commit 61ae170f6c
6 changed files with 78 additions and 47 deletions

View file

@ -1,20 +1,25 @@
package com.zeapo.pwdstore; package com.zeapo.pwdstore;
import android.app.AlertDialog; import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment; import android.app.Fragment;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.ClipData; import android.content.ClipData;
import android.content.ClipboardManager; import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -50,14 +55,16 @@ public class SshKeyGen extends AppCompatActivity {
} }
// Displays the generated public key .ssh_key.pub // Displays the generated public key .ssh_key.pub
public static class ShowSshKeyFragment extends Fragment { public static class ShowSshKeyFragment extends DialogFragment {
public ShowSshKeyFragment() { public ShowSshKeyFragment() {
} }
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View v = inflater.inflate(R.layout.fragment_show_ssh_key, container, false); 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); TextView textView = (TextView) v.findViewById(R.id.public_key);
File file = new File(getActivity().getFilesDir() + "/.ssh_key.pub"); File file = new File(getActivity().getFilesDir() + "/.ssh_key.pub");
@ -68,14 +75,40 @@ public class SshKeyGen extends AppCompatActivity {
e.printStackTrace(); 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 @Override
public void onClick(View v) { public void onClick(DialogInterface dialog, int which) {
getActivity().finish(); 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(); pd.dismiss();
if (e == null) { if (e == null) {
Toast.makeText(SshKeyGen.this, "SSH-key generated", Toast.LENGTH_LONG).show(); Toast.makeText(SshKeyGen.this, "SSH-key generated", Toast.LENGTH_LONG).show();
getFragmentManager().beginTransaction() DialogFragment df = new ShowSshKeyFragment();
.replace(android.R.id.content, new ShowSshKeyFragment()).commit(); df.show(getFragmentManager(), "public_key");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("use_generated_key", true);
editor.apply();
} else { } else {
new AlertDialog.Builder(SshKeyGen.this) new AlertDialog.Builder(SshKeyGen.this)
.setTitle("Error while trying to generate the ssh-key") .setTitle("Error while trying to generate the ssh-key")
@ -168,14 +205,4 @@ public class SshKeyGen extends AppCompatActivity {
public void generate(View view) { public void generate(View view) {
new generateTask().execute(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);
}
} }

View file

@ -1,6 +1,7 @@
package com.zeapo.pwdstore; package com.zeapo.pwdstore;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.DialogFragment;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; 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() { findPreference("git_server_info").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override @Override
public boolean onPreferenceClick(Preference preference) { public boolean onPreferenceClick(Preference preference) {
@ -171,6 +182,13 @@ public class UserPreference extends AppCompatActivity {
findPreference("pref_select_external").setOnPreferenceChangeListener(resetRepo); findPreference("pref_select_external").setOnPreferenceChangeListener(resetRepo);
findPreference("git_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 @Override
@ -256,6 +274,10 @@ public class UserPreference extends AppCompatActivity {
} }
copySshKey(data.getData()); copySshKey(data.getData());
Toast.makeText(this, this.getResources().getString(R.string.ssh_key_success_dialog_title), Toast.LENGTH_LONG).show(); 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); setResult(RESULT_OK);
finish(); finish();
} catch (IOException e) { } catch (IOException e) {

View file

@ -58,8 +58,8 @@ public class pwgenDialogFragment extends DialogFragment {
builder.setPositiveButton(getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() { builder.setPositiveButton(getResources().getString(R.string.dialog_ok), new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
TextView edit = (TextView) pwgenDialogFragment.this.getActivity().findViewById(R.id.crypto_password_edit); TextView edit = (TextView) getActivity().findViewById(R.id.crypto_password_edit);
TextView generate = (TextView) pwgenDialogFragment.this.getDialog().findViewById(R.id.passwordText); TextView generate = (TextView) getDialog().findViewById(R.id.passwordText);
edit.append(generate.getText()); edit.append(generate.getText());
} }
}); });

View file

@ -24,27 +24,5 @@
android:text="@string/ssh_keygen_tip" android:text="@string/ssh_keygen_tip"
android:textAppearance="?android:attr/textAppearanceMedium"/> android:textAppearance="?android:attr/textAppearanceMedium"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<Button
android:id="@+id/copy_public_key"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ssh_keygen_copy"
android:onClick="copy" />
<Button
android:id="@+id/ok_ssh_key"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dialog_ok"/>
</LinearLayout>
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>

View file

@ -96,8 +96,9 @@
<string name="pref_git_username_title">Username</string> <string name="pref_git_username_title">Username</string>
<string name="pref_git_username_hint">username</string> <string name="pref_git_username_hint">username</string>
<string name="pref_edit_server_info">Edit git server settings</string> <string name="pref_edit_server_info">Edit git server settings</string>
<string name="pref_ssh_title">Import ssh-key</string> <string name="pref_ssh_title">Import SSH key</string>
<string name="pref_ssh_keygen_title">Generate ssh-key</string> <string name="pref_ssh_keygen_title">Generate SSH key pair</string>
<string name="pref_ssh_see_key_title">View generated public SSH key</string>
<string name="pref_git_delete_repo">Delete repository</string> <string name="pref_git_delete_repo">Delete repository</string>
<string name="pref_dialog_delete_title">Clear repository</string> <string name="pref_dialog_delete_title">Clear repository</string>
<string name="pref_dialog_delete_msg">Do you want to delete the current password store directory? This will not clear your configuration.</string> <string name="pref_dialog_delete_msg">Do you want to delete the current password store directory? This will not clear your configuration.</string>

View file

@ -10,6 +10,9 @@
<Preference <Preference
android:key="ssh_keygen" android:key="ssh_keygen"
android:title="@string/pref_ssh_keygen_title" /> android:title="@string/pref_ssh_keygen_title" />
<Preference
android:key="ssh_see_key"
android:title="@string/pref_ssh_see_key_title" />
<Preference <Preference
android:key="git_delete_repo" android:key="git_delete_repo"
android:summary="Deletes local repository" android:summary="Deletes local repository"