1 /*
2  * Copyright (C) 2017 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.tv.settings.accessibility;
18 
19 import static com.android.tv.settings.util.InstrumentationUtils.logEntrySelected;
20 
21 import android.app.tvsettings.TvSettingsEnums;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.os.Bundle;
25 import android.provider.Settings;
26 
27 import androidx.annotation.Keep;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceScreen;
30 
31 import com.android.tv.settings.R;
32 import com.android.tv.settings.RadioPreference;
33 import com.android.tv.settings.SettingsPreferenceFragment;
34 import com.android.tv.settings.overlay.FlavorUtils;
35 
36 @Keep
37 public class AccessibilityTimeoutFragment extends SettingsPreferenceFragment
38         implements Preference.OnPreferenceChangeListener {
39     private int mCurrentA11yTimeout;
40 
41     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)42     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
43         setPreferencesFromResource(R.xml.accessibility_timeout, null);
44         PreferenceScreen a11yTimeoutScreen = getPreferenceManager().getPreferenceScreen();
45         final Context themedContext = getPreferenceManager().getContext();
46         final boolean isTwoPanel = FlavorUtils.isTwoPanel(getContext());
47         final String[] entries =
48                 getContext().getResources().getStringArray(R.array.a11y_timeout_entries);
49         final String[] entryValues =
50                 getContext().getResources().getStringArray(R.array.a11y_timeout_values);
51         initA11yTimeoutValue();
52 
53         for (int i = 0; i < entries.length; i++) {
54             final RadioPreference radioPreference = new RadioPreference(themedContext);
55             radioPreference.setTitle(entries[i]);
56             radioPreference.setKey(entryValues[i]);
57             radioPreference.setOnPreferenceChangeListener(this);
58             radioPreference.setPersistent(false);
59             if (mCurrentA11yTimeout == Integer.parseInt(entryValues[i])) {
60                 radioPreference.setChecked(true);
61             }
62             if (isTwoPanel) {
63                 radioPreference.setFragment(AccessibilityTimeoutInfoFragment.class.getName());
64             }
65             a11yTimeoutScreen.addPreference(radioPreference);
66         }
67     }
68 
69     @Override
onPreferenceChange(Preference preference, Object newValue)70     public boolean onPreferenceChange(Preference preference, Object newValue) {
71         RadioPreference radioPreference = (RadioPreference) preference;
72         if (radioPreference.isChecked()) {
73             return false;
74         }
75         PreferenceScreen a11yTimeoutScreen = getPreferenceManager().getPreferenceScreen();
76         radioPreference.clearOtherRadioPreferences(a11yTimeoutScreen);
77         mCurrentA11yTimeout = Integer.parseInt(radioPreference.getKey());
78         commit();
79         radioPreference.setChecked(true);
80         logNewA11yTimeoutSelection(radioPreference.getKey());
81         return true;
82     }
83 
84     @Override
getPageId()85     protected int getPageId() {
86         return TvSettingsEnums.SYSTEM_A11Y_TIMEOUT;
87     }
88 
initA11yTimeoutValue()89     private void initA11yTimeoutValue() {
90         final ContentResolver resolver = getContext().getContentResolver();
91         mCurrentA11yTimeout =
92                 Settings.Secure.getInt(
93                         resolver, Settings.Secure.ACCESSIBILITY_INTERACTIVE_UI_TIMEOUT_MS, 0);
94     }
95 
commit()96     private void commit() {
97         final ContentResolver resolver = getContext().getContentResolver();
98         Settings.Secure.putInt(
99                 resolver,
100                 Settings.Secure.ACCESSIBILITY_INTERACTIVE_UI_TIMEOUT_MS,
101                 mCurrentA11yTimeout);
102         Settings.Secure.putInt(
103                 resolver,
104                 Settings.Secure.ACCESSIBILITY_NON_INTERACTIVE_UI_TIMEOUT_MS,
105                 mCurrentA11yTimeout);
106     }
107 
logNewA11yTimeoutSelection(String entryValue)108     private void logNewA11yTimeoutSelection(String entryValue) {
109         final int[] a11yTimeoutOptions = {
110             TvSettingsEnums.SYSTEM_A11Y_TIMEOUT_DEFAULT,
111             TvSettingsEnums.SYSTEM_A11Y_TIMEOUT_TEN_SECONDS,
112             TvSettingsEnums.SYSTEM_A11Y_TIMEOUT_THIRTY_SECONDS,
113             TvSettingsEnums.SYSTEM_A11Y_TIMEOUT_ONE_MINUTE,
114             TvSettingsEnums.SYSTEM_A11Y_TIMEOUT_TWO_MINUTE,
115         };
116         final String[] entryValues =
117                 getContext().getResources().getStringArray(R.array.a11y_timeout_values);
118         for (int i = 0; i < entryValues.length; i++) {
119             if (entryValue.equals(entryValues[i])) {
120                 logEntrySelected(a11yTimeoutOptions[i]);
121                 return;
122             }
123         }
124     }
125 }
126