/* * File name: DataCleanManager.java * Description: The main functions are to clear internal/external cache, clear database, clear sharedPreference, clear files and clear custom directory*/ import java.io.File; import android.content.Context; import android.os.Environment; /** * This application data clear manager */ public class DataCleanManager { /** * Clear the internal cache of this application (/data/data/com.xxx.xxx/cache) * * @param context */ public static void cleanInternalCache(Context context) { deleteFilesByDirectory(context.getCacheDir()); } /** * Clear all databases of this application (/data/data/com.xxx.xxx/databases) * * @param context */ public static void cleanDatabases(Context context) { deleteFilesByDirectory(new File("/data/data/" + context.getPackageName() + "/databases")); } /** * * Clear this app's SharedPreference(/data/data/com.xxx.xxx/shared_prefs) * * @param * context */ public static void cleanSharedPreference(Context context) { deleteFilesByDirectory(new File("/data/data/" + context.getPackageName() + "/shared_prefs")); } /** * Clear this application database by name * * @param context * @param dbName */ public static void cleanDatabaseByName(Context context, String dbName) { context.deleteDatabase(dbName); } /** * Clear the content under /data/data/com.xxx.xxx/files * * @param context */ public static void cleanFiles(Context context) { deleteFilesByDirectory(context.getFilesDir()); } /** * * Clear the contents of the external cache (/mnt/sdcard/android/data/com.xxx.xxx/cache) * * @param * context */ public static void cleanExternalCache(Context context) { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { deleteFilesByDirectory(context.getExternalCacheDir()); } } /** * Clear the files in the custom path, please be careful not to delete them by mistake. And only supports file deletion in the directory * * @param filePath */ public static void cleanCustomCache(String filePath) { deleteFilesByDirectory(new File(filePath)); } /** * Clear all data of this application * * @param context * @param filepath */ public static void cleanApplicationData(Context context, String... filepath) { cleanInternalCache(context); cleanExternalCache(context); cleanDatabases(context); cleanSharedPreference(context); cleanFiles(context); for (String filePath : filepath) { cleanCustomCache(filePath); } } /** * The deletion method will only delete files under a certain folder. If the incoming directory is a file, it will not be processed. * * @param directory */ private static void deleteFilesByDirectory(File directory) { if (directory != null && directory.exists() && directory.isDirectory()) { for (File item : directory.listFiles()) { item.delete(); } } } }