void show_power_menu()
{

    static char* headers[] = {  "Reboot or Shutdown",
                                "",
                                NULL
    };

    static char* list[] = { "Reboot System",
                            "Reboot to Recovery",
                            "Reboot to Fastboot",
			    "Shutdown",
                            NULL
    };

    for (;;)
    {
        int chosen_item = get_filtered_menu_selection(headers, list, 0, 0, sizeof(list) / sizeof(char*));
        if (chosen_item == GO_BACK)
            break;
        switch (chosen_item)
        {
            case 0:
                ui_print("Rebooting System...\n");
		android_reboot(ANDROID_RB_RESTART, 0, 0);
                break;

            case 1:
                ui_print("Rebooting Into Recovery...\n");
		android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
                break;

	    case 2:
		ui_print("Rebooting Into Fastboot Mode...\n");
        	android_reboot(ANDROID_RB_RESTART2, 0, "bootloader");
        	break;

	    case 3:
		ui_print("Shutting down...\n");
        	android_reboot(ANDROID_RB_POWEROFF, 0, 0);
		break;
	}
    }	
}
// Advanced boot menu(boot, recovery boot, boot-loader) return 1 if chosen 'boot'
void show_advanced_boot_menu()
{
    static char* headers[] = {  "高级电源菜单",
                                "",
                                NULL
    };

    static char* list[] = { "重启系统",
                            "关闭系统",
                            "重启至Recovery",
                            "重启至Fastboot",
                            NULL
    };

    for (;;)
    {
        int chosen_item = get_filtered_menu_selection(headers, list, 0, 0, sizeof(list) / sizeof(char*));
        if (chosen_item == GO_BACK)
            break;
        switch (chosen_item)
        {
        		case 0:
                                ui_print("正在重启...\n");
                                android_reboot(ANDROID_RB_RESTART, 0, 0);
				break;
			case 1:
                                ui_print("正在关机...\n");
                                android_reboot(ANDROID_RB_POWEROFF, 0, 0);
                                break;
			case 2:
				ui_print("正在重启...\n");
				android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
				break;
			case 3:
				ui_print("正在重启...\n");
				android_reboot(ANDROID_RB_RESTART2, 0, "bootloader");
				break;

        }
    }
}
void show_advanced_menu()
{
    static char* headers[] = {  "Advanced Menu",
                                "",
                                NULL
    };

    static char* list[] = { "reboot recovery",
                            "reboot to bootloader",
                            "power off",
                            "wipe dalvik cache",
                            "report error",
                            "key test",
                            "show log",
                            "partition sdcard",
                            "partition external sdcard",
                            "partition internal sdcard",
                            NULL
    };

    char bootloader_mode[PROPERTY_VALUE_MAX];
    property_get("ro.bootloader.mode", bootloader_mode, "");
    if (!strcmp(bootloader_mode, "download")) {
        list[1] = "reboot to download mode";
    }

    if (!can_partition("/sdcard")) {
        list[7] = NULL;
    }
    if (!can_partition("/external_sd")) {
        list[8] = NULL;
    }
    if (!can_partition("/emmc")) {
        list[9] = NULL;
    }

    for (;;)
    {
        int chosen_item = get_filtered_menu_selection(headers, list, 0, 0, sizeof(list) / sizeof(char*));
        if (chosen_item == GO_BACK)
            break;
        switch (chosen_item)
        {
            case 0:
            {
                ui_print("Rebooting recovery...\n");
                reboot_main_system(ANDROID_RB_RESTART2, 0, "recovery");
                break;
            }
            case 1:
            {
                if (!strcmp(bootloader_mode, "download")) {
                    ui_print("Rebooting to download mode...\n");
                    reboot_main_system(ANDROID_RB_RESTART2, 0, "download");
                } else {
                    ui_print("Rebooting to bootloader...\n");
                    reboot_main_system(ANDROID_RB_RESTART2, 0, "bootloader");
                }
                break;
            }
            case 2:
            {
                ui_print("Shutting down...\n");
                reboot_main_system(ANDROID_RB_POWEROFF, 0, 0);
                break;
            }
            case 3:
                if (0 != ensure_path_mounted("/data"))
                    break;
                ensure_path_mounted("/sd-ext");
                ensure_path_mounted("/cache");
                if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) {
                    __system("rm -r /data/dalvik-cache");
                    __system("rm -r /cache/dalvik-cache");
                    __system("rm -r /sd-ext/dalvik-cache");
                    ui_print("Dalvik Cache wiped.\n");
                }
                ensure_path_unmounted("/data");
                break;
            case 4:
                handle_failure(1);
                break;
            case 5:
            {
                ui_print("Outputting key codes.\n");
                ui_print("Go back to end debugging.\n");
                int key;
                int action;
                do
                {
                    key = ui_wait_key();
                    action = device_handle_key(key, 1);
                    ui_print("Key: %d\n", key);
                }
                while (action != GO_BACK);
                break;
            }
            case 6:
                ui_printlogtail(12);
                break;
            case 7:
                partition_sdcard("/sdcard");
                break;
            case 8:
                partition_sdcard("/external_sd");
                break;
            case 9:
                partition_sdcard("/emmc");
                break;
        }
    }
}
void show_nandroid_menu()
{
    static char* headers[] = {  "Backup and Restore",
                                "",
                                NULL
    };

    char* list[] = { "backup",
                            "restore",
                            "delete",
                            "advanced restore",
                            "free unused backup data",
                            "choose default backup format",
                            NULL,
                            NULL,
                            NULL,
                            NULL,
                            NULL,
                            NULL,
                            NULL
    };

    char *other_sd = NULL;
    if (volume_for_path("/emmc") != NULL) {
        other_sd = "/emmc";
        list[6] = "backup to internal sdcard";
        list[7] = "restore from internal sdcard";
        list[8] = "advanced restore from internal sdcard";
        list[9] = "delete from internal sdcard";
    }
    else if (volume_for_path("/external_sd") != NULL) {
        other_sd = "/external_sd";
        list[6] = "backup to external sdcard";
        list[7] = "restore from external sdcard";
        list[8] = "advanced restore from external sdcard";
        list[9] = "delete from external sdcard";
    }
#ifdef RECOVERY_EXTEND_NANDROID_MENU
    extend_nandroid_menu(list, 10, sizeof(list) / sizeof(char*));
#endif

    for (;;) {
        int chosen_item = get_filtered_menu_selection(headers, list, 0, 0, sizeof(list) / sizeof(char*));
        if (chosen_item == GO_BACK)
            break;
        switch (chosen_item)
        {
            case 0:
                {
                    char backup_path[PATH_MAX];
                    time_t t = time(NULL);
                    struct tm *tmp = localtime(&t);
                    if (tmp == NULL)
                    {
                        struct timeval tp;
                        gettimeofday(&tp, NULL);
                        sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
                    }
                    else
                    {
                        strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
                    }
                    nandroid_backup(backup_path);
                    write_recovery_version();
                }
                break;
            case 1:
                show_nandroid_restore_menu("/sdcard");
                write_recovery_version();
                break;
            case 2:
                show_nandroid_delete_menu("/sdcard");
                write_recovery_version();
                break;
            case 3:
                show_nandroid_advanced_restore_menu("/sdcard");
                write_recovery_version();
                break;
            case 4:
                run_dedupe_gc(other_sd);
                break;
            case 5:
                choose_default_backup_format();
                break;
            case 6:
                {
                    char backup_path[PATH_MAX];
                    time_t t = time(NULL);
                    struct tm *timeptr = localtime(&t);
                    if (timeptr == NULL)
                    {
                        struct timeval tp;
                        gettimeofday(&tp, NULL);
                        if (other_sd != NULL) {
                            sprintf(backup_path, "%s/clockworkmod/backup/%d", other_sd, tp.tv_sec);
                        }
                        else {
                            break;
                        }
                    }
                    else
                    {
                        if (other_sd != NULL) {
                            char tmp[PATH_MAX];
                            strftime(tmp, sizeof(tmp), "clockworkmod/backup/%F.%H.%M.%S", timeptr);
                            // this sprintf results in:
                            // /emmc/clockworkmod/backup/%F.%H.%M.%S (time values are populated too)
                            sprintf(backup_path, "%s/%s", other_sd, tmp);
                        }
                        else {
                            break;
                        }
                    }
                    nandroid_backup(backup_path);
                }
                break;
            case 7:
                if (other_sd != NULL) {
                    show_nandroid_restore_menu(other_sd);
                }
                break;
            case 8:
                if (other_sd != NULL) {
                    show_nandroid_advanced_restore_menu(other_sd);
                }
                break;
            case 9:
                if (other_sd != NULL) {
                    show_nandroid_delete_menu(other_sd);
                }
                break;
            default:
#ifdef RECOVERY_EXTEND_NANDROID_MENU
                handle_nandroid_menu(10, chosen_item);
#endif
                break;
        }
    }
}
void show_advanced_menu()
{
    static char* headers[] = {  "Advanced Menu",
                                "",
                                NULL
                             };

    static char* list[] = { "reboot recovery",
                            "wipe dalvik cache",
                            "wipe battery stats",
                            "report error",
                            "key test",
                            "show log",
                            "fix permissions",
                            "partition sdcard",
                            "partition external sdcard",
                            "partition internal sdcard",
                            NULL
                          };

    if (!can_partition("/sdcard")) {
        list[7] = NULL;
    }
    if (!can_partition("/external_sd")) {
        list[8] = NULL;
    }
    if (!can_partition("/emmc")) {
        list[9] = NULL;
    }

    for (;;)
    {
        int chosen_item = get_filtered_menu_selection(headers, list, 0, 0, sizeof(list) / sizeof(char*));
        if (chosen_item == GO_BACK)
            break;
        switch (chosen_item)
        {
        case 0:
            android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
            break;
        case 1:
            if (0 != ensure_path_mounted("/data"))
                break;
            ensure_path_mounted("/sd-ext");
            ensure_path_mounted("/cache");
            if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) {
                __system("rm -r /data/dalvik-cache");
                __system("rm -r /cache/dalvik-cache");
                __system("rm -r /sd-ext/dalvik-cache");
                ui_print("Dalvik Cache wiped.\n");
            }
            ensure_path_unmounted("/data");
            break;
        case 2:
            if (confirm_selection( "Confirm wipe?", "Yes - Wipe Battery Stats"))
                wipe_battery_stats();
            break;
        case 3:
            handle_failure(1);
            break;
        case 4:
        {
            ui_print("Outputting key codes.\n");
            ui_print("Go back to end debugging.\n");
            int key;
            int action;
            do
            {
                key = ui_wait_key();
                action = device_handle_key(key, 1);
                ui_print("Key: %d\n", key);
            }
            while (action != GO_BACK);
            break;
        }
        case 5:
            ui_printlogtail(12);
            break;
        case 6:
            ensure_path_mounted("/system");
            ensure_path_mounted("/data");
            ui_print("Fixing permissions...\n");
            __system("fix_permissions");
            ui_print("Done!\n");
            break;
        case 7:
            partition_sdcard("/sdcard");
            break;
        case 8:
            partition_sdcard("/external_sd");
            break;
        case 9:
            partition_sdcard("/emmc");
            break;
        }
    }
}
void show_advanced_menu()
{
    static char* headers[] = {  "高级设置",
                                "",
                                NULL
    };

    static char* list[] = { "清空Dalvik Cache",
                            "发送错误报告",
                            "按键测试",
                            "显示日志",
                            "对SD卡进行分区",
                            "对外置SD卡进行分区",
                            "对内置SD卡进行分区",
                            NULL
    };

    if (!can_partition("/sdcard")) {
        list[4] = NULL;
    }
    if (!can_partition("/external_sd")) {
        list[5] = NULL;
    }
    if (!can_partition("/emmc")) {
        list[6] = NULL;
    }

    for (;;)
    {
        int chosen_item = get_filtered_menu_selection(headers, list, 0, 0, sizeof(list) / sizeof(char*));
        if (chosen_item == GO_BACK)
            break;
        switch (chosen_item)
        {
            case 0:
                if (0 != ensure_path_mounted("/data"))
                    break;
                ensure_path_mounted("/sd-ext");
                ensure_path_mounted("/cache");
                if (confirm_selection( "确定清空?", "是 - 清空Dalvik Cache")) {
                    __system("rm -r /data/dalvik-cache");
                    __system("rm -r /cache/dalvik-cache");
                    __system("rm -r /sd-ext/dalvik-cache");
                    ui_print("Dalvik Cache 已经清空!\n");
                }
                ensure_path_unmounted("/data");
                break;
            case 1:
                handle_failure(1);
                break;
            case 2:
            {
                ui_print("正在进行键位测试\n");
                ui_print("按返回结束测试.\n");
                int key;
                int action;
                do
                {
                    key = ui_wait_key();
                    action = device_handle_key(key, 1);
                    ui_print("键值: %d\n", key);
                }
                while (action != GO_BACK);
                break;
            }
            case 3:
                ui_printlogtail(12);
                break;
            case 4:
                partition_sdcard("/sdcard");
                break;
            case 5:
                partition_sdcard("/external_sd");
                break;
            case 6:
                partition_sdcard("/emmc");
                break;
        }
    }
}