fix #993, adding a widget showing just the current date

This commit is contained in:
tibbi 2020-05-04 22:32:59 +02:00
parent 85930394dd
commit afcb9825b3
8 changed files with 361 additions and 0 deletions

View file

@ -94,6 +94,15 @@
</intent-filter>
</activity>
<activity
android:name=".activities.WidgetDateConfigureActivity"
android:screenOrientation="portrait"
android:theme="@style/MyWidgetConfigTheme">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
<activity
android:name="com.simplemobiletools.commons.activities.AboutActivity"
android:label="@string/about"
@ -188,6 +197,18 @@
android:resource="@xml/widget_list_info"/>
</receiver>
<receiver
android:name=".helpers.MyWidgetDateProvider"
android:label="@string/todays_date">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_date_info"/>
</receiver>
<service
android:name=".services.WidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS"/>

View file

@ -0,0 +1,141 @@
package com.simplemobiletools.calendar.pro.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.calendar.pro.R
import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.helpers.Formatter
import com.simplemobiletools.calendar.pro.helpers.LOW_ALPHA
import com.simplemobiletools.calendar.pro.helpers.MyWidgetDateProvider
import com.simplemobiletools.commons.dialogs.ColorPickerDialog
import com.simplemobiletools.commons.extensions.adjustAlpha
import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.setFillWithStroke
import kotlinx.android.synthetic.main.widget_config_date.*
class WidgetDateConfigureActivity : 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
private var mWeakTextColor = 0
private var mPrimaryColor = 0
public override fun onCreate(savedInstanceState: Bundle?) {
useDynamicTheme = false
super.onCreate(savedInstanceState)
setResult(Activity.RESULT_CANCELED)
setContentView(R.layout.widget_config_date)
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() }
config_bg_seekbar.setColors(mTextColor, mPrimaryColor, mPrimaryColor)
widget_date_label.text = Formatter.getTodayDayNumber()
widget_month_label.text = Formatter.getCurrentMonthShort()
}
override fun onResume() {
super.onResume()
window.decorView.setBackgroundColor(0)
}
private fun initVariables() {
mTextColorWithoutTransparency = config.widgetTextColor
updateColors()
mBgColor = config.widgetBgColor
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) { wasPositivePressed, color ->
if (wasPositivePressed) {
mBgColorWithoutTransparency = color
updateBgColor()
}
}
}
private fun pickTextColor() {
ColorPickerDialog(this, mTextColor) { wasPositivePressed, color ->
if (wasPositivePressed) {
mTextColorWithoutTransparency = color
updateColors()
}
}
}
private fun requestWidgetUpdate() {
Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetDateProvider::class.java).apply {
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf(mWidgetId))
sendBroadcast(this)
}
}
private fun updateColors() {
mTextColor = mTextColorWithoutTransparency
mWeakTextColor = mTextColorWithoutTransparency.adjustAlpha(LOW_ALPHA)
mPrimaryColor = config.primaryColor
config_text_color.setFillWithStroke(mTextColor, Color.BLACK)
config_save.setTextColor(mTextColor)
widget_date_label.setTextColor(mTextColor)
widget_month_label.setTextColor(mTextColor)
}
private fun updateBgColor() {
mBgColor = mBgColorWithoutTransparency.adjustAlpha(mBgAlpha)
config_date_time_wrapper.background.applyColorFilter(mBgColor)
config_bg_color.setFillWithStroke(mBgColor, Color.BLACK)
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) {}
}
}

View file

@ -56,6 +56,7 @@ fun Context.updateWidgets() {
}
updateListWidget()
updateDateWidget()
}
fun Context.updateListWidget() {
@ -69,6 +70,17 @@ fun Context.updateListWidget() {
}
}
fun Context.updateDateWidget() {
val widgetIDs = AppWidgetManager.getInstance(applicationContext).getAppWidgetIds(ComponentName(applicationContext, MyWidgetDateProvider::class.java))
if (widgetIDs.isNotEmpty()) {
Intent(applicationContext, MyWidgetDateProvider::class.java).apply {
action = AppWidgetManager.ACTION_APPWIDGET_UPDATE
putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIDs)
sendBroadcast(this)
}
}
}
fun Context.scheduleAllEvents() {
val events = eventsDB.getEventsAtReboot(getNowSeconds())
events.forEach {

View file

@ -13,6 +13,7 @@ object Formatter {
const val DAYCODE_PATTERN = "YYYYMMdd"
const val YEAR_PATTERN = "YYYY"
const val TIME_PATTERN = "HHmmss"
private const val MONTH_PATTERN = "MMM"
private const val DAY_PATTERN = "d"
private const val DAY_OF_WEEK_PATTERN = "EEE"
private const val LONGEST_PATTERN = "MMMM d YYYY (EEEE)"
@ -60,6 +61,10 @@ object Formatter {
fun getTodayCode() = getDayCodeFromTS(getNowSeconds())
fun getTodayDayNumber() = getDateTimeFromTS(getNowSeconds()).toString(DAY_PATTERN)
fun getCurrentMonthShort() = getDateTimeFromTS(getNowSeconds()).toString(MONTH_PATTERN)
fun getHours(context: Context, dateTime: DateTime) = dateTime.toString(getHourPattern(context))
fun getTime(context: Context, dateTime: DateTime) = dateTime.toString(getTimePattern(context))

View file

@ -0,0 +1,46 @@
package com.simplemobiletools.calendar.pro.helpers
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.widget.RemoteViews
import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.activities.SplashActivity
import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.commons.extensions.applyColorFilter
import com.simplemobiletools.commons.extensions.getLaunchIntent
import com.simplemobiletools.commons.extensions.setText
class MyWidgetDateProvider : AppWidgetProvider() {
private val OPEN_APP_INTENT_ID = 1
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
appWidgetManager.getAppWidgetIds(getComponentName(context)).forEach {
val views = RemoteViews(context.packageName, R.layout.widget_date).apply {
applyColorFilter(R.id.widget_date_background, context.config.widgetBgColor)
setText(R.id.widget_date_label, Formatter.getTodayDayNumber())
setText(R.id.widget_month_label, Formatter.getCurrentMonthShort())
setTextColor(R.id.widget_date_label, context.config.widgetTextColor)
setTextColor(R.id.widget_month_label, context.config.widgetTextColor)
setupAppOpenIntent(context, this)
appWidgetManager.updateAppWidget(it, this)
}
appWidgetManager.notifyAppWidgetViewDataChanged(it, R.id.widget_date_holder)
}
}
private fun getComponentName(context: Context) = ComponentName(context, MyWidgetDateProvider::class.java)
private fun setupAppOpenIntent(context: Context, views: RemoteViews) {
(context.getLaunchIntent() ?: Intent(context, SplashActivity::class.java)).apply {
val pendingIntent = PendingIntent.getActivity(context, OPEN_APP_INTENT_ID, this, PendingIntent.FLAG_UPDATE_CURRENT)
views.setOnClickPendingIntent(R.id.widget_date_holder, pendingIntent)
}
}
}

View file

@ -0,0 +1,84 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/config_date_time_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="@dimen/activity_margin">
<RelativeLayout
android:id="@+id/config_date_time_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/widget_round_background"
android:paddingBottom="@dimen/small_margin">
<TextView
android:id="@+id/widget_date_label"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="@dimen/normal_margin"
android:textColor="@color/md_grey_white"
android:textSize="26sp"
tools:text="13" />
<TextView
android:id="@+id/widget_month_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/widget_date_label"
android:layout_alignStart="@+id/widget_date_label"
android:layout_alignEnd="@+id/widget_date_label"
android:gravity="center"
android:paddingBottom="@dimen/medium_margin"
android:textColor="@color/md_grey_white"
android:textSize="@dimen/bigger_text_size"
tools:text="Jan" />
</RelativeLayout>
<ImageView
android:id="@+id/config_bg_color"
android:layout_width="@dimen/widget_colorpicker_size"
android:layout_height="@dimen/widget_colorpicker_size"
android:layout_above="@+id/config_text_color" />
<RelativeLayout
android:id="@+id/config_bg_seekbar_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="@+id/config_bg_color"
android:layout_alignBottom="@+id/config_bg_color"
android:layout_toRightOf="@+id/config_bg_color"
android:background="@android:color/white">
<com.simplemobiletools.commons.views.MySeekBar
android:id="@+id/config_bg_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin" />
</RelativeLayout>
<ImageView
android:id="@+id/config_text_color"
android:layout_width="@dimen/widget_colorpicker_size"
android:layout_height="@dimen/widget_colorpicker_size"
android:layout_alignParentBottom="true" />
<Button
android:id="@+id/config_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:fontFamily="sans-serif-light"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:text="@string/ok"
android:textColor="@android:color/white"
android:textSize="@dimen/big_text_size" />
</RelativeLayout>

View file

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/widget_date_holder"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/widget_date_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/widget_date_label"
android:layout_alignTop="@+id/widget_date_label"
android:layout_alignEnd="@+id/widget_date_label"
android:layout_alignBottom="@+id/widget_month_label"
android:src="@drawable/widget_round_background" />
<TextView
android:id="@+id/widget_date_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="@dimen/normal_margin"
android:textColor="@color/md_grey_white"
android:textSize="26sp"
tools:text="1" />
<TextView
android:id="@+id/widget_month_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/widget_date_label"
android:layout_alignStart="@+id/widget_date_label"
android:layout_alignEnd="@+id/widget_date_label"
android:gravity="center"
android:paddingBottom="@dimen/medium_margin"
android:textColor="@color/md_grey_white"
android:textSize="@dimen/bigger_text_size"
tools:text="Jan" />
</RelativeLayout>

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="com.simplemobiletools.calendar.pro.activities.WidgetDateConfigureActivity"
android:initialLayout="@layout/widget_date"
android:minWidth="40dp"
android:minHeight="40dp"
android:minResizeWidth="40dp"
android:minResizeHeight="40dp"
android:previewImage="@drawable/img_widget_list_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="600000" />