Commit 8b6a7deb5a3ace16932c4a16ece6561d4d37e20b

Authored by 陈硕
1 parent 58f7b266

修改有人加入聊天室

app/src/main/java/com/cnlive/im/ui/ChatRoomActivity.java
@@ -52,7 +52,8 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -52,7 +52,8 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
52 private Button exit_chat1; 52 private Button exit_chat1;
53 private Button chat_info_mationBtn; 53 private Button chat_info_mationBtn;
54 public static String user_name_string; 54 public static String user_name_string;
55 - 55 + int i=0;
  56 + private boolean isUserLogin=false;
56 57
57 @Override 58 @Override
58 protected void onCreate(Bundle savedInstanceState) { 59 protected void onCreate(Bundle savedInstanceState) {
@@ -71,8 +72,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -71,8 +72,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
71 String user_id_string = intent.getStringExtra("user_id_string"); 72 String user_id_string = intent.getStringExtra("user_id_string");
72 if (user_name_string != null) { 73 if (user_name_string != null) {
73 ChatUtil.login(new CNUserInfo(user_id_string, user_name_string, ""), connectListener); 74 ChatUtil.login(new CNUserInfo(user_id_string, user_name_string, ""), connectListener);
74 - } else {  
75 - ChatUtil.logOut(connectListener);  
76 } 75 }
77 76
78 } 77 }
@@ -106,11 +105,8 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -106,11 +105,8 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
106 105
107 @Override 106 @Override
108 public void onAttached(CNMessage cnMessage) { 107 public void onAttached(CNMessage cnMessage) {
109 -  
110 Log.v(TAG, "SendMessageListener onAttached" + cnMessage.getTargetId() + cnMessage.getUserInfo().getUserName()); 108 Log.v(TAG, "SendMessageListener onAttached" + cnMessage.getTargetId() + cnMessage.getUserInfo().getUserName());
111 -  
112 } 109 }
113 -  
114 @Override 110 @Override
115 public void onSuccess(final CNMessage cnMessage) { 111 public void onSuccess(final CNMessage cnMessage) {
116 mHandler.post(new Runnable() { 112 mHandler.post(new Runnable() {
@@ -132,7 +128,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -132,7 +128,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
132 }); 128 });
133 129
134 } 130 }
135 -  
136 @Override 131 @Override
137 public void onError(CNMessage cnMessage, int i, final String s) { 132 public void onError(CNMessage cnMessage, int i, final String s) {
138 Log.v(TAG, "SendMessageListener 失败 " + i + " s" + s); 133 Log.v(TAG, "SendMessageListener 失败 " + i + " s" + s);
@@ -150,8 +145,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -150,8 +145,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
150 @Override 145 @Override
151 public void onConnectStatus(int i, String s) { 146 public void onConnectStatus(int i, String s) {
152 Log.i(TAG, "连接状态监听" + i + " " + s); 147 Log.i(TAG, "连接状态监听" + i + " " + s);
153 -  
154 -  
155 } 148 }
156 }; 149 };
157 150
@@ -180,7 +173,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -180,7 +173,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
180 } 173 }
181 } 174 }
182 }); 175 });
183 -  
184 } 176 }
185 }; 177 };
186 178
@@ -190,21 +182,15 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -190,21 +182,15 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
190 @Override 182 @Override
191 public void onJoining(String s) { 183 public void onJoining(String s) {
192 Log.i(TAG, "chatRoomActionListener" + " onJoining" + s); 184 Log.i(TAG, "chatRoomActionListener" + " onJoining" + s);
193 -  
194 } 185 }
195 -  
196 @Override 186 @Override
197 public void onJoined(String s) { 187 public void onJoined(String s) {
198 Log.i(TAG, "chatRoomActionListener " + "onJoined" + s); 188 Log.i(TAG, "chatRoomActionListener " + "onJoined" + s);
199 -  
200 -  
201 } 189 }
202 -  
203 @Override 190 @Override
204 public void onQuited(String s) { 191 public void onQuited(String s) {
205 Log.i(TAG, "chatRoomActionListener " + "onQuited" + s); 192 Log.i(TAG, "chatRoomActionListener " + "onQuited" + s);
206 } 193 }
207 -  
208 @Override 194 @Override
209 public void onError(String s, int i, String s1) { 195 public void onError(String s, int i, String s1) {
210 Log.i(TAG, "chatRoomActionListener " + "onError" + s); 196 Log.i(TAG, "chatRoomActionListener " + "onError" + s);
@@ -221,11 +207,9 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -221,11 +207,9 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
221 @Override 207 @Override
222 public void run() { 208 public void run() {
223 Toast.makeText(ChatRoomActivity.this, extra, Toast.LENGTH_SHORT).show(); 209 Toast.makeText(ChatRoomActivity.this, extra, Toast.LENGTH_SHORT).show();
224 -  
225 } 210 }
226 }); 211 });
227 } 212 }
228 -  
229 @Override 213 @Override
230 public void conByDevId(final int what, final String extra) { 214 public void conByDevId(final int what, final String extra) {
231 Log.e(TAG, what + " : " + extra); 215 Log.e(TAG, what + " : " + extra);
@@ -277,9 +261,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -277,9 +261,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
277 Toast.makeText(this, "不能发送空消息呦~", Toast.LENGTH_SHORT).show(); 261 Toast.makeText(this, "不能发送空消息呦~", Toast.LENGTH_SHORT).show();
278 } else { 262 } else {
279 String objToJsonString = ToStringUtils.objToJsonString(new CNMessageModole(editText.getText().toString(), "1")); 263 String objToJsonString = ToStringUtils.objToJsonString(new CNMessageModole(editText.getText().toString(), "1"));
280 - Log.v("objToJsonString", objToJsonString);  
281 -  
282 -  
283 ChatUtil.sendMessage(IChat.CHATROOM, roomIdOne, objToJsonString, sendMessageListener); 264 ChatUtil.sendMessage(IChat.CHATROOM, roomIdOne, objToJsonString, sendMessageListener);
284 editText.setText(""); 265 editText.setText("");
285 } 266 }
@@ -311,7 +292,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL @@ -311,7 +292,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
311 292
312 } 293 }
313 } 294 }
314 -int i=0; 295 +
315 296
316 private IChat.OnConnectListener connectListener = new IChat.OnConnectListener() { 297 private IChat.OnConnectListener connectListener = new IChat.OnConnectListener() {
317 @Override 298 @Override
@@ -337,6 +318,11 @@ int i=0; @@ -337,6 +318,11 @@ int i=0;
337 }; 318 };
338 319
339 320
  321 +
  322 +
  323 +
  324 +
  325 +
340 public static class CNMessageInfo { 326 public static class CNMessageInfo {
341 CNMessage cnMessage; 327 CNMessage cnMessage;
342 boolean sendMesageInfo; 328 boolean sendMesageInfo;
app/src/main/java/com/cnlive/im/ui/LoGinActivity.java
@@ -46,7 +46,7 @@ public class LoGinActivity extends AppCompatActivity implements View.OnClickList @@ -46,7 +46,7 @@ public class LoGinActivity extends AppCompatActivity implements View.OnClickList
46 break; 46 break;
47 case R.id.user_logOut: 47 case R.id.user_logOut:
48 ChatUtil.disconnect(false); 48 ChatUtil.disconnect(false);
49 - finish(); 49 + LoGinActivity.this.finish();
50 break; 50 break;
51 case R.id.user_login: 51 case R.id.user_login:
52 Intent intent2 = new Intent(LoGinActivity.this, UserLoginActivity.class); 52 Intent intent2 = new Intent(LoGinActivity.this, UserLoginActivity.class);
app/src/main/java/com/cnlive/im/ui/PrivateChatActivity.java
@@ -14,11 +14,16 @@ import android.widget.Toast; @@ -14,11 +14,16 @@ import android.widget.Toast;
14 14
15 import com.cnlive.im.R; 15 import com.cnlive.im.R;
16 import com.cnlive.im.adapter.ChatListAdapter; 16 import com.cnlive.im.adapter.ChatListAdapter;
  17 +import com.cnlive.im.mode.CNMessageModole;
  18 +import com.cnlive.im.util.ToStringUtils;
17 import com.cnlive.libs.util.chat.ChatUtil; 19 import com.cnlive.libs.util.chat.ChatUtil;
18 import com.cnlive.libs.util.chat.base.IChat; 20 import com.cnlive.libs.util.chat.base.IChat;
19 import com.cnlive.libs.util.chat.model.CNMessage; 21 import com.cnlive.libs.util.chat.model.CNMessage;
20 import com.cnlive.libs.util.chat.model.CNUserInfo; 22 import com.cnlive.libs.util.chat.model.CNUserInfo;
21 23
  24 +import org.json.JSONException;
  25 +import org.json.JSONObject;
  26 +
22 import java.util.ArrayList; 27 import java.util.ArrayList;
23 28
24 import static com.cnlive.im.R.id.opposite_id_edit; 29 import static com.cnlive.im.R.id.opposite_id_edit;
@@ -68,7 +73,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli @@ -68,7 +73,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
68 private void initView() { 73 private void initView() {
69 opposite_name_edit = (EditText) findViewById(R.id.opposite_name_edit); 74 opposite_name_edit = (EditText) findViewById(R.id.opposite_name_edit);
70 chatListView = (ListView) findViewById(R.id.private_list_view); 75 chatListView = (ListView) findViewById(R.id.private_list_view);
71 - chatListView.setAdapter(chatListAdapter); 76 +
72 messageContent = (EditText) findViewById(R.id.message_content); 77 messageContent = (EditText) findViewById(R.id.message_content);
73 sendMessageBtn = (Button) findViewById(R.id.send_message); 78 sendMessageBtn = (Button) findViewById(R.id.send_message);
74 sendMessageBtn.setOnClickListener(this); 79 sendMessageBtn.setOnClickListener(this);
@@ -88,7 +93,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli @@ -88,7 +93,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
88 vistor_btn = (Button) findViewById(R.id.vistor_Btn); 93 vistor_btn = (Button) findViewById(R.id.vistor_Btn);
89 vistor_btn.setOnClickListener(this); 94 vistor_btn.setOnClickListener(this);
90 chatListAdapter = new ChatListAdapter(this); 95 chatListAdapter = new ChatListAdapter(this);
91 - 96 + chatListView.setAdapter(chatListAdapter);
92 } 97 }
93 98
94 99
@@ -109,7 +114,8 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli @@ -109,7 +114,8 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
109 if (messageContent.getText().toString().isEmpty()) { 114 if (messageContent.getText().toString().isEmpty()) {
110 Toast.makeText(this, "不能发送空消息呦~", Toast.LENGTH_SHORT).show(); 115 Toast.makeText(this, "不能发送空消息呦~", Toast.LENGTH_SHORT).show();
111 } else { 116 } else {
112 - ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, messageContent.getText().toString(), sendMessageListener); 117 + String objToJsonString = ToStringUtils.objToJsonString(new CNMessageModole(messageContent.getText().toString(), "4"));//私聊消息
  118 + ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, objToJsonString, sendMessageListener);
113 messageContent.setText(""); 119 messageContent.setText("");
114 } 120 }
115 121
@@ -137,16 +143,20 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli @@ -137,16 +143,20 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
137 break; 143 break;
138 144
139 case R.id.agree_btn: 145 case R.id.agree_btn:
140 - ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, "同意", sendMessageListener); 146 + String objToJsonString = ToStringUtils.objToJsonString(new CNMessageModole("同意", "3"));
  147 + ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, objToJsonString, sendMessageListener);
141 agree_reject_lin.setVisibility(View.GONE); 148 agree_reject_lin.setVisibility(View.GONE);
142 break; 149 break;
143 150
144 case R.id.reject_btn: 151 case R.id.reject_btn:
145 - ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, "拒绝", sendMessageListener); 152 + String objToJsonStringDis = ToStringUtils.objToJsonString(new CNMessageModole("拒绝", "3"));//私聊
  153 +
  154 + ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, objToJsonStringDis, sendMessageListener);
146 agree_reject_lin.setVisibility(View.GONE); 155 agree_reject_lin.setVisibility(View.GONE);
147 break; 156 break;
148 case R.id.vistor_Btn: 157 case R.id.vistor_Btn:
149 - ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, "你好我们可以进行私聊吗", sendMessageListener); 158 + String objToJsonStringInvt = ToStringUtils.objToJsonString(new CNMessageModole("你好我们可以进行私聊吗", "3"));//私聊
  159 + ChatUtil.sendMessage(IChat.PRIVATE, oppositeID, objToJsonStringInvt, sendMessageListener);
150 160
151 break; 161 break;
152 } 162 }
@@ -177,8 +187,6 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli @@ -177,8 +187,6 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
177 }; 187 };
178 188
179 189
180 -  
181 -  
182 /** 190 /**
183 * 发送消息监听 191 * 发送消息监听
184 */ 192 */
@@ -193,22 +201,32 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli @@ -193,22 +201,32 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
193 @Override 201 @Override
194 public void onSuccess(final CNMessage cnMessage) { 202 public void onSuccess(final CNMessage cnMessage) {
195 203
196 - if(View.VISIBLE==vistor_btn.getVisibility()){ 204 + if (View.VISIBLE == vistor_btn.getVisibility()) {
197 vistor_btn.setVisibility(View.GONE); 205 vistor_btn.setVisibility(View.GONE);
198 } 206 }
199 mHandler.post(new Runnable() { 207 mHandler.post(new Runnable() {
200 @Override 208 @Override
201 public void run() { 209 public void run() {
202 - Toast.makeText(PrivateChatActivity.this, "发送消息成功", Toast.LENGTH_SHORT).show();  
203 - if (messageList.size() == 0 && cnMessage.getContent().equals("你好我们可以进行私聊吗")) {  
204 - sendInfo = 2;  
205 - Toast.makeText(PrivateChatActivity.this, "申请已发送等待对方验证", Toast.LENGTH_SHORT).show();  
206 - } else if (messageList.size() == 0 && cnMessage.getContent().equals("拒绝")) {  
207 - } else if (messageList.size() == 0 && cnMessage.getContent().equals("同意")) {  
208 - } else {  
209 - messageList.add(new ChatRoomActivity.CNMessageInfo(cnMessage,true));  
210 - chatListAdapter.updateItems(messageList);  
211 - chatListAdapter.notifyDataSetChanged(); 210 + JSONObject jsonObject = null;
  211 + try {
  212 + jsonObject = new JSONObject(cnMessage.getContent().toString());
  213 + String type = (String) jsonObject.get("type");
  214 + String message = (String) jsonObject.get("message");
  215 + Toast.makeText(PrivateChatActivity.this, "发送消息成功", Toast.LENGTH_SHORT).show();
  216 + if ("3".equals(type) && message.equals("你好我们可以进行私聊吗")) {
  217 + sendInfo = 2;
  218 + Toast.makeText(PrivateChatActivity.this, "申请已发送等待对方验证", Toast.LENGTH_SHORT).show();
  219 + } else if ("3".equals(type) && message.equals("拒绝")) {
  220 + } else if ("3".equals(type) && message.equals("同意")) {
  221 + } else if("4".equals(type)) {
  222 + messageList.add(new ChatRoomActivity.CNMessageInfo(cnMessage, true));
  223 + chatListAdapter.updateItems(messageList);
  224 + chatListAdapter.notifyDataSetChanged();
  225 + }
  226 +
  227 + } catch (JSONException e) {
  228 +
  229 +
212 } 230 }
213 231
214 } 232 }
@@ -247,25 +265,33 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli @@ -247,25 +265,33 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
247 private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() { 265 private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() {
248 @Override 266 @Override
249 public void processMessage(final CNMessage cnMessage) { 267 public void processMessage(final CNMessage cnMessage) {
250 - Log.v(TAG, "私聊receiveMessageListener" + " " + cnMessage.getTargetId() + " " + cnMessage.getContent());  
251 mHandler.post(new Runnable() { 268 mHandler.post(new Runnable() {
252 @Override 269 @Override
253 public void run() { 270 public void run() {
254 - vistor_btn.setVisibility(View.GONE);  
255 - if (messageList.size() == 0 && cnMessage.getContent().equals("你好我们可以进行私聊吗")) {  
256 - agree_reject_lin.setVisibility(View.VISIBLE);  
257 - } else if (messageList.size() == 0 && cnMessage.getContent().equals("拒绝")) {  
258 - sendInfo = 4;  
259 - Toast.makeText(PrivateChatActivity.this, "对方已经决绝了您", Toast.LENGTH_SHORT).show();  
260 - } else if (messageList.size() == 0 && cnMessage.getContent().equals("同意")) {  
261 - Toast.makeText(PrivateChatActivity.this, "对方已经同意了您", Toast.LENGTH_SHORT).show();  
262 - sendInfo = 5;  
263 - } else {  
264 - messageList.add(new ChatRoomActivity.CNMessageInfo(cnMessage,false));  
265 - chatListAdapter.updateItems(messageList);  
266 - chatListAdapter.notifyDataSetChanged(); 271 +
  272 + JSONObject jsonObject = null;
  273 + try {
  274 + jsonObject = new JSONObject(cnMessage.getContent().toString());
  275 + String type = (String) jsonObject.get("type");
  276 + if ("3".equals(type) && "你好我们可以进行私聊吗".equals(jsonObject.get("message"))) {
  277 + vistor_btn.setVisibility(View.GONE);
  278 + agree_reject_lin.setVisibility(View.VISIBLE);
  279 + } else if ("3".equals(type) && "同意".equals(jsonObject.get("message"))) {
  280 + sendInfo = 1;
  281 + } else if ("3".equals(type) && "拒绝".equals(jsonObject.get("message"))) {
  282 + sendInfo = 4;
  283 + }else if("4".equals(type) ){
  284 + Log.v(TAG,cnMessage.getContent().toString());
  285 + messageList.add(new ChatRoomActivity.CNMessageInfo(cnMessage, false));
  286 + chatListAdapter.updateItems(messageList);
  287 + chatListAdapter.notifyDataSetChanged();
  288 + }
  289 +
  290 + } catch (JSONException e) {
  291 + e.printStackTrace();
267 } 292 }
268 293
  294 +
269 } 295 }
270 }); 296 });
271 297
app/src/main/java/com/cnlive/im/util/ToStringUtils.java
@@ -2,9 +2,6 @@ package com.cnlive.im.util; @@ -2,9 +2,6 @@ package com.cnlive.im.util;
2 2
3 import com.cnlive.im.mode.CNMessageModole; 3 import com.cnlive.im.mode.CNMessageModole;
4 4
5 -import org.json.JSONException;  
6 -import org.json.JSONObject;  
7 -  
8 /** 5 /**
9 * @author 陈硕 6 * @author 陈硕
10 * @time 2017/3/28 10:20 7 * @time 2017/3/28 10:20
@@ -25,47 +22,22 @@ public class ToStringUtils { @@ -25,47 +22,22 @@ public class ToStringUtils {
25 22
26 StringBuilder buff = new StringBuilder(); 23 StringBuilder buff = new StringBuilder();
27 buff.append("{"); 24 buff.append("{");
28 -  
29 -  
30 buff.append("message"); 25 buff.append("message");
31 buff.append(":"); 26 buff.append(":");
32 buff.append("\""); 27 buff.append("\"");
33 buff.append(cnMessageModole.getMessage() == null ? "" : cnMessageModole.getMessage()); 28 buff.append(cnMessageModole.getMessage() == null ? "" : cnMessageModole.getMessage());
34 buff.append("\""); 29 buff.append("\"");
35 buff.append(","); 30 buff.append(",");
36 -  
37 -  
38 buff.append("type"); 31 buff.append("type");
39 buff.append(":"); 32 buff.append(":");
40 buff.append("\""); 33 buff.append("\"");
41 buff.append(cnMessageModole.getType() == null ? "" : cnMessageModole.getType()); 34 buff.append(cnMessageModole.getType() == null ? "" : cnMessageModole.getType());
42 buff.append("\""); 35 buff.append("\"");
43 -  
44 buff.append("}"); 36 buff.append("}");
45 json = buff.toString(); 37 json = buff.toString();
46 -  
47 -  
48 -  
49 -  
50 -  
51 return json; 38 return json;
52 } 39 }
53 40
54 41
55 42
56 -  
57 -  
58 -  
59 - public static CNMessageModole JsonStringToobj(String CNMessagejson) throws JSONException {  
60 -  
61 -  
62 - JSONObject jsonObj = new JSONObject(CNMessagejson);  
63 - return new CNMessageModole(jsonObj.get("message").toString(), jsonObj.get("type").toString());  
64 -  
65 -  
66 -  
67 -  
68 - }  
69 -  
70 -  
71 } 43 }