DownloadPreference: Add a worker to regularly clear downloads cache

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2024-03-22 17:53:34 +05:30
parent 43e63a9452
commit 68733e1ec1
6 changed files with 114 additions and 1 deletions

View File

@@ -0,0 +1,87 @@
package com.aurora.store.data.work
import android.content.Context
import android.util.Log
import androidx.hilt.work.HiltWorker
import androidx.work.CoroutineWorker
import androidx.work.ExistingPeriodicWorkPolicy.KEEP
import androidx.work.PeriodicWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import com.aurora.store.util.PathUtil
import com.aurora.store.util.Preferences
import com.aurora.store.util.Preferences.PREFERENCE_DOWNLOADS_CACHE_TIME
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import java.io.File
import java.util.Calendar
import java.util.concurrent.TimeUnit.HOURS
import java.util.concurrent.TimeUnit.MINUTES
import kotlin.time.DurationUnit
import kotlin.time.toDuration
@HiltWorker
class CleanCacheWorker @AssistedInject constructor(
@Assisted private val appContext: Context,
@Assisted workerParams: WorkerParameters
) : CoroutineWorker(appContext, workerParams) {
companion object {
private const val TAG = "CleanCacheWorker"
private const val CLEAN_CACHE_WORKER = "CLEAN_CACHE_WORKER"
fun scheduleAutomatedCacheCleanup(context: Context) {
val periodicWorkRequest = PeriodicWorkRequestBuilder<CleanCacheWorker>(
repeatInterval = 1,
repeatIntervalTimeUnit = HOURS,
flexTimeInterval = 30,
flexTimeIntervalUnit = MINUTES
).build()
Log.i(TAG, "Scheduling periodic cache cleanup!")
WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(CLEAN_CACHE_WORKER, KEEP, periodicWorkRequest)
}
}
private val cacheDuration = Preferences.getInteger(appContext, PREFERENCE_DOWNLOADS_CACHE_TIME, 6)
.toLong()
.toDuration(DurationUnit.HOURS)
override suspend fun doWork(): Result {
Log.i(TAG, "Cleaning cache")
PathUtil.getOldDownloadDirectories(appContext).forEach { downloadDir -> // Downloads
Log.i(TAG, "Deleting old unused download directory: $downloadDir")
downloadDir.deleteRecursively()
}
PathUtil.getDownloadDirectory(appContext).listFiles()?.forEach { download -> // com.example.app
// Delete if the download directory is empty
if (download.listFiles().isNullOrEmpty()) {
Log.i(TAG, "Removing empty download directory for ${download.name}")
download.deleteRecursively(); return@forEach
}
download.listFiles()!!.forEach { versionCode -> // 20240325
if (versionCode.listFiles().isNullOrEmpty()) {
// Purge empty non-accessible directory
Log.i(TAG, "Removing empty directory for ${download.name}, ${versionCode.name}")
versionCode.deleteRecursively()
} else {
versionCode.deleteIfOld()
}
}
}
return Result.success()
}
private fun File.deleteIfOld() {
val elapsedTime = Calendar.getInstance().timeInMillis - this.lastModified()
if (elapsedTime.toDuration(DurationUnit.HOURS) > cacheDuration) {
Log.i(TAG, "Removing $this older than ${cacheDuration.inWholeHours} hours")
this.deleteRecursively()
}
}
}

View File

@@ -31,6 +31,13 @@ object PathUtil {
private const val DOWNLOADS = "Downloads"
private const val SPOOF = "SpoofConfigs"
fun getOldDownloadDirectories(context: Context): List<File> {
return listOf(
File(context.filesDir, DOWNLOADS), // till 4.4.2
File(context.getExternalFilesDir(null), DOWNLOADS) // till 4.4.2
)
}
fun getDownloadDirectory(context: Context): File {
return File(context.cacheDir, DOWNLOADS)
}

View File

@@ -44,6 +44,7 @@ object Preferences {
const val PREFERENCE_FILTER_AURORA_ONLY = "PREFERENCE_FILTER_AURORA_ONLY"
const val PREFERENCE_AUTO_INSTALL = "PREFERENCE_AUTO_INSTALL"
const val PREFERENCE_DOWNLOADS_CACHE_TIME = "PREFERENCE_DOWNLOADS_CACHE_TIME"
const val PREFERENCE_AUTO_DELETE = "PREFERENCE_AUTO_DELETE"
const val INSTALLATION_ABANDON_SESSION = "INSTALLATION_ABANDON_SESSION"

View File

@@ -29,6 +29,7 @@ import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.aurora.extensions.isIgnoringBatteryOptimizations
import com.aurora.store.R
import com.aurora.store.data.work.CleanCacheWorker
import com.aurora.store.data.work.UpdateWorker
import com.aurora.store.databinding.FragmentOnboardingBinding
import com.aurora.store.util.Preferences
@@ -129,6 +130,7 @@ class OnboardingFragment : Fragment(R.layout.fragment_onboarding) {
binding.btnForward.isEnabled = true
binding.btnForward.setOnClickListener {
setupAutoUpdates()
CleanCacheWorker.scheduleAutomatedCacheCleanup(requireContext())
save(PREFERENCE_INTRO, true)
findNavController().navigate(
OnboardingFragmentDirections.actionOnboardingFragmentToSplashFragment()

View File

@@ -435,4 +435,8 @@
<!-- MoreDialogFragment -->
<string name="title_more">More</string>
<string name="manage_account">Manage your account</string>
<!-- DownloadPreference -->
<string name="pref_download_cache_title">Keep downloads cache</string>
<string name="pref_download_cache_summary">How long to preserve downloads cache (failed, cancelled and completed), in hours</string>
</resources>

View File

@@ -17,7 +17,8 @@
~
-->
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreferenceCompat
app:defaultValue="true"
@@ -26,4 +27,15 @@
app:summary="@string/pref_install_delete_summary"
app:title="@string/pref_install_delete_title" />
<SeekBarPreference
android:defaultValue="6"
android:key="PREFERENCE_DOWNLOADS_CACHE_TIME"
android:max="24"
android:summary="@string/pref_download_cache_summary"
android:title="@string/pref_download_cache_title"
app:adjustable="true"
app:iconSpaceReserved="false"
app:min="1"
app:showSeekBarValue="true" />
</androidx.preference.PreferenceScreen>