Skip to content

Commit

Permalink
Merge pull request #2442 from square/py/storage
Browse files Browse the repository at this point in the history
Remove cleanup requirement for storage permission
  • Loading branch information
pyricau committed Nov 10, 2022
2 parents e306e5c + 50cbf41 commit dd7c459
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 dd7c459

Please sign in to comment.