@Override public void handleMessage(Message msg) { try { int countDown = mDataArrays.get(msg.what).getCountDown(); mDataArrays.get(msg.what).setCountDown(countDown - 1); countDowmList.set(msg.what, countDown); if (countDown == 1) { Timer tempTimer = timers.get(msg.what); if (tempTimer != null) { tempTimer.cancel(); tempTimer = null; } ChatMsgEntity entity = (ChatMsgEntity) msg.obj; ChatMsgEntity afterReadEntity = UserUtils.deleteMsgAfterRead( getResources().getString(R.string.del_after_read), friendName, entity); mDataArrays.set(msg.what, afterReadEntity); mAdapter.notifyDataSetChanged(); } Timber.d("countDown:" + countDown); mAdapter.notifyDataSetChanged(); } catch (Exception e) { e.printStackTrace(); } }
@Override public void updateChatList(ChatMsgEntity entity) { mDataArrays.add(entity); mAdapter.notifyDataSetChanged(); singleChatLv.setSelection(singleChatLv.getCount() - 1); delMsgAfterRead(mDataArrays.size(), entity); // handler处理阅后即焚消息 countDowmList.add(COUNT_DOWN); }
@Override public void refreshData() { int onLoadDataCount = addData(); if (onLoadDataCount > 0) { mAdapter.notifyDataSetChanged(); singleChatLv.setSelectionFromTop( onLoadDataCount + 1, listViewHeadView.getHeight() + mmPullDownView.getTopViewHeight()); } else { singleChatLv.setSelectionFromTop(1, mmPullDownView.getTopViewHeight()); } }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case Menu.FIRST + 1: if (BluetoothUtils.isBluetoothEnabled()) { item.setIcon(R.drawable.btn_connect_indicator_on); } else { item.setIcon(R.drawable.btn_connect_indicator_off); } startActivity(new Intent(ChatActivity.this, BluetoothSettingActivity.class)); break; case Menu.FIRST + 2: mShakeSensor.setIsMultiGame(false); mShakeSensor.unRegisterListener(); singleChat.sendMsg(friendName, Const.Game.REQUESTCMD + Const.Game.DISCONNECTCMD); singleChatRl.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); try { LinkcubeApplication.toyServiceCall.cacheSexPositionMode(0); } catch (RemoteException e) { e.printStackTrace(); } chatPanelView.stopGame(); ChatMsgEntity lastEntity = UserUtils.sendToFriendMsg( getResources().getString(R.string.you_stop_this_game), friendName); mDataArrays.add(lastEntity); mAdapter.notifyDataSetChanged(); singleChatLv.setSelection(singleChatLv.getCount() - 1); UserManager.getInstance().setMultiPlaying(false); UserManager.getInstance().setPlayingTarget(null); isStartGame = false; ischatPopupRlPop = false; supportInvalidateOptionsMenu(); Toast.makeText(this, R.string.toast_close_game, Toast.LENGTH_LONG).show(); break; case android.R.id.home: // chatForFriendMap.put(friendName, countDowmList); this.finish(); break; default: break; } return false; }
@Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_send: // 发送普通消息 if (!isFriend) { Toast.makeText(this, R.string.toast_not_my_friend_add_again, Toast.LENGTH_SHORT).show(); } String sendMsg = sendMsgEt.getText().toString(); if (sendMsg.equals("")) { } else { ChatMsgEntity entity = UserUtils.sendToFriendMsg(sendMsg, friendName); mDataArrays.add(entity); mAdapter.notifyDataSetChanged(); singleChat.sendMsg(friendName, sendMsg); sendMsgEt.setText(""); singleChatLv.setSelection(singleChatLv.getCount() - 1); delMsgAfterRead(mDataArrays.size(), entity); // handler处理阅后即焚消息 countDowmList.add(COUNT_DOWN); } break; case R.id.img_btn_control_toy: // 点开显示聊天界面游戏层,准备发起游戏 // 打开多人游戏控制界面 inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); if (ischatPopupRlPop == false) { inputManager.hideSoftInputFromWindow(sendMsgEt.getWindowToken(), 0); singleChatRl.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 2.0f)); ischatPopupRlPop = true; } else { singleChatRl.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); ischatPopupRlPop = false; } break; case R.id.send_msg_et: // 发送消息edittext sendMsgEt.setFocusableInTouchMode(true); sendMsgEt.requestFocus(); singleChatRl.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); break; } }
private void initData() { mDataArrays = new ArrayList<ChatMsgEntity>(); dbDataArrays = new ArrayList<ChatMsgEntity>(); Intent intent = getIntent(); friendName = intent.getStringExtra("friendName"); Timber.d("friendName:" + friendName); listViewHeadView = getLayoutInflater().inflate(R.layout.chatting_list_header, null); singleChatLv.addHeaderView(listViewHeadView); mAdapter = new ChatMsgViewAdapter(this, mDataArrays, friendName); singleChatLv.setAdapter(mAdapter); singleChat = new SingleChat(); chatPanelView.setSingleChat(singleChat); chatPanelView.setmShakeSensor(mShakeSensor); chatPanelView.setOnUpdateChatListListener(this); chatPanelView.setOnGameListener(this); List<FriendEntity> friendEntities = new ArrayList<FriendEntity>(); PersistableFriend perFriend = new PersistableFriend(); try { friendEntities = DataManager.getInstance() .query( perFriend, FRIEND_JID + "=? and " + USER_JID + "=?", new String[] {ASmackUtils.getFriendJid(friendName), ASmackUtils.getUserJID()}, null, null, null); } catch (IOException e1) { e1.printStackTrace(); } try { friendName = ASmackUtils.deleteServerAddress(friendEntities.get(0).getFriendJid()); chatPanelView.setFriendName(friendName); if (friendEntities.get(0).getIsFriend().equals("none")) { isFriend = false; } } catch (Exception e) { e.printStackTrace(); } // 判断是否正在游戏中 if (UserManager.getInstance().getPlayingTarget() != null) { FriendEntity friendEntity = UserManager.getInstance().getPlayingTarget(); Timber.d("正在游戏中的FriendJid:" + friendEntity.getFriendJid()); if (friendEntity != null && friendEntity.getFriendJid().equals(ASmackUtils.getFriendJid(friendName))) { chatPanelView.keepGaming(); } } List<ChatEntity> chats = new ArrayList<ChatEntity>(); try { PersistableChat perChat = new PersistableChat(); chats = DataManager.getInstance() .query( perChat, USER_NAME + "=? and " + FRIEND_NAME + "=?", new String[] {ASmackUtils.getRosterName(), friendName}, null, null, null); } catch (IOException e) { e.printStackTrace(); } if (chatForFriendMap.get(friendName) != null) { countDowmList = chatForFriendMap.get(friendName); } else { chatForFriendMap.put(friendName, countDowmList); } if (chats.size() == 0) { countDowmList.clear(); } for (int i = 0; i < chats.size(); i++) { try { ChatEntity chat = chats.get(i); Timber.d("IsAfterRead:" + chat.getIsAfterRead()); ChatMsgEntity entity = new ChatMsgEntity(); entity.setDate(chat.getMsgTime()); // TimeUtils.toNowTime() if (chat.getMsgFlag().equals("get")) { entity.setName(chat.getFriendNickname()); entity.setMsgType(true); } else if (chat.getMsgFlag().equals("send")) { entity.setName(UserManager.getInstance().getUserInfo().getNickName()); entity.setMsgType(false); } if (i < countDowmList.size()) { entity.setCountDown(countDowmList.get(i)); } else { entity.setCountDown(COUNT_DOWN); countDowmList.add(COUNT_DOWN); } if (chat.getIsAfterRead() == 1) { entity.setText(getResources().getString(R.string.del_after_read)); entity.setCountDown(1); } else { entity.setText(chat.getMessage()); } dbDataArrays.add(entity); delMsgAfterRead(dbDataArrays.size(), entity); // handler处理阅后即焚消息 } catch (Exception e) { e.printStackTrace(); } } dataCount = dbDataArrays.size(); if (dbDataArrays.size() != 0) { refreshCount = dbDataArrays.size(); addData(); } mAdapter.notifyDataSetChanged(); singleChatLv.setSelection(singleChatLv.getCount() - 1); // 如果最近一条消息是游戏邀请,且时间少于30s,则弹出接受拒绝界面 ChatMsgEntity chatMsgEntity = GameManager.getInstance().getGameInviteMsgs(friendName); if (chatMsgEntity != null) { SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // TimeUtils.getNowTime()-chatMsgEntity.getDate() Date nowTime = new Date(); Date gameInviteTime = new Date(); int acceptSecond = 0; try { nowTime = simpleFormat.parse(TimeUtils.getNowDateAndTime()); gameInviteTime = simpleFormat.parse(chatMsgEntity.getDate()); acceptSecond = (int) (30 - (nowTime.getTime() - gameInviteTime.getTime()) / 1000); } catch (ParseException e) { e.printStackTrace(); } if (chatMsgEntity.getText().equals(GAME_INVITE_FROM)) { chatPanelView.fromGameInviteMsg(acceptSecond); popUpPanel(); } else if (chatMsgEntity.getText().equals(GAME_INVITE_TO)) { chatPanelView.toGameInviteMsg(acceptSecond); popUpPanel(); } } }