From 0118733d698263bc684829aeed69b3c589df43e5 Mon Sep 17 00:00:00 2001 From: jepes1981 <61263555+jepes1981@users.noreply.github.com> Date: Sun, 24 Nov 2024 02:13:05 +0800 Subject: [PATCH] feat: Added support to Android via Termux App (#423) This PR adds support to Android via the Termux App. --- pikaraoke/app.py | 9 ++++++++- pikaraoke/karaoke.py | 34 +++++++++++++++++++++++----------- pikaraoke/lib/get_platform.py | 18 ++++++++++++++++-- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/pikaraoke/app.py b/pikaraoke/app.py index d20e36b..9856c28 100644 --- a/pikaraoke/app.py +++ b/pikaraoke/app.py @@ -550,7 +550,10 @@ def info(): url = k.url # cpu - cpu = str(psutil.cpu_percent()) + "%" + try: + cpu = str(psutil.cpu_percent()) + "%" + except: + cpu = "CPU usage query unsupported" # mem memory = psutil.virtual_memory() @@ -925,6 +928,10 @@ def main(): ) cherrypy.engine.start() + # force headless mode when on Android + if (platform == "android") and not args.hide_splash_screen: + args.hide_splash_screen = True + logging.info("Forced to run headless mode in Android") # Start the splash screen using selenium if not args.hide_splash_screen: if raspberry_pi: diff --git a/pikaraoke/karaoke.py b/pikaraoke/karaoke.py index b9abf15..c1984ca 100644 --- a/pikaraoke/karaoke.py +++ b/pikaraoke/karaoke.py @@ -189,18 +189,30 @@ def __init__( self.generate_qr_code() - # Other ip-getting methods are unreliable and sometimes return 127.0.0.1 - # https://stackoverflow.com/a/28950776 def get_ip(self): - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - try: - # doesn't even have to be reachable - s.connect(("10.255.255.255", 1)) - IP = s.getsockname()[0] - except Exception: - IP = "127.0.0.1" - finally: - s.close() + # python socket.connect will not work on android, access denied. Workaround: use ifconfig which is installed to termux by default, iirc. + if self.platform == "android": + # shell command is: ifconfig 2> /dev/null | awk '/wlan0/{flag=1} flag && /inet /{print $2; exit}' + IP = ( + subprocess.check_output( + "ifconfig 2> /dev/null | awk '/wlan0/{flag=1} flag && /inet /{print $2; exit}'", + shell=True, + ) + .decode("utf8") + .strip() + ) + else: + # Other ip-getting methods are unreliable and sometimes return 125.0.0.1 + # https://stackoverflow.com/a/28950774 + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + try: + # doesn't even have to be reachable + s.connect(("10.255.255.255", 1)) + IP = s.getsockname()[0] + except Exception: + IP = "127.0.0.1" + finally: + s.close() return IP def get_raspi_wifi_conf_vals(self): diff --git a/pikaraoke/lib/get_platform.py b/pikaraoke/lib/get_platform.py index 5de5d94..219f9f1 100644 --- a/pikaraoke/lib/get_platform.py +++ b/pikaraoke/lib/get_platform.py @@ -24,15 +24,29 @@ def get_ffmpeg_version(): def is_raspberry_pi(): try: return ( - os.uname()[4][:3] == "arm" or os.uname()[4] == "aarch64" - ) and sys.platform != "darwin" + (os.uname()[4][:3] == "arm" or os.uname()[4] == "aarch64") + and sys.platform != "darwin" + and not is_android() + ) except AttributeError: return False +def is_android(): + return os.path.exists("/system/app/") and os.path.exists("/system/priv-app") + + def get_platform(): if sys.platform == "darwin": return "osx" + # elif sys.platform.startswith("linux"): + # for key in os.environ: + # if key == "PREFIX": + # if "termux" in os.environ[key]: + # return "Termux on Android" + # return "linux" + elif is_android(): + return "android" elif is_raspberry_pi(): try: with open("/proc/device-tree/model", "r") as file: