ChatRoomActivity.java
13.2 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package com.cnlive.im.ui;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.cnlive.im.R;
import com.cnlive.im.adapter.ChatListAdapter;
import com.cnlive.im.mode.CNMessageModole;
import com.cnlive.im.util.ToStringUtils;
import com.cnlive.libs.util.chat.ChatUtil;
import com.cnlive.libs.util.chat.base.IChat;
import com.cnlive.libs.util.chat.model.CNChatRoomInfo;
import com.cnlive.libs.util.chat.model.CNChatRoomMemberInfo;
import com.cnlive.libs.util.chat.model.CNMessage;
import com.cnlive.libs.util.chat.model.CNUserInfo;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* @author 陈硕
* @time 2017/3/7 14:36
* @desc ${TODD}
*/
public class ChatRoomActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "ChatRoomActivity";
private ArrayList<CNMessageInfo> groupChatOneListMy;
private String roomIdOne = "ChatRoom05";
private EditText editText;
private Button send_messageOneBtn;
private ListView listViewOne;
private ChatListAdapter chatListAdapterOne;
private Button foundChatBtn;
private Handler mHandler;
private RelativeLayout re_chat;
private LinearLayout lin_user;
private EditText user_id, user_name;
private Button exit_chat1;
private Button chat_info_mationBtn;
public static String user_name_string;
int i = 0;
private boolean isUserLogin = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//取消状态栏
setContentView(R.layout.activity_groupchat);
initUI();
//设置监听
mHandler = new Handler();
ChatUtil.setOnConnectStatusListener(connectStatusListener);
ChatUtil.setOnChatRoomActionListener(chatRoomActionListener);
ChatUtil.setOnReceiveMessageListener(receiveMessageListener);
ChatUtil.setKickedOfflineListener(onKickedOfflineListener);
Intent intent = getIntent();
user_name_string = intent.getStringExtra("user_name_string");
String user_id_string = intent.getStringExtra("user_id_string");
if (user_name_string != null)
ChatUtil.login(new CNUserInfo(user_id_string, user_name_string, ""), connectListener);
else ChatUtil.logOut(connectListener);
}
private void initUI() {
chat_info_mationBtn = (Button) findViewById(R.id.chat_info_mation);
chat_info_mationBtn.setOnClickListener(this);
exit_chat1 = (Button) findViewById(R.id.exit_chat1);
user_id = (EditText) findViewById(R.id.user_ID);
user_name = (EditText) findViewById(R.id.user_name);
lin_user = (LinearLayout) findViewById(R.id.lin_user);
re_chat = (RelativeLayout) findViewById(R.id.re_chat);
foundChatBtn = (Button) findViewById(R.id.creatChat);
listViewOne = (ListView) findViewById(R.id.chat_list_viewOne);
editText = (EditText) findViewById(R.id.message_content);
send_messageOneBtn = (Button) findViewById(R.id.send_messageOne);
exit_chat1.setOnClickListener(this);
foundChatBtn.setOnClickListener(this);
send_messageOneBtn.setOnClickListener(this);
groupChatOneListMy = new ArrayList<>();
chatListAdapterOne = new ChatListAdapter(ChatRoomActivity.this);
listViewOne.setAdapter(chatListAdapterOne);
}
/**
* 发送消息监听
*/
private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() {
@Override
public void onAttached(Object o) {
}
@Override
public void onSuccess(Object o) throws JSONException {
final CNMessage cnMessage = (CNMessage) o;
mHandler.post(new Runnable() {
@Override
public void run() {
try {
if (((String) new JSONObject(cnMessage.getContent()).get("type")).equals("2"))
return;
} catch (JSONException e) {
e.printStackTrace();
}
if ((roomIdOne).equals(cnMessage.getTargetId())) {
groupChatOneListMy.add(new CNMessageInfo(cnMessage, true));
chatListAdapterOne.updateItems(groupChatOneListMy);
chatListAdapterOne.notifyDataSetChanged();
}
}
});
}
@Override
public void onError(Object o, int i, String s) {
}
};
//连接状态监听
private IChat.OnConnectStatusListener connectStatusListener = new IChat.OnConnectStatusListener() {
@Override
public void onConnectStatus(int i, String s) {
Log.i(TAG, "连接状态监听" + i + " " + s);
}
};
private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() {
@Override
public void processMessage(final Object o) {
final CNMessage cnMessage = (CNMessage) o;
mHandler.post(new Runnable() {
@Override
public void run() {
try {
JSONObject jsonObject = new JSONObject(cnMessage.getContent().toString());
String type = (String) jsonObject.get("type");
if ("2".equals(type)) {
Toast.makeText(ChatRoomActivity.this, cnMessage.getUserInfo().getUserName() + "加入聊天室", Toast.LENGTH_SHORT).show();
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
{
groupChatOneListMy.add(new CNMessageInfo(cnMessage, false));
chatListAdapterOne.updateItems(groupChatOneListMy);
chatListAdapterOne.notifyDataSetChanged();
}
}
});
}
};
//接收消息监听
/*private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() {
@Override
public void processMessage(final CNMessage cnMessage) {
Log.v(TAG, "receiveMessageListener" + " " + cnMessage.getTargetId() + " " + cnMessage.getContent());
mHandler.post(new Runnable() {
@Override
public void run() {
try {
JSONObject jsonObject = new JSONObject(cnMessage.getContent().toString());
String type = (String) jsonObject.get("type");
if ("2".equals(type)) {
Toast.makeText(ChatRoomActivity.this, cnMessage.getUserInfo().getUserName() + "加入聊天室", Toast.LENGTH_SHORT).show();
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
{
groupChatOneListMy.add(new CNMessageInfo(cnMessage, false));
chatListAdapterOne.updateItems(groupChatOneListMy);
chatListAdapterOne.notifyDataSetChanged();
}
}
});
}
};
*/
//聊天室操作
private IChat.OnChatRoomActionListener chatRoomActionListener = new IChat.OnChatRoomActionListener() {
@Override
public void onJoining(String s) {
Log.i(TAG, "chatRoomActionListener" + " onJoining" + s);
}
@Override
public void onJoined(String s) {
Log.i(TAG, "chatRoomActionListener " + "onJoined" + s);
}
@Override
public void onQuited(String s) {
Log.i(TAG, "chatRoomActionListener " + "onQuited" + s);
}
@Override
public void onError(String s, int i, String s1) {
Log.i(TAG, "chatRoomActionListener " + "onError" + s);
}
};
//被踢下线状态
private IChat.OnKickedOfflineListener onKickedOfflineListener = new IChat.OnKickedOfflineListener() {
@Override
public void disconnected(int what, final String extra) {
Log.e(TAG, what + " : " + extra);
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(ChatRoomActivity.this, extra, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void conByDevId(final int what, final String extra) {
Log.e(TAG, what + " : " + extra);
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(ChatRoomActivity.this, extra, Toast.LENGTH_SHORT).show();
}
});
}
};
// 加入聊天室监听
private IChat.OnOperationListener onOperationListener = new IChat.OnOperationListener() {
@Override
public void onSuccess(int what, final String extra) {
Log.v(TAG, +what + extra);
mHandler.post(new Runnable() {
@Override
public void run() {
if (i == 0) {
String objToJsonString = ToStringUtils.objToJsonString(new CNMessageModole(user_name_string + "加入聊天室", "2"));
ChatUtil.sendMessage(IChat.CHATROOM, roomIdOne, objToJsonString, sendMessageListener);
++i;
}
Toast.makeText(ChatRoomActivity.this, extra, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onError(int what, String extra) {
Log.v(TAG, "加入聊天室" + what + extra);
}
};
@Override
public void onClick(View view) {
switch (view.getId()) {
//向聊天室1发送消息
case R.id.send_messageOne:
//发送聊天室消息
if (editText.getText().toString().isEmpty()) {
Toast.makeText(this, "不能发送空消息呦~", Toast.LENGTH_SHORT).show();
} else {
String objToJsonString = ToStringUtils.objToJsonString(new CNMessageModole(editText.getText().toString(), "1"));
ChatUtil.sendMessage(IChat.CHATROOM, roomIdOne, objToJsonString, sendMessageListener);
editText.setText("");
}
break;
case R.id.creatChat:
ChatUtil.joinChatRoom(roomIdOne, -1, onOperationListener);
break;
case R.id.exit_chat1:
ChatUtil.quitChatRoom(roomIdOne, onOperationListener);
break;
case R.id.chat_info_mation:
ChatUtil.getChatRoomInfo(roomIdOne, 30, CNChatRoomInfo.ChatRoomMemberOrder.RC_CHAT_ROOM_MEMBER_ASC, new IChat.CNResultCallback<CNChatRoomInfo>() {
@Override
public void onSuccess(CNChatRoomInfo cnChatRoomInfo) {
List<CNChatRoomMemberInfo> memberInfos = cnChatRoomInfo.getMemberInfo();
for (CNChatRoomMemberInfo memberInfo : memberInfos) {
String userId = memberInfo.getUserId();
Log.v("userId", userId);
}
}
@Override
public void onError(int i, String s) {
Log.v("userId", i + "" + s);
}
});
break;
}
}
private IChat.OnConnectListener connectListener = new IChat.OnConnectListener() {
@Override
public void onTokenIncorrect(String s) {
Log.e(TAG, "ConnectListener TokenIncorrect");
}
@Override
public void onSuccess(final String s) {
Log.e(TAG, "ConnectListener 连接监听" + s);
}
@Override
public void onError(int i, final String s) {
Log.e(TAG, "ConnectListener errorCode : " + i + " : errorMessage : " + s);
}
};
public static class CNMessageInfo {
CNMessage cnMessage;
boolean sendMesageInfo;
public CNMessage getCnMessage() {
return cnMessage;
}
public void setCnMessage(CNMessage cnMessage) {
this.cnMessage = cnMessage;
}
public boolean isSendMesageInfo() {
return sendMesageInfo;
}
public void setSendMesageInfo(boolean sendMesageInfo) {
this.sendMesageInfo = sendMesageInfo;
}
public CNMessageInfo(CNMessage cnMessage, boolean sendMesageInfo) {
this.cnMessage = cnMessage;
this.sendMesageInfo = sendMesageInfo;
}
}
}