Skip to content

Commit baf554c

Browse files
committed
Adding more download type handling and unit tests.
1 parent 42b7b6b commit baf554c

File tree

3 files changed

+208
-79
lines changed

3 files changed

+208
-79
lines changed

firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/FirebaseModelDownloader.java

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,27 +115,25 @@ public Task<CustomModel> getModel(
115115
@Nullable CustomModelDownloadConditions conditions)
116116
throws Exception {
117117
CustomModel localModel = sharedPreferencesUtil.getCustomModelDetails(modelName);
118+
if (localModel == null) {
119+
// no local model - get latest.
120+
return getCustomModelTask(modelName, conditions);
121+
}
122+
118123
switch (downloadType) {
119124
case LOCAL_MODEL:
120-
if (localModel != null) {
121-
return Tasks.forResult(localModel);
122-
}
123-
return getCustomModelTask(modelName, conditions);
125+
return Tasks.forResult(localModel);
124126
case LATEST_MODEL:
125-
// check for latest model, wait for latest if needed download newest
126-
return getCustomModelTask(modelName, conditions, localModel.getModelHash());
127-
case LOCAL_MODEL_UPDATE_IN_BACKGROUND:
128-
// start download in back ground return current model if not null.
129-
if (localModel != null) {
130-
// trigger update in background as needed - ignoring response.
131-
getCustomModelTask(modelName, conditions, localModel.getModelHash());
132-
// return local model
133-
return Tasks.forResult(localModel);
134-
}
135-
// no local model - get latest.
127+
// check for latest model, wait for download if newer model exists
136128
return getCustomModelTask(modelName, conditions);
129+
case LOCAL_MODEL_UPDATE_IN_BACKGROUND:
130+
// start download in back ground, return current model
131+
getCustomModelTask(modelName, conditions, localModel.getModelHash());
132+
// return local model
133+
return Tasks.forResult(localModel);
137134
}
138-
throw new UnsupportedOperationException("Not yet implemented.");
135+
throw new IllegalArgumentException(
136+
"Unsupported downloadType, please chose LOCAL_MODEL, LATEST_MODEL, or LOCAL_MODEL_UPDATE_IN_BACKGROUND");
139137
}
140138

141139
// This version of getCustomModelTask will always call the modelDownloadService and upon
@@ -151,7 +149,7 @@ private Task<CustomModel> getCustomModelTask(
151149
private Task<CustomModel> getCustomModelTask(
152150
@NonNull String modelName,
153151
@Nullable CustomModelDownloadConditions conditions,
154-
String modelHash)
152+
@Nullable String modelHash)
155153
throws Exception {
156154
Task<CustomModel> incomingModelDetails =
157155
modelDownloadService.getCustomModelDetails(
@@ -161,18 +159,22 @@ private Task<CustomModel> getCustomModelTask(
161159
executor,
162160
incomingModelDetailTask -> {
163161
if (incomingModelDetailTask.isSuccessful()) {
164-
// null means we have the latest model
165162
CustomModel currentModel = sharedPreferencesUtil.getCustomModelDetails(modelName);
163+
// null means we have the latest model
166164
if (incomingModelDetails.getResult() == null) {
167165
return Tasks.forResult(currentModel);
168166
}
169167

170168
// if modelHash matches current local model just return local model.
171-
if (currentModel.getModelHash().equals(incomingModelDetails.getResult().getModelHash())) {
169+
if (currentModel
170+
.getModelHash()
171+
.equals(incomingModelDetails.getResult().getModelHash())) {
172172
if (!currentModel.getLocalFilePath().isEmpty()) {
173173
return Tasks.forResult(currentModel);
174174
}
175-
// todo(annzimmer) wait for download? continue?
175+
// todo(annzimmer) this shouldn't happen unless they are calling the sdk with multiple
176+
// sets of download types/conditions.
177+
// this should be a download in progress - add appropriate handling.
176178
}
177179

178180
// start download
@@ -184,9 +186,9 @@ private Task<CustomModel> getCustomModelTask(
184186
if (downloadTask.isSuccessful()) {
185187
// read the updated model
186188
CustomModel downloadedModel =
187-
currentModel;
188-
// TODO(annz) trigger file move here as well... right now it's temp
189-
// call loadNewlyDownloadedModelFile
189+
sharedPreferencesUtil.getCustomModelDetails(modelName);
190+
// trigger the file to be moved to permanent location.
191+
fileDownloadService.loadNewlyDownloadedModelFile(downloadedModel);
190192
return Tasks.forResult(downloadedModel);
191193
}
192194
return Tasks.forException(new Exception("File download failed."));

firebase-ml-modeldownloader/src/main/java/com/google/firebase/ml/modeldownloader/internal/ModelFileDownloadService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public File loadNewlyDownloadedModelFile(CustomModel model) throws Exception {
299299
new CustomModel(
300300
model.getName(), model.getModelHash(), model.getSize(), 0, newModelFile.getPath()));
301301

302-
// Cleans up the old files if it is the initial creation.
302+
// todo(annzimmer) Cleans up the old files if it is the initial creation.
303303
return newModelFile;
304304
} else if (statusCode == DownloadManager.STATUS_FAILED) {
305305
// reset original model - removing download id.

0 commit comments

Comments
 (0)