PageRenderTime 703ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/yueai/src/main/java/com/yueai/activity/ChatActivity.java

https://bitbucket.org/juzhiwiscom/datelove
Java | 1981 lines | 815 code | 105 blank | 1061 comment | 112 complexity | 1f9cd2475def9abff8c6790db19dedd0 MD5 | raw file
  1. package com.yueai.activity;
  2. import android.Manifest;
  3. import android.app.Activity;
  4. import android.app.Dialog;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.pm.PackageManager;
  8. import android.graphics.Color;
  9. import android.graphics.drawable.AnimationDrawable;
  10. import android.graphics.drawable.ColorDrawable;
  11. import android.graphics.drawable.Drawable;
  12. import android.net.Uri;
  13. import android.os.Handler;
  14. import android.os.Vibrator;
  15. import android.support.annotation.NonNull;
  16. import android.support.v4.app.ActivityCompat;
  17. import android.support.v4.content.ContextCompat;
  18. import android.support.v4.widget.SwipeRefreshLayout;
  19. import android.support.v7.widget.LinearLayoutManager;
  20. import android.support.v7.widget.RecyclerView;
  21. import android.text.TextUtils;
  22. import android.view.Display;
  23. import android.view.Gravity;
  24. import android.view.LayoutInflater;
  25. import android.view.MotionEvent;
  26. import android.view.View;
  27. import android.view.Window;
  28. import android.view.WindowManager;
  29. import android.widget.AdapterView;
  30. import android.widget.Button;
  31. import android.widget.ImageView;
  32. import android.widget.PopupWindow;
  33. import android.widget.RelativeLayout;
  34. import android.widget.TextView;
  35. import com.alibaba.fastjson.JSON;
  36. import com.library.dialog.OnDoubleDialogClickListener;
  37. import com.library.utils.DialogUtil;
  38. import com.library.utils.LogUtil;
  39. import com.library.utils.SharedPreferenceUtil;
  40. import com.library.utils.ToastUtil;
  41. import com.library.utils.Util;
  42. import com.library.utils.VersionInfoUtil;
  43. import com.library.widgets.AutoSwipeRefreshLayout;
  44. import com.library.widgets.ClearableEditText;
  45. import com.yueai.R;
  46. import com.yueai.adapter.ChatAdapter;
  47. import com.yueai.base.BaseTitleActivity;
  48. import com.yueai.bean.BaseModel;
  49. import com.yueai.bean.Image;
  50. import com.yueai.bean.Message;
  51. import com.yueai.bean.MessageList;
  52. import com.yueai.bean.WriteMsg;
  53. import com.yueai.constant.IConfigConstant;
  54. import com.yueai.constant.IUrlConstant;
  55. import com.yueai.event.MessageChangedEvent;
  56. import com.yueai.event.RefreshChatEvent;
  57. import com.yueai.fragment.MessageFragment;
  58. import com.yueai.haoping.ActionSheetDialog;
  59. import com.yueai.haoping.OnOperItemClickL;
  60. import com.yueai.online.MessageDao;
  61. import com.yueai.utils.FileUtil;
  62. import com.yueai.utils.GreenDaoUtil;
  63. import com.yueai.utils.RecordUtil;
  64. import com.yueai.utils.Utils;
  65. import com.yueai.xml.PlatformInfoXml;
  66. import com.yueai.xml.UserInfoXml;
  67. import com.zhy.http.okhttp.OkHttpUtils;
  68. import com.zhy.http.okhttp.callback.Callback;
  69. import org.greenrobot.eventbus.EventBus;
  70. import org.greenrobot.eventbus.Subscribe;
  71. import org.greenrobot.greendao.query.QueryBuilder;
  72. import java.io.File;
  73. import java.lang.ref.WeakReference;
  74. import java.util.ArrayList;
  75. import java.util.HashMap;
  76. import java.util.List;
  77. import java.util.Map;
  78. import butterknife.BindView;
  79. import butterknife.OnClick;
  80. import okhttp3.Call;
  81. import okhttp3.Response;
  82. /**
  83. * 聊天页面
  84. * Created by zhangdroid on 2016/11/2.
  85. */
  86. public class ChatActivity extends BaseTitleActivity {
  87. @BindView(R.id.chat_swiperefresh)
  88. AutoSwipeRefreshLayout mAutoSwipeRefresh;
  89. @BindView(R.id.chat_RecyclerView)
  90. RecyclerView mRecyclerView;
  91. @BindView(R.id.chat_bottom_bar)
  92. RelativeLayout mRlChatBottom;
  93. @BindView(R.id.chat_operate_container)
  94. RelativeLayout mRlChatContainer;
  95. @BindView(R.id.chat_et_input)
  96. ClearableEditText mEtInput;
  97. @BindView(R.id.chat_send_msg)
  98. Button mBtnSend;// 发送文字消息
  99. @BindView(R.id.chat_send_photo)
  100. ImageView mIvSendPhoto;// 发送图片
  101. @BindView(R.id.chat_intercept)
  102. TextView mTvIntercept;
  103. // 语音
  104. @BindView(R.id.chat_msg_switch)
  105. ImageView mIvChatSwitch;
  106. @BindView(R.id.record)
  107. Button mBtnRecord;
  108. /**
  109. * 用户id
  110. */
  111. private String mUserId;
  112. /**
  113. * 用户昵称
  114. */
  115. private String mNickname;
  116. /**
  117. * 用户头像url
  118. */
  119. private String mAvatarUrl;
  120. private ChatAdapter mChatAdapter;
  121. private LinearLayoutManager mLinearLayoutManager;
  122. // 录音框
  123. private PopupWindow mRecordPopupWindow;
  124. private static final int PERMISSION_CODE_RECORD_AUDIO = 0;
  125. private static final int PERMISSION_CODE_WRITE_EXTERNAL_STORAGE = 1;
  126. private static final int MSG_TYPE_TIMER = 1;
  127. // 录音文件路径
  128. private String mOutputPath;
  129. // 录音文件名
  130. private String mRecordFileName;
  131. // 是否正在录音
  132. private boolean mIsRecording;
  133. // 录音是否被取消
  134. private boolean mIsRecordCanceled;
  135. // 录音时长(毫秒)
  136. private long mRecordDuration;
  137. // 录音计时刷新间隔(毫秒)
  138. private final long mRefreshInterval = 250;
  139. // 计算录音时长的Handler
  140. private Handler mTimerHandler = new Handler() {
  141. @Override
  142. public void handleMessage(android.os.Message msg) {
  143. super.handleMessage(msg);
  144. switch (msg.what) {
  145. case MSG_TYPE_TIMER:
  146. if (mIsRecording) {
  147. // 计时
  148. mRecordDuration += mRefreshInterval;
  149. // 录音超过1分钟自动发送
  150. if (mRecordDuration > 60 * 1000) {
  151. stopRecordAndSend();
  152. } else {
  153. mTimerHandler.sendEmptyMessageDelayed(MSG_TYPE_TIMER, mRefreshInterval);
  154. }
  155. }
  156. break;
  157. }
  158. }
  159. };
  160. private Message msg;
  161. // 录音状态常量
  162. private enum STATE {
  163. /**
  164. * 默认值
  165. */
  166. IDLE,
  167. /**
  168. * 录音中
  169. */
  170. RECORDING,
  171. /**
  172. * 取消
  173. */
  174. CANCELED,
  175. /**
  176. * 录音时间太短
  177. */
  178. TOO_SHORT
  179. }
  180. private boolean mIsShowSendVipByPraise = false;
  181. private boolean mIsShowWriteMsgIntercept = false;
  182. private static final String INTENT_KEY_UID = "key_userId";
  183. private static final String INTENT_KEY_NICKNAME = "key_nickname";
  184. private static final String INTENT_KEY_AVATAR_URL = "key_avatar_url";
  185. private Dialog dialog;
  186. /**
  187. * 跳转聊天页
  188. *
  189. * @param from
  190. * @param userId 用户id
  191. * @param nickname 用户昵称
  192. * @param avatarUrl 用户头像url
  193. */
  194. public static void toChatActivity(Activity from, String userId, String nickname, String avatarUrl) {
  195. Map<String, String> map = new HashMap<String, String>();
  196. map.put(INTENT_KEY_UID, userId);
  197. map.put(INTENT_KEY_NICKNAME, nickname);
  198. map.put(INTENT_KEY_AVATAR_URL, avatarUrl);
  199. Util.gotoActivity(from, ChatActivity.class, false, map);
  200. }
  201. /**
  202. * 跳转聊天页
  203. *
  204. * @param context
  205. * @param userId 用户id
  206. * @param nickname 用户昵称
  207. * @param avatarUrl 用户头像url
  208. */
  209. public static void toChatActivity(Context context, String userId, String nickname, String avatarUrl) {
  210. Intent intent = new Intent(context, ChatActivity.class);
  211. intent.putExtra(INTENT_KEY_UID, userId);
  212. intent.putExtra(INTENT_KEY_NICKNAME, nickname);
  213. intent.putExtra(INTENT_KEY_AVATAR_URL, avatarUrl);
  214. context.startActivity(intent);
  215. }
  216. @Override
  217. protected int getLayoutResId() {
  218. return R.layout.activity_chat;
  219. }
  220. @Override
  221. protected String getCenterTitle() {
  222. return null;
  223. }
  224. @Override
  225. protected void initViewsAndVariables() {
  226. //// /**加载读信拦截对话框**/
  227. // boolean showDalog = SharedPreferenceUtil.getBooleanValue(Utils.getContext(), "INTERCEPT_DIALOG", "DIALOG", true);
  228. // if(showDalog){
  229. // mTvIntercept.setVisibility(View.VISIBLE);
  230. // mRlChatContainer.setVisibility(View.GONE);
  231. // }
  232. Intent intent = getIntent();
  233. if (intent != null) {
  234. mUserId = intent.getStringExtra(INTENT_KEY_UID);
  235. mNickname = intent.getStringExtra(INTENT_KEY_NICKNAME);
  236. mAvatarUrl = intent.getStringExtra(INTENT_KEY_AVATAR_URL);
  237. }
  238. if (!TextUtils.isEmpty(mNickname)) {
  239. setTitle(mNickname);
  240. }
  241. // 设置下拉刷新动画样式
  242. mAutoSwipeRefresh.setColorSchemeResources(R.color.main_color);
  243. mAutoSwipeRefresh.setProgressBackgroundColorSchemeColor(Color.WHITE);
  244. mLinearLayoutManager = new LinearLayoutManager(this);
  245. mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
  246. mRecyclerView.setLayoutManager(mLinearLayoutManager);
  247. mChatAdapter = new ChatAdapter(this, -1);
  248. mRecyclerView.setAdapter(mChatAdapter);
  249. loadMsgList();
  250. }
  251. /**加载读信拦截的对话框**/
  252. private void showDialog() {
  253. dialog= new Dialog(this);
  254. LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
  255. View v=li.inflate(R.layout.dialog_layout, null);
  256. dialog.setContentView(v);
  257. ImageView ivBack = (ImageView)v.findViewById(R.id.iv_back);
  258. Button btnUpgrade = (Button)v.findViewById(R.id.btn_upgrade);
  259. ivBack.setOnClickListener(new View.OnClickListener() {
  260. @Override
  261. public void onClick(View view) {
  262. finish();
  263. // dialog.dismiss();
  264. }
  265. });
  266. btnUpgrade.setOnClickListener(new View.OnClickListener() {
  267. @Override
  268. public void onClick(View view) {
  269. //添加好评送会员
  270. if (mIsShowSendVipByPraise) {
  271. ActionSheetDialogNoTitle();
  272. } else {
  273. BuyServiceActivity.toBuyServiceActivity(ChatActivity.this, BuyServiceActivity.INTENT_FROM_INTERCEPT,
  274. IConfigConstant.PAY_TAG_FROM_CHAT, IConfigConstant.PAY_WAY_ALL);
  275. }
  276. }
  277. });
  278. // dialog.setTitle("Custom Dialog");
  279. Window dialogWindow = dialog.getWindow();
  280. WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
  281. dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
  282. /*
  283. * 将对话框的大小按屏幕大小的百分比设置
  284. */
  285. WindowManager m = getWindowManager();
  286. Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
  287. p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6
  288. p.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.65
  289. p.x = (int) (d.getWidth() * 0.135); // 新位置X坐标
  290. p.y = (int) (d.getHeight() * 0.24); // 新位置Y坐标
  291. // p.y = (int) (d.getHeight() * 0.1); // 新位置Y坐标
  292. // p.alpha = 1.0f; // 透明度
  293. // p.dimAmount = 0.7f;//黑暗度
  294. dialogWindow.setAttributes(p);
  295. // getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
  296. // WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
  297. // getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
  298. dialog.setCancelable(false);
  299. dialog.show();
  300. }
  301. /**根据是否是会员来判断拦截对话框的显示与否**/
  302. private void isShowDialog() {
  303. OkHttpUtils.post()
  304. .url(IUrlConstant.URL_GET_CHAT_MSG)
  305. .addHeader("token", PlatformInfoXml.getToken())
  306. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  307. .addParams("uid", mUserId)
  308. .addParams("isUpdateUnPay", "0")
  309. .build()
  310. .execute(new Callback<MessageList>() {
  311. @Override
  312. public MessageList parseNetworkResponse(Response response, int id) throws Exception {
  313. String resultJson = response.body().string();
  314. if (!TextUtils.isEmpty(resultJson)) {
  315. return JSON.parseObject(resultJson, MessageList.class);
  316. }
  317. return null;
  318. }
  319. @Override
  320. public void onError(Call call, Exception e, int id) {
  321. stopRefresh(3);
  322. }
  323. @Override
  324. public void onResponse(MessageList response, int id) {
  325. if (response != null) {
  326. // 设置支付拦截
  327. String intercept = response.getShowWriteMsgIntercept();
  328. if (!TextUtils.isEmpty(intercept) && "1".equals(intercept)) {
  329. // dialog.show();
  330. // showDialog=true;
  331. showDialog();
  332. SharedPreferenceUtil.setBooleanValue(Utils.getContext(), "INTERCEPT_DIALOG", "DIALOG", true);
  333. } else {
  334. // 不展示拦截入口
  335. // dialog.dismiss();
  336. // showDialog=false;
  337. }
  338. }
  339. // stopRefresh(1);
  340. }
  341. });
  342. }
  343. @Override
  344. protected void addListeners() {
  345. // mIvLeft = (ImageView) findViewById(R.id.activity_base_iv_left);
  346. mIvLeft.setOnClickListener(new View.OnClickListener() {
  347. @Override
  348. public void onClick(View v) {
  349. // 刷新聊天列表
  350. EventBus.getDefault().post(new MessageChangedEvent());
  351. finish();
  352. }
  353. });
  354. mAutoSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  355. @Override
  356. public void onRefresh() {
  357. loadMsgList();
  358. }
  359. });
  360. // 处理滑动冲突
  361. mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
  362. @Override
  363. public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  364. super.onScrollStateChanged(recyclerView, newState);
  365. }
  366. @Override
  367. public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  368. super.onScrolled(recyclerView, dx, dy);
  369. // 判断是否滑动到顶部
  370. int scrolledPosition = (mRecyclerView == null || mRecyclerView.getChildCount() == 0) ? 0 : mRecyclerView.getChildAt(0).getTop();
  371. mAutoSwipeRefresh.setEnabled(scrolledPosition >= 0);
  372. }
  373. });
  374. // 发送文字消息
  375. mEtInput.setTextWatcherCallback(new ClearableEditText.TextWatcherCallback() {
  376. @Override
  377. public void handleTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
  378. if (mEtInput.length() > 0) {
  379. mBtnSend.setVisibility(View.VISIBLE);
  380. mIvSendPhoto.setVisibility(View.GONE);
  381. } else {
  382. mBtnSend.setVisibility(View.GONE);
  383. mIvSendPhoto.setVisibility(View.VISIBLE);
  384. }
  385. }
  386. });
  387. // 切换文字/语音
  388. mIvChatSwitch.setOnClickListener(new View.OnClickListener() {
  389. @Override
  390. public void onClick(View v) {
  391. mIvChatSwitch.setSelected(!mIvChatSwitch.isSelected());
  392. if (mIvChatSwitch.isSelected()) { // 显示发送语音
  393. mBtnRecord.setText(getString(R.string.press_to_speak));
  394. mBtnRecord.setVisibility(View.VISIBLE);
  395. mEtInput.setVisibility(View.GONE);
  396. mBtnSend.setVisibility(View.GONE);
  397. mIvSendPhoto.setVisibility(View.GONE);
  398. if (mIsShowWriteMsgIntercept) {// 支付拦截时不可用
  399. mBtnRecord.setClickable(false);
  400. mBtnRecord.setEnabled(false);
  401. mBtnRecord.setTextColor(getResources().getColor(R.color.textcolor_light_gray));
  402. } else {
  403. mBtnRecord.setClickable(true);
  404. mBtnRecord.setEnabled(true);
  405. mBtnRecord.setTextColor(getResources().getColor(R.color.main_color));
  406. }
  407. } else { // 显示输入框
  408. mBtnRecord.setVisibility(View.GONE);
  409. mEtInput.setVisibility(View.VISIBLE);
  410. if (mEtInput.getText().length() > 0) {
  411. // 已经输入文字
  412. mBtnSend.setVisibility(View.VISIBLE);
  413. mIvSendPhoto.setVisibility(View.GONE);
  414. } else {
  415. mBtnSend.setVisibility(View.GONE);
  416. mIvSendPhoto.setVisibility(View.VISIBLE);
  417. }
  418. }
  419. }
  420. });
  421. // 发送语音消息
  422. mBtnRecord.setOnTouchListener(new View.OnTouchListener() {
  423. @Override
  424. public boolean onTouch(View v, MotionEvent event) {
  425. // 检查权限
  426. if (!checkPermissions()) {// 请求权限时不录音
  427. switch (event.getAction()) {
  428. case MotionEvent.ACTION_DOWN:// 按下时开始录音
  429. mBtnRecord.setText(getString(R.string.loose_to_end));
  430. showRecordPopupWindow(STATE.RECORDING);
  431. startRecord();
  432. mIsRecordCanceled = false;
  433. LogUtil.e(LogUtil.TAG_ZL, "ACTION_DOWN start()");
  434. break;
  435. case MotionEvent.ACTION_MOVE:
  436. if (event.getY() < -100) { // 上滑取消发送
  437. mIsRecordCanceled = true;
  438. showRecordPopupWindow(STATE.CANCELED);
  439. } else {
  440. mIsRecordCanceled = false;
  441. showRecordPopupWindow(STATE.RECORDING);
  442. }
  443. LogUtil.e(LogUtil.TAG_ZL, "ACTION_MOVE " + event.getY() + " mIsRecordCanceled = " + mIsRecordCanceled);
  444. break;
  445. case MotionEvent.ACTION_UP:// 松开时停止录音并发送
  446. mBtnRecord.setText(getString(R.string.press_to_speak));
  447. if (mIsRecordCanceled) {
  448. cancelRecord();
  449. LogUtil.e(LogUtil.TAG_ZL, "ACTION_UP canceld()");
  450. } else {
  451. stopRecordAndSend();
  452. LogUtil.e(LogUtil.TAG_ZL, "ACTION_UP stop()");
  453. }
  454. break;
  455. }
  456. }
  457. return true;
  458. }
  459. });
  460. // 发送图片
  461. mIvSendPhoto.setOnClickListener(new View.OnClickListener() {
  462. @Override
  463. public void onClick(View v) {
  464. GetPhotoActivity.toGetPhotoActivity(ChatActivity.this, IConfigConstant.LOCAL_PIC_PATH, new GetPhotoActivity.OnGetPhotoListener() {
  465. @Override
  466. public void getSelectedPhoto(File file) {
  467. if (file != null) {
  468. // 发送图片前显示要发送的图片进度
  469. Message message = new Message();
  470. if (mChatAdapter != null) {
  471. mChatAdapter.isShowImgLoading(true);
  472. mChatAdapter.isShowImgRetry(false);
  473. message.setUid(UserInfoXml.getUID());
  474. message.setMsgType("10");
  475. message.setCreateDateMills(System.currentTimeMillis());
  476. Image image = new Image();
  477. image.setIsMain(2);
  478. image.setThumbnailUrl(file.getAbsolutePath());
  479. image.setThumbnailUrlM(file.getAbsolutePath());
  480. image.setImageUrl(file.getAbsolutePath());
  481. message.setChatImage(image);
  482. mChatAdapter.insertItem(mChatAdapter.getItemCount(), message);
  483. // 滚动到底部
  484. mLinearLayoutManager.scrollToPosition(mChatAdapter.getItemCount() - 1);
  485. }
  486. sendPhotoMsg(file, message);
  487. }
  488. }
  489. });
  490. }
  491. });
  492. }
  493. private boolean checkPermissions() {
  494. boolean needRequestPermission = false;
  495. if (ContextCompat.checkSelfPermission(ChatActivity.this.getApplicationContext(),
  496. Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_DENIED) {
  497. needRequestPermission = true;
  498. // 请求android.permission.RECORD_AUDIO权限
  499. ActivityCompat.requestPermissions(ChatActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, PERMISSION_CODE_RECORD_AUDIO);
  500. }
  501. if (ContextCompat.checkSelfPermission(ChatActivity.this.getApplicationContext(),
  502. Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
  503. needRequestPermission = true;
  504. // 请求android.permission.WRITE_EXTERNAL_STORAGE权限
  505. ActivityCompat.requestPermissions(ChatActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_CODE_WRITE_EXTERNAL_STORAGE);
  506. }
  507. return needRequestPermission;
  508. }
  509. @Override
  510. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  511. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  512. }
  513. /**
  514. * 开始录音
  515. */
  516. private void startRecord() {
  517. // 录音开始前震动一下
  518. Vibrator vibrator = (Vibrator) ChatActivity.this.getSystemService(Context.VIBRATOR_SERVICE);
  519. vibrator.vibrate(88);
  520. // 根据系统时间生成文件名
  521. mRecordFileName = FileUtil.createFileNameByTime() + ".mp3";
  522. // 录音文件保存路径: 根目录/DateLove/record/用户id/xxx.mp3
  523. mOutputPath = FileUtil.RECORD_DIRECTORY_PATH + File.separator + UserInfoXml.getUID() + File.separator + mRecordFileName;
  524. LogUtil.e(LogUtil.TAG_ZL, "录音存放路径:" + mOutputPath);
  525. // 开始录音
  526. RecordUtil.getInstance().startRecord(mOutputPath);
  527. mIsRecording = true;
  528. mRecordDuration = 0;
  529. // 计时,间隔250毫秒刷新
  530. mTimerHandler.sendEmptyMessageDelayed(MSG_TYPE_TIMER, mRefreshInterval);
  531. }
  532. /**
  533. * 停止录音并发送
  534. */
  535. private void stopRecordAndSend() {
  536. if (mIsRecording) {
  537. mIsRecording = false;
  538. LogUtil.e(LogUtil.TAG_ZL, "录音时长(毫秒):" + mRecordDuration);
  539. if (mRecordDuration < 1 * 1000) {// 录音时长小于1秒的不发送
  540. showRecordPopupWindow(STATE.TOO_SHORT);
  541. new Handler().postDelayed(new Runnable() {
  542. @Override
  543. public void run() {
  544. // 延时500毫秒调用MediaRecorder#stop(),防止出现start()之后立马调用stop()的异常
  545. RecordUtil.getInstance().stopRecord();
  546. // 删除小于1秒的文件
  547. FileUtil.deleteFile(mOutputPath);
  548. }
  549. }, 500);
  550. } else {// 发送语音
  551. RecordUtil.getInstance().stopRecord();
  552. sendVoiceMsg();
  553. }
  554. // 隐藏录音框
  555. new Handler().postDelayed(new Runnable() {
  556. @Override
  557. public void run() {
  558. if (mRecordPopupWindow != null && mRecordPopupWindow.isShowing()) {
  559. mRecordPopupWindow.dismiss();
  560. }
  561. }
  562. }, 500);
  563. }
  564. }
  565. /**
  566. * 录音取消,删除已经录制的文件
  567. */
  568. private void cancelRecord() {
  569. if (mIsRecordCanceled) {
  570. RecordUtil.getInstance().stopRecord();
  571. mIsRecordCanceled = false;
  572. FileUtil.deleteFile(mOutputPath);
  573. // 隐藏录音框
  574. new Handler().postDelayed(new Runnable() {
  575. @Override
  576. public void run() {
  577. if (mRecordPopupWindow != null && mRecordPopupWindow.isShowing()) {
  578. mRecordPopupWindow.dismiss();
  579. }
  580. }
  581. }, 500);
  582. }
  583. }
  584. /**
  585. * 弹出录音框
  586. */
  587. private void showRecordPopupWindow(STATE state) {
  588. if (mRecordPopupWindow == null) {
  589. View contentView = LayoutInflater.from(ChatActivity.this).inflate(R.layout.popup_chat_record, null);
  590. mRecordPopupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.WRAP_CONTENT,
  591. WindowManager.LayoutParams.WRAP_CONTENT, true);
  592. mRecordPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  593. }
  594. View view = mRecordPopupWindow.getContentView();
  595. if (view != null) {
  596. RelativeLayout rlRecording = (RelativeLayout) view.findViewById(R.id.popup_recording_container);
  597. ImageView ivRecording = (ImageView) view.findViewById(R.id.popup_record_anim);
  598. // 设置动画
  599. List<Drawable> drawableList = new ArrayList<Drawable>();
  600. drawableList.add(getResources().getDrawable(R.drawable.voice1));
  601. drawableList.add(getResources().getDrawable(R.drawable.voice2));
  602. drawableList.add(getResources().getDrawable(R.drawable.voice3));
  603. drawableList.add(getResources().getDrawable(R.drawable.voice4));
  604. drawableList.add(getResources().getDrawable(R.drawable.voice5));
  605. drawableList.add(getResources().getDrawable(R.drawable.voice6));
  606. AnimationDrawable animationDrawable = Utils.getFrameAnim(drawableList, true, 150);
  607. ivRecording.setImageDrawable(animationDrawable);
  608. ImageView ivCancel = (ImageView) view.findViewById(R.id.popup_cancel_record);
  609. ImageView ivTooShort = (ImageView) view.findViewById(R.id.popup_record_too_short);
  610. TextView tvState = (TextView) view.findViewById(R.id.popup_record_state);
  611. switch (state) {
  612. case RECORDING: // 正在录音
  613. rlRecording.setVisibility(View.VISIBLE);
  614. // 播放动画
  615. animationDrawable.start();
  616. ivCancel.setVisibility(View.GONE);
  617. ivTooShort.setVisibility(View.GONE);
  618. tvState.setText(getString(R.string.slideup_to_cancel));
  619. break;
  620. case CANCELED: // 取消录音
  621. rlRecording.setVisibility(View.GONE);
  622. // 停止动画
  623. if (animationDrawable.isRunning()) {
  624. animationDrawable.stop();
  625. }
  626. ivCancel.setVisibility(View.VISIBLE);
  627. ivTooShort.setVisibility(View.GONE);
  628. tvState.setText(getString(R.string.loose_to_cancel));
  629. break;
  630. case TOO_SHORT:// 录音时间太短
  631. rlRecording.setVisibility(View.GONE);
  632. // 停止动画
  633. if (animationDrawable.isRunning()) {
  634. animationDrawable.stop();
  635. }
  636. ivCancel.setVisibility(View.GONE);
  637. ivTooShort.setVisibility(View.VISIBLE);
  638. tvState.setText(getString(R.string.record_too_short));
  639. break;
  640. }
  641. }
  642. mRecordPopupWindow.showAtLocation(mRecyclerView, Gravity.CENTER, 0, 0);
  643. }
  644. @OnClick({R.id.chat_send_msg, R.id.chat_intercept})
  645. public void onClick(View view) {
  646. switch (view.getId()) {
  647. case R.id.chat_send_msg:// 发送文字消息
  648. mBtnSend.setEnabled(true);
  649. sendTextMessage();
  650. break;
  651. case R.id.chat_intercept:// 支付拦截
  652. //添加好评送会员
  653. if (mIsShowSendVipByPraise) {
  654. ActionSheetDialogNoTitle();
  655. } else {
  656. BuyServiceActivity.toBuyServiceActivity(ChatActivity.this, BuyServiceActivity.INTENT_FROM_INTERCEPT,
  657. IConfigConstant.PAY_TAG_FROM_CHAT, IConfigConstant.PAY_WAY_ALL);
  658. }
  659. break;
  660. }
  661. }
  662. private void ActionSheetDialogNoTitle() {
  663. final String[] stringItems = {getString(R.string.free_use), getString(R.string.buy_vip_service)};
  664. final ActionSheetDialog dialog = new ActionSheetDialog(ChatActivity.this, stringItems, mTvIntercept);
  665. dialog.isTitleShow(false).show();
  666. dialog.setOnOperItemClickL(new OnOperItemClickL() {
  667. @Override
  668. public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
  669. if (position == 0) {
  670. DialogUtil.showDoubleBtnDialog(getSupportFragmentManager(), getString(R.string.hour12_vip), getString(R.string.send_vip), getString(R.string.go_good), getString(R.string.cancel), false, new OnDoubleDialogClickListener() {
  671. @Override
  672. public void onPositiveClick(View view) {
  673. Intent intent = new Intent();
  674. intent.setAction(Intent.ACTION_VIEW);
  675. intent.setData(Uri.parse("market://details?id=" + VersionInfoUtil.getPackageName(ChatActivity.this)));
  676. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  677. if (VersionInfoUtil.checkAPKExist(ChatActivity.this, "com.android.vending")) {
  678. // 直接跳转Google商店
  679. intent.setPackage("com.android.vending");
  680. }
  681. startActivity(intent);
  682. }
  683. @Override
  684. public void onNegativeClick(View view) {
  685. }
  686. });
  687. } else if (position == 1) {
  688. BuyServiceActivity.toBuyServiceActivity(ChatActivity.this, BuyServiceActivity.INTENT_FROM_INTERCEPT,
  689. IConfigConstant.PAY_TAG_FROM_CHAT, IConfigConstant.PAY_WAY_ALL);
  690. }
  691. dialog.dismiss();
  692. }
  693. });
  694. }
  695. private void loadMsgList() {
  696. OkHttpUtils.post()
  697. .url(IUrlConstant.URL_GET_CHAT_MSG)
  698. .addHeader("token", PlatformInfoXml.getToken())
  699. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  700. .addParams("uid", mUserId)
  701. .build()
  702. .execute(new Callback<MessageList>() {
  703. @Override
  704. public MessageList parseNetworkResponse(Response response, int id) throws Exception {
  705. String resultJson = response.body().string();
  706. if (!TextUtils.isEmpty(resultJson)) {
  707. return JSON.parseObject(resultJson, MessageList.class);
  708. }
  709. return null;
  710. }
  711. @Override
  712. public void onError(Call call, Exception e, int id) {
  713. stopRefresh(3);
  714. }
  715. @Override
  716. public void onResponse(MessageList response, int id) {
  717. if (response != null) {
  718. // 设置支付拦截
  719. String intercept = response.getShowWriteMsgIntercept();
  720. if (!TextUtils.isEmpty(intercept) && "1".equals(intercept)) {
  721. // 展示拦截入口
  722. mIsShowWriteMsgIntercept = true;
  723. mTvIntercept.setVisibility(View.VISIBLE);
  724. mRlChatContainer.setVisibility(View.GONE);
  725. // SharedPreferenceUtil.setBooleanValue(Utils.getContext(), "INTERCEPT_DIALOG", "DIALOG", true);
  726. } else {// 不展示拦截入口
  727. mIsShowWriteMsgIntercept = false;
  728. mTvIntercept.setVisibility(View.GONE);
  729. mRlChatContainer.setVisibility(View.VISIBLE);
  730. // SharedPreferenceUtil.setBooleanValue(Utils.getContext(), "INTERCEPT_DIALOG", "DIALOG", false);
  731. }
  732. mRlChatBottom.setVisibility(View.VISIBLE);
  733. String isSucceed = response.getIsSucceed();
  734. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  735. if (mChatAdapter != null) {
  736. mChatAdapter.setAvatarUrl(mAvatarUrl);
  737. mChatAdapter.setServerSystemTime(response.getSystemTime());
  738. mChatAdapter.isIntercept(mIsShowWriteMsgIntercept);
  739. mChatAdapter.isShowVoiceLoading(false);
  740. mChatAdapter.isShowVoiceRetry(false);
  741. mChatAdapter.isShowImgLoading(false);
  742. mChatAdapter.isShowImgRetry(false);
  743. List<Message> messageList = response.getListMsg();
  744. if (!Util.isListEmpty(messageList)) {
  745. List<Image> imageList = new ArrayList<Image>();
  746. for (Message message : messageList) {
  747. if (message != null) {
  748. Image image = message.getChatImage();
  749. if (image != null) {
  750. imageList.add(image);
  751. }
  752. }
  753. }
  754. mChatAdapter.setAllImageList(imageList);
  755. mChatAdapter.replaceAll(messageList);
  756. // 滚动到底部
  757. mLinearLayoutManager.scrollToPosition(mLinearLayoutManager.getItemCount() - 1);
  758. }
  759. }
  760. }
  761. // 设置五星好评
  762. if (response.getIsShowSendVipByPraise() != null && response.getIsShowSendVipByPraise().equals("1")) {
  763. mIsShowSendVipByPraise = true;
  764. } else {
  765. mIsShowSendVipByPraise = false;
  766. }
  767. }
  768. stopRefresh(1);
  769. }
  770. });
  771. }
  772. private void stopRefresh(int secs) {
  773. new Handler().postDelayed(new Runnable() {
  774. @Override
  775. public void run() {
  776. if (mAutoSwipeRefresh != null) {
  777. mAutoSwipeRefresh.setRefreshing(false);
  778. }
  779. }
  780. }, secs * 1000);
  781. }
  782. /**
  783. * 发送文字消息
  784. */
  785. private void sendTextMessage() {
  786. OkHttpUtils.post()
  787. .url(IUrlConstant.URL_SEND_TEXT_MSG)
  788. .addHeader("token", PlatformInfoXml.getToken())
  789. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  790. .addParams("uid", mUserId)
  791. .addParams("writeMsgType", "1")
  792. .addParams("content", mEtInput.getText().toString().trim())
  793. .build()
  794. .execute(new Callback<WriteMsg>() {
  795. @Override
  796. public WriteMsg parseNetworkResponse(Response response, int id) throws Exception {
  797. String resultJson = response.body().string();
  798. if (!TextUtils.isEmpty(resultJson)) {
  799. return JSON.parseObject(resultJson, WriteMsg.class);
  800. }
  801. mBtnSend.setEnabled(true);
  802. return null;
  803. }
  804. @Override
  805. public void onError(Call call, Exception e, int id) {
  806. ToastUtil.showShortToast(ChatActivity.this, getString(R.string.send_fail));
  807. mBtnSend.setEnabled(true);
  808. }
  809. @Override
  810. public void onResponse(WriteMsg response, int id) {
  811. if (response != null) {
  812. String isSucceed = response.getIsSucceed();
  813. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  814. // 重置状态
  815. mEtInput.setText("");
  816. loadMsgList();
  817. // 发送事件,更新聊天列表
  818. EventBus.getDefault().post(new MessageChangedEvent());
  819. } else {
  820. ToastUtil.showShortToast(ChatActivity.this, getString(R.string.send_fail));
  821. }
  822. } else {
  823. ToastUtil.showShortToast(ChatActivity.this, getString(R.string.send_fail));
  824. }
  825. }
  826. });
  827. }
  828. /**
  829. * 发送语音消息
  830. */
  831. private void sendVoiceMsg() {
  832. // 发送语音前显示要发送的图片进度
  833. final Message message = new Message();
  834. if (mChatAdapter != null) {
  835. mChatAdapter.isShowVoiceLoading(true);
  836. mChatAdapter.isShowVoiceRetry(false);
  837. message.setUid(UserInfoXml.getUID());
  838. message.setMsgType("7");
  839. message.setCreateDateMills(System.currentTimeMillis());
  840. message.setRecevStatus("1");
  841. message.setAudioTime(String.valueOf(Math.round(mRecordDuration / 1000)));
  842. message.setAudioUrl(mOutputPath);
  843. mChatAdapter.insertItem(mChatAdapter.getItemCount(), message);
  844. // 滚动到底部
  845. mLinearLayoutManager.scrollToPosition(mChatAdapter.getItemCount() - 1);
  846. }
  847. OkHttpUtils.post()
  848. .url(IUrlConstant.URL_SEND_VOICE_MSG)
  849. .addHeader("token", PlatformInfoXml.getToken())
  850. .addHeader("platformInfo", PlatformInfoXml.getPlatformJsonString())
  851. .addHeader("uid", mUserId)
  852. .addHeader("writeMsgType", "1")
  853. .addHeader("audioSecond", String.valueOf(Math.round(mRecordDuration / 1000)))
  854. .addFile("file", mRecordFileName, new File(mOutputPath))
  855. .build()
  856. .execute(new Callback<BaseModel>() {
  857. @Override
  858. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  859. String resultJson = response.body().string();
  860. if (!TextUtils.isEmpty(resultJson)) {
  861. return JSON.parseObject(resultJson, BaseModel.class);
  862. }
  863. return null;
  864. }
  865. @Override
  866. public void onError(Call call, Exception e, int id) {
  867. sendVoiceFailed(message);
  868. }
  869. @Override
  870. public void onResponse(BaseModel response, int id) {
  871. if (response != null) {
  872. String isSucceed = response.getIsSucceed();
  873. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  874. loadMsgList();
  875. // 发送事件,更新聊天列表
  876. EventBus.getDefault().post(new MessageChangedEvent());
  877. } else {
  878. sendVoiceFailed(message);
  879. }
  880. } else {
  881. sendVoiceFailed(message);
  882. }
  883. }
  884. });
  885. }
  886. /**
  887. * 发送语音失败,刷新状态
  888. */
  889. private void sendVoiceFailed(Message message) {
  890. mChatAdapter.isShowVoiceLoading(false);
  891. mChatAdapter.isShowVoiceRetry(true);
  892. mChatAdapter.updateItem(mChatAdapter.getItemCount() - 1, message);
  893. ToastUtil.showShortToast(ChatActivity.this, getString(R.string.send_fail));
  894. }
  895. /**
  896. * 发送图片消息
  897. */
  898. private void sendPhotoMsg(File file, final Message message) {
  899. OkHttpUtils.post()
  900. .url(IUrlConstant.URL_SEND_PHOTO_MSG)
  901. .addHeader("token", PlatformInfoXml.getToken())
  902. .addHeader("platformInfo", PlatformInfoXml.getPlatformJsonString())
  903. .addHeader("recevUserId", mUserId)
  904. .addFile("request", file.getName(), file)
  905. .build()
  906. .execute(new Callback<BaseModel>() {
  907. @Override
  908. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  909. String resultJson = response.body().string();
  910. if (!TextUtils.isEmpty(resultJson)) {
  911. return JSON.parseObject(resultJson, BaseModel.class);
  912. }
  913. return null;
  914. }
  915. @Override
  916. public void onError(Call call, Exception e, int id) {
  917. sendPhotoFailed(message);
  918. }
  919. @Override
  920. public void onResponse(BaseModel response, int id) {
  921. if (response != null) {
  922. String isSucceed = response.getIsSucceed();
  923. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  924. loadMsgList();
  925. // 发送事件,更新聊天列表
  926. EventBus.getDefault().post(new MessageChangedEvent());
  927. } else {
  928. sendPhotoFailed(message);
  929. }
  930. } else {
  931. sendPhotoFailed(message);
  932. }
  933. }
  934. });
  935. }
  936. /**
  937. * 发送图片失败,刷新状态
  938. */
  939. private void sendPhotoFailed(Message message) {
  940. mChatAdapter.isShowImgLoading(false);
  941. mChatAdapter.isShowImgRetry(true);
  942. mChatAdapter.updateItem(mChatAdapter.getItemCount() - 1, message);
  943. ToastUtil.showShortToast(ChatActivity.this, getString(R.string.send_fail));
  944. }
  945. @Override
  946. protected void doRegister() {
  947. super.doRegister();
  948. EventBus.getDefault().register(this);
  949. }
  950. @Override
  951. protected void unregister() {
  952. super.unregister();
  953. EventBus.getDefault().unregister(this);
  954. // 释放录音和播放器资源
  955. RecordUtil.getInstance().releaseRecord();
  956. if (RecordUtil.getInstance().isPlaying()) {// 如果正在播放语音,需要停止
  957. RecordUtil.getInstance().stop();
  958. }
  959. RecordUtil.getInstance().release();
  960. }
  961. @Subscribe
  962. public void onEvent(RefreshChatEvent event) {
  963. loadMsgList();
  964. }
  965. }
  966. ///**
  967. // * 聊天页面
  968. // * Created by zhangdroid on 2016/11/2.
  969. // */
  970. //public class ChatActivity extends BaseTitleActivity {
  971. // @BindView(R.id.chat_swiperefresh)
  972. // AutoSwipeRefreshLayout mAutoSwipeRefresh;
  973. // @BindView(R.id.chat_RecyclerView)
  974. // RecyclerView mRecyclerView;
  975. // @BindView(R.id.chat_bottom_bar)
  976. // RelativeLayout mRlChatBottom;
  977. // @BindView(R.id.chat_operate_container)
  978. // RelativeLayout mRlChatContainer;
  979. // @BindView(R.id.chat_et_input)
  980. // ClearableEditText mEtInput;
  981. // @BindView(R.id.chat_send_msg)
  982. // Button mBtnSend;// 发送文字消息
  983. // @BindView(R.id.chat_send_photo)
  984. // ImageView mIvSendPhoto;// 发送图片
  985. // @BindView(R.id.chat_intercept)
  986. // TextView mTvIntercept;
  987. // // 语音
  988. // @BindView(R.id.chat_msg_switch)
  989. // ImageView mIvChatSwitch;
  990. // @BindView(R.id.record)
  991. // Button mBtnRecord;
  992. //
  993. // /**
  994. // * 用户id
  995. // */
  996. // private String mUserId;
  997. // /**
  998. // * 用户昵称
  999. // */
  1000. // private String mNickname;
  1001. // /**
  1002. // * 用户头像url
  1003. // */
  1004. // private String mAvatarUrl;
  1005. //
  1006. // private ChatAdapter mChatAdapter;
  1007. // private LinearLayoutManager mLinearLayoutManager;
  1008. // // 录音框
  1009. // private PopupWindow mRecordPopupWindow;
  1010. // private static final int PERMISSION_CODE_RECORD_AUDIO = 0;
  1011. // private static final int PERMISSION_CODE_WRITE_EXTERNAL_STORAGE = 1;
  1012. // private static final int MSG_TYPE_TIMER = 1;
  1013. //
  1014. // // 录音文件路径
  1015. // private String mOutputPath;
  1016. // // 录音文件名
  1017. // private String mRecordFileName;
  1018. // // 是否正在录音
  1019. // private boolean mIsRecording;
  1020. // // 录音是否被取消
  1021. // private boolean mIsRecordCanceled;
  1022. // // 录音时长(毫秒)
  1023. // private long mRecordDuration;
  1024. // // 录音计时刷新间隔(毫秒)
  1025. // private final long mRefreshInterval = 250;
  1026. // // 计算录音时长的Handler
  1027. // private Handler mTimerHandler = new Handler() {
  1028. //
  1029. // @Override
  1030. // public void handleMessage(android.os.Message msg) {
  1031. // super.handleMessage(msg);
  1032. // switch (msg.what) {
  1033. // case MSG_TYPE_TIMER:
  1034. // if (mIsRecording) {
  1035. // // 计时
  1036. // mRecordDuration += mRefreshInterval;
  1037. // // 录音超过1分钟自动发送
  1038. // if (mRecordDuration > 60 * 1000) {
  1039. // stopRecordAndSend();
  1040. // } else {
  1041. // mTimerHandler.sendEmptyMessageDelayed(MSG_TYPE_TIMER, mRefreshInterval);
  1042. // }
  1043. // }
  1044. // break;
  1045. // }
  1046. // }
  1047. // };
  1048. //
  1049. // // 录音状态常量
  1050. // private enum STATE {
  1051. // /**
  1052. // * 默认值
  1053. // */
  1054. // IDLE,
  1055. // /**
  1056. // * 录音中
  1057. // */
  1058. // RECORDING,
  1059. // /**
  1060. // * 取消
  1061. // */
  1062. // CANCELED,
  1063. // /**
  1064. // * 录音时间太短
  1065. // */
  1066. // TOO_SHORT
  1067. // }
  1068. //
  1069. // private boolean mIsShowSendVipByPraise = false;
  1070. // private boolean mIsShowWriteMsgIntercept = false;
  1071. //
  1072. // private static final String INTENT_KEY_UID = "key_userId";
  1073. // private static final String INTENT_KEY_NICKNAME = "key_nickname";
  1074. // private static final String INTENT_KEY_AVATAR_URL = "key_avatar_url";
  1075. // private static final String INTENT_KEY_NEED_REFRESH = "key_need_refresh";
  1076. //
  1077. // /**
  1078. // * 跳转聊天页
  1079. // *
  1080. // * @param from
  1081. // * @param userId 用户id
  1082. // * @param nickname 用户昵称
  1083. // * @param avatarUrl 用户头像url
  1084. // */
  1085. // public static void toChatActivity(Activity from, String userId, String nickname, String avatarUrl) {
  1086. // Map<String, String> map = new HashMap<String, String>();
  1087. // map.put(INTENT_KEY_UID, userId);
  1088. // map.put(INTENT_KEY_NICKNAME, nickname);
  1089. // map.put(INTENT_KEY_AVATAR_URL, avatarUrl);
  1090. // Util.gotoActivity(from, ChatActivity.class, false, map);
  1091. // }
  1092. //
  1093. // /**
  1094. // * 跳转聊天页
  1095. // *
  1096. // * @param context
  1097. // * @param userId 用户id
  1098. // * @param nickname 用户昵称
  1099. // * @param avatarUrl 用户头像url
  1100. // */
  1101. // public static void toChatActivity(Context context, String userId, String nickname, String avatarUrl) {
  1102. // Intent intent = new Intent(context, ChatActivity.class);
  1103. // intent.putExtra(INTENT_KEY_UID, userId);
  1104. // intent.putExtra(INTENT_KEY_NICKNAME, nickname);
  1105. // intent.putExtra(INTENT_KEY_AVATAR_URL, avatarUrl);
  1106. // context.startActivity(intent);
  1107. // }
  1108. //
  1109. // // 数据库相关
  1110. // private MessageDao mMessageDao;
  1111. // private MyHandler mMyHandler;
  1112. //
  1113. // private static final int MSG_TYPE_DB = 0;
  1114. // private static final int MSG_TYPE_NET = 1;
  1115. //
  1116. // private static class MyHandler extends Handler {
  1117. // private WeakReference<ChatActivity> mChatActivity;
  1118. //
  1119. // public MyHandler(ChatActivity chatActivity) {
  1120. // this.mChatActivity = new WeakReference<ChatActivity>(chatActivity);
  1121. // }
  1122. //
  1123. // @Override
  1124. // public void handleMessage(android.os.Message msg) {
  1125. // super.handleMessage(msg);
  1126. // final ChatActivity chatActivity = mChatActivity.get();
  1127. // switch (msg.what) {
  1128. // case MSG_TYPE_DB:
  1129. // LogUtil.e(LogUtil.TAG_ZL, "load chat history from db.");
  1130. // if (chatActivity != null) {
  1131. // chatActivity.setAdapterData((List<Message>) msg.obj);
  1132. // chatActivity.stopRefresh(1);
  1133. // }
  1134. // break;
  1135. //
  1136. // case MSG_TYPE_NET:
  1137. // LogUtil.e(LogUtil.TAG_ZL, "load chat history from net.");
  1138. // if (chatActivity != null) {
  1139. // chatActivity.loadMsgList();
  1140. // }
  1141. // break;
  1142. // }
  1143. // }
  1144. // }
  1145. //
  1146. // @Override
  1147. // protected int getLayoutResId() {
  1148. // return R.layout.activity_chat;
  1149. // }
  1150. //
  1151. // @Override
  1152. // protected String getCenterTitle() {
  1153. // return null;
  1154. // }
  1155. //
  1156. // @Override
  1157. // protected void initViewsAndVariables() {
  1158. // Intent intent = getIntent();
  1159. // if (intent != null) {
  1160. // mUserId = intent.getStringExtra(INTENT_KEY_UID);
  1161. // mNickname = intent.getStringExtra(INTENT_KEY_NICKNAME);
  1162. // mAvatarUrl = intent.getStringExtra(INTENT_KEY_AVATAR_URL);
  1163. // }
  1164. // if (!TextUtils.isEmpty(mNickname)) {
  1165. // setTitle(mNickname);
  1166. // }
  1167. //
  1168. // // 设置下拉刷新动画样式
  1169. // mAutoSwipeRefresh.setColorSchemeResources(R.color.main_color);
  1170. // mAutoSwipeRefresh.setProgressBackgroundColorSchemeColor(Color.WHITE);
  1171. // mLinearLayoutManager = new LinearLayoutManager(this);
  1172. // mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
  1173. // mRecyclerView.setLayoutManager(mLinearLayoutManager);
  1174. // mChatAdapter = new ChatAdapter(this, -1);
  1175. // mChatAdapter.setUserId(mUserId);
  1176. // mChatAdapter.setAvatarUrl(mAvatarUrl);
  1177. // mRecyclerView.setAdapter(mChatAdapter);
  1178. //
  1179. // // 获得数据库操作对象
  1180. // mMessageDao = GreenDaoUtil.getDaoSession().getMessageDao();
  1181. // mMyHandler = new MyHandler(this);
  1182. // }
  1183. //
  1184. // /**
  1185. // * 先从本地数据库查询,若没有则从网络获取
  1186. // */
  1187. // private void loadChatHistory() {
  1188. // new Thread(new Runnable() {
  1189. // @Override
  1190. // public void run() {
  1191. // // 查询与当前用户的所有聊天记录
  1192. // QueryBuilder queryBuilder = mMessageDao.queryBuilder();
  1193. // queryBuilder.where(MessageDao.Properties.Uid.eq(mUserId), MessageDao.Properties.RecevUserId.eq(UserInfoXml.getUID()))
  1194. // .or(MessageDao.Properties.Uid.eq(UserInfoXml.getUID()), MessageDao.Properties.RecevUserId.eq(mUserId));
  1195. // List<Message> dbList = queryBuilder.list();
  1196. // if (!Util.isListEmpty(dbList)) {
  1197. // // 数据库中存在聊天记录,直接显示
  1198. // android.os.Message message = android.os.Message.obtain();
  1199. // message.what = MSG_TYPE_DB;
  1200. // message.obj = dbList;
  1201. // mMyHandler.sendMessage(message);
  1202. // } else {
  1203. // // 否则从网络加载
  1204. // mMyHandler.sendEmptyMessage(MSG_TYPE_NET);
  1205. // }
  1206. // }
  1207. // }).start();
  1208. // }
  1209. //
  1210. // /**
  1211. // * 从网络加载聊天记录
  1212. // */
  1213. // private void loadMsgList() {
  1214. // OkHttpUtils.post()
  1215. // .url(IUrlConstant.URL_GET_CHAT_MSG)
  1216. // .addHeader("token", PlatformInfoXml.getToken())
  1217. // .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  1218. // .addParams("uid", mUserId)
  1219. // .addParams("startTime", "")
  1220. // .build()
  1221. // .execute(new Callback<MessageList>() {
  1222. //
  1223. // @Override
  1224. // public MessageList parseNetworkResponse(Response response, int id) throws Exception {
  1225. // String resultJson = response.body().string();
  1226. // if (!TextUtils.isEmpty(resultJson)) {
  1227. // return JSON.parseObject(resultJson, MessageList.class);
  1228. // }
  1229. // return null;
  1230. // }
  1231. //
  1232. // @Override
  1233. // public void onError(Call call, Exception e, int id) {
  1234. // stopRefresh(3);
  1235. // }
  1236. //
  1237. // @Override
  1238. // public void onResponse(MessageList response, int id) {
  1239. // if (response != null) {
  1240. // // 设置支付拦截
  1241. // String intercept = response.getShowWriteMsgIntercept();
  1242. // if (!TextUtils.isEmpty(intercept) && "1".equals(intercept)) {
  1243. // // 展示拦截入口
  1244. // mIsShowWriteMsgIntercept = true;
  1245. // mTvIntercept.setVisibility(View.VISIBLE);
  1246. // mRlChatContainer.setVisibility(View.GONE);
  1247. // } else {// 不展示拦截入口
  1248. // mIsShowWriteMsgIntercept = false;
  1249. // mTvIntercept.setVisibility(View.GONE);
  1250. // mRlChatContainer.setVisibility(View.VISIBLE);
  1251. // }
  1252. // // 设置五星好评
  1253. // mIsShowSendVipByPraise = response.getIsShowSendVipByPraise() != null && response.getIsShowSendVipByPraise().equals("1");
  1254. // // 更新聊天列表并存入数据库
  1255. // String isSucceed = response.getIsSucceed();
  1256. // if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  1257. // List<Message> messageList = response.getListMsg();
  1258. // if (!Util.isListEmpty(messageList)) {
  1259. // setAdapterData(messageList);
  1260. // insert(messageList);
  1261. // }
  1262. // }
  1263. // }
  1264. // stopRefresh(1);
  1265. // }
  1266. // });
  1267. // }
  1268. //
  1269. // /**
  1270. // * 向数据库中插入新增的聊天记录
  1271. // *
  1272. // * @param messageList
  1273. // */
  1274. // private void insert(final List<Message> messageList) {
  1275. // new Thread(new Runnable() {
  1276. // @Override
  1277. // public void run() {
  1278. // // 批量插入
  1279. // mMessageDao.insertOrReplaceInTx(messageList);
  1280. // }
  1281. // }).start();
  1282. // }
  1283. //
  1284. // private void setAdapterData(List<Message> messageList) {
  1285. // if (mChatAdapter != null && !Util.isListEmpty(messageList)) {
  1286. // // 查找聊天记录中所有图片
  1287. // List<Image> imageList = new ArrayList<Image>();
  1288. // for (Message message : messageList) {
  1289. // if (message != null) {
  1290. // Image image = message.getChatImage();
  1291. // if (image != null) {
  1292. // imageList.add(image);
  1293. // }
  1294. // }
  1295. // }
  1296. // mChatAdapter.setAllImageList(imageList);
  1297. // mChatAdapter.replaceAll(messageList);
  1298. // // 滚动到底部
  1299. // mLinearLayoutManager.scrollToPosition(mLinearLayoutManager.getItemCount() - 1);
  1300. // }
  1301. // }
  1302. //
  1303. // private void stopRefresh(int secs) {
  1304. // new Handler().postDelayed(new Runnable() {
  1305. // @Override
  1306. // public void run() {
  1307. // if (mAutoSwipeRefresh != null) {
  1308. // mAutoSwipeRefresh.setRefreshing(false);
  1309. // }
  1310. // }
  1311. // }, secs * 1000);
  1312. // }
  1313. //
  1314. // @Override
  1315. // protected void addListeners() {
  1316. // mAutoSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  1317. // @Override
  1318. // public void onRefresh() {
  1319. // loadChatHistory();
  1320. // }
  1321. // });
  1322. // // 处理滑动冲突
  1323. // mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
  1324. // @Override
  1325. // public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
  1326. // super.onScrollStateChanged(recyclerView, newState);
  1327. // }
  1328. //
  1329. // @Override
  1330. // public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
  1331. // super.onScrolled(recyclerView, dx, dy);
  1332. // // 判断是否滑动到顶部
  1333. // int scrolledPosition = (mRecyclerView == null || mRecyclerView.getChildCount() == 0) ? 0 : mRecyclerView.getChildAt(0).getTop();
  1334. // mAutoSwipeRefresh.setEnabled(scrolledPosition >= 0);
  1335. // }
  1336. // });
  1337. // // 发送文字消息
  1338. // mEtInput.setTextWatcherCallback(new ClearableEditText.TextWatcherCallback() {
  1339. //
  1340. // @Override
  1341. // public void handleTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
  1342. // if (mEtInput.length() > 0) {
  1343. // mBtnSend.setVisibility(View.VISIBLE);
  1344. // mIvSendPhoto.setVisibility(View.GONE);
  1345. // } else {
  1346. // mBtnSend.setVisibility(View.GONE);
  1347. // mIvSendPhoto.setVisibility(View.VISIBLE);
  1348. // }
  1349. // }
  1350. // });
  1351. // // 切换文字/语音
  1352. // mIvChatSwitch.setOnClickListener(new View.OnClickListener() {
  1353. // @Override
  1354. // public void onClick(View v) {
  1355. // mIvChatSwitch.setSelected(!mIvChatSwitch.isSelected());
  1356. // if (mIvChatSwitch.isSelected()) { // 显示发送语音
  1357. // mBtnRecord.setText(getString(R.string.press_to_speak));
  1358. // mBtnRecord.setVisibility(View.VISIBLE);
  1359. // mEtInput.setVisibility(View.GONE);
  1360. // mBtnSend.setVisibility(View.GONE);
  1361. // mIvSendPhoto.setVisibility(View.GONE);
  1362. // if (mIsShowWriteMsgIntercept) {// 支付拦截时不可用
  1363. // mBtnRecord.setClickable(false);
  1364. // mBtnRecord.setEnabled(false);
  1365. // mBtnRecord.setTextColor(getResources().getColor(R.color.textcolor_light_gray));
  1366. // } else {
  1367. // mBtnRecord.setClickable(true);
  1368. // mBtnRecord.setEnabled(true);
  1369. // mBtnRecord.setTextColor(getResources().getColor(R.color.main_color));
  1370. // }
  1371. // } else { // 显示输入框
  1372. // mBtnRecord.setVisibility(View.GONE);
  1373. // mEtInput.setVisibility(View.VISIBLE);
  1374. // if (mEtInput.getText().length() > 0) {
  1375. // // 已经输入文字
  1376. // mBtnSend.setVisibility(View.VISIBLE);
  1377. // mIvSendPhoto.setVisibility(View.GONE);
  1378. // } else {
  1379. // mBtnSend.setVisibility(View.GONE);
  1380. // mIvSendPhoto.setVisibility(View.VISIBLE);
  1381. // }
  1382. // }
  1383. // }
  1384. // });
  1385. // // 发送语音消息
  1386. // mBtnRecord.setOnTouchListener(new View.OnTouchListener() {
  1387. // @Override
  1388. // public boolean onTouch(View v, MotionEvent event) {
  1389. // // 检查权限
  1390. // if (!checkPermissions()) {// 请求权限时不录音
  1391. // switch (event.getAction()) {
  1392. // case MotionEvent.ACTION_DOWN:// 按下时开始录音
  1393. // mBtnRecord.setText(getString(R.string.loose_to_end));
  1394. // showRecordPopupWindow(STATE.RECORDING);
  1395. // startRecord();
  1396. // mIsRecordCanceled = false;
  1397. // LogUtil.e(LogUtil.TAG_ZL, "ACTION_DOWN start()");
  1398. // break;
  1399. //
  1400. // case MotionEvent.ACTION_MOVE:
  1401. // if (event.getY() < -100) { // 上滑取消发送
  1402. // mIsRecordCanceled = true;
  1403. // showRecordPopupWindow(STATE.CANCELED);
  1404. // } else {
  1405. // mIsRecordCanceled = false;
  1406. // showRecordPopupWindow(STATE.RECORDING);
  1407. // }
  1408. // LogUtil.e(LogUtil.TAG_ZL, "ACTION_MOVE " + event.getY() + " mIsRecordCanceled = " + mIsRecordCanceled);
  1409. // break;
  1410. //
  1411. // case MotionEvent.ACTION_UP:// 松开时停止录音并发送
  1412. // mBtnRecord.setText(getString(R.string.press_to_speak));
  1413. // if (mIsRecordCanceled) {
  1414. // cancelRecord();
  1415. // LogUtil.e(LogUtil.TAG_ZL, "ACTION_UP canceld()");
  1416. // } else {
  1417. // stopRecordAndSend();
  1418. // LogUtil.e(LogUtil.TAG_ZL, "ACTION_UP stop()");
  1419. // }
  1420. // break;
  1421. // }
  1422. // }
  1423. // return true;
  1424. // }
  1425. // });
  1426. // // 发送图片
  1427. // mIvSendPhoto.setOnClickListener(new View.OnClickListener() {
  1428. // @Override
  1429. // public void onClick(View v) {
  1430. // GetPhotoActivity.toGetPhotoActivity(ChatActivity.this, IConfigConstant.LOCAL_PIC_PATH, new GetPhotoActivity.OnGetPhotoListener() {
  1431. // @Override
  1432. // public void getSelectedPhoto(File file) {
  1433. // if (file != null) {
  1434. // // 发送图片前显示要发送的图片进度
  1435. // Message message = new Message();
  1436. // if (mChatAdapter != null) {
  1437. // mChatAdapter.isShowImgLoading(true);
  1438. // mChatAdapter.isShowImgRetry(false);
  1439. // message.setUid(UserInfoXml.getUID());
  1440. // message.setMsgType("10");
  1441. // message.setCreateDateMills(System.currentTimeMillis());
  1442. // Image image = new Image();
  1443. // image.setIsMain(2);
  1444. // image.setThumbnailUrl(file.getAbsolutePath());
  1445. // image.setThumbnailUrlM(file.getAbsolutePath());
  1446. // image.setImageUrl(file.getAbsolutePath());
  1447. // message.setChatImage(image);
  1448. // mChatAdapter.insertItem(mChatAdapter.getItemCount(), message);
  1449. // // 滚动到底部
  1450. // mLinearLayoutManager.scrollToPosition(mChatAdapter.getItemCount() - 1);
  1451. // }
  1452. // sendPhotoMsg(file, message);
  1453. // }
  1454. // }
  1455. // });
  1456. // }
  1457. // });
  1458. // }
  1459. //
  1460. // private boolean checkPermissions() {
  1461. // boolean needRequestPermission = false;
  1462. // if (ContextCompat.checkSelfPermission(ChatActivity.this.getApplicationContext(),
  1463. // Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_DENIED) {
  1464. // needRequestPermission = true;
  1465. // // 请求android.permission.RECORD_AUDIO权限
  1466. // ActivityCompat.requestPermissions(ChatActivity.this, new String[]{Manifest.permission.RECORD_AUDIO}, PERMISSION_CODE_RECORD_AUDIO);
  1467. // }
  1468. // if (ContextCompat.checkSelfPermission(ChatActivity.this.getApplicationContext(),
  1469. // Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
  1470. // needRequestPermission = true;
  1471. // // 请求android.permission.WRITE_EXTERNAL_STORAGE权限
  1472. // ActivityCompat.requestPermissions(ChatActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_CODE_WRITE_EXTERNAL_STORAGE);
  1473. // }
  1474. // return needRequestPermission;
  1475. // }
  1476. //
  1477. // @Override
  1478. // public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  1479. // super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  1480. // }
  1481. //
  1482. // /**
  1483. // * 开始录音
  1484. // */
  1485. // private void startRecord() {
  1486. // // 录音开始前震动一下
  1487. // Vibrator vibrator = (Vibrator) ChatActivity.this.getSystemService(Context.VIBRATOR_SERVICE);
  1488. // vibrator.vibrate(88);
  1489. // // 根据系统时间生成文件名
  1490. // mRecordFileName = FileUtil.createFileNameByTime() + ".mp3";
  1491. // // 录音文件保存路径: 根目录/DateLove/record/用户id/xxx.mp3
  1492. // mOutputPath = FileUtil.RECORD_DIRECTORY_PATH + File.separator + UserInfoXml.getUID() + File.separator + mRecordFileName;
  1493. // LogUtil.e(LogUtil.TAG_ZL, "录音存放路径:" + mOutputPath);
  1494. // // 开始录音
  1495. // RecordUtil.getInstance().startRecord(mOutputPath);
  1496. // mIsRecording = true;
  1497. // mRecordDuration = 0;
  1498. // // 计时,间隔250毫秒刷新
  1499. // mTimerHandler.sendEmptyMessageDelayed(MSG_TYPE_TIMER, mRefreshInterval);
  1500. // }
  1501. //
  1502. // /**
  1503. // * 停止录音并发送
  1504. // */
  1505. // private void stopRecordAndSend() {
  1506. // if (mIsRecording) {
  1507. // mIsRecording = false;
  1508. // LogUtil.e(LogUtil.TAG_ZL, "录音时长(毫秒):" + mRecordDuration);
  1509. // if (mRecordDuration < 1 * 1000) {// 录音时长小于1秒的不发送
  1510. // showRecordPopupWindow(STATE.TOO_SHORT);
  1511. // new Handler().postDelayed(new Runnable() {
  1512. // @Override
  1513. // public void run() {
  1514. // // 延时500毫秒调用MediaRecorder#stop(),防止出现start()之后立马调用stop()的异常
  1515. // RecordUtil.getInstance().stopRecord();
  1516. // // 删除小于1秒的文件
  1517. // FileUtil.deleteFile(mOutputPath);
  1518. // }
  1519. // }, 500);
  1520. // } else {// 发送语音
  1521. // RecordUtil.getInstance().stopRecord();
  1522. // sendVoiceMsg();
  1523. // }
  1524. // // 隐藏录音框
  1525. // new Handler().postDelayed(new Runnable() {
  1526. // @Override
  1527. // public void run() {
  1528. // if (mRecordPopupWindow != null && mRecordPopupWindow.isShowing()) {
  1529. // mRecordPopupWindow.dismiss();
  1530. // }
  1531. // }
  1532. // }, 500);
  1533. // }
  1534. // }
  1535. //
  1536. // /**
  1537. // * 录音取消,删除已经录制的文件
  1538. // */
  1539. // private void cancelRecord() {
  1540. // if (mIsRecordCanceled) {
  1541. // RecordUtil.getInstance().stopRecord();
  1542. // mIsRecordCanceled = false;
  1543. // FileUtil.deleteFile(mOutputPath);
  1544. //
  1545. // // 隐藏录音框
  1546. // new Handler().postDelayed(new Runnable() {
  1547. // @Override
  1548. // public void run() {
  1549. // if (mRecordPopupWindow != null && mRecordPopupWindow.isShowing()) {
  1550. // mRecordPopupWindow.dismiss();
  1551. // }
  1552. // }
  1553. // }, 500);
  1554. // }
  1555. // }
  1556. //
  1557. // /**
  1558. // * 弹出录音框
  1559. // */
  1560. // private void showRecordPopupWindow(STATE state) {
  1561. // if (mRecordPopupWindow == null) {
  1562. // View contentView = LayoutInflater.from(ChatActivity.this).inflate(R.layout.popup_chat_record, null);
  1563. // mRecordPopupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.WRAP_CONTENT,
  1564. // WindowManager.LayoutParams.WRAP_CONTENT, true);
  1565. // mRecordPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  1566. // }
  1567. //
  1568. // View view = mRecordPopupWindow.getContentView();
  1569. // if (view != null) {
  1570. // RelativeLayout rlRecording = (RelativeLayout) view.findViewById(R.id.popup_recording_container);
  1571. // ImageView ivRecording = (ImageView) view.findViewById(R.id.popup_record_anim);
  1572. // // 设置动画
  1573. // List<Drawable> drawableList = new ArrayList<Drawable>();
  1574. // drawableList.add(getResources().getDrawable(R.drawable.voice1));
  1575. // drawableList.add(getResources().getDrawable(R.drawable.voice2));
  1576. // drawableList.add(getResources().getDrawable(R.drawable.voice3));
  1577. // drawableList.add(getResources().getDrawable(R.drawable.voice4));
  1578. // drawableList.add(getResources().getDrawable(R.drawable.voice5));
  1579. // drawableList.add(getResources().getDrawable(R.drawable.voice6));
  1580. // AnimationDrawable animationDrawable = Utils.getFrameAnim(drawableList, true, 150);
  1581. // ivRecording.setImageDrawable(animationDrawable);
  1582. //
  1583. // ImageView ivCancel = (ImageView) view.findViewById(R.id.popup_cancel_record);
  1584. // ImageView ivTooShort = (ImageView) view.findViewById(R.id.popup_record_too_short);
  1585. // TextView tvState = (TextView) view.findViewById(R.id.popup_record_state);
  1586. // switch (state) {
  1587. // case RECORDING: // 正在录音
  1588. // rlRecording.setVisibility(View.VISIBLE);
  1589. // // 播放动画
  1590. // animationDrawable.start();
  1591. // ivCancel.setVisibility(View.GONE);
  1592. // ivTooShort.setVisibility(View.GONE);
  1593. // tvState.setText(getString(R.string.slideup_to_cancel));
  1594. // break;
  1595. //
  1596. // case CANCELED: // 取消录音
  1597. // rlRecording.setVisibility(View.GONE);
  1598. // // 停止动画
  1599. // if (animationDrawable.isRunning()) {
  1600. // animationDrawable.stop();
  1601. // }
  1602. // ivCancel.setVisibility(View.VISIBLE);
  1603. // ivTooShort.setVisibility(View.GONE);
  1604. // tvState.setText(getString(R.string.loose_to_cancel));
  1605. // break;
  1606. //
  1607. // case TOO_SHORT:// 录音时间太短
  1608. // rlRecording.setVisibility(View.GONE);
  1609. // // 停止动画
  1610. // if (animationDrawable.isRunning()) {
  1611. // animationDrawable.stop();
  1612. // }
  1613. // ivCancel.setVisibility(View.GONE);
  1614. // ivTooShort.setVisibility(View.VISIBLE);
  1615. // tvState.setText(getString(R.string.record_too_short));
  1616. // break;
  1617. // }
  1618. // }
  1619. // mRecordPopupWindow.showAtLocation(mRecyclerView, Gravity.CENTER, 0, 0);
  1620. // }
  1621. //
  1622. // @OnClick({R.id.chat_send_msg, R.id.chat_intercept})
  1623. // public void onClick(View view) {
  1624. // switch (view.getId()) {
  1625. // case R.id.chat_send_msg:// 发送文字消息
  1626. // mBtnSend.setEnabled(false);
  1627. // sendTextMessage();
  1628. // break;
  1629. //
  1630. // case R.id.chat_intercept:// 支付拦截
  1631. // //添加好评送会员
  1632. // if (mIsShowSendVipByPraise) {
  1633. // ActionSheetDialogNoTitle();
  1634. // } else {
  1635. // BuyServiceActivity.toBuyServiceActivity(ChatActivity.this, BuyServiceActivity.INTENT_FROM_INTERCEPT,
  1636. // IConfigConstant.PAY_TAG_FROM_CHAT, IConfigConstant.PAY_WAY_ALL);
  1637. // }
  1638. // break;
  1639. // }
  1640. // }
  1641. //
  1642. // private void ActionSheetDialogNoTitle() {
  1643. // final String[] stringItems = {getString(R.string.free_use), getString(R.string.buy_vip_service)};
  1644. // final ActionSheetDialog dialog = new ActionSheetDialog(ChatActivity.this, stringItems, mTvIntercept);
  1645. // dialog.isTitleShow(false).show();
  1646. //
  1647. // dialog.setOnOperItemClickL(new OnOperItemClickL() {
  1648. // @Override
  1649. // public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
  1650. // if (position == 0) {
  1651. // DialogUtil.showDoubleBtnDialog(getSupportFragmentManager(), getString(R.string.hour12_vip), getString(R.string.send_vip), getString(R.string.go_good), getString(R.string.cancel), false, new OnDoubleDialogClickListener() {
  1652. // @Override
  1653. // public void onPositiveClick(View view) {
  1654. // Intent intent = new Intent();
  1655. // intent.setAction(Intent.ACTION_VIEW);
  1656. // intent.setData(Uri.parse("market://details?id=" + VersionInfoUtil.getPackageName(ChatActivity.this)));
  1657. // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  1658. // if (VersionInfoUtil.checkAPKExist(ChatActivity.this, "com.android.vending")) {
  1659. // // 直接跳转Google商店
  1660. // intent.setPackage("com.android.vending");
  1661. // }
  1662. // startActivity(intent);
  1663. // }
  1664. //
  1665. // @Override
  1666. // public void onNegativeClick(View view) {
  1667. //
  1668. // }
  1669. // });
  1670. // } else if (position == 1) {
  1671. // BuyServiceActivity.toBuyServiceActivity(ChatActivity.this, BuyServiceActivity.INTENT_FROM_INTERCEPT,
  1672. // IConfigConstant.PAY_TAG_FROM_CHAT, IConfigConstant.PAY_WAY_ALL);
  1673. // }
  1674. // dialog.dismiss();
  1675. // }
  1676. // });
  1677. // }
  1678. //
  1679. // /**
  1680. // * 发送文字消息
  1681. // */
  1682. // private void sendTextMessage() {
  1683. // OkHttpUtils.post()
  1684. // .url(IUrlConstant.URL_SEND_TEXT_MSG)
  1685. // .addHeader("token", PlatformInfoXml.getToken())
  1686. // .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  1687. // .addParams("uid", mUserId)
  1688. // .addParams("writeMsgType", "1")
  1689. // .addParams("content", mEtInput.getText().toString().trim())
  1690. // .build()
  1691. // .execute(new Callback<WriteMsg>() {
  1692. // @Override
  1693. // public WriteMsg parseNetworkResponse(Response response, int id) throws Exception {
  1694. // String resultJson = response.body().string();
  1695. // if (!TextUtils.isEmpty(resultJson)) {
  1696. // return JSON.parseObject(resultJson, WriteMsg.class);
  1697. // }
  1698. // return null;
  1699. // }
  1700. //
  1701. // @Override
  1702. // public void onError(Call call, Exception e, int id) {
  1703. // ToastUtil.showShortToast(ChatActivity.this, getString(R.string.send_fail));
  1704. // mBtnSend.setEnabled(true);
  1705. // }
  1706. //
  1707. // @Override
  1708. // public void onResponse(WriteMsg response, int id) {
  1709. // if (response != null) {
  1710. // String isSucceed = response.getIsSucceed();
  1711. // if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  1712. // // 重置状态
  1713. // mEtInput.setText("");
  1714. // loadMsgList();
  1715. // // 发送事件,更新聊天列表
  1716. // EventBus.getDefault().post(new MessageChangedEvent());
  1717. // } else {
  1718. // ToastUtil.showShortToast(ChatActivity.this, getString(R.string.send_fail));
  1719. // }
  1720. // } else {
  1721. // ToastUtil.showShortToast(ChatActivity.this, getString(R.string.send_fail));
  1722. // }
  1723. // mBtnSend.setEnabled(true);
  1724. // }
  1725. // });
  1726. // }
  1727. //
  1728. // /**
  1729. // * 发送语音消息
  1730. // */
  1731. // private void sendVoiceMsg() {
  1732. // // 发送语音前显示要发送的图片进度
  1733. // final Message message = new Message();
  1734. // if (mChatAdapter != null) {
  1735. // mChatAdapter.isShowVoiceLoading(true);
  1736. // mChatAdapter.isShowVoiceRetry(false);
  1737. // message.setUid(UserInfoXml.getUID());
  1738. // message.setMsgType("7");
  1739. // message.setCreateDateMills(System.currentTimeMillis());
  1740. // message.setRecevStatus("1");
  1741. // message.setAudioTime(String.valueOf(Math.round(mRecordDuration / 1000)));
  1742. // message.setAudioUrl(mOutputPath);
  1743. // mChatAdapter.insertItem(mChatAdapter.getItemCount(), message);
  1744. // // 滚动到底部
  1745. // mLinearLayoutManager.scrollToPosition(mChatAdapter.getItemCount() - 1);
  1746. // }
  1747. // OkHttpUtils.post()
  1748. // .url(IUrlConstant.URL_SEND_VOICE_MSG)
  1749. // .addHeader("token", PlatformInfoXml.getToken())
  1750. // .addHeader("platformInfo", PlatformInfoXml.getPlatformJsonString())
  1751. // .addHeader("uid", mUserId)
  1752. // .addHeader("writeMsgType", "1")
  1753. // .addHeader("audioSecond", String.valueOf(Math.round(mRecordDuration / 1000)))
  1754. // .addFile("file", mRecordFileName, new File(mOutputPath))
  1755. // .build()
  1756. // .execute(new Callback<BaseModel>() {
  1757. // @Override
  1758. // public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  1759. // String resultJson = response.body().string();
  1760. // if (!TextUtils.isEmpty(resultJson)) {
  1761. // return JSON.parseObject(resultJson, BaseModel.class);
  1762. // }
  1763. // return null;
  1764. // }
  1765. //
  1766. // @Override
  1767. // public void onError(Call call, Exception e, int id) {
  1768. // sendVoiceFailed(message);
  1769. // }
  1770. //
  1771. // @Override
  1772. // public void onResponse(BaseModel response, int id) {
  1773. // if (response != null) {
  1774. // String isSucceed = response.getIsSucceed();
  1775. // if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  1776. // loadMsgList();
  1777. // // 发送事件,更新聊天列表
  1778. // EventBus.getDefault().post(new MessageChangedEvent());
  1779. // } else {
  1780. // sendVoiceFailed(message);
  1781. // }
  1782. // } else {
  1783. // sendVoiceFailed(message);
  1784. // }
  1785. // }
  1786. // });
  1787. // }
  1788. //
  1789. // /**
  1790. // * 发送语音失败,刷新状态
  1791. // */
  1792. // private void sendVoiceFailed(Message message) {
  1793. // mChatAdapter.isShowVoiceLoading(false);
  1794. // mChatAdapter.isShowVoiceRetry(true);
  1795. // mChatAdapter.updateItem(mChatAdapter.getItemCount() - 1, message);
  1796. // ToastUtil.showShortToast(ChatActivity.this, getString(R.string.send_fail));
  1797. // }
  1798. //
  1799. // /**
  1800. // * 发送图片消息
  1801. // */
  1802. // private void sendPhotoMsg(File file, final Message message) {
  1803. // OkHttpUtils.post()
  1804. // .url(IUrlConstant.URL_SEND_PHOTO_MSG)
  1805. // .addHeader("token", PlatformInfoXml.getToken())
  1806. // .addHeader("platformInfo", PlatformInfoXml.getPlatformJsonString())
  1807. // .addHeader("recevUserId", mUserId)
  1808. // .addFile("request", file.getName(), file)
  1809. // .build()
  1810. // .execute(new Callback<BaseModel>() {
  1811. // @Override
  1812. // public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  1813. // String resultJson = response.body().string();
  1814. // if (!TextUtils.isEmpty(resultJson)) {
  1815. // return JSON.parseObject(resultJson, BaseModel.class);
  1816. // }
  1817. // return null;
  1818. // }
  1819. //
  1820. // @Override
  1821. // public void onError(Call call, Exception e, int id) {
  1822. // sendPhotoFailed(message);
  1823. // }
  1824. //
  1825. // @Override
  1826. // public void onResponse(BaseModel response, int id) {
  1827. // if (response != null) {
  1828. // String isSucceed = response.getIsSucceed();
  1829. // if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  1830. // loadMsgList();
  1831. // // 发送事件,更新聊天列表
  1832. // EventBus.getDefault().post(new MessageChangedEvent());
  1833. // } else {
  1834. // sendPhotoFailed(message);
  1835. // }
  1836. // } else {
  1837. // sendPhotoFailed(message);
  1838. // }
  1839. // }
  1840. // });
  1841. // }
  1842. //
  1843. // /**
  1844. // * 发送图片失败,刷新状态
  1845. // */
  1846. // private void sendPhotoFailed(Message message) {
  1847. // mChatAdapter.isShowImgLoading(false);
  1848. // mChatAdapter.isShowImgRetry(true);
  1849. // mChatAdapter.updateItem(mChatAdapter.getItemCount() - 1, message);
  1850. // ToastUtil.showShortToast(ChatActivity.this, getString(R.string.send_fail));
  1851. // }
  1852. //
  1853. // @Override
  1854. // protected void doRegister() {
  1855. // super.doRegister();
  1856. // EventBus.getDefault().register(this);
  1857. // }
  1858. //
  1859. // @Override
  1860. // protected void unregister() {
  1861. // super.unregister();
  1862. // EventBus.getDefault().unregister(this);
  1863. // // 释放录音和播放器资源
  1864. // RecordUtil.getInstance().releaseRecord();
  1865. // if (RecordUtil.getInstance().isPlaying()) {// 如果正在播放语音,需要停止
  1866. // RecordUtil.getInstance().stop();
  1867. // }
  1868. // RecordUtil.getInstance().release();
  1869. // }
  1870. //
  1871. // @Subscribe
  1872. // public void onEvent(RefreshChatEvent event) {
  1873. // loadMsgList();
  1874. // }
  1875. //
  1876. //}