1 /*
2  * Copyright (C) 2023 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.settings.regionalpreferences;
18 
19 import android.content.Context;
20 import android.icu.util.ULocale;
21 import android.os.LocaleList;
22 import android.provider.Settings;
23 import android.text.TextUtils;
24 
25 import androidx.core.text.util.LocalePreferences;
26 
27 import com.android.internal.app.LocalePicker;
28 import com.android.settings.R;
29 
30 import java.util.Locale;
31 
32 /** Provides utils for regional preferences. */
33 public class RegionalPreferencesDataUtils {
34     static final String DEFAULT_VALUE = "default";
35 
getDefaultUnicodeExtensionData(Context contxt, String type)36     static String getDefaultUnicodeExtensionData(Context contxt, String type) {
37         // 1. Check cache data in Settings provider.
38         String record = Settings.System.getString(
39                 contxt.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
40         String result = "";
41 
42         if (!TextUtils.isEmpty(record)) {
43             result = Locale.forLanguageTag(record).getUnicodeLocaleType(type);
44         }
45         // 2. Check cache data in default Locale(ICU lib).
46         if (TextUtils.isEmpty(result)) {
47             result = Locale.getDefault(Locale.Category.FORMAT).getUnicodeLocaleType(type);
48         }
49 
50         return result == null ? DEFAULT_VALUE : result;
51     }
52 
savePreference(Context context, String type, String value)53     static void savePreference(Context context, String type, String value) {
54         saveToSettingsProvider(context, type, value);
55         saveToSystem(type, value);
56     }
57 
saveToSettingsProvider(Context context, String type, String value)58     private static void saveToSettingsProvider(Context context, String type, String value) {
59         String record = Settings.System.getString(
60                 context.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
61 
62         record = record == null ? "" : record;
63 
64         Settings.System.putString(
65                 context.getContentResolver(),
66                 Settings.System.LOCALE_PREFERENCES,
67                 addUnicodeKeywordToLocale(record, type, value).toLanguageTag());
68     }
69 
saveToSystem(String type, String value)70     private static void saveToSystem(String type, String value) {
71         LocaleList localeList = LocaleList.getDefault();
72         Locale[] resultLocales = new Locale[localeList.size()];
73         for (int i = 0; i < localeList.size(); i++) {
74             resultLocales[i] = addUnicodeKeywordToLocale(localeList.get(i), type, value);
75         }
76         LocalePicker.updateLocales(new LocaleList(resultLocales));
77     }
78 
addUnicodeKeywordToLocale(Locale locale, String type, String value)79     private static Locale addUnicodeKeywordToLocale(Locale locale, String type, String value) {
80         return new Locale.Builder()
81                 .setLocale(locale)
82                 .setUnicodeLocaleKeyword(type, value)
83                 .build();
84     }
85 
addUnicodeKeywordToLocale(String languageTag, String type, String value)86     private static Locale addUnicodeKeywordToLocale(String languageTag, String type, String value) {
87         return addUnicodeKeywordToLocale(Locale.forLanguageTag(languageTag), type, value);
88     }
89 
temperatureUnitsConverter(Context context, String unit)90     static String temperatureUnitsConverter(Context context, String unit) {
91         switch (unit) {
92             case LocalePreferences.TemperatureUnit.CELSIUS:
93                 return context.getString(R.string.celsius_temperature_unit);
94             case LocalePreferences.TemperatureUnit.FAHRENHEIT:
95                 return context.getString(R.string.fahrenheit_temperature_unit);
96             default:
97                 return context.getString(R.string.default_string_of_regional_preference);
98         }
99     }
100 
dayConverter(Context context, String day)101     static String dayConverter(Context context, String day) {
102         switch (day) {
103             case LocalePreferences.FirstDayOfWeek.MONDAY:
104                 return context.getString(R.string.monday_first_day_of_week);
105             case LocalePreferences.FirstDayOfWeek.TUESDAY:
106                 return context.getString(R.string.tuesday_first_day_of_week);
107             case LocalePreferences.FirstDayOfWeek.WEDNESDAY:
108                 return context.getString(R.string.wednesday_first_day_of_week);
109             case LocalePreferences.FirstDayOfWeek.THURSDAY:
110                 return context.getString(R.string.thursday_first_day_of_week);
111             case LocalePreferences.FirstDayOfWeek.FRIDAY:
112                 return context.getString(R.string.friday_first_day_of_week);
113             case LocalePreferences.FirstDayOfWeek.SATURDAY:
114                 return context.getString(R.string.saturday_first_day_of_week);
115             case LocalePreferences.FirstDayOfWeek.SUNDAY:
116                 return context.getString(R.string.sunday_first_day_of_week);
117             default:
118                 return context.getString(R.string.default_string_of_regional_preference);
119         }
120     }
121 }
122