Skip to content

Commit a61c988

Browse files
Refactor: Replace Word of the Day AlarmManager with WorkManager
This commit refactors the Word of the Day widget update mechanism. - Replaced the `AlarmManager`-based scheduling with `WorkManager` for more robust and battery-efficient background task execution. - A `PeriodicWorkRequest` is now used to schedule the daily update at midnight. - The `SCHEDULE_EXACT_ALARM` permission has been removed from the `AndroidManifest.xml` as it's no longer needed. - The unused `HomeAppsService` declaration has also been removed from `AndroidManifest.xml`. - A new `WordOfTheDayWorker` class is introduced to handle the background work. Signed-off-by: CreativeCodeCat <wayne6324@gmail.com>
1 parent 8987b9d commit a61c988

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
2424
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
2525
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
26-
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
2726
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
2827
<uses-permission android:name="android.permission.READ_CONTACTS" />
2928
<uses-permission android:name="android.permission.VIBRATE" />
@@ -134,10 +133,6 @@
134133
android:resource="@xml/accessibility_service_config" />
135134
</service>
136135

137-
<service
138-
android:name=".services.HomeAppsService"
139-
android:permission="android.permission.BIND_REMOTEVIEWS" />
140-
141136
<receiver
142137
android:name=".ui.widgets.wordoftheday.WordOfTheDayWidget"
143138
android:exported="false"
Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,55 @@
11
package com.github.droidworksstudio.mlauncher.ui.widgets.wordoftheday
22

3-
import android.Manifest
4-
import android.annotation.SuppressLint
5-
import android.app.AlarmManager
6-
import android.app.PendingIntent
73
import android.content.Context
84
import android.content.Intent
9-
import androidx.annotation.RequiresPermission
5+
import androidx.work.CoroutineWorker
6+
import androidx.work.ExistingPeriodicWorkPolicy
7+
import androidx.work.PeriodicWorkRequestBuilder
8+
import androidx.work.WorkManager
9+
import androidx.work.WorkerParameters
1010
import java.util.Calendar
11+
import java.util.concurrent.TimeUnit
1112

1213
object WordOfTheDayAlarm {
1314

14-
@RequiresPermission(Manifest.permission.SCHEDULE_EXACT_ALARM)
15-
@SuppressLint("ScheduleExactAlarm")
16-
// Midnight alarm for Word of the Day
1715
fun scheduleNextUpdate(context: Context) {
18-
val intent = Intent(context, WordOfTheDayUpdateReceiver::class.java)
19-
intent.action = "com.github.droidworksstudio.mlauncher.ui.widgets.wordofday.UPDATE_WIDGET"
20-
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
16+
val delay = calculateDelayUntilMidnight()
2117

22-
val calendar = Calendar.getInstance().apply {
23-
timeInMillis = System.currentTimeMillis()
18+
val workRequest = PeriodicWorkRequestBuilder<WordOfTheDayWorker>(1, TimeUnit.DAYS)
19+
.setInitialDelay(delay, TimeUnit.MILLISECONDS)
20+
.build()
21+
22+
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
23+
"WordOfTheDay",
24+
ExistingPeriodicWorkPolicy.UPDATE,
25+
workRequest
26+
)
27+
}
28+
29+
private fun calculateDelayUntilMidnight(): Long {
30+
val now = Calendar.getInstance()
31+
val midnight = Calendar.getInstance().apply {
2432
set(Calendar.HOUR_OF_DAY, 0)
2533
set(Calendar.MINUTE, 0)
2634
set(Calendar.SECOND, 0)
2735
set(Calendar.MILLISECOND, 0)
2836
add(Calendar.DAY_OF_YEAR, 1)
2937
}
38+
return midnight.timeInMillis - now.timeInMillis
39+
}
40+
}
41+
42+
class WordOfTheDayWorker(appContext: Context, workerParams: WorkerParameters) :
43+
CoroutineWorker(appContext, workerParams) {
44+
45+
override suspend fun doWork(): Result {
46+
// TODO: Fetch and update your Word of the Day widget here
47+
// Example: send a broadcast like your receiver did
48+
val intent = Intent(applicationContext, WordOfTheDayUpdateReceiver::class.java).apply {
49+
action = "com.github.droidworksstudio.mlauncher.ui.widgets.wordofday.UPDATE_WIDGET"
50+
}
51+
applicationContext.sendBroadcast(intent)
3052

31-
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
32-
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
53+
return Result.success()
3354
}
3455
}

0 commit comments

Comments
 (0)