Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.os.StatFs;

import java.io.File;

public class Main {
    /**
     * Cache size limit constants.
     */
    private static final int MIN_DISK_CACHE_SIZE = 5 * 1024 * 1024;
    private static final int MAX_DISK_CACHE_SIZE = 50 * 1024 * 1024;
    private static final int MAX_DISK_CACHE_AS_PERCENT = 2;

    /**
     * Returns the amount of storage currently available in the cache.
     *
     * @param dir The cache directory path name.
     * @return The amount of storage available in bytes.
     */
    public static long calculateDiskCacheSize(File dir) {
        long size = MIN_DISK_CACHE_SIZE;

        try {
            StatFs statFs = new StatFs(dir.getAbsolutePath());
            long available = statFs.getBlockCountLong() * statFs.getBlockSizeLong();
            // Target 2% of the total space.
            size = available * MAX_DISK_CACHE_AS_PERCENT / 100;
        } catch (IllegalArgumentException ignored) {
        }

        // Bound inside min/max size for disk cache.
        return Math.max(Math.min(size, MAX_DISK_CACHE_SIZE), MIN_DISK_CACHE_SIZE);
    }
}