Skip to content

Commit 33eed38

Browse files
committed
Updating based on reviewer comments.
1 parent c201a00 commit 33eed38

File tree

4 files changed

+57
-40
lines changed

4 files changed

+57
-40
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ public class CustomModel {
4242
*/
4343
public CustomModel(
4444
@NonNull String name, long downloadId, long fileSize, @NonNull String modelHash) {
45-
this.modelHash = modelHash;
46-
this.name = name;
47-
this.fileSize = fileSize;
48-
this.downloadId = downloadId;
49-
this.localFilePath = "";
45+
this(name, downloadId, fileSize, modelHash, "");
5046
}
5147

5248
/**
@@ -137,6 +133,11 @@ public boolean equals(Object o) {
137133
&& Objects.equal(downloadId, other.downloadId);
138134
}
139135

136+
@Override
137+
public int hashCode() {
138+
return Objects.hashCode(name, modelHash, fileSize, localFilePath, downloadId);
139+
}
140+
140141
/**
141142
* @return the model file path
142143
* @hide

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

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import android.content.Context;
1818
import android.content.SharedPreferences;
19+
import android.content.SharedPreferences.Editor;
1920
import android.os.SystemClock;
2021
import androidx.annotation.NonNull;
2122
import androidx.annotation.Nullable;
@@ -51,8 +52,8 @@ public SharedPreferencesUtil(FirebaseApp firebaseApp) {
5152
* Returns the Custom Model details currently associated with this model. If a fully downloaded
5253
* model is present - this returns the details of that model, including local file path. If an
5354
* update of an existing model is in progress, the local model plus the download id for the new
54-
* upload is returned. To get only details related to the downloading model use
55-
* getDownloadingCustomModelDetails. If this is the initial download of a local file - the
55+
* upload is returned. To get only details related to the downloading model use {@link
56+
* #getDownloadingCustomModelDetails}. If this is the initial download of a local file - the
5657
* downloading model details are returned.
5758
*
5859
* @param modelName - name of the model
@@ -138,7 +139,7 @@ public synchronized void setDownloadingCustomModelDetails(@NonNull CustomModel c
138139
.putLong(
139140
String.format(DOWNLOAD_BEGIN_TIME_MS_PATTERN, persistenceKey, modelName),
140141
SystemClock.elapsedRealtime())
141-
.apply();
142+
.commit();
142143
}
143144

144145
/**
@@ -154,35 +155,19 @@ public synchronized void setUploadedCustomModelDetails(@NonNull CustomModel cust
154155
if (!id.equals(0L)) {
155156
throw new IllegalArgumentException("Only call when Custom model has completed download.");
156157
}
158+
Editor editor = getSharedPreferences().edit();
159+
clearDownloadingModelDetails(editor, customModel.getName());
157160

158161
String modelName = customModel.getName();
159162
String hash = customModel.getModelHash();
160163
long size = customModel.getSize();
161164
String filePath = customModel.getLocalFilePath();
162-
getSharedPreferences()
163-
.edit()
165+
editor
164166
.putString(String.format(LOCAL_MODEL_HASH_PATTERN, persistenceKey, modelName), hash)
165167
.putLong(String.format(LOCAL_MODEL_FILE_SIZE_PATTERN, persistenceKey, modelName), size)
166168
.putString(
167169
String.format(LOCAL_MODEL_FILE_PATH_PATTERN, persistenceKey, modelName), filePath)
168-
.apply();
169-
170-
clearDownloadingModelDetails(customModel.getName());
171-
}
172-
173-
/**
174-
* Clears all stored data related to a custom model download.
175-
*
176-
* @param modelName - name of model
177-
*/
178-
public synchronized void clearDownloadingModelDetails(@NonNull String modelName) {
179-
getSharedPreferences()
180-
.edit()
181-
.remove(String.format(DOWNLOADING_MODEL_ID_PATTERN, persistenceKey, modelName))
182-
.remove(String.format(DOWNLOADING_MODEL_HASH_PATTERN, persistenceKey, modelName))
183-
.remove(String.format(DOWNLOADING_MODEL_SIZE_PATTERN, persistenceKey, modelName))
184-
.remove(String.format(DOWNLOAD_BEGIN_TIME_MS_PATTERN, persistenceKey, modelName))
185-
.apply();
170+
.commit();
186171
}
187172

188173
/**
@@ -194,18 +179,34 @@ public synchronized void clearModelDetails(@NonNull String modelName, boolean cl
194179
if (cleanUpModelFile) {
195180
// TODO(annz) - add code to remove model files from device
196181
}
182+
Editor editor = getSharedPreferences().edit();
197183

198-
clearDownloadingModelDetails(modelName);
184+
clearDownloadingModelDetails(editor, modelName);
199185

200-
getSharedPreferences()
201-
.edit()
186+
editor
202187
.remove(String.format(LOCAL_MODEL_FILE_PATH_PATTERN, persistenceKey, modelName))
203188
.remove(String.format(LOCAL_MODEL_FILE_SIZE_PATTERN, persistenceKey, modelName))
204189
.remove(String.format(LOCAL_MODEL_HASH_PATTERN, persistenceKey, modelName))
190+
.commit();
191+
}
192+
193+
/**
194+
* Clears all stored data related to a custom model download.
195+
*
196+
* @param modelName - name of model
197+
*/
198+
@VisibleForTesting
199+
synchronized void clearDownloadingModelDetails(Editor editor, @NonNull String modelName) {
200+
editor
201+
.remove(String.format(DOWNLOADING_MODEL_ID_PATTERN, persistenceKey, modelName))
202+
.remove(String.format(DOWNLOADING_MODEL_HASH_PATTERN, persistenceKey, modelName))
203+
.remove(String.format(DOWNLOADING_MODEL_SIZE_PATTERN, persistenceKey, modelName))
204+
.remove(String.format(DOWNLOAD_BEGIN_TIME_MS_PATTERN, persistenceKey, modelName))
205205
.apply();
206206
}
207207

208-
private SharedPreferences getSharedPreferences() {
208+
@VisibleForTesting
209+
SharedPreferences getSharedPreferences() {
209210
return firebaseApp
210211
.getApplicationContext()
211212
.getSharedPreferences(PREFERENCES_PACKAGE_NAME, Context.MODE_PRIVATE);

firebase-ml-modeldownloader/src/test/java/com/google/firebase/ml/modeldownloader/CustomModelTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
package com.google.firebase.ml.modeldownloader;
1616

1717
import static org.junit.Assert.assertEquals;
18+
import static org.junit.Assert.assertFalse;
19+
import static org.junit.Assert.assertNotEquals;
1820
import static org.junit.Assert.assertNull;
21+
import static org.junit.Assert.assertTrue;
1922

2023
import org.junit.Test;
2124
import org.junit.runner.RunWith;
@@ -52,4 +55,21 @@ public void customModel_getDownloadId() {
5255
public void customModel_getFile_downloadIncomplete() {
5356
assertNull(CUSTOM_MODEL.getFile());
5457
}
58+
59+
@Test
60+
public void customModel_equals() {
61+
assertTrue(CUSTOM_MODEL.equals(new CustomModel(MODEL_NAME, 0, 100, MODEL_HASH)));
62+
assertFalse(CUSTOM_MODEL.equals(new CustomModel(MODEL_NAME, 0, 101, MODEL_HASH)));
63+
assertFalse(CUSTOM_MODEL.equals(new CustomModel(MODEL_NAME, 101, 100, MODEL_HASH)));
64+
}
65+
66+
@Test
67+
public void customModel_hashCode() {
68+
assertEquals(
69+
CUSTOM_MODEL.hashCode(), new CustomModel(MODEL_NAME, 0, 100, MODEL_HASH).hashCode());
70+
assertNotEquals(
71+
CUSTOM_MODEL.hashCode(), new CustomModel(MODEL_NAME, 0, 101, MODEL_HASH).hashCode());
72+
assertNotEquals(
73+
CUSTOM_MODEL.hashCode(), new CustomModel(MODEL_NAME, 101, 100, MODEL_HASH).hashCode());
74+
}
5575
}

firebase-ml-modeldownloader/src/test/java/com/google/firebase/ml/modeldownloader/internal/SharedPreferencesUtilTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,6 @@ public class SharedPreferencesUtilTest {
4444
private static final CustomModel CUSTOM_MODEL_DOWNLOADING =
4545
new CustomModel(MODEL_NAME, 986, 100, MODEL_HASH);
4646

47-
// private static final FirebaseApp FIREBASE_APP = FirebaseApp.initializeApp(
48-
// ApplicationProvider.getApplicationContext(),
49-
// new FirebaseOptions.Builder()
50-
// .setApplicationId("1:123456789:android:abcdef")
51-
// .setProjectId(TEST_PROJECT_ID)
52-
// .build());
53-
5447
private SharedPreferencesUtil sharedPreferencesUtil;
5548

5649
@Before
@@ -119,7 +112,9 @@ public void clearDownloadingModelDetails_keepsLocalModel() throws IllegalArgumen
119112
sharedPreferencesUtil.setDownloadingCustomModelDetails(CUSTOM_MODEL_DOWNLOADING);
120113
CustomModel retrievedModel = sharedPreferencesUtil.getCustomModelDetails(MODEL_NAME);
121114
assertEquals(retrievedModel, CUSTOM_MODEL_UPDATE_IN_BACKGROUND);
122-
sharedPreferencesUtil.clearDownloadingModelDetails(CUSTOM_MODEL_DOWNLOAD_COMPLETE.getName());
115+
sharedPreferencesUtil.clearDownloadingModelDetails(
116+
sharedPreferencesUtil.getSharedPreferences().edit(),
117+
CUSTOM_MODEL_DOWNLOAD_COMPLETE.getName());
123118
retrievedModel = sharedPreferencesUtil.getCustomModelDetails(MODEL_NAME);
124119
assertEquals(retrievedModel, CUSTOM_MODEL_DOWNLOAD_COMPLETE);
125120
}

0 commit comments

Comments
 (0)