From 3d8cea5966002a9f383f227be8d31c9265c108b4 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Wed, 22 Apr 2020 21:57:52 +0530 Subject: Improve permission handling logic (#732) * Improve permission handling logic Ensure we always ask for storage permissions when required * Refactor storage permission checks and invert return value * PasswordStore: improve permission grant flow * strings: slightly reword permission grant request message Signed-off-by: Harsh Shandilya Co-authored-by: Fabian Henneke Signed-off-by: Harsh Shandilya --- .../main/java/com/zeapo/pwdstore/PasswordStore.kt | 68 ++++++++++++---------- .../main/java/com/zeapo/pwdstore/UserPreference.kt | 6 ++ app/src/main/res/values-ru/strings.xml | 1 - app/src/main/res/values/strings.xml | 3 +- 4 files changed, 45 insertions(+), 33 deletions(-) (limited to 'app') diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt index 58e1f39c..fe18c6b7 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt @@ -12,7 +12,6 @@ import android.content.SharedPreferences import android.content.pm.PackageManager import android.content.pm.ShortcutInfo.Builder import android.content.pm.ShortcutManager -import android.graphics.Color import android.graphics.drawable.Icon import android.net.Uri import android.os.Build @@ -180,34 +179,7 @@ class PasswordStore : AppCompatActivity() { super.onResume() // do not attempt to checkLocalRepository() if no storage permission: immediate crash if (settings.getBoolean("git_external", false)) { - if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) - != PackageManager.PERMISSION_GRANTED) { - if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { - val snack = Snackbar.make( - findViewById(R.id.main_layout), - getString(R.string.access_sdcard_text), - Snackbar.LENGTH_INDEFINITE) - .setAction(R.string.dialog_ok) { - ActivityCompat.requestPermissions( - activity, - arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), - REQUEST_EXTERNAL_STORAGE) - } - snack.show() - val view = snack.view - val tv: AppCompatTextView = view.findViewById(com.google.android.material.R.id.snackbar_text) - tv.setTextColor(Color.WHITE) - tv.maxLines = 10 - } else { - // No explanation needed, we can request the permission. - ActivityCompat.requestPermissions( - activity, - arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), - REQUEST_EXTERNAL_STORAGE) - } - } else { - checkLocalRepository() - } + hasRequiredStoragePermissions(true) } else { checkLocalRepository() } @@ -389,8 +361,12 @@ class PasswordStore : AppCompatActivity() { } private fun initializeRepositoryInfo() { + val externalRepo = settings.getBoolean("git_external", false) val externalRepoPath = settings.getString("git_external_repo", null) - if (settings.getBoolean("git_external", false) && externalRepoPath != null) { + if (externalRepo && !hasRequiredStoragePermissions()) { + return + } + if (externalRepo && externalRepoPath != null) { val dir = File(externalRepoPath) if (dir.exists() && dir.isDirectory && getPasswords(dir, getRepositoryDirectory(this), sortOrder).isNotEmpty()) { @@ -413,6 +389,36 @@ class PasswordStore : AppCompatActivity() { createRepository() } + /** + * Validates if storage permission is granted, and requests for it if not. The return value + * is true if the permission has been granted. + */ + private fun hasRequiredStoragePermissions(checkLocalRepo: Boolean = false): Boolean { + return if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + Snackbar.make( + findViewById(R.id.main_layout), + getString(R.string.access_sdcard_text), + Snackbar.LENGTH_INDEFINITE + ).run { + setAction(getString(R.string.snackbar_action_grant)) { + ActivityCompat.requestPermissions( + activity, + arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), + REQUEST_EXTERNAL_STORAGE + ) + dismiss() + } + show() + } + false + } else { + if (checkLocalRepo) + checkLocalRepository() + true + } + } + private fun checkLocalRepository() { val repo = initialize(this) if (repo == null) { @@ -631,7 +637,7 @@ class PasswordStore : AppCompatActivity() { get() = plist?.currentDir ?: getRepositoryDirectory(applicationContext) private fun commitChange(message: String) { - Companion.commitChange(activity, message) + commitChange(activity, message) } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt index 9d56845c..32d1e87a 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt @@ -461,6 +461,12 @@ class UserPreference : AppCompatActivity() { } } + override fun onBackPressed() { + super.onBackPressed() + setResult(Activity.RESULT_OK) + finish() + } + public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) when (intent?.getStringExtra("operation")) { diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 051ab024..07c4dca7 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -303,7 +303,6 @@ Когда ключено, Password Store будет запрашивать ваш опечаток пальца при каждом запуске приложения Сенсор отпечатка пальца не доступен или отсутствует Очистить сохраненный SSH Key идентификатор OpenKystortore - Хранилище находится на sd-карте, но приложение не имеет прав доступа к ней. Пожалуйста, дайте приложению необходимые разрешения. Ваш публичный ключ Возникла ошибка при попытке генерации ssh ключа Показать скрытые папки diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1c1f480d..bc7f0a6f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -333,7 +333,7 @@ When enabled, Password Store will prompt you for your fingerprint when launching the app Fingerprint hardware not accessible or missing Clear remembered OpenKeystore SSH Key ID - The store is on the sdcard but the app does not have permission to access it. Please give permission. + The store location is in your SD Card or Internal storage, but the app does not have permission to access it. Your public key Error while trying to generate the ssh-key Show hidden folders @@ -364,4 +364,5 @@ Create new password New, revamped Autofill! In this release, Autofill support has been massively improved with advanced features like anti-phishing protection and enhanced reliability. If you have been holding out on using it because of the shortcomings on the previous version, you\'ll likely love the new iteration. Give it a shot! + Grant -- cgit v1.2.3