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 52 private Button exit_chat1;
53 53 private Button chat_info_mationBtn;
54 54 public static String user_name_string;
55   -
  55 + int i=0;
  56 + private boolean isUserLogin=false;
56 57  
57 58 @Override
58 59 protected void onCreate(Bundle savedInstanceState) {
... ... @@ -71,8 +72,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
71 72 String user_id_string = intent.getStringExtra("user_id_string");
72 73 if (user_name_string != null) {
73 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 105  
107 106 @Override
108 107 public void onAttached(CNMessage cnMessage) {
109   -
110 108 Log.v(TAG, "SendMessageListener onAttached" + cnMessage.getTargetId() + cnMessage.getUserInfo().getUserName());
111   -
112 109 }
113   -
114 110 @Override
115 111 public void onSuccess(final CNMessage cnMessage) {
116 112 mHandler.post(new Runnable() {
... ... @@ -132,7 +128,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
132 128 });
133 129  
134 130 }
135   -
136 131 @Override
137 132 public void onError(CNMessage cnMessage, int i, final String s) {
138 133 Log.v(TAG, "SendMessageListener 失败 " + i + " s" + s);
... ... @@ -150,8 +145,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
150 145 @Override
151 146 public void onConnectStatus(int i, String s) {
152 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 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 182 @Override
191 183 public void onJoining(String s) {
192 184 Log.i(TAG, "chatRoomActionListener" + " onJoining" + s);
193   -
194 185 }
195   -
196 186 @Override
197 187 public void onJoined(String s) {
198 188 Log.i(TAG, "chatRoomActionListener " + "onJoined" + s);
199   -
200   -
201 189 }
202   -
203 190 @Override
204 191 public void onQuited(String s) {
205 192 Log.i(TAG, "chatRoomActionListener " + "onQuited" + s);
206 193 }
207   -
208 194 @Override
209 195 public void onError(String s, int i, String s1) {
210 196 Log.i(TAG, "chatRoomActionListener " + "onError" + s);
... ... @@ -221,11 +207,9 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
221 207 @Override
222 208 public void run() {
223 209 Toast.makeText(ChatRoomActivity.this, extra, Toast.LENGTH_SHORT).show();
224   -
225 210 }
226 211 });
227 212 }
228   -
229 213 @Override
230 214 public void conByDevId(final int what, final String extra) {
231 215 Log.e(TAG, what + " : " + extra);
... ... @@ -277,9 +261,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
277 261 Toast.makeText(this, "不能发送空消息呦~", Toast.LENGTH_SHORT).show();
278 262 } else {
279 263 String objToJsonString = ToStringUtils.objToJsonString(new CNMessageModole(editText.getText().toString(), "1"));
280   - Log.v("objToJsonString", objToJsonString);
281   -
282   -
283 264 ChatUtil.sendMessage(IChat.CHATROOM, roomIdOne, objToJsonString, sendMessageListener);
284 265 editText.setText("");
285 266 }
... ... @@ -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 297 private IChat.OnConnectListener connectListener = new IChat.OnConnectListener() {
317 298 @Override
... ... @@ -337,6 +318,11 @@ int i=0;
337 318 };
338 319  
339 320  
  321 +
  322 +
  323 +
  324 +
  325 +
340 326 public static class CNMessageInfo {
341 327 CNMessage cnMessage;
342 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 46 break;
47 47 case R.id.user_logOut:
48 48 ChatUtil.disconnect(false);
49   - finish();
  49 + LoGinActivity.this.finish();
50 50 break;
51 51 case R.id.user_login:
52 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 14  
15 15 import com.cnlive.im.R;
16 16 import com.cnlive.im.adapter.ChatListAdapter;
  17 +import com.cnlive.im.mode.CNMessageModole;
  18 +import com.cnlive.im.util.ToStringUtils;
17 19 import com.cnlive.libs.util.chat.ChatUtil;
18 20 import com.cnlive.libs.util.chat.base.IChat;
19 21 import com.cnlive.libs.util.chat.model.CNMessage;
20 22 import com.cnlive.libs.util.chat.model.CNUserInfo;
21 23  
  24 +import org.json.JSONException;
  25 +import org.json.JSONObject;
  26 +
22 27 import java.util.ArrayList;
23 28  
24 29 import static com.cnlive.im.R.id.opposite_id_edit;
... ... @@ -68,7 +73,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
68 73 private void initView() {
69 74 opposite_name_edit = (EditText) findViewById(R.id.opposite_name_edit);
70 75 chatListView = (ListView) findViewById(R.id.private_list_view);
71   - chatListView.setAdapter(chatListAdapter);
  76 +
72 77 messageContent = (EditText) findViewById(R.id.message_content);
73 78 sendMessageBtn = (Button) findViewById(R.id.send_message);
74 79 sendMessageBtn.setOnClickListener(this);
... ... @@ -88,7 +93,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
88 93 vistor_btn = (Button) findViewById(R.id.vistor_Btn);
89 94 vistor_btn.setOnClickListener(this);
90 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 114 if (messageContent.getText().toString().isEmpty()) {
110 115 Toast.makeText(this, "不能发送空消息呦~", Toast.LENGTH_SHORT).show();
111 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 119 messageContent.setText("");
114 120 }
115 121  
... ... @@ -137,16 +143,20 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
137 143 break;
138 144  
139 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 148 agree_reject_lin.setVisibility(View.GONE);
142 149 break;
143 150  
144 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 155 agree_reject_lin.setVisibility(View.GONE);
147 156 break;
148 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 161 break;
152 162 }
... ... @@ -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 201 @Override
194 202 public void onSuccess(final CNMessage cnMessage) {
195 203  
196   - if(View.VISIBLE==vistor_btn.getVisibility()){
  204 + if (View.VISIBLE == vistor_btn.getVisibility()) {
197 205 vistor_btn.setVisibility(View.GONE);
198 206 }
199 207 mHandler.post(new Runnable() {
200 208 @Override
201 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 265 private IChat.OnReceiveMessageListener receiveMessageListener = new IChat.OnReceiveMessageListener() {
248 266 @Override
249 267 public void processMessage(final CNMessage cnMessage) {
250   - Log.v(TAG, "私聊receiveMessageListener" + " " + cnMessage.getTargetId() + " " + cnMessage.getContent());
251 268 mHandler.post(new Runnable() {
252 269 @Override
253 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 2  
3 3 import com.cnlive.im.mode.CNMessageModole;
4 4  
5   -import org.json.JSONException;
6   -import org.json.JSONObject;
7   -
8 5 /**
9 6 * @author 陈硕
10 7 * @time 2017/3/28 10:20
... ... @@ -25,47 +22,22 @@ public class ToStringUtils {
25 22  
26 23 StringBuilder buff = new StringBuilder();
27 24 buff.append("{");
28   -
29   -
30 25 buff.append("message");
31 26 buff.append(":");
32 27 buff.append("\"");
33 28 buff.append(cnMessageModole.getMessage() == null ? "" : cnMessageModole.getMessage());
34 29 buff.append("\"");
35 30 buff.append(",");
36   -
37   -
38 31 buff.append("type");
39 32 buff.append(":");
40 33 buff.append("\"");
41 34 buff.append(cnMessageModole.getType() == null ? "" : cnMessageModole.getType());
42 35 buff.append("\"");
43   -
44 36 buff.append("}");
45 37 json = buff.toString();
46   -
47   -
48   -
49   -
50   -
51 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 }
... ...