From 55849cad65b699ff16886085db996fc6ce3dec3e Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Fri, 20 Mar 2020 20:45:47 +0530 Subject: [PATCH] Replace PermissionRequestCallback with direct methods These are much cleaner in Kotlin code. Signed-off-by: Harsh Shandilya --- .../android/activity/SettingsActivity.kt | 12 ++++-------- .../preference/LogExporterPreference.kt | 16 ++++++---------- .../preference/ZipExporterPreference.kt | 19 ++++++------------- 3 files changed, 16 insertions(+), 31 deletions(-) diff --git a/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.kt b/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.kt index c7453617..a6bdb187 100644 --- a/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.kt +++ b/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.kt @@ -25,10 +25,10 @@ import java.util.Arrays * Interface for changing application-global persistent settings. */ class SettingsActivity : ThemeChangeAwareActivity() { - private val permissionRequestCallbacks = SparseArray() + private val permissionRequestCallbacks = SparseArray<(permissions: Array, granted: IntArray) -> Unit>() private var permissionRequestCounter = 0 - fun ensurePermissions(permissions: Array, cb: PermissionRequestCallback) { + fun ensurePermissions(permissions: Array, cb: (permissions: Array, granted: IntArray) -> Unit) { val needPermissions: MutableList = ArrayList(permissions.size) permissions.forEach { if (ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED) { @@ -38,7 +38,7 @@ class SettingsActivity : ThemeChangeAwareActivity() { if (needPermissions.isEmpty()) { val granted = IntArray(permissions.size) Arrays.fill(granted, PackageManager.PERMISSION_GRANTED) - cb.done(permissions, granted) + cb.invoke(permissions, granted) return } val idx = permissionRequestCounter++ @@ -70,14 +70,10 @@ class SettingsActivity : ThemeChangeAwareActivity() { val f = permissionRequestCallbacks[requestCode] if (f != null) { permissionRequestCallbacks.remove(requestCode) - f.done(permissions, grantResults) + f.invoke(permissions, grantResults) } } - interface PermissionRequestCallback { - fun done(permissions: Array, grantResults: IntArray) - } - class SettingsFragment : PreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, key: String?) { addPreferencesFromResource(R.xml.preferences) diff --git a/ui/src/main/java/com/wireguard/android/preference/LogExporterPreference.kt b/ui/src/main/java/com/wireguard/android/preference/LogExporterPreference.kt index e49de5e2..1b907530 100644 --- a/ui/src/main/java/com/wireguard/android/preference/LogExporterPreference.kt +++ b/ui/src/main/java/com/wireguard/android/preference/LogExporterPreference.kt @@ -13,7 +13,6 @@ import androidx.preference.Preference import com.google.android.material.snackbar.Snackbar import com.wireguard.android.Application import com.wireguard.android.R -import com.wireguard.android.activity.SettingsActivity import com.wireguard.android.util.DownloadsFileSaver import com.wireguard.android.util.ErrorMessages import com.wireguard.android.util.FragmentUtils @@ -82,15 +81,12 @@ class LogExporterPreference(context: Context, attrs: AttributeSet?) : Preference override fun onClick() { FragmentUtils.getPrefActivity(this) - .ensurePermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), - object: SettingsActivity.PermissionRequestCallback { - override fun done(permissions: Array, grantResults: IntArray) { - if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - isEnabled = false - exportLog() - } - } - }) + .ensurePermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { _, grantResults -> + if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + isEnabled = false + exportLog() + } + } } companion object { diff --git a/ui/src/main/java/com/wireguard/android/preference/ZipExporterPreference.kt b/ui/src/main/java/com/wireguard/android/preference/ZipExporterPreference.kt index db1b7735..0c00a3ef 100644 --- a/ui/src/main/java/com/wireguard/android/preference/ZipExporterPreference.kt +++ b/ui/src/main/java/com/wireguard/android/preference/ZipExporterPreference.kt @@ -13,16 +13,12 @@ import androidx.preference.Preference import com.google.android.material.snackbar.Snackbar import com.wireguard.android.Application import com.wireguard.android.R -import com.wireguard.android.activity.SettingsActivity import com.wireguard.android.model.ObservableTunnel import com.wireguard.android.util.DownloadsFileSaver import com.wireguard.android.util.ErrorMessages import com.wireguard.android.util.FragmentUtils -import com.wireguard.android.util.ObservableSortedKeyedList -import com.wireguard.config.Config import java9.util.concurrent.CompletableFuture import java.nio.charset.StandardCharsets -import java.util.ArrayList import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream @@ -86,15 +82,12 @@ class ZipExporterPreference(context: Context, attrs: AttributeSet?) : Preference override fun onClick() { FragmentUtils.getPrefActivity(this) - .ensurePermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), - object : SettingsActivity.PermissionRequestCallback { - override fun done(permissions: Array, grantResults: IntArray) { - if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - isEnabled = false - exportZip() - } - } - }) + .ensurePermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { _, grantResults -> + if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + isEnabled = false + exportZip() + } + } } companion object {