diff --git a/res/layout/preference_widget_tick.xml b/res/layout/preference_widget_tick.xml new file mode 100644 index 00000000000..d67a93a9712 --- /dev/null +++ b/res/layout/preference_widget_tick.xml @@ -0,0 +1,27 @@ + + + + + \ No newline at end of file diff --git a/res/values/arrays.xml b/res/values/arrays.xml index 4d9a001148d..e41b8c1d78f 100644 --- a/res/values/arrays.xml +++ b/res/values/arrays.xml @@ -1360,4 +1360,39 @@ [CHAR LIMIT=NONE] --> + + + + default + celsius + fahrenheit + kelvin + + + + + default + sun + mon + tue + wed + thu + fri + sat + + + + + default + chinese + dangi + hebrew + indian + islamic + islamic-rgsa + islamic-tbla + islamic-umalqura + persian + + diff --git a/res/values/strings.xml b/res/values/strings.xml index caad32e93c8..31e5de48820 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -371,6 +371,26 @@ Numbers [No preference] + + Celsius + + Fahrenheit + + Kevin + + Sunday + + Monday + + Tuesday + + Wednesday + + Thursday + + Friday + + Saturday {count, plural, diff --git a/res/xml/regional_preference_content_page.xml b/res/xml/regional_preference_content_page.xml new file mode 100644 index 00000000000..ed60eba03ce --- /dev/null +++ b/res/xml/regional_preference_content_page.xml @@ -0,0 +1,18 @@ + + + + + \ No newline at end of file diff --git a/src/com/android/settings/regionalpreferences/CalendarTypeController.java b/src/com/android/settings/regionalpreferences/CalendarTypeController.java index ea5919198df..1a81e7f2cc0 100644 --- a/src/com/android/settings/regionalpreferences/CalendarTypeController.java +++ b/src/com/android/settings/regionalpreferences/CalendarTypeController.java @@ -16,11 +16,17 @@ package com.android.settings.regionalpreferences; +import android.app.settings.SettingsEnums; import android.content.Context; +import android.os.Bundle; import android.provider.Settings; +import android.text.TextUtils; +import android.util.Log; + +import androidx.preference.Preference; -import com.android.settings.R; import com.android.settings.core.BasePreferenceController; +import com.android.settings.core.SubSettingLauncher; import java.util.Locale; @@ -28,6 +34,7 @@ import java.util.Locale; * A controller for the entry of Calendar types' page */ public class CalendarTypeController extends BasePreferenceController { + private static final String TAG = CalendarTypeController.class.getSimpleName(); public CalendarTypeController(Context context, String preferenceKey) { super(context, preferenceKey); } @@ -60,7 +67,26 @@ public class CalendarTypeController extends BasePreferenceController { if (result.isEmpty()) { result = LocalePreferences.getCalendarType(false); } - return result.isEmpty() - ? mContext.getString(R.string.default_string_of_regional_preference) : result; + + String inputStr = result.isEmpty() ? RegionalPreferencesFragment.TYPE_DEFAULT : result; + return RegionalPreferencesDataUtils.calendarConverter(mContext, inputStr); + } + + @Override + public boolean handlePreferenceTreeClick(Preference preference) { + if (!TextUtils.equals(preference.getKey(), mPreferenceKey)) { + Log.e(TAG, "not the key " + preference.getKey() + " / " + mPreferenceKey); + return false; + } + + final Bundle extra = new Bundle(); + extra.putString(RegionalPreferencesFragment.TYPE_OF_REGIONAL_PREFERENCE, + RegionalPreferencesFragment.TYPE_CALENDAR); + new SubSettingLauncher(preference.getContext()) + .setDestination(RegionalPreferencesFragment.class.getName()) + .setSourceMetricsCategory(SettingsEnums.REGIONAL_PREFERENCE) + .setArguments(extra) + .launch(); + return true; } } diff --git a/src/com/android/settings/regionalpreferences/FirstDayOfWeekController.java b/src/com/android/settings/regionalpreferences/FirstDayOfWeekController.java index 3fb8a1ec3d9..22823d7d392 100644 --- a/src/com/android/settings/regionalpreferences/FirstDayOfWeekController.java +++ b/src/com/android/settings/regionalpreferences/FirstDayOfWeekController.java @@ -16,16 +16,25 @@ package com.android.settings.regionalpreferences; +import android.app.settings.SettingsEnums; import android.content.Context; +import android.os.Bundle; import android.provider.Settings; +import android.text.TextUtils; +import android.util.Log; + +import androidx.preference.Preference; import com.android.settings.R; import com.android.settings.core.BasePreferenceController; +import com.android.settings.core.SubSettingLauncher; import java.util.Locale; /** A controller for the entry of First Day of Week's page */ public class FirstDayOfWeekController extends BasePreferenceController { + private static final String TAG = FirstDayOfWeekController.class.getSimpleName(); + public FirstDayOfWeekController(Context context, String preferenceKey) { super(context, preferenceKey); } @@ -59,6 +68,25 @@ public class FirstDayOfWeekController extends BasePreferenceController { result = LocalePreferences.getFirstDayOfWeek(false); } return result.isEmpty() - ? mContext.getString(R.string.default_string_of_regional_preference) : result; + ? mContext.getString(R.string.default_string_of_regional_preference) + : RegionalPreferencesDataUtils.dayConverter(mContext, result); + } + + @Override + public boolean handlePreferenceTreeClick(Preference preference) { + if (!TextUtils.equals(preference.getKey(), mPreferenceKey)) { + Log.e(TAG, "not the key " + preference.getKey() + " / " + mPreferenceKey); + return false; + } + + final Bundle extra = new Bundle(); + extra.putString(RegionalPreferencesFragment.TYPE_OF_REGIONAL_PREFERENCE, + RegionalPreferencesFragment.TYPE_FIRST_DAY_OF_WEEK); + new SubSettingLauncher(preference.getContext()) + .setDestination(RegionalPreferencesFragment.class.getName()) + .setSourceMetricsCategory(SettingsEnums.REGIONAL_PREFERENCE) + .setArguments(extra) + .launch(); + return true; } } diff --git a/src/com/android/settings/regionalpreferences/NumberingSystemController.java b/src/com/android/settings/regionalpreferences/NumberingSystemController.java index e951fc24751..80a2250d1d2 100644 --- a/src/com/android/settings/regionalpreferences/NumberingSystemController.java +++ b/src/com/android/settings/regionalpreferences/NumberingSystemController.java @@ -16,18 +16,26 @@ package com.android.settings.regionalpreferences; +import android.app.settings.SettingsEnums; import android.content.Context; import android.icu.text.NumberingSystem; +import android.os.Bundle; import android.provider.Settings; import android.text.TextUtils; +import android.util.Log; + +import androidx.preference.Preference; import com.android.settings.R; import com.android.settings.core.BasePreferenceController; +import com.android.settings.core.SubSettingLauncher; import java.util.Locale; /** A controller for the entry of Numbering System's page */ public class NumberingSystemController extends BasePreferenceController { + private static final String TAG = NumberingSystemController.class.getSimpleName(); + private static final String UNICODE_EXTENSION_NUMBERING_SYSTEM = "nu"; public NumberingSystemController(Context context, String preferenceKey) { @@ -47,7 +55,8 @@ public class NumberingSystemController extends BasePreferenceController { */ @Override public int getAvailabilityStatus() { - return AVAILABLE; + // Hide this , and waiting for next implementation. + return CONDITIONALLY_UNAVAILABLE; } @Override @@ -73,4 +82,22 @@ public class NumberingSystemController extends BasePreferenceController { .build(); return NumberingSystem.getInstance(locale).getName(); } + + @Override + public boolean handlePreferenceTreeClick(Preference preference) { + if (!TextUtils.equals(preference.getKey(), mPreferenceKey)) { + Log.e(TAG, "not the key " + preference.getKey() + " / " + mPreferenceKey); + return false; + } + + final Bundle extra = new Bundle(); + extra.putString(RegionalPreferencesFragment.TYPE_OF_REGIONAL_PREFERENCE, + RegionalPreferencesFragment.TYPE_NUMBERING_SYSTEM); + new SubSettingLauncher(preference.getContext()) + .setDestination(RegionalPreferencesFragment.class.getName()) + .setSourceMetricsCategory(SettingsEnums.REGIONAL_PREFERENCE) + .setArguments(extra) + .launch(); + return true; + } } diff --git a/src/com/android/settings/regionalpreferences/RegionalPreferencesDataUtils.java b/src/com/android/settings/regionalpreferences/RegionalPreferencesDataUtils.java new file mode 100644 index 00000000000..25dc8e16fa6 --- /dev/null +++ b/src/com/android/settings/regionalpreferences/RegionalPreferencesDataUtils.java @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.regionalpreferences; + +import android.content.Context; +import android.icu.util.ULocale; +import android.os.LocaleList; +import android.provider.Settings; +import android.text.TextUtils; + +import com.android.internal.app.LocalePicker; +import com.android.settings.R; + +import java.util.Locale; + +/** Provides utils for regional preferences. */ +public class RegionalPreferencesDataUtils { + private static final String TAG = RegionalPreferencesDataUtils.class.getSimpleName(); + + private static final String U_EXTENSION_FAHRENHEIT = "fahrenhe"; + private static final String KEYWORD_OF_CALENDAR = "calendar"; + + static String getDefaultUnicodeExtensionData(Context contxt, String type) { + // 1. Check cache data in Settings provider. + String record = Settings.System.getString( + contxt.getContentResolver(), Settings.System.LOCALE_PREFERENCES); + String result = ""; + + if (!TextUtils.isEmpty(record)) { + result = Locale.forLanguageTag(record).getUnicodeLocaleType(type); + } + // 2. Check cache data in default Locale(ICU lib). + if (TextUtils.isEmpty(result)) { + result = Locale.getDefault(Locale.Category.FORMAT).getUnicodeLocaleType(type); + } + if (result == null) { + return RegionalPreferencesFragment.TYPE_DEFAULT; + } + + // In BCP47 expression, the tag of fahrenheit is "fahrenhe" i.e. und-u-mu-fahrenhe, + // so if it may need to convert from the langngiage tag, "fahrenhe", to "fahrenheit". + if (result.equals(U_EXTENSION_FAHRENHEIT)) { + return LocalePreferences.TemperatureUnit.FAHRENHEIT; + } + + return result; + } + + static void savePreference(Context context, String type, String value) { + if (type.equals(RegionalPreferencesFragment.TYPE_TEMPERATURE) + && value != null && value.equals(LocalePreferences.TemperatureUnit.FAHRENHEIT)) { + // In BCP47 expression, the temperature unit is expressed to "fahrenhe" + // i.e. zh-TW-u-mu-fahrenhe. Hence, if we want to save fahrenheit unit to u extension, + // It need to change from "fahrenheit" to "fahrenhe". + value = U_EXTENSION_FAHRENHEIT; + } + saveToSettingsProvider(context, type, value); + saveToSystem(type, value); + } + + private static void saveToSettingsProvider(Context context, String type, String value) { + String record = Settings.System.getString( + context.getContentResolver(), Settings.System.LOCALE_PREFERENCES); + + record = record == null ? "" : record; + + Settings.System.putString( + context.getContentResolver(), + Settings.System.LOCALE_PREFERENCES, + addUnicodeKeywordToLocale(record, type, value).toLanguageTag()); + } + + private static void saveToSystem(String type, String value) { + LocaleList localeList = LocaleList.getDefault(); + Locale[] resultLocales = new Locale[localeList.size()]; + for (int i = 0; i < localeList.size(); i++) { + resultLocales[i] = addUnicodeKeywordToLocale(localeList.get(i), type, value); + } + LocalePicker.updateLocales(new LocaleList(resultLocales)); + } + + private static Locale addUnicodeKeywordToLocale(Locale locale, String type, String value) { + return new Locale.Builder() + .setLocale(locale) + .setUnicodeLocaleKeyword(type, value) + .build(); + } + + private static Locale addUnicodeKeywordToLocale(String languageTag, String type, String value) { + return addUnicodeKeywordToLocale(Locale.forLanguageTag(languageTag), type, value); + } + + static String calendarConverter(Context context, String calendarType) { + if (calendarType.equals(RegionalPreferencesFragment.TYPE_DEFAULT)) { + return context.getString(R.string.default_string_of_regional_preference); + } + + Locale locale = new Locale.Builder() + .setUnicodeLocaleKeyword(RegionalPreferencesFragment.TYPE_CALENDAR, calendarType) + .build(); + return ULocale.getDisplayKeywordValue(locale.toLanguageTag(), KEYWORD_OF_CALENDAR, + ULocale.forLocale(Locale.getDefault(Locale.Category.FORMAT))); + } + + static String temperatureUnitsConverter(Context context, String unit) { + switch (unit) { + case LocalePreferences.TemperatureUnit.CELSIUS: + return context.getString(R.string.celsius_temperature_unit); + case LocalePreferences.TemperatureUnit.FAHRENHEIT: + return context.getString(R.string.fahrenheit_temperature_unit); + case LocalePreferences.TemperatureUnit.KELVIN: + return context.getString(R.string.kevin_temperature_unit); + default: + return context.getString(R.string.default_string_of_regional_preference); + } + } + + static String dayConverter(Context context, String day) { + switch (day) { + case LocalePreferences.FirstDayOfWeek.MONDAY: + return context.getString(R.string.monday_first_day_of_week); + case LocalePreferences.FirstDayOfWeek.TUESDAY: + return context.getString(R.string.tuesday_first_day_of_week); + case LocalePreferences.FirstDayOfWeek.WEDNESDAY: + return context.getString(R.string.wednesday_first_day_of_week); + case LocalePreferences.FirstDayOfWeek.THURSDAY: + return context.getString(R.string.thursday_first_day_of_week); + case LocalePreferences.FirstDayOfWeek.FRIDAY: + return context.getString(R.string.friday_first_day_of_week); + case LocalePreferences.FirstDayOfWeek.SATURDAY: + return context.getString(R.string.saturday_first_day_of_week); + case LocalePreferences.FirstDayOfWeek.SUNDAY: + return context.getString(R.string.sunday_first_day_of_week); + default: + return context.getString(R.string.default_string_of_regional_preference); + } + } +} diff --git a/src/com/android/settings/regionalpreferences/RegionalPreferencesFragment.java b/src/com/android/settings/regionalpreferences/RegionalPreferencesFragment.java new file mode 100644 index 00000000000..aa06a43d018 --- /dev/null +++ b/src/com/android/settings/regionalpreferences/RegionalPreferencesFragment.java @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.regionalpreferences; + +import android.app.settings.SettingsEnums; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.VisibleForTesting; +import androidx.preference.Preference; +import androidx.preference.PreferenceScreen; + +import com.android.settings.R; +import com.android.settings.SettingsPreferenceFragment; + +/** A fragment to include each kind of regional preferences. */ +public class RegionalPreferencesFragment extends SettingsPreferenceFragment { + private static final String TAG = RegionalPreferencesFragment.class.getSimpleName(); + + static final String TYPE_DEFAULT = "default"; + static final String TYPE_TEMPERATURE = "mu"; + static final String TYPE_CALENDAR = "ca"; + static final String TYPE_FIRST_DAY_OF_WEEK = "fw"; + static final String TYPE_NUMBERING_SYSTEM = "nu"; + static final String TYPE_OF_REGIONAL_PREFERENCE = "type_of_regional_preference"; + + private PreferenceScreen mPreferenceScreen; + private String mTitle = ""; + @VisibleForTesting + String mType = ""; + + private String[] initializeUIdata(String type) { + switch(type) { + case TYPE_TEMPERATURE: + mTitle = getPrefContext().getString(R.string.temperature_preferences_title); + return getPrefContext().getResources().getStringArray(R.array.temperature_units); + case TYPE_CALENDAR: + mTitle = getPrefContext().getString(R.string.calendar_preferences_title); + return getPrefContext().getResources().getStringArray(R.array.calendar_type); + case TYPE_FIRST_DAY_OF_WEEK: + mTitle = getPrefContext().getString(R.string.first_day_of_week_preferences_title); + return getPrefContext().getResources().getStringArray(R.array.first_day_of_week); + case TYPE_NUMBERING_SYSTEM: + mTitle = getPrefContext().getString(R.string.numbers_preferences_title); + return new String[0]; + default: + mTitle = getPrefContext().getString(R.string.regional_preferences_title); + return new String[0]; + } + } + + @Override + public boolean onPreferenceTreeClick(Preference preference) { + for (int i = 0; i < mPreferenceScreen.getPreferenceCount(); i++) { + TickButtonPreference pref = (TickButtonPreference) mPreferenceScreen.getPreference(i); + Log.i(TAG, "[onPreferenceClick] key is " + pref.getKey()); + if (pref.getKey().equals(preference.getKey())) { + pref.setTickEnable(true); + RegionalPreferencesDataUtils.savePreference( + getPrefContext(), + mType, + preference.getKey().equals(TYPE_DEFAULT) ? null : preference.getKey()); + continue; + } + pref.setTickEnable(false); + } + return true; + } + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + Bundle bundle = getArguments(); + String type = bundle.getString(TYPE_OF_REGIONAL_PREFERENCE, ""); + if (type.isEmpty()) { + Log.w(TAG, "There is no type name."); + finish(); + } + mType = type; + addPreferencesFromResource(R.xml.regional_preference_content_page); + mPreferenceScreen = getPreferenceScreen(); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View view = super.onCreateView(inflater, container, savedInstanceState); + String[] uiData = initializeUIdata(mType); + + for (String item : uiData) { + TickButtonPreference pref = new TickButtonPreference(getPrefContext()); + if (mType.equals(TYPE_FIRST_DAY_OF_WEEK)) { + pref.setTitle(RegionalPreferencesDataUtils.dayConverter( + getPrefContext(), item)); + } else if (mType.equals(TYPE_TEMPERATURE)) { + pref.setTitle(RegionalPreferencesDataUtils.temperatureUnitsConverter( + getPrefContext(), item)); + } else if (mType.equals(TYPE_CALENDAR)) { + pref.setTitle(RegionalPreferencesDataUtils.calendarConverter( + getPrefContext(), item)); + } else { + Log.d(TAG, "Finish this page due to no suitable type."); + finish(); + } + + String value = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData( + getPrefContext(), mType); + pref.setKey(item); + pref.setTickEnable(!value.isEmpty() && item.equals(value)); + mPreferenceScreen.addPreference(pref); + } + return view; + } + + @Override + public void onStart() { + super.onStart(); + getActivity().setTitle(mTitle); + } + + @Override + public int getMetricsCategory() { + switch(mType) { + case TYPE_CALENDAR: + return SettingsEnums.CALENDAR_PREFERENCE; + case TYPE_FIRST_DAY_OF_WEEK: + return SettingsEnums.FIRST_DAY_OF_WEEK_PREFERENCE; + default: + return SettingsEnums.TEMPERATURE_PREFERENCE; + } + } + +} diff --git a/src/com/android/settings/regionalpreferences/TemperatureUnitController.java b/src/com/android/settings/regionalpreferences/TemperatureUnitController.java index b13b6c4a7a1..950d13799b5 100644 --- a/src/com/android/settings/regionalpreferences/TemperatureUnitController.java +++ b/src/com/android/settings/regionalpreferences/TemperatureUnitController.java @@ -16,16 +16,24 @@ package com.android.settings.regionalpreferences; +import android.app.settings.SettingsEnums; import android.content.Context; +import android.os.Bundle; import android.provider.Settings; +import android.text.TextUtils; +import android.util.Log; + +import androidx.preference.Preference; import com.android.settings.R; import com.android.settings.core.BasePreferenceController; +import com.android.settings.core.SubSettingLauncher; import java.util.Locale; /** A controller for the entry of Temperature units' page */ public class TemperatureUnitController extends BasePreferenceController { + private static final String TAG = TemperatureUnitController.class.getSimpleName(); public TemperatureUnitController(Context context, String preferenceKey) { super(context, preferenceKey); } @@ -58,7 +66,26 @@ public class TemperatureUnitController extends BasePreferenceController { if (result.isEmpty()) { result = LocalePreferences.getTemperatureUnit(false); } + return result.isEmpty() - ? mContext.getString(R.string.default_string_of_regional_preference) : result; + ? mContext.getString(R.string.default_string_of_regional_preference) + : RegionalPreferencesDataUtils.temperatureUnitsConverter(mContext, result); + } + + @Override + public boolean handlePreferenceTreeClick(Preference preference) { + if (!TextUtils.equals(preference.getKey(), mPreferenceKey)) { + Log.e(TAG, "not the key " + preference.getKey() + " / " + mPreferenceKey); + return false; + } + final Bundle extra = new Bundle(); + extra.putString(RegionalPreferencesFragment.TYPE_OF_REGIONAL_PREFERENCE, + RegionalPreferencesFragment.TYPE_TEMPERATURE); + new SubSettingLauncher(preference.getContext()) + .setDestination(RegionalPreferencesFragment.class.getName()) + .setSourceMetricsCategory(SettingsEnums.REGIONAL_PREFERENCE) + .setArguments(extra) + .launch(); + return true; } } diff --git a/src/com/android/settings/regionalpreferences/TickButtonPreference.java b/src/com/android/settings/regionalpreferences/TickButtonPreference.java new file mode 100644 index 00000000000..c01521f548d --- /dev/null +++ b/src/com/android/settings/regionalpreferences/TickButtonPreference.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.regionalpreferences; + +import android.content.Context; +import android.view.View; + +import androidx.preference.PreferenceViewHolder; + +import com.android.settings.R; +import com.android.settingslib.widget.TwoTargetPreference; + +/** A preference with tick button */ +public class TickButtonPreference extends TwoTargetPreference { + private static final String TAG = TickButtonPreference.class.getSimpleName(); + private boolean mIsTickEnabled; + private View mWidgetFrame; + private View mDivider; + + public TickButtonPreference(Context context) { + super(context, null); + } + + /** Set this preference to be selected. */ + public void setTickEnable(boolean isEnable) { + mIsTickEnabled = isEnable; + if (mWidgetFrame != null) { + mWidgetFrame.setVisibility(isEnable ? View.VISIBLE : View.INVISIBLE); + } + } + + /** Check if this preference is selected. */ + public boolean isTickEnabled() { + return mIsTickEnabled; + } + + @Override + public void onBindViewHolder(PreferenceViewHolder holder) { + super.onBindViewHolder(holder); + mDivider = holder.findViewById(R.id.two_target_divider); + mWidgetFrame = holder.findViewById(android.R.id.widget_frame); + if (mDivider != null) { + mDivider.setVisibility(View.GONE); + } + if (mWidgetFrame != null) { + mWidgetFrame.setVisibility(mIsTickEnabled ? View.VISIBLE : View.INVISIBLE); + } + } + + @Override + protected int getSecondTargetResId() { + super.getSecondTargetResId(); + return R.layout.preference_widget_tick; + } +} diff --git a/tests/unit/src/com/android/settings/regionalpreferences/CalendarTypeControllerTest.java b/tests/unit/src/com/android/settings/regionalpreferences/CalendarTypeControllerTest.java index 7e1ec94af60..4ac25328cb6 100644 --- a/tests/unit/src/com/android/settings/regionalpreferences/CalendarTypeControllerTest.java +++ b/tests/unit/src/com/android/settings/regionalpreferences/CalendarTypeControllerTest.java @@ -19,6 +19,7 @@ package com.android.settings.regionalpreferences; import static org.junit.Assert.assertEquals; import android.content.Context; +import android.icu.util.ULocale; import android.provider.Settings; import androidx.test.core.app.ApplicationProvider; @@ -48,49 +49,57 @@ public class CalendarTypeControllerTest { @After public void tearDown() throws Exception { - RegionalPreferenceUtils.setSettingsProviderContent( + RegionalPreferenceTestUtils.setSettingsProviderContent( mApplicationContext, mCacheProviderContent); Locale.setDefault(mCacheLocale); } @Test - public void getSummary_hasProviderValue_resultIsChinese() { - RegionalPreferenceUtils.setSettingsProviderContent( + public void getSummary_hasProviderValue_resultIsChineseCalendar() { + RegionalPreferenceTestUtils.setSettingsProviderContent( mApplicationContext, "und-u-ca-chinese"); - CharSequence type = mController.getSummary(); + String summary = mController.getSummary().toString(); - assertEquals(LocalePreferences.CalendarType.CHINESE, type.toString()); + assertEquals(getDisplayKeywordValue(LocalePreferences.CalendarType.CHINESE), summary); } @Test - public void getSummary_hasProviderValue_resultIsDangi() { - RegionalPreferenceUtils.setSettingsProviderContent( + public void getSummary_hasProviderValue_resultIsDangiCalendar() { + RegionalPreferenceTestUtils.setSettingsProviderContent( mApplicationContext, "und-u-ca-dangi"); - CharSequence type = mController.getSummary(); + String summary = mController.getSummary().toString(); - assertEquals(LocalePreferences.CalendarType.DANGI, type.toString()); + assertEquals(getDisplayKeywordValue(LocalePreferences.CalendarType.DANGI), summary); } @Test - public void getSummary_noProviderValueButHasDefaultLocaleWithSubtag_resultIsSat() { - RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, ""); + public void getSummary_noProviderValueButHasDefaultLocaleWithSubtag_resultIsChineseCalendar() { + RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, ""); Locale.setDefault(Locale.forLanguageTag("en-US-u-ca-chinese")); - CharSequence type = mController.getSummary(); + String summary = mController.getSummary().toString(); - assertEquals(LocalePreferences.CalendarType.CHINESE, type.toString()); + assertEquals(getDisplayKeywordValue(LocalePreferences.CalendarType.CHINESE), summary); } @Test public void getSummary_noProviderValueAndDefaultLocaleWithoutSubtag_resultIsEmpty() { - RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, ""); + RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, ""); Locale.setDefault(Locale.forLanguageTag("en-US")); - CharSequence type = mController.getSummary(); + String summary = mController.getSummary().toString(); assertEquals(ResourcesUtils.getResourcesString( - mApplicationContext, "default_string_of_regional_preference"), type.toString()); + mApplicationContext, "default_string_of_regional_preference"), summary); + } + + private static String getDisplayKeywordValue(String value) { + String languageTag = new Locale.Builder() + .setUnicodeLocaleKeyword( + RegionalPreferencesFragment.TYPE_CALENDAR, value).build().toLanguageTag(); + return ULocale.getDisplayKeywordValue(languageTag, "calendar", + ULocale.forLocale(Locale.getDefault(Locale.Category.FORMAT))); } } diff --git a/tests/unit/src/com/android/settings/regionalpreferences/FirstDayOfWeekControllerTest.java b/tests/unit/src/com/android/settings/regionalpreferences/FirstDayOfWeekControllerTest.java index b6582421362..062aef8393a 100644 --- a/tests/unit/src/com/android/settings/regionalpreferences/FirstDayOfWeekControllerTest.java +++ b/tests/unit/src/com/android/settings/regionalpreferences/FirstDayOfWeekControllerTest.java @@ -48,47 +48,50 @@ public class FirstDayOfWeekControllerTest { @After public void tearDown() throws Exception { - RegionalPreferenceUtils.setSettingsProviderContent( + RegionalPreferenceTestUtils.setSettingsProviderContent( mApplicationContext, mCacheProviderContent); Locale.setDefault(mCacheLocale); } @Test public void getSummary_hasProviderValue_resultIsWed() { - RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, "und-u-fw-wed"); + RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, "und-u-fw-wed"); - CharSequence day = mController.getSummary(); + String summary = mController.getSummary().toString(); - assertEquals(LocalePreferences.FirstDayOfWeek.WEDNESDAY, day.toString()); + assertEquals(ResourcesUtils.getResourcesString( + mApplicationContext, "wednesday_first_day_of_week"), summary); } @Test public void getSummary_hasProviderValue_resultIsSat() { - RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, "und-u-fw-sat"); + RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, "und-u-fw-sat"); - CharSequence day = mController.getSummary(); + String summary = mController.getSummary().toString(); - assertEquals(LocalePreferences.FirstDayOfWeek.SATURDAY, day.toString()); + assertEquals(ResourcesUtils.getResourcesString( + mApplicationContext, "saturday_first_day_of_week"), summary); } @Test public void getSummary_noProviderValueButHasDefaultLocaleWithSubtag_resultIsSat() { - RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, ""); + RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, ""); Locale.setDefault(Locale.forLanguageTag("en-US-u-fw-sat")); - CharSequence day = mController.getSummary(); + String summary = mController.getSummary().toString(); - assertEquals(LocalePreferences.FirstDayOfWeek.SATURDAY, day.toString()); + assertEquals(ResourcesUtils.getResourcesString( + mApplicationContext, "saturday_first_day_of_week"), summary); } @Test - public void getSummary_noProviderValueAndDefaultLocaleWithoutSubtag_resultIsEmpty() { - RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, ""); + public void getSummary_noProviderValueAndDefaultLocaleWithoutSubtag_resultIsdefault() { + RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, ""); Locale.setDefault(Locale.forLanguageTag("en-US")); - CharSequence day = mController.getSummary(); + String summary = mController.getSummary().toString(); assertEquals(ResourcesUtils.getResourcesString( - mApplicationContext, "default_string_of_regional_preference"), day.toString()); + mApplicationContext, "default_string_of_regional_preference"), summary); } } diff --git a/tests/unit/src/com/android/settings/regionalpreferences/RegionalPreferenceUtils.java b/tests/unit/src/com/android/settings/regionalpreferences/RegionalPreferenceTestUtils.java similarity index 95% rename from tests/unit/src/com/android/settings/regionalpreferences/RegionalPreferenceUtils.java rename to tests/unit/src/com/android/settings/regionalpreferences/RegionalPreferenceTestUtils.java index 709413c7523..d973236ee08 100644 --- a/tests/unit/src/com/android/settings/regionalpreferences/RegionalPreferenceUtils.java +++ b/tests/unit/src/com/android/settings/regionalpreferences/RegionalPreferenceTestUtils.java @@ -20,7 +20,7 @@ import android.content.Context; import android.provider.Settings; /** Utils for each regional preference unit test. */ -public final class RegionalPreferenceUtils { +public final class RegionalPreferenceTestUtils { /** Set language tag to Settings Provider */ public static void setSettingsProviderContent(Context context, String languageTag) { Settings.System.putString(context.getContentResolver(), diff --git a/tests/unit/src/com/android/settings/regionalpreferences/RegionalPreferencesDataUtilsTest.java b/tests/unit/src/com/android/settings/regionalpreferences/RegionalPreferencesDataUtilsTest.java new file mode 100644 index 00000000000..0de3b7edaee --- /dev/null +++ b/tests/unit/src/com/android/settings/regionalpreferences/RegionalPreferencesDataUtilsTest.java @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.regionalpreferences; + +import static org.junit.Assert.assertEquals; + +import android.content.Context; +import android.os.LocaleList; +import android.provider.Settings; + +import androidx.test.core.app.ApplicationProvider; + +import com.android.internal.app.LocalePicker; +import com.android.settings.testutils.ResourcesUtils; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.util.Locale; + +public class RegionalPreferencesDataUtilsTest { + private Context mApplicationContext; + private String mCacheProviderContent = ""; + private Locale mCacheLocale; + private LocaleList mCacheLocaleList; + + @Before + public void setUp() throws Exception { + mApplicationContext = ApplicationProvider.getApplicationContext(); + mCacheProviderContent = Settings.System.getString( + mApplicationContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES); + mCacheLocale = Locale.getDefault(Locale.Category.FORMAT); + mCacheLocaleList = LocaleList.getDefault(); + } + + @After + public void tearDown() throws Exception { + RegionalPreferenceTestUtils.setSettingsProviderContent( + mApplicationContext, mCacheProviderContent); + Locale.setDefault(mCacheLocale); + LocalePicker.updateLocales(mCacheLocaleList); + } + + @Test + public void getDefaultUnicodeExtensionData_hasProviderValue_resultIsCelsius() { + RegionalPreferenceTestUtils.setSettingsProviderContent( + mApplicationContext, "und-u-mu-celsius"); + + String unit = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData( + mApplicationContext, RegionalPreferencesFragment.TYPE_TEMPERATURE); + + assertEquals(LocalePreferences.TemperatureUnit.CELSIUS, unit); + } + + @Test + public void getDefaultUnicodeExtensionData_hasDefaultLocaleSubtag_resultIsCelsius() { + RegionalPreferenceTestUtils.setSettingsProviderContent( + mApplicationContext, "und"); + Locale.setDefault(Locale.forLanguageTag("en-US-u-mu-celsius")); + + String unit = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData( + mApplicationContext, RegionalPreferencesFragment.TYPE_TEMPERATURE); + + assertEquals(LocalePreferences.TemperatureUnit.CELSIUS, unit); + } + + @Test + public void getDefaultUnicodeExtensionData_noSubtag_resultIsDefault() { + RegionalPreferenceTestUtils.setSettingsProviderContent( + mApplicationContext, "und"); + Locale.setDefault(Locale.forLanguageTag("en-US")); + + String unit = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData( + mApplicationContext, RegionalPreferencesFragment.TYPE_TEMPERATURE); + + assertEquals(RegionalPreferencesFragment.TYPE_DEFAULT, unit); + } + + @Test + public void savePreference_saveCalendarIsDangi_success() { + RegionalPreferencesDataUtils.savePreference( + mApplicationContext, + RegionalPreferencesFragment.TYPE_CALENDAR, + LocalePreferences.CalendarType.DANGI + ); + String providerContent = Settings.System.getString( + mApplicationContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES); + Locale locale = Locale.forLanguageTag(providerContent); + + + String result1 = locale.getUnicodeLocaleType(RegionalPreferencesFragment.TYPE_CALENDAR); + + assertEquals(LocalePreferences.CalendarType.DANGI, result1); + + String result2 = Locale.getDefault(Locale.Category.FORMAT) + .getUnicodeLocaleType(RegionalPreferencesFragment.TYPE_CALENDAR); + + assertEquals(LocalePreferences.CalendarType.DANGI, result2); + + } + + @Test + public void temperatureUnitsConverter_inputFahrenheit_resultIsFahrenheitString() { + String result = RegionalPreferencesDataUtils.temperatureUnitsConverter(mApplicationContext, + LocalePreferences.TemperatureUnit.FAHRENHEIT); + + assertEquals(ResourcesUtils.getResourcesString( + mApplicationContext, "fahrenheit_temperature_unit"), result); + } + + @Test + public void temperatureUnitsConverter_inputDefault_resultIsDefaultString() { + String result = RegionalPreferencesDataUtils.temperatureUnitsConverter(mApplicationContext, + RegionalPreferencesFragment.TYPE_DEFAULT); + + assertEquals(ResourcesUtils.getResourcesString( + mApplicationContext, "default_string_of_regional_preference"), result); + } + + @Test + public void dayConverter_inputWed_resultIsWedString() { + String result = RegionalPreferencesDataUtils.dayConverter(mApplicationContext, + LocalePreferences.FirstDayOfWeek.WEDNESDAY); + + assertEquals(ResourcesUtils.getResourcesString( + mApplicationContext, "wednesday_first_day_of_week"), result); + } + + @Test + public void dayConverter_inputDefault_resultIsDefaultString() { + String result = RegionalPreferencesDataUtils.dayConverter(mApplicationContext, + RegionalPreferencesFragment.TYPE_DEFAULT); + + assertEquals(ResourcesUtils.getResourcesString( + mApplicationContext, "default_string_of_regional_preference"), result); + } + + @Test + public void calendarConverter_inputDefault_resultIsDefaultString() { + String result = RegionalPreferencesDataUtils.dayConverter(mApplicationContext, + RegionalPreferencesFragment.TYPE_DEFAULT); + + assertEquals(ResourcesUtils.getResourcesString( + mApplicationContext, "default_string_of_regional_preference"), result); + } +} diff --git a/tests/unit/src/com/android/settings/regionalpreferences/RegionalPreferencesFragmentTest.java b/tests/unit/src/com/android/settings/regionalpreferences/RegionalPreferencesFragmentTest.java new file mode 100644 index 00000000000..f059bf3c76e --- /dev/null +++ b/tests/unit/src/com/android/settings/regionalpreferences/RegionalPreferencesFragmentTest.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.regionalpreferences; + +import static org.junit.Assert.assertEquals; + +import android.app.settings.SettingsEnums; +import android.os.Looper; + +import androidx.test.annotation.UiThreadTest; + +import org.junit.Before; +import org.junit.Test; + +public class RegionalPreferencesFragmentTest { + private RegionalPreferencesFragment mFragment; + + @Before + @UiThreadTest + public void setUp() throws Exception { + if (Looper.myLooper() == null) { + Looper.prepare(); + } + mFragment = new RegionalPreferencesFragment(); + } + + @Test + @UiThreadTest + public void getMetricsCategory_typeIsCa_resultIsCalendarPreference() { + mFragment.mType = RegionalPreferencesFragment.TYPE_CALENDAR; + + int result = mFragment.getMetricsCategory(); + + assertEquals(SettingsEnums.CALENDAR_PREFERENCE, result); + } + + @Test + @UiThreadTest + public void getMetricsCategory_typeIsFw_resultIsFirstDayOfWeekPreference() { + mFragment.mType = RegionalPreferencesFragment.TYPE_FIRST_DAY_OF_WEEK; + + int result = mFragment.getMetricsCategory(); + + assertEquals(SettingsEnums.FIRST_DAY_OF_WEEK_PREFERENCE, result); + } + + @Test + @UiThreadTest + public void getMetricsCategory_typeIsMu_resultIsTemperaturePreference() { + mFragment.mType = RegionalPreferencesFragment.TYPE_TEMPERATURE; + + int result = mFragment.getMetricsCategory(); + + assertEquals(SettingsEnums.TEMPERATURE_PREFERENCE, result); + } +} diff --git a/tests/unit/src/com/android/settings/regionalpreferences/TemperatureUnitControllerTest.java b/tests/unit/src/com/android/settings/regionalpreferences/TemperatureUnitControllerTest.java index 4f473ad604d..aa652cab1f4 100644 --- a/tests/unit/src/com/android/settings/regionalpreferences/TemperatureUnitControllerTest.java +++ b/tests/unit/src/com/android/settings/regionalpreferences/TemperatureUnitControllerTest.java @@ -48,49 +48,52 @@ public class TemperatureUnitControllerTest { @After public void tearDown() throws Exception { - RegionalPreferenceUtils.setSettingsProviderContent( + RegionalPreferenceTestUtils.setSettingsProviderContent( mApplicationContext, mCacheProviderContent); Locale.setDefault(mCacheLocale); } @Test public void getSummary_hasProviderValue_resultIsCelsius() { - RegionalPreferenceUtils.setSettingsProviderContent( + RegionalPreferenceTestUtils.setSettingsProviderContent( mApplicationContext, "und-u-mu-celsius"); - CharSequence unit = mController.getSummary(); + String summary = mController.getSummary().toString(); - assertEquals(LocalePreferences.TemperatureUnit.CELSIUS, unit.toString()); + assertEquals(ResourcesUtils.getResourcesString( + mApplicationContext, "celsius_temperature_unit"), summary); } @Test public void getSummary_hasProviderValue_resultIsFahrenheit() { - RegionalPreferenceUtils.setSettingsProviderContent( + RegionalPreferenceTestUtils.setSettingsProviderContent( mApplicationContext, "und-u-mu-fahrenhe"); - CharSequence unit = mController.getSummary(); + String summary = mController.getSummary().toString(); - assertEquals(LocalePreferences.TemperatureUnit.FAHRENHEIT, unit.toString()); + assertEquals(ResourcesUtils.getResourcesString( + mApplicationContext, "fahrenheit_temperature_unit"), summary); } @Test public void getSummary_noProviderValueButHasDefaultLocaleWithSubtag_resultIsFahrenheit() { - RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, ""); + RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, ""); Locale.setDefault(Locale.forLanguageTag("en-US-u-mu-fahrenhe")); - CharSequence unit = mController.getSummary(); + String summary = mController.getSummary().toString(); - assertEquals(LocalePreferences.TemperatureUnit.FAHRENHEIT, unit.toString()); + assertEquals(ResourcesUtils.getResourcesString( + mApplicationContext, "fahrenheit_temperature_unit"), summary); } @Test - public void getSummary_noProviderValueAndDefaultLocaleWithoutSubtag_resultIsEmpty() { - RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, ""); + public void getSummary_noProviderValueAndDefaultLocaleWithoutSubtag_resultIsDefault() { + RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, ""); Locale.setDefault(Locale.forLanguageTag("en-US")); - CharSequence unit = mController.getSummary(); + String summary = mController.getSummary().toString(); assertEquals(ResourcesUtils.getResourcesString( - mApplicationContext, "default_string_of_regional_preference"), unit.toString()); + mApplicationContext, "default_string_of_regional_preference"), summary); } }