CleanUtils.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.cooleshow.base.utils;
  2. import android.app.ActivityManager;
  3. import android.content.Context;
  4. import android.os.Build;
  5. import android.os.Environment;
  6. import java.io.File;
  7. import androidx.annotation.RequiresApi;
  8. /**
  9. * <pre>
  10. * author: Blankj
  11. * blog : http://blankj.com
  12. * time : 2016/09/27
  13. * desc : utils about clean
  14. * </pre>
  15. */
  16. public final class CleanUtils {
  17. private CleanUtils() {
  18. throw new UnsupportedOperationException("u can't instantiate me...");
  19. }
  20. /**
  21. * Clean the internal cache.
  22. * <p>directory: /data/data/package/cache</p>
  23. *
  24. * @return {@code true}: success<br>{@code false}: fail
  25. */
  26. public static boolean cleanInternalCache() {
  27. return UtilsBridge.deleteAllInDir(Utils.getApp().getCacheDir());
  28. }
  29. /**
  30. * Clean the internal files.
  31. * <p>directory: /data/data/package/files</p>
  32. *
  33. * @return {@code true}: success<br>{@code false}: fail
  34. */
  35. public static boolean cleanInternalFiles() {
  36. return UtilsBridge.deleteAllInDir(Utils.getApp().getFilesDir());
  37. }
  38. /**
  39. * Clean the internal databases.
  40. * <p>directory: /data/data/package/databases</p>
  41. *
  42. * @return {@code true}: success<br>{@code false}: fail
  43. */
  44. public static boolean cleanInternalDbs() {
  45. return UtilsBridge.deleteAllInDir(new File(Utils.getApp().getFilesDir().getParent(), "databases"));
  46. }
  47. /**
  48. * Clean the internal database by name.
  49. * <p>directory: /data/data/package/databases/dbName</p>
  50. *
  51. * @param dbName The name of database.
  52. * @return {@code true}: success<br>{@code false}: fail
  53. */
  54. public static boolean cleanInternalDbByName(final String dbName) {
  55. return Utils.getApp().deleteDatabase(dbName);
  56. }
  57. /**
  58. * Clean the internal shared preferences.
  59. * <p>directory: /data/data/package/shared_prefs</p>
  60. *
  61. * @return {@code true}: success<br>{@code false}: fail
  62. */
  63. public static boolean cleanInternalSp() {
  64. return UtilsBridge.deleteAllInDir(new File(Utils.getApp().getFilesDir().getParent(), "shared_prefs"));
  65. }
  66. /**
  67. * Clean the external cache.
  68. * <p>directory: /storage/emulated/0/android/data/package/cache</p>
  69. *
  70. * @return {@code true}: success<br>{@code false}: fail
  71. */
  72. public static boolean cleanExternalCache() {
  73. return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
  74. && UtilsBridge.deleteAllInDir(Utils.getApp().getExternalCacheDir());
  75. }
  76. /**
  77. * Clean the custom directory.
  78. *
  79. * @param dirPath The path of directory.
  80. * @return {@code true}: success<br>{@code false}: fail
  81. */
  82. public static boolean cleanCustomDir(final String dirPath) {
  83. return UtilsBridge.deleteAllInDir(UtilsBridge.getFileByPath(dirPath));
  84. }
  85. @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  86. public static void cleanAppUserData() {
  87. ActivityManager am = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
  88. //noinspection ConstantConditions
  89. am.clearApplicationUserData();
  90. }
  91. }