Refactor CaptionMoreOptionsFragment to improve maintainability

Root cause: There is a bunch of different logic of preferences in CaptionMoreOptionsFragment. It’s hard to implement new features and hard to maintain and hard to be testable.
Solution: Move out different logic of CaptionMoreOptionsFragment into controllers to reduce the complexity of the relationship between preference and fragment.

Bug: 197695932
Test: make RunSettingsRoboTests ROBOTEST_FILTER=CaptionMoreOptionsFragmentTest CaptionLocalePreferenceControllerTest
Change-Id: Ifdf3e22b027328a9f8eb13d756730ca047ece047
This commit is contained in:
menghanli
2022-06-29 08:27:12 +08:00
committed by Menghan Li
parent fe33741eda
commit deb72e29ef
5 changed files with 211 additions and 50 deletions

View File

@@ -0,0 +1,66 @@
/*
* 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.accessibility;
import android.content.Context;
import android.provider.Settings;
import android.view.accessibility.CaptioningManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
/** Controller that shows the caption locale summary. */
public class CaptionLocalePreferenceController extends BasePreferenceController
implements Preference.OnPreferenceChangeListener {
private final CaptioningManager mCaptioningManager;
private LocalePreference mPreference;
public CaptionLocalePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mCaptioningManager = context.getSystemService(CaptioningManager.class);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
final String rawLocale = mCaptioningManager.getRawLocale();
mPreference.setValue(rawLocale == null ? "" : rawLocale);
}
@Override
public CharSequence getSummary() {
return mPreference.getEntry();
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Settings.Secure.putString(mContext.getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_LOCALE, (String) newValue);
mPreference.setValue((String) newValue);
mPreference.setSummary(mPreference.getEntry());
return true;
}
}