package com.ahau.againstpestinfo.utils;

import android.content.Context;

import java.io.File;

/**
 * 注释:
 *
 * @author fanchunchun
 * @date 2018/5/6
 */

public class FileCache {

    private File cacheDir;

    public FileCache(Context context){
        /**
         *  如果有SD卡则在SD卡中建一个LazyList的目录存放缓存的图片
         没有SD卡就放在系统的缓存目录中
         */
        //Find the dir to save cached images
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"teaassistant");
        else
            cacheDir=context.getCacheDir();
        if(!cacheDir.exists())
            cacheDir.mkdirs();
    }

    public File getFile(String url){
        //将url的hashCode作为缓存的文件名
        //I identify images by hashcode. Not a perfect solution, good for the demo.
        String filename=String.valueOf(url.hashCode());
        //Another possible solution (thanks to grantland)
        //String filename = URLEncoder.encode(url);
        File f = new File(cacheDir, filename);//如果目录下没有filename文件,则新建一个。

        return f;

    }

    public void clear(){
        File[] files=cacheDir.listFiles();
        if(files==null)
            return;
        for(File f:files)
            f.delete();
    }

}
