Add measurement system

1. add UX
2. add main functionality

Bug: 375979373
Flag: com.android.settings.flags.regional_preferences_api_enabled
Test: manual, atest
Change-Id: Ie0c82551ff4e9ab0c70fa5d3c32c62d1d7e26b87
This commit is contained in:
danielwbhuang
2024-11-01 23:37:51 +08:00
parent 13d90e8024
commit fffe05404f
11 changed files with 376 additions and 3 deletions

View File

@@ -25,12 +25,14 @@ public class ExtensionTypes {
public static final String FIRST_DAY_OF_WEEK = "fw";
public static final String NUMBERING_SYSTEM = "nu";
public static final String TEMPERATURE_UNIT = "mu";
public static final String MEASUREMENT_SYSTEM = "ms";
@StringDef({
FIRST_DAY_OF_WEEK,
CALENDAR,
TEMPERATURE_UNIT,
NUMBERING_SYSTEM
NUMBERING_SYSTEM,
MEASUREMENT_SYSTEM
})
public @interface Values {}
}

View File

@@ -0,0 +1,68 @@
/**
* Copyright (C) 2024 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.os.LocaleList;
import androidx.annotation.NonNull;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.flags.Flags;
import java.util.Locale;
/** A controller for the entry of measurement system page */
public class MeasurementSystemController extends BasePreferenceController {
private static final String TAG = "MeasurementSystemController";
public MeasurementSystemController(@NonNull Context context, @NonNull String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getAvailabilityStatus() {
if (Flags.regionalPreferencesApiEnabled()) {
return AVAILABLE;
}
return CONDITIONALLY_UNAVAILABLE;
}
@Override
@NonNull
public CharSequence getSummary() {
LocaleList localeList = LocaleList.getDefault();
Locale locale = localeList.get(0);
return getMeasurementSystem(locale);
}
private String getMeasurementSystem(Locale locale) {
String type = locale.getUnicodeLocaleType(
RegionalPreferencesDataUtils.EXTENSION_TYPE_MEASUREMENT_SYSTEM);
if (type != null) {
if (type.equals(RegionalPreferencesDataUtils.MEASUREMENT_SYSTEM_METRIC)) {
return mContext.getString(R.string.metric_measurement_system);
}
if (type.equals(RegionalPreferencesDataUtils.MEASUREMENT_SYSTEM_UK)) {
return mContext.getString(R.string.uk_measurement_system);
}
return mContext.getString(R.string.us_measurement_system);
} else {
return mContext.getString(R.string.default_string_of_regional_preference);
}
}
}

View File

@@ -0,0 +1,62 @@
/**
* Copyright (C) 2024 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.util.Log;
import androidx.annotation.NonNull;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import com.android.settings.widget.PreferenceCategoryController;
/** Category preference controller for measurement system preferences. */
public class MeasurementSystemItemCategoryController extends PreferenceCategoryController {
private static final String LOG_TAG = "MeasurementSystemItemCategoryController";
private static final String KEY_PREFERENCE_CATEGORY_MEASUREMENT_SYSTEM_ITEM =
"measurement_system_item_category";
private static final String KEY_PREFERENCE_MEASUREMENT_SYSTEM_ITEM =
"measurement_system_item_list";
public MeasurementSystemItemCategoryController(@NonNull Context context, @NonNull String key) {
super(context, key);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void displayPreference(@NonNull PreferenceScreen screen) {
super.displayPreference(screen);
PreferenceCategory preferenceCategory =
screen.findPreference(KEY_PREFERENCE_CATEGORY_MEASUREMENT_SYSTEM_ITEM);
if (preferenceCategory == null) {
Log.d(LOG_TAG, "displayPreference(), Can not find the category.");
return;
}
preferenceCategory.setVisible(isAvailable());
MeasurementSystemItemListController measurementSystemItemListController =
new MeasurementSystemItemListController(
mContext,
KEY_PREFERENCE_MEASUREMENT_SYSTEM_ITEM);
measurementSystemItemListController.displayPreference(screen);
}
}

View File

@@ -0,0 +1,72 @@
/**
* Copyright (C) 2024 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.content.Context;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.flags.Flags;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.List;
/** Main fragment to display measurement system. */
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class MeasurementSystemItemFragment extends DashboardFragment {
private static final String LOG_TAG = "MeasurementSystemItemFragment";
private static final String KEY_PREFERENCE_CATEGORY_MEASUREMENT_SYSTEM_ITEM =
"measurement_system_item_category";
@Override
protected int getPreferenceScreenResId() {
return R.xml.regional_preferences_measurement_system;
}
@Override
public int getMetricsCategory() {
return SettingsEnums.MEASUREMENT_SYSTEM_PREFERENCE;
}
@Override
protected String getLogTag() {
return LOG_TAG;
}
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new MeasurementSystemItemCategoryController(context,
KEY_PREFERENCE_CATEGORY_MEASUREMENT_SYSTEM_ITEM));
return controllers;
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.regional_preferences_measurement_system) {
@Override
protected boolean isPageSearchEnabled(Context context) {
if (Flags.regionalPreferencesApiEnabled()) {
return false;
}
return true;
}
};
}

View File

@@ -0,0 +1,69 @@
/**
* Copyright (C) 2024 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.content.Context;
import androidx.annotation.NonNull;
import com.android.settings.R;
/** A controller for handling all measurement system preferences. */
public class MeasurementSystemItemListController extends
RegionalPreferenceListBasePreferenceController {
private static final String KEY_PREFERENCE_CATEGORY_MEASUREMENT_SYSTEM_ITEM =
"measurement_system_item_category";
private static final String KEY_PREFERENCE_MEASUREMENT_SYSTEM_ITEM =
"measurement_system_item_list";
public MeasurementSystemItemListController(@NonNull Context context, @NonNull String key) {
super(context, key);
}
@Override
protected String getPreferenceTitle(String item) {
return RegionalPreferencesDataUtils.measurementSystemConverter(mContext, item);
}
@Override
protected String getPreferenceCategoryKey() {
return KEY_PREFERENCE_CATEGORY_MEASUREMENT_SYSTEM_ITEM;
}
@Override
@NonNull
public String getPreferenceKey() {
return KEY_PREFERENCE_MEASUREMENT_SYSTEM_ITEM;
}
@Override
protected String getExtensionTypes() {
return ExtensionTypes.MEASUREMENT_SYSTEM;
}
@Override
protected String[] getUnitValues() {
return mContext.getResources().getStringArray(R.array.measurement_system);
}
@Override
protected int getMetricsActionKey() {
return SettingsEnums.ACTION_SET_MEASUREMENT_SYSTEM;
}
}

View File

@@ -65,8 +65,8 @@ public abstract class RegionalPreferenceListBasePreferenceController extends
RegionalPreferencesDataUtils.savePreference(mContext, getExtensionTypes(),
item.equals(RegionalPreferencesDataUtils.DEFAULT_VALUE)
? null : item);
String metrics =
getMetricsActionKey() == SettingsEnums.ACTION_SET_FIRST_DAY_OF_WEEK ? ""
String metrics = shouldUseEmptyMetrics()
? ""
: getPreferenceTitle(value) + " > " + getPreferenceTitle(item);
mMetricsFeatureProvider.action(mContext, getMetricsActionKey(), metrics);
});
@@ -79,6 +79,14 @@ public abstract class RegionalPreferenceListBasePreferenceController extends
return AVAILABLE;
}
private boolean shouldUseEmptyMetrics() {
if (getMetricsActionKey() == SettingsEnums.ACTION_SET_FIRST_DAY_OF_WEEK
|| getMetricsActionKey() == SettingsEnums.ACTION_SET_MEASUREMENT_SYSTEM) {
return true;
}
return false;
}
protected abstract String getPreferenceTitle(String item);
protected abstract String getPreferenceCategoryKey();

View File

@@ -17,6 +17,7 @@
package com.android.settings.regionalpreferences;
import android.content.Context;
import android.icu.util.LocaleData;
import android.icu.util.ULocale;
import android.os.LocaleList;
import android.provider.Settings;
@@ -32,6 +33,10 @@ import java.util.Locale;
/** Provides utils for regional preferences. */
public class RegionalPreferencesDataUtils {
static final String DEFAULT_VALUE = "default";
static final String EXTENSION_TYPE_MEASUREMENT_SYSTEM = "ms";
static final String MEASUREMENT_SYSTEM_METRIC = "metric";
static final String MEASUREMENT_SYSTEM_UK = "uksystem";
static final String MEASUREMENT_SYSTEM_US = "ussystem";
static String getDefaultUnicodeExtensionData(Context contxt, String type) {
// 1. Check cache data in Settings provider.
@@ -118,4 +123,30 @@ public class RegionalPreferencesDataUtils {
return context.getString(R.string.default_string_of_regional_preference);
}
}
static String measurementSystemConverter(Context context, String unit) {
switch (unit) {
case MEASUREMENT_SYSTEM_METRIC:
return context.getString(R.string.metric_measurement_system);
case MEASUREMENT_SYSTEM_UK:
return context.getString(R.string.uk_measurement_system);
case MEASUREMENT_SYSTEM_US:
return context.getString(R.string.us_measurement_system);
default:
return context.getString(R.string.default_string_of_regional_preference);
}
}
static String getDefaultMeasurementSystem() {
LocaleList localeList = LocaleList.getDefault();
Locale locale = localeList.get(0);
ULocale uLocale = ULocale.forLocale(locale);
if (LocaleData.getMeasurementSystem(uLocale) == LocaleData.MeasurementSystem.SI) {
return RegionalPreferencesDataUtils.MEASUREMENT_SYSTEM_METRIC;
}
if (LocaleData.getMeasurementSystem(uLocale) == LocaleData.MeasurementSystem.UK) {
return RegionalPreferencesDataUtils.MEASUREMENT_SYSTEM_UK;
}
return RegionalPreferencesDataUtils.MEASUREMENT_SYSTEM_US;
}
}