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 <me@msfjarvis.dev>

* all: rename permission grant method

The new name is a bit more explicit about its purpose

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>

* UserPreference: add static method for external directory selection intent

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-09-09 11:27:03 +05:30 committed by GitHub
parent 9e0fb93f91
commit 276aa4ea7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 18 deletions

View file

@ -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) {

View file

@ -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())
}

View file

@ -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
*/

View file

@ -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
}