Added support for obeying system dark mode toggle on Android 10

This commit is contained in:
Deniz Düzgören
2019-09-05 18:20:30 +02:00
parent 9b5dfd9c60
commit 371221a092
11 changed files with 136 additions and 98 deletions
@@ -73,50 +73,32 @@ internal class Main : AppCompatActivity(R.layout.app_bar_main) {
setTheme(R.style.AppTheme0)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
window.statusBarColor = ContextCompat.getColor(this,
R.color.legacyBlack
)
window.navigationBarColor = ContextCompat.getColor(this,
R.color.legacyBlack
)
}
when (prefs.getInt("themeInt", 0)) {
0 -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Build.VERSION.SDK_INT in 23..28) {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
window.statusBarColor = ContextCompat.getColor(this,
R.color.colorBackground
)
window.navigationBarColor = ContextCompat.getColor(this,
R.color.colorBackground
)
} else {
window.statusBarColor = ContextCompat.getColor(this,
R.color.legacyBlack
)
window.navigationBarColor = ContextCompat.getColor(this,
R.color.legacyBlack
)
window.navigationBarColor = ContextCompat.getColor(context, R.color.colorBackground)
}
}
2 -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
else -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
window.navigationBarColor = ContextCompat.getColor(this,
R.color.colorBackground
)
window.statusBarColor = ContextCompat.getColor(this,
R.color.colorBackground
)
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
window.navigationBarColor = ContextCompat.getColor(context, R.color.colorBackground)
}
}
// else -> {
// when (context.resources.configuration.uiMode) {
// Configuration.UI_MODE_NIGHT_YES, Configuration.UI_MODE_NIGHT_UNDEFINED -> {
// AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
// window.navigationBarColor = ContextCompat.getColor(this, R.color.background)
// }
// }
// Configuration.UI_MODE_NIGHT_NO -> {
// AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
// window.navigationBarColor = ContextCompat.getColor(this, R.color.background)
// }
// }
// } // TODO theming based on system settings for Android Q
}
if (!prefs.getBoolean("autoRefresh", false) && prefs.getInt("firstTimeOpening", 0) != 0) {
@@ -124,7 +124,7 @@ internal object HelperFunctions {
var colour = ""
val edit = prefs.edit()
for (course in languageIndependentCourses) {
for (i in 0 until colourIntegers.size) {
for (i in colourIntegers.indices) {
if (prefs.getInt("bg$course", 0) == colourIntegers[i]) {
colour = colourNames[i]
break
@@ -35,6 +35,7 @@ import com.google.android.material.button.MaterialButton
import com.google.android.material.textfield.TextInputEditText
import com.google.firebase.messaging.FirebaseMessaging
import com.jaredrummler.android.device.DeviceName
import kotlinx.android.synthetic.main.content_settings.*
import kotlin.collections.ArrayList
internal class SettingsFragment : Fragment(R.layout.content_settings), View.OnClickListener,
@@ -73,7 +74,7 @@ internal class SettingsFragment : Fragment(R.layout.content_settings), View.OnCl
val txtCourses = view.findViewById<TextInputEditText>(R.id.txtCourses)
val switchDisableGreeting = view.findViewById<Switch>(R.id.switchDisableGreeting)
val switchDark = view.findViewById<Switch>(R.id.switchDark)
// val switchDark = view.findViewById<Switch>(R.id.switchDark)
val switchNotifications = view.findViewById<Switch>(R.id.switchNotifications)
val switchDefaultPlan = view.findViewById<Switch>(R.id.switchDefaultPlan)
val switchAutoRefresh = view.findViewById<Switch>(R.id.switchAutoRefresh)
@@ -86,7 +87,7 @@ internal class SettingsFragment : Fragment(R.layout.content_settings), View.OnCl
setRingtoneText()
switchDisableGreeting.setOnCheckedChangeListener(this)
switchDark.setOnCheckedChangeListener(this)
// switchDark.setOnCheckedChangeListener(this)
switchNotifications.setOnCheckedChangeListener(this)
switchDefaultPlan.setOnCheckedChangeListener(this)
switchAutoRefresh.setOnCheckedChangeListener(this)
@@ -101,11 +102,59 @@ internal class SettingsFragment : Fragment(R.layout.content_settings), View.OnCl
view.findViewById<LinearLayout>(R.id.btnPrivacyP).setOnClickListener(this)
btnVersion.setOnClickListener(this)
switchDisableGreeting.isChecked = prefs.getBoolean("greeting", true)
switchDark.isChecked = when (prefs.getInt("themeInt", 0)) {
1 -> true
else -> false
val darkModeDropDown = view.findViewById<AutoCompleteTextView>(R.id.darkModeDropDownText)
val darkModeList = if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
R.array.themesPreQ
} else {
R.array.themes
}
val darkModeAdapter = ArrayAdapter.createFromResource(
mContext,
darkModeList,
R.layout.dropdown_item
)
darkModeAdapter.setDropDownViewResource(R.layout.dropdown_item)
darkModeDropDown.setAdapter(darkModeAdapter)
darkModeDropDown.setText(darkModeDropDown.adapter.getItem(prefs.getInt("themeInt", 0)).toString(), false)
darkModeDropDown.setOnItemClickListener { _, _, position, _ ->
edit.putInt("themeInt", position).apply()
val bottomNav = view.rootView.findViewById<BottomNavigationView>(R.id.bottom_nav)
when (position) {
0 -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
if (Build.VERSION.SDK_INT in 23..28) {
window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
window?.navigationBarColor = ContextCompat.getColor(mContext, R.color.colorBackground)
}
}
2 -> { // only accessible on Android 10 (and above)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
else -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
window?.navigationBarColor = ContextCompat.getColor(mContext, R.color.colorBackground)
}
}
}
if (prefs.getBoolean("defaultPersonalised", false)) {
bottomNav.selectedItemId = R.id.personal
} else {
bottomNav.selectedItemId = R.id.plan
}
}
switchDisableGreeting.isChecked = prefs.getBoolean("greeting", true)
// switchDark.isChecked = when (prefs.getInt("themeInt", 0)) {
// 1 -> true
// 2 -> true
// else -> false
// }
switchNotifications.isChecked = prefs.getBoolean("notif", false)
switchDefaultPlan.isChecked = prefs.getBoolean("defaultPersonalised", false)
switchAutoRefresh.isChecked = prefs.getBoolean("autoRefresh", false)
@@ -232,28 +281,6 @@ internal class SettingsFragment : Fragment(R.layout.content_settings), View.OnCl
R.id.switchDisableGreeting -> {
edit.putBoolean("greeting", isChecked)
}
R.id.switchDark -> {
val bottomNav = v.rootView.findViewById<BottomNavigationView>(R.id.bottom_nav)
if (isChecked) {
edit.putInt("themeInt", 1)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
window?.navigationBarColor = ContextCompat.getColor(mContext, R.color.colorBackground)
} else {
edit.putInt("themeInt", 0)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
window?.navigationBarColor = ContextCompat.getColor(mContext, R.color.colorBackground)
}
}
if (prefs.getBoolean("defaultPersonalised", false)) {
bottomNav.selectedItemId = R.id.personal
} else {
bottomNav.selectedItemId = R.id.plan
}
}
R.id.switchNotifications -> {
edit.putBoolean("notif", isChecked)
if (isChecked) {
+22 -32
View File
@@ -111,37 +111,27 @@
app:layout_constraintStart_toStartOf="@+id/txtGreeting"
app:layout_constraintTop_toTopOf="@+id/txtGreeting" />
<TextView
android:id="@+id/txtDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/darkBackground"
android:textColor="@color/colorText"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="@+id/txtGreeting"
app:layout_constraintTop_toBottomOf="@+id/txtJustnice" />
<TextView
android:id="@+id/txtRestart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nightMode"
android:textColor="@color/colorHint"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="@+id/txtDark"
app:layout_constraintTop_toBottomOf="@+id/txtDark" />
<Switch
android:id="@+id/switchDark"
<com.google.android.material.textfield.TextInputLayout android:id="@+id/darkModeDropDown"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginEnd="16dp"
android:gravity="end|center"
app:layout_constraintBottom_toBottomOf="@+id/txtRestart"
android:layout_height="wrap_content"
android:hint="@string/appTheme"
android:layout_marginTop="16dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/txtDark"
app:layout_constraintTop_toTopOf="@+id/txtDark" />
app:layout_constraintTop_toBottomOf="@+id/switchDisableGreeting">
<AutoCompleteTextView
android:id="@+id/darkModeDropDownText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:textColor="@color/colorText"
android:textSize="14sp"/>
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="@+id/txtCustomiseColours1"
@@ -151,8 +141,8 @@
android:text="@string/customiseColoursTitle"
android:textColor="@color/colorText"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="@+id/txtRestart"
app:layout_constraintTop_toBottomOf="@+id/txtRestart" />
app:layout_constraintStart_toStartOf="@+id/txtJustnice"
app:layout_constraintTop_toBottomOf="@+id/darkModeDropDown" />
<TextView
android:id="@+id/txtCustomiseColours2"
@@ -179,7 +169,7 @@
app:layout_constraintBottom_toTopOf="@+id/txtNotifications"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtRestart" />
app:layout_constraintTop_toBottomOf="@+id/darkModeDropDown" />
<TextView
android:id="@+id/txtNotifications"
+10
View File
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
xmlns:android="http://schemas.android.com/apk/res/android" />
+10
View File
@@ -141,4 +141,14 @@
<string name="notReceivingNotifications1">Benachrichtigungen kommen nicht an?</string>
<string name="notReceivingNotifications2">Klick hier um zu sehen, warum</string>
<string name="notReceivingNotificationsHelp">Bemerke, dass diese Tipps insbesondere auf Huawei/Honor-Geräten nicht funktionieren könnten. Du kannst sie trotzdem ausprobieren.\n\nAkkusparfunktionen können dazu führen, dass Benachrichtigungen nicht ankommen. Versuche, Akkusparmodi auf deren Standardeinstellung zu setzen.\n\nEinige Geräte beschränken Apps im Hintergrund. Suche in deinen Geräteeinstellungen unter Apps die Vertretungsplan-App und schaue dann nach einer Einstellung namens \"Hintergrund-Services beschränken\" (oder ähnlich) und stelle sicher, dass diese ausgestellt ist.\n\nBei Schwierigkeiten kann ich auch gerne helfen.</string>
<string name="appTheme">App-Design</string>
<string-array name="themes">
<item>Hell</item>
<item>Dunkel</item>
<item>System folgen</item>
</string-array>
<string-array name="themesPreQ">
<item>Hell</item>
<item>Dunkel</item>
</string-array>
</resources>
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isLight">false</bool>
</resources>
+1
View File
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="isTablet">false</bool>
<bool name="isLight">true</bool>
</resources>
+10
View File
@@ -155,4 +155,14 @@
<string name="notReceivingNotifications1">Not receiving notifications?</string>
<string name="notReceivingNotifications2">Click to see why</string>
<string name="notReceivingNotificationsHelp">Note: on Huawei/Honor devices, there may not be a solution to this problem. You\'re welcome to try the tips provided.\n\nBattery saver functions often cause notifications to be delayed or not be received at all. Try setting any battery saving options to their default setting.\n\nSome devices restrict background usage of apps. Please go to your device app settings, select the app and check for a toggle named \"Restrict background usage\" (or similar) and verify that the setting is turned off.\n\nYou\'re also welcome to ask me for help.</string>
<string name="appTheme">App theme</string>
<string-array name="themes">
<item>Light</item>
<item>Dark</item>
<item>Follow system</item>
</string-array>
<string-array name="themesPreQ">
<item>Light</item>
<item>Dark</item>
</string-array>
</resources>
+5 -1
View File
@@ -1,4 +1,4 @@
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="SelectableItemTheme">
<item name="colorControlHighlight">@color/colorAccent</item>
@@ -15,6 +15,10 @@
<item name="colorAccent">@color/colorAccent</item>
<item name="android:fontFamily">@font/manrope_medium</item>
<item name="android:colorEdgeEffect">@color/colorAccent</item>
<item name="android:navigationBarColor">@color/colorBackground</item>
<item name="android:statusBarColor">@color/colorBackground</item>
<item name="android:windowLightStatusBar" tools:targetApi="23">@bool/isLight</item>
<item name="android:windowLightNavigationBar" tools:targetApi="27">@bool/isLight</item>
</style>
<style name="AlertDialog" parent="Theme.MaterialComponents.Light.Dialog.Alert">
+1 -1
View File
@@ -10,7 +10,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0-rc03'
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.0'
classpath 'io.fabric.tools:gradle:1.31.0'