package com.android.launcher3.auto_get_permission.honor;

import android.accessibilityservice.AccessibilityService;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Build;
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.SystemUtils;

import java.util.List;

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

public class HonorAutoPermissionGetter extends BaseAutoGetPermission {

    private static final String TAG = "HonorAutoPermissionGetter";

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

    @Override
    public void start() {
        super.start();


    }


    @Override
    public void stop() {
        super.stop();
    }


    @Override
    public void requestkDeviceAdmin(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode = event.getSource();
        if ("com.android.settings.Settings$DeviceAdminSettingsActivity".equals(event.getClassName().toString())) {
//                        已跳转到设备管理器页面


            AccessibilityNodeInfo accessibilityNodeInfo1 = getChildNodeEqual(sourceNode, appName);


            if (!performAction(accessibilityNodeInfo1, AccessibilityNodeInfo.ACTION_CLICK)) {
                showToastLong("请找到【" + appName + "】应用并点击");
                return;
            } else {
                sleep();
            }
        } else if ("com.android.settings.DeviceAdminAdd".equals(event.getClassName().toString())) {
            AccessibilityNodeInfo accessibilityNodeInfo = getChildNodeByIdEqualText(sourceNode, "com.android.settings:id/action_button", "激活");
//                            List<AccessibilityNodeInfo> nodeInfoList = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/action_button");

//                            if (nodeInfoList != null && nodeInfoList.size() > 0) {
//                                nodeInfoList.get(0).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
//                                sleep();
//
//                            }

            if (performAction(accessibilityNodeInfo, AccessibilityNodeInfo.ACTION_CLICK)) {
                sleep();
                resetStep();
            } else {
                showToastLong("请点击【启用/激活此设备管理员】");
                return;
            }

        }
    }

    @Override
    public void reqeustAutoStartUp(AccessibilityEvent event) {

    }

    @Override
    public void requestBatteryOptimize(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode = event.getSource();
        if ("com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity".equals(event.getClassName())) {


            List<AccessibilityNodeInfo> listViewNodeList = sourceNode.findAccessibilityNodeInfosByViewId("com.huawei.systemmanager:id/lv_listview");
            if (listViewNodeList != null && listViewNodeList.size() > 0) {

                AccessibilityNodeInfo listViewNode = listViewNodeList.get(0);

                AccessibilityNodeInfo targetAppNode = getChildNodeByIdEqualText(listViewNode, "com.huawei.systemmanager:id/tv_title", appName);
                long time = System.currentTimeMillis();
                while (targetAppNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                    if (listViewNode.isScrollable()) {
                        listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        listViewNode.refresh();
                    } else {
                        break;
                    }

                    targetAppNode = getChildNodeByIdEqualText(listViewNode, "com.huawei.systemmanager:id/tv_title", appName);
                }

//                                将自动管理的swicher关闭
                if (targetAppNode.getParent() != null) {
                    List<AccessibilityNodeInfo> switcherNodes = targetAppNode.getParent().findAccessibilityNodeInfosByViewId("com.huawei.systemmanager:id/switcher");

                    if (switcherNodes != null && switcherNodes.size() > 0) {
                        if (switcherNodes.get(0).isCheckable()) {
                            if (switcherNodes.get(0).isChecked()) {
                                targetAppNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                sleep();
                            } else {
                                List<AccessibilityNodeInfo> manualBtnList = sourceNode.findAccessibilityNodeInfosByViewId("com.huawei.systemmanager:id/listitem_all_manual");
                                long time2 = System.currentTimeMillis();

                                while ((manualBtnList == null || manualBtnList.size() <= 0) && System.currentTimeMillis() - time2 < MAX_SEARCH_TIME) {
                                    if (listViewNode.isScrollable()) {
                                        listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
                                        sleepShort();
                                        listViewNode.refresh();
                                    } else {
                                        break;
                                    }

                                    manualBtnList = sourceNode.findAccessibilityNodeInfosByViewId("com.huawei.systemmanager:id/listitem_all_manual");
                                }

                                if (manualBtnList.size() > 0 && manualBtnList.get(0).getParent() != null) {
                                    manualBtnList.get(0).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                    sleep();
                                }
                            }
                        }
                    }
                }


            }

        }

        if ("android.app.AlertDialog".equals(event.getClassName())) {

            List<AccessibilityNodeInfo> switcherNodes = sourceNode.findAccessibilityNodeInfosByViewId("com.huawei.systemmanager:id/switcher");
            if (switcherNodes != null && switcherNodes.size() > 0) {
                for (AccessibilityNodeInfo switcherNode : switcherNodes) {
                    if (switcherNode.isCheckable() && !switcherNode.isChecked()) {
                        switcherNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        sleepShort();
                    }
                }
//
//                    performGlobalAction(GLOBAL_ACTION_BACK);
//                    sleep();
            }

        }

        if ("com.huawei.systemmanager.appcontrol.activity.BatchManuControlActivity".equals(event.getClassName())) {
            List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");
            if (listViewNodes != null && listViewNodes.size() > 0) {
                AccessibilityNodeInfo listViewNode = listViewNodes.get(0);

                AccessibilityNodeInfo nodeInfo = getChildNodeByIdEqualText(listViewNode, "com.huawei.systemmanager:id/tv_title", appName);
                long time = System.currentTimeMillis();
                while (nodeInfo == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                    if (listViewNode.isScrollable()) {
                        listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        listViewNode.refresh();
                    } else {
                        break;
                    }
                    nodeInfo = getChildNodeByIdEqualText(listViewNode, "com.huawei.systemmanager:id/tv_title", appName);
                }

                if (nodeInfo != null && nodeInfo.getParent() != null) {
                    AccessibilityNodeInfo linearNode = nodeInfo.getParent();


                    List<AccessibilityNodeInfo> checkBoxNodeList = linearNode.findAccessibilityNodeInfosByViewId("com.huawei.systemmanager:id/auto_checkbox");

                    if (checkBoxNodeList != null && checkBoxNodeList.size() > 0) {
                        if (!checkBoxNodeList.get(0).isChecked()) {
                            checkBoxNodeList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                            sleepShort();
                        }
                    }

                    List<AccessibilityNodeInfo> checkBoxNode2List = linearNode.findAccessibilityNodeInfosByViewId("com.huawei.systemmanager:id/associate_checkbox");

                    if (checkBoxNode2List != null && checkBoxNode2List.size() > 0) {
                        if (!checkBoxNode2List.get(0).isChecked()) {
                            checkBoxNode2List.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                            sleepShort();
                        }

                    }

                    List<AccessibilityNodeInfo> checkBoxNode3List = linearNode.findAccessibilityNodeInfosByViewId("com.huawei.systemmanager:id/background_checkbox");

                    if (checkBoxNode3List != null && checkBoxNode3List.size() > 0) {
                        if (!checkBoxNode3List.get(0).isChecked()) {
                            checkBoxNode3List.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                            sleepShort();
                        }


                    }

//                                    PermissionUtil.isIgnoreBatteryOptimize = true;
//                                    resetStep();

                    gotoBatteryOptimizeOnlyHUAWEI();
                    sleep();


                }

            }
        }

        long time2 = System.currentTimeMillis();

        if ("com.android.settings.Settings$HighPowerApplicationsActivity".equals(event.getClassName())) {
            boolean loadFinish = false;
            do {

                AccessibilityNodeInfo allAppNodeInfo = getChildNodeEqual(sourceNode, "所有应用");
                AccessibilityNodeInfo nodeInfo = getChildNodeEqual(sourceNode, "允许");
                if (allAppNodeInfo != null) {
                    loadFinish = true;
                    getChildNodeEqual(sourceNode, appName);
                    List<AccessibilityNodeInfo> listViewNodeList = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");
                    if (listViewNodeList != null && listViewNodeList.size() > 0) {
                        AccessibilityNodeInfo listViewNode = listViewNodeList.get(0);
                        AccessibilityNodeInfo targetAppNode = getChildNodeEqual(listViewNode, appName);
                        long time = System.currentTimeMillis();
                        while (nodeInfo == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                            if (listViewNode.isScrollable()) {
                                listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                                sleepShort();
                                listViewNode.refresh();

                            } else {

                                break;
                            }
                            targetAppNode = getChildNodeEqual(listViewNode, appName);
                        }
                        if (performAction(targetAppNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                            Logger.e(TAG, "忽略电池\t" + targetAppNode.toString());
                            sleepShort();
                        } else {
                            showToastLong("请找到【" + appName + "】并点击");
                            return;
                        }
                    }


                } else {
                    if (performAction(nodeInfo, AccessibilityNodeInfo.ACTION_CLICK)) {
                        loadFinish = true;
                        nodeInfo.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        sleep();
                    } else {
                        showToastLong("请点击【允许】");
                        return;
                    }

                }

            } while (!loadFinish && System.currentTimeMillis() - time2 < MAX_SEARCH_TIME);

        }
        if ("android.widget.FrameLayout".equals(event.getClassName())) {
            AccessibilityNodeInfo nodeInfo = getChildNodeEqual(sourceNode, "所有应用");
            if (performAction(nodeInfo, AccessibilityNodeInfo.ACTION_CLICK)) {
                sleepLongLong();
                sourceNode.refresh();
                operateAccessibilityEvent(event);
            } else {
                showToastLong("请点击【所有应用】");
                return;
            }
        }

        if ("android.app.AlertDialog".equals(event.getClassName())) {

            if (event.getText() != null && event.getText().toString().contains(appName)) {
                AccessibilityNodeInfo acceptTvNode = getChildNodeEqual(sourceNode, "允许");

                if (acceptTvNode != null && acceptTvNode.getParent() != null) {
                    acceptTvNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);

                    sleepShort();

                    AccessibilityNodeInfo deterBtnNode = getChildNodeEqual(sourceNode, "确定");
                    if (performAction(deterBtnNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                        resetStep();
                    } else {
                        showToastLong("请点击【确定】");
                        return;
                    }
                }


            } else {
                getService().performGlobalAction(GLOBAL_ACTION_BACK);
                sleepShort();
            }
        }
    }

    @Override
    public void requestUsageAppPermission(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode = event.getSource();


        if ("com.android.settings.Settings$UsageAccessSettingsActivity".equals(event.getClassName())) {
            getChildNodeEqual(sourceNode, appName);
            sleep();

            List<AccessibilityNodeInfo> appListNodeList = null;

//                getChildNodeEqual(sourceNode,appName);
            appListNodeList = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");

            if (appListNodeList == null || appListNodeList.isEmpty()) {
//
                appListNodeList = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/apps_list");
            }


            if (appListNodeList != null && appListNodeList.size() > 0) {
                AccessibilityNodeInfo appListNode = appListNodeList.get(0);

                AccessibilityNodeInfo targetAppItem = null;

                targetAppItem = getChildNodeByIdEqualText(appListNode, "com.android.settings:id/app_name", appName);
                if (targetAppItem == null) {

                    targetAppItem = getChildNodeByIdEqualText(appListNode, "android:id/title", appName);
                }

                long time = System.currentTimeMillis();
                while (targetAppItem == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {

                    if (appListNode.isScrollable()) {
                        appListNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        appListNode.refresh();
                        targetAppItem = getChildNodeByIdEqualText(appListNode, "com.android.settings:id/app_name", appName);
                        if (targetAppItem == null) {

                            targetAppItem = getChildNodeByIdEqualText(appListNode, "android:id/title", appName);
                        }
                    } else {
                        break;
                    }
                }

                if (!performAction(targetAppItem, AccessibilityNodeInfo.ACTION_CLICK)) {
                    showToastLong("请找到【" + appName + "】并点击");
                    return;
                } else {
                    List<AccessibilityNodeInfo> switcherNodeList = targetAppItem.getParent().findAccessibilityNodeInfosByViewId("com.android.settings:id/app_switch");

                    if (switcherNodeList != null && !switcherNodeList.isEmpty() && performAction(switcherNodeList.get(0), AccessibilityNodeInfo.ACTION_CLICK)) {
                        sleepShort();
//                                resetStep();
                    }
                }

            }
        } else if ("android.app.AlertDialog".equals(event.getClassName())) {
            AccessibilityNodeInfo allowNode = getChildNodeEqual(sourceNode, "允许");
            if (allowNode == null) {
                allowNode = getChildNodeEqual(sourceNode, "确定");
            }

            if (performAction(allowNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                sleep();
                resetStep();
            } else {
                showToastLong("请点击【确定】");
                return;
            }
        }


        /**
         * 使用记录访问权限(华为设备不同设备标题不同)
         */
        if ("com.android.settings.SubSettings".equals(event.getClassName()) && event.getText() != null && (event.getText().toString().contains("使用情况访问权限") || event.getText().toString().contains("使用记录访问权限") || event.getText().toString().contains("使用量数据访问"))) {

            if (SystemUtils.isSAMSUNG()) {
                AccessibilityNodeInfo nodeInfo = getChildNodeEqual(sourceNode, "允许追踪使用情况");

                if (performAction(nodeInfo, AccessibilityNodeInfo.ACTION_CLICK)) {

                    sleepShort();
                    resetStep();
                } else {
                    showToastLong("请点击【允许追踪使用情况】并返回上一页");
                    return;
                }
            } else if (SystemUtils.isNOKIA()) {
                sleep();
                List<AccessibilityNodeInfo> switchWidgetNodeList = sourceNode.findAccessibilityNodeInfosByViewId("android:id/switch_widget");

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

                    if (switchWidgetNodeList.get(0).isChecked()) {
                        resetStep();

                    }

                    switchWidgetNodeList.get(0).getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    resetStep();

                }

            } else {
                AccessibilityNodeInfo textViewNode = getChildNodeByIdEqualText(sourceNode, "android:id/title", "允许访问使用记录");

                if (textViewNode != null && textViewNode.getParent() != null) {
                    if (textViewNode.getParent().isClickable()) {
                        textViewNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        resetStep();
                    }
                } else {
                    textViewNode = getChildNodeByIdEqualText(sourceNode, "android:id/title", "允许查看使用情况");
                    if (performAction(textViewNode, AccessibilityNodeInfo.ACTION_CLICK)) {
                        resetStep();
                    } else {
                        showToastLong("请点击【允许查看使用情况】并返回上一页");
                        return;
                    }
                }
            }
        }
    }

    @Override
    public void requestCloseUSB(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode = event.getSource();
        if ("com.android.settings.Settings$DevelopmentSettingsActivity".equals(event.getClassName())) {

//                oppo也是这个页面
            getChildNodeEqual(sourceNode, "USB 调试");
            List<AccessibilityNodeInfo> listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/list");
            if (listViewNodes == null || listViewNodes.isEmpty()) {

                listViewNodes = sourceNode.findAccessibilityNodeInfosByViewId("android:id/list");
            }
            if (listViewNodes != null && listViewNodes.size() > 0) {
                AccessibilityNodeInfo listViewNode = listViewNodes.get(0);
                AccessibilityNodeInfo usbNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "USB调试");
                if (usbNode == null) {
                    usbNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "USB 调试");
                }
                long time = System.currentTimeMillis();
                while (usbNode == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                    if (listViewNode.isScrollable()) {
                        listViewNode.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        sleepShort();
                        listViewNode.refresh();
                    } else {
                        break;
                    }
                    usbNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "USB调试");
                    if (usbNode == null) {
                        usbNode = getChildNodeByIdEqualText(listViewNode, "android:id/title", "USB 调试");
                    }
                }
                if (usbNode != null && usbNode.getParent() != null) {
                    if (SystemUtils.isOPPO()) {
                        List<AccessibilityNodeInfo> switchWidgetList = usbNode.getParent().findAccessibilityNodeInfosByViewId("android:id/switch_widget");
                        if (switchWidgetList != null && !switchWidgetList.isEmpty()) {
                            switchWidgetList.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                            sleepShort();
                            resetStep();
                        }
                    } else {
                        usbNode.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        sleepShort();
//                            resetStep();
                    }

                }

            }
        }
    }

    @Override
    public void requestNotificationPermission(AccessibilityEvent event) {
        AccessibilityNodeInfo sourceNode = event.getSource();
        if ("com.android.settings.Settings$NotificationAccessSettingsActivity".equals(event.getClassName())) {

            AccessibilityNodeInfo targetApp = getChildNodeEqual(sourceNode, appName);
            if (!performAction(targetApp, AccessibilityNodeInfo.ACTION_CLICK)) {
                List<AccessibilityNodeInfo> nodeInfoList = sourceNode.findAccessibilityNodeInfosByViewId("com.android.settings:id/list");
                if (nodeInfoList != null && nodeInfoList.size() > 0) {
                    AccessibilityNodeInfo listViewNode = nodeInfoList.get(0);

                    AccessibilityNodeInfo targetAppItem = getChildNodeByIdEqualText(listViewNode, "android:id/title", appName);
                    long time = System.currentTimeMillis();
                    while (targetAppItem == null && System.currentTimeMillis() - time < MAX_SEARCH_TIME) {
                        if (listViewNode != null) {

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

                        targetAppItem = getChildNodeByIdEqualText(listViewNode, "android:id/title", appName);


                    }

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


        }

        if ("android.app.AlertDialog".equals(event.getClassName())) {
//                            允许
            List<AccessibilityNodeInfo> nodeInfoList = sourceNode.findAccessibilityNodeInfosByViewId("android:id/button1");
            if (nodeInfoList != null && nodeInfoList.size() > 0 && performAction(nodeInfoList.get(0), AccessibilityNodeInfo.ACTION_CLICK)) {

                resetStep();
                return;

            } else {
                showToastLong("请点击【允许】");
                return;
            }
        }
    }

    @Override
    public void requestDefaultLauncher(AccessibilityEvent event) {
        AccessibilityNodeInfo nodeInfo = event.getSource();
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
            showToastLong("华为8.0以上的设备不支持更换桌面哦");
            finish();
            return;
        }
        if ("com.android.settings.HWSettings".equals(event.getClassName())) {
            AccessibilityNodeInfo accessibilityNodeInfo = getChildNodeEqual(nodeInfo, "应用和通知");
            if (performAction(accessibilityNodeInfo, AccessibilityNodeInfo.ACTION_CLICK)) {
                return;
            } else {
                showToastLong("请点击【应用和通知】");
                return;
            }

        }

        if ("com.android.settings.Settings$AppAndNotificationDashboardActivity".equals(event.getClassName())) {

            AccessibilityNodeInfo defaultAppSetting = getChildNodeEqual(nodeInfo, "默认应用");
            if (performAction(defaultAppSetting, AccessibilityNodeInfo.ACTION_CLICK)) {
                return;
            } else {
                showToastLong("请点击【默认应用】");
                return;
            }

        }

        if ("com.android.settings.SubSettings".equals(event.getClassName()) && TextUtils.equals("[默认应用]", event.getText().toString())) {
//                        默认应用
            AccessibilityNodeInfo defaultLauncherSetting = getChildNodeEqual(nodeInfo, "桌面");
            if (performAction(defaultLauncherSetting, AccessibilityNodeInfo.ACTION_CLICK)) {
                return;
            } else {
                showToastLong("请点击【桌面】");
                return;
            }
        }

        if ("com.android.settings.Settings$PreferredSettingsActivity".equals(event.getClassName())) {
            AccessibilityNodeInfo targetAppNode = getChildNodeEqual(nodeInfo, appName);
            if (performAction(targetAppNode, AccessibilityNodeInfo.ACTION_CLICK)) {
            } else {
                showToastLong("请点击【" + appName + "】");
                return;
            }

            return;
        }

        if ("android.app.AlertDialog".equals(event.getClassName())) {
            if (event.getText() != null) {
                String text = event.getText().toString();
                if (text.contains("更换默认桌面")) {
                    AccessibilityNodeInfo changeBtn = getChildNodeEqual(nodeInfo, "更换");
                    if (performAction(changeBtn, AccessibilityNodeInfo.ACTION_CLICK)) {
                        resetStep();
                    } else {
                        showToastLong("请点击【更换】");
                        return;
                    }
                    return;
                }
            }
        }

        if (!"com.android.settings.HWSettings".equals(event.getClassName())) {
            if (event.getPackageName().toString().contains("com.android.settings")) {
                getService().performGlobalAction(GLOBAL_ACTION_BACK);
                sleepShort();
            } else {
                gotoDefaultLauncherSetting();
                sleep();
            }
        }

    }


    public void gotoBatteryOptimizeOnlyHUAWEI() {

        Intent intent = new Intent();
        ComponentName componentName = new ComponentName("com.android.settings", "com.android.settings.Settings$HighPowerApplicationsActivity");
        intent.setComponent(componentName);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getService().startActivity(intent);

    }
}
