Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added support to Android via Termux App #423

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

jepes1981 marked this conversation as resolved.
Show resolved Hide resolved

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