[fenix] closes https://github.com/mozilla-mobile/fenix/issues/23514: add firefox wallpapers
Before Width: | Height: | Size: 16 KiB |
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
|
||||
}
|
||||
]
|
@ -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()
|
||||
}
|
||||
)
|
||||
}
|
After Width: | Height: | Size: 108 KiB |
After Width: | Height: | Size: 75 KiB |
After Width: | Height: | Size: 69 KiB |
After Width: | Height: | Size: 108 KiB |
After Width: | Height: | Size: 72 KiB |
After Width: | Height: | Size: 68 KiB |
After Width: | Height: | Size: 145 KiB |
After Width: | Height: | Size: 104 KiB |
After Width: | Height: | Size: 130 KiB |
After Width: | Height: | Size: 194 KiB |
After Width: | Height: | Size: 179 KiB |
After Width: | Height: | Size: 335 KiB |
After Width: | Height: | Size: 148 KiB |
After Width: | Height: | Size: 106 KiB |
After Width: | Height: | Size: 97 KiB |
After Width: | Height: | Size: 190 KiB |
After Width: | Height: | Size: 176 KiB |
After Width: | Height: | Size: 269 KiB |