Fix alarm manager handler crash
This commit is contained in:
1 parent
77c95ed14a
commit
170a642858
5 files changed
+39
-25
No files matched your search
@@ -2,7 +2,8 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
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.SET_WALLPAPER"/>
|
||||
<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 {
|
||||
val uri = uriString?.toUri() ?: return false
|
||||
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")
|
||||
valid
|
||||
} catch (e: Exception) {
|
||||
|
||||
+18
@@ -6,10 +6,12 @@ import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.app.AlarmManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.database.CursorWindow
|
||||
import android.graphics.Color
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.provider.Settings
|
||||
@@ -48,6 +50,7 @@ import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import java.lang.reflect.Field
|
||||
import javax.inject.Inject
|
||||
import androidx.core.net.toUri
|
||||
|
||||
|
||||
@AndroidEntryPoint
|
||||
@@ -64,6 +67,7 @@ class MainActivity : ComponentActivity() {
|
||||
super.onCreate(savedInstanceState)
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.SET_WALLPAPER) != PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(context as Activity, arrayOf(Manifest.permission.SET_WALLPAPER), 0)
|
||||
|
||||
}
|
||||
val splashScreen = installSplashScreen()
|
||||
|
||||
@@ -79,6 +83,7 @@ class MainActivity : ComponentActivity() {
|
||||
splashScreen.setKeepOnScreenCondition { settingsViewModel.setKeepOnScreenCondition }
|
||||
|
||||
setContent {
|
||||
ensureExactAlarmPermission(context)
|
||||
val settingsState = settingsViewModel.state.collectAsStateWithLifecycle()
|
||||
val isFirstLaunch = runBlocking { settingsDataStoreImpl.getBoolean(SettingsConstants.FIRST_LAUNCH) } ?: true
|
||||
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(
|
||||
settings: SettingsState,
|
||||
scheduler: WallpaperAlarmSchedulerImpl
|
||||
|
||||
+15
-21
@@ -209,7 +209,7 @@ class WallpaperAlarmSchedulerImpl @Inject constructor(
|
||||
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) {
|
||||
cancelAlarm(Type.REFRESH)
|
||||
if (refresh) {
|
||||
// val intent = Intent(context, WallpaperReceiver::class.java).apply {
|
||||
// putExtra("refresh", true)
|
||||
// }
|
||||
val serviceIntent = Intent(context, HomeWallpaperService::class.java).apply {
|
||||
action = HomeWallpaperService.Actions.REFRESH.toString()
|
||||
val intent = Intent(context, WallpaperReceiver::class.java).apply {
|
||||
putExtra("refresh", true)
|
||||
}
|
||||
context.startService(serviceIntent)
|
||||
// val nextMidnight = LocalDateTime.now().plusMinutes(1)
|
||||
// val nextMidnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0)
|
||||
// alarmManager.setInexactRepeating(
|
||||
// AlarmManager.RTC_WAKEUP,
|
||||
// nextMidnight.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000,
|
||||
// //AlarmManager.INTERVAL_DAY,
|
||||
// AlarmManager.INTERVAL_FIFTEEN_MINUTES,
|
||||
// PendingIntent.getBroadcast(
|
||||
// context,
|
||||
// Type.REFRESH.ordinal,
|
||||
// intent,
|
||||
// PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
|
||||
// )
|
||||
// )
|
||||
val nextMidnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0)
|
||||
alarmManager.setInexactRepeating(
|
||||
AlarmManager.RTC_WAKEUP,
|
||||
nextMidnight.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000,
|
||||
AlarmManager.INTERVAL_DAY,
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
Type.REFRESH.ordinal,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-2
@@ -24,13 +24,14 @@ class WallpaperReceiver : BroadcastReceiver() {
|
||||
lateinit var settingsDataStoreImpl: SettingsDataStore
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
if (context != null) {
|
||||
val refresh = intent?.getBooleanExtra("refresh", true)
|
||||
val refresh = intent?.getBooleanExtra("refresh", false)
|
||||
if (refresh == true) {
|
||||
val serviceIntent = Intent(context, HomeWallpaperService::class.java).apply {
|
||||
action = HomeWallpaperService.Actions.REFRESH.toString()
|
||||
}
|
||||
context.startService(serviceIntent)
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
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 scheduleSeparately = intent?.getBooleanExtra("scheduleSeparately", false) ?: false
|
||||
|
||||
Reference in new issue
Block a user