ChatActivity.java 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. package jiguang.chat.activity;
  2. import android.Manifest;
  3. import android.app.Activity;
  4. import android.app.Dialog;
  5. import android.content.ClipData;
  6. import android.content.ClipboardManager;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.graphics.Bitmap;
  10. import android.net.Uri;
  11. import android.os.Build;
  12. import android.os.Bundle;
  13. import android.os.Handler;
  14. import android.provider.MediaStore;
  15. import android.support.v4.content.FileProvider;
  16. import android.text.Editable;
  17. import android.text.TextUtils;
  18. import android.text.TextWatcher;
  19. import android.util.Log;
  20. import android.view.KeyEvent;
  21. import android.view.View;
  22. import android.view.Window;
  23. import android.view.inputmethod.InputMethodManager;
  24. import android.widget.AbsListView;
  25. import android.widget.Toast;
  26. import net.muliba.fancyfilepickerlibrary.PicturePicker;
  27. import net.zoneland.x.bpm.mobile.v1.zoneXBPM.R;
  28. import net.zoneland.x.bpm.mobile.v1.zoneXBPM.app.o2.person.PersonActivity;
  29. import net.zoneland.x.bpm.mobile.v1.zoneXBPM.utils.ImmersedStatusBarUtils;
  30. import net.zoneland.x.bpm.mobile.v1.zoneXBPM.utils.permission.PermissionRequester;
  31. import net.zoneland.x.bpm.mobile.v1.zoneXBPM.utils.permission.PermissionResult;
  32. import java.io.File;
  33. import java.lang.ref.WeakReference;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. import cn.jpush.im.android.api.JMessageClient;
  37. import cn.jpush.im.android.api.callback.GetGroupInfoCallback;
  38. import cn.jpush.im.android.api.content.EventNotificationContent;
  39. import cn.jpush.im.android.api.content.FileContent;
  40. import cn.jpush.im.android.api.content.ImageContent;
  41. import cn.jpush.im.android.api.content.LocationContent;
  42. import cn.jpush.im.android.api.content.TextContent;
  43. import cn.jpush.im.android.api.enums.ContentType;
  44. import cn.jpush.im.android.api.enums.ConversationType;
  45. import cn.jpush.im.android.api.enums.MessageDirect;
  46. import cn.jpush.im.android.api.event.MessageEvent;
  47. import cn.jpush.im.android.api.event.MessageReceiptStatusChangeEvent;
  48. import cn.jpush.im.android.api.event.MessageRetractEvent;
  49. import cn.jpush.im.android.api.event.OfflineMessageEvent;
  50. import cn.jpush.im.android.api.model.Conversation;
  51. import cn.jpush.im.android.api.model.GroupInfo;
  52. import cn.jpush.im.android.api.model.Message;
  53. import cn.jpush.im.android.api.model.UserInfo;
  54. import cn.jpush.im.android.api.options.MessageSendingOptions;
  55. import cn.jpush.im.android.eventbus.EventBus;
  56. import cn.jpush.im.api.BasicCallback;
  57. import jiguang.chat.adapter.ChattingListAdapter;
  58. import jiguang.chat.application.JGApplication;
  59. import jiguang.chat.entity.Event;
  60. import jiguang.chat.entity.EventType;
  61. import jiguang.chat.location.activity.MapPickerActivity;
  62. import jiguang.chat.model.Constants;
  63. import jiguang.chat.pickerimage.model.PhotoInfo;
  64. import jiguang.chat.pickerimage.utils.RequestCode;
  65. import jiguang.chat.pickerimage.utils.SendImageHelper;
  66. import jiguang.chat.pickerimage.utils.StorageType;
  67. import jiguang.chat.pickerimage.utils.StorageUtil;
  68. import jiguang.chat.utils.IdHelper;
  69. import jiguang.chat.utils.SharePreferenceManager;
  70. import jiguang.chat.utils.SimpleCommonUtils;
  71. import jiguang.chat.utils.ToastUtil;
  72. import jiguang.chat.utils.event.ImageEvent;
  73. import jiguang.chat.utils.imagepicker.bean.ImageItem;
  74. import jiguang.chat.utils.keyboard.XhsEmoticonsKeyBoard;
  75. import jiguang.chat.utils.keyboard.data.EmoticonEntity;
  76. import jiguang.chat.utils.keyboard.interfaces.EmoticonClickListener;
  77. import jiguang.chat.utils.keyboard.utils.EmoticonsKeyboardUtils;
  78. import jiguang.chat.utils.keyboard.widget.EmoticonsEditText;
  79. import jiguang.chat.utils.keyboard.widget.FuncLayout;
  80. import jiguang.chat.view.ChatView;
  81. import jiguang.chat.view.SimpleAppsGridView;
  82. import jiguang.chat.view.TipItem;
  83. import jiguang.chat.view.TipView;
  84. import jiguang.chat.view.listview.DropDownListView;
  85. import rx.functions.Action1;
  86. /**
  87. * Created by ${chenyn} on 2017/3/26.
  88. */
  89. public class ChatActivity extends BaseActivity implements FuncLayout.OnFuncKeyBoardListener, View.OnClickListener {
  90. DropDownListView lvChat;
  91. XhsEmoticonsKeyBoard ekBar;
  92. public static final String JPG = ".jpg";
  93. private static String MsgIDs = "msgIDs";
  94. private String mTitle;
  95. private boolean mLongClick = false;
  96. private static final String MEMBERS_COUNT = "membersCount";
  97. private static final String GROUP_NAME = "groupName";
  98. public static final String TARGET_ID = "targetId";
  99. public static final String TARGET_APP_KEY = "targetAppKey";
  100. private static final String DRAFT = "draft";
  101. private ArrayList<ImageItem> selImageList; //当前选择的所有图片
  102. public static final int REQUEST_CODE_SELECT = 100;
  103. private ChatView mChatView;
  104. private boolean mIsSingle = true;
  105. private Conversation mConv;
  106. private String mTargetId;
  107. private String mTargetAppKey;
  108. private Activity mContext;
  109. private ChattingListAdapter mChatAdapter;
  110. int maxImgCount = 9;
  111. private List<UserInfo> mAtList;
  112. private long mGroupId;
  113. private static final int REFRESH_LAST_PAGE = 0x1023;
  114. private static final int REFRESH_CHAT_TITLE = 0x1024;
  115. private static final int REFRESH_GROUP_NAME = 0x1025;
  116. private static final int REFRESH_GROUP_NUM = 0x1026;
  117. private Dialog mDialog;
  118. private GroupInfo mGroupInfo;
  119. private UserInfo mMyInfo;
  120. private static final String GROUP_ID = "groupId";
  121. private int mAtMsgId;
  122. private int mAtAllMsgId;
  123. private int mUnreadMsgCnt;
  124. private boolean mShowSoftInput = false;
  125. private List<UserInfo> forDel = new ArrayList<>();
  126. Window mWindow;
  127. InputMethodManager mImm;
  128. private final UIHandler mUIHandler = new UIHandler(this);
  129. private boolean mAtAll = false;
  130. private Uri cameraImageUri;
  131. @Override
  132. protected void onCreate(Bundle savedInstanceState) {
  133. super.onCreate(savedInstanceState);
  134. mContext = this;
  135. setContentView(R.layout.activity_chat);
  136. // 沉浸式状态栏
  137. ImmersedStatusBarUtils.INSTANCE.setImmersedStatusBar(this);
  138. mChatView = (ChatView) findViewById(R.id.chat_view);
  139. mChatView.initModule(mDensity, mDensityDpi);
  140. this.mWindow = getWindow();
  141. this.mImm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
  142. mChatView.setListeners(this);
  143. lvChat = (DropDownListView)findViewById(R.id.lv_chat);
  144. ekBar = (XhsEmoticonsKeyBoard)findViewById(R.id.ek_bar);
  145. initData();
  146. initView();
  147. }
  148. private void initData() {
  149. SimpleCommonUtils.initEmoticonsEditText(ekBar.getEtChat());
  150. Intent intent = getIntent();
  151. mTargetId = intent.getStringExtra(TARGET_ID);
  152. mTargetAppKey = intent.getStringExtra(TARGET_APP_KEY);
  153. mTitle = intent.getStringExtra(JGApplication.CONV_TITLE);
  154. mMyInfo = JMessageClient.getMyInfo();
  155. if (!TextUtils.isEmpty(mTargetId)) {
  156. //单聊
  157. mIsSingle = true;
  158. mChatView.setChatTitle(mTitle);
  159. mConv = JMessageClient.getSingleConversation(mTargetId, mTargetAppKey);
  160. if (mConv == null) {
  161. mConv = Conversation.createSingleConversation(mTargetId, mTargetAppKey);
  162. }
  163. mChatAdapter = new ChattingListAdapter(mContext, mConv, longClickListener);
  164. } else {
  165. //群聊
  166. mIsSingle = false;
  167. mGroupId = intent.getLongExtra(GROUP_ID, 0);
  168. final boolean fromGroup = intent.getBooleanExtra("fromGroup", false);
  169. if (fromGroup) {
  170. mChatView.setChatTitle(mTitle, intent.getIntExtra(MEMBERS_COUNT, 0));
  171. mConv = JMessageClient.getGroupConversation(mGroupId);
  172. mChatAdapter = new ChattingListAdapter(mContext, mConv, longClickListener);//长按聊天内容监听
  173. } else {
  174. mAtMsgId = intent.getIntExtra("atMsgId", -1);
  175. mAtAllMsgId = intent.getIntExtra("atAllMsgId", -1);
  176. mConv = JMessageClient.getGroupConversation(mGroupId);
  177. if (mConv != null) {
  178. GroupInfo groupInfo = (GroupInfo) mConv.getTargetInfo();
  179. UserInfo userInfo = groupInfo.getGroupMemberInfo(mMyInfo.getUserName(), mMyInfo.getAppKey());
  180. //如果自己在群聊中,聊天标题显示群人数
  181. if (userInfo != null) {
  182. if (!TextUtils.isEmpty(groupInfo.getGroupName())) {
  183. mChatView.setChatTitle(mTitle, groupInfo.getGroupMembers().size());
  184. } else {
  185. mChatView.setChatTitle(mTitle, groupInfo.getGroupMembers().size());
  186. }
  187. mChatView.showRightBtn();
  188. } else {
  189. if (!TextUtils.isEmpty(mTitle)) {
  190. mChatView.setChatTitle(mTitle);
  191. } else {
  192. mChatView.setChatTitle(R.string.group);
  193. }
  194. mChatView.dismissRightBtn();
  195. }
  196. } else {
  197. mConv = Conversation.createGroupConversation(mGroupId);
  198. }
  199. //更新群名
  200. JMessageClient.getGroupInfo(mGroupId, new GetGroupInfoCallback(false) {
  201. @Override
  202. public void gotResult(int status, String desc, GroupInfo groupInfo) {
  203. if (status == 0) {
  204. mGroupInfo = groupInfo;
  205. mUIHandler.sendEmptyMessage(REFRESH_CHAT_TITLE);
  206. }
  207. }
  208. });
  209. if (mAtMsgId != -1) {
  210. mUnreadMsgCnt = mConv.getUnReadMsgCnt();
  211. // 如果 @我 的消息位于屏幕显示的消息之上,显示 有人@我 的按钮
  212. if (mAtMsgId + 8 <= mConv.getLatestMessage().getId()) {
  213. mChatView.showAtMeButton();
  214. }
  215. mChatAdapter = new ChattingListAdapter(mContext, mConv, longClickListener, mAtMsgId);
  216. } else {
  217. mChatAdapter = new ChattingListAdapter(mContext, mConv, longClickListener);
  218. }
  219. }
  220. //聊天信息标志改变
  221. mChatView.setGroupIcon();
  222. }
  223. String draft = intent.getStringExtra(DRAFT);
  224. if (draft != null && !TextUtils.isEmpty(draft)) {
  225. ekBar.getEtChat().setText(draft);
  226. }
  227. mChatView.setChatListAdapter(mChatAdapter);
  228. // mChatAdapter.initMediaPlayer();
  229. mChatView.getListView().setOnDropDownListener(new DropDownListView.OnDropDownListener() {
  230. @Override
  231. public void onDropDown() {
  232. mUIHandler.sendEmptyMessageDelayed(REFRESH_LAST_PAGE, 1000);
  233. }
  234. });
  235. mChatView.setToBottom();
  236. mChatView.setConversation(mConv);
  237. }
  238. private void initView() {
  239. initEmoticonsKeyBoardBar();
  240. initListView();
  241. ekBar.getEtChat().addTextChangedListener(new TextWatcher() {
  242. private CharSequence temp = "";
  243. @Override
  244. public void afterTextChanged(Editable arg0) {
  245. if (temp.length() > 0) {
  246. mLongClick = false;
  247. }
  248. if (mAtList != null && mAtList.size() > 0) {
  249. for (UserInfo info : mAtList) {
  250. String name = info.getDisplayName();
  251. if (!arg0.toString().contains("@" + name + " ")) {
  252. forDel.add(info);
  253. }
  254. }
  255. mAtList.removeAll(forDel);
  256. }
  257. if (!arg0.toString().contains("@所有成员 ")) {
  258. mAtAll = false;
  259. }
  260. }
  261. @Override
  262. public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
  263. }
  264. @Override
  265. public void onTextChanged(CharSequence s, int start, int count, int after) {
  266. temp = s;
  267. if (s.length() > 0 && after >= 1 && s.subSequence(start, start + 1).charAt(0) == '@' && !mLongClick) {
  268. if (null != mConv && mConv.getType() == ConversationType.group) {
  269. ChooseAtMemberActivity.show(ChatActivity.this, ekBar.getEtChat(), mConv.getTargetId());
  270. }
  271. }
  272. }
  273. });
  274. }
  275. private void initEmoticonsKeyBoardBar() {
  276. ekBar.setAdapter(SimpleCommonUtils.getCommonAdapter(this, emoticonClickListener));
  277. ekBar.addOnFuncKeyBoardListener(this);
  278. SimpleAppsGridView gridView = new SimpleAppsGridView(this);
  279. ekBar.addFuncView(gridView);
  280. ekBar.getEtChat().setOnSizeChangedListener(new EmoticonsEditText.OnSizeChangedListener() {
  281. @Override
  282. public void onSizeChanged(int w, int h, int oldw, int oldh) {
  283. scrollToBottom();
  284. }
  285. });
  286. //发送按钮
  287. ekBar.getBtnSend().setOnClickListener(new View.OnClickListener() {
  288. @Override
  289. public void onClick(View v) {
  290. String mcgContent = ekBar.getEtChat().getText().toString();
  291. scrollToBottom();
  292. if (mcgContent.equals("")) {
  293. return;
  294. }
  295. Message msg;
  296. TextContent content = new TextContent(mcgContent);
  297. if (mAtAll) {
  298. msg = mConv.createSendMessageAtAllMember(content, null);
  299. mAtAll = false;
  300. } else if (null != mAtList) {
  301. msg = mConv.createSendMessage(content, mAtList, null);
  302. } else {
  303. msg = mConv.createSendMessage(content);
  304. }
  305. //设置需要已读回执
  306. MessageSendingOptions options = new MessageSendingOptions();
  307. options.setNeedReadReceipt(false);
  308. JMessageClient.sendMessage(msg, options);
  309. mChatAdapter.addMsgFromReceiptToList(msg);
  310. ekBar.getEtChat().setText("");
  311. if (mAtList != null) {
  312. mAtList.clear();
  313. }
  314. if (forDel != null) {
  315. forDel.clear();
  316. }
  317. }
  318. });
  319. //切换语音输入
  320. ekBar.getVoiceOrText().setOnClickListener(new View.OnClickListener() {
  321. @Override
  322. public void onClick(View v) {
  323. int i = v.getId();
  324. if (i == R.id.btn_voice_or_text) {
  325. ekBar.setVideoText();
  326. ekBar.getBtnVoice().initConv(mConv, mChatAdapter, mChatView);
  327. }
  328. }
  329. });
  330. }
  331. @Override
  332. public void onClick(View v) {
  333. int i = v.getId();
  334. if (i == R.id.jmui_return_btn) {
  335. returnBtn();
  336. } else if (i == R.id.jmui_right_btn) {
  337. startChatDetailActivity(mTargetId, mTargetAppKey, mGroupId);
  338. } else if (i == R.id.jmui_at_me_btn) {
  339. if (mUnreadMsgCnt < ChattingListAdapter.PAGE_MESSAGE_COUNT) {
  340. int position = ChattingListAdapter.PAGE_MESSAGE_COUNT + mAtMsgId - mConv.getLatestMessage().getId();
  341. mChatView.setToPosition(position);
  342. } else {
  343. mChatView.setToPosition(mAtMsgId + mUnreadMsgCnt - mConv.getLatestMessage().getId());
  344. }
  345. } else {
  346. }
  347. }
  348. @Override
  349. public void onBackPressed() {
  350. super.onBackPressed();
  351. returnBtn();
  352. }
  353. private void returnBtn() {
  354. mConv.resetUnreadCount();
  355. dismissSoftInput();
  356. JMessageClient.exitConversation();
  357. //发送保存为草稿事件到会话列表
  358. EventBus.getDefault().post(new Event.Builder().setType(EventType.draft)
  359. .setConversation(mConv)
  360. .setDraft(ekBar.getEtChat().getText().toString())
  361. .build());
  362. finish();
  363. }
  364. private void dismissSoftInput() {
  365. if (mShowSoftInput) {
  366. if (mImm != null) {
  367. mImm.hideSoftInputFromWindow(ekBar.getEtChat().getWindowToken(), 0);
  368. mShowSoftInput = false;
  369. }
  370. try {
  371. Thread.sleep(200);
  372. } catch (InterruptedException e) {
  373. e.printStackTrace();
  374. }
  375. }
  376. }
  377. public void startChatDetailActivity(String targetId, String appKey, long groupId) {
  378. Log.i("ChatActivity", "stargetId:" + targetId + ", appkey:" + appKey + ", groupid:" + groupId);
  379. if (groupId!=0) {
  380. Intent intent = new Intent();
  381. intent.putExtra(TARGET_ID, targetId);
  382. intent.putExtra(TARGET_APP_KEY, appKey);
  383. intent.putExtra(GROUP_ID, groupId);
  384. intent.setClass(this, ChatDetailActivity.class);
  385. startActivityForResult(intent, JGApplication.REQUEST_CODE_CHAT_DETAIL);
  386. }else {//个人
  387. Intent intent = new Intent(this, PersonActivity.class);
  388. intent.putExtra(PersonActivity.Companion.getPERSON_NAME_KEY(), targetId);
  389. startActivity(intent);
  390. }
  391. }
  392. EmoticonClickListener emoticonClickListener = new EmoticonClickListener() {
  393. @Override
  394. public void onEmoticonClick(Object o, int actionType, boolean isDelBtn) {
  395. if (isDelBtn) {
  396. SimpleCommonUtils.delClick(ekBar.getEtChat());
  397. } else {
  398. if (o == null) {
  399. return;
  400. }
  401. if (actionType == Constants.EMOTICON_CLICK_BIGIMAGE) {
  402. if (o instanceof EmoticonEntity) {
  403. OnSendImage(((EmoticonEntity) o).getIconUri());
  404. }
  405. } else {
  406. String content = null;
  407. // Emoji
  408. // EmojiDisplay.
  409. // if (o instanceof EmojiBean) {
  410. // content = ((EmojiBean) o).emoji;
  411. // } else
  412. if (o instanceof EmoticonEntity) {
  413. content = ((EmoticonEntity) o).getContent();
  414. }
  415. if (TextUtils.isEmpty(content)) {
  416. return;
  417. }
  418. int index = ekBar.getEtChat().getSelectionStart();
  419. Editable editable = ekBar.getEtChat().getText();
  420. editable.insert(index, content);
  421. }
  422. }
  423. }
  424. };
  425. @Override
  426. public boolean dispatchKeyEvent(KeyEvent event) {
  427. if (EmoticonsKeyboardUtils.isFullScreen(this)) {
  428. boolean isConsum = ekBar.dispatchKeyEventInFullScreen(event);
  429. return isConsum ? isConsum : super.dispatchKeyEvent(event);
  430. }
  431. return super.dispatchKeyEvent(event);
  432. }
  433. private void initListView() {
  434. lvChat.setAdapter(mChatAdapter);
  435. lvChat.setOnScrollListener(new AbsListView.OnScrollListener() {
  436. @Override
  437. public void onScrollStateChanged(AbsListView view, int scrollState) {
  438. switch (scrollState) {
  439. case SCROLL_STATE_FLING:
  440. break;
  441. case SCROLL_STATE_IDLE:
  442. break;
  443. case SCROLL_STATE_TOUCH_SCROLL:
  444. ekBar.reset();
  445. break;
  446. }
  447. }
  448. @Override
  449. public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
  450. }
  451. });
  452. }
  453. private void scrollToBottom() {
  454. lvChat.requestLayout();
  455. lvChat.post(new Runnable() {
  456. @Override
  457. public void run() {
  458. lvChat.setSelection(lvChat.getBottom());
  459. }
  460. });
  461. }
  462. @Override
  463. public void OnFuncPop(int height) {
  464. scrollToBottom();
  465. }
  466. @Override
  467. public void OnFuncClose() {
  468. }
  469. @Override
  470. protected void onPause() {
  471. super.onPause();
  472. JMessageClient.exitConversation();
  473. ekBar.reset();
  474. }
  475. @Override
  476. protected void onResume() {
  477. String targetId = getIntent().getStringExtra(TARGET_ID);
  478. if (!mIsSingle) {
  479. long groupId = getIntent().getLongExtra(GROUP_ID, 0);
  480. if (groupId != 0) {
  481. JGApplication.isAtMe.put(groupId, false);
  482. JGApplication.isAtall.put(groupId, false);
  483. JMessageClient.enterGroupConversation(groupId);
  484. }
  485. } else if (null != targetId) {
  486. String appKey = getIntent().getStringExtra(TARGET_APP_KEY);
  487. JMessageClient.enterSingleConversation(targetId, appKey);
  488. }
  489. //历史消息中删除后返回到聊天界面刷新界面
  490. if (JGApplication.ids != null && JGApplication.ids.size() > 0) {
  491. for (Message msg : JGApplication.ids) {
  492. mChatAdapter.removeMessage(msg);
  493. }
  494. }
  495. mChatAdapter.notifyDataSetChanged();
  496. //发送名片返回聊天界面刷新信息
  497. if (SharePreferenceManager.getIsOpen()) {
  498. initData();
  499. SharePreferenceManager.setIsOpen(false);
  500. }
  501. super.onResume();
  502. }
  503. public void onEvent(MessageEvent event) {
  504. final Message message = event.getMessage();
  505. //若为群聊相关事件,如添加、删除群成员
  506. if (message.getContentType() == ContentType.eventNotification) {
  507. GroupInfo groupInfo = (GroupInfo) message.getTargetInfo();
  508. long groupId = groupInfo.getGroupID();
  509. EventNotificationContent.EventNotificationType type = ((EventNotificationContent) message
  510. .getContent()).getEventNotificationType();
  511. if (groupId == mGroupId) {
  512. switch (type) {
  513. case group_member_added:
  514. //添加群成员事件
  515. List<String> userNames = ((EventNotificationContent) message.getContent()).getUserNames();
  516. //群主把当前用户添加到群聊,则显示聊天详情按钮
  517. refreshGroupNum();
  518. if (userNames.contains(mMyInfo.getNickname()) || userNames.contains(mMyInfo.getUserName())) {
  519. runOnUiThread(new Runnable() {
  520. @Override
  521. public void run() {
  522. mChatView.showRightBtn();
  523. }
  524. });
  525. }
  526. break;
  527. case group_member_removed:
  528. //删除群成员事件
  529. userNames = ((EventNotificationContent) message.getContent()).getUserNames();
  530. //群主删除了当前用户,则隐藏聊天详情按钮
  531. if (userNames.contains(mMyInfo.getNickname()) || userNames.contains(mMyInfo.getUserName())) {
  532. runOnUiThread(new Runnable() {
  533. @Override
  534. public void run() {
  535. mChatView.dismissRightBtn();
  536. GroupInfo groupInfo = (GroupInfo) mConv.getTargetInfo();
  537. if (TextUtils.isEmpty(groupInfo.getGroupName())) {
  538. mChatView.setChatTitle(R.string.group);
  539. } else {
  540. mChatView.setChatTitle(groupInfo.getGroupName());
  541. }
  542. mChatView.dismissGroupNum();
  543. }
  544. });
  545. } else {
  546. refreshGroupNum();
  547. }
  548. break;
  549. case group_member_exit:
  550. EventNotificationContent content = (EventNotificationContent) message.getContent();
  551. if (content.getUserNames().contains(JMessageClient.getMyInfo().getUserName())) {
  552. mChatAdapter.notifyDataSetChanged();
  553. } else {
  554. refreshGroupNum();
  555. }
  556. break;
  557. }
  558. }
  559. }
  560. runOnUiThread(new Runnable() {
  561. @Override
  562. public void run() {
  563. if (message.getTargetType() == ConversationType.single) {
  564. UserInfo userInfo = (UserInfo) message.getTargetInfo();
  565. String targetId = userInfo.getUserName();
  566. String appKey = userInfo.getAppKey();
  567. if (mIsSingle && targetId.equals(mTargetId) && appKey.equals(mTargetAppKey)) {
  568. Message lastMsg = mChatAdapter.getLastMsg();
  569. if (lastMsg == null || message.getId() != lastMsg.getId()) {
  570. mChatAdapter.addMsgToList(message);
  571. } else {
  572. mChatAdapter.notifyDataSetChanged();
  573. }
  574. }
  575. } else {
  576. long groupId = ((GroupInfo) message.getTargetInfo()).getGroupID();
  577. if (groupId == mGroupId) {
  578. Message lastMsg = mChatAdapter.getLastMsg();
  579. if (lastMsg == null || message.getId() != lastMsg.getId()) {
  580. mChatAdapter.addMsgToList(message);
  581. } else {
  582. mChatAdapter.notifyDataSetChanged();
  583. }
  584. }
  585. }
  586. }
  587. });
  588. }
  589. public void onEventMainThread(MessageRetractEvent event) {
  590. Message retractedMessage = event.getRetractedMessage();
  591. mChatAdapter.delMsgRetract(retractedMessage);
  592. }
  593. /**
  594. * 当在聊天界面断网再次连接时收离线事件刷新
  595. */
  596. public void onEvent(OfflineMessageEvent event) {
  597. Conversation conv = event.getConversation();
  598. if (conv.getType().equals(ConversationType.single)) {
  599. UserInfo userInfo = (UserInfo) conv.getTargetInfo();
  600. String targetId = userInfo.getUserName();
  601. String appKey = userInfo.getAppKey();
  602. if (mIsSingle && targetId.equals(mTargetId) && appKey.equals(mTargetAppKey)) {
  603. List<Message> singleOfflineMsgList = event.getOfflineMessageList();
  604. if (singleOfflineMsgList != null && singleOfflineMsgList.size() > 0) {
  605. mChatView.setToBottom();
  606. mChatAdapter.addMsgListToList(singleOfflineMsgList);
  607. }
  608. }
  609. } else {
  610. long groupId = ((GroupInfo) conv.getTargetInfo()).getGroupID();
  611. if (groupId == mGroupId) {
  612. List<Message> offlineMessageList = event.getOfflineMessageList();
  613. if (offlineMessageList != null && offlineMessageList.size() > 0) {
  614. mChatView.setToBottom();
  615. mChatAdapter.addMsgListToList(offlineMessageList);
  616. }
  617. }
  618. }
  619. }
  620. private void refreshGroupNum() {
  621. Conversation conv = JMessageClient.getGroupConversation(mGroupId);
  622. GroupInfo groupInfo = (GroupInfo) conv.getTargetInfo();
  623. if (!TextUtils.isEmpty(groupInfo.getGroupName())) {
  624. android.os.Message handleMessage = mUIHandler.obtainMessage();
  625. handleMessage.what = REFRESH_GROUP_NAME;
  626. Bundle bundle = new Bundle();
  627. bundle.putString(GROUP_NAME, groupInfo.getGroupName());
  628. bundle.putInt(MEMBERS_COUNT, groupInfo.getGroupMembers().size());
  629. handleMessage.setData(bundle);
  630. handleMessage.sendToTarget();
  631. } else {
  632. android.os.Message handleMessage = mUIHandler.obtainMessage();
  633. handleMessage.what = REFRESH_GROUP_NUM;
  634. Bundle bundle = new Bundle();
  635. bundle.putInt(MEMBERS_COUNT, groupInfo.getGroupMembers().size());
  636. handleMessage.setData(bundle);
  637. handleMessage.sendToTarget();
  638. }
  639. }
  640. private ChattingListAdapter.ContentLongClickListener longClickListener = new ChattingListAdapter.ContentLongClickListener() {
  641. @Override
  642. public void onContentLongClick(final int position, View view) {
  643. final Message msg = mChatAdapter.getMessage(position);
  644. if (msg == null) {
  645. return;
  646. }
  647. //如果是文本消息
  648. if ((msg.getContentType() == ContentType.text) && ((TextContent) msg.getContent()).getStringExtra("businessCard") == null) {
  649. //接收方
  650. if (msg.getDirect() == MessageDirect.receive) {
  651. int[] location = new int[2];
  652. view.getLocationOnScreen(location);
  653. float OldListY = (float) location[1];
  654. float OldListX = (float) location[0];
  655. new TipView.Builder(ChatActivity.this, mChatView, (int) OldListX + view.getWidth() / 2, (int) OldListY + view.getHeight())
  656. .addItem(new TipItem("复制"))
  657. .addItem(new TipItem("转发"))
  658. .addItem(new TipItem("删除"))
  659. .setOnItemClickListener(new TipView.OnItemClickListener() {
  660. @Override
  661. public void onItemClick(String str, final int position) {
  662. if (position == 0) {
  663. if (msg.getContentType() == ContentType.text) {
  664. final String content = ((TextContent) msg.getContent()).getText();
  665. if (Build.VERSION.SDK_INT > 11) {
  666. ClipboardManager clipboard = (ClipboardManager) mContext
  667. .getSystemService(Context.CLIPBOARD_SERVICE);
  668. ClipData clip = ClipData.newPlainText("Simple text", content);
  669. clipboard.setPrimaryClip(clip);
  670. } else {
  671. android.text.ClipboardManager clip = (android.text.ClipboardManager) mContext
  672. .getSystemService(Context.CLIPBOARD_SERVICE);
  673. if (clip.hasText()) {
  674. clip.getText();
  675. }
  676. }
  677. Toast.makeText(ChatActivity.this, "已复制", Toast.LENGTH_SHORT).show();
  678. } else {
  679. Toast.makeText(ChatActivity.this, "只支持复制文字", Toast.LENGTH_SHORT).show();
  680. }
  681. } else if (position == 1) {
  682. Intent intent = new Intent(ChatActivity.this, ForwardMsgActivity.class);
  683. JGApplication.forwardMsg.clear();
  684. JGApplication.forwardMsg.add(msg);
  685. startActivity(intent);
  686. } else {
  687. //删除
  688. mConv.deleteMessage(msg.getId());
  689. mChatAdapter.removeMessage(msg);
  690. }
  691. }
  692. @Override
  693. public void dismiss() {
  694. }
  695. })
  696. .create();
  697. //发送方
  698. } else {
  699. int[] location = new int[2];
  700. view.getLocationOnScreen(location);
  701. float OldListY = (float) location[1];
  702. float OldListX = (float) location[0];
  703. new TipView.Builder(ChatActivity.this, mChatView, (int) OldListX + view.getWidth() / 2, (int) OldListY + view.getHeight())
  704. .addItem(new TipItem("复制"))
  705. .addItem(new TipItem("转发"))
  706. .addItem(new TipItem("撤回"))
  707. .addItem(new TipItem("删除"))
  708. .setOnItemClickListener(new TipView.OnItemClickListener() {
  709. @Override
  710. public void onItemClick(String str, final int position) {
  711. if (position == 0) {
  712. if (msg.getContentType() == ContentType.text) {
  713. final String content = ((TextContent) msg.getContent()).getText();
  714. if (Build.VERSION.SDK_INT > 11) {
  715. ClipboardManager clipboard = (ClipboardManager) mContext
  716. .getSystemService(Context.CLIPBOARD_SERVICE);
  717. ClipData clip = ClipData.newPlainText("Simple text", content);
  718. clipboard.setPrimaryClip(clip);
  719. } else {
  720. android.text.ClipboardManager clip = (android.text.ClipboardManager) mContext
  721. .getSystemService(Context.CLIPBOARD_SERVICE);
  722. if (clip.hasText()) {
  723. clip.getText();
  724. }
  725. }
  726. Toast.makeText(ChatActivity.this, "已复制", Toast.LENGTH_SHORT).show();
  727. } else {
  728. Toast.makeText(ChatActivity.this, "只支持复制文字", Toast.LENGTH_SHORT).show();
  729. }
  730. } else if (position == 1) {
  731. //转发
  732. if (msg.getContentType() == ContentType.text || msg.getContentType() == ContentType.image ||
  733. (msg.getContentType() == ContentType.file && (msg.getContent()).getStringExtra("video") != null)) {
  734. Intent intent = new Intent(ChatActivity.this, ForwardMsgActivity.class);
  735. JGApplication.forwardMsg.clear();
  736. JGApplication.forwardMsg.add(msg);
  737. startActivity(intent);
  738. } else {
  739. Toast.makeText(ChatActivity.this, "只支持转发文本,图片,小视频", Toast.LENGTH_SHORT).show();
  740. }
  741. } else if (position == 2) {
  742. //撤回
  743. mConv.retractMessage(msg, new BasicCallback() {
  744. @Override
  745. public void gotResult(int i, String s) {
  746. if (i == 855001) {
  747. Toast.makeText(ChatActivity.this, "发送时间过长,不能撤回", Toast.LENGTH_SHORT).show();
  748. } else if (i == 0) {
  749. mChatAdapter.delMsgRetract(msg);
  750. }
  751. }
  752. });
  753. } else {
  754. //删除
  755. mConv.deleteMessage(msg.getId());
  756. mChatAdapter.removeMessage(msg);
  757. }
  758. }
  759. @Override
  760. public void dismiss() {
  761. }
  762. })
  763. .create();
  764. }
  765. //除了文本消息类型之外的消息类型
  766. } else {
  767. //接收方
  768. if (msg.getDirect() == MessageDirect.receive) {
  769. int[] location = new int[2];
  770. view.getLocationOnScreen(location);
  771. float OldListY = (float) location[1];
  772. float OldListX = (float) location[0];
  773. new TipView.Builder(ChatActivity.this, mChatView, (int) OldListX + view.getWidth() / 2, (int) OldListY + view.getHeight())
  774. .addItem(new TipItem("转发"))
  775. .addItem(new TipItem("删除"))
  776. .setOnItemClickListener(new TipView.OnItemClickListener() {
  777. @Override
  778. public void onItemClick(String str, final int position) {
  779. if (position == 1) {
  780. //删除
  781. mConv.deleteMessage(msg.getId());
  782. mChatAdapter.removeMessage(msg);
  783. } else {
  784. Intent intent = new Intent(ChatActivity.this, ForwardMsgActivity.class);
  785. JGApplication.forwardMsg.clear();
  786. JGApplication.forwardMsg.add(msg);
  787. startActivity(intent);
  788. }
  789. }
  790. @Override
  791. public void dismiss() {
  792. }
  793. })
  794. .create();
  795. //发送方
  796. } else {
  797. int[] location = new int[2];
  798. view.getLocationOnScreen(location);
  799. float OldListY = (float) location[1];
  800. float OldListX = (float) location[0];
  801. new TipView.Builder(ChatActivity.this, mChatView, (int) OldListX + view.getWidth() / 2, (int) OldListY + view.getHeight())
  802. .addItem(new TipItem("转发"))
  803. .addItem(new TipItem("撤回"))
  804. .addItem(new TipItem("删除"))
  805. .setOnItemClickListener(new TipView.OnItemClickListener() {
  806. @Override
  807. public void onItemClick(String str, final int position) {
  808. if (position == 1) {
  809. //撤回
  810. mConv.retractMessage(msg, new BasicCallback() {
  811. @Override
  812. public void gotResult(int i, String s) {
  813. if (i == 855001) {
  814. Toast.makeText(ChatActivity.this, "发送时间过长,不能撤回", Toast.LENGTH_SHORT).show();
  815. } else if (i == 0) {
  816. mChatAdapter.delMsgRetract(msg);
  817. }
  818. }
  819. });
  820. } else if (position == 0) {
  821. Intent intent = new Intent(ChatActivity.this, ForwardMsgActivity.class);
  822. JGApplication.forwardMsg.clear();
  823. JGApplication.forwardMsg.add(msg);
  824. startActivity(intent);
  825. } else {
  826. //删除
  827. mConv.deleteMessage(msg.getId());
  828. mChatAdapter.removeMessage(msg);
  829. }
  830. }
  831. @Override
  832. public void dismiss() {
  833. }
  834. })
  835. .create();
  836. }
  837. }
  838. }
  839. };
  840. /**
  841. * 消息已读事件
  842. */
  843. public void onEventMainThread(MessageReceiptStatusChangeEvent event) {
  844. List<MessageReceiptStatusChangeEvent.MessageReceiptMeta> messageReceiptMetas = event.getMessageReceiptMetas();
  845. for (MessageReceiptStatusChangeEvent.MessageReceiptMeta meta : messageReceiptMetas) {
  846. long serverMsgId = meta.getServerMsgId();
  847. int unReceiptCnt = meta.getUnReceiptCnt();
  848. mChatAdapter.setUpdateReceiptCount(serverMsgId, unReceiptCnt);
  849. }
  850. }
  851. private Uri getUriFromFile(File file) {
  852. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  853. String applicationId = getApplicationContext().getPackageName();
  854. return FileProvider.getUriForFile(this, applicationId+".fileProvider", file);
  855. }else {
  856. return Uri.fromFile(file);
  857. }
  858. }
  859. public void onEventMainThread(ImageEvent event) {
  860. switch (event.getFlag()) {
  861. case JGApplication.IMAGE_MESSAGE:
  862. final int requestCode = RequestCode.PICK_IMAGE;
  863. new PermissionRequester(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
  864. .subscribe(new Action1<PermissionResult>() {
  865. @Override
  866. public void call(PermissionResult permissionResult) {
  867. if(permissionResult.getGranted()){
  868. new PicturePicker().withActivity(ChatActivity.this)
  869. .requestCode(requestCode)
  870. .chooseType(PicturePicker.CHOOSE_TYPE_MULTIPLE())
  871. .start();
  872. }else {
  873. Toast.makeText(ChatActivity.this, "请在应用管理中打开“读写存储”访问权限!", Toast.LENGTH_LONG).show();
  874. }
  875. }
  876. }, new Action1<Throwable>() {
  877. @Override
  878. public void call(Throwable throwable) {
  879. Toast.makeText(ChatActivity.this, "请在应用管理中打开“读写存储”访问权限!", Toast.LENGTH_LONG).show();
  880. }
  881. });
  882. break;
  883. case JGApplication.TAKE_PHOTO_MESSAGE:
  884. new PermissionRequester(this).request(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO)
  885. .subscribe(new Action1<PermissionResult>() {
  886. @Override
  887. public void call(PermissionResult permissionResult) {
  888. if (permissionResult.getGranted()) {
  889. Log.i("ChatActivity", "take photo ........................");
  890. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  891. //return-data false 不是直接返回拍照后的照片Bitmap 因为照片太大会传输失败
  892. intent.putExtra("return-data", false);
  893. //改用Uri 传递
  894. if (cameraImageUri == null) {
  895. String filePath = StorageUtil.getWritePath(ChatActivity.this, "camera_cache.jpg", StorageType.TYPE_TEMP);
  896. Log.i("ChatActivity", "take photo filePath."+filePath);
  897. cameraImageUri = getUriFromFile(new File(filePath));
  898. }
  899. Log.i("ChatActivity", "take photo ....111111111..."+cameraImageUri.getPath());
  900. intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImageUri);
  901. intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
  902. intent.putExtra("noFaceDetection", true);
  903. startActivityForResult(intent, RequestCode.TAKE_PHOTO);
  904. } else {
  905. Toast.makeText(ChatActivity.this, "请在应用管理中打开“相机,读写存储,录音”访问权限!", Toast.LENGTH_LONG).show();
  906. }
  907. }
  908. }, new Action1<Throwable>() {
  909. @Override
  910. public void call(Throwable throwable) {
  911. Toast.makeText(ChatActivity.this, "请在应用管理中打开“相机,读写存储,录音”访问权限!", Toast.LENGTH_LONG).show();
  912. }
  913. });
  914. break;
  915. case JGApplication.TAKE_LOCATION:
  916. new PermissionRequester(this).request(Manifest.permission.ACCESS_FINE_LOCATION)
  917. .subscribe(new Action1<PermissionResult>() {
  918. @Override
  919. public void call(PermissionResult permissionResult) {
  920. if (permissionResult.getGranted()){
  921. Intent intent = new Intent(mContext, MapPickerActivity.class);
  922. intent.putExtra(JGApplication.TARGET_ID, mTargetId);
  923. intent.putExtra(JGApplication.TARGET_APP_KEY, mTargetAppKey);
  924. intent.putExtra(JGApplication.GROUP_ID, mGroupId);
  925. intent.putExtra("sendLocation", true);
  926. startActivityForResult(intent, JGApplication.REQUEST_CODE_SEND_LOCATION);
  927. }else {
  928. Toast.makeText(ChatActivity.this, "请在应用管理中打开“位置”访问权限!", Toast.LENGTH_LONG).show();
  929. }
  930. }
  931. }, new Action1<Throwable>() {
  932. @Override
  933. public void call(Throwable throwable) {
  934. Toast.makeText(ChatActivity.this, "请在应用管理中打开“位置”访问权限!", Toast.LENGTH_LONG).show();
  935. }
  936. });
  937. break;
  938. case JGApplication.FILE_MESSAGE:
  939. new PermissionRequester(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
  940. .subscribe(new Action1<PermissionResult>() {
  941. @Override
  942. public void call(PermissionResult permissionResult) {
  943. if (permissionResult.getGranted()){
  944. Intent intent = new Intent(mContext, SendFileActivity.class);
  945. intent.putExtra(JGApplication.TARGET_ID, mTargetId);
  946. intent.putExtra(JGApplication.TARGET_APP_KEY, mTargetAppKey);
  947. intent.putExtra(JGApplication.GROUP_ID, mGroupId);
  948. startActivityForResult(intent, JGApplication.REQUEST_CODE_SEND_FILE);
  949. }else {
  950. Toast.makeText(ChatActivity.this, "请在应用管理中打开“读写存储”访问权限!", Toast.LENGTH_LONG).show();
  951. }
  952. }
  953. }, new Action1<Throwable>() {
  954. @Override
  955. public void call(Throwable throwable) {
  956. Toast.makeText(ChatActivity.this, "请在应用管理中打开“读写存储”访问权限!", Toast.LENGTH_LONG).show();
  957. }
  958. });
  959. break;
  960. case JGApplication.BUSINESS_CARD:
  961. Intent intent = new Intent(mContext, FriendListActivity.class);
  962. intent.putExtra("isSingle", mIsSingle);
  963. intent.putExtra("userId", mTargetId);
  964. intent.putExtra("groupId", mGroupId);
  965. startActivity(intent);
  966. break;
  967. case JGApplication.TACK_VIDEO:
  968. case JGApplication.TACK_VOICE:
  969. ToastUtil.shortToast(mContext, "该功能正在添加中");
  970. break;
  971. default:
  972. break;
  973. }
  974. }
  975. @Override
  976. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  977. switch (resultCode) {
  978. case JGApplication.RESULT_CODE_AT_MEMBER:
  979. if (!mIsSingle) {
  980. GroupInfo groupInfo = (GroupInfo) mConv.getTargetInfo();
  981. String username = data.getStringExtra(JGApplication.TARGET_ID);
  982. String appKey = data.getStringExtra(JGApplication.TARGET_APP_KEY);
  983. UserInfo userInfo = groupInfo.getGroupMemberInfo(username, appKey);
  984. if (null == mAtList) {
  985. mAtList = new ArrayList<UserInfo>();
  986. }
  987. mAtList.add(userInfo);
  988. mLongClick = true;
  989. ekBar.getEtChat().appendMention(data.getStringExtra(JGApplication.NAME));
  990. ekBar.getEtChat().setSelection(ekBar.getEtChat().getText().length());
  991. }
  992. break;
  993. case JGApplication.RESULT_CODE_AT_ALL:
  994. mAtAll = data.getBooleanExtra(JGApplication.ATALL, false);
  995. mLongClick = true;
  996. if (mAtAll) {
  997. ekBar.getEtChat().setText(ekBar.getEtChat().getText().toString() + "所有成员 ");
  998. ekBar.getEtChat().setSelection(ekBar.getEtChat().getText().length());
  999. }
  1000. break;
  1001. case JGApplication.RESULT_CODE_SEND_LOCATION:
  1002. //之前是在地图选择那边做的发送逻辑,这里是通过msgID拿到的message放到ui上.但是发现问题,message的status始终是send_going状态
  1003. //因为那边发送的是自己创建的对象,这里通过id取出来的不是同一个对象.尽管内容都是一样的.所以为了保证发送的对象个ui上拿出来的
  1004. //对象是同一个,就地图那边传过来数据,在这边进行创建message
  1005. double latitude = data.getDoubleExtra("latitude", 0);
  1006. double longitude = data.getDoubleExtra("longitude", 0);
  1007. int mapview = data.getIntExtra("mapview", 0);
  1008. String street = data.getStringExtra("street");
  1009. String path = data.getStringExtra("path");
  1010. LocationContent locationContent = new LocationContent(latitude,
  1011. longitude, mapview, street);
  1012. locationContent.setStringExtra("path", path);
  1013. Message message = mConv.createSendMessage(locationContent);
  1014. MessageSendingOptions options = new MessageSendingOptions();
  1015. options.setNeedReadReceipt(true);
  1016. JMessageClient.sendMessage(message, options);
  1017. mChatAdapter.addMsgFromReceiptToList(message);
  1018. int customMsgId = data.getIntExtra("customMsg", -1);
  1019. if (-1 != customMsgId) {
  1020. Message customMsg = mConv.getMessage(customMsgId);
  1021. mChatAdapter.addMsgToList(customMsg);
  1022. }
  1023. mChatView.setToBottom();
  1024. break;
  1025. case JGApplication.RESULT_CODE_SEND_FILE:
  1026. int[] intArrayExtra = data.getIntArrayExtra(MsgIDs);
  1027. for (int msgId : intArrayExtra) {
  1028. handleSendMsg(msgId);
  1029. }
  1030. break;
  1031. case JGApplication.RESULT_CODE_CHAT_DETAIL:
  1032. String title = data.getStringExtra(JGApplication.CONV_TITLE);
  1033. if (!mIsSingle) {
  1034. GroupInfo groupInfo = (GroupInfo) mConv.getTargetInfo();
  1035. UserInfo userInfo = groupInfo.getGroupMemberInfo(mMyInfo.getUserName(), mMyInfo.getAppKey());
  1036. //如果自己在群聊中,同时显示群人数
  1037. if (userInfo != null) {
  1038. if (TextUtils.isEmpty(title)) {
  1039. mChatView.setChatTitle(IdHelper.getString(mContext, "group"),
  1040. data.getIntExtra(MEMBERS_COUNT, 0));
  1041. } else {
  1042. mChatView.setChatTitle(title, data.getIntExtra(MEMBERS_COUNT, 0));
  1043. }
  1044. } else {
  1045. if (TextUtils.isEmpty(title)) {
  1046. mChatView.setChatTitle(IdHelper.getString(mContext, "group"));
  1047. } else {
  1048. mChatView.setChatTitle(title);
  1049. }
  1050. mChatView.dismissGroupNum();
  1051. }
  1052. } else mChatView.setChatTitle(title);
  1053. if (data.getBooleanExtra("deleteMsg", false)) {
  1054. mChatAdapter.clearMsgList();
  1055. }
  1056. break;
  1057. }
  1058. if (resultCode != Activity.RESULT_OK) {
  1059. return;
  1060. }
  1061. switch (requestCode) {
  1062. case RequestCode.PICK_IMAGE://4
  1063. onPickImageActivityResult(data);
  1064. break;
  1065. case RequestCode.TAKE_PHOTO:
  1066. takePhotoResultBack();
  1067. break;
  1068. case RequestCode.TAKE_VIDEO:
  1069. if (data != null) {
  1070. String path = data.getStringExtra("video");
  1071. try {
  1072. FileContent fileContent = new FileContent(new File(path));
  1073. fileContent.setStringExtra("video", "mp4");
  1074. Message msg = mConv.createSendMessage(fileContent);
  1075. handleSendMsg(msg.getId());
  1076. } catch (Exception e) {
  1077. e.printStackTrace();
  1078. }
  1079. }
  1080. break;
  1081. }
  1082. }
  1083. /**
  1084. * 拍照成功后发送消息
  1085. */
  1086. private void takePhotoResultBack() {
  1087. try {
  1088. String filePath = StorageUtil.getWritePath(this, "camera_cache.jpg", StorageType.TYPE_TEMP);
  1089. Log.i("ChatActivity", "take photo path:"+filePath);
  1090. List<PhotoInfo> photoInfos = new ArrayList<>();
  1091. PhotoInfo info = new PhotoInfo();
  1092. info.setAbsolutePath(filePath);
  1093. photoInfos.add(info);
  1094. sendImageAfterSelfImagePicker(photoInfos);
  1095. // InputStream in = getContentResolver().openInputStream(cameraImageUri);
  1096. // Bitmap bitmap = BitmapFactory.decodeStream(in);
  1097. // ImageContent.createImageContentAsync(bitmap, new ImageContent.CreateImageContentCallback() {
  1098. // @Override
  1099. // public void gotResult(int responseCode, String responseMessage, ImageContent imageContent) {
  1100. // if (responseCode == 0) {
  1101. // Message msg = mConv.createSendMessage(imageContent);
  1102. // handleSendMsg(msg.getId());
  1103. // }
  1104. // }
  1105. // });
  1106. Log.i("ChatActivity", "camera take photo success");
  1107. } catch (Exception e) {
  1108. e.printStackTrace();
  1109. Toast.makeText(this, "获取照片异常", Toast.LENGTH_SHORT).show();
  1110. }
  1111. }
  1112. /**
  1113. * 图片选取回调
  1114. */
  1115. private void onPickImageActivityResult( Intent data) {
  1116. if (data == null) {
  1117. return;
  1118. }
  1119. // boolean local = data.getBooleanExtra(Extras.EXTRA_FROM_LOCAL, false);
  1120. // if (local) {
  1121. // // 本地相册
  1122. // sendImageAfterSelfImagePicker(data);
  1123. // }
  1124. List<String> results= data.getStringArrayListExtra(PicturePicker.FANCY_PICTURE_PICKER_ARRAY_LIST_RESULT_KEY());
  1125. List<PhotoInfo> photoInfos = new ArrayList<>();
  1126. if (results!=null && !results.isEmpty()) {
  1127. for (int i = 0; i < results.size(); i++) {
  1128. Log.i("ChatActivity", "choose photo ..."+results.get(i));
  1129. PhotoInfo info = new PhotoInfo();
  1130. info.setAbsolutePath(results.get(i));
  1131. photoInfos.add(info);
  1132. }
  1133. }
  1134. sendImageAfterSelfImagePicker(photoInfos);
  1135. }
  1136. /**
  1137. * 发送图片
  1138. */
  1139. private void sendImageAfterSelfImagePicker(List<PhotoInfo> photoInfos) {
  1140. SendImageHelper.sendImageAfterSelfFancyImagePicker(this, photoInfos, false, new SendImageHelper.Callback() {
  1141. @Override
  1142. public void sendImage(final File file, boolean isOrig) {
  1143. Log.i("ChatActivity", "start sendImage...file:"+file.getAbsolutePath()+ ", isOrig:"+isOrig);
  1144. //所有图片都在这里拿到
  1145. ImageContent.createImageContentAsync(file, new ImageContent.CreateImageContentCallback() {
  1146. @Override
  1147. public void gotResult(int responseCode, String responseMessage, ImageContent imageContent) {
  1148. if (responseCode == 0) {
  1149. Message msg = mConv.createSendMessage(imageContent);
  1150. handleSendMsg(msg.getId());
  1151. }
  1152. }
  1153. });
  1154. }
  1155. });
  1156. }
  1157. //发送极光熊
  1158. private void OnSendImage(String iconUri) {
  1159. String substring = iconUri.substring(7);
  1160. File file = new File(substring);
  1161. ImageContent.createImageContentAsync(file, new ImageContent.CreateImageContentCallback() {
  1162. @Override
  1163. public void gotResult(int responseCode, String responseMessage, ImageContent imageContent) {
  1164. if (responseCode == 0) {
  1165. imageContent.setStringExtra("jiguang", "xiong");
  1166. Message msg = mConv.createSendMessage(imageContent);
  1167. handleSendMsg(msg.getId());
  1168. } else {
  1169. ToastUtil.shortToast(mContext, responseMessage);
  1170. }
  1171. }
  1172. });
  1173. }
  1174. /**
  1175. * 处理发送图片,刷新界面
  1176. *
  1177. * @param data intent
  1178. */
  1179. private void handleSendMsg(int data) {
  1180. mChatAdapter.setSendMsgs(data);
  1181. mChatView.setToBottom();
  1182. }
  1183. private static class UIHandler extends Handler {
  1184. private final WeakReference<ChatActivity> mActivity;
  1185. public UIHandler(ChatActivity activity) {
  1186. mActivity = new WeakReference<ChatActivity>(activity);
  1187. }
  1188. @Override
  1189. public void handleMessage(android.os.Message msg) {
  1190. super.handleMessage(msg);
  1191. ChatActivity activity = mActivity.get();
  1192. if (activity != null) {
  1193. switch (msg.what) {
  1194. case REFRESH_LAST_PAGE:
  1195. activity.mChatAdapter.dropDownToRefresh();
  1196. activity.mChatView.getListView().onDropDownComplete();
  1197. if (activity.mChatAdapter.isHasLastPage()) {
  1198. if (Build.VERSION.SDK_INT >= 21) {
  1199. activity.mChatView.getListView()
  1200. .setSelectionFromTop(activity.mChatAdapter.getOffset(),
  1201. activity.mChatView.getListView().getHeaderHeight());
  1202. } else {
  1203. activity.mChatView.getListView().setSelection(activity.mChatAdapter
  1204. .getOffset());
  1205. }
  1206. activity.mChatAdapter.refreshStartPosition();
  1207. } else {
  1208. activity.mChatView.getListView().setSelection(0);
  1209. }
  1210. //显示上一页的消息数18条
  1211. activity.mChatView.getListView()
  1212. .setOffset(activity.mChatAdapter.getOffset());
  1213. break;
  1214. case REFRESH_GROUP_NAME:
  1215. if (activity.mConv != null) {
  1216. int num = msg.getData().getInt(MEMBERS_COUNT);
  1217. String groupName = msg.getData().getString(GROUP_NAME);
  1218. activity.mChatView.setChatTitle(groupName, num);
  1219. }
  1220. break;
  1221. case REFRESH_GROUP_NUM:
  1222. int num = msg.getData().getInt(MEMBERS_COUNT);
  1223. activity.mChatView.setChatTitle(R.string.group, num);
  1224. break;
  1225. case REFRESH_CHAT_TITLE:
  1226. if (activity.mGroupInfo != null) {
  1227. //检查自己是否在群组中
  1228. UserInfo info = activity.mGroupInfo.getGroupMemberInfo(activity.mMyInfo.getUserName(),
  1229. activity.mMyInfo.getAppKey());
  1230. if (!TextUtils.isEmpty(activity.mGroupInfo.getGroupName())) {
  1231. if (info != null) {
  1232. activity.mChatView.setChatTitle(activity.mTitle,
  1233. activity.mGroupInfo.getGroupMembers().size());
  1234. activity.mChatView.showRightBtn();
  1235. } else {
  1236. activity.mChatView.setChatTitle(activity.mTitle);
  1237. activity.mChatView.dismissRightBtn();
  1238. }
  1239. }
  1240. }
  1241. break;
  1242. }
  1243. }
  1244. }
  1245. }
  1246. }