LibreTube/app/src/main/java/com/github/libretube/util/ThemeHelper.kt

117 lines
4.4 KiB
Kotlin
Raw Normal View History

2022-06-07 13:05:49 +05:30
package com.github.libretube.util
import android.app.NotificationManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
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
2022-06-07 13:05:49 +05:30
import com.github.libretube.R
2022-07-02 21:53:24 +05:30
import com.github.libretube.preferences.PreferenceHelper
2022-07-12 21:09:00 +05:30
import com.google.android.material.color.DynamicColors
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-07-12 21:09:00 +05:30
fun updateTheme(activity: AppCompatActivity) {
2022-07-12 22:29:21 +05:30
val themeMode = PreferenceHelper.getString(activity, "theme_toggle", "A")!!
val blackModeEnabled = themeMode == "O"
updateAccentColor(activity, blackModeEnabled)
updateThemeMode(themeMode)
2022-06-07 13:05:49 +05:30
}
2022-07-12 22:29:21 +05:30
private fun updateAccentColor(
activity: AppCompatActivity,
blackThemeEnabled: Boolean
) {
2022-07-12 21:09:00 +05:30
val theme = when (
PreferenceHelper.getString(
activity,
"accent_color",
"purple"
)
) {
"my" -> {
applyDynamicColors(activity)
2022-07-12 22:29:21 +05:30
if (blackThemeEnabled) R.style.MaterialYou_Black
else R.style.MaterialYou
2022-07-12 21:09:00 +05:30
}
2022-07-12 22:29:21 +05:30
"red" -> if (blackThemeEnabled) R.style.Theme_Red_Black else R.style.Theme_Red
"blue" -> if (blackThemeEnabled) R.style.Theme_Blue_Black else R.style.Theme_Blue
"yellow" -> if (blackThemeEnabled) R.style.Theme_Yellow_Black else R.style.Theme_Yellow
"green" -> if (blackThemeEnabled) R.style.Theme_Green_Black else R.style.Theme_Green
"purple" -> if (blackThemeEnabled) R.style.Theme_Purple_Black else R.style.Theme_Purple
else -> if (blackThemeEnabled) R.style.Theme_Purple_Black else R.style.Theme_Purple
2022-06-07 13:05:49 +05:30
}
2022-07-12 21:09:00 +05:30
activity.setTheme(theme)
}
private fun applyDynamicColors(activity: AppCompatActivity) {
/**
* apply dynamic colors to the activity
*/
DynamicColors.applyToActivityIfAvailable(activity)
2022-06-07 13:05:49 +05:30
}
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
"O" -> 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
}
fun changeIcon(context: Context, newLogoActivityAlias: String) {
val activityAliases = context.resources.getStringArray(R.array.iconsValue)
// Disable Old Icon(s)
for (activityAlias in activityAliases) {
context.packageManager.setComponentEnabledSetting(
ComponentName(context.packageName, "com.github.libretube.$activityAlias"),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
)
}
// Enable New Icon
context.packageManager.setComponentEnabledSetting(
ComponentName(context.packageName, "com.github.libretube.$newLogoActivityAlias"),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP
)
}
// Needed due to different MainActivity Aliases because of the app icons
fun restartMainActivity(context: Context) {
// kill player notification
val nManager = context
.getSystemService(AppCompatActivity.NOTIFICATION_SERVICE) as NotificationManager
nManager.cancelAll()
// restart to MainActivity
val pm: PackageManager = context.packageManager
val intent = pm.getLaunchIntentForPackage(context.packageName)
intent?.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
context.startActivity(intent)
}
2022-07-04 12:25:52 +05:30
fun getThemeColor(context: Context, colorCode: Int): Int {
val value = TypedValue()
context.theme.resolveAttribute(colorCode, value, true)
return value.data
}
fun getStyledAppName(context: Context): Spanned {
val colorPrimary = getThemeColor(context, R.attr.colorPrimaryDark)
val hexColor = String.format("#%06X", (0xFFFFFF and colorPrimary))
return HtmlCompat.fromHtml(
"Libre<span style='color:$hexColor';>Tube</span>",
HtmlCompat.FROM_HTML_MODE_COMPACT
)
}
2022-06-07 13:05:49 +05:30
}