For #27456 - Migrate Context.isDark() into a top-level extension

This commit is contained in:
Noah Bond 2022-10-18 14:38:21 -07:00 committed by mergify[bot]
parent 1e8a719197
commit a0df375b17
2 changed files with 11 additions and 6 deletions

View File

@ -7,6 +7,7 @@ package org.mozilla.fenix.ext
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.os.Build
import android.provider.Settings
import android.view.ContextThemeWrapper
@ -103,3 +104,11 @@ fun Context.navigateToNotificationsSettings() {
}
startActivity(intent)
}
/**
* Helper function used to determine if the user's device is set to dark mode.
*
* @return true if the system is considered to be in dark theme.
*/
fun Context.isSystemInDarkTheme(): Boolean =
resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES

View File

@ -16,6 +16,7 @@ import org.mozilla.fenix.FeatureFlags
import org.mozilla.fenix.R
import org.mozilla.fenix.components.AppStore
import org.mozilla.fenix.components.appstate.AppAction
import org.mozilla.fenix.ext.isSystemInDarkTheme
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.utils.Settings
import java.io.File
@ -355,18 +356,13 @@ class WallpapersUseCases(
*/
private fun Wallpaper.getLocalPathFromContext(context: Context): String {
val orientation = if (context.isLandscape()) "landscape" else "portrait"
val theme = if (context.isDark()) "dark" else "light"
val theme = if (context.isSystemInDarkTheme()) "dark" else "light"
return Wallpaper.legacyGetLocalPath(orientation, theme, name)
}
private fun Context.isLandscape(): Boolean {
return resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
}
private fun Context.isDark(): Boolean {
return resources.configuration.uiMode and
Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
}
}
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)