1717import com .netease .im .uikit .common .util .storage .StorageUtil ;
1818import com .netease .nimlib .sdk .NIMClient ;
1919import com .netease .nimlib .sdk .msg .MsgService ;
20- import com .netease .nimlib .sdk .msg .model .RecentContact ;
2120
2221import java .io .File ;
2322import java .lang .reflect .Method ;
2423import java .util .HashSet ;
25- import java .util .List ;
2624import java .util .Set ;
2725
2826/**
@@ -40,7 +38,15 @@ public class FileCacheUtil {
4038
4139 final static String TAG = "FileCacheUtil" ;
4240
43- public static void getCacheSie () {
41+ interface OnObserverGet {
42+ void onGetCacheSize (String size );
43+ }
44+
45+ interface OnObserverClean {
46+ void onCleanCache (boolean succeeded );
47+ }
48+
49+ public static void getCacheSie (final OnObserverGet observer ) {
4450
4551 new AsyncTask <Void , Void , Void >() {
4652
@@ -70,43 +76,46 @@ public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws R
7076// LogUtil.i(TAG, "externalObbSize" + ":" + FileUtil.formatFileSize(pStats.externalObbSize));
7177 long result = finalAllLength ;
7278 result += pStats .cacheSize ;
73- result += pStats .cacheSize ;
74- LogUtil .i (TAG , "result" + ":" + FileUtil .formatFileSize (result ));
79+ result += pStats .externalCacheSize ;
80+ // LogUtil.i(TAG, "result" + ":" + FileUtil.formatFileSize(result));
81+ if (observer != null ) {
82+ observer .onGetCacheSize (FileUtil .formatFileSize (result ));
83+ }
7584 }
7685 });
7786 return null ;
7887 }
7988 }.executeOnExecutor (AsyncTask .THREAD_POOL_EXECUTOR );
8089 }
8190
82- public static void clearCache ( ) {
91+ public static void cleanCache ( final OnObserverClean observer ) {
8392 new AsyncTask <Void , Void , Void >() {
8493
8594 @ Override
8695 protected Void doInBackground (Void ... params ) {
87- // IMApplication.getImageLoaderKit().clearCache();
96+ IMApplication .getImageLoaderKit ().clearCache ();
8897 Set <String > pathList = getCacheDir ();
8998 for (String s : pathList ) {
90- // deleteDir(new File(s));
91- }
92- List <RecentContact > recentContacts = NIMClient .getService (MsgService .class ).queryRecentContactsBlock ();
93- if (recentContacts != null && !recentContacts .isEmpty ()) {
94- // NIMClient.getService(MsgService.class).clearMsgDatabase(true);
99+ deleteDir (new File (s ));
95100 }
101+ NIMClient .getService (MsgService .class ).clearMsgDatabase (true );
96102 freeStorageAndNotify (new IPackageDataObserver .Stub () {
97103
98104 @ Override
99105 public void onRemoveCompleted (String packageName , boolean succeeded ) throws RemoteException {
100106 LogUtil .i (TAG , "result" + ":" + packageName );
101107 LogUtil .i (TAG , "result" + ":" + succeeded );
108+ if (observer != null ) {
109+ observer .onCleanCache (succeeded );
110+ }
102111 }
103112 });
104113 return null ;
105114 }
106115 }.executeOnExecutor (AsyncTask .THREAD_POOL_EXECUTOR );
107116 }
108117
109- static void deleteDir (File file ) {
118+ private static void deleteDir (File file ) {
110119 if (file == null || !file .exists ()) {
111120 return ;
112121 }
@@ -125,7 +134,7 @@ static void deleteDir(File file) {
125134 }
126135 }
127136
128- static long makeDirSize (File file ) {
137+ private static long makeDirSize (File file ) {
129138
130139 if (file == null || !file .exists ()) {
131140 return 0L ;
@@ -147,7 +156,7 @@ static long makeDirSize(File file) {
147156 return all ;
148157 }
149158
150- static void getCacheSize (IPackageStatsObserver .Stub observer ) {
159+ private static void getCacheSize (IPackageStatsObserver .Stub observer ) {
151160 Context context = IMApplication .getContext ();
152161 String pkg = context .getPackageName ();
153162 PackageManager pm = context .getPackageManager ();
@@ -212,6 +221,4 @@ private static void freeStorageAndNotify(IPackageDataObserver.Stub observer) {
212221 e .printStackTrace ();
213222 }
214223 }
215-
216-
217224}
0 commit comments