MCPcopy Create free account
hub / github.com/tox-dev/platformdirs / _android_folder

Function _android_folder

src/platformdirs/android.py:171–215  ·  view source on GitHub ↗

:returns: base folder for the Android OS or None if it cannot be found

()

Source from the content-addressed store, hash-verified

169
170@lru_cache(maxsize=1)
171def _android_folder() -> str | None: # noqa: C901
172 """:returns: base folder for the Android OS or None if it cannot be found"""
173 result: str | None = None
174 # type checker isn't happy with our "import android", just don't do this when type checking see
175 # https://stackoverflow.com/a/61394121
176 if not TYPE_CHECKING:
177 try:
178 # First try to get a path to android app using python4android (if available)...
179 from android import mActivity # noqa: PLC0415
180
181 context = cast("android.content.Context", mActivity.getApplicationContext()) # noqa: F821
182 result = context.getFilesDir().getParentFile().getAbsolutePath()
183 except Exception: # noqa: BLE001
184 result = None
185 if result is None:
186 try:
187 # ...and fall back to using plain pyjnius, if python4android isn't available or doesn't deliver any useful
188 # result...
189 from jnius import autoclass # noqa: PLC0415 # ty: ignore[unresolved-import]
190
191 context = autoclass("android.content.Context")
192 result = context.getFilesDir().getParentFile().getAbsolutePath()
193 except Exception: # noqa: BLE001
194 result = None
195 if result is None:
196 # and if that fails, too, find an android folder looking at path on the sys.path
197 # warning: only works for apps installed under /data, not adopted storage etc.
198 pattern = re.compile(r"/data/(data|user/\d+)/(.+)/files")
199 for path in sys.path:
200 if pattern.match(path):
201 result = path.split("/files")[0]
202 break
203 else:
204 result = None
205 if result is None:
206 # one last try: find an android folder looking at path on the sys.path taking adopted storage paths into
207 # account
208 pattern = re.compile(r"/mnt/expand/[a-fA-F0-9-]{36}/(data|user/\d+)/(.+)/files")
209 for path in sys.path:
210 if pattern.match(path):
211 result = path.split("/files")[0]
212 break
213 else:
214 result = None
215 return result
216
217
218@lru_cache(maxsize=1)

Callers 10

_set_platform_dir_classFunction · 0.90
test_android_activeFunction · 0.90
user_data_dirMethod · 0.85
user_config_dirMethod · 0.85
user_cache_dirMethod · 0.85
user_bin_dirMethod · 0.85

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…