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.ContentResolver; 20 import android.content.Context; 21 import android.database.ContentObserver; 22 import android.os.Handler; 23 import android.os.Looper; 24 import android.provider.Settings; 25 26 import androidx.annotation.VisibleForTesting; 27 import androidx.preference.Preference; 28 import androidx.preference.PreferenceScreen; 29 import androidx.preference.TwoStatePreference; 30 31 import com.android.settings.R; 32 import com.android.settings.core.BasePreferenceController; 33 import com.android.settingslib.core.lifecycle.LifecycleObserver; 34 import com.android.settingslib.core.lifecycle.events.OnPause; 35 import com.android.settingslib.core.lifecycle.events.OnResume; 36 37 /** Preference controller that controls the fade switch button in accessibility button page. */ 38 public class FloatingMenuFadePreferenceController extends BasePreferenceController implements 39 Preference.OnPreferenceChangeListener, LifecycleObserver, OnResume, OnPause { 40 41 private static final int OFF = 0; 42 private static final int ON = 1; 43 44 private final ContentResolver mContentResolver; 45 @VisibleForTesting 46 final ContentObserver mContentObserver; 47 48 @VisibleForTesting 49 TwoStatePreference mPreference; 50 FloatingMenuFadePreferenceController(Context context, String preferenceKey)51 public FloatingMenuFadePreferenceController(Context context, String preferenceKey) { 52 super(context, preferenceKey); 53 mContentResolver = context.getContentResolver(); 54 mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) { 55 @Override 56 public void onChange(boolean selfChange) { 57 updateAvailabilityStatus(); 58 } 59 }; 60 } 61 62 @Override getAvailabilityStatus()63 public int getAvailabilityStatus() { 64 return AccessibilityUtil.isFloatingMenuEnabled(mContext) 65 ? AVAILABLE : DISABLED_DEPENDENT_SETTING; 66 } 67 68 @Override getSummary()69 public CharSequence getSummary() { 70 int rId = R.string.accessibility_button_fade_summary; 71 if (mPreference != null && !mPreference.isEnabled()) { 72 rId = R.string.accessibility_button_disabled_button_mode_summary; 73 } 74 return mContext.getString(rId); 75 } 76 77 @Override displayPreference(PreferenceScreen screen)78 public void displayPreference(PreferenceScreen screen) { 79 super.displayPreference(screen); 80 81 mPreference = screen.findPreference(getPreferenceKey()); 82 } 83 84 @Override onPreferenceChange(Preference preference, Object newValue)85 public boolean onPreferenceChange(Preference preference, Object newValue) { 86 final boolean isEnabled = (boolean) newValue; 87 putFloatingMenuFadeValue(isEnabled); 88 return true; 89 } 90 91 @Override updateState(Preference preference)92 public void updateState(Preference preference) { 93 super.updateState(preference); 94 final TwoStatePreference switchPreference = (TwoStatePreference) preference; 95 96 switchPreference.setChecked(getFloatingMenuFadeValue() == ON); 97 } 98 99 @Override onResume()100 public void onResume() { 101 mContentResolver.registerContentObserver( 102 Settings.Secure.getUriFor( 103 Settings.Secure.ACCESSIBILITY_BUTTON_MODE), 104 /* notifyForDescendants= */ false, mContentObserver); 105 } 106 107 @Override onPause()108 public void onPause() { 109 mContentResolver.unregisterContentObserver(mContentObserver); 110 } 111 updateAvailabilityStatus()112 private void updateAvailabilityStatus() { 113 mPreference.setEnabled(AccessibilityUtil.isFloatingMenuEnabled(mContext)); 114 refreshSummary(mPreference); 115 } 116 getFloatingMenuFadeValue()117 private int getFloatingMenuFadeValue() { 118 return Settings.Secure.getInt(mContentResolver, 119 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_FADE_ENABLED, ON); 120 } 121 putFloatingMenuFadeValue(boolean isEnabled)122 private void putFloatingMenuFadeValue(boolean isEnabled) { 123 Settings.Secure.putInt(mContentResolver, 124 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_FADE_ENABLED, 125 isEnabled ? ON : OFF); 126 } 127 } 128