]>
glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/services/user_manager.py
1 # Copyright 2021 Erfan Abdi
2 # SPDX-License-Identifier: GPL-3.0-or-later
8 import tools
.helpers
.net
9 from tools
.interfaces
import IUserMonitor
10 from tools
.interfaces
import IPlatform
11 from gi
.repository
import GLib
15 def start(args
, session
, unlocked_cb
=None):
16 waydroid_data
= session
["waydroid_data"]
17 apps_dir
= session
["xdg_data_home"] + "/applications/"
20 "com.android.calculator2",
21 "com.android.camera2",
22 "com.android.contacts",
23 "com.android.deskclock",
24 "com.android.documentsui",
26 "com.android.gallery3d",
27 "com.android.inputmethod.latin",
28 "com.android.settings",
29 "com.google.android.gms",
30 "org.lineageos.eleven",
32 "org.lineageos.jelly",
33 "org.lineageos.recorder"
37 def prepend_list(old_list
, new_list
):
38 for s
in reversed(new_list
):
42 def glib_key_file_get_string_list(key_file
, group
, key
):
44 return key_file
.get_string_list(group
, key
)
48 def glib_key_file_prepend_string_list(key_file
, group
, key
, new_list
):
49 old_list
= glib_key_file_get_string_list(key_file
, group
, key
)
50 prepend_list(old_list
, new_list
)
51 key_file
.set_string_list(group
, key
, new_list
)
53 def glib_key_file_has_value(key_file
, group
, key
):
55 key_file
.get_value(group
, key
)
61 # Creates, deletes, or updates desktop file
62 def updateDesktopFile(appInfo
):
67 for cat
in appInfo
["categories"]:
68 if cat
.strip() == "android.intent.category.LAUNCHER":
72 os
.remove(desktop_file_path
)
76 packageName
= appInfo
["packageName"]
78 desktop_file_path
= apps_dir
+ "/waydroid." + packageName
+ ".desktop"
79 desktop_file
= GLib
.KeyFile()
81 flags
= GLib
.KeyFileFlags
.KEEP_COMMENTS | GLib
.KeyFileFlags
.KEEP_TRANSLATIONS
82 desktop_file
.load_from_file(desktop_file_path
, flags
)
86 desktop_file
.set_string("Desktop Entry", "Type", "Application")
87 desktop_file
.set_string("Desktop Entry", "Name", appInfo
["name"])
88 desktop_file
.set_string("Desktop Entry", "Exec", f
"waydroid app launch {packageName}")
89 desktop_file
.set_string("Desktop Entry", "Icon", f
"{waydroid_data}/icons/{packageName}.png")
90 glib_key_file_prepend_string_list(desktop_file
, "Desktop Entry", "Categories", ["X-WayDroid-App"])
91 desktop_file
.set_string_list("Desktop Entry", "X-Purism-FormFactor", ["Workstation", "Mobile"])
92 glib_key_file_prepend_string_list(desktop_file
, "Desktop Entry", "Actions", ["app_settings"])
93 if packageName
in system_apps
and not glib_key_file_has_value(desktop_file
, "Desktop Entry", "NoDisplay"):
94 desktop_file
.set_boolean("Desktop Entry", "NoDisplay", True)
96 desktop_file
.set_string("Desktop Action app_settings", "Name", "App Settings")
97 desktop_file
.set_string("Desktop Action app_settings", "Exec", f
"waydroid app intent android.settings.APPLICATION_DETAILS_SETTINGS package:{packageName}")
98 desktop_file
.set_string("Desktop Action app_settings", "Icon", f
"{waydroid_data}/icons/com.android.settings.png")
100 desktop_file
.save_to_file(desktop_file_path
)
103 def updateWaydroidDesktopFile(hide
):
104 desktop_file_path
= apps_dir
+ "/Waydroid.desktop"
105 # If the user has set the desktop file as read-only, we won't replace it
106 if os
.path
.isfile(desktop_file_path
) and not os
.access(desktop_file_path
, os
.W_OK
):
107 logging
.info(f
"Desktop file '{desktop_file_path}' is not writeable, not updating it")
110 desktop_file
= GLib
.KeyFile()
112 flags
= GLib
.KeyFileFlags
.KEEP_COMMENTS | GLib
.KeyFileFlags
.KEEP_TRANSLATIONS
113 desktop_file
.load_from_file(desktop_file_path
, flags
)
117 desktop_file
.set_string("Desktop Entry", "Type", "Application")
118 desktop_file
.set_string("Desktop Entry", "Name", "Waydroid")
119 desktop_file
.set_string("Desktop Entry", "Exec", "waydroid show-full-ui")
120 glib_key_file_prepend_string_list(desktop_file
, "Desktop Entry", "Categories", ["X-WayDroid-App", "Utility"])
121 desktop_file
.set_string_list("Desktop Entry", "X-Purism-FormFactor", ["Workstation", "Mobile"])
122 desktop_file
.set_string("Desktop Entry", "Icon", "waydroid")
123 desktop_file
.set_boolean("Desktop Entry", "NoDisplay", hide
)
125 desktop_file
.save_to_file(desktop_file_path
)
127 def userUnlocked(uid
):
128 cfg
= tools
.config
.load(args
)
129 logging
.info("Android with user {} is ready".format(uid
))
131 if cfg
["waydroid"]["auto_adb"] == "True":
133 tools
.helpers
.net
.adb_connect(args
)
137 platformService
= IPlatform
.get_service(args
)
139 if not os
.path
.exists(apps_dir
):
140 os
.mkdir(apps_dir
, 0o700)
141 appsList
= platformService
.getAppsInfo()
143 updateDesktopFile(app
)
144 for existing
in glob
.iglob(f
'{apps_dir}/waydroid.*.desktop'):
145 if os
.path
.basename(existing
) not in map(lambda appInfo
: f
"waydroid.{appInfo['packageName']}.desktop", appsList
):
147 multiwin
= platformService
.getprop("persist.waydroid.multi_windows", "false")
148 updateWaydroidDesktopFile(multiwin
== "true")
152 def packageStateChanged(mode
, packageName
, uid
):
153 platformService
= IPlatform
.get_service(args
)
155 desktop_file_path
= apps_dir
+ "/waydroid." + packageName
+ ".desktop"
156 if mode
== IUserMonitor
.PACKAGE_REMOVED
:
158 os
.remove(desktop_file_path
)
162 appInfo
= platformService
.getAppInfo(packageName
)
163 updateDesktopFile(appInfo
)
165 def service_thread():
167 IUserMonitor
.add_service(args
, userUnlocked
, packageStateChanged
)
171 args
.user_manager
= threading
.Thread(target
=service_thread
)
172 args
.user_manager
.start()
178 if args
.userMonitorLoop
:
179 args
.userMonitorLoop
.quit()
180 except AttributeError:
181 logging
.debug("UserMonitor service is not even started")