chore(tools): device-QA DataStore config injector for the proxy app
This commit is contained in:
parent
4e419146ca
commit
1e4796be2b
1 changed files with 38 additions and 0 deletions
38
tools/mk_datastore.py
Normal file
38
tools/mk_datastore.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Build a Preferences-DataStore protobuf for the vojo proxy app from a ProxyState
|
||||
JSON on stdin; emit it base64-encoded on stdout (pipe through `base64 -d` via run-as).
|
||||
|
||||
Wire layout (androidx.datastore.preferences):
|
||||
PreferenceMap.preferences = field 1 (map) -> entry{ key=field1 string, value=field2 Value }
|
||||
Value.string = field 5
|
||||
"""
|
||||
import sys
|
||||
import base64
|
||||
|
||||
|
||||
def varint(n):
|
||||
out = bytearray()
|
||||
while True:
|
||||
b = n & 0x7F
|
||||
n >>= 7
|
||||
if n:
|
||||
out.append(b | 0x80)
|
||||
else:
|
||||
out.append(b)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def ld(field, data): # length-delimited field: tag=(field<<3)|2
|
||||
return bytes([(field << 3) | 2]) + varint(len(data)) + data
|
||||
|
||||
|
||||
def main():
|
||||
state_json = sys.stdin.read().strip().encode("utf-8")
|
||||
value_msg = ld(5, state_json) # Value.string
|
||||
entry = ld(1, b"state") + ld(2, value_msg) # entry: key + Value
|
||||
pref_map = ld(1, entry) # PreferenceMap.preferences
|
||||
sys.stdout.write(base64.b64encode(pref_map).decode())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Reference in a new issue