From 276aa4ea7cec53d37dd4dc777119c3114535b628 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Wed, 9 Sep 2020 11:27:03 +0530 Subject: Fix directory selection in onboarding if storage permissions are granted (#1090) * OnboardingActivity: fix directory change option Add an else clause to cover the case when storage permission is granted already Signed-off-by: Harsh Shandilya * all: rename permission grant method The new name is a bit more explicit about its purpose Signed-off-by: Harsh Shandilya * UserPreference: add static method for external directory selection intent Signed-off-by: Harsh Shandilya --- .../java/com/zeapo/pwdstore/OnboardingActivity.kt | 20 +++++++++----------- .../main/java/com/zeapo/pwdstore/PasswordStore.kt | 10 ++++------ .../main/java/com/zeapo/pwdstore/UserPreference.kt | 6 ++++++ .../main/java/com/zeapo/pwdstore/utils/Extensions.kt | 2 +- 4 files changed, 20 insertions(+), 18 deletions(-) (limited to 'app/src/main/java/com/zeapo') diff --git a/app/src/main/java/com/zeapo/pwdstore/OnboardingActivity.kt b/app/src/main/java/com/zeapo/pwdstore/OnboardingActivity.kt index d26860dc..5e415766 100644 --- a/app/src/main/java/com/zeapo/pwdstore/OnboardingActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/OnboardingActivity.kt @@ -21,7 +21,7 @@ import com.zeapo.pwdstore.git.BaseGitActivity import com.zeapo.pwdstore.git.GitServerConfigActivity import com.zeapo.pwdstore.utils.PasswordRepository import com.zeapo.pwdstore.utils.PreferenceKeys -import com.zeapo.pwdstore.utils.checkRuntimePermission +import com.zeapo.pwdstore.utils.isPermissionGranted import com.zeapo.pwdstore.utils.getString import com.zeapo.pwdstore.utils.listFilesRecursively import com.zeapo.pwdstore.utils.sharedPrefs @@ -116,12 +116,10 @@ class OnboardingActivity : AppCompatActivity() { settings.edit { putBoolean(PreferenceKeys.GIT_EXTERNAL, true) } val externalRepo = settings.getString(PreferenceKeys.GIT_EXTERNAL_REPO) if (externalRepo == null) { - if (!checkRuntimePermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + if (!isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { registerForActivityResult(RequestPermission()) { granted -> if (granted) { - externalDirectorySelectAction.launch(Intent(this, UserPreference::class.java).apply { - putExtra("operation", "git_external") - }) + externalDirectorySelectAction.launch(UserPreference.createDirectorySelectionIntent(this)) } }.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE) } @@ -130,7 +128,7 @@ class OnboardingActivity : AppCompatActivity() { .setTitle(resources.getString(R.string.directory_selected_title)) .setMessage(resources.getString(R.string.directory_selected_message, externalRepo)) .setPositiveButton(resources.getString(R.string.use)) { _, _ -> - if (!checkRuntimePermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + if (!isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { registerForActivityResult(RequestPermission()) { granted -> if (granted) { initializeRepositoryInfo() @@ -141,14 +139,14 @@ class OnboardingActivity : AppCompatActivity() { } } .setNegativeButton(resources.getString(R.string.change)) { _, _ -> - if (!checkRuntimePermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + if (!isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { registerForActivityResult(RequestPermission()) { granted -> if (granted) { - repositoryInitAction.launch(Intent(this, UserPreference::class.java).apply { - putExtra("operation", "git_external") - }) + repositoryInitAction.launch(UserPreference.createDirectorySelectionIntent(this)) } }.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE) + } else { + repositoryInitAction.launch(UserPreference.createDirectorySelectionIntent(this)) } } .show() @@ -198,7 +196,7 @@ class OnboardingActivity : AppCompatActivity() { private fun initializeRepositoryInfo() { val externalRepo = settings.getBoolean(PreferenceKeys.GIT_EXTERNAL, false) val externalRepoPath = settings.getString(PreferenceKeys.GIT_EXTERNAL_REPO) - if (externalRepo && !checkRuntimePermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + if (externalRepo && !isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { return } if (externalRepo && externalRepoPath != null) { diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt index 2639c6b6..7501146a 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt @@ -60,7 +60,7 @@ import com.zeapo.pwdstore.utils.PasswordRepository.Companion.initialize import com.zeapo.pwdstore.utils.PasswordRepository.Companion.isInitialized import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.base64 -import com.zeapo.pwdstore.utils.checkRuntimePermission +import com.zeapo.pwdstore.utils.isPermissionGranted import com.zeapo.pwdstore.utils.commitChange import com.zeapo.pwdstore.utils.contains import com.zeapo.pwdstore.utils.getString @@ -115,7 +115,7 @@ class PasswordStore : BaseGitActivity() { // prevent attempt to create password list fragment var savedInstance = savedInstanceState if (savedInstanceState != null && (!settings.getBoolean(PreferenceKeys.GIT_EXTERNAL, false) || - !checkRuntimePermission(Manifest.permission.WRITE_EXTERNAL_STORAGE))) { + !isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE))) { savedInstance = null } super.onCreate(savedInstance) @@ -324,7 +324,7 @@ class PasswordStore : BaseGitActivity() { * is true if the permission has been granted. */ private fun hasRequiredStoragePermissions(): Boolean { - return if (!checkRuntimePermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + return if (!isPermissionGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { Snackbar.make( findViewById(R.id.main_layout), getString(R.string.access_sdcard_text), @@ -348,13 +348,11 @@ class PasswordStore : BaseGitActivity() { private fun checkLocalRepository() { val repo = initialize() if (repo == null) { - val intent = Intent(activity, UserPreference::class.java) - intent.putExtra("operation", "git_external") registerForActivityResult(StartActivityForResult()) { result -> if (result.resultCode == RESULT_OK) { checkLocalRepository() } - }.launch(intent) + }.launch(UserPreference.createDirectorySelectionIntent(this)) } else { checkLocalRepository(getRepositoryDirectory()) } diff --git a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt index fa325f9c..d41a7fe7 100644 --- a/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt +++ b/app/src/main/java/com/zeapo/pwdstore/UserPreference.kt @@ -678,6 +678,12 @@ class UserPreference : AppCompatActivity() { private const val TAG = "UserPreference" + fun createDirectorySelectionIntent(context: Context): Intent { + return Intent(context, UserPreference::class.java).run { + putExtra("operation", "git_external") + } + } + /** * Set custom dictionary summary */ diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt b/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt index d54e26cd..d9277bef 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt @@ -135,7 +135,7 @@ suspend fun FragmentActivity.commitChange( }.execute() } -fun FragmentActivity.checkRuntimePermission(permission: String): Boolean { +fun FragmentActivity.isPermissionGranted(permission: String): Boolean { return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED } -- cgit v1.2.3