Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 3d8cea5

Browse files
msfjarvisfmeum
andauthored
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 <me@msfjarvis.dev> Co-authored-by: Fabian Henneke <fabian@henneke.me> Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
1 parent 00361a5 commit 3d8cea5

4 files changed

Lines changed: 45 additions & 33 deletions

File tree

app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import android.content.SharedPreferences
1212
import android.content.pm.PackageManager
1313
import android.content.pm.ShortcutInfo.Builder
1414
import android.content.pm.ShortcutManager
15-
import android.graphics.Color
1615
import android.graphics.drawable.Icon
1716
import android.net.Uri
1817
import android.os.Build
@@ -180,34 +179,7 @@ class PasswordStore : AppCompatActivity() {
180179
super.onResume()
181180
// do not attempt to checkLocalRepository() if no storage permission: immediate crash
182181
if (settings.getBoolean("git_external", false)) {
183-
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
184-
!= PackageManager.PERMISSION_GRANTED) {
185-
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
186-
val snack = Snackbar.make(
187-
findViewById(R.id.main_layout),
188-
getString(R.string.access_sdcard_text),
189-
Snackbar.LENGTH_INDEFINITE)
190-
.setAction(R.string.dialog_ok) {
191-
ActivityCompat.requestPermissions(
192-
activity,
193-
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
194-
REQUEST_EXTERNAL_STORAGE)
195-
}
196-
snack.show()
197-
val view = snack.view
198-
val tv: AppCompatTextView = view.findViewById(com.google.android.material.R.id.snackbar_text)
199-
tv.setTextColor(Color.WHITE)
200-
tv.maxLines = 10
201-
} else {
202-
// No explanation needed, we can request the permission.
203-
ActivityCompat.requestPermissions(
204-
activity,
205-
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
206-
REQUEST_EXTERNAL_STORAGE)
207-
}
208-
} else {
209-
checkLocalRepository()
210-
}
182+
hasRequiredStoragePermissions(true)
211183
} else {
212184
checkLocalRepository()
213185
}
@@ -389,8 +361,12 @@ class PasswordStore : AppCompatActivity() {
389361
}
390362

391363
private fun initializeRepositoryInfo() {
364+
val externalRepo = settings.getBoolean("git_external", false)
392365
val externalRepoPath = settings.getString("git_external_repo", null)
393-
if (settings.getBoolean("git_external", false) && externalRepoPath != null) {
366+
if (externalRepo && !hasRequiredStoragePermissions()) {
367+
return
368+
}
369+
if (externalRepo && externalRepoPath != null) {
394370
val dir = File(externalRepoPath)
395371
if (dir.exists() && dir.isDirectory &&
396372
getPasswords(dir, getRepositoryDirectory(this), sortOrder).isNotEmpty()) {
@@ -413,6 +389,36 @@ class PasswordStore : AppCompatActivity() {
413389
createRepository()
414390
}
415391

392+
/**
393+
* Validates if storage permission is granted, and requests for it if not. The return value
394+
* is true if the permission has been granted.
395+
*/
396+
private fun hasRequiredStoragePermissions(checkLocalRepo: Boolean = false): Boolean {
397+
return if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
398+
!= PackageManager.PERMISSION_GRANTED) {
399+
Snackbar.make(
400+
findViewById(R.id.main_layout),
401+
getString(R.string.access_sdcard_text),
402+
Snackbar.LENGTH_INDEFINITE
403+
).run {
404+
setAction(getString(R.string.snackbar_action_grant)) {
405+
ActivityCompat.requestPermissions(
406+
activity,
407+
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
408+
REQUEST_EXTERNAL_STORAGE
409+
)
410+
dismiss()
411+
}
412+
show()
413+
}
414+
false
415+
} else {
416+
if (checkLocalRepo)
417+
checkLocalRepository()
418+
true
419+
}
420+
}
421+
416422
private fun checkLocalRepository() {
417423
val repo = initialize(this)
418424
if (repo == null) {
@@ -631,7 +637,7 @@ class PasswordStore : AppCompatActivity() {
631637
get() = plist?.currentDir ?: getRepositoryDirectory(applicationContext)
632638

633639
private fun commitChange(message: String) {
634-
Companion.commitChange(activity, message)
640+
commitChange(activity, message)
635641
}
636642

637643
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

app/src/main/java/com/zeapo/pwdstore/UserPreference.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,12 @@ class UserPreference : AppCompatActivity() {
461461
}
462462
}
463463

464+
override fun onBackPressed() {
465+
super.onBackPressed()
466+
setResult(Activity.RESULT_OK)
467+
finish()
468+
}
469+
464470
public override fun onCreate(savedInstanceState: Bundle?) {
465471
super.onCreate(savedInstanceState)
466472
when (intent?.getStringExtra("operation")) {

app/src/main/res/values-ru/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@
303303
<string name="biometric_auth_summary">Когда ключено, Password Store будет запрашивать ваш опечаток пальца при каждом запуске приложения</string>
304304
<string name="biometric_auth_summary_error">Сенсор отпечатка пальца не доступен или отсутствует</string>
305305
<string name="ssh_openkeystore_clear_keyid">Очистить сохраненный SSH Key идентификатор OpenKystortore</string>
306-
<string name="access_sdcard_text">Хранилище находится на sd-карте, но приложение не имеет прав доступа к ней. Пожалуйста, дайте приложению необходимые разрешения.</string>
307306
<string name="your_public_key">Ваш публичный ключ</string>
308307
<string name="error_generate_ssh_key">Возникла ошибка при попытке генерации ssh ключа</string>
309308
<string name="pref_show_hidden_title">Показать скрытые папки</string>

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@
333333
<string name="biometric_auth_summary">When enabled, Password Store will prompt you for your fingerprint when launching the app</string>
334334
<string name="biometric_auth_summary_error">Fingerprint hardware not accessible or missing</string>
335335
<string name="ssh_openkeystore_clear_keyid">Clear remembered OpenKeystore SSH Key ID</string>
336-
<string name="access_sdcard_text">The store is on the sdcard but the app does not have permission to access it. Please give permission.</string>
336+
<string name="access_sdcard_text">The store location is in your SD Card or Internal storage, but the app does not have permission to access it.</string>
337337
<string name="your_public_key">Your public key</string>
338338
<string name="error_generate_ssh_key">Error while trying to generate the ssh-key</string>
339339
<string name="pref_show_hidden_title">Show hidden folders</string>
@@ -364,4 +364,5 @@
364364
<string name="bottom_sheet_create_new_password">Create new password</string>
365365
<string name="autofill_onboarding_dialog_title">New, revamped Autofill!</string>
366366
<string name="autofill_onboarding_dialog_message">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!</string>
367+
<string name="snackbar_action_grant">Grant</string>
367368
</resources>

0 commit comments

Comments
 (0)