Skip to content

Commit 9feb7f8

Browse files
guangyaoguangyao
authored andcommitted
2 parents 7a254d2 + 68ad6ed commit 9feb7f8

File tree

7 files changed

+384
-31
lines changed

7 files changed

+384
-31
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/RNNeteaseImModule.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import android.app.Activity;
66
import android.content.Intent;
77
import android.content.pm.PackageManager;
8+
import android.media.AudioManager;
9+
import android.net.Uri;
810
import android.os.Build;
911
import android.os.Handler;
1012
import android.os.Looper;
@@ -50,6 +52,7 @@
5052
import com.netease.nimlib.sdk.RequestCallback;
5153
import com.netease.nimlib.sdk.RequestCallbackWrapper;
5254
import com.netease.nimlib.sdk.ResponseCode;
55+
import com.netease.nimlib.sdk.auth.AuthService;
5356
import com.netease.nimlib.sdk.auth.LoginInfo;
5457
import com.netease.nimlib.sdk.friend.FriendService;
5558
import com.netease.nimlib.sdk.friend.constant.FriendFieldEnum;
@@ -144,7 +147,7 @@ public void login(String contactId, String token, final Promise promise) {
144147
// LogUtil.i(TAG, "contactId:" + contactId);
145148
// LogUtil.i(TAG, "token:" + token);
146149
// LogUtil.i(TAG, "md5:" + MD5.getStringMD5(token));
147-
// NIMClient.getService(AuthService.class).openLocalCache(contactId);
150+
NIMClient.getService(AuthService.class).openLocalCache(contactId);
148151
LoginService.getInstance().login(new LoginInfo(contactId, token), new RequestCallback<LoginInfo>() {
149152
@Override
150153
public void onSuccess(LoginInfo loginInfo) {
@@ -725,7 +728,7 @@ public void onResult(int code, Team team, Throwable throwable) {
725728
});
726729
}
727730

728-
// @ReactMethod
731+
@ReactMethod
729732
public void upload(String file, final Promise promise) {
730733
if (TextUtils.isEmpty(file)) {
731734
return;
@@ -1270,7 +1273,7 @@ public int onResult(int code, IMMessage message) {
12701273
});
12711274
if (result == 0) {
12721275
showTip("请选择消息");
1273-
}else if (result == 1) {
1276+
} else if (result == 1) {
12741277
showTip("该类型消息不支持撤销");
12751278
}
12761279
return 0;
@@ -1646,6 +1649,13 @@ public void play(String audioFile, Promise promise) {
16461649
audioPlayService.play(handler, reactContext, audioFile);
16471650
}
16481651

1652+
@ReactMethod
1653+
public void playLocal(String resourceFile, String type, Promise promise) {
1654+
1655+
Uri uri = Uri.parse(resourceFile);
1656+
audioPlayService.playAudio(handler, reactContext, AudioManager.STREAM_MUSIC, uri.getScheme(), uri.getPath());
1657+
}
1658+
16491659
/**
16501660
* 停止播放录音
16511661
*

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

android/src/main/java/com/netease/im/session/AudioPlayService.java

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.facebook.react.bridge.ReactContext;
1212
import com.netease.im.ReactCache;
1313
import com.netease.im.uikit.common.util.log.LogUtil;
14-
import com.netease.nimlib.sdk.media.player.AudioPlayer;
1514
import com.netease.nimlib.sdk.media.player.OnPlayListener;
1615

1716
import static android.content.Context.SENSOR_SERVICE;
@@ -22,7 +21,10 @@
2221

2322
public class AudioPlayService implements SensorEventListener {
2423

25-
AudioPlayer currentAudioPlayer;
24+
final static String SCHEME_FILE = "file";
25+
final static String SCHEME_RAW = "raw";
26+
final static String SCHEME_ASSETS = "assets";
27+
AudioPlayerM currentAudioPlayer;
2628
private String currentFile;
2729
private int state;
2830
private int currentAudioStreamType = AudioManager.STREAM_VOICE_CALL;
@@ -42,12 +44,18 @@ public static AudioPlayService getInstance() {
4244
}
4345

4446
public void play(Handler handler, ReactContext context, String filePath) {
47+
playAudio(handler, context, -1, SCHEME_FILE, filePath);
48+
}
49+
50+
public void playAudio(Handler handler, ReactContext context, int audioStreamType, String type, String filePath) {
4551

4652
if (TextUtils.isEmpty(filePath)) {
4753
return;
4854
}
4955

50-
register(context, true);
56+
if (audioStreamType == -1) {
57+
register(context, true);
58+
}
5159
if (isPlayingAudio()) {
5260
stopAudio(handler);
5361
if (currentFile != null && currentFile.equals(filePath)) {
@@ -58,12 +66,18 @@ public void play(Handler handler, ReactContext context, String filePath) {
5866
currentFile = filePath;
5967

6068
if (currentAudioPlayer == null) {
61-
currentAudioPlayer = new AudioPlayer(context);
69+
currentAudioPlayer = new AudioPlayerM(context);
6270
}
6371
// ReactCache.emit(ReactCache.observeAudioRecord, ReactCache.createAudioPlay("Volume", context.getCurrentActivity().getVolumeControlStream()));
6472

6573
context.getCurrentActivity().setVolumeControlStream(currentAudioStreamType); // 默认使用听筒播放
66-
currentAudioPlayer.setDataSource(filePath);
74+
if (TextUtils.equals(type, SCHEME_FILE)) {
75+
currentAudioPlayer.setDataSource(filePath);
76+
} else if (TextUtils.equals(type, SCHEME_RAW)) {
77+
currentAudioPlayer.setDataSource(AudioPlayerM.Type.raw, filePath);
78+
} else if (TextUtils.equals(type, SCHEME_ASSETS)) {
79+
currentAudioPlayer.setDataSource(AudioPlayerM.Type.assets, filePath);
80+
}
6781

6882
currentAudioPlayer.setOnPlayListener(new BasePlayerListener(currentAudioPlayer));
6983

@@ -78,17 +92,24 @@ public void stopPlay(Handler handler, ReactContext context) {
7892

7993
private SensorManager sensorManager;
8094
private Sensor sensor;
95+
private boolean hasRegister;
8196

8297
void register(ReactContext context, boolean register) {
98+
99+
if (hasRegister && register) {
100+
return;
101+
}
102+
83103
if (sensorManager == null) {
84104
sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
85105
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
86106
}
87107
if (register) {
88108
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
89-
} else {
109+
} else if (hasRegister && !register) {
90110
sensorManager.unregisterListener(this, sensor);
91111
}
112+
hasRegister = register;
92113
}
93114

94115
@Override
@@ -189,10 +210,10 @@ interface AudioControllerState {
189210

190211
public class BasePlayerListener implements OnPlayListener {
191212

192-
protected AudioPlayer listenerPlayingAudioPlayer;
213+
protected AudioPlayerM listenerPlayingAudioPlayer;
193214
private long position = -1;
194215

195-
public BasePlayerListener(AudioPlayer playingAudioPlayer) {
216+
public BasePlayerListener(AudioPlayerM playingAudioPlayer) {
196217
listenerPlayingAudioPlayer = playingAudioPlayer;
197218
}
198219

0 commit comments

Comments
 (0)