package com.swiftcleaner.chovey.model.tool;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Debug;
import android.os.StatFs;

import java.io.File;


public class MemoryUtils {

    // 获取手机磁盘总内存
    public static String getTotalDiskMemory(Context context) {
        File path = context.getFilesDir().getAbsoluteFile();
        StatFs statFs = new StatFs(path.getPath());
        long blockSize = statFs.getBlockSizeLong();
        long totalBlocks = statFs.getBlockCountLong();
        long totalMemory = totalBlocks * blockSize;
        return String.format("%.1f", totalMemory / 1024f / 1024f / 1024f) + " GB"; // Convert to GB and format to one decimal place
    }

    // 获取手机磁盘已用内存
    public static String getUsedDiskMemory(Context context) {
        File path = context.getFilesDir().getAbsoluteFile();
        StatFs statFs = new StatFs(path.getPath());
        long blockSize = statFs.getBlockSizeLong();
        long totalBlocks = statFs.getBlockCountLong();
        long availableBlocks = statFs.getAvailableBlocksLong();
        long totalMemory = totalBlocks * blockSize;
        long availableMemory = availableBlocks * blockSize;
        long usedMemory = totalMemory - availableMemory; // Calculate used memory
        return String.format("%.1f", usedMemory / 1024f / 1024f / 1024f) + " GB"; // Convert to GB and format to one decimal place
    }
    public static String getUsedDiskMemoryPercent(Context context) {
        File path = context.getFilesDir().getAbsoluteFile();
        StatFs statFs = new StatFs(path.getPath());
        long blockSize = statFs.getBlockSizeLong();
        long totalBlocks = statFs.getBlockCountLong();
        long availableBlocks = statFs.getAvailableBlocksLong();
        long totalMemory = totalBlocks * blockSize;
        long availableMemory = availableBlocks * blockSize;
        long usedMemory = totalMemory - availableMemory; // Calculate used memory

        // Calculate the percentage
        double usedMemoryPercent = ((double) usedMemory / (double) totalMemory) * 100;

        // Format the percentage to an integer
        return String.format("%d", (int) usedMemoryPercent);
    }
}
