text: package com.example.nanchen.aiyaschoolpush.ui.fragment;
  
  import android.app.Activity;
  import android.content.ClipData;
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.graphics.Bitmap.CompressFormat;
  import android.media.ThumbnailUtils;
  import android.net.Uri;
  import android.os.Build;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.Toast;
  
  import com.example.nanchen.aiyaschoolpush.R;
  import com.example.nanchen.aiyaschoolpush.ui.activity.ContextMenuActivity;
  import com.example.nanchen.aiyaschoolpush.ui.activity.ImageGridActivity;
  import com.example.nanchen.aiyaschoolpush.ui.activity.VideoCallActivity;
  import com.example.nanchen.aiyaschoolpush.ui.activity.VoiceCallActivity;
  import com.example.nanchen.aiyaschoolpush.im.Constant;
  import com.example.nanchen.aiyaschoolpush.im.EmojiconExampleGroupData;
  import com.example.nanchen.aiyaschoolpush.ui.view.ChatRowVoiceCall;
  import com.hyphenate.chat.EMClient;
  import com.hyphenate.chat.EMMessage;
  import com.hyphenate.chat.EMTextMessageBody;
  import com.hyphenate.easeui.EaseConstant;
  import com.hyphenate.easeui.ui.EaseChatFragment;
  import com.hyphenate.easeui.ui.EaseChatFragment.EaseChatFragmentHelper;
  import com.hyphenate.easeui.widget.chatrow.EaseChatRow;
  import com.hyphenate.easeui.widget.chatrow.EaseCustomChatRowProvider;
  import com.hyphenate.easeui.widget.emojicon.EaseEmojiconMenu;
  import com.hyphenate.util.PathUtil;
  
  import java.io.File;
  import java.io.FileOutputStream;
  
  /**
   * @author nanchen
   * @fileName AiYaSchoolPush
   * @packageName com.example.nanchen.aiyaschoolpush.fragment
   * @date 2016/10/28  15:27
   */
  
  public class NewChatFragment extends EaseChatFragment implements EaseChatFragmentHelper{
  
      // constant start from 11 to avoid conflict with constant in base class
      private static final int ITEM_VIDEO = 11;
      private static final int ITEM_FILE = 12;
      private static final int ITEM_VOICE_CALL = 13;
      private static final int ITEM_VIDEO_CALL = 14;
  
      private static final int REQUEST_CODE_SELECT_VIDEO = 11;
      private static final int REQUEST_CODE_SELECT_FILE = 12;
      private static final int REQUEST_CODE_GROUP_DETAIL = 13;
      private static final int REQUEST_CODE_CONTEXT_MENU = 14;
      private static final int REQUEST_CODE_SELECT_AT_USER = 15;
  
  
      private static final int MESSAGE_TYPE_SENT_VOICE_CALL = 1;
      private static final int MESSAGE_TYPE_RECV_VOICE_CALL = 2;
      private static final int MESSAGE_TYPE_SENT_VIDEO_CALL = 3;
      private static final int MESSAGE_TYPE_RECV_VIDEO_CALL = 4;
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          return super.onCreateView(inflater, container, savedInstanceState);
      }
  
      @Override
      protected void setUpView() {
          super.setUpView();
          ((EaseEmojiconMenu)inputMenu.getEmojiconMenu()).addEmojiconGroup(EmojiconExampleGroupData.getData());
      }
  
      @Override
      protected void registerExtendMenuItem() {
          super.registerExtendMenuItem();
          inputMenu.registerExtendMenuItem(R.string.attach_video, R.drawable.em_chat_video_selector, ITEM_VIDEO, extendMenuItemClickListener);
          inputMenu.registerExtendMenuItem(R.string.attach_file, R.drawable.em_chat_file_selector, ITEM_FILE, extendMenuItemClickListener);
          if(chatType == Constant.CHATTYPE_SINGLE){
              inputMenu.registerExtendMenuItem(R.string.attach_voice_call, R.drawable.em_chat_voice_call_selector, ITEM_VOICE_CALL, extendMenuItemClickListener);
              inputMenu.registerExtendMenuItem(R.string.attach_video_call, R.drawable.em_chat_video_call_selector, ITEM_VIDEO_CALL, extendMenuItemClickListener);
          }
      }
  
      @Override
      public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == REQUEST_CODE_CONTEXT_MENU) {
              switch (resultCode) {
                  case ContextMenuActivity.RESULT_CODE_COPY: // copy
                      clipboard.setPrimaryClip(ClipData.newPlainText(null,
                              ((EMTextMessageBody) contextMenuMessage.getBody()).getMessage()));
                      break;
                  case ContextMenuActivity.RESULT_CODE_DELETE: // delete
                      conversation.removeMessage(contextMenuMessage.getMsgId());
                      messageList.refresh();
                      break;
                  default:
                      break;
              }
          }
  
          if(resultCode == Activity.RESULT_OK){
              switch (requestCode) {
                  case REQUEST_CODE_SELECT_VIDEO: //send the video
                      if (data != null) {
                          int duration = data.getIntExtra("dur", 0);
                          String videoPath = data.getStringExtra("path");
                          File file = new File(PathUtil.getInstance().getImagePath(), "thvideo" + System.currentTimeMillis());
                          try {
                              FileOutputStream fos = new FileOutputStream(file);
                              Bitmap ThumbBitmap = ThumbnailUtils.createVideoThumbnail(videoPath, 3);
                              ThumbBitmap.compress(CompressFormat.JPEG, 100, fos);
                              fos.close();
                              sendVideoMessage(videoPath, file.getAbsolutePath(), duration);
                          } catch (Exception e) {
                              e.printStackTrace();
                          }
                      }
                      break;
                  case REQUEST_CODE_SELECT_FILE: //send the file
                      if (data != null) {
                          Uri uri = data.getData();
                          if (uri != null) {
                              sendFileByUri(uri);
                          }
                      }
                      break;
                  //end of red packet code
                  default:
                      break;
              }
          }
      }
  
      @Override
      public boolean onExtendMenuItemClick(int itemId, View view) {
          switch (itemId) {
              case ITEM_VIDEO:
                  Intent intent = new Intent(getActivity(), ImageGridActivity.class);
                  startActivityForResult(intent, REQUEST_CODE_SELECT_VIDEO);
                  break;
              case ITEM_FILE: //file
                  selectFileFromLocal();
                  break;
              case ITEM_VOICE_CALL:
                  startVoiceCall();
                  break;
              case ITEM_VIDEO_CALL:
                  startVideoCall();
                  break;
              //end of red packet code
              default:
                  break;
          }
          //keep exist extend menu
          return false;
      }
  
      /**
       * select file
       */
      protected void selectFileFromLocal() {
          Intent intent = null;
          if (Build.VERSION.SDK_INT < 19) { //api 19 and later, we can't use this way, demo just select from images
              intent = new Intent(Intent.ACTION_GET_CONTENT);
              intent.setType("*/*");
              intent.addCategory(Intent.CATEGORY_OPENABLE);
  
          } else {
              intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
          }
          startActivityForResult(intent, REQUEST_CODE_SELECT_FILE);
      }
  
      /**
       * make a voice call
       */
      protected void startVoiceCall() {
          if (!EMClient.getInstance().isConnected()) {
              Toast.makeText(getActivity(), R.string.not_connect_to_server, Toast.LENGTH_SHORT).show();
          } else {
              startActivity(new Intent(getActivity(), VoiceCallActivity.class).putExtra("username", toChatUsername)
                      .putExtra("isComingCall", false));
              // voiceCallBtn.setEnabled(false);
              inputMenu.hideExtendMenuContainer();
          }
      }
  
      /**
       * make a video call
       */
      protected void startVideoCall() {
          if (!EMClient.getInstance().isConnected())
              Toast.makeText(getActivity(), R.string.not_connect_to_server, Toast.LENGTH_SHORT).show();
          else {
              startActivity(new Intent(getActivity(), VideoCallActivity.class).putExtra("username", toChatUsername)
                      .putExtra("isComingCall", false));
              // videoCallBtn.setEnabled(false);
              inputMenu.hideExtendMenuContainer();
          }
      }
  
  
  
      @Override
      public void onSetMessageAttributes(EMMessage message) {
  
      }
  
      @Override
      public void onEnterToChatDetails() {
  
      }
  
      @Override
      public void onAvatarClick(String username) {
  
      }
  
      @Override
      public void onAvatarLongClick(String username) {
  
      }
  
  
      @Override
      public boolean onMessageBubbleClick(EMMessage message) {
          // 消息框点击事件
          return false;
      }
  
      @Override
      public void onMessageBubbleLongClick(EMMessage message) {
  // no message forward when in chat room
          startActivityForResult((new Intent(getActivity(), ContextMenuActivity.class)).putExtra("message",message)
                          .putExtra("ischatroom", chatType == EaseConstant.CHATTYPE_CHATROOM),
                  REQUEST_CODE_CONTEXT_MENU);
      }
  
      @Override
      public EaseCustomChatRowProvider onSetCustomChatRowProvider() {
          return new CustomChatRowProvider();
      }
  
  
      /**
       * chat row provider
       *
       */
      private final class CustomChatRowProvider implements EaseCustomChatRowProvider {
          @Override
          public int getCustomChatRowTypeCount() {
              //here the number is the message type in EMMessage::Type
              //which is used to count the number of different chat row
              return 10;
          }
  
          @Override
          public int getCustomChatRowType(EMMessage message) {
              if(message.getType() == EMMessage.Type.TXT){
                  //voice call
                  if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false)){
                      return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VOICE_CALL : MESSAGE_TYPE_SENT_VOICE_CALL;
                  }else if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VIDEO_CALL, false)){
                      //video call
                      return message.direct() == EMMessage.Direct.RECEIVE ? MESSAGE_TYPE_RECV_VIDEO_CALL : MESSAGE_TYPE_SENT_VIDEO_CALL;
                  }
              }
              return 0;
          }
  
          @Override
          public EaseChatRow getCustomChatRow(EMMessage message, int position, BaseAdapter adapter) {
              if(message.getType() == EMMessage.Type.TXT){
                  // voice call or video call
                  if (message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false) ||
                          message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VIDEO_CALL, false)){
                      return new ChatRowVoiceCall(getActivity(), message, position, adapter);
                  }
              }
              return null;
          }
  
      }
  }
  