Skip to content

Commit

Permalink
Merge pull request #12 from Anthonyy232/fix-schedule
Browse files Browse the repository at this point in the history
Migrate wallpaper changer functionality from foreground service to alarm manager
  • Loading branch information
Anthonyy232 authored Jun 4, 2024
2 parents b52a477 + 1ff2539 commit 64dedae
Show file tree
Hide file tree
Showing 19 changed files with 1,412 additions and 618 deletions.
43 changes: 23 additions & 20 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ android {
applicationId = "com.anthonyla.paperize"
minSdk = 26
targetSdk = 34
versionCode = 15
versionName = "1.4.1"
versionCode = 17
versionName = "1.5.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
ndk {
debugSymbolLevel = "FULL"
}
}


Expand Down Expand Up @@ -67,40 +70,40 @@ androidComponents {

dependencies {
implementation("androidx.core:core-ktx:1.13.1")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.1")
implementation("androidx.activity:activity-compose:1.9.0")
implementation(platform("androidx.compose:compose-bom:2024.05.00"))
implementation("androidx.compose.ui:ui:1.7.0-beta01")
implementation("androidx.compose.ui:ui-graphics:1.7.0-beta01")
implementation("androidx.compose.ui:ui-tooling-preview:1.7.0-beta01")
implementation("androidx.compose.material3:material3:1.3.0-beta01")
implementation("androidx.navigation:navigation-compose:2.8.0-beta01")
implementation("androidx.compose.material:material:1.7.0-beta01")
implementation("androidx.compose.ui:ui:1.7.0-beta02")
implementation("androidx.compose.ui:ui-graphics:1.7.0-beta02")
implementation("androidx.compose.ui:ui-tooling-preview:1.7.0-beta02")
implementation("androidx.compose.material3:material3:1.3.0-beta02")
implementation("androidx.navigation:navigation-compose:2.8.0-beta02")
implementation("androidx.compose.material:material:1.7.0-beta02")
implementation("androidx.datastore:datastore:1.1.1")
implementation("androidx.datastore:datastore-preferences:1.1.1")
implementation("androidx.compose.material:material-icons-extended:1.7.0-beta01")
implementation("androidx.compose.material:material-icons-extended:1.7.0-beta02")
implementation("com.google.accompanist:accompanist-adaptive:0.34.0")
implementation("androidx.hilt:hilt-navigation-compose:1.2.0")
implementation("androidx.compose.animation:animation:1.7.0-beta01")
implementation("androidx.compose.animation:animation:1.7.0-beta02")
implementation("androidx.core:core-splashscreen:1.2.0-alpha01")
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.8.0")
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.8.1")
implementation("com.google.code.gson:gson:2.11.0")
implementation("androidx.documentfile:documentfile:1.1.0-alpha01")
implementation("net.engawapg.lib:zoomable:1.7.0-beta02")
implementation("com.github.skydoves:landscapist-glide:2.3.3")
implementation("androidx.work:work-runtime-ktx:2.10.0-alpha02")
implementation("androidx.hilt:hilt-work:1.2.0")
implementation("com.airbnb.android:lottie-compose:6.4.0")
implementation("com.airbnb.android:lottie-compose:6.4.1")
implementation("com.google.accompanist:accompanist-permissions:0.35.0-alpha")
implementation("com.mikepenz:aboutlibraries-core:11.2.0")
implementation("com.mikepenz:aboutlibraries-compose-m3:11.2.0")
implementation("androidx.compose.foundation:foundation:1.7.0-beta01")
implementation("com.mikepenz:aboutlibraries-core:11.2.1")
implementation("com.mikepenz:aboutlibraries-compose-m3:11.2.1")
implementation("androidx.compose.foundation:foundation:1.7.0-beta02")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.6.0-beta01")
androidTestImplementation("androidx.compose.ui:ui-test-junit4:1.7.0-beta01")
debugImplementation("androidx.compose.ui:ui-tooling:1.7.0-beta01")
debugImplementation("androidx.compose.ui:ui-test-manifest:1.7.0-beta01")
androidTestImplementation("androidx.test.espresso:espresso-core:3.6.0-rc01")
androidTestImplementation("androidx.compose.ui:ui-test-junit4:1.7.0-beta02")
debugImplementation("androidx.compose.ui:ui-tooling:1.7.0-beta02")
debugImplementation("androidx.compose.ui:ui-test-manifest:1.7.0-beta02")
implementation("com.google.dagger:hilt-android:2.51.1")
ksp("com.google.dagger:hilt-android-compiler:2.51.1")
implementation("androidx.room:room-runtime:2.6.1")
Expand Down
19 changes: 9 additions & 10 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@
<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" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<queries>
<intent>
<action android:name="android.service.wallpaper.WallpaperService" />
</intent>
</queries>

<application
android:allowBackup="true"
android:name=".App"
Expand All @@ -33,11 +28,15 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".feature.wallpaper.wallpaper_service.WallpaperService" android:foregroundServiceType="mediaPlayback"/>
<receiver android:name=".feature.wallpaper.wallpaper_service.WallpaperBootReceiver"
android:exported="true">
<receiver android:name=".feature.wallpaper.wallpaper_alarmmanager.WallpaperReceiver" />
<service android:name=".feature.wallpaper.wallpaper_service.WallpaperService1"/>
<service android:name=".feature.wallpaper.wallpaper_service.WallpaperService2"/>
<receiver android:name=".feature.wallpaper.wallpaper_alarmmanager.WallpaperBootAndTimeChangeReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
</intent-filter>
</receiver>
<provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ object SettingsConstants {
const val SCHEDULE_SEPARATELY = "schedule_separately"
const val BLUR = "blur"
const val BLUR_PERCENTAGE = "blur_percentage"
const val NEXT_SET_TIME_1 = "next_set_time_1"
const val NEXT_SET_TIME_2 = "next_set_time_2"
}
38 changes: 35 additions & 3 deletions app/src/main/java/com/anthonyla/paperize/core/WallpaperUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,18 @@ fun blurBitmap(source: Bitmap, percent: Int): Bitmap {
* Retrieve wallpaper URIs from a folder directory URI
*/
fun getWallpaperFromFolder(folderUri: String, context: Context): List<String> {
val folderDocumentFile = DocumentFileCompat.fromTreeUri(context, folderUri.toUri())
return listFilesRecursive(folderDocumentFile, context)
try {
val folderDocumentFile = DocumentFileCompat.fromTreeUri(context, folderUri.toUri())
return listFilesRecursive(folderDocumentFile, context)
} catch (e: Exception) {
val folderDocumentFile = DocumentFile.fromTreeUri(context, folderUri.toUri())
return listFilesRecursive(folderDocumentFile, context)
}

}

/**
* Helper function to recursively list files in a directory
* Helper function to recursively list files in a directory for DocumentFileCompat
*/
fun listFilesRecursive(parent: DocumentFileCompat?, context: Context): List<String> {
val files = mutableListOf<String>()
Expand All @@ -168,6 +174,21 @@ fun listFilesRecursive(parent: DocumentFileCompat?, context: Context): List<Stri
}
return files
}
/** Overloaded version of the function for DocumentFile */
fun listFilesRecursive(parent: DocumentFile?, context: Context): List<String> {
val files = mutableListOf<String>()
parent?.listFiles()?.forEach { file ->
if (file.isDirectory) {
files.addAll(listFilesRecursive(file, context))
} else {
val allowedExtensions = listOf("jpg", "jpeg", "png", "heif", "webp", "JPG", "JPEG", "PNG", "HEIF", "WEBP")
if ((file.name?.substringAfterLast(".") ?: "") in allowedExtensions) {
files.add(file.uri.toString())
}
}
}
return files
}

/**
* Helper function to find the first valid URI from a list of wallpapers
Expand All @@ -191,3 +212,14 @@ fun findFirstValidUri(context: Context, wallpapers: List<Wallpaper>, folders: Li
return null
}

/**
* Get the folder name from the folder URI
*/
fun getFolderNameFromUri(folderUri: String, context: Context): String? {
return try {
DocumentFileCompat.fromTreeUri(context, folderUri.toUri())?.name
} catch (e: Exception) {
DocumentFile.fromTreeUri(context, folderUri.toUri())?.name
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.anthonyla.paperize.feature.wallpaper.presentation
import android.Manifest
import android.animation.ObjectAnimator
import android.app.Activity
import android.app.ActivityManager
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
Expand All @@ -24,10 +23,7 @@ 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.data.settings.SettingsDataStore
import com.anthonyla.paperize.feature.wallpaper.presentation.album.AlbumsEvent
Expand All @@ -37,12 +33,8 @@ 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_service.WallpaperService
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import javax.inject.Inject


Expand Down Expand Up @@ -79,9 +71,7 @@ class MainActivity : ComponentActivity() {

setContent {
val settingsState = settingsViewModel.state.collectAsStateWithLifecycle()
val selectedState = wallpaperScreenViewModel.state.collectAsStateWithLifecycle()
val isFirstLaunch = runBlocking { settingsDataStoreImpl.getBoolean(SettingsConstants.FIRST_LAUNCH) } ?: true

if (isFirstLaunch) {
wallpaperScreenViewModel.onEvent(WallpaperEvent.Reset)
settingsViewModel.onEvent(SettingsEvent.Reset)
Expand All @@ -92,37 +82,6 @@ class MainActivity : ComponentActivity() {
contentResolver.releasePersistableUriPermission(permission.uri, Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
}
}

LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
lifecycleScope.launch {
val serviceIntent = Intent(this@MainActivity, WallpaperService::class.java)
val isAlreadyRunning = withContext(Dispatchers.IO) {
val activityManager = getSystemService(ACTIVITY_SERVICE) as? ActivityManager
activityManager?.getRunningServices(Integer.MAX_VALUE)?.any {
it.service == serviceIntent.component
} ?: false
}
if (selectedState.value.selectedAlbum != null && settingsState.value.enableChanger) {
if (!isAlreadyRunning) {
settingsViewModel.onEvent(SettingsEvent.RefreshNextSetTime)
val intent = Intent(context, WallpaperService::class.java).apply {
action = WallpaperService.Actions.START.toString()
putExtra("timeInMinutes1", settingsState.value.homeInterval)
putExtra("timeInMinutes2", settingsState.value.lockInterval)
putExtra("scheduleSeparately", settingsState.value.scheduleSeparately)
}
context.startForegroundService(intent)
}
} else {
if (isAlreadyRunning) {
Intent(context, WallpaperService::class.java).also {
it.action = WallpaperService.Actions.STOP.toString()
context.startForegroundService(it)
}
}
}
}
}
PaperizeTheme(settingsState.value.darkMode, settingsState.value.dynamicTheming) {
Surface(tonalElevation = 5.dp) {
PaperizeApp(isFirstLaunch, topInset)
Expand Down
Loading

0 comments on commit 64dedae

Please sign in to comment.