1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.textclassifier.downloader; 18 19 import com.android.textclassifier.common.ModelType.ModelTypeDef; 20 import com.android.textclassifier.downloader.DownloadedModelDatabase.Manifest; 21 import com.android.textclassifier.downloader.DownloadedModelDatabase.ManifestEnrollment; 22 import com.android.textclassifier.downloader.DownloadedModelDatabase.Model; 23 import com.android.textclassifier.utils.IndentingPrintWriter; 24 import com.google.common.collect.ImmutableMap; 25 import java.io.File; 26 import java.util.List; 27 import javax.annotation.Nullable; 28 29 // TODO(licha): Let Worker access DB class directly, then we can make this a lister interface 30 /** An interface to provide easy access to DownloadedModelDatabase. */ 31 public interface DownloadedModelManager { 32 33 /** Returns the directory containing models downloaded by the downloader. */ getModelDownloaderDir()34 File getModelDownloaderDir(); 35 36 /** 37 * Returns all downloaded model files for the given modelType 38 * 39 * <p>This method should return quickly as it may be on the critical path of serving requests. 40 * 41 * @param modelType the type of the model 42 * @return the model files. Empty if no suitable model found 43 */ 44 @Nullable listModels(@odelTypeDef String modelType)45 List<File> listModels(@ModelTypeDef String modelType); 46 47 /** 48 * Returns the model entry if the model represented by the url is in our database. 49 * 50 * @param modelUrl the model url 51 * @return model entry from internal database, null if not exist 52 */ 53 @Nullable getModel(String modelUrl)54 Model getModel(String modelUrl); 55 56 /** 57 * Returns the manifest entry if the manifest represented by the url is in our database. 58 * 59 * @param manifestUrl the manifest url 60 * @return manifest entry from internal database, null if not exist 61 */ 62 @Nullable getManifest(String manifestUrl)63 Manifest getManifest(String manifestUrl); 64 65 /** 66 * Returns the manifest enrollment entry if a manifest is registered for the given type and 67 * locale. 68 * 69 * @param modelType the model type of the enrollment 70 * @param localeTag the locale tag of the enrollment 71 * @return manifest enrollment entry from internal database, null if not exist 72 */ 73 @Nullable getManifestEnrollment(@odelTypeDef String modelType, String localeTag)74 ManifestEnrollment getManifestEnrollment(@ModelTypeDef String modelType, String localeTag); 75 76 /** 77 * Add a newly downloaded model to the internal database. 78 * 79 * <p>The model must be linked to a manifest via #registerManifest(). Otherwise it will be cleaned 80 * up automatically later. 81 * 82 * @param modelUrl the url where we downloaded model from 83 * @param modelPath the path where we store the downloaded model 84 */ registerModel(String modelUrl, String modelPath)85 void registerModel(String modelUrl, String modelPath); 86 87 /** 88 * Add a newly downloaded manifest to the internal database. 89 * 90 * <p>The manifest must be linked to a specific use case via #registerManifestEnrollment(). 91 * Otherwise it will be cleaned up automatically later. Currently there is only one model in one 92 * manifest. 93 * 94 * @param manifestUrl the url where we downloaded manifest 95 * @param modelUrl the url where we downloaded the only model inside the manifest 96 */ registerManifest(String manifestUrl, String modelUrl)97 void registerManifest(String manifestUrl, String modelUrl); 98 99 /** 100 * Add a failure records for the given manifest url. 101 * 102 * <p>If the manifest failed before, then increase the prevFailureCounts by one. We skip manifest 103 * if it failed too many times before. 104 * 105 * @param manifestUrl the failed manifest url 106 */ registerManifestDownloadFailure(String manifestUrl)107 void registerManifestDownloadFailure(String manifestUrl); 108 109 /** 110 * Link a manifest to a specific (modelType, localeTag) use case. 111 * 112 * <p>After this registration, we will start to use this model file for all requests for the given 113 * locale and the specified model type. 114 * 115 * @param modelType the model type 116 * @param localeTag the tag of the locale on user's device that this manifest should be used for 117 * @param manifestUrl the url of the manifest 118 */ registerManifestEnrollment( @odelTypeDef String modelType, String localeTag, String manifestUrl)119 void registerManifestEnrollment( 120 @ModelTypeDef String modelType, String localeTag, String manifestUrl); 121 122 /** 123 * Clean up unused downloaded models and update other internal states. 124 * 125 * @param manifestsToDownload Map<modelType, manifestsToDownloadMyType> that the worker tried to 126 * download 127 */ onDownloadCompleted(ImmutableMap<String, ManifestsToDownloadByType> manifestsToDownload)128 void onDownloadCompleted(ImmutableMap<String, ManifestsToDownloadByType> manifestsToDownload); 129 130 /** 131 * Dumps the internal state for debugging. 132 * 133 * @param printWriter writer to write dumped states 134 */ dump(IndentingPrintWriter printWriter)135 void dump(IndentingPrintWriter printWriter); 136 } 137