Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
android.py
Go to the documentation of this file.
1"""Android."""
2from __future__ import annotations
3
4import os
5import re
6import sys
7from functools import lru_cache
8from typing import cast
9
10from .api import PlatformDirsABC
11
12
14 """
15 Follows the guidance `from here <https://android.stackexchange.com/a/216132>`_. Makes use of the
16 `appname <platformdirs.api.PlatformDirsABC.appname>`,
17 `version <platformdirs.api.PlatformDirsABC.version>`,
18 `ensure_exists <platformdirs.api.PlatformDirsABC.ensure_exists>`.
19 """
20
21 @property
22 def user_data_dir(self) -> str:
23 """:return: data directory tied to the user, e.g. ``/data/user/<userid>/<packagename>/files/<AppName>``"""
24 return self._append_app_name_and_version(cast(str, _android_folder()), "files")
25
26 @property
27 def site_data_dir(self) -> str:
28 """:return: data directory shared by users, same as `user_data_dir`"""
30
31 @property
32 def user_config_dir(self) -> str:
33 """
34 :return: config directory tied to the user, e.g. \
35 ``/data/user/<userid>/<packagename>/shared_prefs/<AppName>``
36 """
37 return self._append_app_name_and_version(cast(str, _android_folder()), "shared_prefs")
38
39 @property
40 def site_config_dir(self) -> str:
41 """:return: config directory shared by the users, same as `user_config_dir`"""
43
44 @property
45 def user_cache_dir(self) -> str:
46 """:return: cache directory tied to the user, e.g. e.g. ``/data/user/<userid>/<packagename>/cache/<AppName>``"""
47 return self._append_app_name_and_version(cast(str, _android_folder()), "cache")
48
49 @property
50 def site_cache_dir(self) -> str:
51 """:return: cache directory shared by users, same as `user_cache_dir`"""
53
54 @property
55 def user_state_dir(self) -> str:
56 """:return: state directory tied to the user, same as `user_data_dir`"""
58
59 @property
60 def user_log_dir(self) -> str:
61 """
62 :return: log directory tied to the user, same as `user_cache_dir` if not opinionated else ``log`` in it,
63 e.g. ``/data/user/<userid>/<packagename>/cache/<AppName>/log``
64 """
66 if self.opinion:
67 path = os.path.join(path, "log") # noqa: PTH118
68 return path
69
70 @property
71 def user_documents_dir(self) -> str:
72 """:return: documents directory tied to the user e.g. ``/storage/emulated/0/Documents``"""
74
75 @property
76 def user_downloads_dir(self) -> str:
77 """:return: downloads directory tied to the user e.g. ``/storage/emulated/0/Downloads``"""
79
80 @property
81 def user_pictures_dir(self) -> str:
82 """:return: pictures directory tied to the user e.g. ``/storage/emulated/0/Pictures``"""
84
85 @property
86 def user_videos_dir(self) -> str:
87 """:return: videos directory tied to the user e.g. ``/storage/emulated/0/DCIM/Camera``"""
89
90 @property
91 def user_music_dir(self) -> str:
92 """:return: music directory tied to the user e.g. ``/storage/emulated/0/Music``"""
94
95 @property
96 def user_runtime_dir(self) -> str:
97 """
98 :return: runtime directory tied to the user, same as `user_cache_dir` if not opinionated else ``tmp`` in it,
99 e.g. ``/data/user/<userid>/<packagename>/cache/<AppName>/tmp``
100 """
102 if self.opinion:
103 path = os.path.join(path, "tmp") # noqa: PTH118
104 return path
105
106
107@lru_cache(maxsize=1)
108def _android_folder() -> str | None:
109 """:return: base folder for the Android OS or None if it cannot be found"""
110 try:
111 # First try to get path to android app via pyjnius
112 from jnius import autoclass
113
114 context = autoclass("android.content.Context")
115 result: str | None = context.getFilesDir().getParentFile().getAbsolutePath()
116 except Exception: # noqa: BLE001
117 # if fails find an android folder looking path on the sys.path
118 pattern = re.compile(r"/data/(data|user/\d+)/(.+)/files")
119 for path in sys.path:
120 if pattern.match(path):
121 result = path.split("/files")[0]
122 break
123 else:
124 result = None
125 return result
126
127
128@lru_cache(maxsize=1)
130 """:return: documents folder for the Android OS"""
131 # Get directories with pyjnius
132 try:
133 from jnius import autoclass
134
135 context = autoclass("android.content.Context")
136 environment = autoclass("android.os.Environment")
138 except Exception: # noqa: BLE001
139 documents_dir = "/storage/emulated/0/Documents"
140
141 return documents_dir
142
143
144@lru_cache(maxsize=1)
146 """:return: downloads folder for the Android OS"""
147 # Get directories with pyjnius
148 try:
149 from jnius import autoclass
150
151 context = autoclass("android.content.Context")
152 environment = autoclass("android.os.Environment")
154 except Exception: # noqa: BLE001
155 downloads_dir = "/storage/emulated/0/Downloads"
156
157 return downloads_dir
158
159
160@lru_cache(maxsize=1)
162 """:return: pictures folder for the Android OS"""
163 # Get directories with pyjnius
164 try:
165 from jnius import autoclass
166
167 context = autoclass("android.content.Context")
168 environment = autoclass("android.os.Environment")
170 except Exception: # noqa: BLE001
171 pictures_dir = "/storage/emulated/0/Pictures"
172
173 return pictures_dir
174
175
176@lru_cache(maxsize=1)
178 """:return: videos folder for the Android OS"""
179 # Get directories with pyjnius
180 try:
181 from jnius import autoclass
182
183 context = autoclass("android.content.Context")
184 environment = autoclass("android.os.Environment")
186 except Exception: # noqa: BLE001
187 videos_dir = "/storage/emulated/0/DCIM/Camera"
188
189 return videos_dir
190
191
192@lru_cache(maxsize=1)
194 """:return: music folder for the Android OS"""
195 # Get directories with pyjnius
196 try:
197 from jnius import autoclass
198
199 context = autoclass("android.content.Context")
200 environment = autoclass("android.os.Environment")
202 except Exception: # noqa: BLE001
203 music_dir = "/storage/emulated/0/Music"
204
205 return music_dir
206
207
208__all__ = [
209 "Android",
210]
str _append_app_name_and_version(self, *str base)
Definition api.py:71
for i