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)
Delete 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)
Get the downloaded model file based on download type and conditions.
Task<Long>
getModelDownloadId(String modelName, Task<CustomModel> getModelTask)
Get the current models' 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)

Delete 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)

Get 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: check for latest model, if different from local model, trigger new download, task only completes when 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)

Get the current models' 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 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 Download Manager.

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 per FirebaseApp, and it is persistent together with app's private data. It means 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 Is logging enabled, set to null (default) to use Firebase wide data collection switch.