From 70037118416eb946a4a192470f5dd3036bdd2e5f Mon Sep 17 00:00:00 2001 From: VishnuSanal Date: Tue, 31 Oct 2023 00:28:54 +0530 Subject: [PATCH 1/2] trigger cleanup criteria on launch --- .../main/java/com/amaze/trashbin/TrashBin.kt | 30 ++++++++++++++++++- .../java/com/amaze/trashbin/TrashBinConfig.kt | 8 ++++- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/trashbin/src/main/java/com/amaze/trashbin/TrashBin.kt b/trashbin/src/main/java/com/amaze/trashbin/TrashBin.kt index f67f827..4e5392f 100644 --- a/trashbin/src/main/java/com/amaze/trashbin/TrashBin.kt +++ b/trashbin/src/main/java/com/amaze/trashbin/TrashBin.kt @@ -16,11 +16,14 @@ */ package com.amaze.trashbin +import android.content.Context import android.util.Log import com.google.gson.Gson import com.google.gson.GsonBuilder import com.google.gson.reflect.TypeToken import com.google.gson.stream.JsonReader +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import java.io.FileReader import java.io.FileWriter @@ -39,7 +42,8 @@ class TrashBin constructor( var trashConfig: TrashBinConfig, var deletePermanentlySuperCallback: DeletePermanentlyCallback?, var listTrashBinFilesSuperCallback: - ListTrashBinFilesCallback? = null + ListTrashBinFilesCallback? = null, + context: Context ) { private var metadata: TrashBinMetadata? = null @@ -47,6 +51,30 @@ class TrashBin constructor( init { trashConfig.getTrashBinFilesDirectory() metadata = getTrashBinMetadata() + + val sharedPreferences = context.getSharedPreferences( + "${context.packageName}.com.amaze.trashbin", + Context.MODE_PRIVATE + ) + + val lastCleanup = sharedPreferences.getLong("com.amaze.trashbin.lastCleanup", -1) + + val currentTime = System.currentTimeMillis() + + val hours = ((lastCleanup - currentTime) / (1000 * 60 * 60)) + + if (hours >= trashConfig.getCleanupIntervalHours()) { + + GlobalScope.launch { + triggerCleanup { + + sharedPreferences.edit().putLong("com.amaze.trashbin.lastCleanup", currentTime).apply() + + false + } + } + } + } fun deletePermanently( diff --git a/trashbin/src/main/java/com/amaze/trashbin/TrashBinConfig.kt b/trashbin/src/main/java/com/amaze/trashbin/TrashBinConfig.kt index 275f3c0..925e46a 100644 --- a/trashbin/src/main/java/com/amaze/trashbin/TrashBinConfig.kt +++ b/trashbin/src/main/java/com/amaze/trashbin/TrashBinConfig.kt @@ -30,7 +30,9 @@ data class TrashBinConfig( val retentionBytes: Long, val retentionNumOfFiles: Int, val deleteRogueFiles: Boolean, - val triggerCleanupAutomatically: Boolean + val triggerCleanupAutomatically: Boolean, + private val cleanupDays: Int, + private val cleanupHours: Int ) { companion object { @@ -65,4 +67,8 @@ data class TrashBinConfig( } return basePath + File.separator + TRASH_BIN_META_FILE } + + fun getCleanupIntervalHours(): Int { + return cleanupDays * 24 + cleanupHours; + } } From a5e68eed9a7ea1d1aa06e6330a45c1b18201aec6 Mon Sep 17 00:00:00 2001 From: Vishal Nehra Date: Tue, 31 Oct 2023 03:54:58 +0530 Subject: [PATCH 2/2] Minor fixes --- gradle.properties | 2 +- .../main/java/com/amaze/trashbin/TrashBin.kt | 32 ++++++++++--------- .../java/com/amaze/trashbin/TrashBinConfig.kt | 8 ++--- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/gradle.properties b/gradle.properties index ced6c8f..9fa7fe8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -21,4 +21,4 @@ kotlin.code.style=official # resources declared in the library itself and none from the library's dependencies, # thereby reducing the size of the R class for that library android.nonTransitiveRClass=true -#org.gradle.java.home=/Applications/Android Studio.app/Contents/jbr/Contents/Home \ No newline at end of file +org.gradle.java.home=/Applications/Android Studio.app/Contents/jbr/Contents/Home \ No newline at end of file diff --git a/trashbin/src/main/java/com/amaze/trashbin/TrashBin.kt b/trashbin/src/main/java/com/amaze/trashbin/TrashBin.kt index 4e5392f..72e6221 100644 --- a/trashbin/src/main/java/com/amaze/trashbin/TrashBin.kt +++ b/trashbin/src/main/java/com/amaze/trashbin/TrashBin.kt @@ -22,6 +22,7 @@ import com.google.gson.Gson import com.google.gson.GsonBuilder import com.google.gson.reflect.TypeToken import com.google.gson.stream.JsonReader +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import java.io.FileReader @@ -39,11 +40,12 @@ typealias ListTrashBinFilesCallback = (parentTrashBinPath: String) -> List= trashConfig.getCleanupIntervalHours()) { - - GlobalScope.launch { + if (trashConfig.getCleanupIntervalHours() != -1 && + hours >= trashConfig.getCleanupIntervalHours() && + doTriggerCleanup && deletePermanentlySuperCallback != null + ) { + GlobalScope.launch(Dispatchers.IO) { triggerCleanup { - - sharedPreferences.edit().putLong("com.amaze.trashbin.lastCleanup", currentTime).apply() - + sharedPreferences.edit().putLong( + "com.amaze.trashbin.lastCleanup", + currentTime + ).apply() false } } } - } fun deletePermanently( diff --git a/trashbin/src/main/java/com/amaze/trashbin/TrashBinConfig.kt b/trashbin/src/main/java/com/amaze/trashbin/TrashBinConfig.kt index 925e46a..4d4fd51 100644 --- a/trashbin/src/main/java/com/amaze/trashbin/TrashBinConfig.kt +++ b/trashbin/src/main/java/com/amaze/trashbin/TrashBinConfig.kt @@ -29,16 +29,16 @@ data class TrashBinConfig( val retentionDays: Int, val retentionBytes: Long, val retentionNumOfFiles: Int, + val cleanupHours: Int = -1, val deleteRogueFiles: Boolean, - val triggerCleanupAutomatically: Boolean, - private val cleanupDays: Int, - private val cleanupHours: Int + val triggerCleanupAutomatically: Boolean ) { companion object { const val RETENTION_DAYS_INFINITE = -1 const val RETENTION_BYTES_INFINITE = -1L const val RETENTION_NUM_OF_FILES = -1 + const val INTERVAL_CLEANUP_HOURS = 1 const val TRASH_BIN_CAPACITY_INVALID = -1 const val TRASH_BIN_DIR = "TrashBinFiles" const val TRASH_BIN_META_FILE = "metadata.json" @@ -69,6 +69,6 @@ data class TrashBinConfig( } fun getCleanupIntervalHours(): Int { - return cleanupDays * 24 + cleanupHours; + return cleanupHours } }