FirebaseModelDownloader

public class FirebaseModelDownloader


Summary

Public fields

final CustomModel.Factory

Public methods

@NonNull Task<Void>

Deletes the local model.

static @NonNull FirebaseModelDownloader

Returns the FirebaseModelDownloader initialized with the default FirebaseApp.

static @NonNull FirebaseModelDownloader

Returns the FirebaseModelDownloader initialized with a custom FirebaseApp.

@NonNull Task<CustomModel>
getModel(
    @NonNull String modelName,
    @NonNull DownloadType downloadType,
    @Nullable CustomModelDownloadConditions conditions
)

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

@NonNull Task<Long>
getModelDownloadId(
    @NonNull String modelName,
    @Nullable Task<CustomModel> getModelTask
)

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

@NonNull Task<Set<CustomModel>>

Lists all models downloaded to device.

void

Enables stats collection in Firebase ML ModelDownloader via Firelog.

Public fields

modelFactory

public final CustomModel.Factory modelFactory

Public methods

deleteDownloadedModel

public @NonNull Task<VoiddeleteDownloadedModel(@NonNull String modelName)

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

Parameters
@NonNull String modelName

Name of the model.

getInstance

public static @NonNull FirebaseModelDownloader getInstance()

Returns the FirebaseModelDownloader initialized with the default FirebaseApp.

getInstance

public static @NonNull FirebaseModelDownloader getInstance(@NonNull FirebaseApp app)

Returns the FirebaseModelDownloader initialized with a custom FirebaseApp.

Parameters
@NonNull FirebaseApp app

A custom FirebaseApp

getModel

public @NonNull Task<CustomModelgetModel(
    @NonNull String modelName,
    @NonNull DownloadType downloadType,
    @Nullable 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:
  • 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
@NonNull String modelName

Model name.

@NonNull DownloadType downloadType

DownloadType to determine which model to return.

@Nullable CustomModelDownloadConditions conditions

CustomModelDownloadConditions to be used during file download.

Returns
@NonNull Task<CustomModel>

Custom model

getModelDownloadId

public @NonNull Task<LonggetModelDownloadId(
    @NonNull String modelName,
    @Nullable 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
@NonNull String modelName

Model name.

@Nullable Task<CustomModel> getModelTask

The most recent getModel task associated with the model name.

Returns
@NonNull Task<Long>

Download ID associated with Android DownloadManager.

listDownloadedModels

public @NonNull Task<Set<CustomModel>> listDownloadedModels()

Lists all models downloaded to device.

Returns
@NonNull Task<Set<CustomModel>>

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

setModelDownloaderCollectionEnabled

public void setModelDownloaderCollectionEnabled(@Nullable 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
@Nullable Boolean enabled

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