diff -r 89116bd505cb Lib/platform.py --- a/Lib/platform.py Wed May 04 12:00:32 2016 -0400 +++ b/Lib/platform.py Thu May 05 03:03:06 2016 +0800 @@ -690,6 +690,52 @@ return release, vendor, vminfo, osinfo +def _android_getprop(prop, default): + # On Android 4.1, `getprop` relies on an inherited file descriptor to + # gain access to the system property area. The id of this file descriptor + # is stored in the environment variable ANDROID_PROPERTY_WORKSPACE + pass_fds = () + android_property_workspace = os.getenv('ANDROID_PROPERTY_WORKSPACE', '') + if ',' in android_property_workspace: + try: + pass_fds = (int(android_property_workspace.split(',')[0]),) + except ValueError: + pass + + try: + return subprocess.check_output(['getprop', prop], + pass_fds=pass_fds).decode('utf-8').strip() + except OSError: + return default + +def android_ver(version='', sdk=0, versioninfo=('', ''), hardwareinfo=('', '')): + """ Attempt to get the Android version information. + + The function checks Android system properties to retrieve info. + + Returns a (version, sdk, versioninfo, hardwareinfo) tuple with + versioninfo being a (codename, incremental) tuple and hardwareinfo + being a (boardname, abi) tuple. + + Values which cannot be determined are set to the defaults + given as parameters (which sdk defaults to 0 and others default to ''). + """ + + version = _android_getprop('ro.build.version.release', version) + sdk = int(_android_getprop('ro.build.version.sdk', str(sdk))) + + codename, incremental = versioninfo + codename = _android_getprop('ro.build.version.codename', codename) + incremental = _android_getprop('ro.build.version.incremental', incremental) + versioninfo = codename, incremental + + boardname, abi = hardwareinfo + boardname = _android_getprop('ro.product.board', boardname) + abi = _android_getprop('ro.product.cpu.abi', abi) + hardwareinfo = boardname, abi + + return version, sdk, versioninfo, hardwareinfo + ### System name aliasing def system_alias(system, release, version):