diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2020-03-20 20:45:47 +0530 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-03-20 22:22:56 -0600 |
commit | 55849cad65b699ff16886085db996fc6ce3dec3e (patch) | |
tree | fb5e9390feaf36bb59a1cae84045b8c9f86e40c5 /ui/src/main | |
parent | edb76af8206d4c9f011087d9b5811fdc74fb764c (diff) |
Replace PermissionRequestCallback with direct methods
These are much cleaner in Kotlin code.
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'ui/src/main')
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<PermissionRequestCallback>() + private val permissionRequestCallbacks = SparseArray<(permissions: Array<String>, granted: IntArray) -> Unit>() private var permissionRequestCounter = 0 - fun ensurePermissions(permissions: Array<String>, cb: PermissionRequestCallback) { + fun ensurePermissions(permissions: Array<String>, cb: (permissions: Array<String>, granted: IntArray) -> Unit) { val needPermissions: MutableList<String> = 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<String>, 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<String>, 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<String>, 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 { |