Streak widget from 1x1 to 4x4 with an animated flame (orange while lit, grey when not), a streak keeper that writes the chosen message once a day to the chosen chats (off until switched on), avatar shapes, widgets and the keeper kept per account, Material 3 tonal surfaces instead of black (AMOLED keeps pure black), launcher previews of our own, and the mod runtime in a dex of its own. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
174 lines
9.4 KiB
Python
174 lines
9.4 KiB
Python
import unittest
|
|
import zipfile
|
|
from pathlib import Path
|
|
|
|
from lihont import manifest as manifest_module
|
|
from lihont import widget_patch
|
|
from lihont.axml import Axml, RES_XML_END_ELEMENT, RES_XML_START_ELEMENT
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
APKS = sorted((ROOT / "OriginalAPKtiktok").glob("*.apk")) if (ROOT / "OriginalAPKtiktok").is_dir() else []
|
|
|
|
|
|
class FakeApk:
|
|
"""Just enough of lihont.apkzip.Apk: reads from the original apk, remembers what is replaced."""
|
|
|
|
def __init__(self, path):
|
|
self.zip = zipfile.ZipFile(path)
|
|
self.replaced = {}
|
|
|
|
def read(self, name):
|
|
return self.replaced.get(name) or self.zip.read(name)
|
|
|
|
def replace(self, name, data):
|
|
self.replaced[name] = data
|
|
|
|
|
|
def provider_node(axml):
|
|
return next(n for n in axml.nodes
|
|
if n.kind == RES_XML_START_ELEMENT and axml.pool.get(n.name) == "appwidget-provider")
|
|
|
|
|
|
@unittest.skipUnless(APKS, "the original TikTok apk is not in OriginalAPKtiktok/")
|
|
class WidgetPatchTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.apk = FakeApk(APKS[0])
|
|
self.xml = Axml.parse(self.apk.read("AndroidManifest.xml"))
|
|
|
|
def receivers(self, xml):
|
|
return {xml.attr_string(n, "name"): n for n in xml.elements("receiver")}
|
|
|
|
def test_four_widgets_become_ours_with_one_name(self):
|
|
report = widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
rebuilt = Axml.parse(self.apk.replaced["AndroidManifest.xml"])
|
|
receivers = self.receivers(rebuilt)
|
|
for size in widget_patch.SIZES:
|
|
self.assertIn(size.widget, receivers)
|
|
self.assertNotIn(size.original, receivers)
|
|
self.assertEqual(widget_patch.LABEL, rebuilt.attr_string(receivers[size.widget], "label"))
|
|
self.assertEqual({"1x1", "2x2", "3x3", "4x4"}, set(report["receivers"]))
|
|
|
|
def test_the_configuration_screen_is_declared_and_exported(self):
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
rebuilt = Axml.parse(self.apk.replaced["AndroidManifest.xml"])
|
|
self.assertTrue(manifest_module.has_activity(rebuilt, widget_patch.CONFIG_CLASS))
|
|
node = next(n for n in rebuilt.elements("activity")
|
|
if rebuilt.attr_string(n, "name") == widget_patch.CONFIG_CLASS)
|
|
self.assertNotEqual(0, rebuilt.attr(node, "exported").data)
|
|
actions = [rebuilt.attr_string(n, "name") for n in rebuilt.elements("action")]
|
|
self.assertIn(widget_patch.CONFIGURE_ACTION, actions)
|
|
|
|
def test_every_provider_has_its_size_its_layout_and_can_be_resized(self):
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
for size in widget_patch.SIZES:
|
|
provider = Axml.parse(self.apk.replaced[size.provider])
|
|
node = provider_node(provider)
|
|
self.assertEqual(widget_patch.CONFIG_CLASS, provider.attr_string(node, "configure"))
|
|
self.assertEqual(size.cells, provider.attr(node, "targetCellWidth").data)
|
|
self.assertEqual(size.cells, provider.attr(node, "targetCellHeight").data)
|
|
self.assertEqual(widget_patch.dp(widget_patch.cell_dp(size.cells)), provider.attr(node, "minWidth").data)
|
|
self.assertEqual(widget_patch.LAYOUT, provider.attr(node, "initialLayout").data)
|
|
self.assertEqual(3, provider.attr(node, "resizeMode").data)
|
|
self.assertEqual(widget_patch.dp(40), provider.attr(node, "minResizeWidth").data)
|
|
self.assertIsNone(provider.attr(node, "description"))
|
|
self.assertIsNone(provider.attr(node, "previewLayout"))
|
|
|
|
def test_provider_attributes_stay_in_ascending_id_order(self):
|
|
# Android reads the attributes side by side with its own sorted list and skips what is out of
|
|
# order; with widgetCategory ahead of resizeMode the launcher saw a widget that could not be resized
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
for size in widget_patch.SIZES:
|
|
provider = Axml.parse(self.apk.replaced[size.provider])
|
|
node = provider_node(provider)
|
|
ids = [provider.resource_map[a.name] for a in node.attributes]
|
|
self.assertEqual(sorted(ids), ids, size.provider)
|
|
self.assertEqual(len(ids), len(set(ids)), "an attribute is written twice in " + size.provider)
|
|
|
|
def test_the_layout_fills_the_widget_instead_of_a_fixed_square(self):
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
layout = Axml.parse(self.apk.replaced[widget_patch.LAYOUT_FILE])
|
|
elements = [n for n in layout.nodes if n.kind == RES_XML_START_ELEMENT]
|
|
picture = next(n for n in elements if layout.pool.get(n.name) == "ImageView")
|
|
for node in (elements[0], picture):
|
|
for resource_id in (widget_patch.ATTR_LAYOUT_WIDTH, widget_patch.ATTR_LAYOUT_HEIGHT):
|
|
attr = widget_patch._attr_by_id(layout, node, resource_id)
|
|
self.assertEqual(widget_patch.MATCH_PARENT, attr.data)
|
|
# the picture view of TikTok's layout is still the one the widget draws into
|
|
self.assertEqual(widget_patch.PICTURE,
|
|
widget_patch._attr_by_id(layout, picture, widget_patch.ATTR_ID).data)
|
|
|
|
def test_the_layout_flips_between_four_pictures_by_itself(self):
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
layout = Axml.parse(self.apk.replaced[widget_patch.LAYOUT_FILE])
|
|
starts = [n for n in layout.nodes if n.kind == RES_XML_START_ELEMENT]
|
|
ends = [n for n in layout.nodes if n.kind == RES_XML_END_ELEMENT]
|
|
self.assertEqual(len(starts), len(ends), "every element is closed")
|
|
names = [layout.pool.get(n.name) for n in starts]
|
|
self.assertEqual(["RelativeLayout", "ViewFlipper", "ImageView", "ImageView", "ImageView", "ImageView"], names[:6])
|
|
|
|
flipper = starts[1]
|
|
flipper_ids = [layout.resource_map[a.name] for a in flipper.attributes]
|
|
self.assertEqual(sorted(flipper_ids), flipper_ids, "the flipper's attributes are in ascending id order")
|
|
by_id = {layout.resource_map[a.name]: a for a in flipper.attributes}
|
|
self.assertEqual(widget_patch.FLIPPER_VIEW, by_id[widget_patch.ATTR_ID].data)
|
|
self.assertEqual(0xFFFFFFFF, by_id[0x010102B5].data, "it starts flipping on its own")
|
|
self.assertEqual(widget_patch.FLIP_INTERVAL_MS, by_id[0x01010179].data)
|
|
self.assertEqual(widget_patch.ANIM_FADE_IN, by_id[0x01010177].data)
|
|
self.assertEqual(widget_patch.ANIM_FADE_OUT, by_id[0x01010178].data)
|
|
|
|
# the old picture view is the first frame; the three new ones copy it with ids of their own
|
|
frames = starts[2:6]
|
|
got = [widget_patch._attr_by_id(layout, f, widget_patch.ATTR_ID).data for f in frames]
|
|
self.assertEqual([widget_patch.PICTURE] + list(widget_patch.FRAME_VIEWS), got)
|
|
sizes = {tuple(widget_patch._attr_by_id(layout, f, rid).data
|
|
for rid in (widget_patch.ATTR_LAYOUT_WIDTH, widget_patch.ATTR_LAYOUT_HEIGHT)) for f in frames}
|
|
self.assertEqual({(widget_patch.MATCH_PARENT, widget_patch.MATCH_PARENT)}, sizes)
|
|
# the hidden badge and label views of TikTok's layout are still there
|
|
self.assertIn("FrameLayout", names)
|
|
self.assertIn("TextView", names)
|
|
|
|
def test_the_java_side_uses_the_same_view_ids(self):
|
|
source = (ROOT / "src" / "lihont" / "two" / "widget" / "StreakWidget.java").read_text(encoding="utf-8")
|
|
import re
|
|
picture = int(re.search(r"PICTURE\s*=\s*(0x[0-9A-Fa-f]+)", source).group(1), 16)
|
|
frames = re.search(r"FRAME_VIEWS\s*=\s*\{([^}]*)\}", source).group(1)
|
|
self.assertEqual(widget_patch.PICTURE, picture)
|
|
self.assertEqual(list(widget_patch.FRAME_VIEWS), [int(h, 16) for h in re.findall(r"0x[0-9A-Fa-f]+", frames)])
|
|
|
|
def test_the_widget_list_shows_our_flower_instead_of_tiktoks_pictures(self):
|
|
report = widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
picture = widget_patch.preview_asset().read_bytes()
|
|
self.assertTrue(picture.startswith(b"\x89PNG"))
|
|
# the pictures of 47.1.3's own widgets: a nine-patch, a webp and a png
|
|
self.assertEqual(["res/c/i2.9.png", "res/c/i6.png", "res/c/i9.webp"], report["previews"])
|
|
for path in report["previews"]:
|
|
self.assertEqual(picture, self.apk.replaced[path])
|
|
|
|
def test_a_layout_that_is_not_the_expected_one_is_refused(self):
|
|
layout = Axml.parse(self.apk.read("res/b/pr.xml"))
|
|
for node in layout.nodes:
|
|
if node.kind == RES_XML_START_ELEMENT and layout.pool.get(node.name) == "ImageView":
|
|
node.name = layout.pool.index("TextView")
|
|
with self.assertRaises(widget_patch.WidgetError):
|
|
widget_patch.fill_layout(layout)
|
|
|
|
def test_a_second_pass_is_refused(self):
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
with self.assertRaises(widget_patch.WidgetError):
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
|
|
def test_a_manifest_without_the_widget_is_refused(self):
|
|
for node in self.xml.elements("receiver"):
|
|
if self.xml.attr_string(node, "name") == widget_patch.SIZES[0].original:
|
|
self.xml.set_attr_string(node, "name", "x.Other")
|
|
with self.assertRaises(widget_patch.WidgetError):
|
|
widget_patch.apply(self.apk, self.xml, 0x7F120364)
|
|
|
|
def test_cells_are_sized_the_way_launchers_count_them(self):
|
|
self.assertEqual([40, 110, 180, 250], [widget_patch.cell_dp(n) for n in (1, 2, 3, 4)])
|
|
self.assertEqual(0x6E01, widget_patch.dp(110))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|