MessageManager.java
4.81 KB
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
package com.shunzhi.parent.manager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.Observer;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.StatusCode;
import com.netease.nimlib.sdk.auth.AuthService;
import com.netease.nimlib.sdk.auth.AuthServiceObserver;
import com.netease.nimlib.sdk.auth.LoginInfo;
import com.netease.nimlib.sdk.msg.MsgServiceObserve;
import com.netease.nimlib.sdk.msg.model.CustomNotification;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.share.mvpsdk.utils.ToastUtils;
import com.shunzhi.parent.AppContext;
import com.shunzhi.parent.bean.NIMLoginResultBean;
import com.shunzhi.parent.ui.activity.LoginAndRegistActivity;
import java.util.List;
import io.reactivex.Observable;
import io.reactivex.subjects.PublishSubject;
import timber.log.Timber;
/**
* Created by 10501 on 2018/3/17.
*/
public class MessageManager {
private static class InstanceHolder {
static final MessageManager instance = new MessageManager();
}
public static MessageManager getInstance() {
return InstanceHolder.instance;
}
private Observer<List<IMMessage>> messageObserver = new Observer<List<IMMessage>>() {
@Override
public void onEvent(List<IMMessage> imMessages) {
onMessageReceive(imMessages);
}
};
private Observer<CustomNotification> customNotificationObserver = new Observer<CustomNotification>() {
@Override
public void onEvent(CustomNotification customNotification) {
onCustomNotificationReceive(customNotification);
}
};
private MessageManager() {
NIMClient.getService(MsgServiceObserve.class).observeReceiveMessage(messageObserver, true);
NIMClient.getService(MsgServiceObserve.class).observeCustomNotification(customNotificationObserver, true);
NIMClient.getService(AuthServiceObserver.class).observeOnlineStatus(new Observer<StatusCode>() {
@Override
public void onEvent(StatusCode statusCode) {
if (statusCode == StatusCode.KICKOUT) {
Context context = AppContext.getInstance();
Intent i = new Intent(context, LoginAndRegistActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}, true);
}
private void onMessageReceive(List<IMMessage> messageList) {
}
private void onCustomNotificationReceive(CustomNotification customnotification) {
}
public Observable<NIMLoginResultBean> login(String account, String password) {
final PublishSubject<NIMLoginResultBean> subject = PublishSubject.create();
SharedPreferences sp = AppContext.getInstance().getApplicationContext().getSharedPreferences("info", Context.MODE_PRIVATE);
sp.edit().putString("account", account).apply();
sp.edit().putString("token", password).apply();
NIMClient.getService(AuthService.class)
.login(new LoginInfo(account, password))
.setCallback(new RequestCallback() {
@Override
public void onSuccess(Object o) {
NIMLoginResultBean bean = new NIMLoginResultBean(true, 200, null);
subject.onNext(bean);
Timber.i("----===onSuccess : %s",o);
}
@Override
public void onFailed(int i) {
Timber.i("----===nim login failed : %s", i);
ToastUtils.showToast("云信服务器登录失败:" + i);
NIMLoginResultBean bean = new NIMLoginResultBean(false, i, null);
subject.onNext(bean);
}
@Override
public void onException(Throwable throwable) {
Timber.i("----===onException");
if (throwable != null) throwable.printStackTrace();
NIMLoginResultBean bean = new NIMLoginResultBean(false, 0, throwable);
subject.onNext(bean);
}
});
return subject;
}
public void logout() {
NIMClient.getService(MsgServiceObserve.class).observeReceiveMessage(messageObserver, false);
NIMClient.getService(MsgServiceObserve.class).observeCustomNotification(customNotificationObserver, false);
NIMClient.getService(AuthService.class).logout();
}
public interface NIMLoginCallback {
void onResult(boolean isSuccess, int resultCode, Throwable throwable);
}
}