Replace PermissionRequestCallback with direct methods
These are much cleaner in Kotlin code. Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
edb76af820
commit
55849cad65
@ -25,10 +25,10 @@ import java.util.Arrays
|
|||||||
* Interface for changing application-global persistent settings.
|
* Interface for changing application-global persistent settings.
|
||||||
*/
|
*/
|
||||||
class SettingsActivity : ThemeChangeAwareActivity() {
|
class SettingsActivity : ThemeChangeAwareActivity() {
|
||||||
private val permissionRequestCallbacks = SparseArray<PermissionRequestCallback>()
|
private val permissionRequestCallbacks = SparseArray<(permissions: Array<String>, granted: IntArray) -> Unit>()
|
||||||
private var permissionRequestCounter = 0
|
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)
|
val needPermissions: MutableList<String> = ArrayList(permissions.size)
|
||||||
permissions.forEach {
|
permissions.forEach {
|
||||||
if (ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED) {
|
||||||
@ -38,7 +38,7 @@ class SettingsActivity : ThemeChangeAwareActivity() {
|
|||||||
if (needPermissions.isEmpty()) {
|
if (needPermissions.isEmpty()) {
|
||||||
val granted = IntArray(permissions.size)
|
val granted = IntArray(permissions.size)
|
||||||
Arrays.fill(granted, PackageManager.PERMISSION_GRANTED)
|
Arrays.fill(granted, PackageManager.PERMISSION_GRANTED)
|
||||||
cb.done(permissions, granted)
|
cb.invoke(permissions, granted)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val idx = permissionRequestCounter++
|
val idx = permissionRequestCounter++
|
||||||
@ -70,14 +70,10 @@ class SettingsActivity : ThemeChangeAwareActivity() {
|
|||||||
val f = permissionRequestCallbacks[requestCode]
|
val f = permissionRequestCallbacks[requestCode]
|
||||||
if (f != null) {
|
if (f != null) {
|
||||||
permissionRequestCallbacks.remove(requestCode)
|
permissionRequestCallbacks.remove(requestCode)
|
||||||
f.done(permissions, grantResults)
|
f.invoke(permissions, grantResults)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PermissionRequestCallback {
|
|
||||||
fun done(permissions: Array<String>, grantResults: IntArray)
|
|
||||||
}
|
|
||||||
|
|
||||||
class SettingsFragment : PreferenceFragmentCompat() {
|
class SettingsFragment : PreferenceFragmentCompat() {
|
||||||
override fun onCreatePreferences(savedInstanceState: Bundle?, key: String?) {
|
override fun onCreatePreferences(savedInstanceState: Bundle?, key: String?) {
|
||||||
addPreferencesFromResource(R.xml.preferences)
|
addPreferencesFromResource(R.xml.preferences)
|
||||||
|
@ -13,7 +13,6 @@ import androidx.preference.Preference
|
|||||||
import com.google.android.material.snackbar.Snackbar
|
import com.google.android.material.snackbar.Snackbar
|
||||||
import com.wireguard.android.Application
|
import com.wireguard.android.Application
|
||||||
import com.wireguard.android.R
|
import com.wireguard.android.R
|
||||||
import com.wireguard.android.activity.SettingsActivity
|
|
||||||
import com.wireguard.android.util.DownloadsFileSaver
|
import com.wireguard.android.util.DownloadsFileSaver
|
||||||
import com.wireguard.android.util.ErrorMessages
|
import com.wireguard.android.util.ErrorMessages
|
||||||
import com.wireguard.android.util.FragmentUtils
|
import com.wireguard.android.util.FragmentUtils
|
||||||
@ -82,15 +81,12 @@ class LogExporterPreference(context: Context, attrs: AttributeSet?) : Preference
|
|||||||
|
|
||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
FragmentUtils.getPrefActivity(this)
|
FragmentUtils.getPrefActivity(this)
|
||||||
.ensurePermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
|
.ensurePermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { _, grantResults ->
|
||||||
object: SettingsActivity.PermissionRequestCallback {
|
|
||||||
override fun done(permissions: Array<String>, grantResults: IntArray) {
|
|
||||||
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
isEnabled = false
|
isEnabled = false
|
||||||
exportLog()
|
exportLog()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -13,16 +13,12 @@ import androidx.preference.Preference
|
|||||||
import com.google.android.material.snackbar.Snackbar
|
import com.google.android.material.snackbar.Snackbar
|
||||||
import com.wireguard.android.Application
|
import com.wireguard.android.Application
|
||||||
import com.wireguard.android.R
|
import com.wireguard.android.R
|
||||||
import com.wireguard.android.activity.SettingsActivity
|
|
||||||
import com.wireguard.android.model.ObservableTunnel
|
import com.wireguard.android.model.ObservableTunnel
|
||||||
import com.wireguard.android.util.DownloadsFileSaver
|
import com.wireguard.android.util.DownloadsFileSaver
|
||||||
import com.wireguard.android.util.ErrorMessages
|
import com.wireguard.android.util.ErrorMessages
|
||||||
import com.wireguard.android.util.FragmentUtils
|
import com.wireguard.android.util.FragmentUtils
|
||||||
import com.wireguard.android.util.ObservableSortedKeyedList
|
|
||||||
import com.wireguard.config.Config
|
|
||||||
import java9.util.concurrent.CompletableFuture
|
import java9.util.concurrent.CompletableFuture
|
||||||
import java.nio.charset.StandardCharsets
|
import java.nio.charset.StandardCharsets
|
||||||
import java.util.ArrayList
|
|
||||||
import java.util.zip.ZipEntry
|
import java.util.zip.ZipEntry
|
||||||
import java.util.zip.ZipOutputStream
|
import java.util.zip.ZipOutputStream
|
||||||
|
|
||||||
@ -86,15 +82,12 @@ class ZipExporterPreference(context: Context, attrs: AttributeSet?) : Preference
|
|||||||
|
|
||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
FragmentUtils.getPrefActivity(this)
|
FragmentUtils.getPrefActivity(this)
|
||||||
.ensurePermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
|
.ensurePermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { _, grantResults ->
|
||||||
object : SettingsActivity.PermissionRequestCallback {
|
|
||||||
override fun done(permissions: Array<String>, grantResults: IntArray) {
|
|
||||||
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
isEnabled = false
|
isEnabled = false
|
||||||
exportZip()
|
exportZip()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
Loading…
Reference in New Issue
Block a user