Skip to content

Commit

Permalink
Fix checking permission for schedule exact alarm on Android 14
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthonyy232 committed Jun 4, 2024
1 parent 62a5934 commit f6688c8
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ android {
applicationId = "com.anthonyla.paperize"
minSdk = 26
targetSdk = 34
versionCode = 17
versionCode = 18
versionName = "1.5.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/anthonyla/paperize/core/WallpaperUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,17 @@ fun getFolderNameFromUri(folderUri: String, context: Context): String? {
}
}

/**
* Check if a URI is valid
*/
fun isValidUri(context: Context, uriString: String?): Boolean {
val uri = uriString?.toUri()
return try {
uri?.let {
val inputStream = context.contentResolver.openInputStream(it)
inputStream?.close()
}
true
} catch (e: Exception) { false }
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package com.anthonyla.paperize.feature.wallpaper.presentation
import android.Manifest
import android.animation.ObjectAnimator
import android.app.Activity
import android.app.AlarmManager
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.view.animation.AccelerateInterpolator
import androidx.activity.ComponentActivity
Expand Down Expand Up @@ -90,6 +92,19 @@ class MainActivity : ComponentActivity() {
}
}

override fun onResume() {
super.onResume()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val alarmManager = ContextCompat.getSystemService(context, AlarmManager::class.java)
if (alarmManager?.canScheduleExactAlarms() == false) {
Intent().also { intent ->
intent.action = Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM
context.startActivity(intent)
}
}
}
}

@RequiresApi(Build.VERSION_CODES.P)
override fun onAttachedToWindow() {
super.onAttachedToWindow()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen

import android.app.AlarmManager
import android.app.WallpaperManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Settings
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
Expand All @@ -28,6 +32,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.anthonyla.paperize.R
Expand Down Expand Up @@ -357,8 +362,23 @@ fun WallpaperScreen(
onDismiss = { openBottomSheet = false },
onSelect = { album ->
openBottomSheet = false
onSelectAlbum(album)
onScheduleWallpaperChanger1(interval1)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val alarmManager = ContextCompat.getSystemService(context, AlarmManager::class.java)
if (alarmManager?.canScheduleExactAlarms() == false) {
Intent().also { intent ->
intent.action = Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM
context.startActivity(intent)
}
}
else {
onSelectAlbum(album)
onScheduleWallpaperChanger1(interval1)
}
}
else {
onSelectAlbum(album)
onScheduleWallpaperChanger1(interval1)
}
},
animate = animate
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.components

import android.content.Context
import android.os.Build
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
Expand Down Expand Up @@ -35,8 +34,8 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri
import com.anthonyla.paperize.R
import com.anthonyla.paperize.core.isValidUri
import com.anthonyla.paperize.feature.wallpaper.domain.model.AlbumWithWallpaperAndFolder
import com.anthonyla.paperize.feature.wallpaper.domain.model.SelectedAlbum
import com.skydoves.landscapist.ImageOptions
Expand All @@ -55,17 +54,6 @@ fun AlbumBottomSheet(
onSelect: (AlbumWithWallpaperAndFolder) -> Unit,
animate: Boolean
) {
fun isValidUri(context: Context, uriString: String?): Boolean {
val uri = uriString?.toUri()
return try {
uri?.let {
val inputStream = context.contentResolver.openInputStream(it)
inputStream?.close()
}
true
} catch (e: Exception) { false }
}

val modalBottomSheetState = rememberModalBottomSheetState()
val scope = rememberCoroutineScope()
ModalBottomSheet(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Settings
import com.anthonyla.paperize.feature.wallpaper.wallpaper_service.WallpaperService1
import com.anthonyla.paperize.feature.wallpaper.wallpaper_service.WallpaperService2
import java.time.LocalDateTime
Expand Down Expand Up @@ -101,16 +103,39 @@ class WallpaperScheduler (
putExtra("type", type.ordinal)
putExtra("origin", origin)
}
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
nextTime.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000,
PendingIntent.getBroadcast(
context,
type.ordinal,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (!alarmManager.canScheduleExactAlarms()) {
Intent().also {
it.action = Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM
context.startActivity(it)
}
}
else {
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
nextTime.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000,
PendingIntent.getBroadcast(
context,
type.ordinal,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
)
)
}
}
else {
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
nextTime.atZone(ZoneId.systemDefault()).toEpochSecond() * 1000,
PendingIntent.getBroadcast(
context,
type.ordinal,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
)
)
)
}
if (update) {
val serviceIntent = Intent().apply {
putExtra("timeInMinutes1", wallpaperAlarmItem.timeInMinutes1)
Expand Down

0 comments on commit f6688c8

Please sign in to comment.