package com.xhly.corelib.utils

import android.app.usage.StorageStatsManager
import android.content.Context
import android.os.Build
import android.os.storage.StorageManager
import androidx.appcompat.app.AppCompatActivity

class FileSizeUtils {
    companion object{
        fun getTotalSpace(context: Context): Long {
            return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val storageStatsManager =
                    context.getSystemService(AppCompatActivity.STORAGE_STATS_SERVICE) as StorageStatsManager
                storageStatsManager.getTotalBytes(StorageManager.UUID_DEFAULT)
            }else{
                context.filesDir.totalSpace
            }
        }

        fun getFreeSpace(context: Context): Long {
            return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val storageStatsManager =
                    context.getSystemService(AppCompatActivity.STORAGE_STATS_SERVICE) as StorageStatsManager
                storageStatsManager.getFreeBytes(StorageManager.UUID_DEFAULT)
            }else{
                context.filesDir.freeSpace
            }
        }
    }
}