ankidroid / ankidroid/Anki-Android
move `useAlarmManager` to `:common`
- Lenguaje dominante
- Kotlin
- Estrellas
- 11.8k
- Forks
- 2.9k
- Merge medio
- 2 d 3 h
- PR fusionados (30 d)
- 171
Descripción
### Problem:
Currently, `WidgetAlarm.kt` rolls its own AlarmManager functionality. It should instead be using the centralized AlarmManager fetching functionality provided by `AlarmManagement.kt`. This is a low-urgency refactor. I'm just creating this issue as a reminder to myself.
### Solution:
WidgetAlarm is located in the `:widget` module, while AlarmManagement is still in `:AnkiDroid`. We should move AlarmManagement to `:common` so that WidgetAlarm can depend on it. AlarmManagement does not belong in `:AnkiDroid`.
However, this move is not possible yet because `useAlarmManager`, in AlarmManagement, invokes string resources. I've created this issue as a TODO for myself, pending strings being extracted to `:common` via https://github.com/ankidroid/Anki-Android/pull/21666. I can't open a draft PR yet as of writing because https://github.com/ankidroid/Anki-Android/pull/21666 is not rebased on top of the commit creating AlarmManagement on `main` yet.
Note that once this move happens, we will need to also pull `scheduleAllNotifications` out of AlarmManagement, as it belongs in `:AnkiDroid`. I propose moving it to `NotificationChannel.kt`.
### Code Snippets:
The code for making WidgetAlarm depend on AlarmManagement properly is already created and was split out of https://github.com/ankidroid/Anki-Android/pull/21271, so there's no need to redo that work:
Patch
```diff
Subject: [PATCH] widget alarm
---
Index: widgets/src/main/java/com/ichi2/widget/WidgetAlarm.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/widgets/src/main/java/com/ichi2/widget/WidgetAlarm.kt b/widgets/src/main/java/com/ichi2/widget/WidgetAlarm.kt
--- a/widgets/src/main/java/com/ichi2/widget/WidgetAlarm.kt (revision a3ba255173d4c63ed8db18a16430bd6c4ae66245)
+++ b/widgets/src/main/java/com/ichi2/widget/WidgetAlarm.kt (revision d831db8bf0503ca1d3b841694fc1b599300154b5)
@@ -10,6 +10,7 @@
import android.content.Context
import android.content.Intent
import android.os.SystemClock
+import com.ichi2.utils.AlarmManagement // this import will need to be modified once AlarmManagement is moved to :common
import timber.log.Timber
import kotlin.time.Duration.Companion.minutes
@@ -19,14 +20,6 @@
*/
const val ACTION_UPDATE_WIDGET = "com.ichi2.widget.ACTION_UPDATE_WIDGET"
-/**
- * Provides the AlarmManager instance.
- *
- * @param context the context of the application
- * @return the AlarmManager instance
- */
-private fun alarmManager(context: Context): AlarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
-
/**
* Retrieves or creates a PendingIntent for the widget.
*
@@ -84,15 +77,16 @@
Timber.v("Creating a new recurring alarm PendingIntent for widget ID: $appWidgetId")
- val alarmManager = alarmManager(context)
val newPendingIntent = getPendingIntent(context, appWidgetId, widgetClass, create = true) ?: return
- alarmManager.setRepeating(
- AlarmManager.ELAPSED_REALTIME,
- SystemClock.elapsedRealtime() + 1.minutes.inWholeMilliseconds,
- 1.minutes.inWholeMilliseconds,
- newPendingIntent,
- )
+ AlarmManagement.useAlarmManager(context) { alarmManager ->
+ alarmManager.setRepeating(
+ AlarmManager.ELAPSED_REALTIME,
+ SystemClock.elapsedRealtime() + 1.minutes.inWholeMilliseconds,
+ 1.minutes.inWholeMilliseconds,
+ newPendingIntent,
+ )
+ }
}
/**
@@ -108,10 +102,11 @@
widgetClass: Class,
) {
val pendingIntent = getPendingIntent(context, appWidgetId, widgetClass, create = true)
- val alarmManager = alarmManager(context)
Timber.d("Canceling recurring alarm for widget ID: $appWidgetId")
if (pendingIntent != null) {
- alarmManager.cancel(pendingIntent)
+ AlarmManagement.useAlarmManager(context) { alarmManager ->
+ alarmManager.cancel(pendingIntent)
+ }
}
}
@@ -148,21 +143,11 @@
Timber.d("Restoring %d alarms for %s", activeIds.size, widgetClass.simpleName)
for (id in activeIds) {
- try {
- setRecurringAlarm(
- context,
- id,
- widgetClass,
- )
- } catch (e: SecurityException) {
- Timber.w(
- e,
- "Failed to restore alarms for %s because system alarm limit reached",
- widgetClass.simpleName,
- )
- } catch (e: Exception) {
- Timber.w(e, "Failed to restore alarms for %s", widgetClass.simpleName)
- }
+ setRecurringAlarm(
+ context,
+ id,
+ widgetClass,
+ )
}
}
}
```
Guía de contribución
Evaluación
Este issue todavía no se ha evaluado.