298 lines
10 KiB
Java
298 lines
10 KiB
Java
package com.yunbao.live.dialog;
|
||
|
||
import android.app.Dialog;
|
||
import android.os.Bundle;
|
||
import android.util.Log;
|
||
import android.view.Gravity;
|
||
import android.view.View;
|
||
import android.view.Window;
|
||
import android.view.WindowManager;
|
||
import android.widget.ImageView;
|
||
import android.widget.TextView;
|
||
|
||
import androidx.annotation.Nullable;
|
||
|
||
import com.yunbao.common.bean.HttpCallbackModel;
|
||
import com.yunbao.common.dialog.AbsDialogFragment;
|
||
import com.yunbao.common.glide.ImgLoader;
|
||
import com.yunbao.common.http.HttpCallback;
|
||
import com.yunbao.common.http.live.LiveNetManager;
|
||
import com.yunbao.common.utils.DialogUitl;
|
||
import com.yunbao.common.utils.MicStatusManager;
|
||
import com.yunbao.common.utils.ToastUtil;
|
||
import com.yunbao.common.utils.WordUtil;
|
||
import com.yunbao.live.R;
|
||
import com.yunbao.common.manager.RandomPkManager;
|
||
import com.yunbao.live.activity.LiveRyAnchorActivity;
|
||
import com.yunbao.live.http.LiveHttpUtil;
|
||
|
||
import java.util.Arrays;
|
||
|
||
/**
|
||
* 随机PK
|
||
*/
|
||
public class RandomPkDialogFragment extends AbsDialogFragment implements View.OnClickListener {
|
||
private static final String TAG = "随机PK";
|
||
private View mPkBtn;
|
||
private TextView mPkBtnTitle;
|
||
private TextView mPkBtnDesc;
|
||
private ImageView mRandomPkSwitch;
|
||
|
||
|
||
private final RandomPkManager.OnRandomPkTimer randomPkTimer = new RandomPkManager.OnRandomPkTimer() {
|
||
@Override
|
||
public void onTimer(String time) {
|
||
if (!mPkBtn.isEnabled()) {
|
||
return;
|
||
}
|
||
Log.i(TAG, "onTimer: " + time);
|
||
mPkBtnTitle.setText(String.format(WordUtil.getString(R.string.random_pk_info_btn_ing), time));
|
||
mPkBtnDesc.setText(R.string.random_pk_info_btn_end_desc);
|
||
mPkBtnDesc.setVisibility(View.VISIBLE);
|
||
mPkBtn.setBackgroundResource(R.drawable.bg_live_room_random_pk_timer);
|
||
}
|
||
|
||
@Override
|
||
public void onPking() {
|
||
resetPkBtn();
|
||
dismiss();
|
||
}
|
||
|
||
@Override
|
||
public void onPkEnd() {
|
||
resetPkBtn();
|
||
}
|
||
|
||
@Override
|
||
public void onPkEndStart() {
|
||
super.onPkEndStart();
|
||
mPkBtn.setEnabled(false);
|
||
}
|
||
|
||
@Override
|
||
public void onPkEndTimer(String time) {
|
||
super.onPkEndTimer(time);
|
||
mPkBtnTitle.setText(String.format(WordUtil.getString(R.string.random_pk_info_btn_end), time));
|
||
mPkBtnDesc.setText(R.string.random_pk_info_btn_end_desc);
|
||
mPkBtn.setEnabled(false);
|
||
}
|
||
|
||
@Override
|
||
public void onPkEndSuccess() {
|
||
super.onPkEndSuccess();
|
||
resetPkBtn();
|
||
mPkBtn.setTag(false);
|
||
mPkBtn.setEnabled(true);
|
||
}
|
||
};
|
||
|
||
@Override
|
||
protected int getLayoutId() {
|
||
return R.layout.dialog_live_random_pk_function;
|
||
}
|
||
|
||
@Override
|
||
protected int getDialogStyle() {
|
||
return R.style.dialog2;
|
||
}
|
||
|
||
@Override
|
||
protected boolean canCancel() {
|
||
return true;
|
||
}
|
||
|
||
@Override
|
||
protected void setWindowAttributes(Window window) {
|
||
WindowManager.LayoutParams params = window.getAttributes();
|
||
window.setWindowAnimations(R.style.bottomToTopAnim);
|
||
params.width = WindowManager.LayoutParams.MATCH_PARENT;
|
||
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
|
||
params.gravity = Gravity.BOTTOM;
|
||
window.setAttributes(params);
|
||
}
|
||
|
||
|
||
@Override
|
||
public void onDestroy() {
|
||
super.onDestroy();
|
||
RandomPkManager.getInstance().unregisterOnRandomPkTimer(randomPkTimer);
|
||
}
|
||
|
||
@Override
|
||
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
||
super.onActivityCreated(savedInstanceState);
|
||
initView();
|
||
initData();
|
||
RandomPkManager.debugUid = null;
|
||
}
|
||
|
||
private void initView() {
|
||
mPkBtn = findViewById(R.id.random_pk_info_btn);
|
||
mPkBtnTitle = (TextView) findViewById(R.id.random_pk_btn_title);
|
||
mPkBtnDesc = (TextView) findViewById(R.id.random_pk_btn_desc);
|
||
mRandomPkSwitch = (ImageView) findViewById(R.id.live_random_pk_switch);
|
||
mRandomPkSwitch.setTag(true);
|
||
mRandomPkSwitch.setOnClickListener(this);
|
||
mPkBtn.setOnClickListener(this);
|
||
mPkBtn.setTag(false);
|
||
/* mPkBtn.setOnLongClickListener(v -> {
|
||
DialogUitl.showSimpleInputDialog(mContext, "输入指定UID", new DialogUitl.SimpleCallback() {
|
||
@Override
|
||
public void onConfirmClick(Dialog dialog, String content) {
|
||
RandomPkManager.debugUid = content;
|
||
ToastUtil.show("随机PK指定id = " + content);
|
||
dialog.dismiss();
|
||
}
|
||
});
|
||
return false;
|
||
});*/
|
||
}
|
||
|
||
private void initData() {
|
||
RandomPkManager.getInstance().addOnRandomPkTimer(randomPkTimer);
|
||
initPkSwitch();
|
||
if (RandomPkManager.getInstance().isRequestPk()) {
|
||
mPkBtnTitle.setText(String.format(WordUtil.getString(R.string.random_pk_info_btn_ing), RandomPkManager.getInstance().getTimer()));
|
||
mPkBtnDesc.setText(R.string.random_pk_info_btn_end_desc);
|
||
mPkBtnDesc.setVisibility(View.VISIBLE);
|
||
mPkBtn.setBackgroundResource(R.drawable.bg_live_room_random_pk_timer);
|
||
} else if (RandomPkManager.getInstance().isExiting()) {
|
||
mPkBtnTitle.setText(String.format(WordUtil.getString(R.string.random_pk_info_btn_end), RandomPkManager.getInstance().getExitTimer()));
|
||
mPkBtnDesc.setText(R.string.random_pk_info_btn_end_desc);
|
||
mPkBtn.setEnabled(false);
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* 设置随机PK开关状态
|
||
*
|
||
* @param open on/off
|
||
*/
|
||
private void setSwitchBtn(boolean open) {
|
||
if (open) {
|
||
ImgLoader.display(mContext, com.yunbao.common.R.mipmap.special_icon_on, mRandomPkSwitch);
|
||
mRandomPkSwitch.setTag(true);
|
||
} else {
|
||
ImgLoader.display(mContext, com.yunbao.common.R.mipmap.special_icon_off, mRandomPkSwitch);
|
||
mRandomPkSwitch.setTag(false);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取随机PK状态
|
||
*/
|
||
private void initPkSwitch() {
|
||
LiveNetManager.get(mContext)
|
||
.getRandomPkSwitch(new com.yunbao.common.http.base.HttpCallback<Integer>() {
|
||
@Override
|
||
public void onSuccess(Integer data) {
|
||
if (data != null) {
|
||
setSwitchBtn(data == 1);
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onError(String error) {
|
||
|
||
}
|
||
});
|
||
}
|
||
|
||
private Dialog loadDialog;
|
||
|
||
/**
|
||
* 设置随机PK状态
|
||
*
|
||
* @param pk 1=on,0=off
|
||
*/
|
||
private void setPkSwitch(int pk) {
|
||
if (RandomPkManager.getInstance().isRequestPk() || RandomPkManager.getInstance().isExiting()) {
|
||
return;
|
||
}
|
||
loadDialog = DialogUitl.loadingDialog(mContext);
|
||
loadDialog.show();
|
||
LiveNetManager.get(mContext)
|
||
.changeRandomPkSwitch(pk, new com.yunbao.common.http.base.HttpCallback<HttpCallbackModel>() {
|
||
@Override
|
||
public void onSuccess(HttpCallbackModel data) {
|
||
loadDialog.dismiss();
|
||
loadDialog = null;
|
||
if (data.getCode() == 0) {
|
||
setSwitchBtn(!(boolean) mRandomPkSwitch.getTag());
|
||
} else if (data.getCode() == 500) {
|
||
DialogUitl.showSimpleDialog(mContext, "您因拒绝随机PK,被限制关闭中", "随机PK可打开时间:" + data.getMsg(), false, new DialogUitl.SimpleCallback() {
|
||
@Override
|
||
public void onConfirmClick(Dialog dialog, String content) {
|
||
dialog.dismiss();
|
||
}
|
||
});
|
||
} else {
|
||
ToastUtil.show(data.getMsg());
|
||
}
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onError(String error) {
|
||
loadDialog.dismiss();
|
||
loadDialog = null;
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 开始匹配
|
||
*/
|
||
private void randomPk() {
|
||
if (mRandomPkSwitch.getTag() == null || !(boolean) mRandomPkSwitch.getTag()) {
|
||
ToastUtil.show("未开启随机PK开关");
|
||
return;
|
||
}
|
||
if ((mPkBtn.getTag() != null && (boolean) mPkBtn.getTag()) || RandomPkManager.getInstance().isRequestPk()) {
|
||
RandomPkManager.getInstance().exitPk();
|
||
return;
|
||
}
|
||
LiveHttpUtil.getMicList(LiveRyAnchorActivity.mLiveUid, 0, new HttpCallback() {
|
||
@Override
|
||
public void onSuccess(int code, String msg, String[] info) {
|
||
System.out.println("code = " + code + ", msg = " + msg + ", info = " + Arrays.deepToString(info));
|
||
//非0认为未开通连麦权限
|
||
if (code != 0) {
|
||
if (RandomPkManager.getInstance().start()) {
|
||
mPkBtnDesc.setVisibility(View.VISIBLE);
|
||
mPkBtnDesc.setText(R.string.random_pk_info_btn_ing_desc);
|
||
mPkBtn.setTag(true);
|
||
}
|
||
} else {
|
||
ToastUtil.show("請先關閉語言連麥");
|
||
}
|
||
}
|
||
});
|
||
|
||
}
|
||
|
||
/**
|
||
* 恢复按钮状态
|
||
*/
|
||
private void resetPkBtn() {
|
||
mPkBtnDesc.setVisibility(View.GONE);
|
||
mPkBtnTitle.setText(R.string.random_pk_info_btn_start);
|
||
mPkBtn.setBackgroundResource(R.drawable.bg_live_random_pk_info_btn);
|
||
}
|
||
|
||
@Override
|
||
public void onClick(View v) {
|
||
int id = v.getId();
|
||
if (id == R.id.live_random_pk_switch) {
|
||
if (mRandomPkSwitch.getTag() != null && (boolean) mRandomPkSwitch.getTag()) {
|
||
setPkSwitch(0);
|
||
} else {
|
||
setPkSwitch(1);
|
||
}
|
||
} else if (id == R.id.random_pk_info_btn) {
|
||
randomPk();
|
||
}
|
||
}
|
||
}
|