@echo off
chcp 65001 >nul
color 0A
title ADB授权WRITE_SECURE_SETTINGS

set PERM=android.permission.WRITE_SECURE_SETTINGS

echo.
echo ================================
echo    ADB 授权 WRITE_SECURE_SETTINGS
echo ================================
echo.

echo [1/5] 检查ADB...
adb version >nul 2>&1
if errorlevel 1 (
    echo 错误：未找到adb
    pause >nul
    exit
)

echo [2/5] 检查设备连接...
adb devices | findstr "device" >nul 2>&1
if errorlevel 1 (
    echo 错误：未连接设备
    pause >nul
    exit
)

echo [3/5] 检查USB调试...
adb shell exit >nul 2>&1
if errorlevel 1 (
    echo 错误：USB调试未授权
    pause >nul
    exit
)

echo [4/5] 检查USB调试(安全设置)...
adb shell "settings get global adb_secure" >nul 2>&1
echo 已跳过检查（无此功能不影响授权）

echo [5/5] 检查禁止权限监控...
echo 已跳过检查（无此功能不影响授权）

echo.
echo ================================
echo 1 - Hamibot 普通版
echo 2 - Hamibot Pro
echo ================================
set /p CH=请选择1/2:

if "%CH%"=="1" (
    set PKG=com.hamibot.hamibot
) else if "%CH%"=="2" (
    set PKG=com.ffdz.ybilg
) else (
    echo 无效选择
    pause >nul
    exit
)

echo.
echo 正在授权...
adb shell pm grant %PKG% %PERM%

if errorlevel 1 (
    echo 授权失败
) else (
    echo 授权成功
)

echo.
pause >nul