Skip to content

Commit

Permalink
Merge pull request #1 from TeamAmaze/trigger-cleanup
Browse files Browse the repository at this point in the history
Trigger cleanup criteria on launch
  • Loading branch information
VishalNehra authored Oct 30, 2023
2 parents 77c12e7 + a5e68ee commit a825bda
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
org.gradle.java.home=/Applications/Android Studio.app/Contents/jbr/Contents/Home
30 changes: 30 additions & 0 deletions trashbin/src/main/java/com/amaze/trashbin/TrashBin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
*/
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.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.FileReader
import java.io.FileWriter

Expand All @@ -36,6 +40,8 @@ typealias ListTrashBinFilesCallback = (parentTrashBinPath: String) -> List<Trash
* Pass null if you want to invoke cleanup manually through triggerCleanup. This will be executed in the same thread where your bin functions are executed.
*/
class TrashBin constructor(
context: Context,
doTriggerCleanup: Boolean,
var trashConfig: TrashBinConfig,
var deletePermanentlySuperCallback: DeletePermanentlyCallback?,
var listTrashBinFilesSuperCallback:
Expand All @@ -47,6 +53,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 (trashConfig.getCleanupIntervalHours() != -1 &&
hours >= trashConfig.getCleanupIntervalHours() &&
doTriggerCleanup && deletePermanentlySuperCallback != null
) {
GlobalScope.launch(Dispatchers.IO) {
triggerCleanup {
sharedPreferences.edit().putLong(
"com.amaze.trashbin.lastCleanup",
currentTime
).apply()
false
}
}
}
}

fun deletePermanently(
Expand Down
6 changes: 6 additions & 0 deletions trashbin/src/main/java/com/amaze/trashbin/TrashBinConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ data class TrashBinConfig(
val retentionDays: Int,
val retentionBytes: Long,
val retentionNumOfFiles: Int,
val cleanupHours: Int = -1,
val deleteRogueFiles: Boolean,
val triggerCleanupAutomatically: Boolean
) {
Expand All @@ -37,6 +38,7 @@ data class TrashBinConfig(
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"
Expand Down Expand Up @@ -65,4 +67,8 @@ data class TrashBinConfig(
}
return basePath + File.separator + TRASH_BIN_META_FILE
}

fun getCleanupIntervalHours(): Int {
return cleanupHours
}
}

0 comments on commit a825bda

Please sign in to comment.