closes #23514: add firefox wallpapers

upstream-sync
Matt Tighe 2 years ago committed by mergify[bot]
parent 4780ebaf50
commit 0837197d84

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

@ -1,14 +0,0 @@
[
{
"name": "wallpaper_1",
"portrait": "wallpapers/wallpaper_1.png",
"landscape": "wallpapers/wallpaper_1.png",
"isDark":true
},
{
"name": "wallpaper_2",
"portrait": "wallpapers/wallpaper_2.png",
"landscape": "wallpapers/wallpaper_2.png",
"isDark":false
}
]

@ -38,7 +38,6 @@ import org.mozilla.fenix.utils.ClipboardHandler
import org.mozilla.fenix.utils.Settings
import org.mozilla.fenix.wallpapers.WallpaperDownloader
import org.mozilla.fenix.wallpapers.WallpaperManager
import org.mozilla.fenix.wallpapers.WallpapersAssetsStorage
import org.mozilla.fenix.wifi.WifiConnectionMonitor
import java.util.concurrent.TimeUnit
@ -148,7 +147,6 @@ class Components(private val context: Context) {
val wallpaperManager by lazyMonitored {
WallpaperManager(
settings,
WallpapersAssetsStorage(context),
WallpaperDownloader(context, core.client, analytics.crashReporter),
analytics.crashReporter,
)

@ -6,6 +6,7 @@ package org.mozilla.fenix.wallpapers
import android.content.Context
import android.content.res.Configuration
import org.mozilla.fenix.R
/**
* A class that represents an available wallpaper and its state.
@ -17,9 +18,6 @@ import android.content.res.Configuration
*/
data class Wallpaper(
val name: String,
val portraitPath: String,
val landscapePath: String,
val isDark: Boolean,
val themeCollection: WallpaperThemeCollection,
)
@ -51,6 +49,13 @@ enum class WallpaperOrigin {
REMOTE,
}
val Wallpaper.drawableId: Int get() = when (name) {
"amethyst" -> R.drawable.amethyst
"cerulean" -> R.drawable.cerulean
"sunrise" -> R.drawable.sunrise
else -> -1
}
/**
* Get the expected local path on disk for a wallpaper. This will differ depending
* on orientation and app theme.

@ -7,7 +7,6 @@ package org.mozilla.fenix.wallpapers
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.content.res.Configuration
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
@ -30,7 +29,6 @@ import java.io.File
@Suppress("TooManyFunctions")
class WallpaperManager(
private val settings: Settings,
private val wallpaperStorage: WallpaperStorage,
private val downloader: WallpaperDownloader,
private val crashReporter: CrashReporter,
) {
@ -38,13 +36,10 @@ class WallpaperManager(
private val remoteWallpapers = listOf(
Wallpaper(
"focus",
portraitPath = "",
landscapePath = "",
isDark = false,
themeCollection = WallpaperThemeCollection.FOCUS
),
)
var availableWallpapers: List<Wallpaper> = loadWallpapers() + remoteWallpapers
var availableWallpapers: List<Wallpaper> = localWallpapers + remoteWallpapers
private set
var currentWallpaper: Wallpaper = getCurrentWallpaperFromSettings()
@ -114,22 +109,18 @@ class WallpaperManager(
*/
fun loadSavedWallpaper(context: Context, wallpaper: Wallpaper): Bitmap? =
if (wallpaper.themeCollection.origin == WallpaperOrigin.LOCAL) {
loadWallpaperFromAssets(context, wallpaper)
loadWallpaperFromDrawables(context, wallpaper)
} else {
loadWallpaperFromDisk(context, wallpaper)
}
private fun loadWallpaperFromAssets(context: Context, wallpaper: Wallpaper): Bitmap? = Result.runCatching {
val path = if (isLandscape(context)) {
wallpaper.landscapePath
} else {
wallpaper.portraitPath
}
context.assets.open(path).use {
BitmapFactory.decodeStream(it)
}
private fun loadWallpaperFromDrawables(context: Context, wallpaper: Wallpaper): Bitmap? = Result.runCatching {
BitmapFactory.decodeResource(context.resources, wallpaper.drawableId)
}.getOrNull()
/**
* Load a wallpaper from app-specific storage.
*/
private fun loadWallpaperFromDisk(context: Context, wallpaper: Wallpaper): Bitmap? = Result.runCatching {
val path = wallpaper.getLocalPathFromContext(context)
runBlockingIncrement {
@ -140,19 +131,6 @@ class WallpaperManager(
}
}.getOrNull()
private fun isLandscape(context: Context): Boolean {
return context.resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
}
private fun loadWallpapers(): List<Wallpaper> {
val wallpapersFromStorage = wallpaperStorage.loadAll()
return if (wallpapersFromStorage.isNotEmpty()) {
listOf(defaultWallpaper) + wallpapersFromStorage
} else {
listOf(defaultWallpaper)
}
}
/**
* Animates the Firefox logo, if it hasn't been animated before, otherwise nothing will happen.
* After animating the first time, the [Settings.shouldAnimateFirefoxLogo] setting
@ -189,12 +167,15 @@ class WallpaperManager(
companion object {
const val DEFAULT_RESOURCE = R.attr.homeBackground
val defaultWallpaper = Wallpaper(
name = "default_wallpaper",
portraitPath = "",
landscapePath = "",
isDark = false,
name = "default",
themeCollection = WallpaperThemeCollection.NONE
)
val localWallpapers = listOf(
defaultWallpaper,
Wallpaper("amethyst", themeCollection = WallpaperThemeCollection.FIREFOX),
Wallpaper("cerulean", themeCollection = WallpaperThemeCollection.FIREFOX),
Wallpaper("sunrise", themeCollection = WallpaperThemeCollection.FIREFOX),
)
private const val ANIMATION_DELAY_MS = 1500L
}
}

@ -1,14 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.wallpapers
/**
* Represents a storage to store [Wallpaper]s.
*/
interface WallpaperStorage {
/**
* Returns all [Wallpaper] from the storage.
*/
fun loadAll(): List<Wallpaper>
}

@ -1,61 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.wallpapers
import android.content.Context
import android.content.res.AssetManager
import mozilla.components.support.base.log.logger.Logger
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import java.lang.Exception
class WallpapersAssetsStorage(private val context: Context) : WallpaperStorage {
val logger = Logger("WallpapersAssetsStorage")
private val wallpapersDirectory = "wallpapers"
@Suppress("TooGenericExceptionCaught")
override fun loadAll(): List<Wallpaper> {
val assetsManager = context.assets
return try {
assetsManager.readArray("$wallpapersDirectory/wallpapers.json").toWallpapers()
} catch (e: Exception) {
logger.error("Unable to load wallpaper", e)
emptyList()
}
}
private fun JSONArray.toWallpapers(): List<Wallpaper> {
return (0 until this.length()).mapNotNull { index ->
this.getJSONObject(index).toWallpaper()
}
}
private fun JSONObject.toWallpaper(): Wallpaper? {
return try {
Wallpaper(
name = getString("name"),
portraitPath = getString("portrait"),
landscapePath = getString("landscape"),
isDark = getBoolean("isDark"),
themeCollection = Result.runCatching {
when (getString("themeCollection")) {
"firefox" -> WallpaperThemeCollection.FIREFOX
else -> WallpaperThemeCollection.NONE
}
}.getOrDefault(WallpaperThemeCollection.NONE)
)
} catch (e: JSONException) {
logger.error("unable to parse json for wallpaper $this", e)
null
}
}
private fun AssetManager.readArray(fileName: String) = JSONArray(
open(fileName).bufferedReader().use {
it.readText()
}
)
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 194 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 179 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 335 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 190 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 269 KiB

@ -13,9 +13,6 @@ import org.mozilla.fenix.utils.Settings
class WallpaperManagerTest {
private val mockSettings: Settings = mockk()
private val mockStorage: WallpaperStorage = mockk {
every { loadAll() } returns listOf()
}
private val mockMetrics: MetricController = mockk()
@Test
@ -27,7 +24,7 @@ class WallpaperManagerTest {
every { mockSettings.currentWallpaper = capture(currentCaptureSlot) } just runs
val updatedWallpaper = WallpaperManager.defaultWallpaper
val wallpaperManager = WallpaperManager(mockSettings, mockStorage, mockk(), mockk())
val wallpaperManager = WallpaperManager(mockSettings, mockk(), mockk())
wallpaperManager.currentWallpaper = updatedWallpaper
assertEquals(updatedWallpaper.name, currentCaptureSlot.captured)

Loading…
Cancel
Save