Skip to main content

Downloads Cleaner

The downloads cleaner allows you to get information about the files in the downloads directory and clear the selected items.

important

Starting with Android 10, devices use a new type of Scoped Storage. For the downloads 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 download cleaner through the cleaner managerval downloadCleaner = CleanerManagerFactory.cleanerManager.getCleaner(CleanerType.DOWNLOAD)
// Get coroutine scope to call cleaner library functionsval cleanerScope = CleanerManagerFactory.cleanerManager.scope
// Run all operations related to libraries only in the cleaner librarycleanerScope.launch {        // Start the analysis process with the following call    downloadCleaner.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 cleaner downloads through the cleaner managerval downloadCleaner = CleanerManagerFactory.cleanerManager.getCleaner(CleanerType.DOWNLOAD)
// Get coroutine scope to call cleaner library functionsval cleanerScope = CleanerManagerFactory.cleanerManager.scope
// Get a list of items to removeval trashItemList: List<TrashItem>
// Run all operations related to libraries only in the cleaner librarycleanerScope.launch {    // Start the analysis process with the following call    downloadCleaner.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 download cleaner through the cleaner managerval downloadCleaner = CleanerManagerFactory.cleanerManager.getCleaner(CleanerType.DOWNLOAD)
// Get coroutine scope to call cleaner library functionsval cleanerScope = CleanerManagerFactory.cleanerManager.scope
// Run all operations related to libraries only in the cleaner librarycleanerScope.launch {    // Subscribe to the states of the download cleaner    downloadCleaner.stateFlow.collect { cleanerState -> // cleanerState: CleanerState        // Do something...    }}

The downloads 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<DownloadItem> - 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<DownloadItem> - list of deleted items
  • error: Trowable? - an error occurred during the cleaning process

Links - CleanerState, DownloadItem, Cleaner.