/**
  * 处理发送图片,刷新界面
  *
  * @param data intent
  */
 private void handleImgRefresh(Intent data) {
   String targetId = data.getStringExtra(JChatDemoApplication.TARGET_ID);
   long groupId = data.getLongExtra(JChatDemoApplication.GROUP_ID, 0);
   Log.i(TAG, "Refresh Image groupId: " + groupId);
   // 判断是否在当前会话中发图片
   if (targetId != null) {
     if (targetId.equals(mChatController.getTargetId())) {
       mChatController
           .getAdapter()
           .setSendImg(targetId, data.getIntArrayExtra(JChatDemoApplication.MsgIDs));
       mChatView.setToBottom();
     }
   } else if (groupId != 0) {
     if (groupId == mChatController.getGroupId()) {
       mChatController
           .getAdapter()
           .setSendImg(groupId, data.getIntArrayExtra(JChatDemoApplication.MsgIDs));
       mChatView.setToBottom();
     }
   }
 }
 @Override
 protected void onStop() {
   mChatController.getAdapter().stopMediaPlayer();
   if (mChatView.getMoreMenu().getVisibility() == View.VISIBLE) {
     mChatView.dismissMoreMenu();
   }
   if (mChatController.getConversation() != null)
     mChatController.getConversation().resetUnreadCount();
   Log.i(TAG, "[Life cycle] - onStop");
   super.onStop();
 }
 @Override
 public void onCreate(Bundle savedInstanceState) {
   // TODO Auto-generated method stub
   super.onCreate(savedInstanceState);
   JMessageClient.registerEventReceiver(this);
   setContentView(R.layout.activity_chat);
   mChatView = (ChatView) findViewById(R.id.chat_view);
   mChatView.initModule(mDensity, mDensityDpi);
   mChatController = new ChatController(mChatView, this);
   mChatView.setListeners(mChatController);
   mChatView.setOnTouchListener(mChatController);
   mChatView.setOnSizeChangedListener(mChatController);
   mChatView.setOnKbdStateListener(mChatController);
   initReceiver();
 }
 @Override
 public void onBackPressed() {
   Log.d(TAG, "onBackPressed!");
   if (RecordVoiceBtnController.mIsPressed) {
     mChatView.dismissRecordDialog();
     mChatView.releaseRecorder();
     RecordVoiceBtnController.mIsPressed = false;
   }
   if (mChatView.getMoreMenu().getVisibility() == View.VISIBLE) {
     mChatView.dismissMoreMenu();
   } else {
     mChatController.resetUnreadMsg();
   }
   super.onBackPressed();
 }
 /*
 重写BaseActivity的handleMsg()方法
  */
 @Override
 public void handleMsg(android.os.Message msg) {
   switch (msg.what) {
     case JChatDemoApplication.REFRESH_GROUP_NAME:
       if (mChatController.getConversation() != null) {
         int num = msg.getData().getInt("membersCount");
         String groupName = msg.getData().getString(JChatDemoApplication.GROUP_NAME);
         mChatView.setChatTitle(groupName, num);
       }
       break;
     case JChatDemoApplication.REFRESH_GROUP_NUM:
       int num = msg.getData().getInt("membersCount");
       mChatView.setChatTitle(ChatActivity.this.getString(R.string.group), num);
       break;
   }
 }
 /** 释放资源 */
 @Override
 protected void onDestroy() {
   // TODO Auto-generated method stub
   JMessageClient.unRegisterEventReceiver(this);
   unregisterReceiver(mReceiver);
   mChatController.releaseMediaPlayer();
   mChatView.releaseRecorder();
   super.onDestroy();
 }
 @Override
 protected void onResume() {
   if (!RecordVoiceBtnController.mIsPressed) mChatView.dismissRecordDialog();
   String targetID = getIntent().getStringExtra(JChatDemoApplication.TARGET_ID);
   boolean isGroup = getIntent().getBooleanExtra(JChatDemoApplication.IS_GROUP, false);
   if (isGroup) {
     try {
       long groupID = getIntent().getLongExtra(JChatDemoApplication.GROUP_ID, 0);
       if (groupID == 0) {
         JMessageClient.enterGroupConversation(Long.parseLong(targetID));
       } else JMessageClient.enterGroupConversation(groupID);
     } catch (NumberFormatException nfe) {
       nfe.printStackTrace();
     }
   } else if (null != targetID) {
     JMessageClient.enterSingleConversaion(targetID);
   }
   mChatController.getAdapter().initMediaPlayer();
   Log.i(TAG, "[Life cycle] - onResume");
   super.onResume();
 }
  /**
   * 用于处理拍照发送图片返回结果以及从其他界面回来后刷新聊天标题 或者聊天消息
   *
   * @param requestCode 请求码
   * @param resultCode 返回码
   * @param data intent
   */
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_CANCELED) {
      return;
    }
    if (requestCode == JChatDemoApplication.REQUEST_CODE_TAKE_PHOTO) {
      final Conversation conv = mChatController.getConversation();
      try {
        String originPath = mChatController.getPhotoPath();
        Bitmap bitmap = BitmapLoader.getBitmapFromFile(originPath, 720, 1280);
        ImageContent.createImageContentAsync(
            bitmap,
            new ImageContent.CreateImageContentCallback() {
              @Override
              public void gotResult(int status, String desc, ImageContent imageContent) {
                if (status == 0) {
                  Message msg = conv.createSendMessage(imageContent);
                  Intent intent = new Intent();
                  intent.putExtra(JChatDemoApplication.MsgIDs, new int[] {msg.getId()});
                  if (conv.getType() == ConversationType.group) {
                    intent.putExtra(
                        JChatDemoApplication.GROUP_ID,
                        ((GroupInfo) conv.getTargetInfo()).getGroupID());
                  } else {
                    intent.putExtra(
                        JChatDemoApplication.TARGET_ID,
                        ((UserInfo) conv.getTargetInfo()).getUserName());
                  }
                  handleImgRefresh(intent);
                }
              }
            });
      } catch (NullPointerException e) {
        Log.i(TAG, "onActivityResult unexpected result");
      }
    } else if (resultCode == JChatDemoApplication.RESULT_CODE_SELECT_PICTURE) {
      handleImgRefresh(data);
    } else if (resultCode == JChatDemoApplication.RESULT_CODE_CHAT_DETAIL) {
      if (mChatController.isGroup()) {
        GroupInfo groupInfo = (GroupInfo) mChatController.getConversation().getTargetInfo();
        UserInfo userInfo = groupInfo.getGroupMemberInfo(JMessageClient.getMyInfo().getUserName());
        // 如果自己在群聊中,同时显示群人数
        if (userInfo != null) {
          if (TextUtils.isEmpty(data.getStringExtra(JChatDemoApplication.NAME))) {
            mChatView.setChatTitle(
                this.getString(R.string.group), data.getIntExtra("currentCount", 0));
          } else {
            mChatView.setChatTitle(
                data.getStringExtra(JChatDemoApplication.NAME),
                data.getIntExtra("currentCount", 0));
          }
        } else {
          if (TextUtils.isEmpty(data.getStringExtra(JChatDemoApplication.NAME))) {
            mChatView.setChatTitle(this.getString(R.string.group));
            mChatView.dismissGroupNum();
          } else {
            mChatView.setChatTitle(data.getStringExtra(JChatDemoApplication.NAME));
            mChatView.dismissGroupNum();
          }
        }

      } else mChatView.setChatTitle(data.getStringExtra(JChatDemoApplication.NAME));
      if (data.getBooleanExtra("deleteMsg", false)) {
        mChatController.getAdapter().clearMsgList();
      }
    } else if (resultCode == JChatDemoApplication.RESULT_CODE_FRIEND_INFO) {
      if (!mChatController.isGroup()) {
        String nickname = data.getStringExtra(JChatDemoApplication.NICKNAME);
        if (nickname != null) {
          mChatView.setChatTitle(nickname);
        }
      }
    }
  }