BasePgpActivity: use runCatching to replace exception handling

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-09-05 20:39:42 +05:30
parent e18416fa0e
commit eded073ef2
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80

View file

@ -6,12 +6,10 @@
package com.zeapo.pwdstore.crypto package com.zeapo.pwdstore.crypto
import android.app.PendingIntent import android.app.PendingIntent
import android.content.ActivityNotFoundException
import android.content.ClipData import android.content.ClipData
import android.content.Intent import android.content.Intent
import android.content.IntentSender import android.content.IntentSender
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
@ -23,6 +21,8 @@ import androidx.appcompat.app.AppCompatActivity
import com.github.ajalt.timberkt.Timber.tag import com.github.ajalt.timberkt.Timber.tag
import com.github.ajalt.timberkt.e import com.github.ajalt.timberkt.e
import com.github.ajalt.timberkt.i import com.github.ajalt.timberkt.i
import com.github.michaelbull.result.getOr
import com.github.michaelbull.result.runCatching
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import com.zeapo.pwdstore.ClipboardService import com.zeapo.pwdstore.ClipboardService
@ -142,34 +142,30 @@ open class BasePgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBou
* Method for subclasses to initiate binding with [OpenPgpServiceConnection]. * Method for subclasses to initiate binding with [OpenPgpServiceConnection].
*/ */
fun bindToOpenKeychain(onBoundListener: OpenPgpServiceConnection.OnBound) { fun bindToOpenKeychain(onBoundListener: OpenPgpServiceConnection.OnBound) {
val installed = try { val installed = runCatching {
packageManager.getPackageInfo(OPENPGP_PROVIDER, 0) packageManager.getPackageInfo(OPENPGP_PROVIDER, 0)
true true
} catch (_: PackageManager.NameNotFoundException) { }.getOr(false)
false
}
if (!installed) { if (!installed) {
previousListener = onBoundListener previousListener = onBoundListener
MaterialAlertDialogBuilder(this) MaterialAlertDialogBuilder(this)
.setTitle(getString(R.string.openkeychain_not_installed_title)) .setTitle(getString(R.string.openkeychain_not_installed_title))
.setMessage(getString(R.string.openkeychain_not_installed_message)) .setMessage(getString(R.string.openkeychain_not_installed_message))
.setPositiveButton(getString(R.string.openkeychain_not_installed_google_play)) { _, _ -> .setPositiveButton(getString(R.string.openkeychain_not_installed_google_play)) { _, _ ->
try { runCatching {
val intent = Intent(Intent.ACTION_VIEW).apply { val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(getString(R.string.play_deeplink_template, OPENPGP_PROVIDER)) data = Uri.parse(getString(R.string.play_deeplink_template, OPENPGP_PROVIDER))
setPackage("com.android.vending") setPackage("com.android.vending")
} }
startActivity(intent) startActivity(intent)
} catch (_: ActivityNotFoundException) {
} }
} }
.setNeutralButton(getString(R.string.openkeychain_not_installed_fdroid)) { _, _ -> .setNeutralButton(getString(R.string.openkeychain_not_installed_fdroid)) { _, _ ->
try { runCatching {
val intent = Intent(Intent.ACTION_VIEW).apply { val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(getString(R.string.fdroid_deeplink_template, OPENPGP_PROVIDER)) data = Uri.parse(getString(R.string.fdroid_deeplink_template, OPENPGP_PROVIDER))
} }
startActivity(intent) startActivity(intent)
} catch (_: ActivityNotFoundException) {
} }
} }
.setOnCancelListener { finish() } .setOnCancelListener { finish() }
@ -253,11 +249,7 @@ open class BasePgpActivity : AppCompatActivity(), OpenPgpServiceConnection.OnBou
fun copyPasswordToClipboard(password: String?) { fun copyPasswordToClipboard(password: String?) {
copyTextToClipboard(password, showSnackbar = false) copyTextToClipboard(password, showSnackbar = false)
var clearAfter = 45 val clearAfter = settings.getString(PreferenceKeys.GENERAL_SHOW_TIME)?.toIntOrNull() ?: 45
try {
clearAfter = (settings.getString(PreferenceKeys.GENERAL_SHOW_TIME) ?: "45").toInt()
} catch (_: NumberFormatException) {
}
if (clearAfter != 0) { if (clearAfter != 0) {
val service = Intent(this, ClipboardService::class.java).apply { val service = Intent(this, ClipboardService::class.java).apply {