Resolve some crashes reported on Play Store (#631)

* ShowSshKeyFragment: Resolve illegal casts

* UserPreference: Try harder to not crash

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-02-21 12:42:27 +05:30 committed by GitHub
parent 9255583f2d
commit 9787489bc5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 6 deletions

View file

@ -481,7 +481,7 @@ class UserPreference : AppCompatActivity() {
// TODO: This is fragile. Workaround until PasswordItem is backed by DocumentFile
val docId = DocumentsContract.getTreeDocumentId(uri)
val split = docId.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val repoPath = "${Environment.getExternalStorageDirectory()}/${split[1]}"
val repoPath = "${Environment.getExternalStorageDirectory()}/${split[1] ?: split[0]}"
Timber.tag(TAG).d("Selected repository path is $repoPath")

View file

@ -22,18 +22,17 @@ import org.apache.commons.io.FileUtils
class ShowSshKeyFragment : DialogFragment() {
private lateinit var activity: SshKeyGenActivity
private lateinit var builder: MaterialAlertDialogBuilder
private lateinit var publicKey: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity = requireActivity() as SshKeyGenActivity
builder = MaterialAlertDialogBuilder(activity)
builder = MaterialAlertDialogBuilder(requireActivity())
}
@SuppressLint("InflateParams")
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val activity = requireActivity()
val view = activity.layoutInflater.inflate(R.layout.fragment_show_ssh_key, null)
publicKey = view.findViewById(R.id.public_key)
readKeyFromFile()
@ -53,13 +52,13 @@ class ShowSshKeyFragment : DialogFragment() {
private fun createMaterialDialog(view: View) {
builder.setView(view)
builder.setTitle(getString(R.string.your_public_key))
builder.setPositiveButton(getString(R.string.dialog_ok)) { _, _ -> activity.finish() }
builder.setPositiveButton(getString(R.string.dialog_ok)) { _, _ -> requireActivity().finish() }
builder.setNegativeButton(getString(R.string.dialog_cancel), null)
builder.setNeutralButton(resources.getString(R.string.ssh_keygen_copy), null)
}
private fun readKeyFromFile() {
val file = File(activity.filesDir.toString() + "/.ssh_key.pub")
val file = File(requireActivity().filesDir.toString() + "/.ssh_key.pub")
try {
publicKey.text = FileUtils.readFileToString(file, StandardCharsets.UTF_8)
} catch (e: Exception) {