Bug 1809798 - Loads the right wallpaper for the right orientation

Before, the orientation was taken from the application context which
didn't update during orientation change. Which caused to load the wrong
wallpaper when the phone was rotated. With these changes we can send
the correct orientation taken from the HomeActivity context.
fenix/123.0
RebecaTudor 5 months ago committed by mergify[bot]
parent cc476767f7
commit 528136c473

@ -1013,7 +1013,7 @@ class HomeFragment : Fragment() {
// qualified type to load the image // qualified type to load the image
val wallpaper = Wallpaper.Default.copy(name = wallpaperName) val wallpaper = Wallpaper.Default.copy(name = wallpaperName)
val wallpaperImage = val wallpaperImage =
requireComponents.useCases.wallpaperUseCases.loadBitmap(wallpaper) context?.let { requireComponents.useCases.wallpaperUseCases.loadBitmap(it, wallpaper) }
wallpaperImage?.let { wallpaperImage?.let {
it.scaleToBottomOfView(binding.wallpaperImageView) it.scaleToBottomOfView(binding.wallpaperImageView)
binding.wallpaperImageView.isVisible = true binding.wallpaperImageView.isVisible = true

@ -5,13 +5,14 @@
package org.mozilla.fenix.wallpapers package org.mozilla.fenix.wallpapers
import android.content.Context import android.content.Context
import android.content.res.Configuration
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import androidx.annotation.UiContext
import androidx.annotation.VisibleForTesting import androidx.annotation.VisibleForTesting
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import mozilla.components.concept.fetch.Client import mozilla.components.concept.fetch.Client
import mozilla.components.support.utils.ext.isLandscape
import org.mozilla.fenix.components.AppStore import org.mozilla.fenix.components.AppStore
import org.mozilla.fenix.components.appstate.AppAction import org.mozilla.fenix.components.appstate.AppAction
import org.mozilla.fenix.ext.settings import org.mozilla.fenix.ext.settings
@ -62,7 +63,6 @@ class WallpapersUseCases(
val loadBitmap: LoadBitmapUseCase by lazy { val loadBitmap: LoadBitmapUseCase by lazy {
DefaultLoadBitmapUseCase( DefaultLoadBitmapUseCase(
getFilesDir = { context.filesDir }, getFilesDir = { context.filesDir },
getOrientation = { context.resources.configuration.orientation },
) )
} }
@ -160,23 +160,25 @@ class WallpapersUseCases(
/** /**
* Load the bitmap for a [wallpaper], if available. * 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 wallpaper The wallpaper to load a bitmap for.
*/ */
suspend operator fun invoke(wallpaper: Wallpaper): Bitmap? suspend operator fun invoke(@UiContext context: Context, wallpaper: Wallpaper): Bitmap?
} }
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal class DefaultLoadBitmapUseCase( internal class DefaultLoadBitmapUseCase(
private val getFilesDir: suspend () -> File, private val getFilesDir: suspend () -> File,
private val getOrientation: () -> Int,
) : LoadBitmapUseCase { ) : LoadBitmapUseCase {
override suspend fun invoke(wallpaper: Wallpaper): Bitmap? = override suspend fun invoke(@UiContext context: Context, wallpaper: Wallpaper): Bitmap? =
loadWallpaperFromDisk(wallpaper) loadWallpaperFromDisk(context, wallpaper)
private suspend fun loadWallpaperFromDisk( @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal suspend fun loadWallpaperFromDisk(
@UiContext context: Context,
wallpaper: Wallpaper, wallpaper: Wallpaper,
): Bitmap? = Result.runCatching { ): Bitmap? = Result.runCatching {
val path = wallpaper.getLocalPathFromContext() val path = wallpaper.getLocalPathFromContext(context)
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val file = File(getFilesDir(), path) val file = File(getFilesDir(), path)
BitmapFactory.decodeStream(file.inputStream()) BitmapFactory.decodeStream(file.inputStream())
@ -187,18 +189,14 @@ class WallpapersUseCases(
* Get the expected local path on disk for a wallpaper. This will differ depending * Get the expected local path on disk for a wallpaper. This will differ depending
* on orientation and app theme. * on orientation and app theme.
*/ */
private fun Wallpaper.getLocalPathFromContext(): String { private fun Wallpaper.getLocalPathFromContext(@UiContext context: Context): String {
val orientation = if (isLandscape()) { val orientation = if (context.isLandscape()) {
Wallpaper.ImageType.Landscape Wallpaper.ImageType.Landscape
} else { } else {
Wallpaper.ImageType.Portrait Wallpaper.ImageType.Portrait
} }
return Wallpaper.getLocalPath(name, orientation) return Wallpaper.getLocalPath(name, orientation)
} }
private fun isLandscape(): Boolean {
return getOrientation() == Configuration.ORIENTATION_LANDSCAPE
}
} }
/** /**

@ -4,6 +4,7 @@
package org.mozilla.fenix.wallpapers package org.mozilla.fenix.wallpapers
import android.content.Context
import io.mockk.Runs import io.mockk.Runs
import io.mockk.coEvery import io.mockk.coEvery
import io.mockk.coVerify import io.mockk.coVerify
@ -494,6 +495,21 @@ class WallpapersUseCasesTest {
verify { appStore.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(wallpaper)) } verify { appStore.dispatch(AppAction.WallpaperAction.UpdateCurrentWallpaper(wallpaper)) }
} }
@Test
fun `GIVEN the context WHEN bitmap is loaded THEN loadWallpaperFromDisk method is called with the correct context and wallpaper`() =
runTest {
val wallpaper: Wallpaper = mockk {
every { name } returns "test"
}
val context = mockk<Context>(relaxed = true)
val defaultLoadBitmapUseCase = spyk(WallpapersUseCases.DefaultLoadBitmapUseCase { mockFolder })
coEvery { defaultLoadBitmapUseCase.loadWallpaperFromDisk(context, wallpaper) } returns mockk()
defaultLoadBitmapUseCase.invoke(context, wallpaper)
coVerify { defaultLoadBitmapUseCase.loadWallpaperFromDisk(context, wallpaper) }
}
private enum class TimeRelation { private enum class TimeRelation {
BEFORE, BEFORE,
NOW, NOW,

Loading…
Cancel
Save