From a1c7fca0d376d66679c2a57ffb95717f52643013 Mon Sep 17 00:00:00 2001 From: RebecaTudor Date: Wed, 31 Jan 2024 17:25:55 +0200 Subject: [PATCH] Bug 1809798 - Loads the right wallpaper when changing orientation Because of the android:configChanges from Manifest that disable the automate Activity recreation, the responsibility of handling the configuration changes is transferred to Activity. When configurations changes, onConfigurationChanged() is called. So now when this function called, the correct orientation is sent forward as parameter of applyWallpaper. --- .../org/mozilla/fenix/home/HomeFragment.kt | 23 ++++++++++---- .../fenix/wallpapers/WallpapersUseCases.kt | 21 +++++++------ .../wallpapers/WallpapersUseCasesTest.kt | 30 ++++++++++++++----- 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt index 056677d96..588737e35 100644 --- a/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/home/HomeFragment.kt @@ -248,7 +248,11 @@ class HomeFragment : Fragment() { val components = requireComponents val currentWallpaperName = requireContext().settings().currentWallpaperName - applyWallpaper(wallpaperName = currentWallpaperName, orientationChange = false) + applyWallpaper( + wallpaperName = currentWallpaperName, + orientationChange = false, + orientation = requireContext().resources.configuration.orientation, + ) components.appStore.dispatch(AppAction.ModeChange(browsingModeManager.mode)) @@ -454,7 +458,11 @@ class HomeFragment : Fragment() { homeMenuView?.dismissMenu() val currentWallpaperName = requireContext().settings().currentWallpaperName - applyWallpaper(wallpaperName = currentWallpaperName, orientationChange = true) + applyWallpaper( + wallpaperName = currentWallpaperName, + orientationChange = true, + orientation = newConfig.orientation, + ) } /** @@ -999,7 +1007,7 @@ class HomeFragment : Fragment() { internal fun shouldEnableWallpaper() = (activity as? HomeActivity)?.themeManager?.currentTheme?.isPrivate?.not() ?: false - private fun applyWallpaper(wallpaperName: String, orientationChange: Boolean) { + private fun applyWallpaper(wallpaperName: String, orientationChange: Boolean, orientation: Int) { when { !shouldEnableWallpaper() || (wallpaperName == lastAppliedWallpaperName && !orientationChange) -> return @@ -1012,8 +1020,7 @@ class HomeFragment : Fragment() { // loadBitmap does file lookups based on name, so we don't need a fully // qualified type to load the image val wallpaper = Wallpaper.Default.copy(name = wallpaperName) - val wallpaperImage = - context?.let { requireComponents.useCases.wallpaperUseCases.loadBitmap(it, wallpaper) } + val wallpaperImage = requireComponents.useCases.wallpaperUseCases.loadBitmap(wallpaper, orientation) wallpaperImage?.let { it.scaleToBottomOfView(binding.wallpaperImageView) binding.wallpaperImageView.isVisible = true @@ -1059,7 +1066,11 @@ class HomeFragment : Fragment() { .distinctUntilChanged() .collect { if (it.name != lastAppliedWallpaperName) { - applyWallpaper(wallpaperName = it.name, orientationChange = false) + applyWallpaper( + wallpaperName = it.name, + orientationChange = false, + orientation = requireContext().resources.configuration.orientation, + ) } } } diff --git a/app/src/main/java/org/mozilla/fenix/wallpapers/WallpapersUseCases.kt b/app/src/main/java/org/mozilla/fenix/wallpapers/WallpapersUseCases.kt index 8d9eb7077..cdb70c163 100644 --- a/app/src/main/java/org/mozilla/fenix/wallpapers/WallpapersUseCases.kt +++ b/app/src/main/java/org/mozilla/fenix/wallpapers/WallpapersUseCases.kt @@ -5,14 +5,13 @@ package org.mozilla.fenix.wallpapers import android.content.Context +import android.content.res.Configuration import android.graphics.Bitmap import android.graphics.BitmapFactory -import androidx.annotation.UiContext import androidx.annotation.VisibleForTesting import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import mozilla.components.concept.fetch.Client -import mozilla.components.support.utils.ext.isLandscape import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.appstate.AppAction import org.mozilla.fenix.ext.settings @@ -160,25 +159,25 @@ class WallpapersUseCases( /** * Load the bitmap for a [wallpaper], if available. * - * @param context The context used to get wallpaper orientation. * @param wallpaper The wallpaper to load a bitmap for. + * @param orientation The orientation of wallpaper. */ - suspend operator fun invoke(@UiContext context: Context, wallpaper: Wallpaper): Bitmap? + suspend operator fun invoke(wallpaper: Wallpaper, orientation: Int): Bitmap? } @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) internal class DefaultLoadBitmapUseCase( private val getFilesDir: suspend () -> File, ) : LoadBitmapUseCase { - override suspend fun invoke(@UiContext context: Context, wallpaper: Wallpaper): Bitmap? = - loadWallpaperFromDisk(context, wallpaper) + override suspend fun invoke(wallpaper: Wallpaper, orientation: Int): Bitmap? = + loadWallpaperFromDisk(wallpaper, orientation) @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) internal suspend fun loadWallpaperFromDisk( - @UiContext context: Context, wallpaper: Wallpaper, + orientation: Int, ): Bitmap? = Result.runCatching { - val path = wallpaper.getLocalPathFromContext(context) + val path = wallpaper.getLocalPathFromContext(orientation) withContext(Dispatchers.IO) { val file = File(getFilesDir(), path) BitmapFactory.decodeStream(file.inputStream()) @@ -189,13 +188,13 @@ class WallpapersUseCases( * Get the expected local path on disk for a wallpaper. This will differ depending * on orientation and app theme. */ - private fun Wallpaper.getLocalPathFromContext(@UiContext context: Context): String { - val orientation = if (context.isLandscape()) { + private fun Wallpaper.getLocalPathFromContext(orientation: Int): String { + val orientationWallpaper = if (orientation == Configuration.ORIENTATION_LANDSCAPE) { Wallpaper.ImageType.Landscape } else { Wallpaper.ImageType.Portrait } - return Wallpaper.getLocalPath(name, orientation) + return Wallpaper.getLocalPath(name, orientationWallpaper) } } diff --git a/app/src/test/java/org/mozilla/fenix/wallpapers/WallpapersUseCasesTest.kt b/app/src/test/java/org/mozilla/fenix/wallpapers/WallpapersUseCasesTest.kt index e2716f300..858f2c2db 100644 --- a/app/src/test/java/org/mozilla/fenix/wallpapers/WallpapersUseCasesTest.kt +++ b/app/src/test/java/org/mozilla/fenix/wallpapers/WallpapersUseCasesTest.kt @@ -4,7 +4,7 @@ package org.mozilla.fenix.wallpapers -import android.content.Context +import android.content.res.Configuration import io.mockk.Runs import io.mockk.coEvery import io.mockk.coVerify @@ -31,7 +31,8 @@ import org.mozilla.fenix.wallpapers.LegacyWallpaperMigration.Companion.TURNING_R import org.mozilla.fenix.wallpapers.LegacyWallpaperMigration.Companion.TURNING_RED_PANDA_WALLPAPER_NAME import org.mozilla.fenix.wallpapers.LegacyWallpaperMigration.Companion.TURNING_RED_WALLPAPER_TEXT_COLOR import java.io.File -import java.util.* +import java.util.Calendar +import java.util.Date import kotlin.random.Random class WallpapersUseCasesTest { @@ -496,18 +497,33 @@ class WallpapersUseCasesTest { } @Test - fun `GIVEN the context WHEN bitmap is loaded THEN loadWallpaperFromDisk method is called with the correct context and wallpaper`() = + fun `GIVEN the portrait orientation WHEN bitmap is loaded THEN loadWallpaperFromDisk method is called with the correct wallpaper and orientation`() = runTest { val wallpaper: Wallpaper = mockk { every { name } returns "test" } - val context = mockk(relaxed = true) + val orientation = Configuration.ORIENTATION_PORTRAIT val defaultLoadBitmapUseCase = spyk(WallpapersUseCases.DefaultLoadBitmapUseCase { mockFolder }) - coEvery { defaultLoadBitmapUseCase.loadWallpaperFromDisk(context, wallpaper) } returns mockk() + coEvery { defaultLoadBitmapUseCase.loadWallpaperFromDisk(wallpaper, orientation) } returns mockk() - defaultLoadBitmapUseCase.invoke(context, wallpaper) + defaultLoadBitmapUseCase.invoke(wallpaper, orientation) - coVerify { defaultLoadBitmapUseCase.loadWallpaperFromDisk(context, wallpaper) } + coVerify { defaultLoadBitmapUseCase.loadWallpaperFromDisk(wallpaper, orientation) } + } + + @Test + fun `GIVEN the landscape orientation WHEN bitmap is loaded THEN loadWallpaperFromDisk method is called with the correct wallpaper and orientation`() = + runTest { + val wallpaper: Wallpaper = mockk { + every { name } returns "test" + } + val orientation = Configuration.ORIENTATION_LANDSCAPE + val defaultLoadBitmapUseCase = spyk(WallpapersUseCases.DefaultLoadBitmapUseCase { mockFolder }) + coEvery { defaultLoadBitmapUseCase.loadWallpaperFromDisk(wallpaper, orientation) } returns mockk() + + defaultLoadBitmapUseCase.invoke(wallpaper, orientation) + + coVerify { defaultLoadBitmapUseCase.loadWallpaperFromDisk(wallpaper, orientation) } } private enum class TimeRelation {