mirror of
https://github.com/LmeSzinc/StarRailCopilot.git
synced 2024-11-16 06:25:24 +00:00
Refactor: [ALAS] Launch app with activity name
(cherry picked from commit debbbdae9695ecdcbd07c9940637f57b28d311f3)
This commit is contained in:
parent
8f86169b41
commit
a01b6d0c03
@ -8,6 +8,7 @@ from adbutils.errors import AdbError
|
||||
from lxml import etree
|
||||
|
||||
from module.base.decorator import Config
|
||||
from module.config.server import DICT_PACKAGE_TO_ACTIVITY
|
||||
from module.device.connection import Connection
|
||||
from module.device.method.utils import (ImageTruncated, PackageNotInstalled, RETRY_TRIES, handle_adb_error,
|
||||
handle_unknown_host_service, remove_prefix, retry_sleep)
|
||||
@ -233,7 +234,7 @@ class Adb(Connection):
|
||||
raise OSError("Couldn't get focused app")
|
||||
|
||||
@retry
|
||||
def app_start_adb(self, package_name=None, allow_failure=False):
|
||||
def _app_start_adb_monkey(self, package_name=None, allow_failure=False):
|
||||
"""
|
||||
Args:
|
||||
package_name (str):
|
||||
@ -241,6 +242,9 @@ class Adb(Connection):
|
||||
|
||||
Returns:
|
||||
bool: If success to start
|
||||
|
||||
Raises:
|
||||
PackageNotInstalled:
|
||||
"""
|
||||
if not package_name:
|
||||
package_name = self.package
|
||||
@ -257,26 +261,104 @@ class Adb(Connection):
|
||||
raise PackageNotInstalled(package_name)
|
||||
elif 'inaccessible' in result:
|
||||
# /system/bin/sh: monkey: inaccessible or not found
|
||||
pass
|
||||
return False
|
||||
else:
|
||||
# Events injected: 1
|
||||
# ## Network stats: elapsed time=4ms (0ms mobile, 0ms wifi, 4ms not connected)
|
||||
return True
|
||||
|
||||
@retry
|
||||
def _app_start_adb_am(self, package_name=None, activity_name=None, allow_failure=False):
|
||||
"""
|
||||
Args:
|
||||
package_name (str):
|
||||
activity_name (str):
|
||||
allow_failure (bool):
|
||||
|
||||
Returns:
|
||||
bool: If success to start
|
||||
|
||||
Raises:
|
||||
PackageNotInstalled:
|
||||
"""
|
||||
if not package_name:
|
||||
package_name = self.package
|
||||
if not activity_name:
|
||||
result = self.adb_shell(['dumpsys', 'package', package_name])
|
||||
res = re.search(r'android.intent.action.MAIN:\s+\w+ ([\w.\/]+) filter \w+\s+'
|
||||
r'.*\s+Category: "android.intent.category.LAUNCHER"',
|
||||
result)
|
||||
if res:
|
||||
# com.bilibili.azurlane/com.manjuu.azurlane.MainActivity
|
||||
activity_name = res.group(1)
|
||||
try:
|
||||
activity_name = activity_name.split('/')[-1]
|
||||
except IndexError:
|
||||
logger.error(f'No activity name from {activity_name}')
|
||||
return False
|
||||
else:
|
||||
if allow_failure:
|
||||
return False
|
||||
else:
|
||||
logger.error(result)
|
||||
raise PackageNotInstalled(package_name)
|
||||
self.adb_shell(['am', 'start', '-a', 'android.intent.action.MAIN', '-c',
|
||||
'android.intent.category.LAUNCHER', '-n', activity_name])
|
||||
|
||||
ret = self.adb_shell(['am', 'start', '-a', 'android.intent.action.MAIN', '-c',
|
||||
'android.intent.category.LAUNCHER', '-n', f'{package_name}/{activity_name}'])
|
||||
# Invalid activity
|
||||
# Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=... }
|
||||
# Error type 3
|
||||
# Error: Activity class {.../...} does not exist.
|
||||
if 'Error: Activity class' in ret:
|
||||
if allow_failure:
|
||||
return False
|
||||
else:
|
||||
logger.error(ret)
|
||||
return False
|
||||
# Already running
|
||||
# Warning: Activity not started, intent has been delivered to currently running top-most instance.
|
||||
if 'Warning: Activity not started' in ret:
|
||||
logger.info('App activity is already started')
|
||||
return True
|
||||
# Success
|
||||
# Starting: Intent...
|
||||
return True
|
||||
|
||||
# No @retry decorator since _app_start_adb_am and _app_start_adb_monkey have @retry already
|
||||
# @retry
|
||||
def app_start_adb(self, package_name=None, activity_name=None, allow_failure=False):
|
||||
"""
|
||||
Args:
|
||||
package_name (str):
|
||||
If None, to get from config
|
||||
activity_name (str):
|
||||
If None, to get from DICT_PACKAGE_TO_ACTIVITY
|
||||
If still None, launch from monkey
|
||||
If monkey failed, fetch activity name and launch from am
|
||||
allow_failure (bool):
|
||||
True for no PackageNotInstalled raising, just return False
|
||||
|
||||
Returns:
|
||||
bool: If success to start
|
||||
|
||||
Raises:
|
||||
PackageNotInstalled:
|
||||
"""
|
||||
if not package_name:
|
||||
package_name = self.package
|
||||
if not activity_name:
|
||||
activity_name = DICT_PACKAGE_TO_ACTIVITY.get(package_name)
|
||||
|
||||
if activity_name:
|
||||
if self._app_start_adb_am(package_name, activity_name, allow_failure):
|
||||
return True
|
||||
if self._app_start_adb_monkey(package_name, allow_failure):
|
||||
return True
|
||||
if self._app_start_adb_am(package_name, activity_name, allow_failure):
|
||||
return True
|
||||
|
||||
logger.error('app_start_adb: All trials failed')
|
||||
return False
|
||||
|
||||
@retry
|
||||
def app_stop_adb(self, package_name=None):
|
||||
|
@ -8,6 +8,7 @@ from adbutils.errors import AdbError
|
||||
from lxml import etree
|
||||
|
||||
from module.base.utils import *
|
||||
from module.config.server import DICT_PACKAGE_TO_ACTIVITY
|
||||
from module.device.connection import Connection
|
||||
from module.device.method.utils import (ImageTruncated, PackageNotInstalled, RETRY_TRIES, handle_adb_error,
|
||||
handle_unknown_host_service, possible_reasons, retry_sleep)
|
||||
@ -223,11 +224,24 @@ class Uiautomator2(Connection):
|
||||
return result['package']
|
||||
|
||||
@retry
|
||||
def app_start_uiautomator2(self, package_name=None):
|
||||
def app_start_uiautomator2(self, package_name=None, activity_name=None):
|
||||
"""
|
||||
Args:
|
||||
package_name (str):
|
||||
If None, to get from config
|
||||
activity_name (str):
|
||||
If None, to get from DICT_PACKAGE_TO_ACTIVITY
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
if not package_name:
|
||||
package_name = self.package
|
||||
if not activity_name:
|
||||
activity_name = DICT_PACKAGE_TO_ACTIVITY.get(package_name)
|
||||
|
||||
try:
|
||||
self.u2.app_start(package_name)
|
||||
self.u2.app_start(package_name, activity_name)
|
||||
except u2.exceptions.BaseError as e:
|
||||
# BaseError: package "com.bilibili.azurlane" not found
|
||||
logger.error(e)
|
||||
|
Loading…
Reference in New Issue
Block a user