ExpressionManager.java 30.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
package com.wd.comment.manager;

import android.animation.Animator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.ColorRes;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;
import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieDrawable;
import com.wd.base.log.Logger;
import com.wd.comment.R;
import com.wd.comment.adapter.ExpressionGridViewAdapter;
import com.wd.comment.adapter.ExpressionPagerAdapter;
import com.wd.comment.adapter.ImageAdapter;
import com.wd.comment.bean.CommentEventBean;
import com.wd.comment.bean.EmojiEntity;
import com.wd.comment.comment.listener.AliTokenListener;
import com.wd.comment.comment.model.GetAliTokenFetcher;
import com.wd.comment.listener.SpeechResultCallback;
import com.wd.comment.utils.KeyBoardObserver;
import com.wd.comment.utils.KeyboardHelper;
import com.wd.common.permissions.IPmsCallBack;
import com.wd.common.permissions.PermissionsUtils;
import com.wd.common.widget.InnerGridView;
import com.wd.foundation.bean.response.SpeechTokenBean;
import com.wd.foundation.wdkit.utils.SpUtils;
import com.wd.foundation.wdkit.utils.ToastNightUtil;
import com.wd.foundation.wdkitcore.thread.ThreadPoolUtils;
import com.wd.foundation.wdkitcore.tools.ResUtils;
import com.wd.foundation.wdkitcore.tools.StringUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LiuKun
 * @date 2023/5/2  17:24
 * @Description:表情键盘切换
 */
public class ExpressionManager implements KeyBoardObserver {

    private final Activity activity;
    private final EditText editText;
    private final ImageView expressionBtn;
    private final ImageView expressionGifBtn;
    private final ImageView voiceBtn;
    /**
     * 普通表情布局
     */
    private final View expressionLayout;
    /**
     * 定制表情布局
     */
    private final View expressionGifLayout;
    /**
     * 最近使用
     */
    private final View recentGifLayout;

    private final View voiceLayout;
    private View expressionLayerView;
    private LinearLayout pageTurningPoint;
    private ViewPager viewPager;

    /**
     * 定制表情宫格和
     * 最近使用表情宫格
     */
    private GridView gifGridView,recentGridView;

    /**
     * 最近表情适配器
     */
    ImageAdapter recentAdapter;
    private List<View> emojiLists;
    private int[] emojiCodes;
    /**
     * 每页emoji数量
     */
    private final int emojiPageNum = 20;
    private int[] page_indicatorId = new int[]{R.drawable.emoji_point_normal_bg, R.drawable.emoji_point_press_bg};
    private ArrayList<ImageView> mPointViews = new ArrayList<>();
    /**
     * 是否限制字数
     */
    private boolean isLimit = false;
    private int mMaxInputLength = 1000;
    /**
     * 当文字超出最大值时是否改变提示文字颜色
     */
    private boolean isChangeInputCountColor = false;
    private int[] changeColors;
    /**
     * 0:show keyboard 1:show voice 2:show expression 3 显示定制表情
     */
    private int showButton;
    private FrameLayout flVoiceInput;
    private TextView voiceBottomTip;
    /**
     * 阿里录音
     */
    private boolean mInit = false;
    SpeechCompService speechProvider;
    /**
     * 是否正在录音
     */
    private boolean isRecording = false;

    /**
     * 语音输入动画
     */
    private LottieAnimationView lavVoiceInput;

    private LottieAnimationView lavVoiceInputsteptwo;
    private boolean isstart;

    /**
     * 全部头像
     */
    private List<String> gifListStr;

    /**
     * 最近使用头像
     */
    private List<String> gifRecentListStr;
    /**
     * 构造方法
     */
    public ExpressionManager(Activity activity, View voiceLayout, View expressionLayout, View expressionGifLayout,View recentGifLayout,
                             EditText editText, ImageView voiceBtn, ImageView expressionBtn, ImageView expressionIvGif,
                             List<String> gifListStr,List<String> gifRecentListStr) {
        this.activity = activity;
        this.voiceLayout = voiceLayout;
        this.expressionLayout = expressionLayout;
        this.expressionGifLayout = expressionGifLayout;
        this.recentGifLayout = recentGifLayout;
        this.editText = editText;
        this.voiceBtn = voiceBtn;
        this.expressionBtn = expressionBtn;
        this.expressionGifBtn = expressionIvGif;
        this.gifListStr = gifListStr;
        this.gifRecentListStr = gifRecentListStr;
        EventBus.getDefault().register(this);
        init();
        getAliToken();
    }

    /**
     * 获取阿里token
     */
    private void getAliToken() {
        GetAliTokenFetcher tokenFetcher = new GetAliTokenFetcher();
        tokenFetcher.getAliToken(new AliTokenListener() {
            @Override
            public void onGetTokenSuccess(SpeechTokenBean tokenBean) {
                if (tokenBean == null) {
                    return;
                }
                String accessToken = tokenBean.getAccessToken();
                speechProvider = new SpeechCompService();
                mInit = speechProvider.initSpeechSdk(accessToken, (FragmentActivity) activity);
                setAliYun();
            }

            @Override
            public void onGetTokenFailed(String error) {

            }
        });
    }

    /**
     * 获取阿里云
     */
    private void setAliYun() {
        if (!mInit) {
            ToastNightUtil.showShort("语音识别SDK初始化失败");
            return;
        }
        if (null == speechProvider) {
            ToastNightUtil.showShort("语音识别SDK初始化失败");
            return;
        }
        speechProvider.setResultCallback(new SpeechResultCallback() {
            @Override
            public void start() {
                ThreadPoolUtils.postToMain(() -> {
                    isRecording = true;
                    voiceBottomTip.setText(com.wd.speech.R.string.dialog_comment_stop_tips);
                    startVoiceInputAnimation();
                });
            }

            @Override
            public void stop() {
                ThreadPoolUtils.postToMain(() -> {
                    voicePressUpLift();
                    if(StringUtils.isEmpty(editText.getText().toString())){
                        voiceBottomTip.setText(ResUtils.getString(com.wd.speech.R.string.str_no_voice_recognized_tips));
                    }else {
                        voiceBottomTip.setText(ResUtils.getString(com.wd.speech.R.string.dialog_comment_long_an));
                    }
                    Logger.t("ExpressionManager").d("识别暂停");
                });
            }

            @Override
            public void error() {
                ThreadPoolUtils.postToMain(() -> {
                    voicePressUpLift();
                    voiceBottomTip.setText(ResUtils.getString(com.wd.speech.R.string.str_no_voice_recognized_tips));
                });
            }

            @Override
            public void result(String result) {
                ThreadPoolUtils.postToMain(() -> {
                    appendString(result);
                    voicePressUpLift();
                    voiceBottomTip.setText(ResUtils.getString(com.wd.speech.R.string.dialog_comment_long_an));
                });
            }
        });
    }

    /**
     * 初始化
     */
    private void init() {
        initView();
        addListener();
        showEmojiView();
        initPageTurningPoint();
        showGifView();
    }

    /**
     * 初始化布局
     */
    private void initView() {
        lavVoiceInput = voiceLayout.findViewById(R.id.lavVoiceInput);
        lavVoiceInputsteptwo =  voiceLayout.findViewById(R.id.lavVoiceInputsteptwo);
        voiceBottomTip = voiceLayout.findViewById(R.id.dv_tv_bottom_tip);
        flVoiceInput = voiceLayout.findViewById(R.id.flVoiceInput);

        pageTurningPoint = expressionLayout.findViewById(R.id.page_turning_point);
        viewPager = expressionLayout.findViewById(R.id.view_pager);
        expressionLayerView = expressionLayout.findViewById(R.id.v_emoji_layer);

        //gif图宫格 定制表情
        gifGridView = expressionGifLayout.findViewById(R.id.gridview_gif);

        //gif图宫格 最近使用定制表情
        recentGridView = recentGifLayout.findViewById(R.id.gridview_gif_recent);

        boolean isNight = SpUtils.isNightMode();
        if (isNight) {
            expressionLayerView.setVisibility(View.VISIBLE);
        } else {
            expressionLayerView.setVisibility(View.GONE);
        }

        if(SpUtils.isNightMode()){
            //夜间模式
            lavVoiceInput.setAnimation("voice_input_night.json");
            lavVoiceInputsteptwo.setAnimation("voice_inputsteptwo_night.json");
        }else {
            lavVoiceInput.setAnimation("voice_input.json");
            lavVoiceInputsteptwo.setAnimation("voice_inputsteptwo.json");
        }
        lavVoiceInput.setRepeatCount(0);
        lavVoiceInputsteptwo.setRepeatCount(LottieDrawable.INFINITE);
        lavVoiceInput.addAnimatorListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if(isstart){
                    isstart = false;
                    lavVoiceInput.pauseAnimation();
                    lavVoiceInputsteptwo.setVisibility(View.VISIBLE);
                    lavVoiceInputsteptwo.playAnimation();
                }

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });



    }

    /**
     * 开始动画
     */
    private void startVoiceInputAnimation() {
        isstart = true;
        lavVoiceInputsteptwo.setVisibility(View.GONE);
        lavVoiceInputsteptwo.cancelAnimation();
        lavVoiceInputsteptwo.setProgress(0f);
        lavVoiceInput.playAnimation();
    }

    /**
     * 清除动画
     */
    private void stopVoiceInputAnimation() {
        isstart = false;
        lavVoiceInput.setVisibility(View.VISIBLE);
        lavVoiceInputsteptwo.setVisibility(View.GONE);
        lavVoiceInput.cancelAnimation();
        lavVoiceInput.setProgress(0f);
    }

    /**
     * 停止动画
     */
    public void voicePressUpLift() {
        isRecording = false;
        stopVoiceInputAnimation();
    }

    /**
     * 追加文字
     */
    private void appendString(String text) {
        int start = editText.getSelectionStart();
        int end = editText.getSelectionEnd();
        if (start == end) {
            editText.getEditableText().insert(start, text);
        } else {
            editText.getEditableText().replace(start, end, text);
        }
    }

    /**
     * 语音录制监听
     */
    @SuppressLint("ClickableViewAccessibility")
    private void addListener() {
        /*flVoiceInput.setOnClickListener(view -> {
            if (isRecording) {
                speechProvider.stopSpeech();
            } else {
                speechProvider.beginSpeech();
            }
        });*/

        flVoiceInput.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //长按输入
                if (!isRecording) {
                    speechProvider.beginSpeech();
                }
                return true;
            }
        });

        flVoiceInput.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP){
                    //松开停止
                    if (isRecording) {
                        speechProvider.stopSpeech();
                    }
                }
                return false;
            }
        });

        //输入框
        editText.addTextChangedListener(mEditInputListener);
        editText.setOnTouchListener((v, event) -> {
            voicePressUpLift();
            showButton = 0;
            hideVoice();
            hideExpression();
            hideExpressionGif();
            switchIcon();
            return false;
        });
        //语音录入
        voiceBtn.setOnClickListener(v -> {
            voicePressUpLift();
            checkPermission();
        });
        //普通表情图标点击
        expressionBtn.setOnClickListener(v -> {
            voicePressUpLift();
            //隐藏定制表情
            hideExpressionGif();
            clickExpression();
            clickNewExpression();
        });
        //定制表情图标点击
        expressionGifBtn.setOnClickListener(v -> {
            voicePressUpLift();
            //隐藏普通表情
            hideExpression();
            clickExpressionGif();
            clickNewExpression();
        });
        //表情滑动布局
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                for (int i = 0; i < mPointViews.size(); i++) {
                    mPointViews.get(i).setImageResource(page_indicatorId[1]);
                    if (position != i) {
                        mPointViews.get(i).setImageResource(page_indicatorId[0]);
                    }
                }
            }
        });
    }

    private void clickNewExpression() {
    }



    /**
     * 获取权限,保存图片
     */
    private void checkPermission() {
        PermissionsUtils.getRecordAudio(activity, new IPmsCallBack() {
            @Override
            public void granted() {
                clickVoice();
            }

            @Override
            public void notGranted() {
//                ToastNightUtil.showShort("没有权限或拒绝权限");
            }
        });
    }

    /**
     * 点击语音输入
     */
    private void clickVoice() {
        hideExpression();
        hideExpressionGif();
        if (isVoiceVisible()) {
            showButton = 0;
            switchIcon();
            hideVoice();
            KeyboardHelper.showSoftInput(editText);
        } else {
            showButton = 1;
            KeyboardHelper.hideSoftInput(editText);
            switchIcon();
            showVoice();
        }
    }

    /**
     * 点击普通表情按钮
     */
    private void clickExpression() {
        hideVoice();
        if (isExpressionVisible()) {
            showButton = 0;
            switchIcon();
            hideExpression();
            KeyboardHelper.showSoftInput(editText);
        } else {
            showButton = 2;
            KeyboardHelper.hideSoftInput(editText);
            switchIcon();
            showExpression();
        }
    }

    /**
     * 点击定制表情按钮
     */
    private void clickExpressionGif() {
        hideVoice();
        if (isExpressionGifVisible()) {
            showButton = 0;
            switchIcon();
            hideExpressionGif();
            KeyboardHelper.showSoftInput(editText);
        } else {
            showButton = 3;
            KeyboardHelper.hideSoftInput(editText);
            switchIcon();
            showExpressionGif();
        }
    }


    /**
     * 数据透传
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void articleShare(CommentEventBean.AudioPermissionApply event) {
        if (event.result) {
            clickVoice();
        }
    }

    /**
     * 更新软键盘弹出
     */
    @Override
    public void update(boolean keyBoardVisibility, int keyBoardHeight) {
        if (keyBoardVisibility) {
            hideExpression();
            hideExpressionGif();
            hideVoice();
            showButton = 0;
            switchIcon();
        } else {
            switchIcon();
            if (showButton == 1) {
                showVoice();
            } else if (showButton == 2) {
                showExpression();
            }
        }
    }

    private boolean isCutText = false;
    private boolean isBeyond = false;

    /**
     * 输入框监听
     */
    private final TextWatcher mEditInputListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!isLimit) {
                return;
            }

            // 判断一下,否则 超出最大值会截取,截取后重新赋值还会走这个方法
            if (isCutText) {
                isCutText = false;
                return;
            }


            String text = editText.getText().toString().trim();
            int length = 0;
            boolean isEmoji;
            for (int i = 0; i < text.length(); ) {
                isEmoji = false;
                if (!isEmojiCharacter(text.charAt(i))) {
                    i++;
                    length += 11;
                    isEmoji = true;
                }
                i++;
                length++;
                if (length > mMaxInputLength) {
                    ToastNightUtil.showShort("已达到输入上限");

                    isCutText = true;

                    int cutOutLength = i - 1;
                    if (isEmoji) {
                        cutOutLength = i - 2;
                    }
                    int endSelect = editText.getSelectionEnd();
                    String newStr = text.substring(0, cutOutLength);
                    editText.setText(newStr);
                    if (endSelect > cutOutLength) {
                        endSelect = cutOutLength;
                    }
                    editText.setSelection(endSelect);
                    break;
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (inputTextListener != null) {
                inputTextListener.changed();
            }
        }
    };

    /**
     * 切换按钮
     */
    public void switchIcon() {
        if (showButton == 1) {
            voiceBtn.setImageResource(R.drawable.ic_comment_select_keyboard);
            expressionBtn.setImageResource(R.drawable.ic_comment_expression);
            expressionGifBtn.setImageResource(R.drawable.ic_comment_expressiongif);
        } else if (showButton == 2) {
            voiceBtn.setImageResource(R.drawable.ic_comment_sound_recording);
            expressionBtn.setImageResource(R.drawable.ic_comment_select_keyboard);
            expressionGifBtn.setImageResource(R.drawable.ic_comment_expressiongif);
        }else if(showButton == 3) {
            voiceBtn.setImageResource(R.drawable.ic_comment_sound_recording);
            expressionBtn.setImageResource(R.drawable.ic_comment_expression);
            expressionGifBtn.setImageResource(R.drawable.ic_comment_select_keyboard);
        } else {
            voiceBtn.setImageResource(R.drawable.ic_comment_sound_recording);
            expressionBtn.setImageResource(R.drawable.ic_comment_expression);
            expressionGifBtn.setImageResource(R.drawable.ic_comment_expressiongif);
        }
    }

    /**
     * 显示普通表情
     */
    private void showEmojiView() {
        emojiCodes = activity.getResources().getIntArray(R.array.emoji_code);
        if (emojiLists == null) {
            emojiLists = new ArrayList<>();
            int length = 0;
            int quotient = emojiCodes.length / emojiPageNum;
            int remainder = emojiCodes.length % emojiPageNum;
            if (remainder > 0) {
                length = quotient + 1;
            }
            for (int i = 0; i < length; i++) {
                emojiLists.add(getEmojiItemViews(i));
            }
            viewPager.setAdapter(new ExpressionPagerAdapter(emojiLists));
        }
    }

    /**
     * 显示gif定制表情
     */
    private void showGifView(){
        // 图片资源ID数组,根据实际情况填充数据
//        String[] imageUrls = {"https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/1.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/2.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/3.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/4.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/5.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/6.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/7.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/8.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/9.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/10.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/11.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/12.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/13.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/14.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/15.gif",
//                "https://cdnjdphoto.aikan.pdnews.cn/social/rmrb-concat/commentpic/16.gif"};
        // 创建自定义Adapter实例并传入数据和上下文信息
        ImageAdapter adapter = new ImageAdapter(activity, gifListStr);
        // 将Adapter设置到GridView上,完成数据绑定和展示。
        gifGridView.setAdapter(adapter);
        gifGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 处理点击事件, position 参数表示被点击的项目在适配器中的位置
                if (gifInputListener != null && gifListStr != null && !gifListStr.isEmpty()) {
                    gifInputListener.changed(gifListStr.get(position));
                }

            }
        });


        // 创建自定义Adapter实例并传入数据和上下文信息
        recentAdapter = new ImageAdapter(activity, gifRecentListStr);
        // 将Adapter设置到GridView上,完成数据绑定和展示。
        recentGridView.setAdapter(recentAdapter);
        recentGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 处理点击事件, position 参数表示被点击的项目在适配器中的位置
                if (recentInputListener != null && gifRecentListStr != null && !gifRecentListStr.isEmpty()) {
                    recentInputListener.changed(gifRecentListStr.get(position));
                }

            }
        });


    }

    /**
     * 表情滑动切换
     */
    private void initPageTurningPoint() {
        pageTurningPoint.removeAllViews();
        for (int count = 0; count < viewPager.getAdapter().getCount(); count++) {
            // 翻页指示的点
            ImageView pointView = new ImageView(activity);
            pointView.setPadding(5, 0, 5, 0);
            if (mPointViews.isEmpty()) {
                pointView.setImageResource(page_indicatorId[1]);
            } else {
                pointView.setImageResource(page_indicatorId[0]);
            }
            mPointViews.add(pointView);
            pageTurningPoint.addView(pointView);
        }

    }

    /**
     * 获取表情
     */
    private View getEmojiItemViews(int page) {
        List<EmojiEntity> list = new ArrayList<>();
        int start = emojiPageNum * page;
        int length = (page + 1) * emojiPageNum;
        for (int i = start; i < length; i++) {
            if (i < emojiCodes.length) {
                EmojiEntity entity = new EmojiEntity(getEmojiStringByUnicode(emojiCodes[i]));
                list.add(entity);
            } else {
                list.add(new EmojiEntity(""));
            }
        }
        list.add(new EmojiEntity(""));

        View view = View.inflate(activity, R.layout.layout_expression_gridview, null);
        InnerGridView gridView = view.findViewById(R.id.gridview);
        final ExpressionGridViewAdapter gridViewAdapter = new ExpressionGridViewAdapter(activity, list);
        gridView.setAdapter(gridViewAdapter);
        gridView.setOnItemClickListener((parent, view1, position, id) -> {
            int start1 = editText.getSelectionStart();
            int end = editText.getSelectionEnd();
            Editable editable = editText.getText();
            if (position == emojiPageNum) {
                if (editable.length() <= 0) {
                    return;
                }
                if (start1 == end) {
                    char lastStr = editable.charAt(editable.length() - 1);
                    int interval = 1;
                    if (!isEmojiCharacter(lastStr)) {
                        interval = 2;
                    }
                    editable.delete(start1 - interval, start1);
                } else {
                    editable.replace(start1, end, "");
                }
            } else {
                if (start1 == end) {
                    editable.insert(start1, gridViewAdapter.getItem(position).code);
                } else {
                    editable.replace(start1, end, gridViewAdapter.getItem(position).code);
                }
            }
        });

        return view;
    }

    private String getEmojiStringByUnicode(int unicodeJoy) {
        return new String(Character.toChars(unicodeJoy));
    }

    private boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) ||
                (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
    }

    /**
     * 一个表情变为12个字符
     */
    private int getEditTextLength12() {
        String text = editText.getText().toString().trim();
        int count = 0;
        for (int i = 0; i < text.length(); i++) {
            if (!isEmojiCharacter(text.charAt(i))) {
                i++;
                count += 11;
            }
            count++;
        }
        return count;
    }

    /**
     * 一个表情改为一个字符
     */
    private int getEditTextLength() {
        String text = editText.getText().toString().trim();
        int count = 0;
        for (int i = 0; i < text.length(); i++) {
            if (!isEmojiCharacter(text.charAt(i))) {
                i++;
            }
            count++;
        }
        return count;
    }

    private boolean isVoiceVisible() {
        return voiceLayout.getVisibility() == View.VISIBLE;
    }

    private void showVoice() {
        if (voiceLayout.getVisibility() == View.GONE) {
            voiceLayout.setVisibility(View.VISIBLE);
        }
    }

    private void hideVoice() {
        if (voiceLayout.getVisibility() == View.VISIBLE) {
            voiceLayout.setVisibility(View.GONE);
        }
    }

    private boolean isExpressionVisible() {
        return expressionLayout.getVisibility() == View.VISIBLE;
    }

    private boolean isExpressionGifVisible() {
        return expressionGifLayout.getVisibility() == View.VISIBLE;
    }

    private void showExpression() {
        if (expressionLayout.getVisibility() == View.GONE) {
            expressionLayout.setVisibility(View.VISIBLE);
        }
    }

    private void showExpressionGif() {
        if (expressionGifLayout.getVisibility() == View.GONE) {
            expressionGifLayout.setVisibility(View.VISIBLE);
            //最新表情显示同步更新
            if(gifRecentListStr !=null && !gifRecentListStr.isEmpty() && recentAdapter != null) {
                recentAdapter.setDataList(gifRecentListStr);
                recentGifLayout.setVisibility(View.VISIBLE);
            }
        }
    }

    private void hideExpression() {
        if (expressionLayout.getVisibility() == View.VISIBLE) {
            expressionLayout.setVisibility(View.GONE);
        }
    }

    private void hideExpressionGif() {
        if (expressionGifLayout.getVisibility() == View.VISIBLE) {
            expressionGifLayout.setVisibility(View.GONE);
            recentGifLayout.setVisibility(View.GONE);
        }
    }

    public void setMaxCountLength(int maxCountLength) {
        this.mMaxInputLength = maxCountLength;
        isLimit = true;
    }

    public void setChangeInputCountColor(@ColorRes int defaultColor, @ColorRes int changeColor) {
        isChangeInputCountColor = true;
        changeColors = new int[]{defaultColor, changeColor};
    }


    /**
     * 结束消息
     */
    public void recycler() {

        EventBus.getDefault().unregister(this);
        if(speechProvider != null){
            speechProvider.release();
        }
    }

    private InputTextListener inputTextListener;

    public void setInputTextListener(InputTextListener inputTextListener) {
        this.inputTextListener = inputTextListener;
    }

    /**
     * 监听输入变化
     */
    public interface InputTextListener {
        void changed();
    }

    private gifInputListener gifInputListener,recentInputListener;

    public void setGifInputTextListener(gifInputListener inputListener) {
        this.gifInputListener = inputListener;
    }

    public void setRecentGifInputTextListener(gifInputListener inputListener) {
        this.recentInputListener = inputListener;
    }

    /**
     * 监听输入变化
     */
    public interface gifInputListener {
        void changed(String url);
    }


    /**
     * 弹出消失
     */
    public void resetStatus() {
        showButton = 0;
        hideVoice();
        hideExpression();
        hideExpressionGif();
        switchIcon();
        voicePressUpLift();
    }

}