예제 #1
0
 @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();
   }
 }
예제 #2
0
  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();
      }
    }
  }