# -*- coding: utf-8 -*-
"""Deeper scan: UserSearchCallingCodeActivity methods / Compose / launch."""
import re
import zipfile

APK = r"C:\Users\Administrator\Desktop\notiMessage\reverse\dumps\tng_base_scan.apk"

def strings_near(data, needle, radius=200, limit=15):
    out = []
    start = 0
    while len(out) < limit:
        idx = data.find(needle, start)
        if idx < 0:
            break
        s = max(0, idx - radius)
        e = min(len(data), idx + len(needle) + radius)
        chunk = re.sub(rb"[^\x20-\x7e]+", b".", data[s:e])
        out.append(chunk.decode("ascii", "ignore"))
        start = idx + 1
    return out

def main():
    z = zipfile.ZipFile(APK)
    # Collect interesting strings from classes that have CallingCode
    keys = [
        b"CallingListScreen",
        b"setContent",
        b"ComposeView",
        b"AbstractComposeView",
        b"ComponentActivity",
        b"getCallingCodeList",
        b"CountryListRepository",
        b"startActivity",
        b"UserSearchCallingCodeActivity",
        b"ll_country",
        b"hardwareAccelerated",
        b"RecyclerView",
        b"LazyColumn",
        b"androidx/compose",
    ]
    for dex in sorted(n for n in z.namelist() if n.endswith(".dex")):
        data = z.read(dex)
        if b"UserSearchCallingCodeActivity" not in data and b"CallingListScreen" not in data and b"ll_country" not in data:
            continue
        print("\n========", dex, "========")
        for k in keys:
            if k in data:
                print("HAS", k.decode())
        if b"CallingListScreen" in data:
            print("--- CallingListScreen ctx ---")
            for c in strings_near(data, b"CallingListScreen", 120, 6):
                print(" ", c[:240])
        if b"ll_country" in data:
            print("--- ll_country ctx ---")
            for c in strings_near(data, b"ll_country", 100, 8):
                print(" ", c[:240])

    # Who references UserSearchCallingCodeActivity (launchers)
    print("\n=== who references UserSearchCallingCodeActivity class desc ===")
    desc = b"Lmy/com/tngdigital/user/view/UserSearchCallingCodeActivity;"
    for dex in sorted(n for n in z.namelist() if n.endswith(".dex")):
        data = z.read(dex)
        count = data.count(desc)
        if count:
            print(dex, "count=", count)

if __name__ == "__main__":
    main()
