Sync changes from internal dev branch.#91
Conversation
PiperOrigin-RevId: 899668942
PiperOrigin-RevId: 901467957
PiperOrigin-RevId: 903887631
PiperOrigin-RevId: 903942917
PiperOrigin-RevId: 903974744
PiperOrigin-RevId: 903994153
PiperOrigin-RevId: 904009306
PiperOrigin-RevId: 904079133
There was a problem hiding this comment.
Code Review
This pull request introduces two new utility functions, disable_package_verifiers and enable_package_verifiers, to manage package verification settings on rooted Android devices via ADB. It also includes a minor formatting update to an existing version extraction check. A review comment suggests refactoring the new functions to use a common private helper to eliminate code duplication and improve maintainability.
|
|
||
| def disable_package_verifiers(ad: android_device.AndroidDevice): | ||
| """Disables package verifier and Play Protect for ADB installs.""" | ||
| try: | ||
| if not ad.is_adb_root: | ||
| ad.log.info( | ||
| 'Device is not rooted. Skipping disabling package verifiers.' | ||
| ) | ||
| return | ||
| ad.log.info( | ||
| 'Device is in Root. Disabling package verifier and Play Protect for ADB' | ||
| ' installs.' | ||
| ) | ||
| ad.adb.shell(['settings', 'put', 'global', 'package_verifier_enable', '0']) | ||
| ad.adb.shell( | ||
| ['settings', 'put', 'global', 'verifier_verify_adb_installs', '0'] | ||
| ) | ||
| ad.adb.shell(['settings', 'put', 'global', 'verifier_engprod', '1']) | ||
| except adb.AdbError as e: | ||
| ad.log.error(f'Failed to disable package verifiers: {e} on ' | ||
| f'device {ad.serial}.') | ||
|
|
||
|
|
||
| def enable_package_verifiers(ad: android_device.AndroidDevice): | ||
| """Reverts package verifier and Play Protect settings.""" | ||
| try: | ||
| if not ad.is_adb_root: | ||
| ad.log.info( | ||
| 'Device is not rooted. Skipping reverting package verifiers.' | ||
| ) | ||
| return | ||
| ad.log.info( | ||
| 'Device is in Root. Reverting package verifier and Play Protect' | ||
| ' settings for ADB installs.' | ||
| ) | ||
| ad.adb.shell(['settings', 'put', 'global', 'package_verifier_enable', '1']) | ||
| ad.adb.shell( | ||
| ['settings', 'put', 'global', 'verifier_verify_adb_installs', '1'] | ||
| ) | ||
| ad.adb.shell(['settings', 'put', 'global', 'verifier_engprod', '0']) | ||
| except adb.AdbError as e: | ||
| ad.log.error(f'Failed to enable package verifiers: {e} on ' |
There was a problem hiding this comment.
The functions disable_package_verifiers and enable_package_verifiers share almost identical logic, including the root check, logging, and error handling. This duplication makes the code harder to maintain. Consider refactoring them to use a common private helper function that takes the desired state as a parameter.
def _set_package_verifiers(ad: android_device.AndroidDevice, enable: bool):
"""Sets package verifier and Play Protect settings."""
try:
if not ad.is_adb_root:
ad.log.info(
f'Device is not rooted. Skipping {"reverting" if enable else "disabling"} package verifiers.'
)
return
action = "Reverting" if enable else "Disabling"
ad.log.info(
f'Device is in Root. {action} package verifier and Play Protect'
' settings for ADB installs.'
)
val = '1' if enable else '0'
eng_val = '0' if enable else '1'
ad.adb.shell(['settings', 'put', 'global', 'package_verifier_enable', val])
ad.adb.shell(['settings', 'put', 'global', 'verifier_verify_adb_installs', val])
ad.adb.shell(['settings', 'put', 'global', 'verifier_engprod', eng_val])
except adb.AdbError as e:
ad.log.error(f'Failed to {action.lower()} package verifiers: {e} on device {ad.serial}.')
def disable_package_verifiers(ad: android_device.AndroidDevice):
"""Disables package verifier and Play Protect for ADB installs."""
_set_package_verifiers(ad, enable=False)
def enable_package_verifiers(ad: android_device.AndroidDevice):
"""Reverts package verifier and Play Protect settings."""
_set_package_verifiers(ad, enable=True)
No description provided.