Loading packages/SystemUI/src/com/android/systemui/settings/UserFileManagerImpl.kt +20 −3 Original line number Diff line number Diff line Loading @@ -49,8 +49,8 @@ class UserFileManagerImpl @Inject constructor( ) : UserFileManager, CoreStartable(context) { companion object { private const val FILES = "files" private const val SHARED_PREFS = "shared_prefs" internal const val ID = "UserFileManager" @VisibleForTesting internal const val SHARED_PREFS = "shared_prefs" @VisibleForTesting internal const val ID = "UserFileManager" } private val broadcastReceiver = object : BroadcastReceiver() { Loading Loading @@ -85,13 +85,15 @@ class UserFileManagerImpl @Inject constructor( fileName ) } else { Environment.buildPath( val secondaryFile = Environment.buildPath( context.filesDir, ID, userId.toString(), FILES, fileName ) ensureParentDirExists(secondaryFile) secondaryFile } } Loading @@ -114,6 +116,7 @@ class UserFileManagerImpl @Inject constructor( fileName ) ensureParentDirExists(secondaryUserDir) return context.getSharedPreferences(secondaryUserDir, mode) } Loading Loading @@ -141,4 +144,18 @@ class UserFileManagerImpl @Inject constructor( } } } /** * Checks to see if parent dir of the file exists. If it does not, we create the parent dirs * recursively. */ @VisibleForTesting internal fun ensureParentDirExists(file: File) { val parent = file.parentFile if (!parent.exists()) { if (!parent.mkdirs()) { Log.e(ID, "Could not create parent directory for file: ${file.absolutePath}") } } } } packages/SystemUI/tests/src/com/android/systemui/settings/UserFileManagerImplTest.kt +35 −2 Original line number Diff line number Diff line Loading @@ -31,6 +31,7 @@ import com.android.systemui.util.mockito.eq import com.android.systemui.util.time.FakeSystemClock import com.google.common.truth.Truth.assertThat import java.util.concurrent.Executor import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith Loading Loading @@ -62,6 +63,14 @@ class UserFileManagerImplTest : SysuiTestCase() { broadcastDispatcher, backgroundExecutor) } @After fun end() { val dir = Environment.buildPath( context.filesDir, UserFileManagerImpl.ID) dir.deleteRecursively() } @Test fun testGetFile() { assertThat(userFileManager.getFile(TEST_FILE_NAME, 0).path) Loading @@ -72,8 +81,19 @@ class UserFileManagerImplTest : SysuiTestCase() { @Test fun testGetSharedPreferences() { val secondarySharedPref = userFileManager.getSharedPreferences(TEST_FILE_NAME, 0, 11) val secondaryUserDir = Environment.buildPath( context.filesDir, UserFileManagerImpl.ID, "11", UserFileManagerImpl.SHARED_PREFS, TEST_FILE_NAME ) assertThat(secondarySharedPref).isNotNull() assertThat(secondaryUserDir.exists()) assertThat(userFileManager.getSharedPreferences(TEST_FILE_NAME, 0, 0)) .isNotEqualTo(userFileManager.getSharedPreferences(TEST_FILE_NAME, 0, 11)) .isNotEqualTo(secondarySharedPref) } @Test Loading Loading @@ -115,6 +135,19 @@ class UserFileManagerImplTest : SysuiTestCase() { verify(userManager).aliveUsers assertThat(secondaryUserDir.exists()).isFalse() assertThat(file.exists()).isFalse() dir.deleteRecursively() } @Test fun testEnsureParentDirExists() { val file = Environment.buildPath( context.filesDir, UserFileManagerImpl.ID, "11", "files", TEST_FILE_NAME ) assertThat(file.parentFile.exists()).isFalse() userFileManager.ensureParentDirExists(file) assertThat(file.parentFile.exists()).isTrue() } } Loading
packages/SystemUI/src/com/android/systemui/settings/UserFileManagerImpl.kt +20 −3 Original line number Diff line number Diff line Loading @@ -49,8 +49,8 @@ class UserFileManagerImpl @Inject constructor( ) : UserFileManager, CoreStartable(context) { companion object { private const val FILES = "files" private const val SHARED_PREFS = "shared_prefs" internal const val ID = "UserFileManager" @VisibleForTesting internal const val SHARED_PREFS = "shared_prefs" @VisibleForTesting internal const val ID = "UserFileManager" } private val broadcastReceiver = object : BroadcastReceiver() { Loading Loading @@ -85,13 +85,15 @@ class UserFileManagerImpl @Inject constructor( fileName ) } else { Environment.buildPath( val secondaryFile = Environment.buildPath( context.filesDir, ID, userId.toString(), FILES, fileName ) ensureParentDirExists(secondaryFile) secondaryFile } } Loading @@ -114,6 +116,7 @@ class UserFileManagerImpl @Inject constructor( fileName ) ensureParentDirExists(secondaryUserDir) return context.getSharedPreferences(secondaryUserDir, mode) } Loading Loading @@ -141,4 +144,18 @@ class UserFileManagerImpl @Inject constructor( } } } /** * Checks to see if parent dir of the file exists. If it does not, we create the parent dirs * recursively. */ @VisibleForTesting internal fun ensureParentDirExists(file: File) { val parent = file.parentFile if (!parent.exists()) { if (!parent.mkdirs()) { Log.e(ID, "Could not create parent directory for file: ${file.absolutePath}") } } } }
packages/SystemUI/tests/src/com/android/systemui/settings/UserFileManagerImplTest.kt +35 −2 Original line number Diff line number Diff line Loading @@ -31,6 +31,7 @@ import com.android.systemui.util.mockito.eq import com.android.systemui.util.time.FakeSystemClock import com.google.common.truth.Truth.assertThat import java.util.concurrent.Executor import org.junit.After import org.junit.Before import org.junit.Test import org.junit.runner.RunWith Loading Loading @@ -62,6 +63,14 @@ class UserFileManagerImplTest : SysuiTestCase() { broadcastDispatcher, backgroundExecutor) } @After fun end() { val dir = Environment.buildPath( context.filesDir, UserFileManagerImpl.ID) dir.deleteRecursively() } @Test fun testGetFile() { assertThat(userFileManager.getFile(TEST_FILE_NAME, 0).path) Loading @@ -72,8 +81,19 @@ class UserFileManagerImplTest : SysuiTestCase() { @Test fun testGetSharedPreferences() { val secondarySharedPref = userFileManager.getSharedPreferences(TEST_FILE_NAME, 0, 11) val secondaryUserDir = Environment.buildPath( context.filesDir, UserFileManagerImpl.ID, "11", UserFileManagerImpl.SHARED_PREFS, TEST_FILE_NAME ) assertThat(secondarySharedPref).isNotNull() assertThat(secondaryUserDir.exists()) assertThat(userFileManager.getSharedPreferences(TEST_FILE_NAME, 0, 0)) .isNotEqualTo(userFileManager.getSharedPreferences(TEST_FILE_NAME, 0, 11)) .isNotEqualTo(secondarySharedPref) } @Test Loading Loading @@ -115,6 +135,19 @@ class UserFileManagerImplTest : SysuiTestCase() { verify(userManager).aliveUsers assertThat(secondaryUserDir.exists()).isFalse() assertThat(file.exists()).isFalse() dir.deleteRecursively() } @Test fun testEnsureParentDirExists() { val file = Environment.buildPath( context.filesDir, UserFileManagerImpl.ID, "11", "files", TEST_FILE_NAME ) assertThat(file.parentFile.exists()).isFalse() userFileManager.ensureParentDirExists(file) assertThat(file.parentFile.exists()).isTrue() } }