Skip to content

Commit 58cec37

Browse files
authored
Add CustomModelDownloadService to retrieve download details about… (firebase#2134)
* Add new CustomModelDownloadService to retrieve download details about the CustomModel. * Add tests for download service calls * Change to mockwire standalone - now builds locally. Test still needs work and code seems to actually run and not use mockwire? * add first working wiremock unit test. * add more wiremock unit test. * Update the registrar to use CustomModelDownloadService. * Update the registrar to use CustomModelDownloadService. * Update the registrar to use CustomModelDownloadService. * Update the registrar to use CustomModelDownloadService. * Fixing returned task values and tests. * Changing equals asserts. * Fix errors caused by timestamp issues, hardcoding to UTC as that's the correct solution. * Reviewer request updates. * Reviewer request updates. * Reviewer request updates.
1 parent e568cc0 commit 58cec37

File tree

10 files changed

+722
-34
lines changed

10 files changed

+722
-34
lines changed

firebase-ml-modeldownloader/firebase-ml-modeldownloader.gradle

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,23 @@ android {
4545
dependencies {
4646
implementation project(':firebase-common')
4747
implementation project(':firebase-components')
48+
implementation project(':firebase-installations-interop')
49+
runtimeOnly project(':firebase-installations')
4850

4951
implementation 'com.google.android.gms:play-services-tasks:17.2.0'
52+
implementation 'com.google.auto.service:auto-service-annotations:1.0-rc6'
5053
implementation 'javax.inject:javax.inject:1'
5154

5255
compileOnly "com.google.auto.value:auto-value-annotations:1.6.6"
5356
annotationProcessor "com.google.auto.value:auto-value:1.6.5"
5457

5558
testImplementation 'androidx.test:core:1.3.0'
56-
testImplementation 'com.google.truth:truth:1.0.1'
57-
testImplementation 'junit:junit:4.13'
59+
testImplementation 'com.github.tomakehurst:wiremock-standalone:2.26.3'
60+
testImplementation "com.google.truth:truth:$googleTruthVersion"
61+
testImplementation 'junit:junit:4.13.1'
62+
//Android compatible version of Apache httpclient.
63+
testImplementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
5864
testImplementation 'org.mockito:mockito-core:3.3.3'
5965
testImplementation "org.robolectric:robolectric:$robolectricVersion"
66+
testImplementation 'com.google.truth.extensions:truth-proto-extension:1.0'
6067
}

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

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,89 @@ public class CustomModel {
3030
private final long fileSize;
3131
private final String modelHash;
3232
private final String localFilePath;
33+
private final String downloadUrl;
34+
private final long downloadUrlExpiry;
3335

3436
/**
3537
* Use when creating a custom model while the initial download is still in progress.
3638
*
3739
* @param name - model name
38-
* @param downloadId - Android Download Manger - download id
40+
* @param modelHash - model hash
3941
* @param fileSize - model file size
40-
* @param modelHash - model hash size
42+
* @param downloadId - Android Download Manger - download id
4143
* @hide
4244
*/
4345
public CustomModel(
44-
@NonNull String name, long downloadId, long fileSize, @NonNull String modelHash) {
45-
this(name, downloadId, fileSize, modelHash, "");
46+
@NonNull String name, @NonNull String modelHash, long fileSize, long downloadId) {
47+
this(name, modelHash, fileSize, downloadId, "", "", 0);
4648
}
4749

4850
/**
49-
* Use when creating a custom model while the initial download is still in progress.
51+
* Use when creating a custom model from a stored model with a new download in the background.
5052
*
5153
* @param name - model name
52-
* @param downloadId - Android Download Manger - download id
54+
* @param modelHash - model hash
5355
* @param fileSize - model file size
54-
* @param modelHash - model hash size
55-
* @param localFilePath - location of the current file
56+
* @param downloadId - Android Download Manger - download id
5657
* @hide
5758
*/
5859
public CustomModel(
5960
@NonNull String name,
61+
@NonNull String modelHash,
62+
long fileSize,
6063
long downloadId,
64+
String localFilePath) {
65+
this(name, modelHash, fileSize, downloadId, localFilePath, "", 0);
66+
}
67+
68+
/**
69+
* Use when creating a custom model from a download service response. Download url and download
70+
* url expiry should go together. These will not be stored in user preferences as this is a
71+
* temporary step towards setting the actual download id.
72+
*
73+
* @param name - model name
74+
* @param modelHash - model hash
75+
* @param fileSize - model file size
76+
* @param downloadUrl - download url path
77+
* @param downloadUrlExpiry - time download url path expires
78+
* @hide
79+
*/
80+
public CustomModel(
81+
@NonNull String name,
82+
@NonNull String modelHash,
6183
long fileSize,
84+
String downloadUrl,
85+
long downloadUrlExpiry) {
86+
this(name, modelHash, fileSize, 0, "", downloadUrl, downloadUrlExpiry);
87+
}
88+
89+
/**
90+
* Use when creating a custom model while the initial download is still in progress.
91+
*
92+
* @param name - model name
93+
* @param modelHash - model hash
94+
* @param fileSize - model file size
95+
* @param downloadId - Android Download Manger - download id
96+
* @param localFilePath - location of the current file
97+
* @param downloadUrl - download url path returned from download service
98+
* @param downloadUrlExpiry - expiry time of download url link
99+
* @hide
100+
*/
101+
private CustomModel(
102+
@NonNull String name,
62103
@NonNull String modelHash,
63-
@NonNull String localFilePath) {
104+
long fileSize,
105+
long downloadId,
106+
@Nullable String localFilePath,
107+
@Nullable String downloadUrl,
108+
long downloadUrlExpiry) {
64109
this.modelHash = modelHash;
65110
this.name = name;
66111
this.fileSize = fileSize;
67112
this.downloadId = downloadId;
68113
this.localFilePath = localFilePath;
114+
this.downloadUrl = downloadUrl;
115+
this.downloadUrlExpiry = downloadUrlExpiry;
69116
}
70117

71118
@NonNull
@@ -81,7 +128,7 @@ public String getName() {
81128
*/
82129
@Nullable
83130
public File getFile() {
84-
if (localFilePath.isEmpty()) {
131+
if (localFilePath == null || localFilePath.isEmpty()) {
85132
return null;
86133
}
87134
throw new UnsupportedOperationException("Not implemented, file retrieval coming soon.");
@@ -130,19 +177,43 @@ public boolean equals(Object o) {
130177
&& Objects.equal(modelHash, other.modelHash)
131178
&& Objects.equal(fileSize, other.fileSize)
132179
&& Objects.equal(localFilePath, other.localFilePath)
133-
&& Objects.equal(downloadId, other.downloadId);
180+
&& Objects.equal(downloadId, other.downloadId)
181+
&& Objects.equal(downloadUrl, other.downloadUrl)
182+
&& Objects.equal(downloadUrlExpiry, other.downloadUrlExpiry);
134183
}
135184

136185
@Override
137186
public int hashCode() {
138-
return Objects.hashCode(name, modelHash, fileSize, localFilePath, downloadId);
187+
return Objects.hashCode(
188+
name, modelHash, fileSize, localFilePath, downloadId, downloadUrl, downloadUrlExpiry);
189+
}
190+
191+
/**
192+
* The expiry time for the current download url.
193+
*
194+
* <p>Internal use only.
195+
*
196+
* @hide
197+
*/
198+
public long getDownloadUrlExpiry() {
199+
return downloadUrlExpiry;
200+
}
201+
202+
/**
203+
* @return the model download url
204+
* <p>Internal use only
205+
* @hide
206+
*/
207+
@Nullable
208+
public String getDownloadUrl() {
209+
return downloadUrl;
139210
}
140211

141212
/**
142213
* @return the model file path
143214
* @hide
144215
*/
145-
@NonNull
216+
@Nullable
146217
public String getLocalFilePath() {
147218
return localFilePath;
148219
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414

1515
package com.google.firebase.ml.modeldownloader;
1616

17+
import android.os.Build.VERSION_CODES;
1718
import androidx.annotation.NonNull;
19+
import androidx.annotation.RequiresApi;
1820
import com.google.firebase.FirebaseApp;
21+
import com.google.firebase.FirebaseOptions;
1922
import com.google.firebase.components.Component;
2023
import com.google.firebase.components.ComponentRegistrar;
2124
import com.google.firebase.components.Dependency;
25+
import com.google.firebase.installations.FirebaseInstallationsApi;
26+
import com.google.firebase.ml.modeldownloader.internal.CustomModelDownloadService;
27+
import com.google.firebase.ml.modeldownloader.internal.SharedPreferencesUtil;
2228
import com.google.firebase.platforminfo.LibraryVersionComponent;
2329
import java.util.Arrays;
2430
import java.util.List;
@@ -33,12 +39,25 @@ public class FirebaseModelDownloaderRegistrar implements ComponentRegistrar {
3339

3440
@Override
3541
@NonNull
42+
@RequiresApi(api = VERSION_CODES.KITKAT)
3643
public List<Component<?>> getComponents() {
3744
return Arrays.asList(
3845
Component.builder(FirebaseModelDownloader.class)
3946
.add(Dependency.required(FirebaseApp.class))
4047
.factory(c -> new FirebaseModelDownloader(c.get(FirebaseApp.class)))
4148
.build(),
49+
Component.builder(SharedPreferencesUtil.class)
50+
.add(Dependency.required(FirebaseApp.class))
51+
.factory(c -> new SharedPreferencesUtil(c.get(FirebaseApp.class)))
52+
.build(),
53+
Component.builder(CustomModelDownloadService.class)
54+
.add(Dependency.required(FirebaseOptions.class))
55+
.add(Dependency.required(FirebaseInstallationsApi.class))
56+
.factory(
57+
c ->
58+
new CustomModelDownloadService(
59+
c.get(FirebaseOptions.class), c.get(FirebaseInstallationsApi.class)))
60+
.build(),
4261
LibraryVersionComponent.create("firebase-ml-modeldownloader", BuildConfig.VERSION_NAME));
4362
}
4463
}

0 commit comments

Comments
 (0)