Manual day night switch button for settings activity.

This commit is contained in:
Anton Chekulaev 2020-08-26 23:12:01 +10:00
parent 88efb2c08c
commit d930dcd9fa
4 changed files with 35 additions and 33 deletions

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M12,3c-4.97,0 -9,4.03 -9,9s4.03,9 9,9c0.83,0 1.5,-0.67 1.5,-1.5 0,-0.39 -0.15,-0.74 -0.39,-1.01 -0.23,-0.26 -0.38,-0.61 -0.38,-0.99 0,-0.83 0.67,-1.5 1.5,-1.5L16,16c2.76,0 5,-2.24 5,-5 0,-4.42 -4.03,-8 -9,-8zM6.5,12c-0.83,0 -1.5,-0.67 -1.5,-1.5S5.67,9 6.5,9 8,9.67 8,10.5 7.33,12 6.5,12zM9.5,8C8.67,8 8,7.33 8,6.5S8.67,5 9.5,5s1.5,0.67 1.5,1.5S10.33,8 9.5,8zM14.5,8c-0.83,0 -1.5,-0.67 -1.5,-1.5S13.67,5 14.5,5s1.5,0.67 1.5,1.5S15.33,8 14.5,8zM17.5,12c-0.83,0 -1.5,-0.67 -1.5,-1.5S16.67,9 17.5,9s1.5,0.67 1.5,1.5 -0.67,1.5 -1.5,1.5z"/>
</vector>

View File

@ -111,17 +111,6 @@
android:layout_marginTop="@dimen/large_spacing"
android:background="?android:dividerHorizontal" />
<TextView
android:id="@+id/themeSwitchButton"
android:layout_width="match_parent"
android:layout_height="@dimen/setting_button_height"
android:background="@color/accent"
android:textColor="@color/text"
android:textSize="@dimen/medium_font_size"
android:textStyle="bold"
android:gravity="center"
android:text="Switch Theme" />
<View
android:layout_width="match_parent"
android:layout_height="1px"

View File

@ -3,6 +3,12 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_change_theme"
android:title=""
android:icon="@drawable/ic_baseline_palette_24"
app:showAsAction="always" />
<item
android:id="@+id/action_qr_code"
android:title=""

View File

@ -7,10 +7,7 @@ import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.net.Uri
import android.os.AsyncTask
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.*
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
@ -99,15 +96,15 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
seedButton.setOnClickListener { showSeed() }
clearAllDataButton.setOnClickListener { clearAllData() }
versionTextView.text = String.format(getString(R.string.version_s), BuildConfig.VERSION_NAME)
themeSwitchButton.setOnClickListener {
setDarkTheme(!isDarkTheme())
recreate()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.settings_general, menu)
// Day/night themes are only available since Android 10
menu.findItem(R.id.action_change_theme)
.setVisible(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
return true
}
@ -115,7 +112,19 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
return when (item.itemId) {
R.id.action_qr_code -> {
showQRCode()
return true
true
}
R.id.action_change_theme -> {
// A temporary demo code that manually switches between day/night themes.
// The effect is reset after the app restart.
val currentUiMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
val nightMode: Int = when(currentUiMode) {
Configuration.UI_MODE_NIGHT_YES -> AppCompatDelegate.MODE_NIGHT_NO
else -> AppCompatDelegate.MODE_NIGHT_YES
}
AppCompatDelegate.setDefaultNightMode(nightMode)
recreate()
true
}
else -> super.onOptionsItemSelected(item)
}
@ -288,18 +297,6 @@ class SettingsActivity : PassphraseRequiredActionBarActivity() {
}
// endregion
//TODO Remove it.
private fun isDarkTheme(): Boolean {
val themeFlag = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
return themeFlag == Configuration.UI_MODE_NIGHT_YES;
}
//TODO Remove it.
private fun setDarkTheme(darkTheme: Boolean) {
// AppCompatDelegate.setDefaultNightMode(if (darkTheme) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO )
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
private inner class DisplayNameEditActionModeCallback: ActionMode.Callback {
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {