Skip to content

Commit a03777a

Browse files
committed
添加android缓存计算,清理AppCacheUtil
添加android缓存计算,清理AppCacheUtil
1 parent 2350b94 commit a03777a

File tree

4 files changed

+77
-20
lines changed

4 files changed

+77
-20
lines changed

android/src/main/java/com/netease/im/FileCacheUtil.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
import com.netease.im.uikit.common.util.storage.StorageUtil;
1818
import com.netease.nimlib.sdk.NIMClient;
1919
import com.netease.nimlib.sdk.msg.MsgService;
20-
import com.netease.nimlib.sdk.msg.model.RecentContact;
2120

2221
import java.io.File;
2322
import java.lang.reflect.Method;
2423
import java.util.HashSet;
25-
import java.util.List;
2624
import 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
}

android/src/main/java/com/netease/im/IMApplication.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public static void init(Context context, Class mainActivityClass, @DrawableRes i
112112
@Override
113113
public void onEvent(CustomNotification customNotification) {
114114
NotificationManager notificationManager = (NotificationManager) IMApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
115-
SessionUtil.receiver(notificationManager,customNotification);
115+
SessionUtil.receiver(notificationManager, customNotification);
116116
}
117117
};
118118

@@ -139,9 +139,10 @@ private static LoginInfo getLoginInfo() {
139139
return LoginService.getInstance().getLoginInfo(context);
140140
}
141141

142-
public static String getSdkStorageRooPath(){
142+
public static String getSdkStorageRooPath() {
143143
return Environment.getExternalStorageDirectory() + "/" + context.getPackageName() + "/nim";
144144
}
145+
145146
private static SDKOptions getOptions(Context context) {
146147
SDKOptions options = new SDKOptions();
147148

@@ -271,6 +272,7 @@ public void onEvent(IMMessage message) {
271272
}
272273
}, true);
273274
}
275+
274276
// 初始化用户信息提供者
275277
private static void initUserInfoProvider(UserInfoProvider userInfoProvider) {
276278

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.netease.im;
2+
3+
import com.facebook.react.bridge.Promise;
4+
import com.facebook.react.bridge.ReactApplicationContext;
5+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
6+
import com.facebook.react.bridge.ReactMethod;
7+
8+
/**
9+
* Created by dowin on 2017/7/13.
10+
*/
11+
12+
public class RNAppCacheUtilModule extends ReactContextBaseJavaModule {
13+
14+
private final static String TAG = "AppCacheUtil";//AppCacheUtil.getCacheSize clearCache
15+
private final static String NAME = "AppCacheUtil";
16+
17+
public RNAppCacheUtilModule(ReactApplicationContext reactContext) {
18+
super(reactContext);
19+
}
20+
21+
@Override
22+
public String getName() {
23+
return NAME;
24+
}
25+
26+
@ReactMethod
27+
public void getCacheSize(final Promise promise) {
28+
FileCacheUtil.getCacheSie(new FileCacheUtil.OnObserverGet() {
29+
@Override
30+
public void onGetCacheSize(String size) {
31+
promise.resolve(size);
32+
}
33+
});
34+
}
35+
36+
@ReactMethod
37+
public void cleanCache(final Promise promise) {
38+
FileCacheUtil.cleanCache(new FileCacheUtil.OnObserverClean() {
39+
40+
@Override
41+
public void onCleanCache(boolean succeeded) {
42+
promise.resolve("" + succeeded);
43+
}
44+
});
45+
}
46+
}

android/src/main/java/com/netease/im/RNNeteaseImPackage.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
public class RNNeteaseImPackage implements ReactPackage {
1414
@Override
1515
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
16-
return Arrays.<NativeModule>asList(new RNNeteaseImModule(reactContext));
16+
return Arrays.<NativeModule>asList(
17+
new RNAppCacheUtilModule(reactContext),
18+
new RNNeteaseImModule(reactContext));
1719
}
1820

1921
@Override

0 commit comments

Comments
 (0)