Repository navigation
feat(device-utils): add StoreKit 2 installer lookup and installation time APIs #332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+604
−5
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
166c694
feat(device-utils): add StoreKit 2 installer lookup and installation …
huhuanming cf67eec
fix(device-utils): address install info review feedback
huhuanming 62aace6
docs(device-utils): make Android install info smoke test reproducible
huhuanming 6ec829f
fix(device-utils): address privacy and Android user review feedback
huhuanming 3244058
docs(device-utils): make secondary-user smoke check deterministic
huhuanming 2c418e5
Merge origin/main into feat/device-utils-install-info
huhuanming File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| # Android DeviceUtils install info smoke test | ||
|
|
||
| Generate the workspace's native bindings, then build the example's release APK | ||
| with the smoke test as its JavaScript entry point. It calls the exported | ||
| DeviceUtils Nitro APIs in the example's React Native runtime. The example | ||
| also starts a background runtime, so its release APK needs `background.bundle`. | ||
|
|
||
| ```sh | ||
| yarn install --immutable | ||
| find native-modules native-views -mindepth 2 -maxdepth 2 -name nitro.json -print0 \ | ||
| | xargs -0 -n1 -P4 sh -c 'yarn --cwd "$(dirname "$1")" nitrogen' _ | ||
| yarn workspace @onekeyfe/react-native-device-utils prepare | ||
| cd example/react-native | ||
| mkdir -p android/app/src/main/assets | ||
| yarn exec react-native bundle --platform android --dev false --entry-file background.js \ | ||
| --bundle-output android/app/src/main/assets/background.bundle | ||
| cd android | ||
| ENTRY_FILE=smoke/device-utils-install-info.js \ | ||
| ./gradlew :app:assembleRelease -PreactNativeArchitectures=arm64-v8a | ||
| ``` | ||
|
|
||
| The command builds for ARM64. Set `reactNativeArchitectures` to `x86_64` | ||
| for an x86-64 emulator. On each unlocked emulator, install the APK and run | ||
| the check. Set `ANDROID_SERIAL` to select the emulator when multiple devices | ||
| are attached. Use API 29 and API 33 or newer to cover the legacy and modern | ||
| PackageManager paths. | ||
|
|
||
| ```sh | ||
| adb install -r -i com.android.shell app/build/outputs/apk/release/app-release.apk | ||
| python3 ../smoke/check-device-utils-install-info.py | ||
| ``` | ||
|
|
||
| The explicit installer exercises the `other` enum. A fresh install with no | ||
| installer recorded exercises `unknown`. The check compares the JavaScript | ||
| installer enum and epoch-millisecond timestamp with PackageManager, whose | ||
| dump reports the install time to whole seconds. The script launches and | ||
| queries the app as the foreground user. | ||
|
|
||
| ## Secondary-user regression check | ||
|
|
||
| Use an API 34 emulator with `com.example` absent for all users. Create a | ||
| secondary user or use an existing secondary user on that clean emulator. | ||
| Run the commands from `example/react-native/android` and set `secondary_user` | ||
| to the ID shown by `adb shell pm list users`. | ||
|
|
||
| ```sh | ||
| secondary_user=10 | ||
| adb install --user 0 -i com.android.shell app/build/outputs/apk/release/app-release.apk | ||
| sleep 2 | ||
| adb shell pm install-existing --user "$secondary_user" com.example | ||
| adb shell dumpsys package com.example | ||
| ``` | ||
|
|
||
| Confirm that the `firstInstallTime` under `User 0` differs from the value | ||
| under the secondary user, then switch users. | ||
|
|
||
| ```sh | ||
| adb shell am switch-user "$secondary_user" | ||
| ``` | ||
|
|
||
| Unlock the emulator, wait for the secondary user's home screen to appear, | ||
| and run the check. | ||
|
|
||
| ```sh | ||
| python3 ../smoke/check-device-utils-install-info.py | ||
| ``` |
102 changes: 102 additions & 0 deletions
102
example/react-native/smoke/check-device-utils-install-info.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import re | ||
| import subprocess | ||
| import time | ||
| import xml.etree.ElementTree as ET | ||
|
|
||
|
|
||
| PACKAGE = "com.example" | ||
| MARKER = "DeviceUtilsInstallInfo: " | ||
| DUMP_PATH = "/data/local/tmp/device-utils-install-info.xml" | ||
| INSTALL_TIME = re.compile( | ||
| r"^[ ]*firstInstallTime=(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})$", | ||
| re.MULTILINE, | ||
| ) | ||
|
|
||
|
|
||
| def shell(*args): | ||
| return subprocess.check_output(["adb", "shell", *args], text=True, timeout=30) | ||
|
|
||
|
|
||
| def install_time_for_user(package_dump, user_id): | ||
| package_fields, *user_fields = re.split( | ||
| r"^[ \t]+User (\d+):[^\n]*\n", package_dump, flags=re.MULTILINE | ||
| ) | ||
| user_times = { | ||
| user: match.group(1) | ||
| for user, fields in zip(user_fields[::2], user_fields[1::2]) | ||
| if (match := INSTALL_TIME.search(fields)) | ||
| } | ||
| if user_times: | ||
| return user_times.get(user_id) | ||
| legacy_time = INSTALL_TIME.search(package_fields) | ||
| return legacy_time.group(1) if legacy_time else None | ||
|
|
||
|
|
||
| def bridge_result(user_id): | ||
| shell("am", "start", "--user", user_id, "-S", "-W", "-n", f"{PACKAGE}/.MainActivity") | ||
| deadline = time.monotonic() + 45 | ||
| while time.monotonic() < deadline: | ||
| shell("rm", "-f", DUMP_PATH) | ||
| if DUMP_PATH in shell("uiautomator", "dump", DUMP_PATH): | ||
| xml = shell("cat", DUMP_PATH) | ||
| shell("rm", "-f", DUMP_PATH) | ||
| for node in ET.fromstring(xml).iter("node"): | ||
| if node.get("package") != PACKAGE: | ||
| continue | ||
| label = node.get("content-desc") or node.get("text", "") | ||
| if label.startswith(MARKER): | ||
| result = label.removeprefix(MARKER) | ||
| if result != "pending": | ||
| return result | ||
| time.sleep(1) | ||
| raise SystemExit("No DeviceUtils result appeared in the example app") | ||
|
|
||
|
|
||
| def main(): | ||
| user_id = shell("am", "get-current-user").strip() | ||
| if not re.fullmatch(r"[0-9]+", user_id): | ||
| raise SystemExit(f"Could not identify the foreground Android user: {user_id!r}") | ||
|
|
||
| packages = shell( | ||
| "cmd", "package", "list", "packages", "--user", user_id, "-i", PACKAGE | ||
| ) | ||
| installer = re.search( | ||
| rf"^package:{re.escape(PACKAGE)}[ \t]+installer=([^\r\n]*)$", | ||
| packages, | ||
| re.MULTILINE, | ||
| ) | ||
| package_dump = shell("dumpsys", "package", PACKAGE) | ||
| installed = install_time_for_user(package_dump, user_id) | ||
| if not installer or not installed: | ||
| raise SystemExit( | ||
| f"PackageManager did not report install info for {PACKAGE} in user {user_id}" | ||
| ) | ||
|
|
||
| installer_package = installer.group(1).strip() | ||
| expected_installer = { | ||
| "com.android.vending": "playStore", | ||
| "com.huawei.appmarket": "huaweiAppGallery", | ||
| "null": "unknown", | ||
| "": "unknown", | ||
| }.get(installer_package, "other") | ||
| # dumpsys renders firstInstallTime in the device's local time, to whole seconds. | ||
| expected_seconds = int(shell("date", "-d", f"'{installed}'", "+%s")) | ||
| result = bridge_result(user_id) | ||
| if shell("am", "get-current-user").strip() != user_id: | ||
| raise SystemExit("The foreground Android user changed during the smoke test") | ||
| value = re.fullmatch(r"(\w+) ([1-9][0-9]*)", result) | ||
| if ( | ||
| not value | ||
| or value.group(1) != expected_installer | ||
| or int(value.group(2)) // 1000 != expected_seconds | ||
| ): | ||
| raise SystemExit( | ||
| f"Bridge returned {result!r}, expected {expected_installer} " | ||
| f"at {expected_seconds} epoch seconds" | ||
| ) | ||
| sdk = shell("getprop", "ro.build.version.sdk").strip() | ||
| print(f"API {sdk}, user {user_id}: bridge returned {result}, matching PackageManager") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import React, { useEffect, useState } from 'react'; | ||
| import { AppRegistry, Text } from 'react-native'; | ||
| import { ReactNativeDeviceUtils } from '@onekeyfe/react-native-device-utils'; | ||
| import { name as appName } from '../app.json'; | ||
|
|
||
| function InstallInfoSmokeTest() { | ||
| const [result, setResult] = useState('pending'); | ||
|
|
||
| useEffect(() => { | ||
| async function run() { | ||
| try { | ||
| const [installer, time] = await Promise.all([ | ||
| ReactNativeDeviceUtils.getInstallerPackageNameAsync(), | ||
| ReactNativeDeviceUtils.getInstallationTime(), | ||
| ]); | ||
| if ( | ||
| typeof installer !== 'string' || | ||
| !Number.isSafeInteger(time) || | ||
| time <= 0 | ||
| ) { | ||
| throw new Error( | ||
| 'Expected a string installer and a positive integer timestamp', | ||
| ); | ||
| } | ||
| setResult(`${installer} ${time}`); | ||
| } catch (error) { | ||
| setResult(`failed: ${String(error)}`); | ||
| } | ||
| } | ||
| void run(); | ||
| }, []); | ||
|
|
||
| const marker = `DeviceUtilsInstallInfo: ${result}`; | ||
| return ( | ||
| <Text accessibilityLabel={marker} style={{ margin: 48 }}> | ||
| {marker} | ||
| </Text> | ||
| ); | ||
| } | ||
|
|
||
| AppRegistry.registerComponent(appName, () => InstallInfoSmokeTest); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.