From 1949e433c730f81dbf2b2a6bf5cfc3f5d5f8a80c Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 11 Mar 2018 21:12:46 +0100 Subject: [PATCH] adding some core widget related things --- app/src/main/AndroidManifest.xml | 21 +++ .../WidgetDateTimeConfigureActivity.kt | 134 ++++++++++++++++++ .../clock/helpers/MyWidgetDateTimeProvider.kt | 16 +++ .../res/layout/widget_config_date_time.xml | 72 ++++++++++ app/src/main/res/layout/widget_date_time.xml | 27 ++++ app/src/main/res/values-pt/strings.xml | 1 + app/src/main/res/values-sk/strings.xml | 1 + app/src/main/res/values/dimens.xml | 2 + app/src/main/res/values/strings.xml | 1 + .../main/res/xml/widget_date_time_info.xml | 9 ++ 10 files changed, 284 insertions(+) create mode 100644 app/src/main/kotlin/com/simplemobiletools/clock/activities/WidgetDateTimeConfigureActivity.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/clock/helpers/MyWidgetDateTimeProvider.kt create mode 100644 app/src/main/res/layout/widget_config_date_time.xml create mode 100644 app/src/main/res/layout/widget_date_time.xml create mode 100644 app/src/main/res/xml/widget_date_time_info.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 669901f4..88d28b88 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -63,6 +63,15 @@ android:name=".activities.SnoozeReminderActivity" android:theme="@style/Theme.Transparent"/> + + + + + + @@ -76,5 +85,17 @@ + + + + + + + + diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/activities/WidgetDateTimeConfigureActivity.kt b/app/src/main/kotlin/com/simplemobiletools/clock/activities/WidgetDateTimeConfigureActivity.kt new file mode 100644 index 00000000..1c3e5677 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/clock/activities/WidgetDateTimeConfigureActivity.kt @@ -0,0 +1,134 @@ +package com.simplemobiletools.clock.activities + +import android.app.Activity +import android.appwidget.AppWidgetManager +import android.content.Intent +import android.graphics.Color +import android.os.Bundle +import android.widget.SeekBar +import com.simplemobiletools.clock.R +import com.simplemobiletools.clock.extensions.config +import com.simplemobiletools.clock.helpers.MyWidgetDateTimeProvider +import com.simplemobiletools.commons.dialogs.ColorPickerDialog +import com.simplemobiletools.commons.extensions.adjustAlpha +import kotlinx.android.synthetic.main.widget_config_date_time.* + +class WidgetDateTimeConfigureActivity : SimpleActivity() { + private var mBgAlpha = 0f + private var mWidgetId = 0 + private var mBgColorWithoutTransparency = 0 + private var mBgColor = 0 + private var mTextColorWithoutTransparency = 0 + private var mTextColor = 0 + + public override fun onCreate(savedInstanceState: Bundle?) { + useDynamicTheme = false + super.onCreate(savedInstanceState) + setResult(Activity.RESULT_CANCELED) + setContentView(R.layout.widget_config_date_time) + 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() } + + val primaryColor = config.primaryColor + config_bg_seekbar.setColors(mTextColor, primaryColor, primaryColor) + } + + override fun onResume() { + super.onResume() + window.decorView.setBackgroundColor(0) + } + + private fun initVariables() { + mTextColorWithoutTransparency = config.widgetTextColor + updateColors() + + 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.setOnSeekBarChangeListener(bgSeekbarChangeListener) + config_bg_seekbar.progress = (mBgAlpha * 100).toInt() + updateBgColor() + } + + private fun saveConfig() { + storeWidgetColors() + requestWidgetUpdate() + + Intent().apply { + putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId) + setResult(Activity.RESULT_OK, this) + } + finish() + } + + private fun storeWidgetColors() { + config.apply { + widgetBgColor = mBgColor + widgetTextColor = mTextColorWithoutTransparency + } + } + + private fun pickBackgroundColor() { + ColorPickerDialog(this, mBgColorWithoutTransparency) { + mBgColorWithoutTransparency = it + updateBgColor() + } + } + + private fun pickTextColor() { + ColorPickerDialog(this, mTextColor) { + mTextColorWithoutTransparency = it + updateColors() + } + } + + private fun requestWidgetUpdate() { + Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetDateTimeProvider::class.java).apply { + putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf(mWidgetId)) + sendBroadcast(this) + } + } + + private fun updateColors() { + mTextColor = mTextColorWithoutTransparency + config_text_color.setBackgroundColor(mTextColor) + config_save.setTextColor(mTextColor) + } + + private fun updateBgColor() { + mBgColor = mBgColorWithoutTransparency.adjustAlpha(mBgAlpha) + config_bg_color.setBackgroundColor(mBgColor) + config_save.setBackgroundColor(mBgColor) + } + + private val bgSeekbarChangeListener = object : SeekBar.OnSeekBarChangeListener { + override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { + mBgAlpha = progress.toFloat() / 100.toFloat() + updateBgColor() + } + + override fun onStartTrackingTouch(seekBar: SeekBar) { + + } + + override fun onStopTrackingTouch(seekBar: SeekBar) { + + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/MyWidgetDateTimeProvider.kt b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/MyWidgetDateTimeProvider.kt new file mode 100644 index 00000000..f276592f --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/MyWidgetDateTimeProvider.kt @@ -0,0 +1,16 @@ +package com.simplemobiletools.clock.helpers + +import android.appwidget.AppWidgetManager +import android.appwidget.AppWidgetProvider +import android.content.Context + +class MyWidgetDateTimeProvider : AppWidgetProvider() { + + override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) { + performUpdate(context) + } + + private fun performUpdate(context: Context) { + + } +} diff --git a/app/src/main/res/layout/widget_config_date_time.xml b/app/src/main/res/layout/widget_config_date_time.xml new file mode 100644 index 00000000..d1cb3d23 --- /dev/null +++ b/app/src/main/res/layout/widget_config_date_time.xml @@ -0,0 +1,72 @@ + + + + + + + +