Google is committed to advancing racial equity for Black communities. See how.

FirebaseModelDownloader

public class FirebaseModelDownloader extends Object

Public Method Summary

Task<Void>
deleteDownloadedModel(String modelName)
Deletes the local model.
static FirebaseModelDownloader
getInstance()
Returns the FirebaseModelDownloader initialized with the default FirebaseApp.
static FirebaseModelDownloader
getInstance(FirebaseApp app)
Returns the FirebaseModelDownloader initialized with a custom FirebaseApp.
Task<CustomModel>
getModel(String modelName, DownloadType downloadType, CustomModelDownloadConditions conditions)
Gets the downloaded model file based on download type and conditions.
Task<Long>
getModelDownloadId(String modelName, Task<CustomModel> getModelTask)
Gets the current model's download ID (returns background download ID when applicable).
Task<Set<CustomModel>>
listDownloadedModels()
Lists all models downloaded to device.
void
setModelDownloaderCollectionEnabled(Boolean enabled)
Enables stats collection in Firebase ML ModelDownloader via Firelog.

Inherited Method Summary

Public Methods

public Task<Void> deleteDownloadedModel (String modelName)

Deletes the local model. Removes any information and files associated with the model name.

Parameters
modelName Name of the model.

public static FirebaseModelDownloader getInstance ()

Returns the FirebaseModelDownloader initialized with the default FirebaseApp.

Returns

public static FirebaseModelDownloader getInstance (FirebaseApp app)

Returns the FirebaseModelDownloader initialized with a custom FirebaseApp.

Parameters
app A custom FirebaseApp
Returns

public Task<CustomModel> getModel (String modelName, DownloadType downloadType, CustomModelDownloadConditions conditions)

Gets the downloaded model file based on download type and conditions. DownloadType behaviours:

  • LOCAL_MODEL: returns the current model if present, otherwise triggers new download (or finds one in progress) and only completes when download is finished
  • LOCAL_MODEL_UPDATE_IN_BACKGROUND: returns the current model if present and triggers an update to fetch a new version in the background. If no local model is present triggers a new download (or finds one in progress) and only completes when download is finished.
  • LATEST_MODEL: returns the latest model. Checks if latest model is different from local model. If the models are the same, returns the current model. Otherwise, triggers a new model download and returns when this download finishes.
Most common exceptions include:
Parameters
modelName Model name.
downloadType DownloadType to determine which model to return.
conditions CustomModelDownloadConditions to be used during file download.
Returns
  • Custom model

public Task<Long> getModelDownloadId (String modelName, Task<CustomModel> getModelTask)

Gets the current model's download ID (returns background download ID when applicable). This ID can be used to create a progress bar to track file download progress.

[Preferred] If getModelTask is not null, then this task returns when the download ID is not 0 (download has been enqueued) or when the getModelTask completes (returning 0).

If getModelTask is null, then this task immediately returns the download ID of the model. This will be 0 if the model doesn't exist, the model has completed downloading, or the download hasn't been enqueued.

Parameters
modelName Model name.
getModelTask The most recent getModel task associated with the model name.
Returns
  • Download ID associated with Android DownloadManager.

public Task<Set<CustomModel>> listDownloadedModels ()

Lists all models downloaded to device.

Returns
  • The set of all models that are downloaded to this device.

public void setModelDownloaderCollectionEnabled (Boolean enabled)

Enables stats collection in Firebase ML ModelDownloader via Firelog. The stats include API calls counts, errors, API call durations, options, etc. No personally identifiable information is logged.

The setting is set by the initialization of FirebaseApp, and it is persistent together with the app's private data. It means that if the user uninstalls the app or clears all app data, the setting will be erased. The best practice is to set the flag in each initialization.

By default, the logging matches the Firebase-wide data collection switch.

Parameters
enabled Turns the logging state on or off. To revert to using the Firebase-wide data collection switch, set this value to null.