-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
[Multiple-Profile] feat: profile context wrapper #19703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
criticalAY
wants to merge
2
commits into
ankidroid:main
Choose a base branch
from
criticalAY:mp/context-wrapper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+459
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
188 changes: 188 additions & 0 deletions
188
AnkiDroid/src/main/java/com/ichi2/anki/multiprofile/ProfileContextWrapper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| /* | ||
| * Copyright (c) 2025 Ashish Yadav <[email protected]> | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify it under | ||
| * the terms of the GNU General Public License as published by the Free Software | ||
| * Foundation; either version 3 of the License, or (at your option) any later | ||
| * version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| * details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License along with | ||
| * this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.ichi2.anki.multiprofile | ||
|
|
||
| import android.content.Context | ||
| import android.content.ContextWrapper | ||
| import android.content.SharedPreferences | ||
| import com.ichi2.utils.withFileNameSafe | ||
| import timber.log.Timber | ||
| import java.io.File | ||
| import java.io.IOException | ||
|
|
||
| /** | ||
| * A ContextWrapper that redirects private file access to a specific profile's directory | ||
| * and transparently namespaces SharedPreferences. | ||
| * | ||
| * Use [ProfileContextWrapper.create] to instantiate. | ||
| */ | ||
| class ProfileContextWrapper private constructor( | ||
| base: Context, | ||
| val profileId: ProfileId, | ||
| private val profileBaseDir: File, | ||
| ) : ContextWrapper(base) { | ||
| override fun getFilesDir(): File = | ||
| if (profileId.isDefault()) { | ||
| super.getFilesDir() | ||
| } else { | ||
| File(profileBaseDir, "files").apply { mkdirs() } | ||
| } | ||
|
|
||
| override fun getCacheDir(): File = | ||
| if (profileId.isDefault()) { | ||
| super.getCacheDir() | ||
| } else { | ||
| File(profileBaseDir, "cache").apply { mkdirs() } | ||
| } | ||
|
|
||
| override fun getCodeCacheDir(): File = | ||
| if (profileId.isDefault()) { | ||
| super.getCodeCacheDir() | ||
| } else { | ||
| File(profileBaseDir, "code_cache").apply { mkdirs() } | ||
| } | ||
|
|
||
| override fun getNoBackupFilesDir(): File = | ||
| if (profileId.isDefault()) { | ||
| super.getNoBackupFilesDir() | ||
| } else { | ||
| File(profileBaseDir, "no_backup").apply { mkdirs() } | ||
| } | ||
|
|
||
| override fun getDatabasePath(name: String): File { | ||
| if (profileId.isDefault()) { | ||
| return super.getDatabasePath(name) | ||
| } | ||
| val dbDir = profileBaseDir.withFileNameSafe("databases").apply { mkdirs() } | ||
|
|
||
| if (name == "init") return dbDir | ||
|
|
||
| return dbDir.withFileNameSafe(name) | ||
| } | ||
|
|
||
| /** | ||
| * Used for things like ACRA, textures, etc. | ||
| */ | ||
| override fun getDir( | ||
| name: String, | ||
| mode: Int, | ||
| ): File { | ||
| if (profileId.isDefault()) { | ||
| return super.getDir(name, mode) | ||
| } | ||
|
|
||
| // Prevent directory traversal | ||
| return profileBaseDir.withFileNameSafe(name).apply { mkdirs() } | ||
| } | ||
|
|
||
| override fun getSharedPreferences( | ||
| name: String, | ||
| mode: Int, | ||
| ): SharedPreferences { | ||
| if (profileId.isDefault()) { | ||
| return super.getSharedPreferences(name, mode) | ||
| } | ||
|
|
||
| val prefix = "profile_${profileId.value}_" | ||
| if (name.startsWith(prefix)) { | ||
| return super.getSharedPreferences(name, mode) | ||
| } | ||
|
|
||
| return super.getSharedPreferences("$prefix$name", mode) | ||
| } | ||
|
|
||
| /** | ||
| * Creates the directories required for the Context Wrapper. | ||
| * | ||
| * @throws SecurityException If the resolved path escapes the parent directory. | ||
| * @throws IOException If the directory could not be created | ||
| */ | ||
| private fun requireCustomDirectories() { | ||
| // Default uses the base context wrapper validation | ||
| if (profileId.isDefault()) return | ||
|
|
||
| // create/validate our custom baseDir | ||
| if (profileBaseDir.exists()) { | ||
| if (!profileBaseDir.isDirectory) { | ||
| throw IOException("Profile root exists but is not a directory: $profileBaseDir") | ||
| } | ||
| } else { | ||
| if (!profileBaseDir.mkdirs()) { | ||
| throw IOException("Failed to create profile root directory: $profileBaseDir") | ||
| } | ||
| } | ||
|
|
||
| fun File.mkdirsOrFail() { | ||
| if (mkdirs() || exists()) return | ||
| throw IOException("Failed to create directory: $absolutePath") | ||
| } | ||
|
|
||
| // create the subfolders | ||
| this.filesDir.mkdirsOrFail() | ||
| this.cacheDir.mkdirsOrFail() | ||
| this.codeCacheDir.mkdirsOrFail() | ||
| this.noBackupFilesDir.mkdirsOrFail() | ||
| this.getDatabasePath("init").mkdirsOrFail() | ||
| } | ||
|
|
||
| companion object { | ||
| /** | ||
| * Factory method to safely create a ProfileContextWrapper. | ||
| * | ||
| * Side Effect: This method creates the physical directory structure on disk. | ||
| * | ||
| * @throws IllegalArgumentException If [profileBaseDir] is not inside the application's private storage. | ||
| * @throws IOException If the profile directory structure cannot be created. | ||
| * @throws SecurityException If a path traversal attack occurs | ||
| */ | ||
| @JvmStatic | ||
| fun create( | ||
| context: Context, | ||
| profileId: ProfileId, | ||
| profileBaseDir: File, | ||
| ): ProfileContextWrapper { | ||
| requirePathInFilesDir(profileBaseDir, context) | ||
|
|
||
| return ProfileContextWrapper(context, profileId, profileBaseDir).apply { | ||
| requireCustomDirectories() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @throws IllegalArgumentException if `profileBaseDir` is not in [Context.getFilesDir] | ||
| */ | ||
| private fun requirePathInFilesDir( | ||
| profileBaseDir: File, | ||
| context: Context, | ||
| ) { | ||
| val appDataRoot = context.filesDir.parentFile ?: return | ||
|
|
||
| val validLocation = | ||
| try { | ||
| profileBaseDir.canonicalPath.startsWith(appDataRoot.canonicalPath) | ||
| } catch (e: IOException) { | ||
| Timber.e(e, "Failed to canonicalize path: %s", profileBaseDir) | ||
| false | ||
| } | ||
|
|
||
| if (!validLocation) { | ||
| throw IllegalArgumentException("Profile path must be inside internal storage: $profileBaseDir") | ||
| } | ||
| } | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
AnkiDroid/src/main/java/com/ichi2/anki/multiprofile/ProfileId.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * Copyright (c) 2025 Ashish Yadav <[email protected]> | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify it under | ||
| * the terms of the GNU General Public License as published by the Free Software | ||
| * Foundation; either version 3 of the License, or (at your option) any later | ||
| * version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| * details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License along with | ||
| * this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.ichi2.anki.multiprofile | ||
|
|
||
| /** | ||
| * A type-safe identifier for a user profile. | ||
| */ | ||
| @JvmInline | ||
| value class ProfileId( | ||
| val value: String, | ||
| ) { | ||
| companion object { | ||
| val DEFAULT = ProfileId("default") | ||
| } | ||
|
|
||
| fun isDefault(): Boolean = this == DEFAULT | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.