@@ -115,27 +115,25 @@ public Task<CustomModel> getModel(
115
115
@ Nullable CustomModelDownloadConditions conditions )
116
116
throws Exception {
117
117
CustomModel localModel = sharedPreferencesUtil .getCustomModelDetails (modelName );
118
+ if (localModel == null ) {
119
+ // no local model - get latest.
120
+ return getCustomModelTask (modelName , conditions );
121
+ }
122
+
118
123
switch (downloadType ) {
119
124
case LOCAL_MODEL :
120
- if (localModel != null ) {
121
- return Tasks .forResult (localModel );
122
- }
123
- return getCustomModelTask (modelName , conditions );
125
+ return Tasks .forResult (localModel );
124
126
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
136
128
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 );
137
134
}
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" );
139
137
}
140
138
141
139
// This version of getCustomModelTask will always call the modelDownloadService and upon
@@ -151,7 +149,7 @@ private Task<CustomModel> getCustomModelTask(
151
149
private Task <CustomModel > getCustomModelTask (
152
150
@ NonNull String modelName ,
153
151
@ Nullable CustomModelDownloadConditions conditions ,
154
- String modelHash )
152
+ @ Nullable String modelHash )
155
153
throws Exception {
156
154
Task <CustomModel > incomingModelDetails =
157
155
modelDownloadService .getCustomModelDetails (
@@ -161,18 +159,22 @@ private Task<CustomModel> getCustomModelTask(
161
159
executor ,
162
160
incomingModelDetailTask -> {
163
161
if (incomingModelDetailTask .isSuccessful ()) {
164
- // null means we have the latest model
165
162
CustomModel currentModel = sharedPreferencesUtil .getCustomModelDetails (modelName );
163
+ // null means we have the latest model
166
164
if (incomingModelDetails .getResult () == null ) {
167
165
return Tasks .forResult (currentModel );
168
166
}
169
167
170
168
// 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 ())) {
172
172
if (!currentModel .getLocalFilePath ().isEmpty ()) {
173
173
return Tasks .forResult (currentModel );
174
174
}
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.
176
178
}
177
179
178
180
// start download
@@ -184,9 +186,9 @@ private Task<CustomModel> getCustomModelTask(
184
186
if (downloadTask .isSuccessful ()) {
185
187
// read the updated model
186
188
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 );
190
192
return Tasks .forResult (downloadedModel );
191
193
}
192
194
return Tasks .forException (new Exception ("File download failed." ));
0 commit comments