summaryrefslogtreecommitdiffhomepage
path: root/ui/src/main/java/com/wireguard/android/activity
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2020-09-16 17:56:07 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2020-09-17 14:50:37 +0200
commita9ec8285062636fc7be68fee123bb5899e639fdd (patch)
treef11be67e983779e74eb0f5431b2bcda0e5c0d480 /ui/src/main/java/com/wireguard/android/activity
parenteebeece8565f985d1a4039d7430f650aa91d87e1 (diff)
DownloadsFileSaver: encapsulate permission checks
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/activity')
-rw-r--r--ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt7
-rw-r--r--ui/src/main/java/com/wireguard/android/activity/SettingsActivity.kt38
2 files changed, 3 insertions, 42 deletions
diff --git a/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt b/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt
index 456ad6ce..19f67834 100644
--- a/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt
+++ b/ui/src/main/java/com/wireguard/android/activity/LogViewerActivity.kt
@@ -60,7 +60,6 @@ import java.util.regex.Matcher
import java.util.regex.Pattern
class LogViewerActivity : AppCompatActivity() {
-
private lateinit var binding: LogViewerActivityBinding
private lateinit var logAdapter: LogEntryAdapter
private var logLines = arrayListOf<LogLine>()
@@ -161,15 +160,15 @@ class LogViewerActivity : AppCompatActivity() {
withContext(Dispatchers.IO) {
try {
outputFile = DownloadsFileSaver.save(this@LogViewerActivity, "wireguard-log.txt", "text/plain", true)
- outputFile?.outputStream.use {
- it?.write(rawLogLines.toString().toByteArray(Charsets.UTF_8))
- }
+ outputFile?.outputStream?.write(rawLogLines.toString().toByteArray(Charsets.UTF_8))
} catch (e: Throwable) {
outputFile?.delete()
exception = e
}
}
saveButton?.isEnabled = true
+ if (outputFile == null)
+ return
Snackbar.make(findViewById(android.R.id.content),
if (exception == null) getString(R.string.log_export_success, outputFile?.fileName)
else getString(R.string.log_export_error, ErrorMessages[exception]),
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 8d4c181c..feac43ca 100644
--- a/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.kt
+++ b/ui/src/main/java/com/wireguard/android/activity/SettingsActivity.kt
@@ -5,13 +5,9 @@
package com.wireguard.android.activity
import android.content.Intent
-import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
-import android.util.SparseArray
import android.view.MenuItem
-import androidx.core.app.ActivityCompat
-import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
@@ -23,35 +19,11 @@ import com.wireguard.android.util.ModuleLoader
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
-import java.util.ArrayList
-import java.util.Arrays
/**
* Interface for changing application-global persistent settings.
*/
class SettingsActivity : ThemeChangeAwareActivity() {
- private val permissionRequestCallbacks = SparseArray<(permissions: Array<String>, granted: IntArray) -> Unit>()
- private var permissionRequestCounter = 0
-
- 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) {
- needPermissions.add(it)
- }
- }
- if (needPermissions.isEmpty()) {
- val granted = IntArray(permissions.size)
- Arrays.fill(granted, PackageManager.PERMISSION_GRANTED)
- cb.invoke(permissions, granted)
- return
- }
- val idx = permissionRequestCounter++
- permissionRequestCallbacks.put(idx, cb)
- ActivityCompat.requestPermissions(this,
- needPermissions.toTypedArray(), idx)
- }
-
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (supportFragmentManager.findFragmentById(android.R.id.content) == null) {
@@ -69,16 +41,6 @@ class SettingsActivity : ThemeChangeAwareActivity() {
return super.onOptionsItemSelected(item)
}
- override fun onRequestPermissionsResult(requestCode: Int,
- permissions: Array<String>,
- grantResults: IntArray) {
- val f = permissionRequestCallbacks[requestCode]
- if (f != null) {
- permissionRequestCallbacks.remove(requestCode)
- f.invoke(permissions, grantResults)
- }
- }
-
class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, key: String?) {
addPreferencesFromResource(R.xml.preferences)