Fix alarm manager handler crash

This commit is contained in:
Anthony La committed 2025-04-25 23:29:29 -07:00
1 parent 77c95ed14a
commit 170a642858
5 files changed
+39 -25

No files matched your search

+2 -1
View File
@@ -2,7 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" android:maxSdkVersion="32" /> <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.USE_EXACT_ALARM"/> <uses-permission android:name="android.permission.USE_EXACT_ALARM"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/> <uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
@@ -500,7 +500,7 @@ fun isValidUri(context: Context, uriString: String?): Boolean {
fun isDirectory(context: Context, uriString: String?): Boolean { fun isDirectory(context: Context, uriString: String?): Boolean {
val uri = uriString?.toUri() ?: return false val uri = uriString?.toUri() ?: return false
return try { return try {
val valid = DocumentFileCompat.fromSingleUri(context, uri)?.isDirectory() ?: false val valid = DocumentFile.fromSingleUri(context, uri)?.isDirectory() ?: false
Log.d("WallpaperUtil", "URI is directory: $uri") Log.d("WallpaperUtil", "URI is directory: $uri")
valid valid
} catch (e: Exception) { } catch (e: Exception) {
@@ -6,10 +6,12 @@ import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.app.AlarmManager import android.app.AlarmManager
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.database.CursorWindow import android.database.CursorWindow
import android.graphics.Color import android.graphics.Color
import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.provider.Settings import android.provider.Settings
@@ -48,6 +50,7 @@ import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import java.lang.reflect.Field import java.lang.reflect.Field
import javax.inject.Inject import javax.inject.Inject
import androidx.core.net.toUri
@AndroidEntryPoint @AndroidEntryPoint
@@ -64,6 +67,7 @@ class MainActivity : ComponentActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
if (ContextCompat.checkSelfPermission(context, Manifest.permission.SET_WALLPAPER) != PackageManager.PERMISSION_GRANTED) { if (ContextCompat.checkSelfPermission(context, Manifest.permission.SET_WALLPAPER) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(context as Activity, arrayOf(Manifest.permission.SET_WALLPAPER), 0) ActivityCompat.requestPermissions(context as Activity, arrayOf(Manifest.permission.SET_WALLPAPER), 0)
} }
val splashScreen = installSplashScreen() val splashScreen = installSplashScreen()
@@ -79,6 +83,7 @@ class MainActivity : ComponentActivity() {
splashScreen.setKeepOnScreenCondition { settingsViewModel.setKeepOnScreenCondition } splashScreen.setKeepOnScreenCondition { settingsViewModel.setKeepOnScreenCondition }
setContent { setContent {
ensureExactAlarmPermission(context)
val settingsState = settingsViewModel.state.collectAsStateWithLifecycle() val settingsState = settingsViewModel.state.collectAsStateWithLifecycle()
val isFirstLaunch = runBlocking { settingsDataStoreImpl.getBoolean(SettingsConstants.FIRST_LAUNCH) } ?: true val isFirstLaunch = runBlocking { settingsDataStoreImpl.getBoolean(SettingsConstants.FIRST_LAUNCH) } ?: true
val scheduler = WallpaperAlarmSchedulerImpl(context) val scheduler = WallpaperAlarmSchedulerImpl(context)
@@ -106,6 +111,19 @@ class MainActivity : ComponentActivity() {
} }
} }
private fun ensureExactAlarmPermission(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val am = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
if (!am.canScheduleExactAlarms()) {
val intent = Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM).apply {
data = "package:${context.packageName}".toUri()
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
context.startActivity(intent)
}
}
}
private suspend fun handleWallpaperScheduling( private suspend fun handleWallpaperScheduling(
settings: SettingsState, settings: SettingsState,
scheduler: WallpaperAlarmSchedulerImpl scheduler: WallpaperAlarmSchedulerImpl
@@ -209,7 +209,7 @@ class WallpaperAlarmSchedulerImpl @Inject constructor(
calculateFutureTime(baseTime, wallpaperAlarmItem.homeInterval.toLong()) calculateFutureTime(baseTime, wallpaperAlarmItem.homeInterval.toLong())
} }
} }
return nextTime return nextTime.withSecond(0).withNano(0)
} }
} }
@@ -246,27 +246,21 @@ class WallpaperAlarmSchedulerImpl @Inject constructor(
fun scheduleRefresh(refresh: Boolean = true) { fun scheduleRefresh(refresh: Boolean = true) {
cancelAlarm(Type.REFRESH) cancelAlarm(Type.REFRESH)
if (refresh) { if (refresh) {
// val intent = Intent(context, WallpaperReceiver::class.java).apply { val intent = Intent(context, WallpaperReceiver::class.java).apply {
// putExtra("refresh", true) putExtra("refresh", true)
// }
val serviceIntent = Intent(context, HomeWallpaperService::class.java).apply {
action = HomeWallpaperService.Actions.REFRESH.toString()
} }
context.startService(serviceIntent) val nextMidnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0)
// val nextMidnight = LocalDateTime.now().plusMinutes(1) alarmManager.setInexactRepeating(
// val nextMidnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0) AlarmManager.RTC_WAKEUP,
// alarmManager.setInexactRepeating( nextMidnight.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000,
// AlarmManager.RTC_WAKEUP, AlarmManager.INTERVAL_DAY,
// nextMidnight.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000, PendingIntent.getBroadcast(
// //AlarmManager.INTERVAL_DAY, context,
// AlarmManager.INTERVAL_FIFTEEN_MINUTES, Type.REFRESH.ordinal,
// PendingIntent.getBroadcast( intent,
// context, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
// Type.REFRESH.ordinal, )
// intent, )
// PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
// )
// )
} }
} }
@@ -24,13 +24,14 @@ class WallpaperReceiver : BroadcastReceiver() {
lateinit var settingsDataStoreImpl: SettingsDataStore lateinit var settingsDataStoreImpl: SettingsDataStore
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
if (context != null) { if (context != null) {
val refresh = intent?.getBooleanExtra("refresh", true) val refresh = intent?.getBooleanExtra("refresh", false)
if (refresh == true) { if (refresh == true) {
val serviceIntent = Intent(context, HomeWallpaperService::class.java).apply { val serviceIntent = Intent(context, HomeWallpaperService::class.java).apply {
action = HomeWallpaperService.Actions.REFRESH.toString() action = HomeWallpaperService.Actions.REFRESH.toString()
} }
context.startService(serviceIntent) context.startService(serviceIntent)
} else { }
else {
val homeInterval = intent?.getIntExtra("homeInterval", WALLPAPER_CHANGE_INTERVAL_DEFAULT) ?: WALLPAPER_CHANGE_INTERVAL_DEFAULT val homeInterval = intent?.getIntExtra("homeInterval", WALLPAPER_CHANGE_INTERVAL_DEFAULT) ?: WALLPAPER_CHANGE_INTERVAL_DEFAULT
val lockInterval = intent?.getIntExtra("lockInterval", WALLPAPER_CHANGE_INTERVAL_DEFAULT) ?: WALLPAPER_CHANGE_INTERVAL_DEFAULT val lockInterval = intent?.getIntExtra("lockInterval", WALLPAPER_CHANGE_INTERVAL_DEFAULT) ?: WALLPAPER_CHANGE_INTERVAL_DEFAULT
val scheduleSeparately = intent?.getBooleanExtra("scheduleSeparately", false) ?: false val scheduleSeparately = intent?.getBooleanExtra("scheduleSeparately", false) ?: false