2017-10-22 15:37:36 +02:00
|
|
|
package com.simplemobiletools.calculator.activities
|
|
|
|
|
|
|
|
import android.app.Activity
|
|
|
|
import android.appwidget.AppWidgetManager
|
|
|
|
import android.content.Intent
|
2022-07-17 22:11:23 +02:00
|
|
|
import android.content.res.ColorStateList
|
2017-10-22 15:37:36 +02:00
|
|
|
import android.graphics.Color
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.widget.RemoteViews
|
|
|
|
import android.widget.SeekBar
|
2022-12-21 21:16:05 +01:00
|
|
|
import android.widget.TextView
|
2017-11-05 16:25:01 +01:00
|
|
|
import com.simplemobiletools.calculator.R
|
2023-06-26 19:22:56 +02:00
|
|
|
import com.simplemobiletools.calculator.databinding.WidgetConfigBinding
|
2017-11-05 17:48:49 +01:00
|
|
|
import com.simplemobiletools.calculator.extensions.config
|
2023-06-26 19:22:56 +02:00
|
|
|
import com.simplemobiletools.calculator.extensions.viewBinding
|
2017-11-05 16:25:01 +01:00
|
|
|
import com.simplemobiletools.calculator.helpers.MyWidgetProvider
|
2017-10-22 15:37:36 +02:00
|
|
|
import com.simplemobiletools.commons.dialogs.ColorPickerDialog
|
2022-06-29 23:13:28 +02:00
|
|
|
import com.simplemobiletools.commons.dialogs.FeatureLockedDialog
|
2022-03-29 15:23:49 +02:00
|
|
|
import com.simplemobiletools.commons.extensions.*
|
2018-05-15 23:36:12 +02:00
|
|
|
import com.simplemobiletools.commons.helpers.IS_CUSTOMIZING_COLORS
|
2017-10-22 15:37:36 +02:00
|
|
|
|
2017-11-29 22:39:39 +01:00
|
|
|
class WidgetConfigureActivity : SimpleActivity() {
|
2020-05-24 22:52:21 +02:00
|
|
|
private var mBgAlpha = 0f
|
2017-11-05 17:48:49 +01:00
|
|
|
private var mWidgetId = 0
|
2020-05-24 22:52:21 +02:00
|
|
|
private var mBgColor = 0
|
2017-11-05 17:48:49 +01:00
|
|
|
private var mTextColor = 0
|
2020-05-24 22:52:21 +02:00
|
|
|
private var mBgColorWithoutTransparency = 0
|
2022-06-29 23:13:28 +02:00
|
|
|
private var mFeatureLockedDialog: FeatureLockedDialog? = null
|
2017-10-22 15:37:36 +02:00
|
|
|
|
2023-06-26 19:22:56 +02:00
|
|
|
private val binding by viewBinding(WidgetConfigBinding::inflate)
|
|
|
|
|
2017-10-22 15:37:36 +02:00
|
|
|
public override fun onCreate(savedInstanceState: Bundle?) {
|
2017-11-29 22:39:39 +01:00
|
|
|
useDynamicTheme = false
|
2017-10-22 15:37:36 +02:00
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setResult(Activity.RESULT_CANCELED)
|
2023-06-26 19:22:56 +02:00
|
|
|
setContentView(binding.root)
|
2017-10-22 15:37:36 +02:00
|
|
|
initVariables()
|
|
|
|
|
2022-05-20 21:16:05 +02:00
|
|
|
val isCustomizingColors = intent.extras?.getBoolean(IS_CUSTOMIZING_COLORS) ?: false
|
2018-05-15 23:36:12 +02:00
|
|
|
mWidgetId = intent.extras?.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID) ?: AppWidgetManager.INVALID_APPWIDGET_ID
|
2017-10-22 15:37:36 +02:00
|
|
|
|
2022-05-20 21:16:05 +02:00
|
|
|
if (mWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID && !isCustomizingColors) {
|
2017-10-22 15:37:36 +02:00
|
|
|
finish()
|
2018-05-15 23:36:12 +02:00
|
|
|
}
|
2017-11-05 17:48:49 +01:00
|
|
|
|
2023-06-26 19:22:56 +02:00
|
|
|
binding.configSave.setOnClickListener { saveConfig() }
|
|
|
|
binding.configBgColor.setOnClickListener { pickBackgroundColor() }
|
|
|
|
binding.configTextColor.setOnClickListener { pickTextColor() }
|
2022-05-20 21:12:53 +02:00
|
|
|
|
|
|
|
val primaryColor = getProperPrimaryColor()
|
2023-06-26 19:22:56 +02:00
|
|
|
binding.configBgSeekbar.setColors(mTextColor, primaryColor, primaryColor)
|
2022-05-20 21:16:05 +02:00
|
|
|
|
|
|
|
if (!isCustomizingColors && !isOrWasThankYouInstalled()) {
|
2022-06-29 23:13:28 +02:00
|
|
|
mFeatureLockedDialog = FeatureLockedDialog(this) {
|
2022-05-20 21:16:05 +02:00
|
|
|
if (!isOrWasThankYouInstalled()) {
|
|
|
|
finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
window.decorView.setBackgroundColor(0)
|
|
|
|
|
2022-06-29 23:13:28 +02:00
|
|
|
if (mFeatureLockedDialog != null && isOrWasThankYouInstalled()) {
|
|
|
|
mFeatureLockedDialog?.dismissDialog()
|
2022-05-20 21:16:05 +02:00
|
|
|
}
|
2017-10-22 15:37:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun initVariables() {
|
2017-11-05 17:48:49 +01:00
|
|
|
mBgColor = config.widgetBgColor
|
2020-05-24 22:52:21 +02:00
|
|
|
mBgAlpha = Color.alpha(mBgColor) / 255.toFloat()
|
2017-10-22 15:37:36 +02:00
|
|
|
|
2023-06-26 19:22:56 +02:00
|
|
|
binding.configCalc.btnReset.beVisible()
|
2017-10-22 15:37:36 +02:00
|
|
|
mBgColorWithoutTransparency = Color.rgb(Color.red(mBgColor), Color.green(mBgColor), Color.blue(mBgColor))
|
2023-06-26 19:22:56 +02:00
|
|
|
binding.configBgSeekbar.setOnSeekBarChangeListener(seekbarChangeListener)
|
|
|
|
binding.configBgSeekbar.progress = (mBgAlpha * 100).toInt()
|
2017-10-22 15:37:36 +02:00
|
|
|
updateBackgroundColor()
|
|
|
|
|
2017-11-05 17:48:49 +01:00
|
|
|
mTextColor = config.widgetTextColor
|
2023-06-26 19:22:56 +02:00
|
|
|
if (mTextColor == resources.getColor(com.simplemobiletools.commons.R.color.default_widget_text_color, theme) && config.isUsingSystemTheme) {
|
|
|
|
mTextColor = resources.getColor(com.simplemobiletools.commons.R.color.you_primary_color, theme)
|
2022-12-21 14:41:51 +01:00
|
|
|
}
|
|
|
|
|
2017-10-22 15:37:36 +02:00
|
|
|
updateTextColor()
|
|
|
|
|
2023-06-26 19:22:56 +02:00
|
|
|
binding.configCalc.formula.text = "15,937*5"
|
|
|
|
binding.configCalc.result.text = "79,685"
|
2017-10-22 15:37:36 +02:00
|
|
|
}
|
|
|
|
|
2017-11-05 17:48:49 +01:00
|
|
|
private fun saveConfig() {
|
2021-05-13 14:54:25 +02:00
|
|
|
val appWidgetManager = AppWidgetManager.getInstance(this) ?: return
|
2020-05-24 23:04:16 +02:00
|
|
|
val views = RemoteViews(packageName, R.layout.widget).apply {
|
2023-06-26 19:22:56 +02:00
|
|
|
applyColorFilter(binding.widgetBackground.id, mBgColor)
|
2020-05-24 23:04:16 +02:00
|
|
|
}
|
|
|
|
|
2017-10-22 15:37:36 +02:00
|
|
|
appWidgetManager.updateAppWidget(mWidgetId, views)
|
|
|
|
|
2017-11-05 17:48:49 +01:00
|
|
|
storeWidgetColors()
|
2017-10-22 15:37:36 +02:00
|
|
|
requestWidgetUpdate()
|
|
|
|
|
2017-11-05 17:48:49 +01:00
|
|
|
Intent().apply {
|
|
|
|
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId)
|
|
|
|
setResult(Activity.RESULT_OK, this)
|
|
|
|
}
|
2017-10-22 15:37:36 +02:00
|
|
|
finish()
|
|
|
|
}
|
|
|
|
|
2017-11-05 17:48:49 +01:00
|
|
|
private fun storeWidgetColors() {
|
|
|
|
config.apply {
|
|
|
|
widgetBgColor = mBgColor
|
|
|
|
widgetTextColor = mTextColor
|
|
|
|
}
|
2017-10-22 15:37:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun requestWidgetUpdate() {
|
2017-11-05 17:48:49 +01:00
|
|
|
Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider::class.java).apply {
|
|
|
|
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf(mWidgetId))
|
|
|
|
sendBroadcast(this)
|
|
|
|
}
|
2017-10-22 15:37:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateBackgroundColor() {
|
2017-11-05 17:48:49 +01:00
|
|
|
mBgColor = mBgColorWithoutTransparency.adjustAlpha(mBgAlpha)
|
2023-06-26 19:22:56 +02:00
|
|
|
binding.widgetBackground.applyColorFilter(mBgColor)
|
|
|
|
binding.configBgColor.setFillWithStroke(mBgColor, mBgColor)
|
|
|
|
binding.configSave.backgroundTintList = ColorStateList.valueOf(getProperPrimaryColor())
|
2017-10-22 15:37:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateTextColor() {
|
2023-06-26 19:22:56 +02:00
|
|
|
binding.configTextColor.setFillWithStroke(mTextColor, mTextColor)
|
2017-10-22 15:37:36 +02:00
|
|
|
|
2022-03-29 11:01:22 +02:00
|
|
|
val viewIds = intArrayOf(
|
|
|
|
R.id.btn_0, R.id.btn_1, R.id.btn_2, R.id.btn_3, R.id.btn_4, R.id.btn_5, R.id.btn_6, R.id.btn_7, R.id.btn_8,
|
2020-05-24 22:52:21 +02:00
|
|
|
R.id.btn_9, R.id.btn_percent, R.id.btn_power, R.id.btn_root, R.id.btn_clear, R.id.btn_reset, R.id.btn_divide, R.id.btn_multiply,
|
2022-03-29 11:01:22 +02:00
|
|
|
R.id.btn_minus, R.id.btn_plus, R.id.btn_decimal, R.id.btn_equals
|
|
|
|
)
|
2022-12-21 16:59:26 +01:00
|
|
|
|
2023-06-26 19:22:56 +02:00
|
|
|
|
|
|
|
binding.configCalc.result.setTextColor(mTextColor)
|
|
|
|
binding.configCalc.formula.setTextColor(mTextColor)
|
|
|
|
binding.configSave.setTextColor(getProperPrimaryColor().getContrastColor())
|
2017-10-22 15:37:36 +02:00
|
|
|
|
2017-11-05 17:48:49 +01:00
|
|
|
viewIds.forEach {
|
2022-12-21 21:16:05 +01:00
|
|
|
(findViewById<TextView>(it)).setTextColor(mTextColor)
|
|
|
|
(findViewById<TextView>(it)).background = null
|
2017-10-22 15:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 23:14:57 +02:00
|
|
|
private fun pickBackgroundColor() {
|
|
|
|
ColorPickerDialog(this, mBgColorWithoutTransparency) { wasPositivePressed, color ->
|
|
|
|
if (wasPositivePressed) {
|
|
|
|
mBgColorWithoutTransparency = color
|
|
|
|
updateBackgroundColor()
|
|
|
|
}
|
2017-10-22 15:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 23:14:57 +02:00
|
|
|
private fun pickTextColor() {
|
|
|
|
ColorPickerDialog(this, mTextColor) { wasPositivePressed, color ->
|
|
|
|
if (wasPositivePressed) {
|
|
|
|
mTextColor = color
|
|
|
|
updateTextColor()
|
|
|
|
}
|
2017-10-22 15:37:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 17:48:49 +01:00
|
|
|
private val seekbarChangeListener = object : SeekBar.OnSeekBarChangeListener {
|
|
|
|
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
|
|
|
|
mBgAlpha = progress.toFloat() / 100.toFloat()
|
|
|
|
updateBackgroundColor()
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:52:21 +02:00
|
|
|
override fun onStartTrackingTouch(seekBar: SeekBar) {}
|
2017-11-05 17:48:49 +01:00
|
|
|
|
2020-05-24 22:52:21 +02:00
|
|
|
override fun onStopTrackingTouch(seekBar: SeekBar) {}
|
2017-10-22 15:37:36 +02:00
|
|
|
}
|
|
|
|
}
|