diff --git a/docs/changelog/+android-device-protected.bugfix.rst b/docs/changelog/+android-device-protected.bugfix.rst new file mode 100644 index 00000000..472be0de --- /dev/null +++ b/docs/changelog/+android-device-protected.bugfix.rst @@ -0,0 +1 @@ +Recognize Android device-protected app storage in the ``sys.path`` fallback and preserve its user ID when resolving shared storage paths. diff --git a/src/platformdirs/_android.py b/src/platformdirs/_android.py index 01fa3b17..f35d67d5 100644 --- a/src/platformdirs/_android.py +++ b/src/platformdirs/_android.py @@ -43,7 +43,7 @@ def android_folder() -> str | None: _ANDROID_FOLDER_PATTERN: Final[re.Pattern[str]] = re.compile( r""" (?P - /data/(?:data|user/\d+) # internal storage of the primary user or of user N + /data/(?:data|user(?:_de)?/\d+) # credential- or device-protected internal storage /[^/]+ # package name, one path segment ) /files(?=/|\Z) # the files directory the interpreter runs from @@ -55,7 +55,7 @@ def android_folder() -> str | None: r""" (?P /mnt/expand/[a-fA-F0-9-]{36} # adopted storage volume, named by its UUID - /(?:data|user/\d+) # the primary user or user N + /(?:data|user(?:_de)?/\d+) # credential- or device-protected storage /[^/]+ # package name, one path segment ) /files(?=/|\Z) # the files directory the interpreter runs from diff --git a/src/platformdirs/android.py b/src/platformdirs/android.py index b4a453ca..59884540 100644 --- a/src/platformdirs/android.py +++ b/src/platformdirs/android.py @@ -209,7 +209,7 @@ def _shared_storage() -> str: _USER_ID: Final[re.Pattern[str]] = re.compile( r""" - /user/ # the per-user data root, under /data or an adopted /mnt/expand volume + /user(?:_de)?/ # the credential- or device-protected per-user data root (?P\d+) # the Android user id / # followed by the package folder """, diff --git a/tests/platformdirs/test_android.py b/tests/platformdirs/test_android.py index 5c6b0106..1fb64c04 100644 --- a/tests/platformdirs/test_android.py +++ b/tests/platformdirs/test_android.py @@ -173,12 +173,18 @@ def test_android(params: AppParams, func: str) -> None: ("path", "expected"), [ pytest.param("/data/user/1/a/files", "/data/user/1/a", id="user"), + pytest.param("/data/user_de/10/a/files/app", "/data/user_de/10/a", id="device-protected"), pytest.param("/data/data/a/files", "/data/data/a", id="data"), pytest.param( "/mnt/expand/8e06fc2f-a86a-44e8-81ce-109e0eedd5ed/user/1/a/files", "/mnt/expand/8e06fc2f-a86a-44e8-81ce-109e0eedd5ed/user/1/a", id="adopted", ), + pytest.param( + "/mnt/expand/8e06fc2f-a86a-44e8-81ce-109e0eedd5ed/user_de/11/a/files/app", + "/mnt/expand/8e06fc2f-a86a-44e8-81ce-109e0eedd5ed/user_de/11/a", + id="adopted-device-protected", + ), pytest.param("/data/data/filesync.app/files/app", "/data/data/filesync.app", id="data-files-prefix"), pytest.param( "/mnt/expand/8e06fc2f-a86a-44e8-81ce-109e0eedd5ed/user/0/filesync.app/files/app", @@ -213,11 +219,17 @@ def _clear_android_caches() -> None: [ pytest.param("/data/data/org.example.app", "/storage/emulated/0", id="owner"), pytest.param("/data/user/10/org.example.app", "/storage/emulated/10", id="secondary-user"), + pytest.param("/data/user_de/10/org.example.app", "/storage/emulated/10", id="device-protected"), pytest.param( "/mnt/expand/8e06fc2f-a86a-44e8-81ce-109e0eedd5ed/user/11/org.example.app", "/storage/emulated/11", id="adopted", ), + pytest.param( + "/mnt/expand/8e06fc2f-a86a-44e8-81ce-109e0eedd5ed/user_de/11/org.example.app", + "/storage/emulated/11", + id="adopted-device-protected", + ), pytest.param(None, "/storage/emulated/0", id="unknown"), ], )