add "Switch button color" in App setting / Color

This commit is contained in:
tateisu 2019-07-24 03:48:21 +09:00
parent c3ed56c03a
commit a69f7a2846
15 changed files with 149 additions and 25 deletions

View File

@ -40,7 +40,7 @@
</value> </value>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

View File

@ -5,7 +5,7 @@ import androidx.test.runner.AndroidJUnit4
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import jp.juggler.subwaytooter.util.CharacterGroup import jp.juggler.util.CharacterGroup
import jp.juggler.util.WordTrieTree import jp.juggler.util.WordTrieTree
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals

View File

@ -5,6 +5,7 @@ import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.graphics.Color import android.graphics.Color
import android.graphics.PorterDuff
import android.graphics.Typeface import android.graphics.Typeface
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
@ -31,6 +32,7 @@ import org.jetbrains.anko.backgroundDrawable
import org.jetbrains.anko.textColor import org.jetbrains.anko.textColor
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.InputStream
import java.text.NumberFormat import java.text.NumberFormat
import java.util.* import java.util.*
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@ -88,6 +90,8 @@ class ActAppSettingChild : AppCompatActivity()
internal const val COLOR_DIALOG_ID_EVENT_BG_VOTE = 24 internal const val COLOR_DIALOG_ID_EVENT_BG_VOTE = 24
internal const val COLOR_DIALOG_ID_EVENT_BG_FOLLOWREQUEST = 25 internal const val COLOR_DIALOG_ID_EVENT_BG_FOLLOWREQUEST = 25
internal const val COLOR_DIALOG_ID_SWITCH_BUTTON = 26
internal const val REQUEST_CODE_TIMELINE_FONT = 1 internal const val REQUEST_CODE_TIMELINE_FONT = 1
internal const val REQUEST_CODE_TIMELINE_FONT_BOLD = 2 internal const val REQUEST_CODE_TIMELINE_FONT_BOLD = 2
@ -106,6 +110,8 @@ class ActAppSettingChild : AppCompatActivity()
private val booleanViewList = ArrayList<BooleanViewInfo>() private val booleanViewList = ArrayList<BooleanViewInfo>()
private lateinit var svContent : View
private var spBackButtonAction : Spinner? = null private var spBackButtonAction : Spinner? = null
private var spUITheme : Spinner? = null private var spUITheme : Spinner? = null
private var spResizeImage : Spinner? = null private var spResizeImage : Spinner? = null
@ -138,6 +144,7 @@ class ActAppSettingChild : AppCompatActivity()
private var event_bg_color_quote : Int = 0 private var event_bg_color_quote : Int = 0
private var event_bg_color_vote : Int = 0 private var event_bg_color_vote : Int = 0
private var event_bg_color_follow_request : Int = 0 private var event_bg_color_follow_request : Int = 0
private var switch_button_color = 0
private var color_column_header_bg : Int = 0 private var color_column_header_bg : Int = 0
private var color_column_header_fg : Int = 0 private var color_column_header_fg : Int = 0
@ -275,7 +282,11 @@ class ActAppSettingChild : AppCompatActivity()
private fun initUI() { private fun initUI() {
Styler.fixHorizontalPadding(findViewById(R.id.svContent)) svContent = findViewById(R.id.svContent)
setSwitchColor(svContent)
Styler.fixHorizontalPadding(svContent)
// initialize Switch and CheckBox // initialize Switch and CheckBox
for(info in Pref.map.values) { for(info in Pref.map.values) {
@ -432,6 +443,8 @@ class ActAppSettingChild : AppCompatActivity()
, R.id.btnCustomShare2Reset , R.id.btnCustomShare2Reset
, R.id.btnCustomShare3Edit , R.id.btnCustomShare3Edit
, R.id.btnCustomShare3Reset , R.id.btnCustomShare3Reset
, R.id.btnSwitchButtonColorEdit
, R.id.btnSwitchButtonColorReset
).forEach { ).forEach {
findViewById<View>(it)?.setOnClickListener(this) findViewById<View>(it)?.setOnClickListener(this)
} }
@ -617,6 +630,7 @@ class ActAppSettingChild : AppCompatActivity()
event_bg_color_quote = Pref.ipEventBgColorQuote(pref) event_bg_color_quote = Pref.ipEventBgColorQuote(pref)
event_bg_color_vote = Pref.ipEventBgColorVote(pref) event_bg_color_vote = Pref.ipEventBgColorVote(pref)
event_bg_color_follow_request = Pref.ipEventBgColorFollowRequest(pref) event_bg_color_follow_request = Pref.ipEventBgColorFollowRequest(pref)
switch_button_color = Pref.ipSwitchOnColor(pref)
color_column_header_bg = Pref.ipCcdHeaderBg(pref) color_column_header_bg = Pref.ipCcdHeaderBg(pref)
color_column_header_fg = Pref.ipCcdHeaderFg(pref) color_column_header_fg = Pref.ipCcdHeaderFg(pref)
@ -792,6 +806,7 @@ class ActAppSettingChild : AppCompatActivity()
put(Pref.ipEventBgColorQuote, event_bg_color_quote) put(Pref.ipEventBgColorQuote, event_bg_color_quote)
put(Pref.ipEventBgColorVote, event_bg_color_vote) put(Pref.ipEventBgColorVote, event_bg_color_vote)
put(Pref.ipEventBgColorFollowRequest, event_bg_color_follow_request) put(Pref.ipEventBgColorFollowRequest, event_bg_color_follow_request)
put(Pref.ipSwitchOnColor, switch_button_color)
} }
@ -1181,6 +1196,19 @@ class ActAppSettingChild : AppCompatActivity()
saveUIToData() saveUIToData()
} }
R.id.btnSwitchButtonColorEdit -> openColorPicker(
COLOR_DIALOG_ID_SWITCH_BUTTON,
switch_button_color,
true
)
R.id.btnSwitchButtonColorReset -> {
switch_button_color = Pref.ipSwitchOnColor.defVal
saveUIToData()
setSwitchColor(svContent)
}
R.id.btnTranslateAppComponentEdit -> openCustomShareChooser(CustomShareTarget.Translate) R.id.btnTranslateAppComponentEdit -> openCustomShareChooser(CustomShareTarget.Translate)
R.id.btnCustomShare1Edit -> openCustomShareChooser(CustomShareTarget.CustomShare1) R.id.btnCustomShare1Edit -> openCustomShareChooser(CustomShareTarget.CustomShare1)
R.id.btnCustomShare2Edit -> openCustomShareChooser(CustomShareTarget.CustomShare2) R.id.btnCustomShare2Edit -> openCustomShareChooser(CustomShareTarget.CustomShare2)
@ -1369,6 +1397,11 @@ class ActAppSettingChild : AppCompatActivity()
saveUIToData() saveUIToData()
} }
COLOR_DIALOG_ID_SWITCH_BUTTON -> {
switch_button_color = colorOpaque
saveUIToData()
setSwitchColor(svContent)
}
} }
} }
@ -1522,7 +1555,7 @@ class ActAppSettingChild : AppCompatActivity()
val tmp_file = File(dir, "$file_name.tmp") val tmp_file = File(dir, "$file_name.tmp")
val source = contentResolver.openInputStream(uri) // nullable val source : InputStream? = contentResolver.openInputStream(uri)
if(source == null) { if(source == null) {
showToast(this, false, "openInputStream returns null. uri=%s", uri) showToast(this, false, "openInputStream returns null. uri=%s", uri)
return null return null
@ -1819,4 +1852,72 @@ class ActAppSettingChild : AppCompatActivity()
tv.setCompoundDrawablesRelativeWithIntrinsicBounds(icon, null, null, null) tv.setCompoundDrawablesRelativeWithIntrinsicBounds(icon, null, null, null)
} }
private fun setSwitchColor(root : View?) {
fun mixColor(col1 : Int, col2 : Int) : Int = Color.rgb(
(Color.red(col1) + Color.red(col2)) ushr 1,
(Color.green(col1) + Color.green(col2)) ushr 1,
(Color.blue(col1) + Color.blue(col2)) ushr 1
)
val colorBg = getAttributeColor(this, R.attr.colorWindowBackground)
val colorOn = Pref.ipSwitchOnColor(pref)
val colorOff = /* Pref.ipSwitchOffColor(pref).notZero() ?: */
getAttributeColor(this, android.R.attr.colorPrimary)
val colorDisabled = mixColor(colorBg, colorOff)
val colorTrackDisabled = mixColor( colorBg,colorDisabled)
val colorTrackOn = mixColor( colorBg,colorOn)
val colorTrackOff = mixColor( colorBg,colorOff)
// set Switch Color
// https://stackoverflow.com/a/25635526/9134243
val thumbStates = ColorStateList(
arrayOf(
intArrayOf(- android.R.attr.state_enabled),
intArrayOf(android.R.attr.state_checked),
intArrayOf()
),
intArrayOf(
colorDisabled,
colorOn,
colorOff
)
)
val trackStates = ColorStateList(
arrayOf(
intArrayOf(- android.R.attr.state_enabled),
intArrayOf(android.R.attr.state_checked),
intArrayOf()
),
intArrayOf(
colorTrackDisabled,
colorTrackOn,
colorTrackOff
)
)
root?.scan {
if(it !is Switch) {
} else if(Build.VERSION.SDK_INT < 23) {
// android 5
it.thumbDrawable?.setTintList(thumbStates)
it.trackDrawable?.setTintList(thumbStates) // not trackState
} else {
// android 6
it.thumbTintList = thumbStates
if(Build.VERSION.SDK_INT >= 24) {
// android 7
it.trackTintList = trackStates
it.trackTintMode = PorterDuff.Mode.SRC_OVER
}
}
}
}
} }

View File

@ -2,6 +2,7 @@ package jp.juggler.subwaytooter
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.graphics.Color
import android.preference.PreferenceManager import android.preference.PreferenceManager
import jp.juggler.util.optInt import jp.juggler.util.optInt
@ -52,7 +53,7 @@ class BooleanPref(
} }
} }
class IntPref(key : String, private val defVal : Int) : BasePref<Int>(key) { class IntPref(key : String, val defVal : Int) : BasePref<Int>(key) {
override operator fun invoke(pref : SharedPreferences) : Int { override operator fun invoke(pref : SharedPreferences) : Int {
return pref.getInt(key, defVal) return pref.getInt(key, defVal)
@ -449,6 +450,8 @@ object Pref {
val ipLinkColor = IntPref("LinkColor", 0) val ipLinkColor = IntPref("LinkColor", 0)
val ipSwitchOnColor = IntPref("SwitchOnColor", Color.BLACK or 0x0080ff)
val ipTootColorUnlisted = IntPref("ipTootColorUnlisted", 0) val ipTootColorUnlisted = IntPref("ipTootColorUnlisted", 0)
val ipTootColorFollower = IntPref("ipTootColorFollower", 0) val ipTootColorFollower = IntPref("ipTootColorFollower", 0)
val ipTootColorDirectUser = IntPref("ipTootColorDirectUser", 0) val ipTootColorDirectUser = IntPref("ipTootColorDirectUser", 0)

View File

@ -1,6 +1,5 @@
package jp.juggler.subwaytooter.util package jp.juggler.subwaytooter.util
import android.content.Context
import android.text.Spannable import android.text.Spannable
import android.text.SpannableString import android.text.SpannableString
import android.text.SpannableStringBuilder import android.text.SpannableStringBuilder

View File

@ -1,4 +1,4 @@
package jp.juggler.subwaytooter.util package jp.juggler.util
import android.util.SparseBooleanArray import android.util.SparseBooleanArray
import android.util.SparseIntArray import android.util.SparseIntArray
@ -248,7 +248,10 @@ class CharacterGroup {
addGroup(arrayOf("", "", "")) addGroup(arrayOf("", "", ""))
// チルダ // チルダ
addGroup(arrayOf("~", i2s(tmp, 0x301C), i2s(tmp, 0xFF5E))) addGroup(arrayOf("~",
i2s(tmp, 0x301C),
i2s(tmp, 0xFF5E)
))
// 半角カナの濁音,半濁音は2文字になる // 半角カナの濁音,半濁音は2文字になる
addGroup(arrayOf("", "", "ガ")) addGroup(arrayOf("", "", "ガ"))

View File

@ -1,6 +1,5 @@
package jp.juggler.util package jp.juggler.util
import android.app.Dialog
import android.content.ClipData import android.content.ClipData
import android.content.ClipboardManager import android.content.ClipboardManager
import android.content.Context import android.content.Context
@ -14,14 +13,12 @@ import android.graphics.drawable.*
import android.graphics.drawable.shapes.RectShape import android.graphics.drawable.shapes.RectShape
import android.os.Build import android.os.Build
import android.os.SystemClock import android.os.SystemClock
import androidx.core.content.ContextCompat
import android.text.Editable import android.text.Editable
import android.text.TextWatcher import android.text.TextWatcher
import android.util.SparseArray import android.util.SparseArray
import android.view.View
import android.widget.ImageView import android.widget.ImageView
import androidx.core.content.ContextCompat
import jp.juggler.subwaytooter.R import jp.juggler.subwaytooter.R
import org.jetbrains.anko.backgroundDrawable
import java.util.* import java.util.*
object UiUtils{ object UiUtils{

View File

@ -1,8 +1,5 @@
package jp.juggler.util package jp.juggler.util
import androidx.collection.SparseArrayCompat
import jp.juggler.subwaytooter.util.CharacterGroup
import java.util.ArrayList import java.util.ArrayList
class WordTrieTree { class WordTrieTree {

View File

@ -2,5 +2,5 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android" <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:shape="rectangle"
> >
<solid android:color="@color/Dark_colorBackground" /> <solid android:color="?attr/colorWindowBackground" />
</shape> </shape>

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="@color/Light_colorBackground" />
</shape>

View File

@ -900,5 +900,29 @@
<View style="@style/setting_divider"/> <View style="@style/setting_divider"/>
<TextView
style="@style/setting_row_label"
android:text="@string/switch_button_color"
/>
<LinearLayout style="@style/setting_row_form">
<Button
android:id="@+id/btnSwitchButtonColorEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/edit"
android:textAllCaps="false"
/>
<Button
android:id="@+id/btnSwitchButtonColorReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reset"
android:textAllCaps="false"
/>
</LinearLayout>
</LinearLayout> </LinearLayout>
</ScrollView> </ScrollView>

View File

@ -921,5 +921,6 @@
<string name="additional_buttons_position">追加ボタンの位置(アプリ再起動が必要)</string> <string name="additional_buttons_position">追加ボタンの位置(アプリ再起動が必要)</string>
<string name="top"></string> <string name="top"></string>
<string name="bottom"></string> <string name="bottom"></string>
<string name="switch_button_color">Switch button color</string>
</resources> </resources>

View File

@ -91,4 +91,7 @@
<!-- VectorDrawableのXMLに指定するfillColor --> <!-- VectorDrawableのXMLに指定するfillColor -->
<attr name="colorVectorDrawable" format="color"/> <attr name="colorVectorDrawable" format="color"/>
<!-- @drawable/window_background_light と @drawable/window_background に指定した色をgetAttributeColorで取得したい -->
<attr name="colorWindowBackground" format="color"/>
</resources> </resources>

View File

@ -914,5 +914,6 @@
<string name="additional_buttons_position">Additional buttons position (app restart required)</string> <string name="additional_buttons_position">Additional buttons position (app restart required)</string>
<string name="top">Top</string> <string name="top">Top</string>
<string name="bottom">Bottom</string> <string name="bottom">Bottom</string>
<string name="switch_button_color">Switch button color</string>
</resources> </resources>

View File

@ -3,7 +3,7 @@
<!-- light theme --> <!-- light theme -->
<style name="AppTheme.Light" parent="Theme.AppCompat.Light.DarkActionBar"> <style name="AppTheme.Light" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowBackground">@drawable/window_background_light</item> <item name="android:windowBackground">@drawable/window_background</item>
<item name="android:textColor">?attr/colorContentText</item> <item name="android:textColor">?attr/colorContentText</item>
<item name="colorPrimary">@color/Light_colorPrimary</item> <item name="colorPrimary">@color/Light_colorPrimary</item>
@ -45,7 +45,7 @@
<item name="colorButtonBgCw">@color/Light_colorButtonBgCw</item> <item name="colorButtonBgCw">@color/Light_colorButtonBgCw</item>
<item name="colorRefreshErrorBg">@color/Light_colorRefreshErrorBg</item> <item name="colorRefreshErrorBg">@color/Light_colorRefreshErrorBg</item>
<item name="colorVectorDrawable">@color/Light_colorVectorDrawable</item> <item name="colorVectorDrawable">@color/Light_colorVectorDrawable</item>
<item name="colorWindowBackground">@color/Light_colorBackground</item>
</style> </style>
@ -57,7 +57,7 @@
<!-- dark theme --> <!-- dark theme -->
<style name="AppTheme.Dark" parent="Theme.AppCompat"> <style name="AppTheme.Dark" parent="Theme.AppCompat">
<item name="android:windowBackground">@drawable/window_background_dark</item> <item name="android:windowBackground">@drawable/window_background</item>
<item name="android:textColor">?attr/colorContentText</item> <item name="android:textColor">?attr/colorContentText</item>
<item name="android:navigationBarColor">@color/Dark_colorColumnStripBackground</item> <item name="android:navigationBarColor">@color/Dark_colorColumnStripBackground</item>
@ -102,6 +102,7 @@
<item name="colorRefreshErrorBg">@color/Dark_colorRefreshErrorBg</item> <item name="colorRefreshErrorBg">@color/Dark_colorRefreshErrorBg</item>
<item name="colorVectorDrawable">@color/Dark_colorVectorDrawable</item> <item name="colorVectorDrawable">@color/Dark_colorVectorDrawable</item>
<item name="colorWindowBackground">@color/Dark_colorBackground</item>
</style> </style>