-
The theme of LSPosed App is very beautiful, and I plan to use this theme system in my application. But when I work to night mode, I found that it will not automatically switch to night mode anyway, manual switching is effective. Example Activity: package com.xfq.easytest.activity
import com.xfq.easytest.activity.base.BaseActivity
class MainActivity : BaseActivity() {
//My codes.
} BaseActivity: package com.xfq.easytest.activity.base
import android.content.res.Resources
import android.content.res.Resources.Theme
import android.graphics.Color
import android.os.Build
import com.xfq.easytest.util.ThemeUtil
import rikka.core.util.ResourceUtils.resolveColor
import rikka.material.app.MaterialActivity
open class BaseActivity : MaterialActivity() {
override fun onApplyUserThemeResource(theme: Theme, isDecorView: Boolean) {
theme.applyStyle(ThemeUtil.getNightThemeStyleRes(this), true)
theme.applyStyle(ThemeUtil.getColorThemeStyleRes(), true)
}
override fun computeUserThemeKey(): String? {
return ThemeUtil.getColorTheme() + ThemeUtil.getNightTheme(this)
}
override fun onApplyTranslucentSystemBars() {
super.onApplyTranslucentSystemBars()
window.statusBarColor = Color.TRANSPARENT
window.decorView.post {
if (window.decorView.rootWindowInsets.systemWindowInsetBottom >= Resources.getSystem().displayMetrics.density * 40) {
window.navigationBarColor = resolveColor(theme, android.R.attr.navigationBarColor) and 0x00ffffff or -0x20000000
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
window.isNavigationBarContrastEnforced = false
}
} else {
window.navigationBarColor = Color.TRANSPARENT
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
window.isNavigationBarContrastEnforced = true
}
}
}
}
} (ThemeUtil.java has not been changed) //Some codes.
public class ThemeUtil {
//Some codes.
private static final String THEME_BLACK = "BLACK";
static {
//The following line is the change.
preferences = MyClass.INSTANCE.getPreferences();
colorThemeMap.put("COLOR_PRIMARY", R.style.ThemeOverlay_color_primary);
colorThemeMap.put("MATERIAL_RED", R.style.ThemeOverlay_material_red);
colorThemeMap.put("MATERIAL_PINK", R.style.ThemeOverlay_material_pink);
//Some codes.
}
//Some codes.
} MyClass: package com.xfq.easytest.util
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
@SuppressLint("StaticFieldLeak")
object MyClass {
private var context: Context? = null
fun getPreferences(): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context)
}
fun init(context: Context) { //Will be called in Application.
MyClass.context = context
}
} prefs: <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<com.takisoft.preferencex.PreferenceCategory android:title="@string/theme">
<com.xfq.easytest.util.ThemeColorPreference
android:defaultValue=""
android:dialogTitle="@string/theme_color"
android:key="theme_color"
android:summary="@string/theme_color_summary"
android:title="@string/theme_color" />
<rikka.preference.SimpleMenuPreference
android:defaultValue="MODE_NIGHT_FOLLOW_SYSTEM"
android:entries="@array/theme_texts"
android:entryValues="@array/theme_values"
android:key="dark_theme"
android:summary="%s"
android:title="@string/dark_theme" />
<SwitchPreference
android:key="black_dark_theme"
android:summary="@string/black_dark_theme_summary"
android:title="@string/black_dark_theme" />
</com.takisoft.preferencex.PreferenceCategory>
</PreferenceScreen> Settings Activity: package com.xfq.easytest.activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.preference.SwitchPreference
import androidx.recyclerview.widget.RecyclerView
import com.takisoft.preferencex.PreferenceFragmentCompat
import com.xfq.easytest.R
import com.xfq.easytest.activity.base.BaseActivity
import com.xfq.easytest.databinding.ActivitySettingsBinding
import com.xfq.easytest.util.MyClass
import com.xfq.easytest.util.ThemeColorPreference
import com.xfq.easytest.util.ThemeUtil
import rikka.core.util.ResourceUtils
import rikka.material.app.DayNightDelegate
import rikka.preference.SimpleMenuPreference
import rikka.recyclerview.fixEdgeEffect
import rikka.widget.borderview.BorderRecyclerView
class SettingsActivity : BaseActivity() {
private val prefixKey = SettingsActivity::class.java.name + '.'
private val savedInstanceStateExtra = prefixKey + "SAVED_INSTANCE_STATE"
//My codes.
class SettingsFragment(val binding: ActivitySettingsBinding) : PreferenceFragmentCompat() {
override fun onCreatePreferencesFix(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
findPreference<SimpleMenuPreference>("dark_theme")?.setOnPreferenceChangeListener { _, newValue ->
if (!MyClass.getPreferences().getString("dark_theme", ThemeUtil.MODE_NIGHT_FOLLOW_SYSTEM)?.equals(newValue)!!) {
DayNightDelegate.setDefaultNightMode(ThemeUtil.getDarkTheme(newValue as String))
val activity = activity as SettingsActivity
activity.restart()
}
true
}
findPreference<SwitchPreference>("black_dark_theme")?.setOnPreferenceChangeListener { _, _ ->
val activity = activity as SettingsActivity
if (ResourceUtils.isNightMode(resources.configuration)) {
activity.restart()
}
true
}
findPreference<ThemeColorPreference>("theme_color")?.setOnPreferenceChangeListener { _, _ ->
val activity = activity as SettingsActivity
activity.restart()
true
}
}
override fun onCreateRecyclerView(inflater: LayoutInflater?, parent: ViewGroup?, savedInstanceState: Bundle?): RecyclerView {
val recyclerView: BorderRecyclerView = super.onCreateRecyclerView(inflater, parent, savedInstanceState) as BorderRecyclerView
recyclerView.fixEdgeEffect(false)
recyclerView.borderViewDelegate.setBorderVisibilityChangedListener { top, _, _, _ ->
binding.appbar.isRaised = !top
}
return recyclerView
}
}
private fun newIntent(context: Context): Intent {
return Intent(context, SettingsActivity::class.java)
}
private fun newIntent(savedInstanceState: Bundle, context: Context): Intent {
return newIntent(context).putExtra(savedInstanceStateExtra, savedInstanceState)
}
private fun restart() {
val savedInstanceState = Bundle()
onSaveInstanceState(savedInstanceState)
finish()
startActivity(newIntent(savedInstanceState, this))
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
}
} If you don’t mind, you can check my project: https://github.com/xfqwdsj/EasyTest-Android |
Beta Was this translation helpful? Give feedback.
Answered by
xfqwdsj
Jun 12, 2021
Replies: 1 comment
-
Add DayNightDelegate.setApplicationContext(this)
DayNightDelegate.setDefaultNightMode(ThemeUtil.getDarkTheme()) to Application.java |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
xfqwdsj
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add
to Application.java