
package com.secspace.lib.common.env;

import android.content.Intent;

import com.secspace.lib.common.base.BaseApplication;

import java.util.ArrayList;
import java.util.HashSet;

import static com.secspace.lib.common.env.Packages.PkgHuaWei.PACKAGENAME_HUAWEI_MOBILE_SERVICE;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_BROWSER;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_CAMERA;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_CONTACTS;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_CONTACTS_PROVIDER;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_DIALER;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_EXPLORER;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_GALLERY;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_MDMENHANCE;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_MDM_ONLINE;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_MEDIA_PROVIDER;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_MIDDLEWARE;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_MMS_PROVIDER;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_MUSIC;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_OUT;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_SECCONFIG;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_SETTINGS;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_SMS;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_SMS_AGENT;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_STORE;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_SYSTEM_AGENT;
import static com.secspace.lib.common.env.Packages.PkgSecSpace.PACKAGENAME_VIDEO;
import static com.secspace.lib.common.env.Packages.PkgThird.PACKAGENAME_DISPATCH;
import static com.secspace.lib.common.env.Packages.PkgThird.PACKAGENAME_EMM;
import static com.secspace.lib.common.env.Packages.PkgThird.PACKAGENAME_XDJA_SAFECLIENT;
import static com.secspace.lib.common.env.Packages.PkgXDJA.PACKAGENAME_SECSPACE_AT;
import static com.secspace.lib.common.env.Packages.PkgXDJA.PACKAGENAME_SECSPACE_XPGJ;

public class BlackandWhiteList {
    private final static ArrayList<String> LIST_SELF = new ArrayList<>();
    private final static ArrayList<String> LIST_MIDDLEWARE = new ArrayList<>();

    static {
        LIST_SELF.add(BaseApplication.getBase().getPackageName());
        LIST_MIDDLEWARE.add(PACKAGENAME_MIDDLEWARE);
    }

    /**
     * 获取安全域本身的集合
     *
     * @return
     */

    public static ArrayList<String> getListSelf() {
        return LIST_SELF;
    }

    /**
     * 获取中间件本身的集合
     *
     * @return
     */

    public static ArrayList<String> getListMiddleware() {
        return LIST_MIDDLEWARE;
    }

    //-------------普通系统黑白名单
    public static class NormalSystem {
        private static final ArrayList<String> TRUST_APPS = new ArrayList<>();

        static {
            TRUST_APPS.add(PACKAGENAME_MDMENHANCE);
            TRUST_APPS.add(PACKAGENAME_SYSTEM_AGENT);
            TRUST_APPS.add(PACKAGENAME_SMS_AGENT);
        }

        public static ArrayList<String> getTrustApps() {
            return TRUST_APPS;
        }
    }


    //安全域内的黑白名单
    public static class SecSystem {

        /**
         * 系统插件，禁止卸载
         */
        private final static ArrayList<String> SYSTEM_APPS = new ArrayList<>();
        /**
         * 不显示水印的插件
         */
        private final static ArrayList<String> NO_WATERMARK_APPS = new ArrayList<>();
        /**
         * 免杀插件白名单
         */
        private final static ArrayList<String> WHITE_KILLED_APPS = new ArrayList<>();

        /**
         * 卸载插件白名单
         */
        private final static ArrayList<String> WHITE_UNINSTALL_APPS = new ArrayList<>();


        /**
         * 更新安全域时需要卸载掉的插件，比如更换浏览器
         */
        private final static ArrayList<String> MUST_UNINSTALLED_APPS_WHEN_UPDATE = new ArrayList<>();

        /**
         * 管控插件安装时，签名白名单
         */
        private final static ArrayList<String> WHITE_SIGNATURES = new ArrayList<>();

        /**
         * 重定向加密TF卡的插件名单
         */
        private final static ArrayList<String> REDIRECT_TF_APPS = new ArrayList<>();

        /**
         * 进程未启动时也能接收广播的包名白名单
         */
        private static final HashSet<String> WHITE_APP_BROADCAST_START_LIST = new HashSet<>();
        /**
         * 广播中心分为服务进程和插件进程，服务进程的广播中心用于进程未启动时的静态广播分发，比如开机广播，
         * 由于安卓P对一个进程广播数的限制，所以服务进程不能再对所有插件的静态广播做统一管理，现改成只对白名单广播进行管理分发
         */
        private static final HashSet<String> WHITE_BROADCAST_START_LIST = new HashSet<>();


        static {
            SYSTEM_APPS.add(PACKAGENAME_OUT);
            SYSTEM_APPS.add(PACKAGENAME_SETTINGS);
            SYSTEM_APPS.add(PACKAGENAME_EXPLORER);
            SYSTEM_APPS.add(PACKAGENAME_DIALER);
//            SYSTEM_APPS.add(PACKAGENAME_STORE);
//            SYSTEM_APPS.add(PACKAGENAME_BROWSER);
            SYSTEM_APPS.add(PACKAGENAME_CONTACTS);
//            SYSTEM_APPS.add(PACKAGENAME_GALLERY);
//            SYSTEM_APPS.add(PACKAGENAME_VIDEO);
//            SYSTEM_APPS.add(PACKAGENAME_MUSIC);
//            SYSTEM_APPS.add(PACKAGENAME_MDM_ONLINE);
            SYSTEM_APPS.add(PACKAGENAME_CAMERA);
            SYSTEM_APPS.add(PACKAGENAME_SECCONFIG);
            SYSTEM_APPS.add(PACKAGENAME_SMS);
            SYSTEM_APPS.add(PACKAGENAME_MMS_PROVIDER);
            SYSTEM_APPS.add(PACKAGENAME_HUAWEI_MOBILE_SERVICE);
            //Provider
            SYSTEM_APPS.add(PACKAGENAME_CONTACTS_PROVIDER);
            SYSTEM_APPS.add(PACKAGENAME_MEDIA_PROVIDER);
        }

        static {
//            NO_WATERMARK_APPS.add(PACKAGENAME_CAMERA);
//            NO_WATERMARK_APPS.add(PACKAGENAME_SETTINGS);
        }

        static {
            WHITE_KILLED_APPS.add(PACKAGENAME_SECCONFIG);
            WHITE_KILLED_APPS.add(PACKAGENAME_HUAWEI_MOBILE_SERVICE);
        }

        static {
            WHITE_UNINSTALL_APPS.add(PACKAGENAME_DISPATCH);
//            WHITE_UNINSTALL_APPS.add(PACKAGENAME_EMM);
            WHITE_UNINSTALL_APPS.add(PACKAGENAME_XDJA_SAFECLIENT);
            WHITE_UNINSTALL_APPS.add(PACKAGENAME_GALLERY);
            WHITE_UNINSTALL_APPS.add(PACKAGENAME_VIDEO);
            WHITE_UNINSTALL_APPS.add(PACKAGENAME_MUSIC);
            WHITE_UNINSTALL_APPS.add(PACKAGENAME_BROWSER);
        }

        static {
            MUST_UNINSTALLED_APPS_WHEN_UPDATE.add("com.secspace.browser");
            MUST_UNINSTALLED_APPS_WHEN_UPDATE.add("org.chromium.chrome.browser");
            MUST_UNINSTALLED_APPS_WHEN_UPDATE.add("com.mxtech.videoplayer.pro");
        }

        static {
            //REDIRECT_TF_APPS.add(PACKAGENAME_TF1);
//            REDIRECT_TF_APPS.add("");
            REDIRECT_TF_APPS.add(PACKAGENAME_SECSPACE_AT);
            REDIRECT_TF_APPS.add(PACKAGENAME_SECSPACE_XPGJ);
        }

        static {
            WHITE_APP_BROADCAST_START_LIST.add(PACKAGENAME_MDM_ONLINE);
            WHITE_APP_BROADCAST_START_LIST.add(PACKAGENAME_DIALER);
        }

        static {
            WHITE_BROADCAST_START_LIST.add(Intent.ACTION_BOOT_COMPLETED);
        }


        public static ArrayList<String> getSystemApps() {
            return SYSTEM_APPS;
        }

        public static ArrayList<String> getNoWaterMarkApps() {
            return NO_WATERMARK_APPS;
        }

        public static ArrayList<String> getWhiteKilledApps() {
            return WHITE_KILLED_APPS;
        }

        public static ArrayList<String> getWhiteUninstallApps() {
            return WHITE_UNINSTALL_APPS;
        }

        public static ArrayList<String> getMustUninstalledAppsWhenUpdate() {
            return MUST_UNINSTALLED_APPS_WHEN_UPDATE;
        }

        public static ArrayList<String> getWhiteSignatures() {
            return WHITE_SIGNATURES;
        }

        public static ArrayList<String> getRedirectTFApps() {
            return REDIRECT_TF_APPS;
        }

        public static void addWhiteAppStaticBroadCastStartList(String packageName) {
            WHITE_APP_BROADCAST_START_LIST.add(packageName);
        }

        public static boolean isWhiteAppForStaticBroadCastStart(String packageName) {
            return WHITE_APP_BROADCAST_START_LIST.contains(packageName);
        }

        public static boolean isWhiteStaticBroadcastStart(String receiver) {
            return WHITE_BROADCAST_START_LIST.contains(receiver);
        }
    }
}