Skip to content

Commit

Permalink
feat: Added support to Android via Termux App (#423)
Browse files Browse the repository at this point in the history
This PR adds support to Android via the Termux App.
  • Loading branch information
jepes1981 authored Nov 23, 2024
1 parent a26c61c commit 0118733
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
9 changes: 8 additions & 1 deletion pikaraoke/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
34 changes: 23 additions & 11 deletions pikaraoke/karaoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
18 changes: 16 additions & 2 deletions pikaraoke/lib/get_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0118733

Please sign in to comment.