Merge "[Regional Preference] Create each page for regional preferences"

This commit is contained in:
Tom Hsu
2023-01-05 05:34:10 +00:00
committed by Android (Google) Code Review
17 changed files with 876 additions and 51 deletions

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<!-- Settings button -->
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tick_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="center"
android:src="@drawable/ic_check_24dp" />

View File

@@ -1360,4 +1360,39 @@
[CHAR LIMIT=NONE] -->
<string-array name="allowlist_hide_summary_in_battery_usage" translatable="false">
</string-array>
<!-- A list for all temperature units. [CHAR LIMIT=NONE] -->
<string-array name="temperature_units">
<item>default</item>
<item>celsius</item>
<item>fahrenheit</item>
<item>kelvin</item>
</string-array>
<!-- A list for all days of a week. [CHAR LIMIT=NONE] -->
<string-array name="first_day_of_week">
<item>default</item>
<item>sun</item>
<item>mon</item>
<item>tue</item>
<item>wed</item>
<item>thu</item>
<item>fri</item>
<item>sat</item>
</string-array>
<!-- A list for all supported calendar types. [CHAR LIMIT=NONE] -->
<string-array name="calendar_type">
<item>default</item>
<item>chinese</item>
<item>dangi</item>
<item>hebrew</item>
<item>indian</item>
<item>islamic</item>
<item>islamic-rgsa</item>
<item>islamic-tbla</item>
<item>islamic-umalqura</item>
<item>persian</item>
</string-array>
</resources>

View File

@@ -371,6 +371,26 @@
<string name="numbers_preferences_title">Numbers</string>
<!-- The summary of default string for each regional preference. [CHAR LIMIT=50] -->
<string name="default_string_of_regional_preference">[No preference]</string>
<!-- The title of Celsius for preference of temperature unit. [CHAR LIMIT=50] -->
<string name="celsius_temperature_unit">Celsius</string>
<!-- The title of Fahrenheit for preference of temperature unit. [CHAR LIMIT=50] -->
<string name="fahrenheit_temperature_unit">Fahrenheit</string>
<!-- The title of Kevin for preference of temperature unit. [CHAR LIMIT=50] -->
<string name="kevin_temperature_unit">Kevin</string>
<!-- The title of sunday for preference of first day of week. [CHAR LIMIT=50] -->
<string name="sunday_first_day_of_week">Sunday</string>
<!-- The title of monday for preference of first day of week. [CHAR LIMIT=50] -->
<string name="monday_first_day_of_week">Monday</string>
<!-- The title of tuesday for preference of first day of week. [CHAR LIMIT=50] -->
<string name="tuesday_first_day_of_week">Tuesday</string>
<!-- The title of wednesday for preference of first day of week. [CHAR LIMIT=50] -->
<string name="wednesday_first_day_of_week">Wednesday</string>
<!-- The title of thursday for preference of first day of week. [CHAR LIMIT=50] -->
<string name="thursday_first_day_of_week">Thursday</string>
<!-- The title of friday for preference of first day of week. [CHAR LIMIT=50] -->
<string name="friday_first_day_of_week">Friday</string>
<!-- The title of saturday for preference of first day of week. [CHAR LIMIT=50] -->
<string name="saturday_first_day_of_week">Saturday</string>
<!-- The title of the confirmation dialog shown when the user selects one / several languages and tries to remove them [CHAR LIMIT=60] -->
<string name="dlg_remove_locales_title">{count, plural,

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
</PreferenceScreen>

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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)));
}
}

View File

@@ -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);
}
}

View File

@@ -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(),

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}