From 371221a0926bca4107893f0c752c3b0fecf02c1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deniz=20D=C3=BCzg=C3=B6ren?= Date: Thu, 5 Sep 2019 18:20:30 +0200 Subject: [PATCH] Added support for obeying system dark mode toggle on Android 10 --- .../substitutionplan/activities/Main.kt | 52 ++++-------- .../substitutionplan/data/HelperFunctions.kt | 2 +- .../fragments/SettingsFragment.kt | 83 ++++++++++++------- app/src/main/res/layout/content_settings.xml | 54 +++++------- app/src/main/res/layout/dropdown_item.xml | 10 +++ app/src/main/res/values-de/strings.xml | 10 +++ app/src/main/res/values-night/attr.xml | 4 + app/src/main/res/values/attr.xml | 1 + app/src/main/res/values/strings.xml | 10 +++ app/src/main/res/values/styles.xml | 6 +- build.gradle | 2 +- 11 files changed, 136 insertions(+), 98 deletions(-) create mode 100644 app/src/main/res/layout/dropdown_item.xml create mode 100644 app/src/main/res/values-night/attr.xml diff --git a/app/src/main/java/com/denizd/substitutionplan/activities/Main.kt b/app/src/main/java/com/denizd/substitutionplan/activities/Main.kt index 845c318..bc9ec24 100644 --- a/app/src/main/java/com/denizd/substitutionplan/activities/Main.kt +++ b/app/src/main/java/com/denizd/substitutionplan/activities/Main.kt @@ -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) { diff --git a/app/src/main/java/com/denizd/substitutionplan/data/HelperFunctions.kt b/app/src/main/java/com/denizd/substitutionplan/data/HelperFunctions.kt index 1906ce2..cd408d6 100644 --- a/app/src/main/java/com/denizd/substitutionplan/data/HelperFunctions.kt +++ b/app/src/main/java/com/denizd/substitutionplan/data/HelperFunctions.kt @@ -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 diff --git a/app/src/main/java/com/denizd/substitutionplan/fragments/SettingsFragment.kt b/app/src/main/java/com/denizd/substitutionplan/fragments/SettingsFragment.kt index 21b466a..4a881dd 100644 --- a/app/src/main/java/com/denizd/substitutionplan/fragments/SettingsFragment.kt +++ b/app/src/main/java/com/denizd/substitutionplan/fragments/SettingsFragment.kt @@ -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(R.id.txtCourses) val switchDisableGreeting = view.findViewById(R.id.switchDisableGreeting) - val switchDark = view.findViewById(R.id.switchDark) +// val switchDark = view.findViewById(R.id.switchDark) val switchNotifications = view.findViewById(R.id.switchNotifications) val switchDefaultPlan = view.findViewById(R.id.switchDefaultPlan) val switchAutoRefresh = view.findViewById(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(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(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(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(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) { diff --git a/app/src/main/res/layout/content_settings.xml b/app/src/main/res/layout/content_settings.xml index b53f9d6..b287440 100644 --- a/app/src/main/res/layout/content_settings.xml +++ b/app/src/main/res/layout/content_settings.xml @@ -111,37 +111,27 @@ app:layout_constraintStart_toStartOf="@+id/txtGreeting" app:layout_constraintTop_toTopOf="@+id/txtGreeting" /> - - - - - + app:layout_constraintTop_toBottomOf="@+id/switchDisableGreeting"> + + + + + app:layout_constraintStart_toStartOf="@+id/txtJustnice" + app:layout_constraintTop_toBottomOf="@+id/darkModeDropDown" /> + app:layout_constraintTop_toBottomOf="@+id/darkModeDropDown" /> + \ No newline at end of file diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 99e772d..92ec189 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -141,4 +141,14 @@ Benachrichtigungen kommen nicht an? Klick hier um zu sehen, warum 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. + App-Design + + Hell + Dunkel + System folgen + + + Hell + Dunkel + \ No newline at end of file diff --git a/app/src/main/res/values-night/attr.xml b/app/src/main/res/values-night/attr.xml new file mode 100644 index 0000000..3b094f6 --- /dev/null +++ b/app/src/main/res/values-night/attr.xml @@ -0,0 +1,4 @@ + + + false + \ No newline at end of file diff --git a/app/src/main/res/values/attr.xml b/app/src/main/res/values/attr.xml index f855de6..6fb8bef 100644 --- a/app/src/main/res/values/attr.xml +++ b/app/src/main/res/values/attr.xml @@ -1,4 +1,5 @@ false + true \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f273587..480fad3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -155,4 +155,14 @@ Not receiving notifications? Click to see why 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. + App theme + + Light + Dark + Follow system + + + Light + Dark + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index ef34521..bda7912 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,4 +1,4 @@ - +