GrayManager.java 4.17 KB
package com.wd.common.utils;

import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.View;


import com.wd.foundation.bean.response.MourningModelBean;

import java.util.List;

/**
 * 国殇模式下的工具
 *
 * @version 1.0.0
 * @description:
 * @author: liyubing
 * @date :2023/3/28 9:41
 */
public class GrayManager {

    private static GrayManager mInstance;
    // 添加灰色蒙层
    private Paint mGrayPaint;
    private ColorMatrix mGrayMatrix;

    // 清理灰色蒙层
    private Paint cGrayPaint;

    // 国殇总开关
    public boolean switchOpen;

    // 所有频道都开启国殇
    public boolean selectAll = false;

    // tab 新闻中国殇 频道id
    public List<String> newsChannelList;
    // tab 视频中国殇 频道id
    public List<String> videoChannelList;
    // tab 人民号 中国殇 频道id
    public List<String> rmhList;
    // tab 服务中国殇 频道id
    public List<String> serverList;

    public static GrayManager getInstance() {
        if (mInstance == null) {
            synchronized (GrayManager.class) {
                if (mInstance == null) {
                    mInstance = new GrayManager();
                }
            }
        }

        return mInstance;
    }

    //灰色蒙层初始化
    public void init() {
        mGrayMatrix = new ColorMatrix();
        mGrayPaint = new Paint();
        mGrayMatrix.setSaturation(0);
        mGrayPaint.setColorFilter(new ColorMatrixColorFilter(mGrayMatrix));

    }

    //硬件加速置灰方
    public void setLayerGrayType(View view) {
        if (mGrayMatrix == null && mGrayPaint == null) {
            init();
        }
        view.setLayerType(View.LAYER_TYPE_HARDWARE, mGrayPaint);
    }


    /**
     * 清理蒙层
     *
     * @param view
     */
    public void clearLayerGray(View view) {
        if(cGrayPaint == null){
            ColorMatrix cGrayMatrix = new ColorMatrix();
            cGrayPaint = new Paint();
            cGrayPaint.setColorFilter(new ColorMatrixColorFilter(cGrayMatrix));
        }

        view.setLayerType(View.LAYER_TYPE_HARDWARE, cGrayPaint);
    }


    /**
     * 记录底部导航栏需要处理频道
     *
     * @param modelBean
     */
    public void saveChannelList(MourningModelBean modelBean) {

        // 新闻 频道
        List<String> navNewslList = modelBean.getNewsList();
        if (navNewslList != null && navNewslList.size() > 0) {
            newsChannelList = navNewslList;
        } else {
            newsChannelList = null;
        }

        // 视频 频道
        List<String> navVideoList = modelBean.getVideoList();
        if (navVideoList != null && navVideoList.size() > 0) {
            videoChannelList = navVideoList;
        } else {
            videoChannelList = null;
        }

        // 人民号 频道
        List<String> navBrannList = modelBean.getRmhList();
        if (navBrannList != null && navBrannList.size() > 0) {
            rmhList = navBrannList;
        } else {
            rmhList = null;
        }

        // 服务 频道
        List<String> navSectionList = modelBean.getServerList();
        if (navSectionList != null && navSectionList.size() > 0) {
            serverList = navSectionList;
        } else {
            serverList = null;
        }
    }

    /**
     * 清理数据
     */
    public void clearData() {
        newsChannelList = null;
        videoChannelList = null;
        rmhList = null;
        serverList = null;
    }


    /**
     * 检测当前页面所在频道是否需要开启国殇模式
     *
     * @param channelId
     * @param channelList
     * @return
     */
    public boolean checkChannelHaveGrayView(String channelId, List<String> channelList) {

        if(selectAll){
            return true;
        }

        boolean open = false;

        if (switchOpen && channelList != null && channelList.size() > 0 && !TextUtils.isEmpty(channelId)) {

            for (String a : channelList) {

                if (channelId.equals(a)) {
                    open = true;
                    break;
                }
            }
        }
        return open;

    }

}