Ask storage permissions only when using external volumes

This commit is contained in:
Matéo Duparc 2021-06-07 14:55:01 +02:00
parent 9f8b653cc7
commit 9cf3e71fd2
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
6 changed files with 67 additions and 88 deletions

View File

@ -12,15 +12,15 @@ open class BaseActivity: CyaneaAppCompatActivity() {
protected lateinit var sharedPrefs: SharedPreferences
protected var isRecreating = false
override fun onCreate(savedInstanceState: Bundle?) {
val themeColor = ThemeColor.getThemeColor(this)
if (cyanea.accent != themeColor){
changeThemeColor(themeColor)
}
super.onCreate(savedInstanceState)
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this)
if (!sharedPrefs.getBoolean("usf_screenshot", false)){
window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)
}
val themeColor = ThemeColor.getThemeColor(this)
if (cyanea.accent != themeColor){
changeThemeColor(themeColor)
}
}
fun changeThemeColor(themeColor: Int? = null){
val accentColor = themeColor ?: ThemeColor.getThemeColor(this)

View File

@ -78,7 +78,7 @@ class ChangePasswordActivity : VolumeActionActivity() {
}
fun pickDirectory(view: View?) {
safePickDirectory()
askPermissionThenPickDirectory()
}
override fun onDirectoryPicked(uri: Uri) {

View File

@ -42,7 +42,7 @@ class CreateActivity : VolumeActionActivity() {
}
fun pickDirectory(view: View?) {
safePickDirectory()
askPermissionThenPickDirectory()
}
override fun onDirectoryPicked(uri: Uri) {

View File

@ -1,64 +1,19 @@
package sushi.hardcore.droidfs
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.util.DisplayMetrics
import android.view.*
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_main.*
import android.view.Menu
import android.view.MenuItem
import android.view.View
import kotlinx.android.synthetic.main.toolbar.*
import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder
class MainActivity : BaseActivity() {
companion object {
private const val STORAGE_PERMISSIONS_REQUEST = 1
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
if (!isRecreating) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) +
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE), STORAGE_PERMISSIONS_REQUEST)
} else {
onStoragePermissionGranted()
}
} else {
onStoragePermissionGranted()
}
}
}
private fun onStoragePermissionGranted(){
if (checkStorageAvailability()){
checkFirstOpening()
}
}
private fun checkStorageAvailability(): Boolean {
val state = Environment.getExternalStorageState()
val storageAvailable = Environment.MEDIA_MOUNTED == state || Environment.MEDIA_MOUNTED_READ_ONLY == state
return if (!storageAvailable) {
ColoredAlertDialogBuilder(this)
.setTitle(R.string.storage_unavailable)
.setMessage(R.string.storage_unavailable_msg)
.setCancelable(false)
.setPositiveButton(R.string.ok) { _, _ -> finish() }.show()
false
} else {
true
}
}
private fun checkFirstOpening() {
if (sharedPrefs.getBoolean("applicationFirstOpening", true)){
ColoredAlertDialogBuilder(this)
.setTitle(R.string.warning)
@ -74,21 +29,6 @@ class MainActivity : BaseActivity() {
.show()
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {
STORAGE_PERMISSIONS_REQUEST -> if (grantResults.size == 2) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
ColoredAlertDialogBuilder(this)
.setTitle(R.string.storage_perm_denied)
.setMessage(R.string.storage_perm_denied_msg)
.setCancelable(false)
.setPositiveButton(R.string.ok) { _, _ -> finish() }.show()
} else {
onStoragePermissionGranted()
}
}
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {

View File

@ -107,8 +107,11 @@ class OpenActivity : VolumeActionActivity() {
}
fun pickDirectory(view: View?) {
askPermissionThenPickDirectory()
}
override fun onPickingDirectory() {
isStartingActivity = true
safePickDirectory()
}
override fun onDirectoryPicked(uri: Uri) {
@ -253,8 +256,8 @@ class OpenActivity : VolumeActionActivity() {
isFinishingIntentionally = true
}
override fun onPause() {
super.onPause()
override fun onStop() {
super.onStop()
if (intent.action == "pick"){
if (isStartingActivity) {
isStartingActivity = false

View File

@ -1,8 +1,10 @@
package sushi.hardcore.droidfs
import android.Manifest
import android.app.KeyguardManager
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.security.keystore.KeyGenParameterSpec
@ -50,16 +52,32 @@ abstract class VolumeActionActivity : BaseActivity() {
private lateinit var originalHiddenVolumeSectionLayoutParams: LinearLayout.LayoutParams
private lateinit var originalNormalVolumeSectionLayoutParams: LinearLayout.LayoutParams
companion object {
private const val STORAGE_PERMISSIONS_REQUEST = 0
private const val ANDROID_KEY_STORE = "AndroidKeyStore"
private const val KEY_ALIAS = "Hash Key"
private const val KEY_SIZE = 256
private const val GCM_TAG_LEN = 128
}
protected open fun onPickingDirectory() {}
protected abstract fun onDirectoryPicked(uri: Uri)
protected fun safePickDirectory() {
protected fun askPermissionThenPickDirectory() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) +
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE), STORAGE_PERMISSIONS_REQUEST)
} else {
safePickDirectory()
}
} else {
safePickDirectory()
}
}
private fun safePickDirectory() {
try {
onPickingDirectory()
pickDirectory.launch(null)
} catch (e: ActivityNotFoundException) {
ColoredAlertDialogBuilder(this)
@ -70,6 +88,24 @@ abstract class VolumeActionActivity : BaseActivity() {
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
when (requestCode) {
STORAGE_PERMISSIONS_REQUEST -> if (grantResults.size == 2) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) {
ColoredAlertDialogBuilder(this)
.setTitle(R.string.storage_perm_denied)
.setMessage(R.string.storage_perm_denied_msg)
.setCancelable(false)
.setPositiveButton(R.string.ok, null)
.show()
} else {
safePickDirectory()
}
}
}
}
protected fun setupFingerprintStuff(){
originalHiddenVolumeSectionLayoutParams = hidden_volume_section.layoutParams as LinearLayout.LayoutParams
originalNormalVolumeSectionLayoutParams = normal_volume_section.layoutParams as LinearLayout.LayoutParams