VoiceSearchDialog.java 5.62 KB
package com.people.speech.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.people.speech.R;
import com.people.speech.SpeechCompServiceImpl;
import com.people.speech.manager.ExpressionManager;
import com.people.speech.utils.KeyboardHelper;
import com.people.speech.view.BackEditText;
import com.wd.common.dialog.LayerDialog;

/**
 * 语音识别弹窗
 *
 * @author : ouyang
 * @since: 2022/7/20
 */
public class VoiceSearchDialog extends LayerDialog {

    private final OnDialogClickListener mOnDialogClickListener;

    private final Activity activity;

    private SpeechCompServiceImpl speechCompService;

    // 表情管理
    private ExpressionManager expressionManager;

    /**
     * 输入框
     */
    private BackEditText inputEt;

    /**
     * 语音按钮
     */
    private ImageView voiceIv;

    /**
     * 发送按钮
     */
    private TextView publishBtn;

    private final int maxCountLength;

    /**
     * 使用类型,0:默认 1:语音搜索
     */
    private int useType = 0;

    public VoiceSearchDialog(Context context, SpeechCompServiceImpl speechCompService) {
        super(context, R.style.NoAnimBottom);
        this.activity = (Activity) context;
        this.speechCompService = speechCompService;
        this.mOnDialogClickListener = speechCompService;
        this.maxCountLength = 900;
    }

    public VoiceSearchDialog(Context context, SpeechCompServiceImpl speechCompService,int useType) {
        super(context, R.style.NoAnimBottom);
        this.activity = (Activity) context;
        this.speechCompService = speechCompService;
        this.mOnDialogClickListener = speechCompService;
        this.maxCountLength = 900;
        this.useType = useType;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_voice_search_layout);
        initDialog();
//        setCanceledOnTouchOutside(false);

        initView();
        initEvent();
    }

    private void initView() {
        inputEt = findViewById(R.id.cd_et_input);
        // 搜索按钮
        publishBtn = findViewById(R.id.cd_tv_publish_btn);
        // 搜索按钮点击事件
        publishBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                speechCompService.searchClick(getText());
                dismiss();
            }
        });
        //语音
        voiceIv = findViewById(R.id.cd_iv_voice_btn);
        // 语音录入布局
        View voiceLayout = findViewById(R.id.layout_voice);

        // 表情管理
        expressionManager = new ExpressionManager(this,activity, speechCompService, voiceLayout,
                inputEt, voiceIv,useType);
        if (maxCountLength > 0) {
            expressionManager.setMaxCountLength(maxCountLength);
        }
        // 搜索按钮状态改变监听
        expressionManager.setInputTextListener(this::publishStatus);
    }

    /**
     * 初始化弹出框
     */
    private void initDialog() {
        this.setCanceledOnTouchOutside(true);
        WindowManager manager = this.getWindow().getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.width = width;
        lp.gravity = Gravity.BOTTOM;
        this.getWindow().setAttributes(lp);
    }

    private void initEvent() {

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (mOnDialogClickListener != null) {
                    mOnDialogClickListener.onClickCancel();
                }
            }
        });
    }

    /**
     * 发送按钮状态
     */
    private void publishStatus() {
        String text = getText();
        if (!TextUtils.isEmpty(text)) {
            publishBtn.setBackgroundResource(R.drawable.shape_comment_input_publish);
            publishBtn.setTextColor(ContextCompat.getColor(publishBtn.getContext(),
                    R.color.res_color_common_C8_keep));
        } else {
            publishBtn.setBackgroundResource(R.drawable.shape_comment_input_publish_default);
            publishBtn.setTextColor(ContextCompat.getColor(publishBtn.getContext(),
                    R.color.res_color_general_FFFFFF_50_keep));
        }
    }

    public String getText() {
        return inputEt.getText().toString();
    }

    /**
     * hint为说两句
     */
    public void showDefaultHint() {
        setHint(activity.getResources().getString(R.string.res_two_sentences));
        showWithSoftBoard();
    }

    /**
     * 设置提示语
     */
    public void setHint(String hint) {
        if (inputEt != null) {
            inputEt.setText("");
            inputEt.setHint(hint);
            inputEt.clearFocus();
            KeyboardHelper.hideSoftInput(inputEt);
        }
    }

    /**
     * 带有软键盘
     */
    public void showWithSoftBoard() {
        show();
    }

    public interface OnDialogClickListener {

        /**
         * 取消
         */
        void onClickCancel();

        /**
         * 重试
         */
        void onClickRetry();
    }

}