Files
Simple-Notes/app/src/main/kotlin/com/simplemobiletools/notes/activities/WidgetConfigureActivity.kt
2017-01-06 22:42:46 +01:00

159 lines
5.2 KiB
Kotlin

package com.simplemobiletools.notes.activities
import android.app.Activity
import android.appwidget.AppWidgetManager
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.TypedValue
import android.widget.RemoteViews
import android.widget.SeekBar
import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.extensions.getTextSize
import com.simplemobiletools.notes.helpers.Config
import com.simplemobiletools.notes.helpers.MyWidgetProvider
import kotlinx.android.synthetic.main.widget_config.*
import yuku.ambilwarna.AmbilWarnaDialog
class WidgetConfigureActivity : AppCompatActivity() {
private var mBgAlpha = 0f
private var mWidgetId = 0
private var mBgColor = 0
private var mBgColorWithoutTransparency = 0
private var mTextColor = 0
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setResult(RESULT_CANCELED)
setContentView(R.layout.widget_config)
initVariables()
val extras = intent.extras
if (extras != null)
mWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID)
if (mWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
finish()
config_save.setOnClickListener { saveConfig() }
config_bg_color.setOnClickListener { pickBackgroundColor() }
config_text_color.setOnClickListener { pickTextColor() }
}
override fun onResume() {
super.onResume()
notes_view.setTextSize(TypedValue.COMPLEX_UNIT_PX, applicationContext.getTextSize())
}
private fun initVariables() {
val config = Config.newInstance(this)
mBgColor = config.widgetBgColor
if (mBgColor == 1) {
mBgColor = Color.BLACK
mBgAlpha = .2f
} else {
mBgAlpha = Color.alpha(mBgColor) / 255.toFloat()
}
mBgColorWithoutTransparency = Color.rgb(Color.red(mBgColor), Color.green(mBgColor), Color.blue(mBgColor))
config_bg_seekbar.apply {
setOnSeekBarChangeListener(bgSeekbarChangeListener)
progress = (mBgAlpha * 100).toInt()
}
updateBackgroundColor()
mTextColor = config.widgetTextColor
updateTextColor()
}
fun saveConfig() {
val views = RemoteViews(packageName, R.layout.activity_main)
views.setInt(R.id.notes_view, "setBackgroundColor", mBgColor)
AppWidgetManager.getInstance(this).updateAppWidget(mWidgetId, views)
storeWidgetBackground()
requestWidgetUpdate()
Intent().apply {
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId)
setResult(Activity.RESULT_OK, this)
}
finish()
}
private fun storeWidgetBackground() {
Config.newInstance(this).apply {
widgetBgColor = mBgColor
widgetTextColor = mTextColor
}
}
private fun requestWidgetUpdate() {
Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider::class.java).apply {
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf(mWidgetId))
sendBroadcast(this)
}
}
private fun updateBackgroundColor() {
mBgColor = adjustAlpha(mBgColorWithoutTransparency, mBgAlpha)
notes_view.setBackgroundColor(mBgColor)
config_bg_color.setBackgroundColor(mBgColor)
config_save.setBackgroundColor(mBgColor)
}
private fun updateTextColor() {
config_text_color.setBackgroundColor(mTextColor)
config_save.setTextColor(mTextColor)
notes_view.setTextColor(mTextColor)
}
fun pickBackgroundColor() {
AmbilWarnaDialog(this, mBgColorWithoutTransparency, object : AmbilWarnaDialog.OnAmbilWarnaListener {
override fun onCancel(dialog: AmbilWarnaDialog) {
}
override fun onOk(dialog: AmbilWarnaDialog, color: Int) {
mBgColorWithoutTransparency = color
updateBackgroundColor()
}
}).show()
}
fun pickTextColor() {
AmbilWarnaDialog(this, mTextColor, object : AmbilWarnaDialog.OnAmbilWarnaListener {
override fun onCancel(dialog: AmbilWarnaDialog) {
}
override fun onOk(dialog: AmbilWarnaDialog, color: Int) {
mTextColor = color
updateTextColor()
}
}).show()
}
private val bgSeekbarChangeListener = object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
mBgAlpha = progress.toFloat() / 100.toFloat()
updateBackgroundColor()
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
}
}
private fun adjustAlpha(color: Int, factor: Float): Int {
val alpha = Math.round(Color.alpha(color) * factor)
val red = Color.red(color)
val green = Color.green(color)
val blue = Color.blue(color)
return Color.argb(alpha, red, green, blue)
}
}