ankidroid / ankidroid/Anki-Android

move `useAlarmManager` to `:common`

Đang mở
#21,747 1 bình luận 0 reaction 1 người được giao Được @ericli3690 nhận Xem trên GitHub
Ngôn ngữ chính
Kotlin
Star
11.8k
Fork
2.9k
Merge trung bình
2 ngày 3 giờ
Pull request đã merge (30 ngày)
171

Mô tả

### 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,
+ )
}
}
}
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.