forked from mozilla-mobile/fenix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For mozilla-mobile#26423: add wallpaper metadata fetcher
- Loading branch information
1 parent
7bfd6ca
commit ae26141
Showing
8 changed files
with
493 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
app/src/main/java/org/mozilla/fenix/wallpapers/WallpaperMetadataFetcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* 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 kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import mozilla.components.concept.fetch.Client | ||
import mozilla.components.concept.fetch.Request | ||
import org.json.JSONArray | ||
import org.json.JSONObject | ||
import org.mozilla.fenix.BuildConfig | ||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
import java.util.Locale | ||
|
||
/** | ||
* Utility class for downloading wallpaper metadata from the remote server. | ||
* | ||
* @property client The client that will be used to fetch metadata. | ||
*/ | ||
class WallpaperMetadataFetcher( | ||
private val client: Client | ||
) { | ||
private val metadataUrl = BuildConfig.WALLPAPER_URL.substringBefore("android") + | ||
"metadata/v$currentJsonVersion/wallpapers.json" | ||
|
||
/** | ||
* Downloads the list of wallpapers from the remote source. Failures will return an empty list. | ||
*/ | ||
suspend fun downloadWallpaperList(): List<Wallpaper> = withContext(Dispatchers.IO) { | ||
Result.runCatching { | ||
val request = Request(url = metadataUrl, method = Request.Method.GET) | ||
val response = client.fetch(request) | ||
response.body.useBufferedReader { | ||
val json = it.readText() | ||
JSONObject(json).parseAsWallpapers() | ||
} | ||
}.getOrElse { listOf() } | ||
} | ||
|
||
private fun JSONObject.parseAsWallpapers(): List<Wallpaper> = with(getJSONArray("collections")) { | ||
(0 until length()).map { index -> | ||
getJSONObject(index).toCollectionOfWallpapers() | ||
}.flatten() | ||
} | ||
|
||
private fun JSONObject.toCollectionOfWallpapers(): List<Wallpaper> { | ||
val collectionId = getString("id") | ||
val availableLocales = optJSONArray("available-locales")?.getAvailableLocales() | ||
val availabilityRange = optJSONObject("availability-range")?.getAvailabilityRange() | ||
val learnMoreUrl = optString("learn-more-url") | ||
val collection = Wallpaper.Collection( | ||
name = collectionId, | ||
availableLocales = availableLocales, | ||
startDate = availabilityRange?.first, | ||
endDate = availabilityRange?.second, | ||
learnMoreUrl = learnMoreUrl, | ||
) | ||
return getJSONArray("wallpapers").toWallpaperList(collection) | ||
} | ||
|
||
private fun JSONArray.getAvailableLocales(): List<String>? = | ||
(0 until length()).map { getString(it) } | ||
|
||
private fun JSONObject.getAvailabilityRange(): Pair<Date, Date>? { | ||
val formatter = SimpleDateFormat("yyyy-MM-dd", Locale.US) | ||
return Result.runCatching { | ||
formatter.parse(getString("start"))!! to formatter.parse(getString("end"))!! | ||
}.getOrNull() | ||
} | ||
|
||
private fun JSONArray.toWallpaperList(collection: Wallpaper.Collection): List<Wallpaper> = | ||
(0 until length()).map { index -> | ||
with(getJSONObject(index)) { | ||
Wallpaper( | ||
name = getString("id"), | ||
textColor = getArgbValueAsLong("text-color"), | ||
cardColor = getArgbValueAsLong("card-color"), | ||
collection = collection, | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* The wallpaper metadata has 6 digit hex color codes for compatibility with iOS. Since Android | ||
* expects 8 digit ARBG values, we prepend FF for the "fully visible" version of the color | ||
* listed in the metadata. | ||
*/ | ||
private fun JSONObject.getArgbValueAsLong(propName: String): Long = "FF${getString(propName)}" | ||
.toLong(radix = 16) | ||
|
||
companion object { | ||
internal const val currentJsonVersion = 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.