Enable auto restart for alarm manager when opening the app
This commit is contained in:
1 parent
322a10bf0c
commit
265edf8f63
8 files changed
+54
-10
No files matched your search
@@ -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
|
||||
*/
|
||||
|
||||
+44
-1
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -71,6 +71,7 @@ import java.nio.charset.StandardCharsets
|
||||
@Composable
|
||||
fun PaperizeApp(
|
||||
firstLaunch: Boolean,
|
||||
scheduler : WallpaperScheduler,
|
||||
topInsets: Dp,
|
||||
albumsViewModel: AlbumsViewModel = hiltViewModel(),
|
||||
settingsViewModel: SettingsViewModel = hiltViewModel(),
|
||||
@@ -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) {
|
||||
|
||||
+2
-3
@@ -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
|
||||
@@ -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
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
+1
-1
@@ -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
|
||||
@@ -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()
|
||||
|
||||
+1
-1
@@ -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
|
||||
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user