Skip to content

Commit

Permalink
Merge pull request #3396 from kiwix/Issue#3370
Browse files Browse the repository at this point in the history
Fixed Invasive access photos and media permission request at start
  • Loading branch information
kelson42 authored Jun 5, 2023
2 parents 7df79cd + 035d56d commit 02e703c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,10 @@ abstract class CoreReaderFragment :
isPermissionGranted = true
} else {
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
requestPermissions(
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
REQUEST_WRITE_STORAGE_PERMISSION_ADD_NOTE
)
/* shouldShowRequestPermissionRationale() returns false when:
* 1) User has previously checked on "Don't ask me again", and/or
* 2) Permission has been disabled on device
Expand All @@ -1207,11 +1211,12 @@ abstract class CoreReaderFragment :
R.string.ext_storage_permission_rationale_add_note,
Toast.LENGTH_LONG
)
} else {
alertDialogShower?.show(
KiwixDialog.ReadPermissionRequired,
requireActivity()::navigateToAppSettings
)
}
requestPermissions(
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
REQUEST_WRITE_STORAGE_PERMISSION_ADD_NOTE
)
}
} else { // For Android versions below Marshmallow 6.0 (API 23)
isPermissionGranted = true // As already requested at install time
Expand Down Expand Up @@ -1269,8 +1274,8 @@ abstract class CoreReaderFragment :
externalLinkOpener?.openExternalUrl(intent)
}

protected fun openZimFile(file: File) {
if (hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
protected fun openZimFile(file: File, isCustomApp: Boolean = false) {
if (hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE) || isCustomApp) {
if (file.isFileExist()) {
openAndSetInContainer(file)
updateTitle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,28 @@ class CustomFileValidator @Inject constructor(private val context: Context) {

private fun obbFiles() = scanDirs(ContextCompat.getObbDirs(context), "obb")

private fun zimFiles() =
scanDirs(ContextCompat.getExternalFilesDirs(context, null), "zim")
private fun zimFiles(): List<File> {
// Create a list to store the parent directories
val directoryList = mutableListOf<File>()

// Get the external files directories for the app
ContextCompat.getExternalFilesDirs(context, null).forEach { dir ->
// Check if the directory's parent is not null
dir?.parent?.let { parentPath ->
// Add the parent directory to the list, so we can scan all the files contained in the folder.
// We are doing this because ContextCompat.getExternalFilesDirs(context, null) method returns the path to the
// "files" folder, which is specific to the app's package name, both for internal and SD card storage.
// By obtaining the parent directory, we can scan files from the app-specific directory itself.
directoryList.add(File(parentPath))
} ?: kotlin.run {
// If the parent directory is null, it means the current directory is the target folder itself.
// Add the current directory to the list, as it represents the app-specific directory for both internal
// and SD card storage. This allows us to scan files directly from this directory.
directoryList.add(dir)
}
}
return scanDirs(directoryList.toTypedArray(), "zim")
}

private fun scanDirs(dirs: Array<out File?>?, extensionToMatch: String): List<File> =
dirs?.filterNotNull()?.fold(listOf()) { acc, dir ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ import org.kiwix.kiwixmobile.custom.R
import org.kiwix.kiwixmobile.custom.customActivityComponent
import org.kiwix.kiwixmobile.custom.databinding.ActivityCustomMainBinding

const val REQUEST_READ_FOR_OBB = 5002

class CustomMainActivity : CoreMainActivity() {

override val navController: NavController by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,15 @@

package org.kiwix.kiwixmobile.custom.main

import android.Manifest
import android.Manifest.permission.READ_EXTERNAL_STORAGE
import android.annotation.TargetApi
import android.app.Dialog
import android.content.pm.PackageManager
import android.content.pm.PackageManager.PERMISSION_DENIED
import android.os.Build
import android.os.Bundle
import android.view.Menu
import android.view.MenuInflater
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
Expand All @@ -43,12 +37,10 @@ import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.setupDrawerToggl
import org.kiwix.kiwixmobile.core.main.CoreReaderFragment
import org.kiwix.kiwixmobile.core.main.FIND_IN_PAGE_SEARCH_STRING
import org.kiwix.kiwixmobile.core.main.MainMenu
import org.kiwix.kiwixmobile.core.navigateToAppSettings
import org.kiwix.kiwixmobile.core.search.viewmodel.effects.SearchItemToOpen
import org.kiwix.kiwixmobile.core.utils.LanguageUtils
import org.kiwix.kiwixmobile.core.utils.TAG_FILE_SEARCHED
import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog
import org.kiwix.kiwixmobile.core.utils.titleToUrl
import org.kiwix.kiwixmobile.core.utils.urlSuffixToParsableUrl
import org.kiwix.kiwixmobile.custom.BuildConfig
Expand Down Expand Up @@ -151,63 +143,20 @@ class CustomReaderFragment : CoreReaderFragment() {
customFileValidator.validate(
onFilesFound = {
when (it) {
is ValidationState.HasFile -> openZimFile(it.file)
is ValidationState.HasFile -> openZimFile(it.file, true)
is ValidationState.HasBothFiles -> {
it.zimFile.delete()
openZimFile(it.obbFile)
openZimFile(it.obbFile, true)
}
else -> {}
}
},
onNoFilesFound = {
if (ContextCompat.checkSelfPermission(
requireActivity(),
READ_EXTERNAL_STORAGE
) == PERMISSION_DENIED &&
sharedPreferenceUtil?.isPlayStoreBuildWithAndroid11OrAbove() == false &&
Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU
) {
requestPermissions(arrayOf(READ_EXTERNAL_STORAGE), REQUEST_READ_FOR_OBB)
} else {
findNavController().navigate(R.id.customDownloadFragment)
}
findNavController().navigate(R.id.customDownloadFragment)
}
)
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (permissions.isNotEmpty() && permissions[0] == Manifest.permission.READ_EXTERNAL_STORAGE) {
if (readStorageHasBeenPermanentlyDenied(grantResults)) {
if (permissionRequiredDialog?.isShowing != true) {
permissionRequiredDialog = dialogShower?.create(
KiwixDialog.ReadPermissionRequired,
{
requireActivity().navigateToAppSettings()
appSettingsLaunched = true
}
)
permissionRequiredDialog?.show()
}
} else {
openObbOrZim()
permissionRequiredDialog?.dismiss()
permissionRequiredDialog = null
}
}
}

private fun readStorageHasBeenPermanentlyDenied(grantResults: IntArray) =
grantResults[0] == PackageManager.PERMISSION_DENIED &&
!ActivityCompat.shouldShowRequestPermissionRationale(
requireActivity(),
Manifest.permission.READ_EXTERNAL_STORAGE
)

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
menu.findItem(R.id.menu_help)?.isVisible = false
Expand Down

0 comments on commit 02e703c

Please sign in to comment.