Skip to content

Commit

Permalink
Add utility to list all installed package names.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 707473054
  • Loading branch information
The android_world Authors committed Dec 18, 2024
1 parent 1c05f3c commit 5fc92c3
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions android_world/env/adb_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,23 +544,18 @@ def get_adb_activity(app_name: str) -> Optional[str]:
return activity


def get_all_apps(
def get_all_package_names(
env: env_interface.AndroidEnvInterface,
timeout_sec: Optional[float] = _DEFAULT_TIMEOUT_SECS,
) -> list[str]:
"""Returns all packages installed on the device.
Note: the output list will not be exhaustive as it is currently based on a
mapping we define, so any apps not included in that mapping will not be
output here.
Args:
env: The AndroidEnv interface.
timeout_sec: A timeout to use for this operation. If not set the default
timeout will be used.
timeout_sec: A timeout to use for this operation.
Returns:
A list of app names.
A list of installed package names.
"""
response = env.execute_adb_call(
adb_pb2.AdbRequest(
Expand All @@ -573,16 +568,36 @@ def get_all_apps(
)
)
if response.status != adb_pb2.AdbResponse.Status.OK:
logging.error(
'Failed to issue package manager request',
)
logging.error('Failed to issue package manager request.')

package_names = list(response.package_manager.list.items)
return package_names


def get_all_apps(
env: env_interface.AndroidEnvInterface,
timeout_sec: Optional[float] = _DEFAULT_TIMEOUT_SECS,
) -> list[str]:
"""Returns all apps installed on the device.
Note: the output list will not be exhaustive as it is currently based on a
mapping we define, so any apps not included in that mapping will not be
output here.
Args:
env: The AndroidEnv interface.
timeout_sec: A timeout to use for this operation. If not set the default
timeout will be used.
Returns:
A list of app names.
"""
packages = get_all_package_names(env, timeout_sec)
package_to_app = {
v.split('/')[0]: k.split('|')[0] for k, v in _PATTERN_TO_ACTIVITY.items()
}

app_names = []
for package in response.package_manager.list.items:
for package in packages:
if package in package_to_app:
app_names.append(package_to_app[package])

Expand Down

0 comments on commit 5fc92c3

Please sign in to comment.