1 /* 2 * Copyright 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.content.Context; 20 import android.os.SystemProperties; 21 22 import androidx.annotation.VisibleForTesting; 23 import androidx.preference.Preference; 24 import androidx.preference.SwitchPreference; 25 26 import com.android.settings.core.PreferenceControllerMixin; 27 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 28 import com.android.settingslib.development.SystemPropPoker; 29 30 /** 31 * Controller that toggles window blurs on SurfaceFlinger on devices that support it. 32 */ 33 public final class EnableBlursPreferenceController extends DeveloperOptionsPreferenceController 34 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { 35 36 @VisibleForTesting 37 static final String DISABLE_BLURS_SYSPROP = "persist.sys.sf.disable_blurs"; 38 private static final String ENABLE_BLURS_ON_WINDOWS = "enable_blurs_on_windows"; 39 private final boolean mBlurSupported; 40 EnableBlursPreferenceController(Context context)41 public EnableBlursPreferenceController(Context context) { 42 this(context, SystemProperties 43 .getBoolean("ro.surface_flinger.supports_background_blur", false)); 44 } 45 46 @VisibleForTesting EnableBlursPreferenceController(Context context, boolean blurSupported)47 public EnableBlursPreferenceController(Context context, boolean blurSupported) { 48 super(context); 49 mBlurSupported = blurSupported; 50 } 51 52 @Override getPreferenceKey()53 public String getPreferenceKey() { 54 return ENABLE_BLURS_ON_WINDOWS; 55 } 56 57 @Override onPreferenceChange(Preference preference, Object newValue)58 public boolean onPreferenceChange(Preference preference, Object newValue) { 59 final boolean isDisabled = !(Boolean) newValue; 60 SystemProperties.set(DISABLE_BLURS_SYSPROP, isDisabled ? "1" : "0"); 61 SystemPropPoker.getInstance().poke(); 62 return true; 63 } 64 65 @Override isAvailable()66 public boolean isAvailable() { 67 return mBlurSupported; 68 } 69 70 @Override updateState(Preference preference)71 public void updateState(Preference preference) { 72 boolean isEnabled = !SystemProperties.getBoolean( 73 DISABLE_BLURS_SYSPROP, false /* default */); 74 ((SwitchPreference) mPreference).setChecked(isEnabled); 75 } 76 77 @Override onDeveloperOptionsSwitchDisabled()78 protected void onDeveloperOptionsSwitchDisabled() { 79 super.onDeveloperOptionsSwitchDisabled(); 80 SystemProperties.set(DISABLE_BLURS_SYSPROP, null); 81 updateState(null); 82 } 83 } 84