package com.secspace.lib.common.utils;

import android.content.Context;

import com.secspace.mdmengine.api.manager.MdmFactoryManager;

import java.util.ArrayList;
import java.util.List;

import static com.secspace.lib.common.env.Packages.PkgHuaWei.PACKAGENAME_HW_LAUNCHER;
import static com.secspace.lib.common.utils.Package.ComponentNames.huaweiLuancher;

public class MdmUtils {
    /**
     * 设置默认桌面，设置默认桌面相当于开机自启
     * @param context
     */
    public static void setDefaultLauncher(Context context){
        DisableCompat.disableComponent(context,huaweiLuancher);
        DpmHelper.getInstance(context).initMiddleWare();
        MdmFactoryManager.getInstance().getDeviceSettingsManager().setDefaultLauncher(context.getPackageName(), "com.android.launcher3.settings.BlankAcitvity");
        MdmFactoryManager.getInstance().getDeviceRestrictionManager().setStatusBarExpandPanelDisabled(true);
//        MdmFactoryManager.getInstance().getDeviceRestrictionManager().setTaskButtonDisabled(true);
        //搜索wifi列表时需要将gps打开  否则无法找到列表
//        MdmFactoryManager.getInstance().getDeviceRestrictionManager().turnOnGPS(true);
        Prefs.setAutoStartSelf(context,true);
        //设置默认桌面需要将华为桌面禁用，在用过华为桌面后否则会在最近任务栏显示华为桌面
        if(!Prefs.isReinforce(context)){
            return;
        }
        List<String> packages = PkgManager.getInstance(context).queryLauncherPackages();
        packages.remove(context.getPackageName());
        if (packages.size() != 0) {
            Prefs.setHideLauncherPackages(context, packages);
        }
        for (String aPackage : packages) {
            DisableCompat.disableApplication(context, aPackage);
        }
    }

    /**
     * 清除默认桌面和一系列的状态
     * @param context
     */
    public static void clearDefaultLauncher(Context context){
        DpmHelper.getInstance(context).initMiddleWare();
        MdmFactoryManager.getInstance().getDeviceSettingsManager().clearDefaultLauncher(context.getPackageName());
        Prefs.setAutoStartSelf(context,false);
        DisableCompat.enableComponent(context,huaweiLuancher);
        List<String> launchers = Prefs.getHideLauncherPackages(context);
        if (launchers == null) {
            launchers = new ArrayList<>();
        }
        if (!launchers.contains(PACKAGENAME_HW_LAUNCHER)) {
            launchers.add(PACKAGENAME_HW_LAUNCHER);
        }
        for (String launcher : launchers) {
            DisableCompat.enableApplication(context, launcher);
        }

    }
}
