package w.soulofpluto.stockauditean;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import java.io.File;
import static android.content.Context.ACTIVITY_SERVICE;

 /*
 Create by Ramesh Babu
 */

 public class AppUtils {
    public static void deleteCache(Context context) {
        Log.e("AppUtils ====<","Calling deleteCache");
        try {
            File dir = context.getCacheDir();
            deleteDir(dir);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

     public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            Log.e("AppUtils ====","Calling 1 deleteCache");
            if (children != null) {
                int i = 0;
                while (i < children.length) {
                    boolean success = deleteDir(new File(dir, children[i]));
                    if(!success){
                       return false;
                    }
                    i++;
                }
            }
            return dir.delete();
        } else if (dir != null && dir.isFile()) {
           return dir.delete();
        } else {
           return false;
        }
     }

     public static void clearAppData(Context context) {
         try{
             if(Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) {
                ((ActivityManager)context.getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();
             }else {
                String packageName = context.getApplicationContext().getPackageName();
                Runtime runtime = Runtime.getRuntime();
                runtime.exec("pm clear "+packageName);
             }
         }catch(Exception e) {
           e.printStackTrace();
         }
     }
}
