# -*- coding: utf-8 -*-
import frida
import subprocess
import time

PKG = "ph.seabank.seabank"
ADB = r"C:\Users\Administrator\AppData\Local\Android\Sdk\platform-tools\adb.exe"
subprocess.run([ADB, "shell", "monkey", "-p", PKG, "-c", "android.intent.category.LAUNCHER", "1"], capture_output=True)

d = frida.get_usb_device(10)
pid = None
for a in d.enumerate_applications():
    if a.identifier == PKG and a.pid > 0:
        pid = a.pid
        print("pid", pid, a.name)
        break
if not pid:
    raise SystemExit("no pid")

s = d.attach(pid)
JS = r"""
var n = 0;
function waitJava() {
  if (typeof Java !== 'undefined' && Java.available) {
    send({event: 'java_ready', n: n});
    Java.perform(function () {
      send({event: 'perform_ok'});
    });
    return;
  }
  n++;
  if (n % 10 === 0) send({event: 'waiting', n: n});
  if (n < 120) setTimeout(waitJava, 500);
  else send({event: 'timeout', n: n});
}
setImmediate(waitJava);
"""

def on_m(msg, data):
    print(msg)

sc = s.create_script(JS)
sc.on("message", on_m)
sc.load()
time.sleep(70)
