@ -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
}
}
}
/ * *
/ * *