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.internal.inputmethod; 18 19 import android.annotation.Nullable; 20 import android.text.TextUtils; 21 22 import java.util.Locale; 23 24 /** 25 * A utility class to handle {@link Locale} related logic for 26 * {@link android.view.inputmethod.InputMethodSubtype} and 27 * {@link android.view.textservice.SpellCheckerSubtype}. 28 */ 29 public class SubtypeLocaleUtils { 30 /** 31 * Maintains deprecated logic about how subtype locales specified in XML resources have been 32 * parsed. 33 * 34 * <p>This logic is kept basically for compatibility purpose. Consider relying on languageTag 35 * attribute instead.</p> 36 * 37 * @param localeStr string representation that is specified in the locale attribute 38 * @return {@link Locale} object parsed from {@code localeStr}. {@code null} for unexpected 39 * format 40 * 41 * @attr ref android.R.styleable#InputMethod_Subtype_imeSubtypeLocale 42 * @attr ref android.R.styleable#InputMethod_Subtype_languageTag 43 * @attr ref android.R.styleable#SpellChecker_Subtype_languageTag 44 * @attr ref android.R.styleable#SpellChecker_Subtype_subtypeLocale 45 */ 46 @Nullable constructLocaleFromString(String localeStr)47 public static Locale constructLocaleFromString(String localeStr) { 48 if (TextUtils.isEmpty(localeStr)) { 49 return null; 50 } 51 // TODO: Use {@link Locale#toLanguageTag()} and {@link Locale#forLanguageTag(languageTag)}. 52 String[] localeParams = localeStr.split("_", 3); 53 if (localeParams.length >= 1 && "tl".equals(localeParams[0])) { 54 // Convert a locale whose language is "tl" to one whose language is "fil". 55 // For example, "tl_PH" will get converted to "fil_PH". 56 // Versions of Android earlier than Lollipop did not support three letter language 57 // codes, and used "tl" (Tagalog) as the language string for "fil" (Filipino). 58 // On Lollipop and above, the current three letter version must be used. 59 localeParams[0] = "fil"; 60 } 61 // The length of localeStr is guaranteed to always return a 1 <= value <= 3 62 // because localeStr is not empty. 63 if (localeParams.length == 1) { 64 return new Locale(localeParams[0]); 65 } else if (localeParams.length == 2) { 66 return new Locale(localeParams[0], localeParams[1]); 67 } else if (localeParams.length == 3) { 68 return new Locale(localeParams[0], localeParams[1], localeParams[2]); 69 } 70 return null; 71 } 72 } 73