Skip to content

Commit

Permalink
Remove cleanup requirement for storage permission
Browse files Browse the repository at this point in the history
We're able to write directly to the Downloads folder (tested on API 33) so there's no reason to check for storage permission when trying to cleanup, we instead just check if we can actually write to that directory and if not we don't cleanup.

This is related to #2415
  • Loading branch information
pyricau committed Nov 10, 2022
1 parent e306e5c commit 50cbf41
Showing 1 changed file with 19 additions and 19 deletions.
Expand Up @@ -43,24 +43,6 @@ internal class LeakDirectoryProvider constructor(
) {
private val context: Context = context.applicationContext

fun listFiles(filter: FilenameFilter): MutableList<File> {
if (!hasStoragePermission() && requestExternalStoragePermission()) {
requestWritePermissionNotification()
}
val files = ArrayList<File>()

val externalFiles = externalStorageDirectory().listFiles(filter)
if (externalFiles != null) {
files.addAll(externalFiles)
}

val appFiles = appStorageDirectory().listFiles(filter)
if (appFiles != null) {
files.addAll(appFiles)
}
return files
}

fun newHeapDumpFile(): File? {
cleanupOldHeapDumps()

Expand Down Expand Up @@ -147,7 +129,7 @@ internal class LeakDirectoryProvider constructor(
}

private fun cleanupOldHeapDumps() {
val hprofFiles = listFiles { _, name ->
val hprofFiles = listWritableFiles { _, name ->
name.endsWith(
HPROF_SUFFIX
)
Expand Down Expand Up @@ -177,6 +159,24 @@ internal class LeakDirectoryProvider constructor(
}
}

private fun listWritableFiles(filter: FilenameFilter): MutableList<File> {
val files = ArrayList<File>()

val externalStorageDirectory = externalStorageDirectory()
if (externalStorageDirectory.exists() && externalStorageDirectory.canWrite()) {
val externalFiles = externalStorageDirectory.listFiles(filter)
if (externalFiles != null) {
files.addAll(externalFiles)
}
}

val appFiles = appStorageDirectory().listFiles(filter)
if (appFiles != null) {
files.addAll(appFiles)
}
return files
}

companion object {
@Volatile private var writeExternalStorageGranted: Boolean = false
@Volatile private var permissionNotificationDisplayed: Boolean = false
Expand Down

0 comments on commit 50cbf41

Please sign in to comment.