FirebaseModelDownloader

class FirebaseModelDownloader


Summary

Public functions

Task<Void!>

Deletes the local model.

java-static FirebaseModelDownloader

Returns the FirebaseModelDownloader initialized with the default FirebaseApp.

java-static FirebaseModelDownloader

Returns the FirebaseModelDownloader initialized with a custom FirebaseApp.

Task<CustomModel!>
getModel(
    modelName: String,
    downloadType: DownloadType,
    conditions: CustomModelDownloadConditions?
)

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

Task<Long!>
getModelDownloadId(modelName: String, getModelTask: Task<CustomModel!>?)

Gets the current model's download ID (returns background download ID when applicable).

Task<(Mutable)Set<CustomModel!>!>

Lists all models downloaded to device.

Unit

Enables stats collection in Firebase ML ModelDownloader via Firelog.

Public properties

CustomModel.Factory!

Public functions

deleteDownloadedModel

fun deleteDownloadedModel(modelName: String): Task<Void!>

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

Parameters
modelName: String

Name of the model.

getInstance

java-static fun getInstance(): FirebaseModelDownloader

Returns the FirebaseModelDownloader initialized with the default FirebaseApp.

getInstance

java-static fun getInstance(app: FirebaseApp): FirebaseModelDownloader

Returns the FirebaseModelDownloader initialized with a custom FirebaseApp.

Parameters
app: FirebaseApp

A custom FirebaseApp

getModel

fun getModel(
    modelName: String,
    downloadType: DownloadType,
    conditions: CustomModelDownloadConditions?
): Task<CustomModel!>

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:
  • NO_NETWORK_CONNECTION: Error connecting to the network.
  • NOT_FOUND: No model found with the given name.
  • NOT_ENOUGH_SPACE: Not enough space on device to download model.
  • DOWNLOAD_URL_EXPIRED: URL used to fetch model expired before model download completed. (This return is rare; these calls are retried internally before being raised.)
Parameters
modelName: String

Model name.

downloadType: DownloadType

DownloadType to determine which model to return.

conditions: CustomModelDownloadConditions?

CustomModelDownloadConditions to be used during file download.

Returns
Task<CustomModel!>

Custom model

getModelDownloadId

fun getModelDownloadId(modelName: String, getModelTask: Task<CustomModel!>?): Task<Long!>

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: String

Model name.

getModelTask: Task<CustomModel!>?

The most recent getModel task associated with the model name.

Returns
Task<Long!>

Download ID associated with Android DownloadManager.

listDownloadedModels

fun listDownloadedModels(): Task<(Mutable)Set<CustomModel!>!>

Lists all models downloaded to device.

Returns
Task<(Mutable)Set<CustomModel!>!>

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

setModelDownloaderCollectionEnabled

fun setModelDownloaderCollectionEnabled(enabled: Boolean?): Unit

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: Boolean?

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

Public properties

modelFactory

val modelFactoryCustomModel.Factory!