From 9cf3e71fd23467633f5669e4d11360511c34be47 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Mon, 7 Jun 2021 14:55:01 +0200 Subject: [PATCH] Ask storage permissions only when using external volumes --- .../sushi/hardcore/droidfs/BaseActivity.kt | 8 +- .../droidfs/ChangePasswordActivity.kt | 2 +- .../sushi/hardcore/droidfs/CreateActivity.kt | 2 +- .../sushi/hardcore/droidfs/MainActivity.kt | 94 ++++--------------- .../sushi/hardcore/droidfs/OpenActivity.kt | 11 ++- .../hardcore/droidfs/VolumeActionActivity.kt | 38 +++++++- 6 files changed, 67 insertions(+), 88 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/BaseActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/BaseActivity.kt index 01f61be..09a5c4e 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/BaseActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/BaseActivity.kt @@ -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) diff --git a/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt index 5ae55cf..26a4679 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/ChangePasswordActivity.kt @@ -78,7 +78,7 @@ class ChangePasswordActivity : VolumeActionActivity() { } fun pickDirectory(view: View?) { - safePickDirectory() + askPermissionThenPickDirectory() } override fun onDirectoryPicked(uri: Uri) { diff --git a/app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt index bcd8191..5188d13 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/CreateActivity.kt @@ -42,7 +42,7 @@ class CreateActivity : VolumeActionActivity() { } fun pickDirectory(view: View?) { - safePickDirectory() + askPermissionThenPickDirectory() } override fun onDirectoryPicked(uri: Uri) { diff --git a/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt index d2f0a56..0803535 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt @@ -1,92 +1,32 @@ 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) - .setMessage(R.string.usf_home_warning_msg) - .setCancelable(false) - .setPositiveButton(R.string.see_unsafe_features){ _, _ -> - val intent = Intent(this, SettingsActivity::class.java) - intent.putExtra("screen", "UnsafeFeaturesSettingsFragment") - startActivity(intent) - } - .setNegativeButton(R.string.ok, null) - .setOnDismissListener { sharedPrefs.edit().putBoolean("applicationFirstOpening", false).apply() } - .show() - } - } - - override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, 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() - } + if (!isRecreating) { + if (sharedPrefs.getBoolean("applicationFirstOpening", true)){ + ColoredAlertDialogBuilder(this) + .setTitle(R.string.warning) + .setMessage(R.string.usf_home_warning_msg) + .setCancelable(false) + .setPositiveButton(R.string.see_unsafe_features){ _, _ -> + val intent = Intent(this, SettingsActivity::class.java) + intent.putExtra("screen", "UnsafeFeaturesSettingsFragment") + startActivity(intent) + } + .setNegativeButton(R.string.ok, null) + .setOnDismissListener { sharedPrefs.edit().putBoolean("applicationFirstOpening", false).apply() } + .show() } } } diff --git a/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt index 11f1cd5..088538d 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/OpenActivity.kt @@ -107,8 +107,11 @@ class OpenActivity : VolumeActionActivity() { } fun pickDirectory(view: View?) { + askPermissionThenPickDirectory() + } + + override fun onPickingDirectory() { isStartingActivity = true - safePickDirectory() } override fun onDirectoryPicked(uri: Uri) { @@ -253,10 +256,10 @@ class OpenActivity : VolumeActionActivity() { isFinishingIntentionally = true } - override fun onPause() { - super.onPause() + override fun onStop() { + super.onStop() if (intent.action == "pick"){ - if (isStartingActivity){ + if (isStartingActivity) { isStartingActivity = false } else { finish() diff --git a/app/src/main/java/sushi/hardcore/droidfs/VolumeActionActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/VolumeActionActivity.kt index cca7655..37e227e 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/VolumeActionActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/VolumeActionActivity.kt @@ -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, 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