Skip to main content

Thumbnail Cleaner

This type of cleaner is used to clean up the ../.thumblails system folder. This folder is responsible for storing the cache of thumbnails of images on the device. With this type of cleaner, you can get a list of all files in the ../.thumbnails system folder and delete the selected ones.

important

Starting with Android 10, devices use a new type of Scoped Storage. For the thumbnail cleaner to work within Scoped Storage, you need a MANAGE_EXTERNAL_STORAGE permission

Analysis​

The following code example demonstrates how to use the analysis operation:

// Get the thumbnail cleaner through the cleaner manager
val thumbCleaner = CleanerManagerFactory.cleanerManager.getCleaner(CleanerType.THUMBNAIL)

// Get coroutine scope to call cleaner library functions
val cleanerScope = CleanerManagerFactory.cleanerManager.scope

// Run all operations related to libraries only in the cleaner library
cleanerScope.launch {

// Start the analysis process with the following call
thumbCleaner.analyze(
isFullRefresh = true
)
}

The cleaner is represented by an object that implements the interface Cleaner, the method analyze which takes isFullRefresh: Boolean:

  • true - when analyzing, the cleaner scans the system again;
  • false - the method returns the cached result of the parsing (only non-deleted items).

Cleaning​

Use the following code example to clear items.

// Get the thumbnail cleaner through the cleaner manager
val thumbCleaner = CleanerManagerFactory.cleanerManager.getCleaner(CleanerType.THUMBNAIL)

// Get coroutine scope to call cleaner library functions
val cleanerScope = CleanerManagerFactory.cleanerManager.scope

// Get a list of items to remove
val trashItemList: List<TrashItem>

// Run all operations related to libraries only in the cleaner library
cleanerScope.launch {
// Start the analysis process with the following call
thumbCleaner.clean(
content = trashItemList
)
}

A cleaner is represented by an object that implements the Cleaner interface, whose clean method accepts content: List<TrashItem> (list of items to remove)

Getting results​

In order to get the state of the cleaner and the results of its work, you need to subscribe to its state as follows:

// Get the thumbnail cleaner through the cleaner manager
val thumbCleaner = CleanerManagerFactory.cleanerManager.getCleaner(CleanerType.THUMBNAIL)

// Get coroutine scope to call cleaner library functions
val cleanerScope = CleanerManagerFactory.cleanerManager.scope

// Run all operations related to libraries only in the cleaner library
cleanerScope.launch {
// Subscribe to the states of the thumbnail cleaner
thumbCleaner.stateFlow.collect { cleanerState -> // cleanerState: CleanerState
// Do something...
}
}

Thumbnail cleaner inherits Cleaner interface that provides stateFlow: StateFlow (StateFlow) which emits CleanerState objects containing information about the condition of the cleaner. Here are the following possible states of the cleaner:

Inactive - this is the initial state of the cleaner before starting work.

Analyzing - this state indicates that the cleaner is in the process of analyzing data. It includes the following fields:

  • itemCount: Int - number of items found
  • size: Long - size of found elements in bits

Analysed - this state indicates that the cleaner has finished analyzing the data. It includes the following fields:

  • items: List<ThumbItem> - list of found items
  • error: Trowable? - an error occurred during the analysis process

Cleaning -this state indicates that the cleaner is in the process of deleting data. It includes the following fields:

  • itemCount: Int - number of items found
  • size: Long - size of found elements in bits

Cleaned - this state indicates that the cleaner has completed the data cleaning process. It includes the following fields:

  • items: List<ThumbItem> - list of deleted items
  • error: Trowable? - an error occurred during the cleaning process

Links - CleanerState, ThumbItem, Cleaner.