diff options
| author | Harsh Shandilya <msfjarvis@gmail.com> | 2019-05-29 00:42:09 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-05-29 00:42:09 +0530 |
| commit | 53b42905f17553b74160eaae7d5352fc7aa648d7 (patch) | |
| tree | 6df5e28bdcf4b10eabda3c15f89410114114862d /app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.kt | |
| parent | 321035c319979010cafb10ce8bd83890d0c0a195 (diff) | |
| download | HardenedPass-53b42905f17553b74160eaae7d5352fc7aa648d7.tar.gz HardenedPass-53b42905f17553b74160eaae7d5352fc7aa648d7.zip | |
Convert autofill package to Kotlin (#515)
Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.kt')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.kt | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.kt b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.kt new file mode 100644 index 00000000..aa1c329b --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/autofill/AutofillFragment.kt @@ -0,0 +1,211 @@ +package com.zeapo.pwdstore.autofill + +import android.annotation.SuppressLint +import android.app.Dialog +import android.content.Context +import android.content.Intent +import android.content.SharedPreferences +import android.content.pm.PackageManager +import android.os.Bundle +import android.view.View +import android.view.ViewGroup +import android.widget.ArrayAdapter +import android.widget.EditText +import android.widget.ListView +import android.widget.RadioButton +import android.widget.RadioGroup +import android.widget.TextView +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat +import androidx.fragment.app.DialogFragment +import com.zeapo.pwdstore.PasswordStore +import com.zeapo.pwdstore.R +import com.zeapo.pwdstore.utils.splitLines + +class AutofillFragment : DialogFragment() { + private var adapter: ArrayAdapter<String>? = null + private var isWeb: Boolean = false + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val builder = AlertDialog.Builder(requireContext()) + // this fragment is only created from the settings page (AutofillPreferenceActivity) + // need to interact with the recyclerAdapter which is a member of activity + val callingActivity = requireActivity() as AutofillPreferenceActivity + val inflater = callingActivity.layoutInflater + val args = requireNotNull(arguments) + + @SuppressLint("InflateParams") val view = inflater.inflate(R.layout.fragment_autofill, null) + + builder.setView(view) + + val packageName = args.getString("packageName") + val appName = args.getString("appName") + isWeb = args.getBoolean("isWeb") + + // set the dialog icon and title or webURL editText + val iconPackageName: String? + if (!isWeb) { + iconPackageName = packageName + builder.setTitle(appName) + view.findViewById<View>(R.id.webURL).visibility = View.GONE + } else { + iconPackageName = "com.android.browser" + builder.setTitle("Website") + (view.findViewById<View>(R.id.webURL) as EditText).setText(packageName) + } + try { + builder.setIcon(callingActivity.packageManager.getApplicationIcon(iconPackageName)) + } catch (e: PackageManager.NameNotFoundException) { + e.printStackTrace() + } + + // set up the listview now for items added by button/from preferences + adapter = object : ArrayAdapter<String>(requireContext(), android.R.layout.simple_list_item_1, android.R.id.text1) { + // set text color to black because default is white... + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + val textView = super.getView(position, convertView, parent) as TextView + textView.setTextColor(ContextCompat.getColor(context, R.color.grey_black_1000)) + return textView + } + } + (view.findViewById<View>(R.id.matched) as ListView).adapter = adapter + // delete items by clicking them + (view.findViewById<View>(R.id.matched) as ListView).setOnItemClickListener { _, _, position, _ -> adapter!!.remove(adapter!!.getItem(position)) } + + // set the existing preference, if any + val prefs: SharedPreferences = if (!isWeb) { + callingActivity.applicationContext.getSharedPreferences("autofill", Context.MODE_PRIVATE) + } else { + callingActivity.applicationContext.getSharedPreferences("autofill_web", Context.MODE_PRIVATE) + } + when (val preference = prefs.getString(packageName, "")) { + "" -> (view.findViewById<View>(R.id.use_default) as RadioButton).toggle() + "/first" -> (view.findViewById<View>(R.id.first) as RadioButton).toggle() + "/never" -> (view.findViewById<View>(R.id.never) as RadioButton).toggle() + else -> { + (view.findViewById<View>(R.id.match) as RadioButton).toggle() + // trim to remove the last blank element + adapter!!.addAll(*preference!!.trim { it <= ' ' }.splitLines()) + } + } + + // add items with the + button + val matchPassword = { _: View -> + (view.findViewById<View>(R.id.match) as RadioButton).toggle() + val intent = Intent(activity, PasswordStore::class.java) + intent.putExtra("matchWith", true) + startActivityForResult(intent, MATCH_WITH) + } + view.findViewById<View>(R.id.matchButton).setOnClickListener(matchPassword) + + // write to preferences when OK clicked + builder.setPositiveButton(R.string.dialog_ok) { _, _ -> } + builder.setNegativeButton(R.string.dialog_cancel, null) + val editor = prefs.edit() + if (isWeb) { + builder.setNeutralButton(R.string.autofill_apps_delete) { _, _ -> + if (callingActivity.recyclerAdapter != null + && packageName != null && packageName != "") { + editor.remove(packageName) + callingActivity.recyclerAdapter?.removeWebsite(packageName) + editor.apply() + } + } + } + return builder.create() + } + + // need to the onClick here for buttons to dismiss dialog only when wanted + override fun onStart() { + super.onStart() + val ad = dialog as? AlertDialog + if (ad != null) { + val positiveButton = ad.getButton(Dialog.BUTTON_POSITIVE) + positiveButton.setOnClickListener { + val callingActivity = requireActivity() as AutofillPreferenceActivity + val dialog = dialog + val args = requireNotNull(arguments) + + val prefs: SharedPreferences = if (!isWeb) { + callingActivity.applicationContext.getSharedPreferences("autofill", Context.MODE_PRIVATE) + } else { + callingActivity.applicationContext.getSharedPreferences("autofill_web", Context.MODE_PRIVATE) + } + val editor = prefs.edit() + + var packageName = args.getString("packageName", "") + if (isWeb) { + // handle some errors and don't dismiss the dialog + val webURL = dialog.findViewById<EditText>(R.id.webURL) + + packageName = webURL.text.toString() + + if (packageName == "") { + webURL.error = "URL cannot be blank" + return@setOnClickListener + } + val oldPackageName = args.getString("packageName", "") + if (oldPackageName != packageName && prefs.all.containsKey(packageName)) { + webURL.error = "URL already exists" + return@setOnClickListener + } + } + + // write to preferences accordingly + val radioGroup = dialog.findViewById<RadioGroup>(R.id.autofill_radiogroup) + when (radioGroup.checkedRadioButtonId) { + R.id.use_default -> if (!isWeb) { + editor.remove(packageName) + } else { + editor.putString(packageName, "") + } + R.id.first -> editor.putString(packageName, "/first") + R.id.never -> editor.putString(packageName, "/never") + else -> { + val paths = StringBuilder() + for (i in 0 until adapter!!.count) { + paths.append(adapter!!.getItem(i)) + if (i != adapter!!.count) { + paths.append("\n") + } + } + editor.putString(packageName, paths.toString()) + } + } + editor.apply() + + // notify the recycler adapter if it is loaded + callingActivity.recyclerAdapter?.apply { + val position: Int + if (!isWeb) { + val appName = args.getString("appName", "") + position = getPosition(appName) + notifyItemChanged(position) + } else { + position = getPosition(packageName) + when (val oldPackageName = args.getString("packageName", "")) { + packageName -> notifyItemChanged(position) + "" -> addWebsite(packageName) + else -> { + editor.remove(oldPackageName) + updateWebsite(oldPackageName, packageName) + } + } + } + } + dismiss() + } + } + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { + if (resultCode == AppCompatActivity.RESULT_OK) { + adapter!!.add(data.getStringExtra("path")) + } + } + + companion object { + private const val MATCH_WITH = 777 + } +} |
