package com.akexorcist.sleepingforless.util;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import com.bumptech.glide.Glide;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;

/**
 * Created by Akexorcist on 3/10/2016 AD.
 */
public class StorageUtility {
    private static StorageUtility utility;

    public static StorageUtility getInstance() {
        if (utility == null) {
            utility = new StorageUtility();
        }
        return utility;
    }

    public void copyToDownloadsDirectory(File source, File destination) {
        try {
            InputStream inputStream = new FileInputStream(source);
            OutputStream outputStream = new FileOutputStream(destination);

            byte[] buf = new byte[1024];
            int len;
            while ((len = inputStream.read(buf)) > 0) {
                outputStream.write(buf, 0, len);
            }
            inputStream.close();
            outputStream.close();
        } catch (IOException e) {
        }
    }

    public File getDownloadsDirectory() {
        return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    }

    public void updateImageToMediaScanner(File file) {
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(file));
        Contextor.getContext().sendBroadcast(intent);
    }

    public void clearAllCache() {
        clearCache(Contextor.getContext().getCacheDir());
        clearCache(Contextor.getContext().getExternalCacheDir());
    }

    private void clearCache(File cacheDirectory) {
        try {
            if (cacheDirectory != null && cacheDirectory.isDirectory()) {
                deleteDir(cacheDirectory);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean deleteDir(File directory) {
        if (directory != null && directory.isDirectory()) {
            String[] childrenList = directory.list();
            for (String children : childrenList) {
                boolean success = deleteDir(new File(directory, children));
                if (!success) {
                    return false;
                }
            }
            return directory.delete();
        }
        return false;
    }

    public String getAllCacheSize() {
        return readableFileSize(getCacheSize(Contextor.getContext().getCacheDir()) + getCacheSize(Contextor.getContext().getExternalCacheDir()));
    }

    private long getCacheSize(File cacheDirectory) {
        long length = 0;
        for (File file : cacheDirectory.listFiles()) {
            if (file.isFile())
                length += file.length();
            else
                length += getCacheSize(file);
        }
        return length;
    }

    public String readableFileSize(long size) {
        if (size <= 0) return "0 Bytes";
        final String[] units = new String[]{"Bytes", "kB", "MB", "GB", "TB"};
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }
}

@@ -128,67 +128,8 @@
 ent;
-%0Aimport android.util.Log;%0A%0Aimport com.bumptech.glide.Glide;
 %0A%0Aim
@@ -2047,20 +2047,36 @@
  != null
- && 
+) %7B%0A            if (
 director
@@ -2086,32 +2086,36 @@
 sDirectory()) %7B%0A
+    
             Stri
@@ -2160,24 +2160,28 @@
             
+    
 for (String 
@@ -2223,16 +2223,20 @@
         
+   
+ 
 boolean 
@@ -2299,24 +2299,28 @@
             
+    
 if (!success
@@ -2335,32 +2335,36 @@
                 
+    
 return false;%0A  
@@ -2357,24 +2357,46 @@
 turn false;%0A
+                    %7D%0A
             

Fix delete cache method