forked from Schanbro/WeekFocus
158 lines
5.5 KiB
Kotlin
158 lines
5.5 KiB
Kotlin
package de.schanbro.weekfocus
|
|
|
|
import android.app.AlarmManager
|
|
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 java.time.DayOfWeek
|
|
import java.time.LocalDate
|
|
import java.time.ZoneId
|
|
import java.time.temporal.TemporalAdjusters
|
|
|
|
class SimpleWidget : AppWidgetProvider() {
|
|
|
|
override fun onReceive(context: Context, intent: Intent) {
|
|
super.onReceive(context, intent)
|
|
|
|
when (intent.action) {
|
|
ACTION_WEEK_CHANGED,
|
|
Intent.ACTION_BOOT_COMPLETED,
|
|
Intent.ACTION_DATE_CHANGED,
|
|
Intent.ACTION_TIME_CHANGED,
|
|
Intent.ACTION_TIMEZONE_CHANGED -> updateAllWidgets(context)
|
|
}
|
|
}
|
|
|
|
override fun onUpdate(
|
|
context: Context,
|
|
appWidgetManager: AppWidgetManager,
|
|
appWidgetIds: IntArray
|
|
) {
|
|
for (appWidgetId in appWidgetIds) {
|
|
updateAppWidget(context, appWidgetManager, appWidgetId)
|
|
}
|
|
scheduleNextWeekChange(context)
|
|
}
|
|
|
|
override fun onEnabled(context: Context) {
|
|
scheduleNextWeekChange(context)
|
|
}
|
|
|
|
override fun onDisabled(context: Context) {
|
|
cancelWeekChange(context)
|
|
}
|
|
|
|
companion object {
|
|
const val ACTION_WEEK_CHANGED = "de.schanbro.weekfocus.ACTION_WEEK_CHANGED"
|
|
|
|
private const val PREFS_NAME = "WeekFocusPrefs"
|
|
private const val KEY_WEEK = "current_week_key"
|
|
private const val KEY_WORD = "current_word"
|
|
|
|
/**
|
|
* Aktualisiert alle Widget-Instanzen und plant den nächsten Wochenwechsel.
|
|
*/
|
|
fun updateAllWidgets(context: Context) {
|
|
val appWidgetManager = AppWidgetManager.getInstance(context)
|
|
val componentName = ComponentName(context, SimpleWidget::class.java)
|
|
val appWidgetIds = appWidgetManager.getAppWidgetIds(componentName)
|
|
if (appWidgetIds.isEmpty()) return
|
|
|
|
for (appWidgetId in appWidgetIds) {
|
|
updateAppWidget(context, appWidgetManager, appWidgetId)
|
|
}
|
|
scheduleNextWeekChange(context)
|
|
}
|
|
|
|
fun updateAppWidget(
|
|
context: Context,
|
|
appWidgetManager: AppWidgetManager,
|
|
appWidgetId: Int
|
|
) {
|
|
val displayText = resolveWordOfWeek(context)
|
|
?: context.getString(R.string.msg_no_words)
|
|
|
|
val views = RemoteViews(context.packageName, R.layout.widget_main)
|
|
views.setTextViewText(R.id.appwidget_text, displayText)
|
|
|
|
appWidgetManager.updateAppWidget(appWidgetId, views)
|
|
}
|
|
|
|
/**
|
|
* Holt das Wort für die aktuelle Woche unter Verwendung von WordOfWeek
|
|
* und speichert es persistent ab, solange sich die Woche nicht ändert.
|
|
*/
|
|
private fun resolveWordOfWeek(context: Context): String? {
|
|
// Wörter sortiert laden, damit die Reihenfolge deterministisch bleibt
|
|
val words = WordOfWeek.loadWords(context).sorted()
|
|
|
|
if (words.isEmpty()) {
|
|
context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
|
.edit().remove(KEY_WEEK).remove(KEY_WORD).apply()
|
|
return null
|
|
}
|
|
|
|
val currentWeek = WordOfWeek.weekOf(0)
|
|
val weekKey = currentWeek.weekYear * 100 + currentWeek.weekNumber
|
|
|
|
val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
|
val storedWord = prefs.getString(KEY_WORD, null)
|
|
|
|
// Wenn die gespeicherte Woche passt und das gespeicherte Wort noch in der Liste existiert: behalten
|
|
if (prefs.getInt(KEY_WEEK, -1) == weekKey && storedWord != null && storedWord in words) {
|
|
return storedWord
|
|
}
|
|
|
|
// Ansonsten neu über WordOfWeek bestimmen
|
|
val newWord = WordOfWeek.wordFor(words, currentWeek) ?: return null
|
|
|
|
// Speichern für den Rest der Woche
|
|
prefs.edit()
|
|
.putInt(KEY_WEEK, weekKey)
|
|
.putString(KEY_WORD, newWord)
|
|
.apply()
|
|
|
|
return newWord
|
|
}
|
|
|
|
/**
|
|
* Plant einen Alarm für den nächsten Montag, 00:00 Uhr (lokale Zeit).
|
|
*/
|
|
private fun scheduleNextWeekChange(context: Context) {
|
|
val alarmManager = context.getSystemService(AlarmManager::class.java) ?: return
|
|
|
|
val triggerAtMillis = LocalDate.now()
|
|
.with(TemporalAdjusters.next(DayOfWeek.MONDAY))
|
|
.atStartOfDay(ZoneId.systemDefault())
|
|
.toInstant()
|
|
.toEpochMilli()
|
|
|
|
alarmManager.setAndAllowWhileIdle(
|
|
AlarmManager.RTC_WAKEUP,
|
|
triggerAtMillis,
|
|
weekChangePendingIntent(context)
|
|
)
|
|
}
|
|
|
|
private fun cancelWeekChange(context: Context) {
|
|
val alarmManager = context.getSystemService(AlarmManager::class.java) ?: return
|
|
alarmManager.cancel(weekChangePendingIntent(context))
|
|
}
|
|
|
|
private fun weekChangePendingIntent(context: Context): PendingIntent {
|
|
val intent = Intent(context, SimpleWidget::class.java).apply {
|
|
action = ACTION_WEEK_CHANGED
|
|
}
|
|
return PendingIntent.getBroadcast(
|
|
context,
|
|
0,
|
|
intent,
|
|
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
|
)
|
|
}
|
|
}
|
|
} |