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 android.text.TextUtils; 20 import android.util.Pair; 21 import com.android.textclassifier.common.ModelType.ModelTypeDef; 22 import com.android.textclassifier.common.TextClassifierSettings; 23 import com.google.common.annotations.VisibleForTesting; 24 import com.google.common.collect.ImmutableMap; 25 import java.util.Collection; 26 import java.util.Locale; 27 import javax.annotation.Nullable; 28 29 /** Utilities for locale matching. */ 30 final class LocaleUtils { 31 @VisibleForTesting static final String UNIVERSAL_LOCALE_TAG = "universal"; 32 33 /** 34 * Find the best locale tag as well as the configured manfiest url from device config. 35 * 36 * @param modelType the model type 37 * @param targetLocale target locale 38 * @param settings TextClassifierSettings to check device config 39 * @return a pair of <bestLocaleTag, manfiestUrl>. Null if not found. 40 */ 41 @Nullable lookupBestLocaleTagAndManifestUrl( @odelTypeDef String modelType, Locale targetLocale, TextClassifierSettings settings)42 static Pair<String, String> lookupBestLocaleTagAndManifestUrl( 43 @ModelTypeDef String modelType, Locale targetLocale, TextClassifierSettings settings) { 44 ImmutableMap<String, String> localeTagUrlMap = 45 settings.getLanguageTagAndManifestUrlMap(modelType); 46 Collection<String> allLocaleTags = localeTagUrlMap.keySet(); 47 String bestLocaleTag = lookupBestLocaleTag(targetLocale, allLocaleTags); 48 if (bestLocaleTag == null) { 49 return null; 50 } 51 String manifestUrl = localeTagUrlMap.get(bestLocaleTag); 52 if (TextUtils.isEmpty(manifestUrl)) { 53 return null; 54 } 55 return Pair.create(bestLocaleTag, manifestUrl); 56 } 57 /** Find the best locale tag for the target locale. Return null if no one is suitable. */ 58 @Nullable lookupBestLocaleTag(Locale targetLocale, Collection<String> availableTags)59 static String lookupBestLocaleTag(Locale targetLocale, Collection<String> availableTags) { 60 // Notice: this lookup API just trys to match the longest prefix for the target locale tag. 61 // Its implementation looks inefficient and the behavior may not be 100% desired. E.g. if the 62 // target locale is en, and we only have en-uk in available tags, the current API returns null. 63 String bestTag = 64 Locale.lookupTag(Locale.LanguageRange.parse(targetLocale.toLanguageTag()), availableTags); 65 if (bestTag != null) { 66 return bestTag; 67 } 68 if (availableTags.contains(UNIVERSAL_LOCALE_TAG)) { 69 return UNIVERSAL_LOCALE_TAG; 70 } 71 return null; 72 } 73 LocaleUtils()74 private LocaleUtils() {} 75 } 76