1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
public class FileDataManager {
private static final String TAG = FileDataManager.class.getName();
public static long getCacheDirSize(Context context) { try { return getFolderSize(context.getExternalCacheDir()) + getFolderSize(context.getCacheDir()); } catch (Exception e) { return 0L; } }
public static void cleanAllAppData(Context context, String... filepath) { cleanInternalCache(context); cleanExternalCache(context); cleanDatabases(context); cleanSharedPreference(context); cleanFiles(context); if (filepath == null || filepath.length <= 0) return; for (String filePath : filepath) { cleanCustomCache(filePath); } }
public static void cleanAppCacheData(Context context, String... filepath) { cleanInternalCache(context); cleanExternalCache(context); if (filepath != null && filepath.length > 0) { for (String filePath : filepath) { cleanCustomCache(filePath); } } EventBus.getDefault().post(AppConstants.EVENT_CLEAN_CACHE); }
public static void cleanInternalCache(Context context) { deleteFolderFile(context.getCacheDir(), false); }
public static void cleanDatabases(Context context) { deleteFolderFile(new File("/data/data/" + context.getPackageName() + "/databases"),false);
}
public static void cleanSharedPreference(Context context) { deleteFolderFile(new File("/data/data/" + context.getPackageName() + "/shared_prefs"),false); }
public static void cleanDatabaseByName(Context context, String dbName) { context.deleteDatabase(dbName); }
public static void cleanFiles(Context context) { deleteFolderFile(context.getFilesDir(), false); }
public static void cleanExternalCache(Context context) { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { deleteFolderFile(context.getExternalCacheDir(), false); } }
public static void cleanCustomCache(String filePath) { deleteFolderFile(filePath, false); }
public static long getFolderSize(String filePath) { if (StringUtils.isEmpty(filePath)) { return 0L; } return getFolderSize(new File(filePath)); }
public static long getFolderSize(File file) { long size = 0; try { File[] fileList = file.listFiles(); for (int i = 0; i < fileList.length; i++) { if (fileList[i].isDirectory()) { size = size + getFolderSize(fileList[i]); } else { size = size + fileList[i].length(); } } } catch (Exception e) { e.printStackTrace(); } LogUtils.d(TAG, "getFolderSize() called with: file = [" + file + "]--size = [" + size + "]"); return size; }
public static void deleteFolderFile(File file, boolean deleteThisPath) { if (file!=null) { try { if (file.isDirectory()) { File files[] = file.listFiles(); for (int i = 0; i < files.length; i++) { deleteFolderFile(files[i].getAbsolutePath(), true); } } if (deleteThisPath) { if (!file.isDirectory()) { file.delete(); } else { if (file.listFiles().length == 0) { file.delete(); } } } LogUtils.d(TAG, "deleteFolderFile() called with: file = [" + file + "], deleteThisPath = [" + deleteThisPath + "]"); } catch (Exception e) { e.printStackTrace(); } } }
public static void deleteFolderFile(String filePath, boolean deleteThisPath) { if (!TextUtils.isEmpty(filePath)) { deleteFolderFile(new File(filePath),deleteThisPath); } }
public static String getFormatSize(double size) { double kiloByte = size / 1024; if (kiloByte < 1) { return size + " Byte"; }
double megaByte = kiloByte / 1024; if (megaByte < 1) { BigDecimal result1 = new BigDecimal(Double.toString(kiloByte)); return result1.setScale(2, BigDecimal.ROUND_HALF_UP) .toPlainString() + "KB"; }
double gigaByte = megaByte / 1024; if (gigaByte < 1) { BigDecimal result2 = new BigDecimal(Double.toString(megaByte)); return result2.setScale(2, BigDecimal.ROUND_HALF_UP) .toPlainString() + "MB"; }
double teraBytes = gigaByte / 1024; if (teraBytes < 1) { BigDecimal result3 = new BigDecimal(Double.toString(gigaByte)); return result3.setScale(2, BigDecimal.ROUND_HALF_UP) .toPlainString() + "GB"; } BigDecimal result4 = new BigDecimal(teraBytes); return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB"; } }
|