Last active
March 27, 2019 11:27
-
-
Save johngorithm/cf07d7c95069db46404135d77c47337d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import java.io.File; | |
public class CacheManager { | |
/** | |
* Private Constructor to make this a Utility class. | |
*/ | |
private CacheManager() { | |
// Private constructor | |
} | |
/** | |
* Deletes app's cache. | |
* @param context app context | |
*/ | |
public static void deleteCache(Context context) { | |
try { | |
File dir = context.getCacheDir(); | |
deleteDir(dir); | |
} catch (Exception e) { e.printStackTrace();} | |
} | |
/** | |
* Does the actual deletion of our app's cache. | |
* @param dir cache directory | |
* @return boolean value of whether operation was successful or not. | |
*/ | |
private static boolean deleteDir(File dir) { | |
if (dir != null && dir.isDirectory()) { | |
String[] children = dir.list(); | |
for (int i = 0; i < children.length; i++) { | |
boolean success = deleteDir(new File(dir, children[i])); | |
if (!success) { | |
return false; | |
} | |
} | |
return dir.delete(); | |
} else if(dir!= null && dir.isFile()) { | |
return dir.delete(); | |
} else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment