Commit 4db3f56b5918563c06e963d4fcc5466d7b181e04
1 parent
841cfbca
更新聊天室SDK
Showing
7 changed files
with
329 additions
and
76 deletions
app/libs/libcnlivechat.jar
No preview for this file type
app/libs/libcnliveutil.jar
No preview for this file type
app/proguard-rules.pro
app/src/main/java/com/cnlive/demo/chat/MainActivity.java
| 1 | 1 | package com.cnlive.demo.chat; |
| 2 | 2 | |
| 3 | 3 | import android.os.Bundle; |
| 4 | -import android.support.annotation.LayoutRes; | |
| 5 | 4 | import android.support.v7.app.AppCompatActivity; |
| 6 | 5 | import android.text.TextUtils; |
| 7 | -import android.util.Log; | |
| 8 | 6 | import android.view.View; |
| 9 | 7 | import android.view.ViewGroup; |
| 10 | 8 | import android.widget.BaseAdapter; |
| ... | ... | @@ -13,17 +11,14 @@ import android.widget.EditText; |
| 13 | 11 | import android.widget.ListView; |
| 14 | 12 | import android.widget.TextView; |
| 15 | 13 | |
| 16 | -import com.cnlive.libs.chat.ChatCallback; | |
| 17 | -import com.cnlive.libs.chat.ChatConnect; | |
| 18 | -import com.cnlive.libs.chat.ChatState; | |
| 14 | +import com.cnlive.demo.chat.util.ChatUtil; | |
| 19 | 15 | |
| 20 | 16 | import java.util.ArrayList; |
| 21 | -import java.util.Date; | |
| 22 | 17 | import java.util.List; |
| 23 | 18 | |
| 24 | -public class MainActivity extends AppCompatActivity implements ChatCallback { | |
| 19 | +public class MainActivity extends AppCompatActivity { | |
| 25 | 20 | |
| 26 | - private ChatConnect chatConnect; | |
| 21 | + private ChatUtil mChatUtil; | |
| 27 | 22 | private static final String name = "游客"; |
| 28 | 23 | //测试聊天室 房间号 |
| 29 | 24 | private static final String room = "60_123456"; |
| ... | ... | @@ -31,6 +26,7 @@ public class MainActivity extends AppCompatActivity implements ChatCallback { |
| 31 | 26 | private EditText editText; |
| 32 | 27 | private ListView listView; |
| 33 | 28 | private Button button; |
| 29 | + private TextView chatState; | |
| 34 | 30 | private List<String> list = new ArrayList<>(); |
| 35 | 31 | private BaseAdapter adapter; |
| 36 | 32 | |
| ... | ... | @@ -38,6 +34,7 @@ public class MainActivity extends AppCompatActivity implements ChatCallback { |
| 38 | 34 | editText = (EditText) findViewById(R.id.edit_send); |
| 39 | 35 | listView = (ListView) findViewById(android.R.id.list); |
| 40 | 36 | button = (Button) findViewById(R.id.btn_send); |
| 37 | + chatState = (TextView) findViewById(R.id.chat_state); | |
| 41 | 38 | |
| 42 | 39 | button.setOnClickListener(new View.OnClickListener() { |
| 43 | 40 | @Override |
| ... | ... | @@ -78,86 +75,37 @@ public class MainActivity extends AppCompatActivity implements ChatCallback { |
| 78 | 75 | super.onCreate(savedInstanceState); |
| 79 | 76 | setContentView(R.layout.activity_main); |
| 80 | 77 | initTestView(); |
| 81 | - } | |
| 82 | 78 | |
| 83 | - @Override | |
| 84 | - public void setContentView(@LayoutRes int layoutResID) { | |
| 85 | - super.setContentView(layoutResID); | |
| 86 | - //当前SDK版本号 | |
| 87 | - Log.e("Chat", "Version " + ChatConnect.getVersion()); | |
| 88 | - //创建ChatConnect对象 传入回调接口 | |
| 89 | - chatConnect = new ChatConnect(this); | |
| 90 | - //初始化ChatConnect对象 传入房间号 | |
| 91 | - chatConnect.init(room); | |
| 92 | - //设置最大历史条数 | |
| 93 | - chatConnect.setMaxStanzas(10); | |
| 79 | + mChatUtil = new ChatUtil(this); | |
| 80 | + mChatUtil.setStateView(chatState); | |
| 81 | + mChatUtil.setOnMessageListener(new ChatUtil.OnMessageListener() { | |
| 82 | + @Override | |
| 83 | + public void onMessage(String message) { | |
| 84 | + list.add(message); | |
| 85 | + adapter.notifyDataSetChanged(); | |
| 86 | + listView.setSelection(list.size() - 1); | |
| 87 | + } | |
| 88 | + }); | |
| 89 | + | |
| 90 | + mChatUtil.initChat(room, name); | |
| 91 | + mChatUtil.connectChat(); | |
| 94 | 92 | } |
| 95 | 93 | |
| 94 | + | |
| 96 | 95 | @Override |
| 97 | 96 | protected void onDestroy() { |
| 98 | 97 | super.onDestroy(); |
| 99 | 98 | //页面关闭 销毁ChatConnect对象 |
| 100 | - if (chatConnect != null) chatConnect.destroy(); | |
| 99 | + mChatUtil.destroy(); | |
| 101 | 100 | } |
| 102 | 101 | |
| 103 | 102 | public void sendMessage(String msg) { |
| 104 | 103 | if (TextUtils.isEmpty(msg)) return; |
| 105 | 104 | //判断登录状态 发送消息 |
| 106 | - if (chatConnect != null && chatConnect.isJoin()) { | |
| 105 | + if (mChatUtil.isJoin()) { | |
| 107 | 106 | //发送聊天信息 |
| 108 | - chatConnect.send(msg); | |
| 109 | - } | |
| 110 | - } | |
| 111 | - | |
| 112 | - @Override | |
| 113 | - public void onChatState(int what, String extra) { | |
| 114 | - switch (what) { | |
| 115 | - case ChatState.StateInitDone: //初始化完成 | |
| 116 | - // 连接聊天室 | |
| 117 | - connectChat(); | |
| 118 | - break; | |
| 119 | - case ChatState.ErrorInitAuth: //认证失败 | |
| 120 | - case ChatState.ErrorInitNetwork: //未检测到网络 | |
| 121 | - case ChatState.ErrorInitOther: //初始化错误 | |
| 122 | - //TODO chatConnect.init(roomId); 重新初始化聊天室 | |
| 123 | - break; | |
| 124 | - case ChatState.StateConnect: //与服务器建立连接 | |
| 125 | - break; | |
| 126 | - case ChatState.StateJoin: //聊天室登录成功 | |
| 127 | - //TODO 可以发送消息 | |
| 128 | - break; | |
| 129 | - case ChatState.StateReconnect: //与服务器重新连接 | |
| 130 | - //TODO 自动重连后 调用connectChat();重新连接聊天室 | |
| 131 | - connectChat(); | |
| 132 | - break; | |
| 133 | - case ChatState.ErrorConnect: //连接服务器失败 | |
| 134 | - case ChatState.ErrorChat: //聊天室连接失败 | |
| 135 | - case ChatState.ErrorName: //昵称冲突 | |
| 136 | - //TODO connectChat();重新连接聊天室 | |
| 137 | - break; | |
| 138 | - case ChatState.ErrorConnectCloseOnError: //聊天室异常断开 | |
| 139 | - //TODO | |
| 140 | - break; | |
| 141 | - case ChatState.ErrorSendOther: //消息发送失败 | |
| 142 | - //TODO | |
| 143 | - break; | |
| 144 | - case ChatState.ErrorSendNoConnect: //消息发送失败 没有连接聊天室 | |
| 145 | - //TODO | |
| 146 | - break; | |
| 107 | + mChatUtil.sendMessage(msg); | |
| 147 | 108 | } |
| 148 | - | |
| 149 | - Log.e("Chat", extra); | |
| 150 | 109 | } |
| 151 | 110 | |
| 152 | - private void connectChat() { | |
| 153 | - chatConnect.connect(name + System.currentTimeMillis()); | |
| 154 | - } | |
| 155 | - | |
| 156 | - @Override | |
| 157 | - public void onChatMessage(String body, Date date) { | |
| 158 | - list.add(body); | |
| 159 | - adapter.notifyDataSetChanged(); | |
| 160 | - listView.setSelection(list.size() - 1); | |
| 161 | - Log.e("Chat", " Body " + body + " Date " + date.getTime()); | |
| 162 | - } | |
| 163 | 111 | } | ... | ... |
app/src/main/java/com/cnlive/demo/chat/util/ChatUtil.java
0 → 100644
| 1 | +package com.cnlive.demo.chat.util; | |
| 2 | + | |
| 3 | +import android.content.Context; | |
| 4 | +import android.graphics.Color; | |
| 5 | +import android.os.Handler; | |
| 6 | +import android.text.Spannable; | |
| 7 | +import android.text.SpannableString; | |
| 8 | +import android.text.style.ForegroundColorSpan; | |
| 9 | +import android.text.style.UnderlineSpan; | |
| 10 | +import android.view.View; | |
| 11 | +import android.widget.TextView; | |
| 12 | +import android.widget.Toast; | |
| 13 | + | |
| 14 | +import com.cnlive.libs.chat.Chat; | |
| 15 | +import com.cnlive.libs.chat.base.IChat; | |
| 16 | + | |
| 17 | +import org.jivesoftware.smack.packet.Message; | |
| 18 | + | |
| 19 | +import java.util.ArrayList; | |
| 20 | +import java.util.List; | |
| 21 | + | |
| 22 | +public class ChatUtil implements IChatUtil, IChat.OnInfoListener, IChat.OnErrorListener, IChat.OnMessageListener { | |
| 23 | + | |
| 24 | + public interface OnMessageListener { | |
| 25 | + void onMessage(String message); | |
| 26 | + } | |
| 27 | + | |
| 28 | + private TextView mStateTextView; | |
| 29 | + | |
| 30 | + private Chat mChat; | |
| 31 | + | |
| 32 | + private Handler mHandler; | |
| 33 | + | |
| 34 | + private Context mContext; | |
| 35 | + | |
| 36 | + private OnMessageListener mMessageListener; | |
| 37 | + | |
| 38 | + private List<String> mMessageList; | |
| 39 | + | |
| 40 | + private class UpdateMessageCallback implements Handler.Callback { | |
| 41 | + @Override | |
| 42 | + public boolean handleMessage(android.os.Message msg) { | |
| 43 | + String message = null; | |
| 44 | + if (mMessageList.size() > 0) message = mMessageList.remove(0); | |
| 45 | + if (mMessageListener != null && message != null) | |
| 46 | + mMessageListener.onMessage(message); | |
| 47 | + mHandler.sendEmptyMessageDelayed(0, 500); | |
| 48 | + return true; | |
| 49 | + } | |
| 50 | + } | |
| 51 | + | |
| 52 | + | |
| 53 | + public ChatUtil(Context context) { | |
| 54 | + mContext = context; | |
| 55 | + mChat = new Chat(context); | |
| 56 | + mMessageList = new ArrayList<>(); | |
| 57 | + } | |
| 58 | + | |
| 59 | + @Override | |
| 60 | + public void setStateView(TextView textView) { | |
| 61 | + mStateTextView = textView; | |
| 62 | + if (mStateTextView != null) mStateTextView.setVisibility(View.GONE); | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Override | |
| 66 | + public void showMessage(String msg, View.OnClickListener listener) { | |
| 67 | + if (mStateTextView != null) mStateTextView.setOnClickListener(listener); | |
| 68 | + showMessage(msg); | |
| 69 | + } | |
| 70 | + | |
| 71 | + @Override | |
| 72 | + public void showMessage(SpannableString msg, View.OnClickListener listener) { | |
| 73 | + if (mStateTextView != null) mStateTextView.setOnClickListener(listener); | |
| 74 | + showMessage(msg); | |
| 75 | + } | |
| 76 | + | |
| 77 | + @Override | |
| 78 | + public void showMessage(String msg) { | |
| 79 | + if (mStateTextView == null) return; | |
| 80 | + mStateTextView.setVisibility(View.VISIBLE); | |
| 81 | + mStateTextView.setText(msg); | |
| 82 | + } | |
| 83 | + | |
| 84 | + @Override | |
| 85 | + public void showMessage(SpannableString msg) { | |
| 86 | + if (mStateTextView == null) return; | |
| 87 | + mStateTextView.setVisibility(View.VISIBLE); | |
| 88 | + mStateTextView.setText(msg); | |
| 89 | + } | |
| 90 | + | |
| 91 | + @Override | |
| 92 | + public void hideMessage() { | |
| 93 | + if (mStateTextView == null) return; | |
| 94 | + mStateTextView.setVisibility(View.GONE); | |
| 95 | + } | |
| 96 | + | |
| 97 | + @Override | |
| 98 | + public void initChat(String room, String name) { | |
| 99 | + destroy(); | |
| 100 | + | |
| 101 | + mChat.setMaxStanzas(10); | |
| 102 | + mChat.init(room, name); | |
| 103 | + mChat.setOnMessageListener(this); | |
| 104 | + mChat.setOnInfoListener(this); | |
| 105 | + mChat.setOnErrorListener(this); | |
| 106 | + | |
| 107 | + mHandler = new Handler(new UpdateMessageCallback()); | |
| 108 | + mHandler.sendEmptyMessageDelayed(0, 1_000); | |
| 109 | + | |
| 110 | + mMessageList.clear(); | |
| 111 | + } | |
| 112 | + | |
| 113 | + @Override | |
| 114 | + public void connectChat() { | |
| 115 | + showMessage("正在连接聊天室..."); | |
| 116 | + mChat.connect(); | |
| 117 | + } | |
| 118 | + | |
| 119 | + @Override | |
| 120 | + public boolean isJoin() { | |
| 121 | + return mChat != null && mChat.isJoin(); | |
| 122 | + } | |
| 123 | + | |
| 124 | + @Override | |
| 125 | + public void sendMessage(String message) { | |
| 126 | + mChat.send(message); | |
| 127 | + } | |
| 128 | + | |
| 129 | + public void destroy() { | |
| 130 | + if (mMessageList != null) mMessageList.clear(); | |
| 131 | + if (mChat != null) mChat.destroy(); | |
| 132 | + if (mHandler != null) mHandler.removeCallbacks(null); | |
| 133 | + } | |
| 134 | + | |
| 135 | + @Override | |
| 136 | + public void setOnMessageListener(OnMessageListener listener) { | |
| 137 | + mMessageListener = listener; | |
| 138 | + } | |
| 139 | + | |
| 140 | + @Override | |
| 141 | + public void onError(int what, String s) { | |
| 142 | + switch (what) { | |
| 143 | + case IChat.ErrorInitRoomId://RoomId为空 | |
| 144 | + case IChat.ErrorInitAuth://认证失败 | |
| 145 | + case IChat.ErrorInitNetwork://未检测到网络 | |
| 146 | + case IChat.ErrorInitOther://初始化失败 | |
| 147 | + showMessage("聊天室初始化失败,点击重试", clickErrorConnect);//设置错误信息 | |
| 148 | + break; | |
| 149 | + case IChat.ErrorConnect://连接服务器失败 | |
| 150 | + case IChat.ErrorChat://聊天室连接失败 | |
| 151 | + case IChat.ErrorName://昵称冲突 | |
| 152 | + String message = "聊天室连接失败,点击重试"; | |
| 153 | + SpannableString spannableString = new SpannableString(message); //定义文字样式 | |
| 154 | + spannableString.setSpan(new UnderlineSpan(), message.length() - 2, message.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); | |
| 155 | + spannableString.setSpan(new ForegroundColorSpan(Color.rgb(255, 213, 0)), message.length() - 2, message.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE); | |
| 156 | + showMessage(spannableString, clickErrorConnect);//设置错误信息 | |
| 157 | + break; | |
| 158 | + case IChat.ErrorConnectCloseOnError://聊天室异常断开 | |
| 159 | + showMessage("聊天室已断开,请检查网络"); | |
| 160 | + break; | |
| 161 | + case IChat.ErrorSendNoConnect://消息发送失败 没有连接聊天室 | |
| 162 | + Toast.makeText(mContext, "聊天室未连接", Toast.LENGTH_SHORT).show(); | |
| 163 | + break; | |
| 164 | + case IChat.ErrorSendOther://消息发送失败 | |
| 165 | + Toast.makeText(mContext, "消息发送失败", Toast.LENGTH_SHORT).show(); | |
| 166 | + break; | |
| 167 | + } | |
| 168 | + } | |
| 169 | + | |
| 170 | + @Override | |
| 171 | + public void onInfo(int what, String s) { | |
| 172 | + switch (what) { | |
| 173 | + case IChat.StateInitDone: | |
| 174 | + //初始化完成 | |
| 175 | + break; | |
| 176 | + case IChat.StateConnect: | |
| 177 | + //服务器连接成功 | |
| 178 | + break; | |
| 179 | + case IChat.StateJoin: | |
| 180 | + //登录成功 | |
| 181 | + hideMessage(); | |
| 182 | + break; | |
| 183 | + case IChat.StateReconnect: | |
| 184 | + //重连成功 | |
| 185 | + connectChat(); | |
| 186 | + break; | |
| 187 | + } | |
| 188 | + } | |
| 189 | + | |
| 190 | + | |
| 191 | + @Override | |
| 192 | + public void processMessage(Message message) { | |
| 193 | + String from = message.getFrom().substring(message.getFrom().indexOf("/") + 1); | |
| 194 | + String body = message.getBody(); | |
| 195 | + | |
| 196 | + mMessageList.add(from + " : " + body); | |
| 197 | + } | |
| 198 | + | |
| 199 | + //聊天室连接失败 点击事件 | |
| 200 | + private View.OnClickListener clickErrorConnect = new View.OnClickListener() { | |
| 201 | + @Override | |
| 202 | + public void onClick(View v) { | |
| 203 | + v.setOnClickListener(null); | |
| 204 | + connectChat(); | |
| 205 | + } | |
| 206 | + }; | |
| 207 | +} | ... | ... |
app/src/main/java/com/cnlive/demo/chat/util/IChatUtil.java
0 → 100644
| 1 | +package com.cnlive.demo.chat.util; | |
| 2 | + | |
| 3 | +import android.text.SpannableString; | |
| 4 | +import android.view.View; | |
| 5 | +import android.widget.TextView; | |
| 6 | + | |
| 7 | + | |
| 8 | +public interface IChatUtil { | |
| 9 | + | |
| 10 | + /** | |
| 11 | + * 设置聊天室状态视图 | |
| 12 | + * | |
| 13 | + * @param textView 状态视图 | |
| 14 | + */ | |
| 15 | + void setStateView(TextView textView); | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * 设置聊天状态信息及点击事件并显示 | |
| 19 | + * | |
| 20 | + * @param msg 状态信息 | |
| 21 | + * @param listener 点击事件 | |
| 22 | + */ | |
| 23 | + void showMessage(String msg, View.OnClickListener listener); | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * 设置聊天状态信息及点击事件并显示 | |
| 27 | + * | |
| 28 | + * @param msg 状态信息 | |
| 29 | + * @param listener 点击事件 | |
| 30 | + */ | |
| 31 | + void showMessage(SpannableString msg, View.OnClickListener listener); | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * 设置聊天状态信息并显示 | |
| 35 | + * | |
| 36 | + * @param msg 状态信息 | |
| 37 | + */ | |
| 38 | + void showMessage(String msg); | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * 设置聊天状态信息并显示 | |
| 42 | + * | |
| 43 | + * @param msg 状态信息 | |
| 44 | + */ | |
| 45 | + void showMessage(SpannableString msg); | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * 隐藏状态信息 | |
| 49 | + */ | |
| 50 | + void hideMessage(); | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * 初始化聊天室 | |
| 54 | + * @param roomId 房间号 | |
| 55 | + * @param userName 用户名 | |
| 56 | + */ | |
| 57 | + void initChat(String roomId, String userName); | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * 连接聊天室 | |
| 61 | + */ | |
| 62 | + void connectChat(); | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * 获取聊天室登录状态 | |
| 66 | + * @return 是否登录 | |
| 67 | + */ | |
| 68 | + boolean isJoin(); | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * 发送消息 | |
| 72 | + * @param message 信息 | |
| 73 | + */ | |
| 74 | + void sendMessage(String message); | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * 释放聊天室 | |
| 78 | + */ | |
| 79 | + void destroy(); | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * 设置聊天室消息监听 | |
| 83 | + * @param listener 消息监听 | |
| 84 | + */ | |
| 85 | + void setOnMessageListener(ChatUtil.OnMessageListener listener); | |
| 86 | +} | ... | ... |
app/src/main/res/layout/activity_main.xml
| ... | ... | @@ -9,6 +9,7 @@ |
| 9 | 9 | android:paddingTop="@dimen/activity_vertical_margin" |
| 10 | 10 | tools:context="com.cnlive.demo.chat.MainActivity"> |
| 11 | 11 | |
| 12 | + | |
| 12 | 13 | <ListView |
| 13 | 14 | android:id="@android:id/list" |
| 14 | 15 | android:layout_width="match_parent" |
| ... | ... | @@ -33,4 +34,15 @@ |
| 33 | 34 | android:layout_toStartOf="@+id/btn_send" |
| 34 | 35 | android:layout_alignParentBottom="true" |
| 35 | 36 | android:imeOptions="flagNoExtractUi"/> |
| 37 | + | |
| 38 | + <TextView | |
| 39 | + android:id="@+id/chat_state" | |
| 40 | + android:layout_width="match_parent" | |
| 41 | + android:layout_height="42dp" | |
| 42 | + android:background="#6f2b2b2b" | |
| 43 | + android:gravity="center_vertical" | |
| 44 | + android:paddingLeft="25dp" | |
| 45 | + android:textColor="@android:color/black" | |
| 46 | + android:textSize="14dp" /> | |
| 47 | + | |
| 36 | 48 | </RelativeLayout> | ... | ... |