/**
   * 消息分发,选择跳转到哪个fragment
   *
   * @param intent
   */
  private void enterFragment(Intent intent) {
    String tag = null;

    if (intent != null) {
      Fragment fragment = null;

      if (intent.getExtras() != null && intent.getExtras().containsKey(RongConst.EXTRA.CONTENT)) {
        String fragmentName = intent.getExtras().getString(RongConst.EXTRA.CONTENT);
        fragment = Fragment.instantiate(this, fragmentName);
      } else if (intent.getData() != null) {

        if (intent.getData().getPathSegments().get(0).equals("conversation")) {
          tag = "conversation";
          String fragmentName = ConversationFragment.class.getCanonicalName();
          fragment = Fragment.instantiate(this, fragmentName);

        } else if (intent.getData().getLastPathSegment().equals("conversationlist")) {
          tag = "conversationlist";
          String fragmentName = ConversationListFragment.class.getCanonicalName();
          fragment = Fragment.instantiate(this, fragmentName);
        } else if (intent.getData().getLastPathSegment().equals("subconversationlist")) {
          tag = "subconversationlist";
          String fragmentName = SubConversationListFragment.class.getCanonicalName();
          fragment = Fragment.instantiate(this, fragmentName);
          SubConversationListFragment sub = (SubConversationListFragment) fragment;
          sub.setAdapter(new SubConversationListAdapterEx(RongContext.getInstance()));
        } else if (intent.getData().getPathSegments().get(0).equals("friend")) {
          tag = "friend";
          String fragmentName = FriendMultiChoiceFragment.class.getCanonicalName();
          fragment = Fragment.instantiate(this, fragmentName);
          ActionBar actionBar = getSupportActionBar();
          actionBar.hide(); // 隐藏ActionBar
        }

        targetId = intent.getData().getQueryParameter("targetId");
        targetIds = intent.getData().getQueryParameter("targetIds");
        mDiscussionId = intent.getData().getQueryParameter("discussionId");

        if (targetId != null) {
          mConversationType =
              Conversation.ConversationType.valueOf(
                  intent.getData().getLastPathSegment().toUpperCase(Locale.getDefault()));
        } else if (targetIds != null) {
          mConversationType =
              Conversation.ConversationType.valueOf(
                  intent.getData().getLastPathSegment().toUpperCase(Locale.getDefault()));
        }
      }

      if ("tag".equals(tag)) {
        showRealTimeLocationBar(null); // RealTimeLocation
      }

      if (fragment != null) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.de_content, fragment, tag);
        transaction.addToBackStack(null).commitAllowingStateLoss();
      }
    }
  }
  private void initData() {
    if (mConversationType.equals(Conversation.ConversationType.DISCUSSION)) {
      RongIM.getInstance()
          .getRongIMClient()
          .getDiscussion(
              mTargetId,
              new RongIMClient.ResultCallback<Discussion>() {
                @Override
                public void onSuccess(Discussion discussion) {
                  mIdList = discussion.getMemberIdList();
                  mAdapter.setCreatorId(discussion.getCreatorId());
                  Message msg = new Message();
                  msg.what = PREPARE_LIST;
                  msg.obj = mIdList;
                  getHandler().sendMessage(msg);
                }

                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {
                  getHandler().sendEmptyMessage(SHOW_TOAST);
                }
              });
    } else if (mConversationType.equals(Conversation.ConversationType.PRIVATE)) {
      mIdList.add(mTargetId);
      Message msg = new Message();
      msg.what = PREPARE_LIST;
      msg.obj = mIdList;
      getHandler().sendMessage(msg);
    }
  }
 private void joinRealTimeLocation() {
   RongIMClient.getInstance().joinRealTimeLocation(mConversationType, targetId);
   Intent intent = new Intent(RongActivity.this, RealTimeLocationActivity.class);
   intent.putExtra("conversationType", mConversationType.getValue());
   intent.putExtra("targetId", targetId);
   startActivity(intent);
 }
  @Override
  public boolean onCreateOptionsMenu(final Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.de_conversation_menu, menu);

    if (mConversationType != null) {
      if (mConversationType.equals(Conversation.ConversationType.CHATROOM)) {
        menu.getItem(0).setVisible(false);
      } else if (mConversationType.equals(Conversation.ConversationType.DISCUSSION)
          && isDiscussion) {
        menu.getItem(0).setVisible(false);
      }
    }

    return super.onCreateOptionsMenu(menu);
  }
  /**
   * 收到 push 以后,打开会话页面
   *
   * @param conversation
   * @param targetId
   * @param conversationType
   */
  private void openConversationFragment(
      String conversation, String targetId, String conversationType) {

    String tag;

    if (conversation.equals("conversation")) {
      tag = "conversation";
      ConversationFragment conversationFragment = new ConversationFragment();

      Uri uri =
          Uri.parse("rong://" + getApplicationInfo().packageName)
              .buildUpon()
              .appendPath("conversation")
              .appendPath(conversationType.toLowerCase())
              .appendQueryParameter("targetId", targetId)
              .build();

      conversationFragment.setUri(uri);
      mConversationType = Conversation.ConversationType.valueOf(conversationType);

      showRealTimeLocationBar(null); // RealTimeLocation

      if (conversationFragment != null) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.de_content, conversationFragment, tag);
        transaction.addToBackStack(null).commitAllowingStateLoss();
      }
    }
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent;

    RongContext.getInstance().getEventBus().register(this);

    if (getActivity() != null) {

      intent = getActivity().getIntent();

      if (intent.getData() != null) {

        mConversationType =
            Conversation.ConversationType.valueOf(
                intent.getData().getLastPathSegment().toUpperCase());

        mTargetId = intent.getData().getQueryParameter("targetId");
      }
    }
    mAdapter = new RongConversationAddMemberAdapter(getActivity());
    mAdapter.setDeleteIconListener(this);

    if (RongIM.getInstance() != null && RongIM.getInstance().getRongIMClient() != null) {
      initData();
    }
  }
  /** 根据 targetid 和 ConversationType 进入到设置页面 */
  private void enterSettingActivity() {

    if (mConversationType == Conversation.ConversationType.PUBLIC_SERVICE
        || mConversationType == Conversation.ConversationType.APP_PUBLIC_SERVICE) {

      RongIM.getInstance().startPublicServiceProfile(this, mConversationType, targetId);
    } else {
      // 通过targetId 和 会话类型 打开指定的设置页面
      if (!TextUtils.isEmpty(targetId)) {

        Uri uri =
            Uri.parse("demo://" + getApplicationInfo().packageName)
                .buildUpon()
                .appendPath("conversationSetting")
                .appendPath(mConversationType.getName())
                .appendQueryParameter("targetId", targetId)
                .build();

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(uri);
        startActivity(intent);
        // 当你刚刚创建完讨论组以后获得的是 targetIds
      } else if (!TextUtils.isEmpty(targetIds)) {

        UriFragment fragment = (UriFragment) getSupportFragmentManager().getFragments().get(0);
        fragment.getUri();
        // 得到讨论组的 targetId
        targetId = fragment.getUri().getQueryParameter("targetId");

        if (!TextUtils.isEmpty(targetId)) {
          Uri uri =
              Uri.parse("demo://" + getApplicationInfo().packageName)
                  .buildUpon()
                  .appendPath("conversationSetting")
                  .appendPath(mConversationType.getName())
                  .appendQueryParameter("targetId", targetId)
                  .build();

          Intent intent = new Intent(Intent.ACTION_VIEW);
          intent.setData(uri);
          startActivity(intent);
        } else {
          WinToast.toast(RongActivity.this, "讨论组尚未创建成功");
        }
      }
    }
  }
  protected void initView() {
    mHandler = new Handler(this);
    mDialog = new LoadingDialog(this);
    mFragmentManager = getSupportFragmentManager();
    getSupportActionBar().setTitle(R.string.main_name);
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm); // 获取屏幕信息
    indicatorWidth = dm.widthPixels / 4; // 指示器宽度为屏幕宽度的4/1

    mMainShow = (LinearLayout) findViewById(R.id.main_show);
    mMainConversationLiner = (RelativeLayout) findViewById(R.id.main_conversation_liner);
    mMainGroupLiner = (RelativeLayout) findViewById(R.id.main_group_liner);
    mMainChatroomLiner = (RelativeLayout) findViewById(R.id.main_chatroom_liner);
    mMainCustomerLiner = (RelativeLayout) findViewById(R.id.main_customer_liner);
    mMainConversationTv = (TextView) findViewById(R.id.main_conversation_tv);
    mMainGroupTv = (TextView) findViewById(R.id.main_group_tv);
    mMainChatroomTv = (TextView) findViewById(R.id.main_chatroom_tv);
    mMainCustomerTv = (TextView) findViewById(R.id.main_customer_tv);
    mViewPager = (ViewPager) findViewById(R.id.main_viewpager);
    mMainSelectImg = (ImageView) findViewById(R.id.main_switch_img);
    mUnreadNumView = (TextView) findViewById(R.id.de_num);
    mCustomerNoRead = (TextView) findViewById(R.id.customer_noread);

    ViewGroup.LayoutParams cursor_Params = mMainSelectImg.getLayoutParams();
    cursor_Params.width = indicatorWidth; // 初始化滑动下标的宽
    mMainSelectImg.setLayoutParams(cursor_Params);
    // 获取布局填充器
    mInflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);

    if (getIntent() != null) {
      if (getIntent().hasExtra("PUSH_TOKEN") && getIntent().hasExtra("PUSH_INTENT")) {

        Uri uri = getIntent().getParcelableExtra("PUSH_INTENT");
        String token = getIntent().getStringExtra("PUSH_TOKEN").toString();
        String pathSegments;
        String conversationType = null;
        String targetId = null;

        if (uri.getPathSegments().get(0).equals("conversation")) {
          pathSegments = uri.getPathSegments().get(0);
          conversationType =
              Conversation.ConversationType.valueOf(
                      uri.getLastPathSegment().toUpperCase(Locale.getDefault()))
                  .toString();
          targetId = uri.getQueryParameter("targetId").toString();
        } else {
          pathSegments = uri.getLastPathSegment();
        }
        reconnect(token, pathSegments, conversationType, targetId);

        if (DemoContext.getInstance() != null) {
          mGetMyGroupsRequest =
              DemoContext.getInstance().getDemoApi().getMyGroups(MainActivity.this);
        }
      }
    }
  }
  @Override
  public boolean handleMessage(android.os.Message msg) {
    switch (msg.what) {
      case PREPARE_LIST:
        List<String> mMemberInfo = (List<String>) msg.obj;
        int i = 0;
        for (String id : mMemberInfo) {
          if (i < 50) {
            UserInfo userInfo = RongContext.getInstance().getUserInfoFromCache(id);
            if (userInfo == null) {
              mMembers.add(new UserInfo(id, null, null));
            } else mMembers.add(userInfo);
          } else {
            break;
          }

          i++;
        }
        UserInfo addBtn = new UserInfo("RongAddBtn", null, null);
        mMembers.add(addBtn);

        String curUserId = RongIM.getInstance().getRongIMClient().getCurrentUserId();
        if (mAdapter.getCreatorId() != null
            && mConversationType.equals(Conversation.ConversationType.DISCUSSION)
            && curUserId.equals(mAdapter.getCreatorId())) {
          UserInfo deleteBtn = new UserInfo("RongDelBtn", null, null);
          mMembers.add(deleteBtn);
        }

        mAdapter.addCollection(mMembers);
        mAdapter.notifyDataSetChanged();
        break;
      case REMOVE_ITEM:
        int position = (Integer) msg.obj;
        mAdapter.remove(position);
        mAdapter.notifyDataSetChanged();
        break;
      case SHOW_TOAST:
        break;
    }
    return true;
  }
 public void startConversationSystem(
     Context context,
     Conversation.ConversationType conversationType,
     String targetId,
     String title) {
   if (context != null && !TextUtils.isEmpty(targetId) && conversationType != null) {
     if (RongContext.getInstance() == null) {
       throw new ExceptionInInitializerError("RongCloud SDK not init");
     } else {
       Uri uri =
           Uri.parse("rong://" + context.getApplicationInfo().processName + ".system")
               .buildUpon()
               .appendPath("conversation")
               .appendPath(conversationType.getName().toLowerCase())
               .appendQueryParameter("targetId", targetId)
               .appendQueryParameter("title", title)
               .build();
       context.startActivity(new Intent("android.intent.action.VIEW", uri));
     }
   } else {
     throw new IllegalArgumentException();
   }
 }
  /** 设置 title */
  protected void initData() {

    if (mConversationType != null) {
      if (mConversationType.equals(Conversation.ConversationType.PRIVATE)) {
        if (DemoContext.getInstance() != null) {
          UserInfos userInfos =
              mUserInfosDao
                  .queryBuilder()
                  .where(UserInfosDao.Properties.Userid.eq(targetId))
                  .unique();

          if (userInfos == null) {
            getSupportActionBar().setTitle("");
          } else {
            getSupportActionBar().setTitle(userInfos.getUsername().toString());
          }
        }
      } else if (mConversationType.equals(Conversation.ConversationType.GROUP)) {
        if (DemoContext.getInstance() != null) {
          getSupportActionBar().setTitle(DemoContext.getInstance().getGroupNameById(targetId));
        }
      } else if (mConversationType.equals(Conversation.ConversationType.DISCUSSION)) {
        if (targetId != null) {

          RongIM.getInstance()
              .getRongIMClient()
              .getDiscussion(
                  targetId,
                  new RongIMClient.ResultCallback<Discussion>() {
                    @Override
                    public void onSuccess(Discussion discussion) {
                      getSupportActionBar().setTitle(discussion.getName());
                    }

                    @Override
                    public void onError(RongIMClient.ErrorCode e) {
                      if (e.equals(RongIMClient.ErrorCode.NOT_IN_DISCUSSION)) {
                        getSupportActionBar().setTitle("不在讨论组中");
                        isDiscussion = true;
                        supportInvalidateOptionsMenu();
                      }
                    }
                  });

        } else if (targetIds != null) {
          setDiscussionName(targetIds);
        } else {
          getSupportActionBar().setTitle("讨论组");
        }
      } else if (mConversationType.equals(Conversation.ConversationType.SYSTEM)) {
        getSupportActionBar().setTitle("系统会话类型");
      } else if (mConversationType.equals(Conversation.ConversationType.CHATROOM)) {
        getSupportActionBar().setTitle("聊天室");
      } else if (mConversationType.equals(Conversation.ConversationType.CUSTOMER_SERVICE)) {
        getSupportActionBar().setTitle("客服");
      } else if (mConversationType.equals(Conversation.ConversationType.APP_PUBLIC_SERVICE)) {
        if (RongIM.getInstance() != null && RongIM.getInstance().getRongIMClient() != null) {

          RongIM.getInstance()
              .getRongIMClient()
              .getPublicServiceProfile(
                  Conversation.PublicServiceType.APP_PUBLIC_SERVICE,
                  targetId,
                  new RongIMClient.ResultCallback<PublicServiceProfile>() {
                    @Override
                    public void onSuccess(PublicServiceProfile publicServiceProfile) {
                      getSupportActionBar().setTitle(publicServiceProfile.getName().toString());
                    }

                    @Override
                    public void onError(RongIMClient.ErrorCode errorCode) {}
                  });
        }

      } else if (mConversationType.equals(Conversation.ConversationType.PUBLIC_SERVICE)) {
        if (RongIM.getInstance() != null && RongIM.getInstance().getRongIMClient() != null) {

          RongIM.getInstance()
              .getRongIMClient()
              .getPublicServiceProfile(
                  Conversation.PublicServiceType.PUBLIC_SERVICE,
                  targetId,
                  new RongIMClient.ResultCallback<PublicServiceProfile>() {
                    @Override
                    public void onSuccess(PublicServiceProfile publicServiceProfile) {
                      getSupportActionBar().setTitle(publicServiceProfile.getName().toString());
                    }

                    @Override
                    public void onError(RongIMClient.ErrorCode errorCode) {}
                  });
        }
      }
    }

    if (!TextUtils.isEmpty(targetId) && mConversationType != null) {

      RealTimeLocationConstant.RealTimeLocationErrorCode errorCode =
          RongIMClient.getInstance().getRealTimeLocation(mConversationType, targetId);
      Log.e(TAG, "register addRealTimeLocationListener:--111111111-" + errorCode);
      if (errorCode
              == RealTimeLocationConstant.RealTimeLocationErrorCode.RC_REAL_TIME_LOCATION_SUCCESS
          || errorCode
              == RealTimeLocationConstant.RealTimeLocationErrorCode
                  .RC_REAL_TIME_LOCATION_IS_ON_GOING) {
        Log.e(TAG, "register addRealTimeLocationListener:--22222222222-");
        RongIMClient.getInstance()
            .addRealTimeLocationListener(mConversationType, targetId, this); // 设置监听
        currentLocationStatus =
            RongIMClient.getInstance().getRealTimeLocationCurrentState(mConversationType, targetId);

        if (currentLocationStatus
            == RealTimeLocationConstant.RealTimeLocationStatus
                .RC_REAL_TIME_LOCATION_STATUS_INCOMING) {
          showRealTimeLocationBar(currentLocationStatus);
        }
      }
    } else {
    }
  }