@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;
  }
  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();
      }
    }
  }
 @Override
 protected void onDestroy() {
   super.onDestroy();
   chatPanelView.unRegisteMsgReceiver();
 }