2023-01-31 21:13:39 +05:30
|
|
|
package com.github.libretube.helpers
|
2022-06-07 13:05:49 +05:30
|
|
|
|
2022-08-25 14:56:10 +05:30
|
|
|
import android.app.Activity
|
2022-06-07 13:05:49 +05:30
|
|
|
import android.content.ComponentName
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.pm.PackageManager
|
2022-07-04 12:25:52 +05:30
|
|
|
import android.text.Spanned
|
|
|
|
import android.util.TypedValue
|
2022-06-07 13:05:49 +05:30
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
import androidx.appcompat.app.AppCompatDelegate
|
2022-07-04 12:25:52 +05:30
|
|
|
import androidx.core.text.HtmlCompat
|
2023-01-16 18:30:45 +05:30
|
|
|
import androidx.core.text.parseAsHtml
|
2022-06-07 13:05:49 +05:30
|
|
|
import com.github.libretube.R
|
2022-09-08 21:59:00 +05:30
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2022-11-21 20:42:21 +05:30
|
|
|
import com.github.libretube.ui.adapters.IconsSheetAdapter
|
2022-07-12 21:09:00 +05:30
|
|
|
import com.google.android.material.color.DynamicColors
|
2023-07-11 19:53:55 +05:30
|
|
|
import java.lang.IllegalArgumentException
|
2022-06-07 13:05:49 +05:30
|
|
|
|
2022-06-26 15:56:19 +05:30
|
|
|
object ThemeHelper {
|
2022-06-07 13:05:49 +05:30
|
|
|
|
2022-08-20 13:12:24 +05:30
|
|
|
/**
|
|
|
|
* Set the theme, including accent color and night mode
|
|
|
|
*/
|
2022-07-12 21:09:00 +05:30
|
|
|
fun updateTheme(activity: AppCompatActivity) {
|
2022-08-12 18:30:07 +05:30
|
|
|
val themeMode = PreferenceHelper.getString(PreferenceKeys.THEME_MODE, "A")
|
2022-07-12 22:29:21 +05:30
|
|
|
|
2022-08-25 14:56:10 +05:30
|
|
|
updateAccentColor(activity)
|
|
|
|
applyPureThemeIfEnabled(activity)
|
2022-07-12 22:29:21 +05:30
|
|
|
updateThemeMode(themeMode)
|
2022-06-07 13:05:49 +05:30
|
|
|
}
|
|
|
|
|
2022-08-20 13:12:24 +05:30
|
|
|
/**
|
2023-07-11 19:53:55 +05:30
|
|
|
* Update the accent color of the app and apply dynamic colors if needed
|
2022-08-20 13:12:24 +05:30
|
|
|
*/
|
2022-07-12 22:29:21 +05:30
|
|
|
private fun updateAccentColor(
|
2023-06-24 23:27:00 +05:30
|
|
|
activity: AppCompatActivity
|
2022-07-12 22:29:21 +05:30
|
|
|
) {
|
2023-07-11 19:53:55 +05:30
|
|
|
var accentColor = PreferenceHelper.getString(PreferenceKeys.ACCENT_COLOR, "")
|
|
|
|
|
|
|
|
// automatically choose an accent color on the first app startup
|
|
|
|
if (accentColor.isEmpty()) {
|
|
|
|
accentColor = when (DynamicColors.isDynamicColorAvailable()) {
|
|
|
|
true -> "my"
|
|
|
|
else -> "blue"
|
|
|
|
}
|
|
|
|
PreferenceHelper.putString(PreferenceKeys.ACCENT_COLOR, accentColor)
|
|
|
|
}
|
|
|
|
|
|
|
|
val theme = when (accentColor) {
|
2022-08-25 14:41:31 +05:30
|
|
|
// set the accent color, use the pure black/white theme if enabled
|
2022-08-25 14:56:10 +05:30
|
|
|
"my" -> R.style.BaseTheme
|
|
|
|
"red" -> R.style.Theme_Red
|
|
|
|
"blue" -> R.style.Theme_Blue
|
|
|
|
"yellow" -> R.style.Theme_Yellow
|
|
|
|
"green" -> R.style.Theme_Green
|
|
|
|
"purple" -> R.style.Theme_Purple
|
2022-12-02 18:29:06 +05:30
|
|
|
"monochrome" -> R.style.Theme_Monochrome
|
2022-12-09 21:37:30 +05:30
|
|
|
"violet" -> R.style.Theme_Violet
|
2023-07-11 19:53:55 +05:30
|
|
|
else -> throw IllegalArgumentException()
|
2022-06-07 13:05:49 +05:30
|
|
|
}
|
2022-07-12 21:09:00 +05:30
|
|
|
activity.setTheme(theme)
|
2023-07-11 19:53:55 +05:30
|
|
|
// apply dynamic wallpaper based colors
|
|
|
|
if (accentColor == "my") DynamicColors.applyToActivityIfAvailable(activity)
|
2022-08-25 14:56:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* apply the pure black/white theme
|
|
|
|
*/
|
|
|
|
private fun applyPureThemeIfEnabled(activity: Activity) {
|
|
|
|
val pureThemeEnabled = PreferenceHelper.getBoolean(
|
|
|
|
PreferenceKeys.PURE_THEME,
|
2023-06-24 23:27:00 +05:30
|
|
|
false
|
2022-08-25 14:56:10 +05:30
|
|
|
)
|
|
|
|
if (pureThemeEnabled) activity.theme.applyStyle(R.style.Pure, true)
|
2022-06-07 13:05:49 +05:30
|
|
|
}
|
|
|
|
|
2022-08-20 13:12:24 +05:30
|
|
|
/**
|
|
|
|
* set the theme mode (light, dark, auto)
|
|
|
|
*/
|
2022-07-12 22:29:21 +05:30
|
|
|
private fun updateThemeMode(themeMode: String) {
|
|
|
|
val mode = when (themeMode) {
|
|
|
|
"A" -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
|
|
|
"L" -> AppCompatDelegate.MODE_NIGHT_NO
|
|
|
|
"D" -> AppCompatDelegate.MODE_NIGHT_YES
|
|
|
|
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
2022-06-07 13:05:49 +05:30
|
|
|
}
|
2022-07-12 22:29:21 +05:30
|
|
|
AppCompatDelegate.setDefaultNightMode(mode)
|
2022-06-07 13:05:49 +05:30
|
|
|
}
|
|
|
|
|
2022-08-20 13:12:24 +05:30
|
|
|
/**
|
|
|
|
* change the app icon
|
|
|
|
*/
|
2022-06-07 13:05:49 +05:30
|
|
|
fun changeIcon(context: Context, newLogoActivityAlias: String) {
|
|
|
|
// Disable Old Icon(s)
|
2022-11-21 20:42:21 +05:30
|
|
|
for (appIcon in IconsSheetAdapter.availableIcons) {
|
|
|
|
val activityClass = "com.github.libretube." + appIcon.activityAlias
|
2022-07-17 20:59:37 +05:30
|
|
|
|
2022-07-17 21:05:36 +05:30
|
|
|
// remove old icons
|
2022-06-07 13:05:49 +05:30
|
|
|
context.packageManager.setComponentEnabledSetting(
|
2022-07-17 20:59:37 +05:30
|
|
|
ComponentName(context.packageName, activityClass),
|
2022-06-07 13:05:49 +05:30
|
|
|
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
2023-06-24 23:27:00 +05:30
|
|
|
PackageManager.DONT_KILL_APP
|
2022-06-07 13:05:49 +05:30
|
|
|
)
|
|
|
|
}
|
2022-07-17 20:59:37 +05:30
|
|
|
|
|
|
|
// set the class name for the activity alias
|
2023-07-11 19:53:55 +05:30
|
|
|
val newLogoActivityClass = "com.github.libretube.$newLogoActivityAlias"
|
2022-06-07 13:05:49 +05:30
|
|
|
// Enable New Icon
|
|
|
|
context.packageManager.setComponentEnabledSetting(
|
2022-07-17 20:59:37 +05:30
|
|
|
ComponentName(context.packageName, newLogoActivityClass),
|
2022-06-07 13:05:49 +05:30
|
|
|
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
|
2023-06-24 23:27:00 +05:30
|
|
|
PackageManager.DONT_KILL_APP
|
2022-06-07 13:05:49 +05:30
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-20 13:12:24 +05:30
|
|
|
/**
|
|
|
|
* Get a color by a resource code
|
|
|
|
*/
|
2022-07-23 03:28:23 +05:30
|
|
|
fun getThemeColor(context: Context, colorCode: Int): Int {
|
2022-07-04 12:25:52 +05:30
|
|
|
val value = TypedValue()
|
|
|
|
context.theme.resolveAttribute(colorCode, value, true)
|
|
|
|
return value.data
|
|
|
|
}
|
|
|
|
|
2022-08-20 13:12:24 +05:30
|
|
|
/**
|
|
|
|
* Get the styled app name
|
|
|
|
*/
|
2022-07-04 12:25:52 +05:30
|
|
|
fun getStyledAppName(context: Context): Spanned {
|
2023-04-18 06:08:40 +05:30
|
|
|
val colorPrimary = getThemeColor(context, androidx.appcompat.R.attr.colorPrimaryDark)
|
2023-06-08 22:45:14 +05:30
|
|
|
val hexColor = "#%06X".format(0xFFFFFF and colorPrimary)
|
2023-01-16 18:30:45 +05:30
|
|
|
return "Libre<span style='color:$hexColor';>Tube</span>"
|
|
|
|
.parseAsHtml(HtmlCompat.FROM_HTML_MODE_COMPACT)
|
2022-07-04 12:25:52 +05:30
|
|
|
}
|
2022-06-07 13:05:49 +05:30
|
|
|
}
|