X-Git-Url: https://glassweightruler.freedombox.rocks/gitweb/waydroid.git/blobdiff_plain/13360b5cf1bb8e714932170b41fbc1193e30c4f8..20ad11568352afdbf30dacaa34c9ecd3ee926b26:/tools/services/user_manager.py diff --git a/tools/services/user_manager.py b/tools/services/user_manager.py index 09b6bab..dbde361 100644 --- a/tools/services/user_manager.py +++ b/tools/services/user_manager.py @@ -4,92 +4,93 @@ import logging import os import threading import tools.config +import tools.helpers.net from tools.interfaces import IUserMonitor from tools.interfaces import IPlatform +stopping = False -def start(args, unlocked_cb=None): +def start(args, session, unlocked_cb=None): + waydroid_data = session["waydroid_data"] + apps_dir = session["xdg_data_home"] + "/applications/" def makeDesktopFile(appInfo): + if appInfo is None: + return -1 + showApp = False for cat in appInfo["categories"]: if cat.strip() == "android.intent.category.LAUNCHER": showApp = True if not showApp: return -1 - - packageName = appInfo["packageName"] - desktop_file_path = args.apps_dir + "/waydroid." + packageName + ".desktop" - #TODO: Drop me - if os.path.exists(desktop_file_path): - with open(desktop_file_path) as file: - filedata = file.read() - filedata = filedata.replace( - "Icon=" + args.old_waydroid_data, "Icon=" + args.waydroid_data) - with open(desktop_file_path, 'w') as file: - file.write(filedata) - os.chmod(desktop_file_path, 0o755) + packageName = appInfo["packageName"] + desktop_file_path = apps_dir + "/waydroid." + packageName + ".desktop" if not os.path.exists(desktop_file_path): - lines = ["[Desktop Entry]", "Type=Application"] - lines.append("Name=" + appInfo["name"]) - lines.append("Exec=waydroid app launch " + packageName) - lines.append("Icon=" + args.waydroid_data + "/icons/" + packageName + ".png") - desktop_file = open(desktop_file_path, "w") - for line in lines: - desktop_file.write(line + "\n") - desktop_file.close() - os.chmod(desktop_file_path, 0o755) + with open(desktop_file_path, "w") as desktop_file: + desktop_file.write(f"""\ +[Desktop Entry] +Type=Application +Name={appInfo["name"]} +Exec=waydroid app launch {packageName} +Icon={waydroid_data}/icons/{packageName}.png +Categories=X-WayDroid-App; +X-Purism-FormFactor=Workstation;Mobile; +Actions=app_settings; + +[Desktop Action app_settings] +Name=App Settings +Exec=waydroid app intent android.settings.APPLICATION_DETAILS_SETTINGS package:{packageName} +Icon={waydroid_data}/icons/com.android.settings.png +""") return 0 def makeWaydroidDesktopFile(hide): - desktop_file_path = args.apps_dir + "/Waydroid.desktop" - if os.path.isfile(desktop_file_path): - os.remove(desktop_file_path) - lines = ["[Desktop Entry]", "Type=Application"] - lines.append("Name=Waydroid") - lines.append("Exec=waydroid show-full-ui") - if hide: - lines.append("NoDisplay=true") - lines.append("Icon=" + tools.config.tools_src + "/data/AppIcon.png") - desktop_file = open(desktop_file_path, "w") - for line in lines: - desktop_file.write(line + "\n") - desktop_file.close() - os.chmod(desktop_file_path, 0o755) + desktop_file_path = apps_dir + "/Waydroid.desktop" + # If the user has set the desktop file as read-only, we won't replace it + if os.path.isfile(desktop_file_path) and not os.access(desktop_file_path, os.W_OK): + logging.info(f"Desktop file '{desktop_file_path}' is not writeable, not updating it") + else: + if os.path.isfile(desktop_file_path): + os.remove(desktop_file_path) + with open(desktop_file_path, "w") as desktop_file: + desktop_file.write(f"""\ +[Desktop Entry] +Type=Application +Name=Waydroid +Exec=waydroid show-full-ui +Categories=X-WayDroid-App; +X-Purism-FormFactor=Workstation;Mobile; +Icon=waydroid +NoDisplay={str(hide).lower()} +""") def userUnlocked(uid): + cfg = tools.config.load(args) logging.info("Android with user {} is ready".format(uid)) - session_cfg = tools.config.load_session() - args.waydroid_data = session_cfg["session"]["waydroid_data"] - #TODO: Drop me - args.old_waydroid_data = session_cfg["session"]["host_user"] + \ - "/waydroid/data" - args.apps_dir = session_cfg["session"]["xdg_data_home"] + \ - "/applications/" + + if cfg["waydroid"]["auto_adb"] == "True": + tools.helpers.net.adb_connect(args) platformService = IPlatform.get_service(args) if platformService: - if not os.path.exists(args.apps_dir): - os.mkdir(args.apps_dir) - os.chmod(args.apps_dir, 0o700) + if not os.path.exists(apps_dir): + os.mkdir(apps_dir, 0o700) appsList = platformService.getAppsInfo() for app in appsList: makeDesktopFile(app) multiwin = platformService.getprop("persist.waydroid.multi_windows", "false") - if multiwin == "false": - makeWaydroidDesktopFile(False) - else: - makeWaydroidDesktopFile(True) + makeWaydroidDesktopFile(multiwin == "true") if unlocked_cb: - unlocked_cb(args) + unlocked_cb() def packageStateChanged(mode, packageName, uid): platformService = IPlatform.get_service(args) if platformService: appInfo = platformService.getAppInfo(packageName) - desktop_file_path = args.apps_dir + "/" + packageName + ".desktop" + desktop_file_path = apps_dir + "/waydroid." + packageName + ".desktop" if mode == 0: # Package added makeDesktopFile(appInfo) @@ -102,12 +103,17 @@ def start(args, unlocked_cb=None): os.remove(desktop_file_path) def service_thread(): - IUserMonitor.add_service(args, userUnlocked, packageStateChanged) + while not stopping: + IUserMonitor.add_service(args, userUnlocked, packageStateChanged) + global stopping + stopping = False args.user_manager = threading.Thread(target=service_thread) args.user_manager.start() def stop(args): + global stopping + stopping = True try: if args.userMonitorLoop: args.userMonitorLoop.quit()