android.py (3011B)
1 #!/data/data/com.termux/files/usr/bin/python 2 3 import eculocate 4 import termuxgui as tg 5 import threading 6 import time 7 import sys 8 from nacl.signing import SigningKey 9 from nacl.encoding import RawEncoder 10 11 hosts = {} 12 13 def add_host(activity, name, parent): 14 button = tg.Button(activity, "?", parent) 15 button.setlinearlayoutparams(0) 16 button.setheight(tg.View.WRAP_CONTENT) 17 hosts[name] = button 18 return button 19 20 with open("privkey.bin", "rb") as f: 21 signing_key = SigningKey(f.read(), encoder=RawEncoder) 22 23 with tg.Connection() as c: 24 a = tg.Activity(c) 25 26 vertical = tg.LinearLayout(a) 27 instances = tg.LinearLayout(a, vertical) 28 29 tg.Space(a, vertical) 30 31 status = tg.TextView(a, "initializing", vertical) 32 33 tg.Space(a, vertical) 34 35 buttons = tg.LinearLayout(a, vertical, False) 36 37 start_button = tg.Button(a, "(start)", buttons) 38 start_button.setlinearlayoutparams(0) 39 start_button.setheight(tg.View.WRAP_CONTENT) 40 # start_button.setwidth(tg.View.WRAP_CONTENT) 41 start_button.setgravity(1, 1) 42 43 def repaint(client): 44 if client.selected_service_name == None: 45 start_button.settext("----") 46 else: 47 start_button.settext("STOP" if client.started else "START") 48 49 class GuiClient(eculocate.Client): 50 def on_services_changed(self): 51 for name, service in self.host_addresses.items(): 52 widget = hosts.get(name, None) 53 if widget == None: 54 widget = add_host(a, name, instances) 55 if name == self.selected_service_name: 56 label = f"* {name}" 57 else: 58 label = name 59 widget.settext(label) 60 print("hosts") 61 print(hosts) 62 repaint(self) 63 64 def fake_host_add(self): 65 self.host_addresses = { 66 'Bike3._keihin._udp.local.': ('192.168.8.125', 5002) 67 } 68 self.on_services_changed() 69 70 def __init__(self, key): 71 super().__init__(key) 72 # threading.Thread(target = self.fake_host_add, daemon = True).start() 73 74 with open("privkey.bin", "rb") as f: 75 signing_key = SigningKey(f.read(), encoder=RawEncoder) 76 client = GuiClient(signing_key) 77 78 def update_byte_counter(): 79 while True: 80 label = f"recorded {client.bytes_recorded} bytes" if client.started else "not recording" 81 status.settext(label) 82 time.sleep(1) 83 84 threading.Thread(target = update_byte_counter, daemon = True).start() 85 86 for ev in c.events(): # waits for events from the gui 87 if ev.type == tg.Event.destroy: 88 sys.exit() 89 if ev.type == tg.Event.click: 90 for k, v in hosts.items(): 91 if v == ev.value["id"]: 92 client.select_service_name(k) 93 if ev.value["id"] == start_button: 94 client.stop_recording() if client.started else client.start_recording() 95 repaint(client)