/**
   * 根据消息内容和消息类型获取消息内容提示
   *
   * @param message
   * @param context
   * @return
   */
  private String getMessageDigest(EMMessage message, Context context) {
    String digest = "";
    switch (message.getType()) {
      case LOCATION: // 位置消息
        if (message.direct == EMMessage.Direct.RECEIVE) {
          // 从sdk中提到了ui中,使用更简单不犯错的获取string的方法
          // digest = EasyUtils.getAppResourceString(context,
          // "location_recv");
          digest = getStrng(context, R.string.location_recv);
          digest = String.format(digest, message.getFrom());
          return digest;
        } else {
          // digest = EasyUtils.getAppResourceString(context,
          // "location_prefix");
          digest = getStrng(context, R.string.location_prefix);
        }
        break;
      case IMAGE: // 图片消息
        ImageMessageBody imageBody = (ImageMessageBody) message.getBody();
        digest = getStrng(context, R.string.picture) + imageBody.getFileName();
        break;
      case VOICE: // 语音消息
        digest = getStrng(context, R.string.voice);
        break;
      case VIDEO: // 视频消息
        digest = getStrng(context, R.string.video);
        break;
      case TXT: // 文本消息
        if (((DemoHXSDKHelper) HXSDKHelper.getInstance()).isRobotMenuMessage(message)) {
          digest = ((DemoHXSDKHelper) HXSDKHelper.getInstance()).getRobotMenuMessageDigest(message);
        } else if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false)) {
          TextMessageBody txtBody = (TextMessageBody) message.getBody();
          digest = getStrng(context, R.string.voice_call) + txtBody.getMessage();
        } else {
          TextMessageBody txtBody = (TextMessageBody) message.getBody();
          digest = txtBody.getMessage();
        }
        break;
      case FILE: // 普通文件消息
        digest = getStrng(context, R.string.file);
        break;
      default:
        EMLog.e(TAG, "unknow type");
        return "";
    }

    return digest;
  }
 @Override
 protected void onDestroy() {
   super.onDestroy();
   HXSDKHelper.getInstance().isVideoCalling = false;
   stopMonitor();
   try {
     callHelper.setSurfaceView(null);
     cameraHelper.stopCapture();
     oppositeSurface = null;
     cameraHelper = null;
   } catch (Exception e) {
   }
 }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = inflater.inflate(R.layout.row_chat_history, parent, false);
    }
    ViewHolder holder = (ViewHolder) convertView.getTag();
    if (holder == null) {
      holder = new ViewHolder();
      holder.name = (TextView) convertView.findViewById(R.id.name);
      holder.unreadLabel = (TextView) convertView.findViewById(R.id.unread_msg_number);
      holder.message = (TextView) convertView.findViewById(R.id.message);
      holder.time = (TextView) convertView.findViewById(R.id.time);
      holder.avatar = (ImageView) convertView.findViewById(R.id.avatar);
      holder.msgState = convertView.findViewById(R.id.msg_state);
      holder.list_item_layout = (RelativeLayout) convertView.findViewById(R.id.list_item_layout);
      convertView.setTag(holder);
    }
    if (position % 2 == 0) {
      holder.list_item_layout.setBackgroundResource(R.drawable.mm_listitem);
    } else {
      holder.list_item_layout.setBackgroundResource(R.drawable.mm_listitem_grey);
    }

    // 获取与此用户/群组的会话
    EMConversation conversation = getItem(position);
    // 获取用户username或者群组groupid
    String username = conversation.getUserName();
    if (conversation.getType() == EMConversationType.GroupChat) {
      // 群聊消息,显示群聊头像
      holder.avatar.setImageResource(R.drawable.group_icon);
      EMGroup group = EMGroupManager.getInstance().getGroup(username);
      holder.name.setText(group != null ? group.getGroupName() : username);
    } else if (conversation.getType() == EMConversationType.ChatRoom) {
      holder.avatar.setImageResource(R.drawable.group_icon);
      EMChatRoom room = EMChatManager.getInstance().getChatRoom(username);
      holder.name.setText(
          room != null && !TextUtils.isEmpty(room.getName()) ? room.getName() : username);
    } else {
      UserUtils.setUserAvatar(getContext(), username, holder.avatar);
      if (username.equals(Constant.GROUP_USERNAME)) {
        holder.name.setText("群聊");

      } else if (username.equals(Constant.NEW_FRIENDS_USERNAME)) {
        holder.name.setText("申请与通知");
      }
      Map<String, RobotUser> robotMap =
          ((DemoHXSDKHelper) HXSDKHelper.getInstance()).getRobotList();
      if (robotMap != null && robotMap.containsKey(username)) {
        String nick = robotMap.get(username).getNick();
        if (!TextUtils.isEmpty(nick)) {
          holder.name.setText(nick);
        } else {
          holder.name.setText(username);
        }
      } else {
        UserUtils.setUserNick(username, holder.name);
      }
    }

    if (conversation.getUnreadMsgCount() > 0) {
      // 显示与此用户的消息未读数
      holder.unreadLabel.setText(String.valueOf(conversation.getUnreadMsgCount()));
      holder.unreadLabel.setVisibility(View.VISIBLE);
    } else {
      holder.unreadLabel.setVisibility(View.INVISIBLE);
    }

    if (conversation.getMsgCount() != 0) {
      // 把最后一条消息的内容作为item的message内容
      EMMessage lastMessage = conversation.getLastMessage();
      holder.message.setText(
          SmileUtils.getSmiledText(
              getContext(), getMessageDigest(lastMessage, (this.getContext()))),
          BufferType.SPANNABLE);

      holder.time.setText(DateUtils.getTimestampString(new Date(lastMessage.getMsgTime())));
      if (lastMessage.direct == EMMessage.Direct.SEND
          && lastMessage.status == EMMessage.Status.FAIL) {
        holder.msgState.setVisibility(View.VISIBLE);
      } else {
        holder.msgState.setVisibility(View.GONE);
      }
    }

    return convertView;
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
      finish();
      return;
    }
    setContentView(R.layout.activity_video_call);

    HXSDKHelper.getInstance().isVideoCalling = true;
    getWindow()
        .addFlags(
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

    callStateTextView = (TextView) findViewById(R.id.tv_call_state);
    comingBtnContainer = (LinearLayout) findViewById(R.id.ll_coming_call);
    rootContainer = (RelativeLayout) findViewById(R.id.root_layout);
    refuseBtn = (Button) findViewById(R.id.btn_refuse_call);
    answerBtn = (Button) findViewById(R.id.btn_answer_call);
    hangupBtn = (Button) findViewById(R.id.btn_hangup_call);
    muteImage = (ImageView) findViewById(R.id.iv_mute);
    handsFreeImage = (ImageView) findViewById(R.id.iv_handsfree);
    callStateTextView = (TextView) findViewById(R.id.tv_call_state);
    nickTextView = (TextView) findViewById(R.id.tv_nick);
    chronometer = (Chronometer) findViewById(R.id.chronometer);
    voiceContronlLayout = (LinearLayout) findViewById(R.id.ll_voice_control);
    btnsContainer = (RelativeLayout) findViewById(R.id.ll_btns);
    topContainer = (LinearLayout) findViewById(R.id.ll_top_container);
    bottomContainer = (LinearLayout) findViewById(R.id.ll_bottom_container);
    monitorTextView = (TextView) findViewById(R.id.tv_call_monitor);

    refuseBtn.setOnClickListener(this);
    answerBtn.setOnClickListener(this);
    hangupBtn.setOnClickListener(this);
    muteImage.setOnClickListener(this);
    handsFreeImage.setOnClickListener(this);
    rootContainer.setOnClickListener(this);

    msgid = UUID.randomUUID().toString();
    // 获取通话是否为接收方向的
    isInComingCall = getIntent().getBooleanExtra("isComingCall", false);
    username = getIntent().getStringExtra("username");

    // 设置通话人
    nickTextView.setText(username);

    // 显示本地图像的surfaceview
    localSurface = (SurfaceView) findViewById(R.id.local_surface);
    localSurface.setZOrderMediaOverlay(true);
    localSurface.setZOrderOnTop(true);
    localSurfaceHolder = localSurface.getHolder();

    // 获取callHelper,cameraHelper
    callHelper = EMVideoCallHelper.getInstance();
    cameraHelper = new CameraHelper(callHelper, localSurfaceHolder);

    // 显示对方图像的surfaceview
    oppositeSurface = (SurfaceView) findViewById(R.id.opposite_surface);
    oppositeSurfaceHolder = oppositeSurface.getHolder();
    // 设置显示对方图像的surfaceview
    callHelper.setSurfaceView(oppositeSurface);

    localSurfaceHolder.addCallback(new LocalCallback());
    oppositeSurfaceHolder.addCallback(new OppositeCallback());

    // 设置通话监听
    addCallStateListener();
    if (!isInComingCall) { // 拨打电话
      soundPool = new SoundPool(1, AudioManager.STREAM_RING, 0);
      outgoing = soundPool.load(this, R.raw.outgoing, 1);

      comingBtnContainer.setVisibility(View.INVISIBLE);
      hangupBtn.setVisibility(View.VISIBLE);
      String st = getResources().getString(R.string.Are_connected_to_each_other);
      callStateTextView.setText(st);

      handler.postDelayed(
          new Runnable() {
            public void run() {
              streamID = playMakeCallSounds();
            }
          },
          300);
    } else { // 有电话进来
      voiceContronlLayout.setVisibility(View.INVISIBLE);
      localSurface.setVisibility(View.INVISIBLE);
      Uri ringUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
      audioManager.setMode(AudioManager.MODE_RINGTONE);
      audioManager.setSpeakerphoneOn(true);
      ringtone = RingtoneManager.getRingtone(this, ringUri);
      ringtone.play();
    }
  }