From af09700a25cc1f83b201f7958d6537db4b9089f8 Mon Sep 17 00:00:00 2001 From: jepes1981 <61263555+jepes1981@users.noreply.github.com> Date: Fri, 22 Nov 2024 14:34:10 +0800 Subject: [PATCH 1/2] feat : Added support to Android via Termux App --- pikaraoke/app.py | 9 ++++++++- pikaraoke/karaoke.py | 34 +++++++++++++++++++++++----------- pikaraoke/lib/get_platform.py | 14 ++++++++++++-- scripts/README.md | 21 +++++++++++++++++++++ 4 files changed, 64 insertions(+), 14 deletions(-) diff --git a/pikaraoke/app.py b/pikaraoke/app.py index d20e36b7..d36629ed 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 = "Android 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 "Android" in platform 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 b9abf15e..47f3f02d 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 "Android" in self.platform: + # 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 5de5d94c..abe142f9 100644 --- a/pikaraoke/lib/get_platform.py +++ b/pikaraoke/lib/get_platform.py @@ -24,8 +24,10 @@ 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 (os.path.exists("/system/app/") and os.path.exists("/system/priv-app")) + ) except AttributeError: return False @@ -33,6 +35,14 @@ def is_raspberry_pi(): 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 os.path.exists("/system/app/") and os.path.exists("/system/priv-app"): + return "Android" elif is_raspberry_pi(): try: with open("/proc/device-tree/model", "r") as file: diff --git a/scripts/README.md b/scripts/README.md index f53e750e..3229a2b9 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -44,6 +44,27 @@ setup-windows.bat Windows firewall may initially block connections to port 5555 and 5556. Be sure to allow these. It should prompt the first time you run pikaraoke and launch a song. Otherwise, configure it manually in the security settings. +### Android (via Termux App) + +Install the Termux App via https://f-droid.org/en/packages/com.termux/ or from Google Play via https://play.google.com/store/apps/details?id=com.termux + +Open the Termux App (A terminal emulator similar to linux) + +Optional (but recommended): +Updated the packages using the command: + +``` +pkg update && pkg upgrade +``` + +Install python and ffmpeg: + +``` +pkg install python ffmpeg +``` + +from this point onwards, you only need to follow the installation for Raspberry pi / Linux / OSX (see above). + ## Launch cd to the pikaraoke directory and run: From 81c17ade326fc8b072aba26bcf8bd1b58fb373e8 Mon Sep 17 00:00:00 2001 From: jepes1981 <61263555+jepes1981@users.noreply.github.com> Date: Sat, 23 Nov 2024 12:49:27 +0800 Subject: [PATCH 2/2] Address review feedback: Revert README.md / Wrap android validation to a function / use lowecase convention for android / Generalize the error CPU usage query not supported / Use precise statement '==' vs 'in' --- pikaraoke/app.py | 4 ++-- pikaraoke/karaoke.py | 2 +- pikaraoke/lib/get_platform.py | 10 +++++++--- scripts/README.md | 21 --------------------- 4 files changed, 10 insertions(+), 27 deletions(-) diff --git a/pikaraoke/app.py b/pikaraoke/app.py index d36629ed..9856c289 100644 --- a/pikaraoke/app.py +++ b/pikaraoke/app.py @@ -553,7 +553,7 @@ def info(): try: cpu = str(psutil.cpu_percent()) + "%" except: - cpu = "Android CPU usage query unsupported." + cpu = "CPU usage query unsupported" # mem memory = psutil.virtual_memory() @@ -929,7 +929,7 @@ def main(): cherrypy.engine.start() # force headless mode when on Android - if "Android" in platform and not args.hide_splash_screen: + 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 diff --git a/pikaraoke/karaoke.py b/pikaraoke/karaoke.py index 47f3f02d..c1984cab 100644 --- a/pikaraoke/karaoke.py +++ b/pikaraoke/karaoke.py @@ -191,7 +191,7 @@ def __init__( def get_ip(self): # python socket.connect will not work on android, access denied. Workaround: use ifconfig which is installed to termux by default, iirc. - if "Android" in self.platform: + if self.platform == "android": # shell command is: ifconfig 2> /dev/null | awk '/wlan0/{flag=1} flag && /inet /{print $2; exit}' IP = ( subprocess.check_output( diff --git a/pikaraoke/lib/get_platform.py b/pikaraoke/lib/get_platform.py index abe142f9..219f9f1c 100644 --- a/pikaraoke/lib/get_platform.py +++ b/pikaraoke/lib/get_platform.py @@ -26,12 +26,16 @@ def is_raspberry_pi(): return ( (os.uname()[4][:3] == "arm" or os.uname()[4] == "aarch64") and sys.platform != "darwin" - and not (os.path.exists("/system/app/") and os.path.exists("/system/priv-app")) + 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" @@ -41,8 +45,8 @@ def get_platform(): # if "termux" in os.environ[key]: # return "Termux on Android" # return "linux" - elif os.path.exists("/system/app/") and os.path.exists("/system/priv-app"): - return "Android" + elif is_android(): + return "android" elif is_raspberry_pi(): try: with open("/proc/device-tree/model", "r") as file: diff --git a/scripts/README.md b/scripts/README.md index 3229a2b9..f53e750e 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -44,27 +44,6 @@ setup-windows.bat Windows firewall may initially block connections to port 5555 and 5556. Be sure to allow these. It should prompt the first time you run pikaraoke and launch a song. Otherwise, configure it manually in the security settings. -### Android (via Termux App) - -Install the Termux App via https://f-droid.org/en/packages/com.termux/ or from Google Play via https://play.google.com/store/apps/details?id=com.termux - -Open the Termux App (A terminal emulator similar to linux) - -Optional (but recommended): -Updated the packages using the command: - -``` -pkg update && pkg upgrade -``` - -Install python and ffmpeg: - -``` -pkg install python ffmpeg -``` - -from this point onwards, you only need to follow the installation for Raspberry pi / Linux / OSX (see above). - ## Launch cd to the pikaraoke directory and run: