From 073346c157ec34b750d175c8543f346e6eae94de Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Tue, 1 Oct 2019 22:14:28 +0530 Subject: Redesign UI and introduce dark theme (#519) Caveats: - The openpgp preference had to be removed because the open-intents developers are too lazy to update their libraries. Over the coming weeks I will be reimplementing a local solution for this instead. - The autofill dialog is broken but I since it is being worked on in #410 already I'm not going to bother fixing it. --- .../main/java/com/zeapo/pwdstore/SshKeyGen.java | 50 ++++++++++++---------- 1 file changed, 28 insertions(+), 22 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java') diff --git a/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java b/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java index 593a9bbb..77fbcf27 100644 --- a/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java +++ b/app/src/main/java/com/zeapo/pwdstore/SshKeyGen.java @@ -2,8 +2,6 @@ package com.zeapo.pwdstore; import android.annotation.SuppressLint; import android.app.Dialog; -import android.app.DialogFragment; -import android.app.Fragment; import android.app.ProgressDialog; import android.content.ClipData; import android.content.ClipboardManager; @@ -22,13 +20,19 @@ import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Spinner; -import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.AppCompatTextView; +import androidx.fragment.app.DialogFragment; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; import androidx.preference.PreferenceManager; +import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.textfield.TextInputEditText; import com.jcraft.jsch.JSch; import com.jcraft.jsch.KeyPair; @@ -37,6 +41,7 @@ import org.apache.commons.io.FileUtils; import java.io.File; import java.io.FileOutputStream; import java.lang.ref.WeakReference; +import java.nio.charset.StandardCharsets; public class SshKeyGen extends AppCompatActivity { @@ -50,7 +55,7 @@ public class SshKeyGen extends AppCompatActivity { setTitle("Generate SSH Key"); if (savedInstanceState == null) { - getFragmentManager().beginTransaction() + getSupportFragmentManager().beginTransaction() .replace(android.R.id.content, new SshKeyGenFragment()).commit(); } } @@ -77,20 +82,20 @@ public class SshKeyGen extends AppCompatActivity { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View v = inflater.inflate(R.layout.fragment_ssh_keygen, container, false); - Typeface monoTypeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/sourcecodepro.ttf"); + Typeface monoTypeface = Typeface.createFromAsset(requireContext().getAssets(), "fonts/sourcecodepro.ttf"); Spinner spinner = v.findViewById(R.id.length); Integer[] lengths = new Integer[]{2048, 4096}; - ArrayAdapter adapter = new ArrayAdapter<>(getActivity(), + ArrayAdapter adapter = new ArrayAdapter<>(requireContext(), android.R.layout.simple_spinner_dropdown_item, lengths); spinner.setAdapter(adapter); - ((EditText) v.findViewById(R.id.passphrase)).setTypeface(monoTypeface); + ((TextInputEditText) v.findViewById(R.id.passphrase)).setTypeface(monoTypeface); - CheckBox checkbox = v.findViewById(R.id.show_passphrase); + final CheckBox checkbox = v.findViewById(R.id.show_passphrase); checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> { - EditText editText = v.findViewById(R.id.passphrase); - int selection = editText.getSelectionEnd(); + final TextInputEditText editText = v.findViewById(R.id.passphrase); + final int selection = editText.getSelectionEnd(); if (isChecked) { editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); } else { @@ -108,25 +113,27 @@ public class SshKeyGen extends AppCompatActivity { public ShowSshKeyFragment() { } + @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - LayoutInflater inflater = getActivity().getLayoutInflater(); + final FragmentActivity activity = requireActivity(); + final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireContext()); + LayoutInflater inflater = activity.getLayoutInflater(); @SuppressLint("InflateParams") final View v = inflater.inflate(R.layout.fragment_show_ssh_key, null); builder.setView(v); - TextView textView = v.findViewById(R.id.public_key); - File file = new File(getActivity().getFilesDir() + "/.ssh_key.pub"); + AppCompatTextView textView = v.findViewById(R.id.public_key); + File file = new File(activity.getFilesDir() + "/.ssh_key.pub"); try { - textView.setText(FileUtils.readFileToString(file)); + textView.setText(FileUtils.readFileToString(file, StandardCharsets.UTF_8)); } catch (Exception e) { System.out.println("Exception caught :("); e.printStackTrace(); } builder.setPositiveButton(getResources().getString(R.string.dialog_ok), (dialog, which) -> { - if (getActivity() instanceof SshKeyGen) - getActivity().finish(); + if (activity instanceof SshKeyGen) + activity.finish(); }); builder.setNegativeButton(getResources().getString(R.string.dialog_cancel), (dialog, which) -> { @@ -139,8 +146,8 @@ public class SshKeyGen extends AppCompatActivity { 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); + AppCompatTextView textView1 = getDialog().findViewById(R.id.public_key); + ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("public key", textView1.getText().toString()); clipboard.setPrimaryClip(clip); }); @@ -198,20 +205,19 @@ public class SshKeyGen extends AppCompatActivity { if (e == null) { Toast.makeText(weakReference.get(), "SSH-key generated", Toast.LENGTH_LONG).show(); DialogFragment df = new ShowSshKeyFragment(); - df.show(weakReference.get().getFragmentManager(), "public_key"); + df.show(weakReference.get().getSupportFragmentManager(), "public_key"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(weakReference.get()); SharedPreferences.Editor editor = prefs.edit(); editor.putBoolean("use_generated_key", true); editor.apply(); } else { - new AlertDialog.Builder(weakReference.get()) + new MaterialAlertDialogBuilder(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), (dialogInterface, i) -> { // pass }).show(); } - } } } -- cgit v1.2.3