414 lines
14 KiB
Java
414 lines
14 KiB
Java
package com.yunbao.live.views;
|
|
|
|
import android.content.Context;
|
|
import android.net.Uri;
|
|
import android.text.Editable;
|
|
import android.text.TextUtils;
|
|
import android.text.TextWatcher;
|
|
import android.util.AttributeSet;
|
|
import android.util.Log;
|
|
import android.view.View;
|
|
import android.view.inputmethod.InputMethodManager;
|
|
import android.widget.Button;
|
|
import android.widget.EditText;
|
|
import android.widget.ImageView;
|
|
import android.widget.LinearLayout;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import com.adjust.sdk.Adjust;
|
|
import com.adjust.sdk.AdjustEvent;
|
|
import com.facebook.appevents.AppEventsLogger;
|
|
import com.google.firebase.analytics.FirebaseAnalytics;
|
|
import com.yunbao.common.http.CommonHttpUtil;
|
|
import com.yunbao.common.http.HttpCallback;
|
|
import com.yunbao.common.manager.NoviceInstructorManager;
|
|
import com.yunbao.common.utils.FileUtil;
|
|
import com.yunbao.common.utils.TimeUtils;
|
|
import com.yunbao.common.utils.ToastUtil;
|
|
import com.yunbao.live.R;
|
|
import com.yunbao.live.utils.FileSizeUtil;
|
|
|
|
import io.rong.imkit.IMCenter;
|
|
import io.rong.imlib.IRongCallback;
|
|
import io.rong.imlib.RongIMClient;
|
|
import io.rong.imlib.model.Conversation;
|
|
import io.rong.imlib.model.Message;
|
|
import io.rong.message.FileMessage;
|
|
import io.rong.message.ImageMessage;
|
|
import io.rong.message.SightMessage;
|
|
import io.rong.message.TextMessage;
|
|
|
|
/**
|
|
* 自定义输入区
|
|
*/
|
|
public class InputPanelViewHolder extends LinearLayout implements View.OnClickListener {
|
|
private ImageView inputPanelAddBtn;
|
|
private EditText editBtn;
|
|
public Button inputPanelSendBtn;
|
|
//接收方的用户id
|
|
private String targetId;
|
|
//是否可以发送消息
|
|
private boolean isSend = false;
|
|
private LinearLayout pluginList;
|
|
//接口返回预置信息
|
|
private String presetInformation = "";
|
|
private boolean isAdmin = false;
|
|
|
|
public void setAdmin(boolean admin) {
|
|
isAdmin = admin;
|
|
}
|
|
|
|
public InputPanelViewHolder setPresetInformation(String presetInformation) {
|
|
this.presetInformation = presetInformation;
|
|
editBtn.setText(presetInformation);
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* 获取输入框信息
|
|
*/
|
|
public String getPresetInformation() {
|
|
return editBtn.getText().toString().trim();
|
|
}
|
|
|
|
/**
|
|
* 必传字段
|
|
*
|
|
* @param targetId 接收方的用户id
|
|
* @return
|
|
*/
|
|
public InputPanelViewHolder setTargetId(String targetId) {
|
|
this.targetId = targetId;
|
|
return this;
|
|
}
|
|
|
|
public InputPanelViewHolder(Context context) {
|
|
super(context);
|
|
}
|
|
|
|
public InputPanelViewHolder(Context context, @Nullable AttributeSet attrs) {
|
|
super(context, attrs);
|
|
View inputPanelView = View.inflate(context, R.layout.view_input_panel, this);
|
|
initView(inputPanelView);
|
|
}
|
|
|
|
/**
|
|
* 初始化布局
|
|
*/
|
|
private void initView(View inputPanelView) {
|
|
inputPanelAddBtn = inputPanelView.findViewById(R.id.input_panel_add_btn);
|
|
editBtn = inputPanelView.findViewById(R.id.edit_btn);
|
|
inputPanelSendBtn = inputPanelView.findViewById(R.id.input_panel_send_btn);
|
|
pluginList = inputPanelView.findViewById(R.id.plugin_list);
|
|
editBtn.addTextChangedListener(mEditTextWatcher);
|
|
inputPanelSendBtn.setOnClickListener(this);
|
|
inputPanelAddBtn.setOnClickListener(this);
|
|
editBtn.setOnClickListener(this);
|
|
inputPanelView.findViewById(R.id.lt_photo_button).setOnClickListener(this);
|
|
inputPanelView.findViewById(R.id.lt_choospic_button).setOnClickListener(this);
|
|
inputPanelView.findViewById(R.id.lt_video_button).setOnClickListener(this);
|
|
}
|
|
|
|
@Override
|
|
public void onClick(View v) {
|
|
int id = v.getId();
|
|
//点击加号拓展按钮
|
|
if (id == R.id.input_panel_add_btn) {
|
|
showPlugin();
|
|
//点击发送按钮
|
|
} else if (id == R.id.input_panel_send_btn) {
|
|
sendMessage();
|
|
} else if (id == R.id.edit_btn) {
|
|
if (pluginList.getVisibility() == VISIBLE) {
|
|
pluginList.setVisibility(GONE);
|
|
editBtn.setFocusable(true);
|
|
}
|
|
} else if (id == R.id.lt_photo_button) {
|
|
if (messageCallback != null) {
|
|
messageCallback.choosePic(1);
|
|
}
|
|
} else if (id == R.id.lt_choospic_button) {
|
|
if (messageCallback != null) {
|
|
messageCallback.choosePic(2);
|
|
}
|
|
} else if (id == R.id.lt_video_button) {
|
|
if (messageCallback != null) {
|
|
messageCallback.choosePic(3);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 展示拓展插件列表
|
|
*/
|
|
private void showPlugin() {
|
|
if (pluginList.getVisibility() == VISIBLE) {
|
|
pluginList.setVisibility(GONE);
|
|
} else {
|
|
pluginList.setVisibility(VISIBLE);
|
|
//失去焦点
|
|
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
imm.showSoftInput(editBtn, InputMethodManager.SHOW_FORCED);
|
|
imm.hideSoftInputFromWindow(editBtn.getWindowToken(), 0); //强制隐藏键盘
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 发送文字消息
|
|
*/
|
|
private void sendMessage() {
|
|
if (!isSend) return;
|
|
if (!TextUtils.isEmpty(editBtn.getText()) && !TextUtils.isEmpty(editBtn.getText().toString().trim())) {
|
|
String text = editBtn.getText().toString();
|
|
editBtn.setText("");
|
|
Conversation.ConversationType conversationType = Conversation.ConversationType.PRIVATE;
|
|
TextMessage messageContent = TextMessage.obtain(text);
|
|
Message message = Message.obtain(targetId, conversationType, messageContent);
|
|
IMCenter.getInstance().sendMessage(message, null, null, new IRongCallback.ISendMessageCallback() {
|
|
|
|
@Override
|
|
public void onAttached(Message message) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onSuccess(Message message) {
|
|
if(isAdmin){
|
|
AdjustEvent adjustEvent1 = new AdjustEvent("2kjbwx");
|
|
Adjust.trackEvent(adjustEvent1);
|
|
CommonHttpUtil.setAdvertisingChannels("2kjbwx", new HttpCallback() {
|
|
@Override
|
|
public void onSuccess(int code, String msg, String[] info) {
|
|
if (code == 0) {
|
|
FirebaseAnalytics.getInstance(getContext()).logEvent("FS_director_first_chat", null);
|
|
AppEventsLogger.newLogger(getContext()).logEvent("FB_director_first_chat");
|
|
}
|
|
}
|
|
});
|
|
AdjustEvent adjustEvent2 = new AdjustEvent("g9lzss");
|
|
Adjust.trackEvent(adjustEvent2);
|
|
FirebaseAnalytics.getInstance(getContext()).logEvent("FS_director_chat", null);
|
|
AppEventsLogger.newLogger(getContext()).logEvent("FB_director_chat");
|
|
}
|
|
|
|
IMCenter.getInstance().clearTextMessageDraft(Conversation.ConversationType.PRIVATE, targetId, null);
|
|
}
|
|
|
|
@Override
|
|
public void onError(Message message, RongIMClient.ErrorCode errorCode) {
|
|
ToastUtil.show(errorCode.msg);
|
|
}
|
|
});
|
|
} else {
|
|
ToastUtil.show("不可以发送空消息");
|
|
}
|
|
|
|
//发送以后删除相应标志
|
|
NoviceInstructorManager.get(getContext()).removeNoviceInstructor(targetId);
|
|
}
|
|
|
|
private TextWatcher mEditTextWatcher = new TextWatcher() {
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
}
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
//存入草稿
|
|
Conversation.ConversationType types = Conversation.ConversationType.PRIVATE;
|
|
if (s != null && s.length() != 0) {
|
|
isSend = true;
|
|
inputPanelSendBtn.setBackground(getResources().getDrawable(R.mipmap.btn_sand1));
|
|
} else {
|
|
isSend = false;
|
|
inputPanelSendBtn.setBackground(getResources().getDrawable(R.mipmap.btn_sand));
|
|
}
|
|
int offset;
|
|
if (count == 0) {
|
|
int var10000 = start + before;
|
|
offset = -before;
|
|
} else {
|
|
offset = count;
|
|
}
|
|
if (offset != 0) {
|
|
RongIMClient.getInstance().sendTypingStatus(types, targetId, "RC:TxtMsg");
|
|
}
|
|
}
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 发送媒体消息 隱藏
|
|
*
|
|
* @param imagePath
|
|
*/
|
|
public void sendImageFile(String imagePath) {
|
|
if (pluginList.getVisibility() == VISIBLE) {
|
|
pluginList.setVisibility(GONE);
|
|
}
|
|
Uri localUri = Uri.parse("file://" + imagePath);
|
|
ImageMessage imageMessage = ImageMessage.obtain(localUri, localUri);
|
|
Conversation.ConversationType conversationType = Conversation.ConversationType.PRIVATE;
|
|
Message message = Message.obtain(targetId, conversationType, imageMessage);
|
|
|
|
IMCenter.getInstance().sendMediaMessage(message, null, null, new IRongCallback.ISendMediaMessageCallback() {
|
|
@Override
|
|
public void onProgress(Message message, int i) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onCanceled(Message message) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onAttached(Message message) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onSuccess(Message message) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onError(final Message message, final RongIMClient.ErrorCode errorCode) {
|
|
|
|
}
|
|
});
|
|
//发送以后删除相应标志
|
|
NoviceInstructorManager.get(getContext()).removeNoviceInstructor(targetId);
|
|
}
|
|
|
|
public void sendVideoFile(String filePath) {
|
|
if (pluginList.getVisibility() == VISIBLE) {
|
|
pluginList.setVisibility(GONE);
|
|
}
|
|
long duration = FileUtil.getDuration(filePath);
|
|
double fileSize = FileSizeUtil.getFileOrFilesSize(filePath, FileSizeUtil.SIZETYPE_MB);
|
|
//将视频获取的毫秒时长转成分总
|
|
long minute = duration / 60000;
|
|
//大于两分钟走发送文件,否则走发送小视频
|
|
if (minute > 2) {
|
|
sendFile(filePath);
|
|
} else {
|
|
sendSightExtension(filePath, duration);
|
|
}
|
|
Log.e("InputPanelViewHolder", "时长" + duration + "文件大小");
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* 发送小视频消息
|
|
*
|
|
* @param filePath 文件路径
|
|
* @param duration 视频时长
|
|
*/
|
|
private void sendSightExtension(String filePath, long duration) {
|
|
Conversation.ConversationType conversationType = Conversation.ConversationType.PRIVATE;
|
|
Uri localUri = Uri.parse("file://" + filePath);
|
|
SightMessage sightMessage = SightMessage.obtain(localUri, (int) TimeUtils.durationToSecond(duration));
|
|
Message message = Message.obtain(targetId, conversationType, sightMessage);
|
|
IMCenter.getInstance().sendMediaMessage(message, null, null, new IRongCallback.ISendMediaMessageCallback() {
|
|
@Override
|
|
public void onProgress(Message message, int i) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onCanceled(Message message) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onAttached(Message message) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onSuccess(Message message) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onError(final Message message, final RongIMClient.ErrorCode errorCode) {
|
|
ToastUtil.show(errorCode.msg);
|
|
}
|
|
});//
|
|
//发送以后删除相应标志
|
|
NoviceInstructorManager.get(getContext()).removeNoviceInstructor(targetId);
|
|
}
|
|
|
|
/**
|
|
* 发送文件消息
|
|
*
|
|
* @param filePath 文件路径
|
|
*/
|
|
private void sendFile(String filePath) {
|
|
Message message;
|
|
|
|
Uri localUri = Uri.parse("file://" + filePath);
|
|
Conversation.ConversationType conversationType = Conversation.ConversationType.PRIVATE;
|
|
|
|
|
|
FileMessage videoMessage = FileMessage.obtain(getContext(), localUri);
|
|
|
|
message = Message.obtain(targetId, conversationType, videoMessage);
|
|
|
|
IMCenter.getInstance().sendMediaMessage(message, null, null, new IRongCallback.ISendMediaMessageCallback() {
|
|
@Override
|
|
public void onProgress(Message message, int i) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onCanceled(Message message) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onAttached(Message message) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onSuccess(Message message) {
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onError(final Message message, final RongIMClient.ErrorCode errorCode) {
|
|
ToastUtil.show(errorCode.msg);
|
|
}
|
|
});
|
|
//发送以后删除相应标志
|
|
NoviceInstructorManager.get(getContext()).removeNoviceInstructor(targetId);
|
|
}
|
|
|
|
/**
|
|
* 多媒体信息
|
|
*/
|
|
public interface MediaMessageCallback {
|
|
void choosePic(int intoIndex);
|
|
}
|
|
|
|
private MediaMessageCallback messageCallback;
|
|
|
|
public void addMediaMessageCallback(MediaMessageCallback callback) {
|
|
messageCallback = callback;
|
|
}
|
|
|
|
/**
|
|
* 隱藏插件列表
|
|
*/
|
|
public void hidePluginList() {
|
|
pluginList.setVisibility(GONE);
|
|
}
|
|
|
|
}
|