@ -5,13 +5,14 @@
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
@ -62,7 +63,6 @@ class WallpapersUseCases(
val loadBitmap : LoadBitmapUseCase by lazy {
DefaultLoadBitmapUseCase (
getFilesDir = { context . filesDir } ,
getOrientation = { context . resources . configuration . orientation } ,
)
}
@ -160,23 +160,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 .
* /
suspend operator fun invoke ( wallpaper : Wallpaper ) : Bitmap ?
suspend operator fun invoke ( @UiContext context : Context , wallpaper : Wallpaper ) : Bitmap ?
}
@VisibleForTesting ( otherwise = VisibleForTesting . PRIVATE )
internal class DefaultLoadBitmapUseCase (
private val getFilesDir : suspend ( ) -> File ,
private val getOrientation : ( ) -> Int ,
) : LoadBitmapUseCase {
override suspend fun invoke ( wallpaper : Wallpaper ) : Bitmap ? =
loadWallpaperFromDisk ( wallpaper)
override suspend fun invoke ( @UiContext context : Context , wallpaper : Wallpaper ) : Bitmap ? =
loadWallpaperFromDisk ( context, wallpaper)
private suspend fun loadWallpaperFromDisk (
@VisibleForTesting ( otherwise = VisibleForTesting . PRIVATE )
internal suspend fun loadWallpaperFromDisk (
@UiContext context : Context ,
wallpaper : Wallpaper ,
) : Bitmap ? = Result . runCatching {
val path = wallpaper . getLocalPathFromContext ( )
val path = wallpaper . getLocalPathFromContext ( context )
withContext ( Dispatchers . IO ) {
val file = File ( getFilesDir ( ) , path )
BitmapFactory . decodeStream ( file . inputStream ( ) )
@ -187,18 +189,14 @@ 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 ( ) : String {
val orientation = if ( isLandscape( ) ) {
private fun Wallpaper . getLocalPathFromContext ( @UiContext context : Context ) : String {
val orientation = if ( context. isLandscape( ) ) {
Wallpaper . ImageType . Landscape
} else {
Wallpaper . ImageType . Portrait
}
return Wallpaper . getLocalPath ( name , orientation )
}
private fun isLandscape ( ) : Boolean {
return getOrientation ( ) == Configuration . ORIENTATION _LANDSCAPE
}
}
/ * *