PrivateChatActivity.java
7.99 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
package com.cnlive.im.ui;
import android.os.Bundle;
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.Toast;
import com.cnlive.im.R;
import com.cnlive.im.adapter.ChatListAdapter;
import com.cnlive.libs.util.chat.ChatUtil;
import com.cnlive.libs.util.chat.base.IChat;
import com.cnlive.libs.util.chat.model.CNMessage;
import com.cnlive.libs.util.chat.model.CNUserInfo;
import java.util.ArrayList;
import static com.cnlive.im.R.id.opposite_id_edit;
/**
* @author 陈硕
* @time 2017/3/7 14:36
* @desc ${TODD}
*/
public class PrivateChatActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "PrivateChatActivity";
private ListView chatListView;
private EditText messageContent;
private Button sendMessageBtn;
private ChatListAdapter chatListAdapter;
private ArrayList<CNMessage> messageList = new ArrayList<>();
private CNMessage cnMessage;
private LinearLayout linearLayoutUseID;
private EditText user_id_edit;
private Button userId_btn_top;
private String oppositeID;
private LinearLayout edit_layout;
private EditText my_id_edit;
private String my_id_editID;
private LinearLayout my_liner_userID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_private_chat);
initView();
ChatUtil.setConnectStatusListener(connectStatusListener);
ChatUtil.setOnChatRoomActionListener(chatRoomActionListener);
ChatUtil.setOnReceiveMessageListener(receiveMessageListener);
ChatUtil.connect(connectListener);
}
private void initView() {
chatListView = (ListView) findViewById(R.id.private_list_view);
chatListAdapter = new ChatListAdapter(this);
chatListView.setAdapter(chatListAdapter);
messageContent = (EditText) findViewById(R.id.message_content);
sendMessageBtn = (Button) findViewById(R.id.send_message);
sendMessageBtn.setOnClickListener(this);
linearLayoutUseID = (LinearLayout) findViewById(R.id.opposite_liner_useID);
userId_btn_top = (Button) findViewById(R.id.userId_btn_top);
userId_btn_top.setOnClickListener(this);
edit_layout = (LinearLayout) findViewById(R.id.edit_layout);
my_id_edit = (EditText) findViewById(R.id.my_id_edit);
user_id_edit = (EditText) findViewById(opposite_id_edit);
my_liner_userID = (LinearLayout) findViewById(R.id.my_liner_userID);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_message:
//发送私聊消息,需要传入目标ID与发送的消息内容
if (messageContent.getText().toString().isEmpty()) {
Toast.makeText(this, "不能发送空消息呦~", Toast.LENGTH_SHORT).show();
} else {
ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, messageContent.getText().toString(), sendMessageListener);
messageContent.setText("");
}
break;
case R.id.userId_btn_top:
//发送私聊消息,需要传入目标ID与发送的消息内容
if (user_id_edit.getText().toString().isEmpty()) {
Toast.makeText(this, "对方ID不能为空", Toast.LENGTH_SHORT).show();
} else if (my_id_edit.getText().toString().isEmpty()) {
Toast.makeText(this, "你的ID不能为空", Toast.LENGTH_SHORT).show();
} else {
user_id_edit.getText().toString();
oppositeID = user_id_edit.getText().toString();
my_id_editID = my_id_edit.getText().toString();
linearLayoutUseID.setVisibility(View.INVISIBLE);
edit_layout.setVisibility(View.VISIBLE);
chatListView.setVisibility(View.VISIBLE);
my_liner_userID.setVisibility(View.INVISIBLE);
CNUserInfo cnName = new CNUserInfo(my_id_editID, "cnName", "");
ChatUtil.login(cnName, connectListener);
}
break;
}
}
//连接监听
private IChat.OnConnectListener connectListener = new IChat.OnConnectListener() {
@Override
public void onTokenIncorrect() {
Log.e(TAG, "私聊ConnectListener TokenIncorrect");
}
@Override
public void onSuccess(String s) {
Log.e(TAG, "私聊ConnectListener 连接监听" + s);
}
@Override
public void onError(int i, String s) {
Log.e(TAG, "私聊ConnectListener errorCode : " + i + " : errorMessage : " + s);
}
};
/**
* 发送消息监听
*/
private IChat.OnSendMessageListener sendMessageListener = new IChat.OnSendMessageListener() {
@Override
public void onAttached(CNMessage cnMessage) {
Toast.makeText(PrivateChatActivity.this,"onAttached",Toast.LENGTH_LONG).show();
Log.v(TAG, "私聊SendMessageListener onAttached" + cnMessage.getTargetId() + cnMessage.getUserInfo().getUserName());
}
@Override
public void onSuccess(CNMessage cnMessage) {
Toast.makeText(PrivateChatActivity.this,"onSuccess",Toast.LENGTH_LONG).show();
messageList.add(cnMessage);
chatListAdapter.updateItems(messageList);
chatListAdapter.notifyDataSetChanged();
Log.v(TAG, "私聊SendMessageListener SUCCESSES" + cnMessage.getTargetId() + cnMessage.getUserInfo().getUserName());
}
@Override
public void onError(CNMessage cnMessage, int i, String s) {
Log.v(TAG, "私聊SendMessageListener 失败 " + i + " s" + s);
Toast.makeText(PrivateChatActivity.this,"失败",Toast.LENGTH_LONG).show();
}
};
//连接状态监听
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(CNMessage cnMessage) {
Log.v(TAG, "私聊receiveMessageListener" + " " + cnMessage.getTargetId() + " " + cnMessage.getContent());
messageList.add(cnMessage);
chatListAdapter.updateItems(messageList);
chatListAdapter.notifyDataSetChanged();
}
};
//聊天室操作
private IChat.OnChatRoomActionListener chatRoomActionListener = new IChat.OnChatRoomActionListener() {
@Override
public void onJoining(String s) {
Log.i(TAG, "私聊chatRoomActionListener" + " onJoined" + 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.OnLatestMessageListener latestMessageListener = new IChat.OnLatestMessageListener() {
// @Override
// public void onSuccess(List<CNMessage> list) {
//
// }
//
// @Override
// public void onError(int i, String s) {
//
// }
// };
@Override
protected void onPause() {
super.onPause();
ChatUtil.disconnect(true);
}
}