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-17 21:48:39 +05:30
|
|
|
import com.github.libretube.preferences.PreferenceKeys
|
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-17 21:48:39 +05:30
|
|
|
val themeMode = PreferenceHelper.getString(PreferenceKeys.THEME_MODE, "A")!!
|
|
|
|
val pureThemeEnabled = PreferenceHelper.getBoolean(PreferenceKeys.PURE_THEME, false)
|
2022-07-12 22:29:21 +05:30
|
|
|
|
2022-07-15 02:02:42 +05:30
|
|
|
updateAccentColor(activity, pureThemeEnabled)
|
2022-07-12 22:29:21 +05:30
|
|
|
updateThemeMode(themeMode)
|
2022-06-07 13:05:49 +05:30
|
|
|
}
|
|
|
|
|
2022-07-12 22:29:21 +05:30
|
|
|
private fun updateAccentColor(
|
|
|
|
activity: AppCompatActivity,
|
2022-07-15 02:02:42 +05:30
|
|
|
pureThemeEnabled: Boolean
|
2022-07-12 22:29:21 +05:30
|
|
|
) {
|
2022-07-12 21:09:00 +05:30
|
|
|
val theme = when (
|
|
|
|
PreferenceHelper.getString(
|
2022-07-17 21:48:39 +05:30
|
|
|
PreferenceKeys.ACCENT_COLOR,
|
2022-07-12 21:09:00 +05:30
|
|
|
"purple"
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
"my" -> {
|
|
|
|
applyDynamicColors(activity)
|
2022-07-15 02:02:42 +05:30
|
|
|
if (pureThemeEnabled) R.style.MaterialYou_Pure
|
2022-07-12 22:29:21 +05:30
|
|
|
else R.style.MaterialYou
|
2022-07-12 21:09:00 +05:30
|
|
|
}
|
2022-07-15 02:02:42 +05:30
|
|
|
// set the theme, use the pure theme if enabled
|
|
|
|
"red" -> if (pureThemeEnabled) R.style.Theme_Red_Pure else R.style.Theme_Red
|
|
|
|
"blue" -> if (pureThemeEnabled) R.style.Theme_Blue_Pure else R.style.Theme_Blue
|
|
|
|
"yellow" -> if (pureThemeEnabled) R.style.Theme_Yellow_Pure else R.style.Theme_Yellow
|
|
|
|
"green" -> if (pureThemeEnabled) R.style.Theme_Green_Pure else R.style.Theme_Green
|
|
|
|
"purple" -> if (pureThemeEnabled) R.style.Theme_Purple_Pure else R.style.Theme_Purple
|
|
|
|
else -> if (pureThemeEnabled) R.style.Theme_Purple_Pure 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
|
|
|
|
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) {
|
2022-07-17 20:59:37 +05:30
|
|
|
val activityClass = "com.github.libretube." +
|
2022-07-17 21:05:36 +05:30
|
|
|
if (activityAlias == activityAliases[0]) "activities.MainActivity" // default icon/activity
|
|
|
|
else 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,
|
|
|
|
PackageManager.DONT_KILL_APP
|
|
|
|
)
|
|
|
|
}
|
2022-07-17 20:59:37 +05:30
|
|
|
|
|
|
|
// set the class name for the activity alias
|
|
|
|
val newLogoActivityClass = "com.github.libretube." +
|
2022-07-17 21:05:36 +05:30
|
|
|
if (newLogoActivityAlias == activityAliases[0]) "activities.MainActivity" // default icon/activity
|
|
|
|
else 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,
|
|
|
|
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
|
|
|
}
|