From e5b965c646933d14351d125059ffb1b063faf765 Mon Sep 17 00:00:00 2001 From: Sean Mac Gillicuddy Date: Fri, 20 Dec 2019 13:13:09 +0000 Subject: [PATCH] #1673 Crash Report 3.1.2 Custom: Successive permission requests - handle case where permission is denied forever --- .../core/utils/AlertDialogShower.kt | 1 + .../kiwixmobile/core/utils/KiwixDialog.kt | 11 +++++++- core/src/main/res/values/strings.xml | 3 +++ .../custom/main/CustomMainActivity.kt | 26 +++++++++++++++++-- 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/AlertDialogShower.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/AlertDialogShower.kt index 77cb22e6a..8cf6c7f1f 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/AlertDialogShower.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/AlertDialogShower.kt @@ -45,6 +45,7 @@ class AlertDialogShower @Inject constructor(private val activity: Activity) : Di ?.invoke() } } + setCancelable(dialog.cancelable) } .show() } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/KiwixDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/KiwixDialog.kt index 9c26ee1f5..54679afc6 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/KiwixDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/KiwixDialog.kt @@ -26,7 +26,8 @@ sealed class KiwixDialog( val title: Int?, val message: Int, val positiveMessage: Int, - val negativeMessage: Int? + val negativeMessage: Int?, + val cancelable: Boolean = true ) { data class DeleteZim(override val args: List) : KiwixDialog( @@ -64,6 +65,14 @@ sealed class KiwixDialog( null ) + object ReadPermissionRequired : KiwixDialog( + R.string.storage_permission_denied, + R.string.grant_read_storage_permission, + R.string.go_to_permissions, + null, + cancelable = false + ) + data class ShowHotspotDetails(override val args: List) : KiwixDialog( R.string.hotspot_turned_on, R.string.hotspot_details_message, diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 25f568404..28f125fc6 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -289,6 +289,9 @@ Vid Text Only Short Text + Storage Permission Denied + This app requires the ability to read storage to function. Please grant the permission in your settings + Go to Settings On Off diff --git a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt index b8b9823e3..a024e5c82 100644 --- a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt +++ b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt @@ -20,16 +20,22 @@ package org.kiwix.kiwixmobile.custom.main import android.Manifest.permission.READ_EXTERNAL_STORAGE import android.annotation.TargetApi +import android.content.Intent import android.content.pm.PackageManager.PERMISSION_DENIED +import android.net.Uri import android.os.Build.VERSION_CODES import android.os.Bundle +import android.provider.Settings import android.util.Log import android.view.Menu +import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.start import org.kiwix.kiwixmobile.core.main.CoreMainActivity import org.kiwix.kiwixmobile.core.main.WebViewCallback import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer +import org.kiwix.kiwixmobile.core.utils.DialogShower +import org.kiwix.kiwixmobile.core.utils.KiwixDialog import org.kiwix.kiwixmobile.core.utils.LanguageUtils import org.kiwix.kiwixmobile.custom.BuildConfig import org.kiwix.kiwixmobile.custom.R @@ -44,6 +50,7 @@ const val REQUEST_READ_FOR_OBB = 5002 class CustomMainActivity : CoreMainActivity() { @Inject lateinit var customFileValidator: CustomFileValidator + @Inject lateinit var dialogShower: DialogShower override fun showHomePage() { Log.e("CustomMain", "tried to show home page") @@ -94,11 +101,26 @@ class CustomMainActivity : CoreMainActivity() { grantResults: IntArray ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) - if (requestCode == REQUEST_READ_FOR_OBB) { - openObbOrZim() + if (permissions.isNotEmpty() && permissions[0] == READ_EXTERNAL_STORAGE) { + if (readStorageHasBeenPermanentlyDenied(grantResults)) { + dialogShower.show(KiwixDialog.ReadPermissionRequired, ::goToSettings) + } else { + openObbOrZim() + } } } + private fun goToSettings() { + startActivity(Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply { + data = Uri.fromParts("package", packageName, null) + }) + } + + @TargetApi(VERSION_CODES.JELLY_BEAN) + private fun readStorageHasBeenPermanentlyDenied(grantResults: IntArray) = + grantResults[0] == PERMISSION_DENIED && + !ActivityCompat.shouldShowRequestPermissionRationale(this, READ_EXTERNAL_STORAGE) + override fun onCreateOptionsMenu(menu: Menu?): Boolean { val onCreateOptionsMenu = super.onCreateOptionsMenu(menu) menu?.findItem(R.id.menu_help)?.isVisible = false