-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor, Share Storage Info, Check Relevant Permissions
- Loading branch information
Showing
7 changed files
with
169 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.komu.sekia.utils | ||
|
||
import android.os.Environment | ||
import android.os.StatFs | ||
import komu.seki.domain.models.DirectoryInfo | ||
import komu.seki.domain.models.StorageInfo | ||
import java.io.File | ||
import java.text.DecimalFormat | ||
|
||
fun getStorageInfo(): StorageInfo { | ||
val externalStorage = Environment.getExternalStorageDirectory() | ||
val stat = StatFs(externalStorage.path) | ||
|
||
val blockSize = stat.blockSizeLong | ||
val totalBlocks = stat.blockCountLong | ||
val availableBlocks = stat.availableBlocksLong | ||
|
||
val totalSpace = totalBlocks * blockSize // Total storage space in bytes | ||
val availableSpace = availableBlocks * blockSize // Available storage space in bytes | ||
val usedSpace = totalSpace - availableSpace // Used storage space in bytes | ||
|
||
// Return the formatted storage info as a StorageInfo object | ||
return StorageInfo( | ||
totalSpace = totalSpace, | ||
freeSpace = availableSpace, | ||
usedSpace = usedSpace | ||
) | ||
} | ||
|
||
fun formatSize(size: Long): String { | ||
val kilo = 1024.0 | ||
val mega = kilo * 1024 | ||
val giga = mega * 1024 | ||
|
||
val formatter = DecimalFormat("#.##") | ||
|
||
return when { | ||
size >= giga -> {formatter.format(size / giga)} | ||
size >= mega -> {formatter.format(size / mega)} | ||
else -> {formatter.format(size / kilo)} | ||
} | ||
} | ||
|
||
fun getRootDirectory(): List<DirectoryInfo> { | ||
val rootDirectory = Environment.getExternalStorageDirectory() | ||
return listFilesInDirectory(rootDirectory) | ||
} | ||
|
||
|
||
// Function to retrieve files and folders in a given directory | ||
fun listFilesInDirectory(directory: File): List<DirectoryInfo> { | ||
val fileList = mutableListOf<DirectoryInfo>() | ||
|
||
// Get list of files and folders in the directory | ||
val files = directory.listFiles() ?: return emptyList() | ||
|
||
for (file in files) { | ||
fileList.add( | ||
DirectoryInfo( | ||
path = file.absolutePath, | ||
name = file.name, | ||
isDirectory = file.isDirectory, | ||
size = if (file.isFile) formatSize(file.length()) else formatSize(file.totalSpace) // Get size only for files | ||
) | ||
) | ||
} | ||
return fileList | ||
} | ||
|
||
fun getDirectoryStructure(path: String): List<DirectoryInfo> { | ||
val directory = File(path) | ||
if (directory.exists() && directory.isDirectory) { | ||
return listFilesInDirectory(directory) | ||
} | ||
return emptyList() // Return an empty list if the directory doesn't exist | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters