• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     private boolean isTwoPanel;
41 
42     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)43     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
44         setPreferencesFromResource(R.xml.accessibility_timeout, null);
45         PreferenceScreen a11yTimeoutScreen = getPreferenceManager().getPreferenceScreen();
46         final Context themedContext = getPreferenceManager().getContext();
47         isTwoPanel = FlavorUtils.isTwoPanel(getContext());
48         final String[] entries =
49                 getContext().getResources().getStringArray(R.array.a11y_timeout_entries);
50         final String[] entryValues =
51                 getContext().getResources().getStringArray(R.array.a11y_timeout_values);
52         initA11yTimeoutValue();
53 
54         for (int i = 0; i < entries.length; i++) {
55             final RadioPreference radioPreference = new RadioPreference(themedContext);
56             radioPreference.setTitle(entries[i]);
57             radioPreference.setKey(entryValues[i]);
58             radioPreference.setOnPreferenceChangeListener(this);
59             radioPreference.setPersistent(false);
60             if (mCurrentA11yTimeout == Integer.parseInt(entryValues[i])) {
61                 radioPreference.setChecked(true);
62             }
63             if (isTwoPanel){
64                 if(i==0) {
65                     // Setting information fragment only for default value
66                     radioPreference.setFragment(AccessibilityTimeoutInfoFragment.class.getName());
67                 }else{
68                     radioPreference.setFragment(null);
69                 }
70             }
71             a11yTimeoutScreen.addPreference(radioPreference);
72         }
73     }
74 
75     @Override
onPreferenceChange(Preference preference, Object newValue)76     public boolean onPreferenceChange(Preference preference, Object newValue) {
77         RadioPreference radioPreference = (RadioPreference) preference;
78         if (radioPreference.isChecked()) {
79             return false;
80         }
81         PreferenceScreen a11yTimeoutScreen = getPreferenceManager().getPreferenceScreen();
82         radioPreference.clearOtherRadioPreferences(a11yTimeoutScreen);
83         mCurrentA11yTimeout = Integer.parseInt(radioPreference.getKey());
84         final String[] entryValues =
85                 getContext().getResources().getStringArray(R.array.a11y_timeout_values);
86         commit();
87         radioPreference.setChecked(true);
88         if(isTwoPanel){
89             if (mCurrentA11yTimeout==Integer.parseInt(entryValues[0])) {
90                 // Setting information fragment only for default value
91                 radioPreference.setFragment(AccessibilityTimeoutInfoFragment.class.getName());
92             }else{
93                 radioPreference.setFragment(null);
94             }
95         }
96         logNewA11yTimeoutSelection(radioPreference.getKey());
97         return true;
98     }
99 
100     @Override
getPageId()101     protected int getPageId() {
102         return TvSettingsEnums.SYSTEM_A11Y_TIMEOUT;
103     }
104 
initA11yTimeoutValue()105     private void initA11yTimeoutValue() {
106         final ContentResolver resolver = getContext().getContentResolver();
107         mCurrentA11yTimeout =
108                 Settings.Secure.getInt(
109                         resolver, Settings.Secure.ACCESSIBILITY_INTERACTIVE_UI_TIMEOUT_MS, 0);
110     }
111 
commit()112     private void commit() {
113         final ContentResolver resolver = getContext().getContentResolver();
114         Settings.Secure.putInt(
115                 resolver,
116                 Settings.Secure.ACCESSIBILITY_INTERACTIVE_UI_TIMEOUT_MS,
117                 mCurrentA11yTimeout);
118         Settings.Secure.putInt(
119                 resolver,
120                 Settings.Secure.ACCESSIBILITY_NON_INTERACTIVE_UI_TIMEOUT_MS,
121                 mCurrentA11yTimeout);
122     }
123 
logNewA11yTimeoutSelection(String entryValue)124     private void logNewA11yTimeoutSelection(String entryValue) {
125         final int[] a11yTimeoutOptions = {
126             TvSettingsEnums.SYSTEM_A11Y_TIMEOUT_DEFAULT,
127             TvSettingsEnums.SYSTEM_A11Y_TIMEOUT_TEN_SECONDS,
128             TvSettingsEnums.SYSTEM_A11Y_TIMEOUT_THIRTY_SECONDS,
129             TvSettingsEnums.SYSTEM_A11Y_TIMEOUT_ONE_MINUTE,
130             TvSettingsEnums.SYSTEM_A11Y_TIMEOUT_TWO_MINUTE,
131         };
132         final String[] entryValues =
133                 getContext().getResources().getStringArray(R.array.a11y_timeout_values);
134         for (int i = 0; i < entryValues.length; i++) {
135             if (entryValue.equals(entryValues[i])) {
136                 logEntrySelected(a11yTimeoutOptions[i]);
137                 return;
138             }
139         }
140     }
141 }
142