-
Notifications
You must be signed in to change notification settings - Fork 614
Add implementation for listDownloadedModels. #2154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
deb18fc
bc436f0
c83d392
f21799f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,10 @@ | |
import androidx.annotation.VisibleForTesting; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.ml.modeldownloader.CustomModel; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** @hide */ | ||
public class SharedPreferencesUtil { | ||
|
@@ -33,6 +37,8 @@ public class SharedPreferencesUtil { | |
// local model details | ||
private static final String LOCAL_MODEL_HASH_PATTERN = "current_model_hash_%s_%s"; | ||
private static final String LOCAL_MODEL_FILE_PATH_PATTERN = "current_model_path_%s_%s"; | ||
private static final String LOCAL_MODEL_FILE_PATH_MATCHER = "current_model_path_(.*?)_([^/]+)/?"; | ||
|
||
private static final String LOCAL_MODEL_FILE_SIZE_PATTERN = "current_model_size_%s_%s"; | ||
// details about model during download. | ||
private static final String DOWNLOADING_MODEL_HASH_PATTERN = "downloading_model_hash_%s_%s"; | ||
|
@@ -190,6 +196,24 @@ public synchronized void clearModelDetails(@NonNull String modelName, boolean cl | |
.commit(); | ||
} | ||
|
||
public synchronized Set<CustomModel> listDownloadedModels() { | ||
Set<CustomModel> customModels = new HashSet<>(); | ||
Set<String> keySet = getSharedPreferences().getAll().keySet(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you're reading all the keys at once and not dealing with the sharedpreferences anymore, it may not be necessary to sync the whole method. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The second part (added todo) will need to coordinate with android download manager, so I'll need the sync when I add that. |
||
|
||
for (String key : keySet) { | ||
// if a local file path is present - get model details. | ||
Matcher matcher = Pattern.compile(LOCAL_MODEL_FILE_PATH_MATCHER).matcher(key); | ||
if (matcher.find()) { | ||
String modelName = matcher.group(matcher.groupCount()); | ||
CustomModel extractModel = getCustomModelDetails(modelName); | ||
if (extractModel != null) { | ||
customModels.add(extractModel); | ||
} | ||
} | ||
} | ||
return customModels; | ||
} | ||
|
||
/** | ||
* Clears all stored data related to a custom model download. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since sharedPreferences are sync, you could try to create an actual task here to get concurrency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done - thanks.