from appium.options.android import UiAutomator2Options
import logging
from selenium import webdriver

def app_setup_driver(platformName, platformVersion, deviceName, appPackage, appActivity, udid):

    desired_caps = {
        'platformName': platformName,  # 被测手机是安卓
        'platformVersion': platformVersion,  # 手机安卓版本，如果是鸿蒙系统，依次尝试 12、11、10 这些版本号
        'deviceName': deviceName,  # 设备名，安卓手机可以随意填写
        'appPackage': appPackage,  # 启动APP Package名称
        'appActivity': appActivity,  # 启动Activity名称
        'unicodeKeyboard': True,  # 自动化需要输入中文时填True
        'resetKeyboard': True,  # 执行完程序恢复原来输入法
        'noReset': True,  # 不要重置App
        'newCommandTimeout': 6000,
        'automationName': 'UiAutomator2',
        'skipUnlock': True,
        'autoGrantPermissions': True,
        'udid':udid
    }
    logging.info(f"desired_caps参数：{desired_caps}")

    try:
        logging.info("正在初始化 Appium 驱动程序...")
        driver = webdriver.Remote('http://localhost:4723/wd/hub',
                                  options=UiAutomator2Options().load_capabilities(desired_caps))
        logging.info("Appium 驱动程序初始化成功。")
        return driver
    except Exception as e:
        logging.error(f"初始化驱动程序失败: {e}")
        raise

def swipe_up(app_driver):
    # 获取屏幕尺寸
    size = app_driver.get_window_size()
    start_x = size['width'] // 2
    start_y = int(size['height'] * 0.2)  # 起始y坐标，屏幕高度的20%
    end_x = start_x
    end_y = int(size['height'] * 0.8)  # 结束y坐标，屏幕高度的80%

    # 执行滑动操作
    app_driver.swipe(start_x, start_y, end_x, end_y, duration=500)