Aperture: Dedup some permissions stuff
Change-Id: I39af1db586b6576c68afcd792433c0393c750291
diff --git a/app/src/main/java/org/lineageos/aperture/CameraActivity.kt b/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
index 1dfef5b..e84322a 100644
--- a/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/CameraActivity.kt
@@ -5,12 +5,10 @@
package org.lineageos.aperture
-import android.Manifest
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.app.KeyguardManager
import android.content.Intent
-import android.content.pm.PackageManager
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.AnimatedVectorDrawable
@@ -88,6 +86,7 @@
import org.lineageos.aperture.utils.Framerate
import org.lineageos.aperture.utils.GridMode
import org.lineageos.aperture.utils.MediaType
+import org.lineageos.aperture.utils.PermissionsUtils
import org.lineageos.aperture.utils.ShortcutsUtils
import org.lineageos.aperture.utils.StabilizationMode
import org.lineageos.aperture.utils.StorageUtils
@@ -151,6 +150,7 @@
private val sharedPreferences by lazy {
PreferenceManager.getDefaultSharedPreferences(this)
}
+ private val permissionsUtils by lazy { PermissionsUtils(this) }
// Current camera state
private lateinit var camera: Camera
@@ -270,7 +270,9 @@
// Reset cached location
location = null
- if (allLocationPermissionsGranted() && sharedPreferences.saveLocation == true) {
+ if (permissionsUtils.locationPermissionsGranted()
+ && sharedPreferences.saveLocation == true
+ ) {
// Request location updates
locationManager.allProviders.forEach {
locationManager.requestLocationUpdates(it, 1000, 1f, this)
@@ -291,13 +293,13 @@
ActivityResultContracts.RequestMultiplePermissions()
) {
if (it.isNotEmpty()) {
- if (!allPermissionsGranted()) {
+ if (!permissionsUtils.mainPermissionsGranted()) {
Toast.makeText(
this, getString(R.string.app_permissions_toast), Toast.LENGTH_SHORT
).show()
finish()
}
- sharedPreferences.saveLocation = allLocationPermissionsGranted()
+ sharedPreferences.saveLocation = permissionsUtils.locationPermissionsGranted()
}
}
@@ -617,10 +619,8 @@
super.onResume()
// Request camera permissions
- if (!allPermissionsGranted() || sharedPreferences.saveLocation == null) {
- requestMultiplePermissions.launch(
- REQUIRED_PERMISSIONS + REQUIRED_PERMISSIONS_LOCATION
- )
+ if (!permissionsUtils.mainPermissionsGranted() || sharedPreferences.saveLocation == null) {
+ requestMultiplePermissions.launch(PermissionsUtils.allPermissions)
}
// Set bright screen
@@ -1490,14 +1490,6 @@
levelerView.isVisible = enabled
}
- private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
- ContextCompat.checkSelfPermission(baseContext, it) == PackageManager.PERMISSION_GRANTED
- }
-
- private fun allLocationPermissionsGranted() = REQUIRED_PERMISSIONS_LOCATION.all {
- ContextCompat.checkSelfPermission(baseContext, it) == PackageManager.PERMISSION_GRANTED
- }
-
private fun updateGalleryButton() {
runOnUiThread {
val uri = sharedPreferences.lastSavedUri
@@ -1689,21 +1681,6 @@
companion object {
private const val LOG_TAG = "Aperture"
- private val REQUIRED_PERMISSIONS =
- mutableListOf(
- Manifest.permission.CAMERA,
- Manifest.permission.RECORD_AUDIO
- ).apply {
- if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
- add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
- }
- }.toTypedArray()
- internal val REQUIRED_PERMISSIONS_LOCATION =
- listOf(
- Manifest.permission.ACCESS_COARSE_LOCATION,
- Manifest.permission.ACCESS_FINE_LOCATION
- ).toTypedArray()
-
private const val MSG_HIDE_ZOOM_SLIDER = 0
private const val MSG_HIDE_FOCUS_RING = 1
private const val MSG_HIDE_EXPOSURE_SLIDER = 2
diff --git a/app/src/main/java/org/lineageos/aperture/SettingsActivity.kt b/app/src/main/java/org/lineageos/aperture/SettingsActivity.kt
index 69cdf37..4631947 100644
--- a/app/src/main/java/org/lineageos/aperture/SettingsActivity.kt
+++ b/app/src/main/java/org/lineageos/aperture/SettingsActivity.kt
@@ -6,7 +6,6 @@
package org.lineageos.aperture
import android.annotation.SuppressLint
-import android.content.pm.PackageManager
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
@@ -16,11 +15,11 @@
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
-import androidx.core.content.ContextCompat
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import org.lineageos.aperture.utils.CameraSoundsUtils
+import org.lineageos.aperture.utils.PermissionsUtils
class SettingsActivity : AppCompatActivity() {
@@ -68,10 +67,12 @@
findPreference<SwitchPreference>("video_stabilization_ois")!!
}
+ private val permissionsUtils by lazy { PermissionsUtils(requireContext()) }
+
private val requestLocationPermissions = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) {
- if (it.any { permission -> !permission.value }) {
+ if (!permissionsUtils.locationPermissionsGranted()) {
saveLocation?.isChecked = false
Toast.makeText(
requireContext(), getString(R.string.save_location_toast), Toast.LENGTH_SHORT
@@ -127,13 +128,11 @@
setPreferencesFromResource(R.xml.root_preferences, rootKey)
saveLocation?.let {
// Reset location back to off if permissions aren't granted
- it.isChecked = it.isChecked && allLocationPermissionsGranted()
+ it.isChecked = it.isChecked && permissionsUtils.locationPermissionsGranted()
it.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _, newValue ->
if (newValue as Boolean) {
- requestLocationPermissions.launch(
- CameraActivity.REQUIRED_PERMISSIONS_LOCATION
- )
+ requestLocationPermissions.launch(PermissionsUtils.locationPermissions)
}
true
}
@@ -153,13 +152,5 @@
videoStabilizationOis.isEnabled =
videoStabilization.isChecked && !videoStabilizationPreview.isChecked
}
-
- @SuppressLint("UnsafeOptInUsageError")
- private fun allLocationPermissionsGranted() =
- CameraActivity.REQUIRED_PERMISSIONS_LOCATION.all {
- ContextCompat.checkSelfPermission(
- requireContext(), it
- ) == PackageManager.PERMISSION_GRANTED
- }
}
}
diff --git a/app/src/main/java/org/lineageos/aperture/utils/PermissionsUtils.kt b/app/src/main/java/org/lineageos/aperture/utils/PermissionsUtils.kt
new file mode 100644
index 0000000..3bc981b
--- /dev/null
+++ b/app/src/main/java/org/lineageos/aperture/utils/PermissionsUtils.kt
@@ -0,0 +1,55 @@
+/*
+ * SPDX-FileCopyrightText: 2023 The LineageOS Project
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package org.lineageos.aperture.utils
+
+import android.Manifest
+import android.content.Context
+import android.content.pm.PackageManager
+import android.os.Build
+import androidx.core.content.ContextCompat
+
+/**
+ * App's permissions utils.
+ */
+class PermissionsUtils(private val context: Context) {
+ fun mainPermissionsGranted() = permissionsGranted(mainPermissions)
+ fun locationPermissionsGranted() = permissionsGranted(locationPermissions)
+
+ private fun permissionGranted(permission: String) =
+ ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED
+
+ private fun permissionsGranted(permissions: Array<String>) = permissions.all {
+ permissionGranted(it)
+ }
+
+ companion object {
+ /**
+ * Permissions required to run the app
+ */
+ val mainPermissions = mutableListOf(
+ Manifest.permission.CAMERA,
+ Manifest.permission.RECORD_AUDIO,
+ ).apply {
+ if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
+ add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
+ }
+ }.toTypedArray()
+
+ /**
+ * Permissions needed for location tag in saved photos and videos
+ */
+ val locationPermissions = arrayOf(
+ Manifest.permission.ACCESS_COARSE_LOCATION,
+ Manifest.permission.ACCESS_FINE_LOCATION,
+ )
+
+ /**
+ * All the permissions that the app might use
+ */
+ val allPermissions = mainPermissions
+ .plus(locationPermissions)
+ }
+}