# -*- coding: utf-8 -*-
"""Scan TNG APK for CallingCode / country UI / HW-related classes."""
import re
import sys
import zipfile

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

NEEDLES = [
    b"UserSearchCallingCodeActivity",
    b"CallingCode",
    b"ll_country",
    b"BottomSelect",
    b"BottomSelectDialogFragment",
    b"ftv_title",
    b"i7.l",
    b"enableHardwareAcceleration",
    b"FLAG_HARDWARE_ACCELERATED",
]


def main():
    z = zipfile.ZipFile(APK)
    print("=== DEX STRING HITS ===")
    for name in sorted(z.namelist()):
        if not name.endswith(".dex"):
            continue
        data = z.read(name)
        hits = [s.decode() for s in NEEDLES if s in data]
        if hits:
            print("%s -> %s" % (name, hits))

    print("\n=== CLASS NAMES (CallingCode / BottomSelect / country) ===")
    pat = re.compile(
        rb"L[a-zA-Z0-9_$/]*(?:CallingCode|BottomSelect|Country|country)[a-zA-Z0-9_$/]*;"
    )
    found = set()
    for name in sorted(z.namelist()):
        if not name.endswith(".dex"):
            continue
        data = z.read(name)
        for m in pat.findall(data):
            found.add(m.decode("ascii", "ignore")[1:-1].replace("/", "."))
    for c in sorted(found):
        print(" ", c)

    print("\n=== CONTEXT AROUND UserSearchCallingCodeActivity ===")
    target = b"UserSearchCallingCodeActivity"
    for name in sorted(z.namelist()):
        if not name.endswith(".dex"):
            continue
        data = z.read(name)
        start = 0
        n = 0
        while True:
            idx = data.find(target, start)
            if idx < 0:
                break
            s = max(0, idx - 80)
            e = min(len(data), idx + len(target) + 120)
            chunk = re.sub(rb"[^\x20-\x7e]+", b".", data[s:e])
            print("[%s @%d] %s" % (name, idx, chunk.decode("ascii", "ignore")))
            start = idx + 1
            n += 1
            if n >= 8:
                break

    # Manifest component
    print("\n=== ANDROIDMANIFEST snippets ===")
    try:
        # binary manifest — just search utf16/utf8 remnants in apk
        data = z.read("AndroidManifest.xml")
        for key in (b"CallingCode", b"hardwareAccelerated", b"user.view"):
            if key in data or key.decode().encode("utf-16le") in data:
                print(" manifest contains", key)
    except KeyError:
        pass


if __name__ == "__main__":
    main()
