Skip to content

Commit

Permalink
Enable auto restart for alarm manager when opening the app
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthonyy232 committed Jun 5, 2024
1 parent 322a10b commit 265edf8
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {
applicationId = "com.anthonyla.paperize"
minSdk = 26
targetSdk = 34
versionCode = 18
versionName = "1.5.0"
versionCode = 19
versionName = "1.5.1"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper
import com.google.android.renderscript.Toolkit
import com.lazygeniouz.dfc.file.DocumentFileCompat

enum class Type { HOME, LOCK, BOTH }

/**
* Get the dimensions of the image from the uri
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.Manifest
import android.animation.ObjectAnimator
import android.app.Activity
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
Expand All @@ -25,8 +26,12 @@ import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LifecycleEventEffect
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.anthonyla.paperize.core.SettingsConstants
import com.anthonyla.paperize.core.Type
import com.anthonyla.paperize.data.settings.SettingsDataStore
import com.anthonyla.paperize.feature.wallpaper.presentation.album.AlbumsEvent
import com.anthonyla.paperize.feature.wallpaper.presentation.album.AlbumsViewModel
Expand All @@ -35,7 +40,11 @@ import com.anthonyla.paperize.feature.wallpaper.presentation.settings_screen.Set
import com.anthonyla.paperize.feature.wallpaper.presentation.themes.PaperizeTheme
import com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.WallpaperEvent
import com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.WallpaperScreenViewModel
import com.anthonyla.paperize.feature.wallpaper.wallpaper_alarmmanager.WallpaperAlarmItem
import com.anthonyla.paperize.feature.wallpaper.wallpaper_alarmmanager.WallpaperReceiver
import com.anthonyla.paperize.feature.wallpaper.wallpaper_alarmmanager.WallpaperScheduler
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import javax.inject.Inject

Expand Down Expand Up @@ -72,6 +81,7 @@ class MainActivity : ComponentActivity() {
splashScreen.setKeepOnScreenCondition { settingsViewModel.setKeepOnScreenCondition }

setContent {
val scheduler = WallpaperScheduler(context)
val settingsState = settingsViewModel.state.collectAsStateWithLifecycle()
val isFirstLaunch = runBlocking { settingsDataStoreImpl.getBoolean(SettingsConstants.FIRST_LAUNCH) } ?: true
if (isFirstLaunch) {
Expand All @@ -84,9 +94,31 @@ class MainActivity : ComponentActivity() {
contentResolver.releasePersistableUriPermission(permission.uri, Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
}
}
LifecycleEventEffect(Lifecycle.Event.ON_CREATE) {
lifecycleScope.launch {
if (settingsDataStoreImpl.getBoolean(SettingsConstants.ENABLE_CHANGER) == true) {
val shouldScheduleSeparately = settingsDataStoreImpl.getBoolean(SettingsConstants.SCHEDULE_SEPARATELY) ?: false
val shouldScheduleAlarm = if (shouldScheduleSeparately) {
!(isPendingIntentSet(Type.HOME.ordinal) && isPendingIntentSet(Type.LOCK.ordinal))
} else {
!isPendingIntentSet(Type.BOTH.ordinal)
}
if (shouldScheduleAlarm) {
scheduler.scheduleWallpaperAlarm(
WallpaperAlarmItem(
timeInMinutes1 = settingsDataStoreImpl.getInt(SettingsConstants.HOME_WALLPAPER_CHANGE_INTERVAL) ?: SettingsConstants.WALLPAPER_CHANGE_INTERVAL_DEFAULT,
timeInMinutes2 = settingsDataStoreImpl.getInt(SettingsConstants.LOCK_WALLPAPER_CHANGE_INTERVAL) ?: SettingsConstants.WALLPAPER_CHANGE_INTERVAL_DEFAULT,
scheduleSeparately = shouldScheduleSeparately
), null, true, true
)
}
}
}
}

PaperizeTheme(settingsState.value.darkMode, settingsState.value.dynamicTheming) {
Surface(tonalElevation = 5.dp) {
PaperizeApp(isFirstLaunch, topInset)
PaperizeApp(isFirstLaunch, scheduler, topInset)
}
}
}
Expand All @@ -110,4 +142,15 @@ class MainActivity : ComponentActivity() {
super.onAttachedToWindow()
topInset = window.decorView.rootWindowInsets.stableInsetTop.dp
}

private fun isPendingIntentSet(requestCode: Int): Boolean {
val intent = Intent(context.applicationContext, WallpaperReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(
context.applicationContext,
requestCode,
intent,
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_NO_CREATE
)
return pendingIntent != null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import java.nio.charset.StandardCharsets
@Composable
fun PaperizeApp(
firstLaunch: Boolean,
scheduler : WallpaperScheduler,
topInsets: Dp,
albumsViewModel: AlbumsViewModel = hiltViewModel(),
settingsViewModel: SettingsViewModel = hiltViewModel(),
Expand All @@ -84,7 +85,6 @@ fun PaperizeApp(
val context = LocalContext.current
var job by remember { mutableStateOf<Job?>(null) }
val scope = rememberCoroutineScope()
val scheduler = WallpaperScheduler(context)

// React to albumState changes and change selectedAlbum's details to keep it from being stale
LaunchedEffect(albumState.value.albumsWithWallpapers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.content.Intent
import android.util.Log
import com.anthonyla.paperize.core.SettingsConstants.WALLPAPER_CHANGE_INTERVAL_DEFAULT
import com.anthonyla.paperize.core.Type
import com.anthonyla.paperize.feature.wallpaper.wallpaper_service.WallpaperService1
import com.anthonyla.paperize.feature.wallpaper.wallpaper_service.WallpaperService2
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -14,8 +15,6 @@ import dagger.hilt.android.AndroidEntryPoint
*/
@AndroidEntryPoint
class WallpaperReceiver: BroadcastReceiver() {
enum class Type { HOME, LOCK, BOTH }

override fun onReceive(context: Context?, intent: Intent?) {
if (context != null) {
val timeInMinutes1 = intent?.getIntExtra("timeInMinutes1", WALLPAPER_CHANGE_INTERVAL_DEFAULT) ?: WALLPAPER_CHANGE_INTERVAL_DEFAULT
Expand All @@ -28,7 +27,7 @@ class WallpaperReceiver: BroadcastReceiver() {
putExtra("scheduleSeparately", scheduleSeparately)
putExtra("type", type)
}
if (type == WallpaperScheduler.Type.BOTH.ordinal || type == WallpaperScheduler.Type.HOME.ordinal) {
if (type == Type.BOTH.ordinal || type == Type.HOME.ordinal) {
serviceIntent.setClass(context, WallpaperService1::class.java).apply {
action = WallpaperService1.Actions.START.toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Settings
import com.anthonyla.paperize.core.Type
import com.anthonyla.paperize.feature.wallpaper.wallpaper_service.WallpaperService1
import com.anthonyla.paperize.feature.wallpaper.wallpaper_service.WallpaperService2
import java.time.LocalDateTime
Expand All @@ -20,7 +21,6 @@ class WallpaperScheduler (
private val context: Context,
): WallpaperAlarmScheduler {
private val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
enum class Type { HOME, LOCK, BOTH }

override fun scheduleWallpaperAlarm(wallpaperAlarmItem: WallpaperAlarmItem, origin: Int?, changeImmediate: Boolean, cancelImmediate: Boolean) {
if (cancelImmediate) cancelWallpaperAlarm()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.documentfile.provider.DocumentFile
import com.anthonyla.paperize.R
import com.anthonyla.paperize.core.ScalingConstants
import com.anthonyla.paperize.core.SettingsConstants
import com.anthonyla.paperize.core.Type
import com.anthonyla.paperize.core.blurBitmap
import com.anthonyla.paperize.core.calculateInSampleSize
import com.anthonyla.paperize.core.darkenBitmap
Expand Down Expand Up @@ -56,7 +57,6 @@ import kotlin.math.min
*/
@AndroidEntryPoint
class WallpaperService1: Service() {
private enum class Type { HOME, LOCK, BOTH }
private val handleThread = HandlerThread("MyThread1")
private lateinit var workerHandler: Handler
@Inject lateinit var selectedRepository: SelectedAlbumRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.documentfile.provider.DocumentFile
import com.anthonyla.paperize.R
import com.anthonyla.paperize.core.ScalingConstants
import com.anthonyla.paperize.core.SettingsConstants
import com.anthonyla.paperize.core.Type
import com.anthonyla.paperize.core.blurBitmap
import com.anthonyla.paperize.core.calculateInSampleSize
import com.anthonyla.paperize.core.darkenBitmap
Expand Down Expand Up @@ -56,7 +57,6 @@ import kotlin.math.min
*/
@AndroidEntryPoint
class WallpaperService2: Service() {
private enum class Type { HOME, LOCK, BOTH }
private val handleThread = HandlerThread("MyThread2")
private lateinit var workerHandler: Handler
@Inject lateinit var selectedRepository: SelectedAlbumRepository
Expand Down

0 comments on commit 265edf8

Please sign in to comment.