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.car.settings.language; 18 19 import androidx.annotation.Nullable; 20 import androidx.preference.Preference; 21 22 import com.android.internal.app.LocaleStore; 23 24 import java.util.Locale; 25 26 /** Utilities to get/set LocaleInfo from preferences. */ 27 public class LocaleUtil { 28 /** This key is also used to add Locale to the bundle for {@link ChildLocalePickerFragment}. */ 29 public static final String LOCALE_BUNDLE_KEY = "locale_key"; 30 31 /** Private constructor to prevent others from instantiating this class. */ LocaleUtil()32 private LocaleUtil() { 33 } 34 35 /** Extract the locale from the given locale info and add to preference arguments. */ setLocaleArgument(Preference preference, LocaleStore.LocaleInfo localeInfo)36 public static void setLocaleArgument(Preference preference, LocaleStore.LocaleInfo localeInfo) { 37 preference.getExtras().putSerializable(LOCALE_BUNDLE_KEY, localeInfo.getLocale()); 38 } 39 40 /** Extract the localeInfo from the preference, if it exists. */ 41 @Nullable getLocaleArgument(Preference preference)42 public static LocaleStore.LocaleInfo getLocaleArgument(Preference preference) { 43 Locale locale = (Locale) preference.getExtras().getSerializable(LOCALE_BUNDLE_KEY); 44 if (locale == null) { 45 return null; 46 } 47 return LocaleStore.getLocaleInfo(locale); 48 } 49 } 50