package com.android.launcher3.auto_get_permission.xiaomi;

import android.accessibilityservice.AccessibilityService;
import android.text.TextUtils;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.android.launcher3.accessibility.PermissionOpenAccessiblityService;
import com.android.launcher3.auto_get_permission.BaseAutoGetPermission;
import com.android.launcher3.util.Logger;
import com.android.launcher3.util.PermissionUtil;
import com.android.launcher3.util.SystemUtils;

import java.util.List;

import static android.accessibilityservice.AccessibilityService.GLOBAL_ACTION_BACK;
import static com.android.launcher3.auto_get_permission.AccessibilityNodeOperateUtil.*;

public class XiaoMiAutoPermissionGetter extends BaseAutoGetPermission {
    public static final String TAG = "XiaoMiAutoPermissionGetter";


    public XiaoMiAutoPermissionGetter(PermissionOpenAccessiblityService service) {
        super(service);
    }

    @Override
    public void requestkDeviceAdmin(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode = event.getSource();
        if (SystemUtils.getMIUIVersion().equals("V10")) {
            if ("com.android.settings.MiuiSettings".equals(event.getClassName().toString())) {

                List<AccessibilityNodeInfo> listViewNodeS = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                if (listViewNodeS != null && listViewNodeS.size() > 0) {

                    AccessibilityNodeInfo listViewNode = listViewNodeS.get(0);

                    AccessibilityNodeInfo targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "更多设置");
                    long time = System.currentTimeMillis();
                    while (targetNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {

                        if (listViewNode.isScrollable()) {
                            listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                            sleepShort();
                            listViewNode.refresh();
                        } else {
                            break;
                        }

                        targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "更多设置");
                    }

                    if (targetNode != null && performAction(targetNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                        sleepShort();
                    } else {
                        showToastLong("请点击【更多设置】");
                        return;
                    }
                }
                return;


            } else if ("com.android.settings.SubSettings".equals(event.getClassName())) {

                String title = event.getText().toString();

                if (TextUtils.equals(title, "[更多设置]")) {
                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "系统安全");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                                listViewNode.refresh();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "系统安全");
                        }


                        if (systemSafeBtn != null && performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【系统安全】");
                            return;
                        }
                    }
                    return;

                }

                if (TextUtils.equals(title, "[安全性和位置信息]")) {

                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "特殊应用权限");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                                listViewNode.refresh();
                            } else {
                                break;
                            }
                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "特殊应用权限");
                        }

                        if (systemSafeBtn != null && performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【系统安全】");
                            return;
                        }
                    }
                    return;
                }

                if (TextUtils.equals(title, "[特殊应用权限]")) {
                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "设备管理应用");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                                listViewNode.refresh();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "设备管理应用");
                        }


                        if (systemSafeBtn != null && performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【系统安全】");
                        }
                    }
                    return;
                }

                if (TextUtils.equals(title, "[设备管理应用]")) {

                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "com.android.settings:id/name", appName);
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                                listViewNode.refresh();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "com.android.settings:id/name", appName);
                        }

                        if (systemSafeBtn != null && performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【系统安全】");
                            return;
                        }
                    }
                    return;
                }

            } else if ("com.android.settings.DeviceAdminAdd".equals(event.getClassName().toString())) {

                List<AccessibilityNodeInfo> nodeInfoList = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/restricted_action");

                if (nodeInfoList != null && nodeInfoList.size() > 0 && nodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK)) {
                    sleep();
//                                resetStep();
                    return;
                } else {
                    showToastLong("请点击启用/激活此设备管理员");
                    return;
                }


            }

            Logger.e(TAG, "gotoDeviceAdminPermissionSetting\t" + event.getClassName().toString());

            if ("com.miui.permcenter.install.DeviceManagerApplyActivity".equals(event.getClassName())) {

                boolean finish = clickXiaomiSafeWarning(sourceNode);

                if (finish) {
                    resetStep();
                }
                return;

            }


            if (event.getPackageName() != null && event.getPackageName().equals("com.android.settings")) {

                getService().performGlobalAction(GLOBAL_ACTION_BACK);
                sleepShort();
//                            gotoDeviceAdminPermissionSetting();
                return;
            }

            if (event.getPackageName() == null) {
                gotoDeviceAdminPermissionSetting();
                sleep();

            }
        } else {
            if ("com.android.settings.MiuiSettings".equals(event.getClassName().toString())) {

                List<AccessibilityNodeInfo> listViewNodeS = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                if (listViewNodeS != null && listViewNodeS.size() > 0) {

                    AccessibilityNodeInfo listViewNode = listViewNodeS.get(0);

                    AccessibilityNodeInfo targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "密码、隐私与安全");
                    long time = System.currentTimeMillis();
                    while (targetNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {

                        if (listViewNode.isScrollable()) {
                            listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                            sleepShort();
                            listViewNode.refresh();
                        } else {
                            break;
                        }

                        targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "密码、隐私与安全");
                    }

                    if (targetNode != null && performAction(targetNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                        sleepShort();
                    } else {
                        showToastLong("请点击【密码、隐私与安全】");
                        return;
                    }
                }
                return;


            } else if ("com.android.settings.SubSettings".equals(event.getClassName())) {

                String title = event.getText().toString();

                if (TextUtils.equals(title, "[密码、隐私与安全]")) {
                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "系统安全");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                                listViewNode.refresh();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "系统安全");
                        }


                        if (systemSafeBtn != null && performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【系统安全】");
                            return;
                        }
                    }
                    return;

                }

                if (TextUtils.equals(title, "[系统安全]")) {

                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "特殊应用权限");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                                listViewNode.refresh();
                            } else {
                                break;
                            }
                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "特殊应用权限");
                        }

                        if (systemSafeBtn != null && performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【系统安全】");
                            return;
                        }
                    }
                    return;
                }

                if (TextUtils.equals(title, "[特殊应用权限]")) {
                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "设备管理应用");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                                listViewNode.refresh();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "设备管理应用");
                        }


                        if (systemSafeBtn != null && performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【系统安全】");
                        }
                    }
                    return;
                }

                if (TextUtils.equals(title, "[设备管理应用]")) {

                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "com.android.settings:id/name", appName);
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                                listViewNode.refresh();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "com.android.settings:id/name", appName);
                        }

                        if (systemSafeBtn != null && performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【系统安全】");
                            return;
                        }
                    }
                    return;
                }

            } else if ("com.android.settings.DeviceAdminAdd".equals(event.getClassName().toString())) {

                List<AccessibilityNodeInfo> nodeInfoList = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/restricted_action");

                if (nodeInfoList != null && nodeInfoList.size() > 0 && nodeInfoList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK)) {
                    sleep();
//                                resetStep();
                    return;
                } else {
                    showToastLong("请点击启用/激活此设备管理员");
                    return;
                }


            }

            Logger.e(TAG, "gotoDeviceAdminPermissionSetting\t" + event.getClassName().toString());

            if ("com.miui.permcenter.install.DeviceManagerApplyActivity".equals(event.getClassName())) {

                boolean finish = clickXiaomiSafeWarning(sourceNode);

                if (finish) {
                    resetStep();
                }
                return;

            }


            if (event.getPackageName() != null && event.getPackageName().equals("com.android.settings")) {

                getService().performGlobalAction(GLOBAL_ACTION_BACK);
                sleepShort();
//                            gotoDeviceAdminPermissionSetting();
                return;
            }

            if (event.getPackageName() == null) {
                gotoDeviceAdminPermissionSetting();
                sleep();

            }
        }
    }

    @Override
    public void reqeustAutoStartUp(AccessibilityEvent event) {

    }

    @Override
    public void requestBatteryOptimize(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode = event.getSource();
        if (sourceNode == null) {
            return;
        }
        if ("com.android.settings.MiuiSettings".equals(event.getClassName().toString())) {

            List<AccessibilityNodeInfo> listViewNodeS = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

            if (listViewNodeS != null && listViewNodeS.size() > 0) {

                AccessibilityNodeInfo listViewNode = listViewNodeS.get(0);

                AccessibilityNodeInfo targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "应用设置");
                if (targetNode == null) {
                    targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "应用管理");
                }
                long time = System.currentTimeMillis();
                while (targetNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {

                    if (listViewNode.isScrollable()) {
                        listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        listViewNode.refresh();
                    } else {
                        break;
                    }

                    targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "应用设置");
                    if (targetNode == null) {
                        targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "应用管理");
                    }
                }
                if (targetNode != null && performAction(targetNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                    sleepShort();
                } else {
                    showToastLong("请点击【应用设置】");
                    return;
                }
            }
            return;


        } else if ("com.android.settings.SubSettings".equals(event.getClassName())) {

            String title = event.getText().toString();

            if (TextUtils.equals(title, "[应用设置]")) {
                List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                if (listViewNodes != null && listViewNodes.size() > 0) {
                    AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                    AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "应用管理");
                    long time = System.currentTimeMillis();
                    while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                        if (listViewNode != null) {
                            listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                            sleepShort();
                            listViewNode.refresh();
                        } else {
                            break;
                        }

                        systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "应用管理");
                    }


                    if (systemSafeBtn != null && performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                        sleepShort();
                    } else {
                        showToastLong("请点击【应用管理】");
                        return;
                    }
                }
                return;
            }
        }

        if ("com.miui.appmanager.AppManagerMainActivity".equals(event.getClassName().toString())) {

            List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("com.miui.securitycenter:id/app_manager_list_view");

            if (listViewNodes != null && listViewNodes.size() > 0) {
                AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "com.miui.securitycenter:id/am_label", appName);
                long time = System.currentTimeMillis();
                while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                    if (listViewNode != null) {
                        listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        listViewNode.refresh();
                    } else {
                        break;
                    }

                    systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "com.miui.securitycenter:id/am_label", appName);
                }

                if (systemSafeBtn != null && performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {

                    sleepShort();
                } else {
                    showToastLong("请找到【" + appName + "】并点击");
                    return;
                }
            }
            return;
        }

        Logger.e(TAG, "gotoDeviceAdminPermissionSetting\t" + event.getClassName().toString());


        if ("com.miui.appmanager.ApplicationsDetailsActivity".equals(event.getClassName().toString()) && event.getText() != null && event.getText().toString().contains(appName)) {


            List<AccessibilityNodeInfo> scrollViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("com.miui.securitycenter:id/am_scrollview");

            if (scrollViewNodes != null && scrollViewNodes.size() > 0) {
                AccessibilityNodeInfo scrollViewNode = scrollViewNodes.get(0);
                List<AccessibilityNodeInfo> switcherBtnNode = scrollViewNode.findAccessibilityNodeInfosByViewId("com.miui.securitycenter:id/am_switch");
//                    AccessibilityNodeInfo systemSafeBtn = getChildNodeEqual(sourceNode, "自启动");
                long time = System.currentTimeMillis();
                while ((switcherBtnNode == null || switcherBtnNode.size() <= 0) && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                    if (scrollViewNode != null) {
                        scrollViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        scrollViewNode.refresh();
                    } else {
                        break;
                    }

                    switcherBtnNode = scrollViewNode.findAccessibilityNodeInfosByViewId("com.miui.securitycenter:id/am_switch");
                }


                if (switcherBtnNode != null && performAction(switcherBtnNode.get(0), AccessibilityNodeInfo.ACTION_CLICK)) {
                    if (!switcherBtnNode.get(0).isChecked()) {
                        switcherBtnNode.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        sleepShort();

                    }
                } else {
                    showToastLong("请点击【自启动】");
                    return;
                }

            }

        }

        if ("miui.app.AlertDialog".equals(event.getClassName())) {
            AccessibilityNodeInfo acceptBtnNode = getChildNodeEqual(sourceNode, "确定");

            if (acceptBtnNode != null && performAction(acceptBtnNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                sleepShort();
                getService().performGlobalAction(GLOBAL_ACTION_BACK);
            } else {
                showToastLong("请点击【确定】");
                return;
            }
        }

        if ("com.miui.appmanager.ApplicationsDetailsActivity".equals(event.getClassName().toString()) && event.getText() != null && event.getText().toString().contains(appName)) {


            List<AccessibilityNodeInfo> scrollViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("com.miui.securitycenter:id/am_scrollview");

            if (scrollViewNodes != null && scrollViewNodes.size() > 0) {
                AccessibilityNodeInfo scrollViewNode = scrollViewNodes.get(0);


                AccessibilityNodeInfo batterySaveBtn = getChildNodeEqual(sourceNode, "省电策略");
                long time = System.currentTimeMillis();
                while (batterySaveBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                    if (scrollViewNode != null) {
                        scrollViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        scrollViewNode.refresh();
                    } else {
                        break;
                    }

                    batterySaveBtn = getChildNodeEqual(sourceNode, "省电策略");
                }

                if (batterySaveBtn != null && performAction(batterySaveBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                    sleepShort();
                } else {
                    showToastLong("请点击【省电策略】");
                    return;
                }
            }
            return;
        }

        if ("com.miui.powerkeeper.ui.HiddenAppsConfigActivity".equals(event.getClassName()) && event.getText() != null && event.getText().toString().contains(appName)) {
//                省电策略
            AccessibilityNodeInfo accessibilityNodeInfo = getChildNodeEqual(sourceNode, "无限制");

            if (accessibilityNodeInfo != null && performAction(accessibilityNodeInfo, AccessibilityNodeInfo.ACTION_CLICK)) {

                sleepShort();
                resetStep();
            } else {
                showToastLong("请点击【无限制】");
                return;
            }

        }

        if (event.getPackageName() != null && event.getPackageName().equals("com.android.settings")) {

            getService().performGlobalAction(GLOBAL_ACTION_BACK);
            sleepShort();
//                            gotoDeviceAdminPermissionSetting();
            return;
        }


        //gotoBatteryOptimize();
        sleep();
    }

    @Override
    public void requestUsageAppPermission(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode = event.getSource();
        if (sourceNode == null) {
            return;
        }
        if (SystemUtils.getMIUIVersion().equals("V10")) {
            if ("com.android.settings.MiuiSettings".equals(event.getClassName().toString())) {

                List<AccessibilityNodeInfo> listViewNodeS = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                if (listViewNodeS != null && listViewNodeS.size() > 0) {

                    AccessibilityNodeInfo listViewNode = listViewNodeS.get(0);

                    AccessibilityNodeInfo targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "更多设置");
                    long time = System.currentTimeMillis();
                    while (targetNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {

                        if (listViewNode.isScrollable()) {
                            listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                            sleepShort();
                            listViewNode.refresh();
                        } else {
                            break;
                        }

                        targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "更多设置");
                    }

                    if (performAction(targetNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                        sleepShort();
                    } else {
                        showToastLong("请点击【更多设置】");
                        return;
                    }
                }
                return;


            } else if ("com.android.settings.SubSettings".equals(event.getClassName())) {

                String title = event.getText().toString();

                if (TextUtils.equals(title, "[更多设置]")) {
                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "系统安全");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "系统安全");
                        }

                        if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【系统安全】");
                            return;
                        }
                    }
                    return;

                } else if (TextUtils.equals(title, "[安全性和位置信息]")) {

                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "特殊应用权限");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                                listViewNode.refresh();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "特殊应用权限");
                        }


                        if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【特殊应用权限】");
                            return;
                        }
                    }
                    return;
                } else if (TextUtils.equals(title, "[特殊应用权限]")) {
                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "使用情况访问权限");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                                listViewNode.refresh();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "使用情况访问权限");
                        }

                        if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【使用情况访问权限】");
                            return;
                        }
                    }
                    return;
                } else if (TextUtils.equals(title, "[使用情况访问权限]")) {

                    AccessibilityNodeInfo allowPermissionNode = getChildNodeEqual(sourceNode, "允许查看使用情况");

                    if (allowPermissionNode != null && allowPermissionNode.getParent() != null) {
                        allowPermissionNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        sleep();
                        resetStep();
                    } else {

                        sleepLong();
                        AccessibilityNodeInfo loadingNode = getChildNodeEqual(sourceNode, "正在加载...");
                  /*  long time = System.currentTimeMillis();
                    while (loadingNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                        sleepLong();
                        sourceNode.refresh();
                        return;
                    }*/

                        List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/apps_list");

                        if (listViewNodes != null && listViewNodes.size() > 0) {
                            AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                            AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", appName);
                            long time2 = System.currentTimeMillis();
                            while (systemSafeBtn == null && System.currentTimeMillis() - time2 < MAX_SEARCH_TIME) {
                                if (listViewNode != null) {
                                    listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                    sleepShort();
                                    listViewNode.refresh();
                                } else {
                                    break;
                                }

                                systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", appName);
                            }


                            if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                                sleepShort();
                            } else {
                                showToastLong("请点击【" + appName + "】");
                                return;
                            }
                        }
                    }


                    return;
                }

            }


            if (event.getPackageName() != null && (event.getPackageName().equals("com.android.settings") || event.getPackageName().equals("com.miui.securitycenter"))) {

                getService().performGlobalAction(GLOBAL_ACTION_BACK);
                sleepShort();
                return;
            }
            gotoAppUsagePermission();
        } else {
            if ("com.android.settings.MiuiSettings".equals(event.getClassName().toString())) {

                List<AccessibilityNodeInfo> listViewNodeS = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                if (listViewNodeS != null && listViewNodeS.size() > 0) {

                    AccessibilityNodeInfo listViewNode = listViewNodeS.get(0);

                    AccessibilityNodeInfo targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "密码、隐私与安全");
                    long time = System.currentTimeMillis();
                    while (targetNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {

                        if (listViewNode.isScrollable()) {
                            listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                            sleepShort();
                            listViewNode.refresh();
                        } else {
                            break;
                        }

                        targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "密码、隐私与安全");
                    }

                    if (performAction(targetNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                        sleepShort();
                    } else {
                        showToastLong("请点击【密码、隐私与安全】");
                        return;
                    }
                }
                return;


            } else if ("com.android.settings.SubSettings".equals(event.getClassName())) {

                String title = event.getText().toString();

                if (TextUtils.equals(title, "[密码、隐私与安全]")) {
                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "系统安全");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "系统安全");
                        }

                        if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【系统安全】");
                            return;
                        }
                    }
                    return;

                } else if (TextUtils.equals(title, "[系统安全]")) {

                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "特殊应用权限");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                                listViewNode.refresh();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "特殊应用权限");
                        }


                        if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【特殊应用权限】");
                            return;
                        }
                    }
                    return;
                } else if (TextUtils.equals(title, "[特殊应用权限]")) {
                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "使用情况访问权限");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                                listViewNode.refresh();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "使用情况访问权限");
                        }

                        if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【使用情况访问权限】");
                            return;
                        }
                    }
                    return;
                } else if (TextUtils.equals(title, "[使用情况访问权限]")) {

                    AccessibilityNodeInfo allowPermissionNode = getChildNodeEqual(sourceNode, "允许查看使用情况");

                    if (allowPermissionNode != null && allowPermissionNode.getParent() != null) {
                        allowPermissionNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        sleep();
                        resetStep();
                    } else {

                        sleepLong();
                        AccessibilityNodeInfo loadingNode = getChildNodeEqual(sourceNode, "正在加载...");
                  /*  long time = System.currentTimeMillis();
                    while (loadingNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                        sleepLong();
                        sourceNode.refresh();
                        return;
                    }*/

                        List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/apps_list");

                        if (listViewNodes != null && listViewNodes.size() > 0) {
                            AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                            AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", appName);
                            long time2 = System.currentTimeMillis();
                            while (systemSafeBtn == null && System.currentTimeMillis() - time2 < MAX_SEARCH_TIME) {
                                if (listViewNode != null) {
                                    listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                    sleepShort();
                                    listViewNode.refresh();
                                } else {
                                    break;
                                }

                                systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", appName);
                            }


                            if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                                sleepShort();
                            } else {
                                showToastLong("请点击【" + appName + "】");
                                return;
                            }
                        }
                    }


                    return;
                }

            }


            if (event.getPackageName() != null && (event.getPackageName().equals("com.android.settings") || event.getPackageName().equals("com.miui.securitycenter"))) {

                getService().performGlobalAction(GLOBAL_ACTION_BACK);
                sleepShort();
                return;
            }
            gotoAppUsagePermission();
        }
    }

    @Override
    public void requestCloseUSB(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode = event.getSource();
        if (sourceNode == null) {
            return;
        }

        if ("com.android.settings.MiuiSettings".equals(event.getClassName().toString())) {

            List<AccessibilityNodeInfo> listViewNodeS = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

            if (listViewNodeS != null && listViewNodeS.size() > 0) {

                AccessibilityNodeInfo listViewNode = listViewNodeS.get(0);

                AccessibilityNodeInfo targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "更多设置");
                long time = System.currentTimeMillis();
                while (targetNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {

                    if (listViewNode.isScrollable()) {
                        listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        listViewNode.refresh();

                    } else {
                        break;
                    }

                    targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "更多设置");
                }


                if (performAction(targetNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                    sleepShort();
                } else {
                    showToastLong("请点击【更多设置】");
                    return;
                }
            }
            return;


        } else if ("com.android.settings.SubSettings".equals(event.getClassName())) {

            String title = event.getText().toString();

            if (TextUtils.equals(title, "[更多设置]")) {
                List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                if (listViewNodes != null && listViewNodes.size() > 0) {
                    AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                    AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "开发者选项");
                    long time = System.currentTimeMillis();
                    while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                        if (listViewNode != null) {
                            listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                            sleepShort();
                        } else {
                            break;
                        }

                        systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "开发者选项");
                    }


                    if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                        sleepShort();
                    } else {
                        showToastLong("请点击【开发者选项】");
                        return;
                    }
                }
                return;

            }

            if (TextUtils.equals(title, "[开发者选项]")) {

                List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                if (listViewNodes != null && listViewNodes.size() > 0) {
                    AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                    AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "USB 调试");

                    long time = System.currentTimeMillis();
                    while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                        if (listViewNode != null) {
                            listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                            sleepShort();
                        } else {
                            break;
                        }

                        systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "USB 调试");
                    }

                    if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                        systemSafeBtn.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        sleepShort();

                        if (!PermissionUtil.enAbleADB(getApplicationContext())) {
                            resetStep();
                        }

                    } else {
                        showToastLong("请点击【USB 调试】");
                        return;
                    }
                }
                return;
            }

        }


        if (event.getPackageName() != null && event.getPackageName().equals("com.android.settings")) {

            getService().performGlobalAction(GLOBAL_ACTION_BACK);
            sleepShort();
//                            gotoDeviceAdminPermissionSetting();
            return;
        }

        gotoDeveloperChoice();
    }

    @Override
    public void requestNotificationPermission(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode = event.getSource();
        if (sourceNode == null) {
            return;
        }
        if (SystemUtils.getMIUIVersion().equals("V10")) {
            if ("com.android.settings.MiuiSettings".equals(event.getClassName().toString())) {

                List<AccessibilityNodeInfo> listViewNodeS = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                if (listViewNodeS != null && listViewNodeS.size() > 0) {

                    AccessibilityNodeInfo listViewNode = listViewNodeS.get(0);

                    AccessibilityNodeInfo targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "更多设置");
                    long time = System.currentTimeMillis();
                    while (targetNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {

                        if (listViewNode.isScrollable()) {
                            listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                            sleepShort();
                            listViewNode.refresh();
                        } else {
                            break;
                        }

                        targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "更多设置");
                    }

                    if (performAction(targetNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                        sleepShort();
                    } else {
                        showToastLong("请点击【更多设置】");
                        return;
                    }
                }
                return;


            } else if ("com.android.settings.SubSettings".equals(event.getClassName())) {

                String title = event.getText().toString();

                if (TextUtils.equals(title, "[更多设置]")) {
                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "系统安全");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "系统安全");
                        }

                        if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【系统安全】");
                            return;
                        }
                    }
                    return;

                }

                if (TextUtils.equals(title, "[安全性和位置信息]")) {

                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "特殊应用权限");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "特殊应用权限");
                        }


                        if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【特殊应用权限】");
                            return;
                        }
                    }
                    return;
                }

                if (TextUtils.equals(title, "[特殊应用权限]")) {
                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "通知使用权");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "通知使用权");
                        }


                        if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【通知使用权】");
                            return;
                        }
                    }
                    return;
                }

                if (TextUtils.equals(title, "[通知使用权]")) {

                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", appName);
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", appName);
                        }

                        if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【" + appName + "】");
                            return;
                        }
                    }


                    return;
                }

            }


            if ("android.app.AlertDialog".equals(event.getClassName()) && event.getText() != null && event.getText().toString().contains(appName)) {
                AccessibilityNodeInfo acceptBtnNode = getChildNodeEqual(sourceNode, "允许");

                if (performAction(acceptBtnNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                    sleepShort();
                    resetStep();
                    getService().performGlobalAction(GLOBAL_ACTION_BACK);
                } else {
                    showToastLong("请点击【允许】");
                    return;
                }
            }


            if (event.getPackageName() != null && (event.getPackageName().equals("com.android.settings") || event.getPackageName().equals("com.miui.securitycenter"))) {

                getService().performGlobalAction(GLOBAL_ACTION_BACK);
                sleepShort();
                return;
            }
            gotoAppUsagePermission();
        } else {
            if ("com.android.settings.MiuiSettings".equals(event.getClassName().toString())) {

                List<AccessibilityNodeInfo> listViewNodeS = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                if (listViewNodeS != null && listViewNodeS.size() > 0) {

                    AccessibilityNodeInfo listViewNode = listViewNodeS.get(0);

                    AccessibilityNodeInfo targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "密码、隐私与安全");
                    long time = System.currentTimeMillis();
                    while (targetNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {

                        if (listViewNode.isScrollable()) {
                            listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                            sleepShort();
                            listViewNode.refresh();
                        } else {
                            break;
                        }

                        targetNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "密码、隐私与安全");
                    }

                    if (performAction(targetNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                        sleepShort();
                    } else {
                        showToastLong("请点击【密码、隐私与安全】");
                        return;
                    }
                }
                return;


            } else if ("com.android.settings.SubSettings".equals(event.getClassName())) {

                String title = event.getText().toString();

                if (TextUtils.equals(title, "[密码、隐私与安全]")) {

                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "系统安全");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "系统安全");
                        }

                        if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【系统安全】");
                            return;
                        }
                    }
                    return;

                }

                if (TextUtils.equals(title, "[系统安全]")) {

                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "特殊应用权限");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "特殊应用权限");
                        }


                        if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【特殊应用权限】");
                            return;
                        }
                    }
                    return;
                }

                if (TextUtils.equals(title, "[特殊应用权限]")) {
                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "通知使用权");
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", "通知使用权");
                        }


                        if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【通知使用权】");
                            return;
                        }
                    }
                    return;
                }

                if (TextUtils.equals(title, "[通知使用权]")) {

                    List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

                    if (listViewNodes != null && listViewNodes.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                        AccessibilityNodeInfo systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", appName);
                        long time = System.currentTimeMillis();
                        while (systemSafeBtn == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode != null) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                            } else {
                                break;
                            }

                            systemSafeBtn = getChildNodeByIdEqualText(sourceNode, "android:id/title", appName);
                        }

                        if (performAction(systemSafeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                            sleepShort();
                        } else {
                            showToastLong("请点击【" + appName + "】");
                            return;
                        }
                    }


                    return;
                }

            }


            if ("android.app.AlertDialog".equals(event.getClassName()) && event.getText() != null && event.getText().toString().contains(appName)) {
                AccessibilityNodeInfo acceptBtnNode = getChildNodeEqual(sourceNode, "允许");

                if (performAction(acceptBtnNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                    sleepShort();
                    resetStep();
                    getService().performGlobalAction(GLOBAL_ACTION_BACK);
                } else {
                    showToastLong("请点击【允许】");
                    return;
                }
            }


            if (event.getPackageName() != null && (event.getPackageName().equals("com.android.settings") || event.getPackageName().equals("com.miui.securitycenter"))) {

                getService().performGlobalAction(GLOBAL_ACTION_BACK);
                sleepShort();
                return;
            }
            gotoAppUsagePermission();
        }
    }

    @Override
    public void requestDefaultLauncher(AccessibilityEvent event) {
        AccessibilityNodeInfo nodeInfo = event.getSource();
        if (nodeInfo == null) {
            return;
        }
        if ("com.android.internal.app.ResolverActivity".equals(event.getClassName())) {
            AccessibilityNodeInfo targetAppNode = getChildNodeEqual(nodeInfo, appName);

            if (targetAppNode != null && targetAppNode.getParent() != null) {
                targetAppNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                sleep();
            }

            List<AccessibilityNodeInfo> alwaysBtnList = nodeInfo.findAccessibilityNodeInfosByViewId("android:id/button_always");
            if (alwaysBtnList != null && alwaysBtnList.size() > 0) {
                alwaysBtnList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }

//                    AccessibilityNodeInfo alwaysBtn = getChildNodeByIdEqualText(nodeInfo, "android:id/button_always");
        }
    }
}
