1 /* 2 * Copyright (C) 2021 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.accessibility; 18 19 import android.content.Context; 20 import android.provider.Settings; 21 import android.util.ArrayMap; 22 23 import androidx.preference.ListPreference; 24 import androidx.preference.Preference; 25 26 import com.android.settings.R; 27 import com.android.settings.core.BasePreferenceController; 28 29 import com.google.common.primitives.Ints; 30 31 /** Preference controller that controls the preferred location in accessibility button page. */ 32 public class AccessibilityButtonLocationPreferenceController extends BasePreferenceController 33 implements Preference.OnPreferenceChangeListener { 34 35 private final ArrayMap<String, String> mValueTitleMap = new ArrayMap<>(); 36 private int mDefaultLocation; 37 AccessibilityButtonLocationPreferenceController(Context context, String preferenceKey)38 public AccessibilityButtonLocationPreferenceController(Context context, String preferenceKey) { 39 super(context, preferenceKey); 40 initValueTitleMap(); 41 } 42 43 @Override getAvailabilityStatus()44 public int getAvailabilityStatus() { 45 return AccessibilityUtil.isGestureNavigateEnabled(mContext) 46 ? CONDITIONALLY_UNAVAILABLE : AVAILABLE; 47 } 48 49 @Override onPreferenceChange(Preference preference, Object newValue)50 public boolean onPreferenceChange(Preference preference, Object newValue) { 51 final ListPreference listPreference = (ListPreference) preference; 52 final Integer value = Ints.tryParse((String) newValue); 53 if (value != null) { 54 Settings.Secure.putInt(mContext.getContentResolver(), 55 Settings.Secure.ACCESSIBILITY_BUTTON_MODE, value); 56 updateState(listPreference); 57 } 58 return true; 59 } 60 61 @Override updateState(Preference preference)62 public void updateState(Preference preference) { 63 super.updateState(preference); 64 final ListPreference listPreference = (ListPreference) preference; 65 66 listPreference.setValue(getCurrentAccessibilityButtonMode()); 67 } 68 getCurrentAccessibilityButtonMode()69 private String getCurrentAccessibilityButtonMode() { 70 final int mode = Settings.Secure.getInt(mContext.getContentResolver(), 71 Settings.Secure.ACCESSIBILITY_BUTTON_MODE, mDefaultLocation); 72 return String.valueOf(mode); 73 } 74 initValueTitleMap()75 private void initValueTitleMap() { 76 if (mValueTitleMap.size() == 0) { 77 final String[] values = mContext.getResources().getStringArray( 78 R.array.accessibility_button_location_selector_values); 79 final String[] titles = mContext.getResources().getStringArray( 80 R.array.accessibility_button_location_selector_titles); 81 final int mapSize = values.length; 82 83 mDefaultLocation = Integer.parseInt(values[0]); 84 for (int i = 0; i < mapSize; i++) { 85 mValueTitleMap.put(values[i], titles[i]); 86 } 87 } 88 } 89 } 90