1 /*
2  * Copyright (C) 2020 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.development;
18 
19 import android.app.ActivityManager;
20 import android.app.IActivityManager;
21 import android.content.Context;
22 import android.content.res.Configuration;
23 import android.os.RemoteException;
24 import android.os.SystemProperties;
25 import android.text.TextUtils;
26 
27 import androidx.annotation.VisibleForTesting;
28 import androidx.preference.ListPreference;
29 import androidx.preference.Preference;
30 
31 import com.android.settings.core.PreferenceControllerMixin;
32 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
33 
34 /**
35  * Preference controller to control Grammatical Gender
36  */
37 public class GrammaticalGenderPreferenceController extends DeveloperOptionsPreferenceController
38         implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
39 
40     private static final String GRAMMATICAL_GENDER_KEY =
41             "grammatical_gender";
42     @VisibleForTesting
43     static final String GRAMMATICAL_GENDER_PROPERTY = "persist.sys.grammatical_gender";
44     private final String[] mListValues;
45     private final String[] mListSummaries;
46 
47     private IActivityManager mActivityManager;
48 
GrammaticalGenderPreferenceController(Context context)49     public GrammaticalGenderPreferenceController(Context context) {
50         this(context, ActivityManager.getService());
51     }
52 
53     @VisibleForTesting
GrammaticalGenderPreferenceController(Context context, IActivityManager activityManager)54     GrammaticalGenderPreferenceController(Context context,
55             IActivityManager activityManager) {
56         super(context);
57 
58         mListValues = context.getResources()
59                 .getStringArray(com.android.settingslib.R.array.grammatical_gender_values);
60         mListSummaries = context.getResources()
61                 .getStringArray(com.android.settingslib.R.array.grammatical_gender_entries);
62         mActivityManager = activityManager;
63     }
64 
65     @Override
getPreferenceKey()66     public String getPreferenceKey() {
67         return GRAMMATICAL_GENDER_KEY;
68     }
69 
70     @Override
onPreferenceChange(Preference preference, Object newValue)71     public boolean onPreferenceChange(Preference preference, Object newValue) {
72         final var oldValue = SystemProperties.getInt(GRAMMATICAL_GENDER_PROPERTY,
73                 Configuration.GRAMMATICAL_GENDER_NOT_SPECIFIED);
74         SystemProperties.set(GRAMMATICAL_GENDER_PROPERTY, newValue.toString());
75         updateState(mPreference);
76         try {
77             Configuration config = mActivityManager.getConfiguration();
78             // Only apply the developer settings value if it is the one currently used,
79             // otherwise it means there's some kind of override that we don't want to
80             // touch here.
81             if (config.getGrammaticalGender() == oldValue) {
82                 config.setGrammaticalGender(Integer.parseInt(newValue.toString()));
83                 mActivityManager.updatePersistentConfiguration(config);
84             }
85         } catch (RemoteException ex) {
86             // intentional no-op
87         }
88         return true;
89     }
90 
91     @Override
updateState(Preference preference)92     public void updateState(Preference preference) {
93         final ListPreference listPreference = (ListPreference) preference;
94         final String currentValue = SystemProperties.get(GRAMMATICAL_GENDER_PROPERTY);
95         int index = 0; // Defaults to Not Selected
96         for (int i = 0; i < mListValues.length; i++) {
97             if (TextUtils.equals(currentValue, mListValues[i])) {
98                 index = i;
99                 break;
100             }
101         }
102         listPreference.setValue(mListValues[index]);
103         listPreference.setSummary(mListSummaries[index]);
104     }
105 
106     @Override
isAvailable()107     public boolean isAvailable() {
108         return android.app.Flags.systemTermsOfAddressEnabled();
109     }
110 }
111