Last active
November 3, 2020 15:01
-
-
Save rahulsainani/595c17b70cec09665b0f07c3284f45f7 to your computer and use it in GitHub Desktop.
Clean Cache Worker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Application | |
import androidx.work.Constraints | |
import androidx.work.ExistingPeriodicWorkPolicy | |
import androidx.work.PeriodicWorkRequestBuilder | |
import androidx.work.WorkManager | |
import java.util.concurrent.TimeUnit | |
class App: Application(){ | |
override fun onCreate() { | |
super.onCreate() | |
setupCleanCacheWorker() | |
} | |
fun setupCleanCacheWorker(){ | |
val constraints = | |
Constraints.Builder() | |
.setRequiresBatteryNotLow(true) | |
.setRequiresDeviceIdle(true) | |
.build() | |
val clearCacheWorkRequest = | |
PeriodicWorkRequestBuilder<ClearAppCacheWorker>(REPEAT_INTERVAL_IN_DAYS, TimeUnit.DAYS) | |
.setConstraints(constraints) | |
.setInitialDelay(INITIAL_DELAY_IN_DAYS, TimeUnit.DAYS) | |
.build() | |
WorkManager.getInstance(app).enqueueUniquePeriodicWork( | |
ClearAppCacheWorker::class.java.simpleName, | |
ExistingPeriodicWorkPolicy.KEEP, | |
clearCacheWorkRequest | |
) | |
} | |
companion object { | |
private const val REPEAT_INTERVAL_IN_DAYS = 7L // Configure based on the app | |
private const val INITIAL_DELAY_IN_DAYS = 3L | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import androidx.work.CoroutineWorker | |
import androidx.work.ListenableWorker | |
import androidx.work.WorkerParameters | |
import kotlinx.coroutines.Dispatchers | |
import timber.log.Timber | |
class ClearAppCacheWorker( | |
private val context: Context, | |
workerParameters: WorkerParameters | |
) : CoroutineWorker(context, workerParameters) { | |
override suspend fun doWork(): Result = with(Dispatchers.IO) { | |
Timber.d("App cache work started") | |
val cacheDirectorySize = context.cacheDir.directorySizeInKiloBytes() | |
Timber.d("Cache directory size: $cacheDirectorySize kB") | |
track("clear_cache", cacheDirectorySize))) | |
val success = context.cacheDir.deleteRecursively() | |
if (success) { | |
Timber.d("App cache work successfully cleared") | |
Result.success() | |
} else { | |
Timber.d("App cache work failed") | |
Result.failure() | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File | |
fun File.directorySizeInKiloBytes(): Long = | |
walkBottomUp() | |
.sumByDouble { it.length().toDouble() } | |
.div(BYTES_IN_KB) | |
.roundToLong() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment