PageRenderTime 5998ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/yueai/src/main/java/com/yueai/adapter/ChatAdapter.java

https://bitbucket.org/juzhiwiscom/datelove
Java | 1025 lines | 726 code | 75 blank | 224 comment | 119 complexity | 8b55b77e26d768c2c37604b58d0e9295 MD5 | raw file
  1. package com.yueai.adapter;
  2. import android.app.Activity;
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.graphics.drawable.AnimationDrawable;
  6. import android.graphics.drawable.Drawable;
  7. import android.net.Uri;
  8. import android.support.v4.app.FragmentActivity;
  9. import android.support.v7.widget.LinearLayoutManager;
  10. import android.support.v7.widget.RecyclerView;
  11. import android.text.TextUtils;
  12. import android.view.View;
  13. import android.widget.ImageView;
  14. import android.widget.LinearLayout;
  15. import android.widget.RelativeLayout;
  16. import com.alibaba.fastjson.JSON;
  17. import com.library.adapter.recyclerview.MultiItemRecyclerViewAdapter;
  18. import com.library.adapter.recyclerview.RecyclerViewHolder;
  19. import com.library.dialog.OnDoubleDialogClickListener;
  20. import com.library.imageloader.CircleTransformation;
  21. import com.library.imageloader.ImageLoader;
  22. import com.library.imageloader.ImageLoaderUtil;
  23. import com.library.utils.DensityUtil;
  24. import com.library.utils.DialogUtil;
  25. import com.library.utils.TimeUtil;
  26. import com.library.utils.ToastUtil;
  27. import com.library.utils.Util;
  28. import com.library.widgets.ChatImageView;
  29. import com.yueai.R;
  30. import com.yueai.activity.BuyServiceActivity;
  31. import com.yueai.activity.PictureBrowseActivity;
  32. import com.yueai.activity.UserInfoDetailActivity;
  33. import com.yueai.bean.BaseModel;
  34. import com.yueai.bean.Image;
  35. import com.yueai.bean.Message;
  36. import com.yueai.bean.QaAnswer;
  37. import com.yueai.bean.QaQuestion;
  38. import com.yueai.bean.WriteMsg;
  39. import com.yueai.constant.IConfigConstant;
  40. import com.yueai.constant.IUrlConstant;
  41. import com.yueai.event.MessageChangedEvent;
  42. import com.yueai.event.RefreshChatEvent;
  43. import com.yueai.utils.FileUtil;
  44. import com.yueai.utils.RecordUtil;
  45. import com.yueai.utils.Utils;
  46. import com.yueai.xml.PlatformInfoXml;
  47. import com.yueai.xml.UserInfoXml;
  48. import com.zhy.http.okhttp.OkHttpUtils;
  49. import com.zhy.http.okhttp.callback.Callback;
  50. import org.greenrobot.eventbus.EventBus;
  51. import java.io.File;
  52. import java.util.ArrayList;
  53. import java.util.List;
  54. import okhttp3.Call;
  55. import okhttp3.Response;
  56. /**
  57. * 聊天信息适配器,多item布局
  58. * Created by zhangdroid on 2016/8/17.
  59. */
  60. public class ChatAdapter extends MultiItemRecyclerViewAdapter<Message> {
  61. // 消息类型常量
  62. /**
  63. * 管理员消息
  64. */
  65. private static final String MSG_TYPE_ADMIN = "3";
  66. /**
  67. * 文字消息
  68. */
  69. private static final String MSG_TYPE_TEXT = "4";
  70. /**
  71. * 语音消息
  72. */
  73. private static final String MSG_TYPE_VOICE = "7";
  74. /**
  75. * QA消息
  76. */
  77. private static final String MSG_TYPE_QA = "8";
  78. /**
  79. * 图片消息
  80. */
  81. private static final String MSG_TYPE_IMAGE = "10";
  82. // item type常量
  83. /**
  84. * 文字聊天(对方)
  85. */
  86. private static final int CHAT_TYPE_MSG_LEFT = -1;
  87. /**
  88. * 文字聊天(我方)
  89. */
  90. private static final int CHAT_TYPE_MSG_RIGHT = 1;
  91. /**
  92. * 语音聊天(对方)
  93. */
  94. private static final int CHAT_TYPE_VOICE_LEFT = -2;
  95. /**
  96. * 语音聊天(我方)
  97. */
  98. private static final int CHAT_TYPE_VOICE_RIGHT = 2;
  99. /**
  100. * QA聊天(对方)
  101. */
  102. private static final int CHAT_TYPE_QA_LEFT = -3;
  103. /**
  104. * 图片消息(对方)
  105. */
  106. private static final int CHAT_TYPE_IMAGE_LEFT = -4;
  107. /**
  108. * 图片消息(我方)
  109. */
  110. private static final int CHAT_TYPE_IMAGE_RIGHT = 4;
  111. // 语音播放动画
  112. private AnimationDrawable mVoiceAnimLeft;
  113. private AnimationDrawable mVoiceAnimRight;
  114. /**
  115. * 对方id
  116. */
  117. private String mUId;
  118. /**
  119. * 对方用户头像
  120. */
  121. private String mAvatarUrl;
  122. /**
  123. * 我方方用户头像
  124. */
  125. private String mMyAvatarUrl;
  126. /**
  127. * 服务器时间戳毫秒数
  128. */
  129. private long mServerSystemTime;
  130. /**
  131. * 是否支付拦截
  132. */
  133. private boolean mIsIntercept;
  134. /**
  135. * 所有图片列表缓存,用来展示大图浏览
  136. */
  137. private List<Image> mAllImageList = new ArrayList<Image>();
  138. /**
  139. * 是否显示图片加载进度
  140. */
  141. private boolean mIsShowImgLoading;
  142. /**
  143. * 是否显示图片重发
  144. */
  145. private boolean mIsShowImgRetry;
  146. /**
  147. * 是否显示语音加载进度
  148. */
  149. private boolean mIsShowVoiceLoading;
  150. /**
  151. * 是否显示语音重发
  152. */
  153. private boolean mIsShowVoiceRetry;
  154. /**
  155. * 设置对方用户id
  156. *
  157. * @param uId
  158. */
  159. public void setUserId(String uId) {
  160. this.mUId = uId;
  161. }
  162. /**
  163. * 设置对方用户头像
  164. *
  165. * @param url
  166. */
  167. public void setAvatarUrl(String url) {
  168. this.mAvatarUrl = url;
  169. }
  170. public void setServerSystemTime(long serverSystemTime) {
  171. this.mServerSystemTime = serverSystemTime;
  172. }
  173. public void isIntercept(boolean isIntercept) {
  174. this.mIsIntercept = isIntercept;
  175. }
  176. public void setAllImageList(List<Image> allImageList) {
  177. this.mAllImageList = allImageList;
  178. }
  179. public void isShowImgLoading(boolean isShowImgLoading) {
  180. this.mIsShowImgLoading = isShowImgLoading;
  181. }
  182. public void isShowImgRetry(boolean isShowImgRetry) {
  183. this.mIsShowImgRetry = isShowImgRetry;
  184. }
  185. public void isShowVoiceLoading(boolean isShowVoiceLoading) {
  186. this.mIsShowVoiceLoading = isShowVoiceLoading;
  187. }
  188. public void isShowVoiceRetry(boolean isShowVoiceRetry) {
  189. this.mIsShowVoiceRetry = isShowVoiceRetry;
  190. }
  191. public ChatAdapter(Context context, int layoutResId) {
  192. this(context, layoutResId, null);
  193. }
  194. public ChatAdapter(Context context, int layoutResId, List<Message> list) {
  195. super(context, layoutResId, list);
  196. this.mContext = context;
  197. // 对方语音动画
  198. List<Drawable> lefList = new ArrayList<Drawable>();
  199. lefList.add(mContext.getResources().getDrawable(R.drawable.msg_voice_left_one));
  200. lefList.add(mContext.getResources().getDrawable(R.drawable.msg_voice_left_two));
  201. lefList.add(mContext.getResources().getDrawable(R.drawable.msg_voice_left_three));
  202. mVoiceAnimLeft = Utils.getFrameAnim(lefList, true, 300);
  203. // 我方语音动画
  204. List<Drawable> rightList = new ArrayList<Drawable>();
  205. rightList.add(mContext.getResources().getDrawable(R.drawable.msg_voice_right_one));
  206. rightList.add(mContext.getResources().getDrawable(R.drawable.msg_voice_right_two));
  207. rightList.add(mContext.getResources().getDrawable(R.drawable.msg_voice_right_three));
  208. mVoiceAnimRight = Utils.getFrameAnim(rightList, true, 300);
  209. }
  210. @Override
  211. protected int getItemViewType(int position, Message message) {
  212. String userId = message.getUid();
  213. if (!TextUtils.isEmpty(userId)) {
  214. String msgType = message.getMsgType();
  215. if (userId.equals(UserInfoXml.getUID())) {// 己方消息
  216. if (!TextUtils.isEmpty(msgType)) {
  217. if (MSG_TYPE_TEXT.equals(msgType)) {// 文本消息
  218. return CHAT_TYPE_MSG_RIGHT;
  219. } else if (MSG_TYPE_VOICE.equals(msgType)) {// 语音消息
  220. return CHAT_TYPE_VOICE_RIGHT;
  221. } else if (MSG_TYPE_IMAGE.equals(msgType)) {// 图片消息
  222. return CHAT_TYPE_IMAGE_RIGHT;
  223. }
  224. }
  225. } else { // 对方消息
  226. if (!TextUtils.isEmpty(msgType)) {
  227. if (MSG_TYPE_ADMIN.equals(msgType)) {// 管理员消息
  228. return CHAT_TYPE_MSG_LEFT;
  229. } else if (MSG_TYPE_TEXT.equals(msgType)) {// 文本消息
  230. return CHAT_TYPE_MSG_LEFT;
  231. } else if (MSG_TYPE_VOICE.equals(msgType)) {// 语音消息
  232. return CHAT_TYPE_VOICE_LEFT;
  233. } else if (MSG_TYPE_QA.equals(msgType)) {// QA消息
  234. return CHAT_TYPE_QA_LEFT;
  235. } else if (MSG_TYPE_IMAGE.equals(msgType)) {// 图片消息
  236. return CHAT_TYPE_IMAGE_LEFT;
  237. }
  238. }
  239. }
  240. }
  241. return 0;
  242. }
  243. @Override
  244. protected int getItemLayoutResId(int viewType) {
  245. int layoutResId = 0;
  246. switch (viewType) {
  247. case CHAT_TYPE_MSG_LEFT:// 对方文字消息
  248. layoutResId = R.layout.item_chat_msg_left;
  249. break;
  250. case CHAT_TYPE_MSG_RIGHT:// 我方文字消息
  251. layoutResId = R.layout.item_chat_msg_right;
  252. break;
  253. case CHAT_TYPE_VOICE_LEFT:// 对方语音消息
  254. layoutResId = R.layout.item_chat_voice_left;
  255. break;
  256. case CHAT_TYPE_VOICE_RIGHT:// 我方语音消息
  257. layoutResId = R.layout.item_chat_voice_right;
  258. break;
  259. case CHAT_TYPE_QA_LEFT:// 对方QA消息
  260. layoutResId = R.layout.item_chat_qa;
  261. break;
  262. case CHAT_TYPE_IMAGE_LEFT:// 对方图片消息
  263. layoutResId = R.layout.item_chat_image_left;
  264. break;
  265. case CHAT_TYPE_IMAGE_RIGHT:// 我方图片消息
  266. layoutResId = R.layout.item_chat_image_right;
  267. break;
  268. }
  269. return layoutResId;
  270. }
  271. @Override
  272. protected void convert(int position, RecyclerViewHolder viewHolder, Message bean) {
  273. if (bean != null) {
  274. String userId = bean.getUid();
  275. if (!TextUtils.isEmpty(userId)) {
  276. String msgType = bean.getMsgType();
  277. if (userId.equals(UserInfoXml.getUID())) {// 己方消息
  278. if (!TextUtils.isEmpty(msgType)) {
  279. switch (msgType) {
  280. case MSG_TYPE_TEXT:// 文本消息
  281. setRightMsg(viewHolder, bean);
  282. break;
  283. case MSG_TYPE_VOICE:// 语音消息
  284. setRightVoiceMsg(viewHolder, bean);
  285. break;
  286. case MSG_TYPE_IMAGE://图片消息
  287. setRightImageMsg(viewHolder, bean);
  288. break;
  289. }
  290. }
  291. } else { // 对方消息
  292. if (!TextUtils.isEmpty(msgType)) {
  293. if (!TextUtils.isEmpty(msgType)) {
  294. switch (msgType) {
  295. case MSG_TYPE_ADMIN:// 管理员消息
  296. setLeftMsg(viewHolder, bean, true);
  297. break;
  298. case MSG_TYPE_TEXT:// 文本消息
  299. setLeftMsg(viewHolder, bean, false);
  300. break;
  301. case MSG_TYPE_VOICE:// 语音消息
  302. setLeftVoiceMsg(viewHolder, bean);
  303. break;
  304. case MSG_TYPE_QA:// QA消息
  305. setQaMesssage(viewHolder, bean);
  306. break;
  307. case MSG_TYPE_IMAGE://图片消息
  308. setLeftImageMsg(viewHolder, bean);
  309. break;
  310. }
  311. }
  312. }
  313. }
  314. }
  315. }
  316. }
  317. /**
  318. * 设置对方文本消息
  319. *
  320. * @param viewHolder
  321. * @param message {Message}对象
  322. * @param isAdmin 是否为管理员消息
  323. */
  324. private void setLeftMsg(RecyclerViewHolder viewHolder, final Message message, boolean isAdmin) {
  325. if (message != null) {
  326. // 消息发送时间
  327. viewHolder.setText(R.id.chat_qa_msg_time_left, TimeUtil.getLocalTime(mContext, mServerSystemTime, message.getCreateDateMills()));
  328. // 头像
  329. ImageView ivAvatar = (ImageView) viewHolder.getView(R.id.chat_qa_msg_avatar_left);
  330. if (isAdmin) {
  331. } else {
  332. ImageLoaderUtil.getInstance().loadImage(mContext, new ImageLoader.Builder()
  333. .url(mAvatarUrl).transform(new CircleTransformation()).imageView(ivAvatar).build());
  334. // 点击头像可查看用户详情
  335. ivAvatar.setOnClickListener(new View.OnClickListener() {
  336. @Override
  337. public void onClick(View v) {
  338. UserInfoDetailActivity.toUserInfoDetailActivity((Activity) mContext, message.getUid(), null, UserInfoDetailActivity.SOURCE_FROM_CHAT_CONTENT);
  339. }
  340. });
  341. }
  342. // 聊天内容
  343. String content = message.getContent();
  344. if (!TextUtils.isEmpty(content)) {
  345. viewHolder.setText(R.id.chat_qa_msg_question, Util.convertText(content));
  346. }
  347. }
  348. }
  349. /**
  350. * 设置己方文本消息
  351. *
  352. * @param viewHolder
  353. * @param message
  354. */
  355. private void setRightMsg(RecyclerViewHolder viewHolder, final Message message) {
  356. if (message != null) {
  357. // 消息发送时间
  358. viewHolder.setText(R.id.chat_msg_time_right, TimeUtil.getLocalTime(mContext, mServerSystemTime, message.getCreateDateMills()));
  359. // 头像
  360. ImageView ivAvatar = (ImageView) viewHolder.getView(R.id.chat_qa_msg_avatar_Right);
  361. ImageLoaderUtil.getInstance().loadImage(mContext, new ImageLoader.Builder()
  362. .url(UserInfoXml.getAvatarUrl()).transform(new CircleTransformation()).imageView(ivAvatar).build());
  363. // // 点击头像可查看用户详情
  364. // ivAvatar.setOnClickListener(new View.OnClickListener() {
  365. // @Override
  366. // public void onClick(View v) {
  367. // UserInfoDetailActivity.toUserInfoDetailActivity((Activity) mContext, message.getUid(), null, UserInfoDetailActivity.SOURCE_FROM_CHAT_CONTENT);
  368. // }
  369. // });
  370. // 聊天内容
  371. String content = message.getContent();
  372. if (!TextUtils.isEmpty(content)) {
  373. viewHolder.setText(R.id.chat_msg_right, Util.convertText(content));
  374. }
  375. }
  376. }
  377. /**
  378. * 设置对方语音消息
  379. *
  380. * @param viewHolder
  381. * @param message
  382. */
  383. private void setLeftVoiceMsg(final RecyclerViewHolder viewHolder, final Message message) {
  384. if (message != null) {
  385. // 消息发送时间
  386. viewHolder.setText(R.id.chat_voice_lasttime_left, TimeUtil.getLocalTime(mContext, mServerSystemTime, message.getCreateDateMills()));
  387. // 头像
  388. ImageView ivAvatar = (ImageView) viewHolder.getView(R.id.chat_voice_avatar_left);
  389. ImageLoaderUtil.getInstance().loadImage(mContext, new ImageLoader.Builder()
  390. .url(mAvatarUrl).transform(new CircleTransformation()).imageView(ivAvatar).build());
  391. // 点击头像可查看用户详情
  392. ivAvatar.setOnClickListener(new View.OnClickListener() {
  393. @Override
  394. public void onClick(View v) {
  395. UserInfoDetailActivity.toUserInfoDetailActivity((Activity) mContext, message.getUid(), "0", UserInfoDetailActivity.SOURCE_FROM_CHAT_CONTENT);
  396. }
  397. });
  398. // 将语音下载到本地(根据用户id存放到不同的目录下)
  399. String downloadFileDir = FileUtil.RECORD_DIRECTORY_PATH + File.separator + message.getUid();
  400. String downloadFileName = FileUtil.createFileNameByDate(message.getCreateDate()) + ".mp3";
  401. final String downloadPath = downloadFileDir + File.separator + downloadFileName;
  402. FileUtil.downloadFile(message.getAudioUrl(), downloadFileDir, downloadFileName);
  403. // 设置语音信息
  404. String audioTime = message.getAudioTime();
  405. if (!TextUtils.isEmpty(audioTime)) {
  406. viewHolder.setText(R.id.chat_voice_left_tv, audioTime + "''");
  407. }
  408. final ImageView ivUnread = (ImageView) viewHolder.getView(R.id.chat_voice_left_unread);
  409. String readState = message.getRecevStatus();
  410. if (!TextUtils.isEmpty(readState) && "0".equals(readState)) {
  411. ivUnread.setVisibility(View.VISIBLE);
  412. } else {
  413. ivUnread.setVisibility(View.GONE);
  414. }
  415. // 点击播放语音
  416. final ImageView ivAnim = (ImageView) viewHolder.getView(R.id.chat_voice_left_iv);
  417. ivAnim.setImageResource(R.drawable.msg_voice_left_three);
  418. viewHolder.getView(R.id.chat_voice_left).setOnClickListener(new View.OnClickListener() {
  419. @Override
  420. public void onClick(View v) {
  421. // 更新状态为已读
  422. String readState = message.getRecevStatus();
  423. if (!TextUtils.isEmpty(readState) && "0".equals(readState)) {
  424. message.setRecevStatus("1");
  425. ivUnread.setVisibility(View.GONE);
  426. updateVoiceMsgStatus(message.getId());
  427. }
  428. // 当前是否正在播放语音
  429. if (RecordUtil.getInstance().isPlaying()) {
  430. // 先停止正在播放的语音
  431. RecordUtil.getInstance().stop();
  432. if (mVoiceAnimLeft.isRunning()) {
  433. mVoiceAnimLeft.stop();
  434. }
  435. if (mVoiceAnimRight.isRunning()) {
  436. mVoiceAnimRight.stop();
  437. }
  438. }
  439. File file = new File(downloadPath);
  440. if (file.exists()) {
  441. // 播放/停止语音动画
  442. if (!mVoiceAnimLeft.isRunning()) {
  443. ivAnim.setImageDrawable(mVoiceAnimLeft);
  444. mVoiceAnimLeft.start();
  445. }
  446. // 播放/暂停语音
  447. RecordUtil.getInstance().toggle(downloadPath, new RecordUtil.OnPlayerListener() {
  448. @Override
  449. public void onCompleted() {
  450. if (mVoiceAnimLeft.isRunning()) {
  451. mVoiceAnimLeft.stop();
  452. ivAnim.setImageResource(R.drawable.msg_voice_left_three);
  453. }
  454. }
  455. @Override
  456. public void onPaused() {
  457. if (mVoiceAnimLeft.isRunning()) {
  458. mVoiceAnimLeft.stop();
  459. ivAnim.setImageResource(R.drawable.msg_voice_left_three);
  460. }
  461. }
  462. });
  463. } else {
  464. ToastUtil.showShortToast(mContext, mContext.getResources().getString(R.string.not_exist));
  465. }
  466. }
  467. });
  468. }
  469. }
  470. /**
  471. * 设置我方语音消息
  472. *
  473. * @param viewHolder
  474. * @param message
  475. */
  476. private void setRightVoiceMsg(RecyclerViewHolder viewHolder, final Message message) {
  477. if (message != null) {
  478. // 消息发送时间
  479. viewHolder.setText(R.id.chat_voice_lasttime_right, TimeUtil.getLocalTime(mContext, mServerSystemTime, message.getCreateDateMills()));
  480. // 头像
  481. ImageView ivAvatar = (ImageView) viewHolder.getView(R.id.chat_voice_avatar_right);
  482. ImageLoaderUtil.getInstance().loadImage(mContext, new ImageLoader.Builder()
  483. .url(UserInfoXml.getAvatarUrl()).transform(new CircleTransformation()).imageView(ivAvatar).build());
  484. // // 点击头像可查看用户详情
  485. // ivAvatar.setOnClickListener(new View.OnClickListener() {
  486. // @Override
  487. // public void onClick(View v) {
  488. // UserInfoDetailActivity.toUserInfoDetailActivity((Activity) mContext, message.getUid(), "0", UserInfoDetailActivity.SOURCE_FROM_CHAT_CONTENT);
  489. // }
  490. // });
  491. // 设置语音信息
  492. String audioTime = message.getAudioTime();
  493. if (!TextUtils.isEmpty(audioTime)) {
  494. viewHolder.setText(R.id.chat_voice_right_tv, audioTime + "''");
  495. }
  496. String tempPath;
  497. String audioUrl = message.getAudioUrl();
  498. if (Utils.isHttpUrl(audioUrl)) {
  499. // 将语音下载到本地(根据用户id存放到不同的目录下)
  500. String downloadFileDir = FileUtil.RECORD_DIRECTORY_PATH + File.separator + message.getUid();
  501. String downloadFileName = FileUtil.createFileNameByDate(message.getCreateDate()) + ".mp3";
  502. tempPath = downloadFileDir + File.separator + downloadFileName;
  503. FileUtil.downloadFile(audioUrl, downloadFileDir, downloadFileName);
  504. } else {
  505. tempPath = audioUrl;
  506. }
  507. final String localPath = tempPath;
  508. // 语音加载进度
  509. final ImageView ivLoading = (ImageView) viewHolder.getView(R.id.chat_voice_msg_loading);
  510. AnimationDrawable animationDrawable = Util.getFrameAnim(Utils.getLoadingDrawableList(mContext), true, 50);
  511. animationDrawable.start();
  512. ivLoading.setImageDrawable(animationDrawable);
  513. ivLoading.setVisibility(mIsShowVoiceLoading ? View.VISIBLE : View.GONE);
  514. // 图片重发
  515. final ImageView ivRetry = (ImageView) viewHolder.getView(R.id.chat_voice_msg_retry);
  516. ivRetry.setVisibility(mIsShowVoiceRetry ? View.VISIBLE : View.GONE);
  517. ivRetry.setOnClickListener(new View.OnClickListener() {
  518. @Override
  519. public void onClick(View v) {
  520. // 点击重发
  521. DialogUtil.showDoubleBtnDialog(((FragmentActivity) mContext).getSupportFragmentManager(),
  522. null, mContext.getString(R.string.retry_message), mContext.getString(R.string.positive),
  523. mContext.getString(R.string.negative), true, new OnDoubleDialogClickListener() {
  524. @Override
  525. public void onPositiveClick(View view) {
  526. mIsShowVoiceLoading = true;
  527. mIsShowVoiceRetry = false;
  528. ivLoading.setVisibility(View.VISIBLE);
  529. ivRetry.setVisibility(View.GONE);
  530. sendVoiceMsg(new File(localPath), message);
  531. }
  532. @Override
  533. public void onNegativeClick(View view) {
  534. }
  535. });
  536. }
  537. });
  538. // 点击播放语音
  539. final ImageView ivAnim = (ImageView) viewHolder.getView(R.id.chat_voice_right_iv);
  540. ivAnim.setImageResource(R.drawable.msg_voice_right_three);
  541. viewHolder.getView(R.id.chat_voice_right).setOnClickListener(new View.OnClickListener() {
  542. @Override
  543. public void onClick(View v) {
  544. // 当前是否正在播放语音
  545. if (RecordUtil.getInstance().isPlaying()) {
  546. // 先停止正在播放的语音
  547. RecordUtil.getInstance().stop();
  548. if (mVoiceAnimLeft.isRunning()) {
  549. mVoiceAnimLeft.stop();
  550. }
  551. if (mVoiceAnimRight.isRunning()) {
  552. mVoiceAnimRight.stop();
  553. }
  554. }
  555. File file = new File(localPath);
  556. if (file.exists()) {
  557. // 播放/停止语音动画
  558. if (!mVoiceAnimRight.isRunning()) {
  559. ivAnim.setImageDrawable(mVoiceAnimRight);
  560. mVoiceAnimRight.start();
  561. }
  562. // 播放/暂停语音
  563. RecordUtil.getInstance().toggle(localPath, new RecordUtil.OnPlayerListener() {
  564. @Override
  565. public void onCompleted() {
  566. if (mVoiceAnimRight.isRunning()) {
  567. mVoiceAnimRight.stop();
  568. ivAnim.setImageResource(R.drawable.msg_voice_right_three);
  569. }
  570. }
  571. @Override
  572. public void onPaused() {
  573. if (mVoiceAnimRight.isRunning()) {
  574. mVoiceAnimRight.stop();
  575. ivAnim.setImageResource(R.drawable.msg_voice_right_three);
  576. }
  577. }
  578. });
  579. } else {
  580. ToastUtil.showShortToast(mContext, mContext.getResources().getString(R.string.not_exist));
  581. }
  582. }
  583. });
  584. }
  585. }
  586. /**
  587. * 设置对方QA消息
  588. *
  589. * @param viewHolder
  590. * @param message
  591. */
  592. private void setQaMesssage(RecyclerViewHolder viewHolder, final Message message) {
  593. if (message != null) {
  594. // 消息发送时间
  595. viewHolder.setText(R.id.chat_qa_msg_time_left, TimeUtil.getLocalTime(mContext, mServerSystemTime, message.getCreateDateMills()));
  596. // 头像
  597. ImageView ivAvatar = (ImageView) viewHolder.getView(R.id.chat_qa_msg_avatar_left);
  598. ImageLoaderUtil.getInstance().loadImage(mContext, new ImageLoader.Builder()
  599. .url(mAvatarUrl).transform(new CircleTransformation()).imageView(ivAvatar).build());
  600. // 点击头像可查看用户详情
  601. ivAvatar.setOnClickListener(new View.OnClickListener() {
  602. @Override
  603. public void onClick(View v) {
  604. UserInfoDetailActivity.toUserInfoDetailActivity((Activity) mContext, message.getUid(), null, UserInfoDetailActivity.SOURCE_FROM_CHAT_CONTENT);
  605. }
  606. });
  607. // QA问题
  608. String content = message.getContent();
  609. if (!TextUtils.isEmpty(content)) {
  610. viewHolder.setText(R.id.chat_qa_msg_question, Util.convertText(content));
  611. }
  612. // QA答案列表
  613. QaQuestion qaQuestion = message.getQaQuestion();
  614. if (qaQuestion != null) {
  615. List<QaAnswer> qaAnswerList = qaQuestion.getListQaAnswer();
  616. if (!Util.isListEmpty(qaAnswerList)) {
  617. RecyclerView answerListView = (RecyclerView) viewHolder.getView(R.id.chat_qa_msg_answer_list);
  618. LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
  619. linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
  620. answerListView.setLayoutManager(linearLayoutManager);
  621. final QaAnswerAdapter qaAnswerAdapter = new QaAnswerAdapter(mContext, R.layout.item_chat_qa_answer_list, qaAnswerList);
  622. answerListView.setAdapter(qaAnswerAdapter);
  623. qaAnswerAdapter.setOnItemClickListener(new OnItemClickListener() {
  624. @Override
  625. public void onItemClick(View view, int position, RecyclerViewHolder viewHolder) {
  626. if (!mIsIntercept) {
  627. QaAnswer qaAnswer = qaAnswerAdapter.getItemByPosition(position);
  628. if (qaAnswer != null) {
  629. sendQaMessage(qaAnswer.getId());
  630. }
  631. } else {
  632. BuyServiceActivity.toBuyServiceActivity((Activity) mContext, BuyServiceActivity.INTENT_FROM_INTERCEPT,
  633. IConfigConstant.PAY_TAG_FROM_CHAT, IConfigConstant.PAY_WAY_ALL);
  634. }
  635. }
  636. });
  637. }
  638. }
  639. }
  640. }
  641. /**
  642. * 设置对方图片消息
  643. *
  644. * @param viewHolder
  645. * @param message
  646. */
  647. private void setLeftImageMsg(RecyclerViewHolder viewHolder, final Message message) {
  648. if (message != null) {
  649. viewHolder.setText(R.id.chat_image_msg_time_left, TimeUtil.getLocalTime(mContext, mServerSystemTime, message.getCreateDateMills()));
  650. // 头像
  651. ImageView ivAvatar = (ImageView) viewHolder.getView(R.id.chat_image_msg_avatar_left);
  652. ImageLoaderUtil.getInstance().loadImage(mContext, new ImageLoader.Builder()
  653. .url(mAvatarUrl).transform(new CircleTransformation()).imageView(ivAvatar).build());
  654. // 点击头像可查看用户详情
  655. ivAvatar.setOnClickListener(new View.OnClickListener() {
  656. @Override
  657. public void onClick(View v) {
  658. UserInfoDetailActivity.toUserInfoDetailActivity((Activity) mContext, message.getUid(), "0", UserInfoDetailActivity.SOURCE_FROM_CHAT_CONTENT);
  659. }
  660. });
  661. final Image image = message.getChatImage();
  662. if (image != null) {
  663. ChatImageView imageView = (ChatImageView) viewHolder.getView(R.id.chat_image_msg_left);
  664. // 设置图片规格:1/3屏幕宽度,宽高比3:4
  665. int width = DensityUtil.getScreenWidth(mContext) / 3;
  666. RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, (int) ((4 / 3.0f) * width));
  667. layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.chat_image_msg_avatar_left);
  668. layoutParams.addRule(RelativeLayout.BELOW, R.id.chat_image_msg_time_left);
  669. imageView.setLayoutParams(layoutParams);
  670. ImageLoaderUtil.getInstance().loadImage(mContext, new ImageLoader.Builder().url(image.getThumbnailUrlM())
  671. .imageView(imageView).placeHolder(R.drawable.img_default).build());
  672. // 点击图片浏览大图
  673. imageView.setOnClickListener(new View.OnClickListener() {
  674. @Override
  675. public void onClick(View v) {
  676. PictureBrowseActivity.toPictureBrowseActivity(mContext, mAllImageList.indexOf(image), mAllImageList);
  677. }
  678. });
  679. }
  680. }
  681. }
  682. /**
  683. * 设置我方图片消息
  684. *
  685. * @param viewHolder
  686. * @param message
  687. */
  688. private void setRightImageMsg(RecyclerViewHolder viewHolder, final Message message) {
  689. if (message != null) {
  690. viewHolder.setText(R.id.chat_image_msg_time_right, TimeUtil.getLocalTime(mContext, mServerSystemTime, message.getCreateDateMills()));
  691. final Image image = message.getChatImage();
  692. // 头像
  693. ImageView ivAvatar = (ImageView) viewHolder.getView(R.id.chat_image_msg_avatar_Right);
  694. ImageLoaderUtil.getInstance().loadImage(mContext, new ImageLoader.Builder()
  695. .url(UserInfoXml.getAvatarUrl()).transform(new CircleTransformation()).imageView(ivAvatar).build());
  696. // // 点击头像可查看用户详情
  697. // ivAvatar.setOnClickListener(new View.OnClickListener() {
  698. // @Override
  699. // public void onClick(View v) {
  700. // UserInfoDetailActivity.toUserInfoDetailActivity((Activity) mContext, message.getUid(), "0", UserInfoDetailActivity.SOURCE_FROM_CHAT_CONTENT);
  701. // }
  702. // });
  703. if (image != null) {
  704. ChatImageView imageView = (ChatImageView) viewHolder.getView(R.id.chat_image_msg_right);
  705. // 设置图片规格:1/3屏幕宽度,宽高比3:4
  706. int width = DensityUtil.getScreenWidth(mContext) / 3;
  707. LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, (int) ((4 / 3.0f) * width));
  708. imageView.setLayoutParams(layoutParams);
  709. imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
  710. String imageUrl = image.getThumbnailUrlM();
  711. if (Utils.isHttpUrl(imageUrl)) {
  712. ImageLoaderUtil.getInstance().loadImage(mContext, new ImageLoader.Builder().url(imageUrl)
  713. .imageView(imageView).placeHolder(R.drawable.img_default).build());
  714. } else {
  715. ImageLoaderUtil.getInstance().loadLocalImage(mContext, imageView, Uri.fromFile(new File(imageUrl)));
  716. }
  717. // 点击图片浏览大图
  718. imageView.setOnClickListener(new View.OnClickListener() {
  719. @Override
  720. public void onClick(View v) {
  721. PictureBrowseActivity.toPictureBrowseActivity(mContext, mAllImageList.indexOf(image), mAllImageList);
  722. }
  723. });
  724. }
  725. // 图片加载进度
  726. final ImageView ivLoading = (ImageView) viewHolder.getView(R.id.chat_image_msg_loading);
  727. AnimationDrawable animationDrawable = Util.getFrameAnim(Utils.getLoadingDrawableList(mContext), true, 50);
  728. animationDrawable.start();
  729. ivLoading.setImageDrawable(animationDrawable);
  730. ivLoading.setVisibility(mIsShowImgLoading ? View.VISIBLE : View.GONE);
  731. // 图片重发
  732. final ImageView ivRetry = (ImageView) viewHolder.getView(R.id.chat_image_msg_retry);
  733. ivRetry.setVisibility(mIsShowImgRetry ? View.VISIBLE : View.GONE);
  734. ivRetry.setOnClickListener(new View.OnClickListener() {
  735. @Override
  736. public void onClick(View v) {
  737. // 点击重发
  738. DialogUtil.showDoubleBtnDialog(((FragmentActivity) mContext).getSupportFragmentManager(),
  739. null, mContext.getString(R.string.retry_message), mContext.getString(R.string.positive),
  740. mContext.getString(R.string.negative), true, new OnDoubleDialogClickListener() {
  741. @Override
  742. public void onPositiveClick(View view) {
  743. mIsShowImgLoading = true;
  744. mIsShowImgRetry = false;
  745. ivLoading.setVisibility(View.VISIBLE);
  746. ivRetry.setVisibility(View.GONE);
  747. sendPhotoMsg(new File(message.getChatImage().getImageUrl()), message);
  748. }
  749. @Override
  750. public void onNegativeClick(View view) {
  751. }
  752. });
  753. }
  754. });
  755. }
  756. }
  757. /**
  758. * 更新语音信已读状态
  759. *
  760. * @param msgId
  761. */
  762. private void updateVoiceMsgStatus(final String msgId) {
  763. OkHttpUtils.post()
  764. .url(IUrlConstant.URL_UPDATE_VOICE_MSG_STATUS)
  765. .addHeader("token", PlatformInfoXml.getToken())
  766. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  767. .addParams("msgId", msgId)
  768. .build().execute(new Callback<BaseModel>() {
  769. @Override
  770. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  771. String resultJson = response.body().string();
  772. if (!TextUtils.isEmpty(resultJson)) {
  773. return JSON.parseObject(resultJson, WriteMsg.class);
  774. }
  775. return null;
  776. }
  777. @Override
  778. public void onError(Call call, Exception e, int id) {
  779. // 失败后再次更新
  780. updateVoiceMsgStatus(msgId);
  781. }
  782. @Override
  783. public void onResponse(BaseModel response, int id) {
  784. if (response != null) {
  785. String isSucceed = response.getIsSucceed();
  786. if (!TextUtils.isEmpty(isSucceed) && "0".equals(isSucceed)) {
  787. // 失败后再次更新
  788. updateVoiceMsgStatus(msgId);
  789. }
  790. }
  791. }
  792. });
  793. }
  794. /**
  795. * 发送QA消息
  796. *
  797. * @param qaId
  798. */
  799. private void sendQaMessage(final String qaId) {
  800. final Dialog dialog = Utils.showLoadingDialog(mContext, true);
  801. OkHttpUtils.post()
  802. .url(IUrlConstant.URL_SEND_TEXT_MSG)
  803. .addHeader("token", PlatformInfoXml.getToken())
  804. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  805. .addParams("uid", mUId)
  806. .addParams("writeMsgType", "1")
  807. .addParams("qaAnswerId", qaId)
  808. .build()
  809. .execute(new Callback<WriteMsg>() {
  810. @Override
  811. public WriteMsg parseNetworkResponse(Response response, int id) throws Exception {
  812. String resultJson = response.body().string();
  813. if (!TextUtils.isEmpty(resultJson)) {
  814. return JSON.parseObject(resultJson, WriteMsg.class);
  815. }
  816. return null;
  817. }
  818. @Override
  819. public void onError(Call call, Exception e, int id) {
  820. ToastUtil.showShortToast(mContext, mContext.getString(R.string.send_fail));
  821. if (dialog != null && dialog.isShowing()) {
  822. dialog.dismiss();
  823. }
  824. }
  825. @Override
  826. public void onResponse(WriteMsg response, int id) {
  827. if (response != null) {
  828. String isSucceed = response.getIsSucceed();
  829. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  830. // 发送事件,刷新当前聊天记录
  831. EventBus.getDefault().post(new RefreshChatEvent());
  832. // 发送事件,更新聊天列表
  833. EventBus.getDefault().post(new MessageChangedEvent());
  834. } else {
  835. ToastUtil.showShortToast(mContext, mContext.getString(R.string.send_fail));
  836. }
  837. } else {
  838. ToastUtil.showShortToast(mContext, mContext.getString(R.string.send_fail));
  839. }
  840. if (dialog != null && dialog.isShowing()) {
  841. dialog.dismiss();
  842. }
  843. }
  844. });
  845. }
  846. /**
  847. * 发送语音消息
  848. */
  849. private void sendVoiceMsg(File file, final Message message) {
  850. OkHttpUtils.post()
  851. .url(IUrlConstant.URL_SEND_VOICE_MSG)
  852. .addHeader("token", PlatformInfoXml.getToken())
  853. .addHeader("platformInfo", PlatformInfoXml.getPlatformJsonString())
  854. .addHeader("uid", mUId)
  855. .addHeader("writeMsgType", "1")
  856. .addHeader("audioSecond", message.getAudioTime())
  857. .addFile("file", file.getName(), file)
  858. .build()
  859. .execute(new Callback<BaseModel>() {
  860. @Override
  861. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  862. String resultJson = response.body().string();
  863. if (!TextUtils.isEmpty(resultJson)) {
  864. return JSON.parseObject(resultJson, BaseModel.class);
  865. }
  866. return null;
  867. }
  868. @Override
  869. public void onError(Call call, Exception e, int id) {
  870. sendVoiceFailed(message);
  871. }
  872. @Override
  873. public void onResponse(BaseModel response, int id) {
  874. if (response != null) {
  875. String isSucceed = response.getIsSucceed();
  876. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  877. // 发送事件,刷新当前聊天记录
  878. EventBus.getDefault().post(new RefreshChatEvent());
  879. // 发送事件,更新聊天列表
  880. EventBus.getDefault().post(new MessageChangedEvent());
  881. } else {
  882. sendVoiceFailed(message);
  883. }
  884. } else {
  885. sendVoiceFailed(message);
  886. }
  887. }
  888. });
  889. }
  890. /**
  891. * 发送语音失败,刷新状态
  892. */
  893. private void sendVoiceFailed(Message message) {
  894. mIsShowVoiceLoading = false;
  895. mIsShowVoiceRetry = true;
  896. updateItem(getItemCount() - 1, message);
  897. ToastUtil.showShortToast(mContext, mContext.getString(R.string.send_fail));
  898. }
  899. /**
  900. * 发送图片消息
  901. */
  902. private void sendPhotoMsg(File file, final Message message) {
  903. OkHttpUtils.post()
  904. .url(IUrlConstant.URL_SEND_PHOTO_MSG)
  905. .addHeader("token", PlatformInfoXml.getToken())
  906. .addHeader("platformInfo", PlatformInfoXml.getPlatformJsonString())
  907. .addHeader("recevUserId", mUId)
  908. .addFile("request", file.getName(), file)
  909. .build()
  910. .execute(new Callback<BaseModel>() {
  911. @Override
  912. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  913. String resultJson = response.body().string();
  914. if (!TextUtils.isEmpty(resultJson)) {
  915. return JSON.parseObject(resultJson, BaseModel.class);
  916. }
  917. return null;
  918. }
  919. @Override
  920. public void onError(Call call, Exception e, int id) {
  921. sendPhotoFailed(message);
  922. }
  923. @Override
  924. public void onResponse(BaseModel response, int id) {
  925. if (response != null) {
  926. String isSucceed = response.getIsSucceed();
  927. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  928. // 发送事件,刷新当前聊天记录
  929. EventBus.getDefault().post(new RefreshChatEvent());
  930. // 发送事件,更新聊天列表
  931. EventBus.getDefault().post(new MessageChangedEvent());
  932. } else {
  933. sendPhotoFailed(message);
  934. }
  935. } else {
  936. sendPhotoFailed(message);
  937. }
  938. }
  939. });
  940. }
  941. /**
  942. * 发送图片失败,刷新状态
  943. */
  944. private void sendPhotoFailed(Message message) {
  945. mIsShowImgLoading = false;
  946. mIsShowImgRetry = true;
  947. updateItem(getItemCount() - 1, message);
  948. ToastUtil.showShortToast(mContext, mContext.getString(R.string.send_fail));
  949. }
  950. }