From 67fc0458176c78a1c8435d928a78b02a5969abc8 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 27 Nov 2017 17:35:18 +0100 Subject: [PATCH] some more work on the color customization themes --- .../commons/activities/BaseSimpleActivity.kt | 21 +- .../commons/dialogs/LineColorPickerDialog.kt | 14 +- .../commons/extensions/Activity-themes.kt | 198 ++++ .../commons/extensions/Int.kt | 5 +- .../commons/helpers/Constants.kt | 2 +- .../commons/views/LineColorPicker.kt | 2 +- commons/src/main/res/values/styles.xml | 1040 ++++++++++++++++- 7 files changed, 1249 insertions(+), 33 deletions(-) create mode 100644 commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity-themes.kt diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt index ea4ae5452..ee2909f6e 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/activities/BaseSimpleActivity.kt @@ -4,8 +4,6 @@ import android.annotation.SuppressLint import android.annotation.TargetApi import android.app.Activity import android.content.Intent -import android.content.res.Resources -import android.graphics.Color import android.graphics.drawable.ColorDrawable import android.net.Uri import android.os.Build @@ -14,9 +12,7 @@ import android.provider.DocumentsContract import android.support.v4.app.ActivityCompat import android.support.v4.util.Pair import android.support.v7.app.AppCompatActivity -import android.text.SpannableString -import android.text.style.AbsoluteSizeSpan -import android.view.Menu +import android.text.Html import android.view.MenuItem import com.simplemobiletools.commons.R import com.simplemobiletools.commons.asynctasks.CopyMoveTask @@ -40,6 +36,7 @@ open class BaseSimpleActivity : AppCompatActivity() { } override fun onCreate(savedInstanceState: Bundle?) { + setTheme(getThemeId()) super.onCreate(savedInstanceState) if (!packageName.startsWith("com.simplemobiletools.", true)) { if ((0..50).random() == 10 || baseConfig.appRunCount % 100 == 0) { @@ -54,7 +51,6 @@ open class BaseSimpleActivity : AppCompatActivity() { override fun onResume() { super.onResume() updateBackgroundColor() - updateActionbarColor() } override fun onStop() { @@ -81,6 +77,7 @@ open class BaseSimpleActivity : AppCompatActivity() { fun updateActionbarColor(color: Int = baseConfig.primaryColor) { supportActionBar?.setBackgroundDrawable(ColorDrawable(color)) + supportActionBar?.title = Html.fromHtml("${supportActionBar?.title}") updateStatusbarColor(color) } @@ -90,18 +87,6 @@ open class BaseSimpleActivity : AppCompatActivity() { } } - fun updateMenuTextSize(resources: Resources, menu: Menu) { - val textSize = resources.getDimension(R.dimen.normal_text_size).toInt() - (0 until menu.size()) - .map { menu.getItem(it) } - .forEach { - SpannableString(it.title).apply { - setSpan(AbsoluteSizeSpan(textSize, false), 0, length, 0) - it.title = this - } - } - } - override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { super.onActivityResult(requestCode, resultCode, resultData) if (requestCode == OPEN_DOCUMENT_TREE && resultCode == Activity.RESULT_OK && resultData != null) { diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/LineColorPickerDialog.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/LineColorPickerDialog.kt index 4a37fdd88..a5892101d 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/LineColorPickerDialog.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/LineColorPickerDialog.kt @@ -13,6 +13,7 @@ import java.util.* class LineColorPickerDialog(val activity: BaseSimpleActivity, val callback: (wasChanged: Boolean, color: Int) -> Unit) { private val PRIMARY_COLORS_COUNT = 19 + private val DEFAULT_COLOR_VALUE = -689152 private val DEFAULT_PRIMARY_COLOR_INDEX = 14 private val DEFAULT_SECONDARY_COLOR_INDEX = 6 @@ -29,7 +30,7 @@ class LineColorPickerDialog(val activity: BaseSimpleActivity, val callback: (was val secondaryColors = getColorsForIndex(index) secondary_line_color_picker.updateColors(secondaryColors) dialog?.window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) - activity.updateActionbarColor(secondary_line_color_picker.selectedColor()) + activity.updateActionbarColor(secondary_line_color_picker.getCurrentColor()) } } @@ -52,21 +53,28 @@ class LineColorPickerDialog(val activity: BaseSimpleActivity, val callback: (was } private fun getColorIndexes(color: Int): Pair { + if (color == DEFAULT_COLOR_VALUE) { + return getDefaultColorPair() + } + for (i in 0 until PRIMARY_COLORS_COUNT) { val colors = getColorsForIndex(i) val size = colors.size (0 until size).filter { color == colors[it] } .forEach { return Pair(i, it) } } - return Pair(DEFAULT_PRIMARY_COLOR_INDEX, DEFAULT_SECONDARY_COLOR_INDEX) + + return getDefaultColorPair() } + private fun getDefaultColorPair() = Pair(DEFAULT_PRIMARY_COLOR_INDEX, DEFAULT_SECONDARY_COLOR_INDEX) + private fun dialogDismissed() { callback(false, 0) } private fun dialogConfirmed() { - val color = view.secondary_line_color_picker.selectedColor() + val color = view.secondary_line_color_picker.getCurrentColor() callback(true, color) } diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity-themes.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity-themes.kt new file mode 100644 index 000000000..b9bbec063 --- /dev/null +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Activity-themes.kt @@ -0,0 +1,198 @@ +package com.simplemobiletools.commons.extensions + +import android.app.Activity +import com.simplemobiletools.commons.R + +fun Activity.getThemeId() = when (baseConfig.primaryColor) { + -12846 -> R.style.AppTheme_Red_100 + -1074534 -> R.style.AppTheme_Red_200 + -1739917 -> R.style.AppTheme_Red_300 + -1092784 -> R.style.AppTheme_Red_400 + -769226 -> R.style.AppTheme_Red_500 + -1754827 -> R.style.AppTheme_Red_600 + -2937041 -> R.style.AppTheme_Red_700 + -3790808 -> R.style.AppTheme_Red_800 + -4776932 -> R.style.AppTheme_Red_900 + + -476208 -> R.style.AppTheme_Pink_100 + -749647 -> R.style.AppTheme_Pink_200 + -1023342 -> R.style.AppTheme_Pink_300 + -1294214 -> R.style.AppTheme_Pink_400 + -1499549 -> R.style.AppTheme_Pink_500 + -2614432 -> R.style.AppTheme_Pink_600 + -4056997 -> R.style.AppTheme_Pink_700 + -5434281 -> R.style.AppTheme_Pink_800 + -7860657 -> R.style.AppTheme_Pink_900 + + -1982745 -> R.style.AppTheme_Purple_100 + -3238952 -> R.style.AppTheme_Purple_200 + -4560696 -> R.style.AppTheme_Purple_300 + -5552196 -> R.style.AppTheme_Purple_400 + -6543440 -> R.style.AppTheme_Purple_500 + -7461718 -> R.style.AppTheme_Purple_600 + -8708190 -> R.style.AppTheme_Purple_700 + -9823334 -> R.style.AppTheme_Purple_800 + -11922292 -> R.style.AppTheme_Purple_900 + + -3029783 -> R.style.AppTheme_Deep_Purple_100 + -5005861 -> R.style.AppTheme_Deep_Purple_200 + -6982195 -> R.style.AppTheme_Deep_Purple_300 + -8497214 -> R.style.AppTheme_Deep_Purple_400 + -10011977 -> R.style.AppTheme_Deep_Purple_500 + -10603087 -> R.style.AppTheme_Deep_Purple_600 + -11457112 -> R.style.AppTheme_Deep_Purple_700 + -12245088 -> R.style.AppTheme_Deep_Purple_800 + -13558894 -> R.style.AppTheme_Deep_Purple_900 + + -3814679 -> R.style.AppTheme_Indigo_100 + -6313766 -> R.style.AppTheme_Indigo_200 + -8812853 -> R.style.AppTheme_Indigo_300 + -10720320 -> R.style.AppTheme_Indigo_400 + -12627531 -> R.style.AppTheme_Indigo_500 + -13022805 -> R.style.AppTheme_Indigo_600 + -13615201 -> R.style.AppTheme_Indigo_700 + -14142061 -> R.style.AppTheme_Indigo_800 + -15064194 -> R.style.AppTheme_Indigo_900 + + -4464901 -> R.style.AppTheme_Blue_100 + -7288071 -> R.style.AppTheme_Blue_200 + -10177034 -> R.style.AppTheme_Blue_300 + -12409355 -> R.style.AppTheme_Blue_400 + -14575885 -> R.style.AppTheme_Blue_500 + -14776091 -> R.style.AppTheme_Blue_600 + -15108398 -> R.style.AppTheme_Blue_700 + -15374912 -> R.style.AppTheme_Blue_800 + -15906911 -> R.style.AppTheme_Blue_900 + + -4987396 -> R.style.AppTheme_Light_Blue_100 + -8268550 -> R.style.AppTheme_Light_Blue_200 + -11549705 -> R.style.AppTheme_Light_Blue_300 + -14043396 -> R.style.AppTheme_Light_Blue_400 + -16537100 -> R.style.AppTheme_Light_Blue_500 + -16540699 -> R.style.AppTheme_Light_Blue_600 + -16611119 -> R.style.AppTheme_Light_Blue_700 + -16615491 -> R.style.AppTheme_Light_Blue_800 + -16689253 -> R.style.AppTheme_Light_Blue_900 + + -5051406 -> R.style.AppTheme_Cyan_100 + -8331542 -> R.style.AppTheme_Cyan_200 + -11677471 -> R.style.AppTheme_Cyan_300 + -14235942 -> R.style.AppTheme_Cyan_400 + -16728876 -> R.style.AppTheme_Cyan_500 + -16732991 -> R.style.AppTheme_Cyan_600 + -16738393 -> R.style.AppTheme_Cyan_700 + -16743537 -> R.style.AppTheme_Cyan_800 + -16752540 -> R.style.AppTheme_Cyan_900 + + -5054501 -> R.style.AppTheme_Teal_100 + -8336444 -> R.style.AppTheme_Teal_200 + -11684180 -> R.style.AppTheme_Teal_300 + -14244198 -> R.style.AppTheme_Teal_400 + -16738680 -> R.style.AppTheme_Teal_500 + -16742021 -> R.style.AppTheme_Teal_600 + -16746133 -> R.style.AppTheme_Teal_700 + -16750244 -> R.style.AppTheme_Teal_800 + -16757440 -> R.style.AppTheme_Teal_900 + + -3610935 -> R.style.AppTheme_Green_100 + -5908825 -> R.style.AppTheme_Green_200 + -8271996 -> R.style.AppTheme_Green_300 + -10044566 -> R.style.AppTheme_Green_400 + -11751600 -> R.style.AppTheme_Green_500 + -12345273 -> R.style.AppTheme_Green_600 + -13070788 -> R.style.AppTheme_Green_700 + -13730510 -> R.style.AppTheme_Green_800 + -14983648 -> R.style.AppTheme_Green_900 + + -2298424 -> R.style.AppTheme_Light_Green_100 + -3808859 -> R.style.AppTheme_Light_Green_200 + -5319295 -> R.style.AppTheme_Light_Green_300 + -6501275 -> R.style.AppTheme_Light_Green_400 + -7617718 -> R.style.AppTheme_Light_Green_500 + -8604862 -> R.style.AppTheme_Light_Green_600 + -9920712 -> R.style.AppTheme_Light_Green_700 + -11171025 -> R.style.AppTheme_Light_Green_800 + -13407970 -> R.style.AppTheme_Light_Green_900 + + -985917 -> R.style.AppTheme_Lime_100 + -1642852 -> R.style.AppTheme_Lime_200 + -2300043 -> R.style.AppTheme_Lime_300 + -2825897 -> R.style.AppTheme_Lime_400 + -3285959 -> R.style.AppTheme_Lime_500 + -4142541 -> R.style.AppTheme_Lime_600 + -5983189 -> R.style.AppTheme_Lime_700 + -6382300 -> R.style.AppTheme_Lime_800 + -8227049 -> R.style.AppTheme_Lime_900 + + -1596 -> R.style.AppTheme_Yellow_100 + -2672 -> R.style.AppTheme_Yellow_200 + -3722 -> R.style.AppTheme_Yellow_300 + -4520 -> R.style.AppTheme_Yellow_400 + -5317 -> R.style.AppTheme_Yellow_500 + -141259 -> R.style.AppTheme_Yellow_600 + -278483 -> R.style.AppTheme_Yellow_700 + -415707 -> R.style.AppTheme_Yellow_800 + -688361 -> R.style.AppTheme_Yellow_900 + + -4941 -> R.style.AppTheme_Amber_100 + -8062 -> R.style.AppTheme_Amber_200 + -10929 -> R.style.AppTheme_Amber_300 + -13784 -> R.style.AppTheme_Amber_400 + -16121 -> R.style.AppTheme_Amber_500 + -19712 -> R.style.AppTheme_Amber_600 + -24576 -> R.style.AppTheme_Amber_700 + -28928 -> R.style.AppTheme_Amber_800 + -37120 -> R.style.AppTheme_Amber_900 + + -8014 -> R.style.AppTheme_Orange_100 + -13184 -> R.style.AppTheme_Orange_200 + -18611 -> R.style.AppTheme_Orange_300 + -22746 -> R.style.AppTheme_Orange_400 + -26624 -> R.style.AppTheme_Orange_500 + -291840 -> R.style.AppTheme_Orange_600 + -689152 -> R.style.AppTheme_Orange_700 + -1086464 -> R.style.AppTheme_Orange_800 + -1683200 -> R.style.AppTheme_Orange_900 + + -13124 -> R.style.AppTheme_Deep_Orange_100 + -21615 -> R.style.AppTheme_Deep_Orange_200 + -30107 -> R.style.AppTheme_Deep_Orange_300 + -36797 -> R.style.AppTheme_Deep_Orange_400 + -43230 -> R.style.AppTheme_Deep_Orange_500 + -765666 -> R.style.AppTheme_Deep_Orange_600 + -1684967 -> R.style.AppTheme_Deep_Orange_700 + -2604267 -> R.style.AppTheme_Deep_Orange_800 + -4246004 -> R.style.AppTheme_Deep_Orange_900 + + -2634552 -> R.style.AppTheme_Brown_100 + -4412764 -> R.style.AppTheme_Brown_200 + -6190977 -> R.style.AppTheme_Brown_300 + -7508381 -> R.style.AppTheme_Brown_400 + -8825528 -> R.style.AppTheme_Brown_500 + -9614271 -> R.style.AppTheme_Brown_600 + -10665929 -> R.style.AppTheme_Brown_700 + -11652050 -> R.style.AppTheme_Brown_800 + -12703965 -> R.style.AppTheme_Brown_900 + + -1 -> R.style.AppTheme_Grey_100 + -1118482 -> R.style.AppTheme_Grey_200 + -2039584 -> R.style.AppTheme_Grey_300 + -4342339 -> R.style.AppTheme_Grey_400 + -6381922 -> R.style.AppTheme_Grey_500 + -9079435 -> R.style.AppTheme_Grey_600 + -10395295 -> R.style.AppTheme_Grey_700 + -12434878 -> R.style.AppTheme_Grey_800 + -16777216 -> R.style.AppTheme_Grey_900 + + -3155748 -> R.style.AppTheme_Blue_Grey_100 + -5194811 -> R.style.AppTheme_Blue_Grey_200 + -7297874 -> R.style.AppTheme_Blue_Grey_300 + -8875876 -> R.style.AppTheme_Blue_Grey_400 + -10453621 -> R.style.AppTheme_Blue_Grey_500 + -11243910 -> R.style.AppTheme_Blue_Grey_600 + -12232092 -> R.style.AppTheme_Blue_Grey_700 + -13154481 -> R.style.AppTheme_Blue_Grey_800 + -14273992 -> R.style.AppTheme_Blue_Grey_900 + + else -> throw RuntimeException("Unknown theme id ${baseConfig.primaryColor}") +} diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Int.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Int.kt index fbb6fc588..f4c3982d0 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Int.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/extensions/Int.kt @@ -4,10 +4,13 @@ import android.graphics.Color import java.util.* fun Int.getContrastColor(): Int { + val DARK_GREY = -13421773 val y = (299 * Color.red(this) + 587 * Color.green(this) + 114 * Color.blue(this)) / 1000 - return if (y >= 128) Color.BLACK else Color.WHITE + return if (y >= 186) DARK_GREY else Color.WHITE } +fun Int.toHex() = String.format("#%06X", 0xFFFFFF and this) + fun Int.adjustAlpha(factor: Float): Int { val alpha = Math.round(Color.alpha(this) * factor) val red = Color.red(this) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt index 6911d1214..e75bbc8c9 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/Constants.kt @@ -15,7 +15,7 @@ val SD_CARD_PATH = "sd_card_path_2" val INTERNAL_STORAGE_PATH = "internal_storage_path" val TEXT_COLOR = "text_color" val BACKGROUND_COLOR = "background_color" -val PRIMARY_COLOR = "primary_color" +val PRIMARY_COLOR = "primary_color_2" val CUSTOM_TEXT_COLOR = "custom_text_color" val CUSTOM_BACKGROUND_COLOR = "custom_background_color" val CUSTOM_PRIMARY_COLOR = "custom_primary_color" diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/views/LineColorPicker.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/views/LineColorPicker.kt index 004e2fdde..b12852879 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/views/LineColorPicker.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/views/LineColorPicker.kt @@ -66,7 +66,7 @@ class LineColorPicker(context: Context, attrs: AttributeSet) : LinearLayout(cont updateItemMargin(lastColorIndex, false) } - fun selectedColor() = colors[lastColorIndex] + fun getCurrentColor() = colors[lastColorIndex] private fun initColorPicker() { removeAllViews() diff --git a/commons/src/main/res/values/styles.xml b/commons/src/main/res/values/styles.xml index 537b8e368..fe2a31ae7 100644 --- a/commons/src/main/res/values/styles.xml +++ b/commons/src/main/res/values/styles.xml @@ -1,10 +1,10 @@ - - - - @@ -27,8 +22,8 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +