]> glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/services/user_manager.py
Add device IP address to status, auto adb connect
[waydroid.git] / tools / services / user_manager.py
1 # Copyright 2021 Erfan Abdi
2 # SPDX-License-Identifier: GPL-3.0-or-later
3 import logging
4 import os
5 import threading
6 import tools.config
7 import tools.helpers.net
8 from tools.interfaces import IUserMonitor
9 from tools.interfaces import IPlatform
10
11 stopping = False
12
13 def start(args, session, unlocked_cb=None):
14 waydroid_data = session["waydroid_data"]
15 apps_dir = session["xdg_data_home"] + "/applications/"
16
17 def makeDesktopFile(appInfo):
18 if appInfo is None:
19 return -1
20
21 showApp = False
22 for cat in appInfo["categories"]:
23 if cat.strip() == "android.intent.category.LAUNCHER":
24 showApp = True
25 if not showApp:
26 return -1
27
28 packageName = appInfo["packageName"]
29
30 desktop_file_path = apps_dir + "/waydroid." + packageName + ".desktop"
31 if not os.path.exists(desktop_file_path):
32 lines = ["[Desktop Entry]", "Type=Application"]
33 lines.append("Name=" + appInfo["name"])
34 lines.append("Exec=waydroid app launch " + packageName)
35 lines.append("Icon=" + waydroid_data + "/icons/" + packageName + ".png")
36 lines.append("Categories=X-WayDroid-App;")
37 lines.append("X-Purism-FormFactor=Workstation;Mobile;")
38 lines.append("Actions=app_settings;")
39 lines.append("[Desktop Action app_settings]")
40 lines.append("Name=App Settings")
41 lines.append("Exec=waydroid app intent android.settings.APPLICATION_DETAILS_SETTINGS package:" + packageName)
42 desktop_file = open(desktop_file_path, "w")
43 for line in lines:
44 desktop_file.write(line + "\n")
45 desktop_file.close()
46 os.chmod(desktop_file_path, 0o644)
47 return 0
48
49 def makeWaydroidDesktopFile(hide):
50 desktop_file_path = apps_dir + "/Waydroid.desktop"
51 if os.path.isfile(desktop_file_path):
52 os.remove(desktop_file_path)
53 lines = ["[Desktop Entry]", "Type=Application"]
54 lines.append("Name=Waydroid")
55 lines.append("Exec=waydroid show-full-ui")
56 lines.append("Categories=X-WayDroid-App;")
57 lines.append("X-Purism-FormFactor=Workstation;Mobile;")
58 if hide:
59 lines.append("NoDisplay=true")
60 lines.append("Icon=waydroid")
61 desktop_file = open(desktop_file_path, "w")
62 for line in lines:
63 desktop_file.write(line + "\n")
64 desktop_file.close()
65 os.chmod(desktop_file_path, 0o644)
66
67 def userUnlocked(uid):
68 logging.info("Android with user {} is ready".format(uid))
69
70 tools.helpers.net.adb_connect(args)
71
72 platformService = IPlatform.get_service(args)
73 if platformService:
74 if not os.path.exists(apps_dir):
75 os.mkdir(apps_dir)
76 os.chmod(apps_dir, 0o700)
77 appsList = platformService.getAppsInfo()
78 for app in appsList:
79 makeDesktopFile(app)
80 multiwin = platformService.getprop("persist.waydroid.multi_windows", "false")
81 if multiwin == "false":
82 makeWaydroidDesktopFile(False)
83 else:
84 makeWaydroidDesktopFile(True)
85 if unlocked_cb:
86 unlocked_cb()
87
88 def packageStateChanged(mode, packageName, uid):
89 platformService = IPlatform.get_service(args)
90 if platformService:
91 appInfo = platformService.getAppInfo(packageName)
92 desktop_file_path = apps_dir + "/waydroid." + packageName + ".desktop"
93 if mode == 0:
94 # Package added
95 makeDesktopFile(appInfo)
96 elif mode == 1:
97 if os.path.isfile(desktop_file_path):
98 os.remove(desktop_file_path)
99 else:
100 if os.path.isfile(desktop_file_path):
101 if makeDesktopFile(appInfo) == -1:
102 os.remove(desktop_file_path)
103
104 def service_thread():
105 while not stopping:
106 IUserMonitor.add_service(args, userUnlocked, packageStateChanged)
107
108 global stopping
109 stopping = False
110 args.user_manager = threading.Thread(target=service_thread)
111 args.user_manager.start()
112
113 def stop(args):
114 global stopping
115 stopping = True
116 try:
117 if args.userMonitorLoop:
118 args.userMonitorLoop.quit()
119 except AttributeError:
120 logging.debug("UserMonitor service is not even started")