From 31dfbdabf10a2b085e06321a984ef5d8d5477403 Mon Sep 17 00:00:00 2001 From: Zoey Chen Date: Wed, 6 Nov 2024 13:14:53 +0000 Subject: [PATCH] [Settings] Refactor: Add LocalePickerBaseListPreferenceController Bug: 377664066 Flag: EXEMPT refactor Change-Id: I54d212ea520b67263f5b797175936c046a534519 --- .../LocaleLinearLayoutManager.java | 3 + .../LocaleListSearchCallback.java | 29 +++ ...alePickerBaseListPreferenceController.java | 210 ++++++++++++++++++ .../LocalePickerWithRegionActivity.java | 7 +- 4 files changed, 248 insertions(+), 1 deletion(-) create mode 100644 src/com/android/settings/localepicker/LocaleListSearchCallback.java create mode 100644 src/com/android/settings/localepicker/LocalePickerBaseListPreferenceController.java diff --git a/src/com/android/settings/localepicker/LocaleLinearLayoutManager.java b/src/com/android/settings/localepicker/LocaleLinearLayoutManager.java index 59a5fb0a596..a7ebe32b841 100644 --- a/src/com/android/settings/localepicker/LocaleLinearLayoutManager.java +++ b/src/com/android/settings/localepicker/LocaleLinearLayoutManager.java @@ -39,7 +39,10 @@ import com.android.settings.R; *

It only removes one locale at the time, but most users don't * really add many locales "by mistake", so there is no real need * to delete a lot of locales at once.

+ * + * @deprecated use SettingLib's widget instead of customized UIs. */ +@Deprecated public class LocaleLinearLayoutManager extends LinearLayoutManager { private final LocaleDragAndDropAdapter mAdapter; private final Context mContext; diff --git a/src/com/android/settings/localepicker/LocaleListSearchCallback.java b/src/com/android/settings/localepicker/LocaleListSearchCallback.java new file mode 100644 index 00000000000..b80bbccccdd --- /dev/null +++ b/src/com/android/settings/localepicker/LocaleListSearchCallback.java @@ -0,0 +1,29 @@ +/** + * 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.localepicker; + +import androidx.annotation.NonNull; + +import com.android.internal.app.LocaleStore; + +import java.util.List; + +/** Interface for when locale list changes in SearchView . */ +public interface LocaleListSearchCallback { + + /** Callback method for searching changes. */ + void onSearchListChanged(@NonNull List localeInfoList); +} diff --git a/src/com/android/settings/localepicker/LocalePickerBaseListPreferenceController.java b/src/com/android/settings/localepicker/LocalePickerBaseListPreferenceController.java new file mode 100644 index 00000000000..8c71abf7248 --- /dev/null +++ b/src/com/android/settings/localepicker/LocalePickerBaseListPreferenceController.java @@ -0,0 +1,210 @@ +/** + * 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.localepicker; + +import android.content.Context; +import android.os.LocaleList; +import android.provider.Settings; +import android.util.ArrayMap; +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.preference.Preference; +import androidx.preference.PreferenceCategory; +import androidx.preference.PreferenceScreen; + +import com.android.internal.app.LocaleCollectorBase; +import com.android.internal.app.LocaleHelper; +import com.android.internal.app.LocaleStore; +import com.android.settings.core.BasePreferenceController; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +/** A base controller for handling locale controllers. */ +public abstract class LocalePickerBaseListPreferenceController extends + BasePreferenceController implements LocaleListSearchCallback { + + private static final String TAG = "LocalePickerBaseListPreference"; + private static final String KEY_SUGGESTED = "suggested"; + + private PreferenceCategory mPreferenceCategory; + private LocaleList mExplicitLocales; + private Set mLocaleList; + private List mLocaleOptions; + private Map mPreferences; + private String mPackageName; + private boolean mCountryMode; + + public LocalePickerBaseListPreferenceController(@NonNull Context context, + @NonNull String preferenceKey) { + super(context, preferenceKey); + // TODO: Should get extra from fragment. +// if (isDeviceDemoMode()) { +// Bundle bundle = preference.getExtras(); +// mExplicitLocales = bundle == null +// ? null +// : bundle.getParcelable(Settings.EXTRA_EXPLICIT_LOCALES, LocaleList.class); +// Log.d(TAG, "Has explicit locales : " + mExplicitLocales); +// } + mLocaleList = getLocaleCollectorController(context).getSupportedLocaleList(null, + false, false); + mLocaleOptions = new ArrayList<>(mLocaleList.size()); + mPreferences = new ArrayMap<>(); + } + + private boolean isDeviceDemoMode() { + return Settings.Global.getInt( + mContext.getContentResolver(), Settings.Global.DEVICE_DEMO_MODE, 0) == 1; + } + + @Override + public void displayPreference(@NonNull PreferenceScreen screen) { + super.displayPreference(screen); + mPreferenceCategory = screen.findPreference(getPreferenceCategoryKey()); + updatePreferences(); + } + + private void updatePreferences() { + if (mPreferenceCategory == null) { + Log.d(TAG, "updatePreferences, mPreferenceCategory is null"); + return; + } + + List result; + result = getSortedLocaleList( + getPreferenceCategoryKey().contains(KEY_SUGGESTED) ? getSuggestedLocaleList() + : getSupportedLocaleList()); + + final Map existingPreferences = mPreferences; + mPreferences = new ArrayMap<>(); + setupPreference(result, existingPreferences); + + for (Preference pref : existingPreferences.values()) { + mPreferenceCategory.removePreference(pref); + } + } + + @Override + public void onSearchListChanged(@NonNull List newList) { + mPreferenceCategory.removeAll(); + mPreferences.clear(); + final Map existingPreferences = mPreferences; + if (getPreferenceCategoryKey().contains(KEY_SUGGESTED)) { + newList = getSortedSuggestedLocaleFromSearchList( + newList, getSuggestedLocaleList()); + } + if (!newList.isEmpty()) { + mPreferenceCategory.setVisible(true); + setupPreference(newList, existingPreferences); + } else { + mPreferenceCategory.setVisible(false); + } + } + + private List getSortedSuggestedLocaleFromSearchList( + List listOptions, + List listSuggested) { + List searchItem = new ArrayList<>(); + for (LocaleStore.LocaleInfo option : listOptions) { + for (LocaleStore.LocaleInfo suggested : listSuggested) { + if (suggested.toString().contains(option.toString())) { + searchItem.add(suggested); + } + } + } + searchItem = getSortedLocaleList(searchItem); + return searchItem; + } + + private void setupPreference(List localeInfoList, + Map existingPreferences) { + localeInfoList.stream().forEach(locale -> + { + Preference pref = existingPreferences.remove(locale.getId()); + if (pref == null) { + pref = new Preference(mContext); + mPreferenceCategory.addPreference(pref); + } + String localeName = + mCountryMode ? locale.getFullCountryNameNative() : locale.getFullNameNative(); + pref.setTitle(localeName); + pref.setKey(locale.toString()); + pref.setOnPreferenceClickListener(clickedPref -> { + // TODO: Click locale to show region or numbering system page if needed. + return true; + }); + mPreferences.put(locale.getId(), pref); + }); + } + + @Override + public int getAvailabilityStatus() { + return AVAILABLE; + } + + protected abstract String getPreferenceCategoryKey(); + + protected abstract LocaleCollectorBase getLocaleCollectorController(Context context); + + protected String getPackageName() { + return mPackageName; + } + + protected LocaleList getExplicitLocaleList() { + return mExplicitLocales; + } + + protected List getSuggestedLocaleList() { + mLocaleOptions.clear(); + if (mLocaleList != null && !mLocaleList.isEmpty()) { + mLocaleOptions.addAll(mLocaleList.stream() + .filter(localeInfo -> localeInfo.isSuggested()) + .collect(Collectors.toList())); + } else { + Log.d(TAG, "Can not get suggested locales because the locale list is null or empty."); + } + + return mLocaleOptions; + } + + protected List getSupportedLocaleList() { + if (mLocaleList != null && !mLocaleList.isEmpty()) { + mLocaleOptions.addAll(mLocaleList.stream() + .filter(localeInfo -> !localeInfo.isSuggested()) + .collect(Collectors.toList())); + } else { + Log.d(TAG, "Can not get supported locales because the locale list is null or empty."); + } + + return mLocaleOptions; + } + + private List getSortedLocaleList( + List localeInfos) { + final Locale sortingLocale = Locale.getDefault(); + final LocaleHelper.LocaleInfoComparator comp = + new LocaleHelper.LocaleInfoComparator(sortingLocale, mCountryMode); + Collections.sort(localeInfos, comp); + return localeInfos; + } +} diff --git a/src/com/android/settings/localepicker/LocalePickerWithRegionActivity.java b/src/com/android/settings/localepicker/LocalePickerWithRegionActivity.java index 05cb6a48d1e..3cf76837ea5 100644 --- a/src/com/android/settings/localepicker/LocalePickerWithRegionActivity.java +++ b/src/com/android/settings/localepicker/LocalePickerWithRegionActivity.java @@ -34,7 +34,12 @@ import com.android.internal.app.LocaleStore; import com.android.settings.R; import com.android.settings.core.SettingsBaseActivity; -/** A activity to show the locale picker page. */ +/** + * An activity to show the locale picker page. + * + * @deprecated use {@link com.android.settings.localepicker.SystemLocalePickerFragment} instead. + */ +@Deprecated public class LocalePickerWithRegionActivity extends SettingsBaseActivity implements LocalePickerWithRegion.LocaleSelectedListener, MenuItem.OnActionExpandListener { private static final String TAG = LocalePickerWithRegionActivity.class.getSimpleName();