1 /** 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.localepicker; 18 19 import android.content.Context; 20 21 import androidx.annotation.NonNull; 22 import androidx.preference.PreferenceScreen; 23 24 import com.android.settings.core.BasePreferenceController; 25 import com.android.settings.overlay.FeatureFactory; 26 import com.android.settings.widget.TickButtonPreference; 27 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 28 29 import java.util.concurrent.Executor; 30 import java.util.concurrent.Executors; 31 32 public abstract class TermsOfAddressBaseController extends BasePreferenceController { 33 34 private static final Executor sExecutor = Executors.newSingleThreadExecutor(); 35 private PreferenceScreen mPreferenceScreen; 36 private MetricsFeatureProvider mMetricsFeatureProvider; 37 private TickButtonPreference mPreference; 38 private TermsOfAddressHelper mTermsOfAddressHelper; 39 TermsOfAddressBaseController(Context context, String preferenceKey)40 public TermsOfAddressBaseController(Context context, String preferenceKey) { 41 super(context, preferenceKey); 42 mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider(); 43 } 44 setTermsOfAddressHelper(@onNull TermsOfAddressHelper termsOfAddressHelper)45 public void setTermsOfAddressHelper(@NonNull TermsOfAddressHelper termsOfAddressHelper) { 46 mTermsOfAddressHelper = termsOfAddressHelper; 47 } 48 49 @Override displayPreference(PreferenceScreen screen)50 public void displayPreference(PreferenceScreen screen) { 51 super.displayPreference(screen); 52 mPreferenceScreen = screen; 53 mPreference = screen.findPreference(getPreferenceKey()); 54 mPreference.setOnPreferenceClickListener(clickedPref -> { 55 sExecutor.execute( 56 () -> { 57 mTermsOfAddressHelper.setSystemGrammaticalGender( 58 getGrammaticalGenderType()); 59 }); 60 setSelected(mPreference); 61 mMetricsFeatureProvider.action(mContext, getMetricsActionKey()); 62 return true; 63 }); 64 updatePreferences(); 65 } 66 setSelected(TickButtonPreference preference)67 private void setSelected(TickButtonPreference preference) { 68 for (int i = 1; i < mPreferenceScreen.getPreferenceCount(); i++) { 69 TickButtonPreference pref = (TickButtonPreference) mPreferenceScreen.getPreference(i); 70 pref.setSelected(pref.getKey().equals(preference.getKey())); 71 } 72 } 73 updatePreferences()74 private void updatePreferences() { 75 if (mPreference == null) { 76 return; 77 } 78 mPreference.setSelected( 79 mTermsOfAddressHelper.getSystemGrammaticalGender() == getGrammaticalGenderType()); 80 } 81 82 @Override getAvailabilityStatus()83 public int getAvailabilityStatus() { 84 return AVAILABLE; 85 } 86 getMetricsActionKey()87 protected abstract int getMetricsActionKey(); 88 getGrammaticalGenderType()89 protected abstract int getGrammaticalGenderType(); 90 } 91