1 /* 2 * Copyright (C) 2018 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.car.settings.display; 18 19 import static android.os.UserManager.DISALLOW_CONFIG_BRIGHTNESS; 20 21 import static com.android.car.settings.enterprise.ActionDisabledByAdminDialogFragment.DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG; 22 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByDpm; 23 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByUm; 24 import static com.android.settingslib.display.BrightnessUtils.GAMMA_SPACE_MAX; 25 import static com.android.settingslib.display.BrightnessUtils.convertGammaToLinear; 26 import static com.android.settingslib.display.BrightnessUtils.convertLinearToGamma; 27 28 import android.car.drivingstate.CarUxRestrictions; 29 import android.content.Context; 30 import android.database.ContentObserver; 31 import android.net.Uri; 32 import android.os.Handler; 33 import android.os.Looper; 34 import android.os.PowerManager; 35 import android.os.UserHandle; 36 import android.provider.Settings; 37 import android.widget.Toast; 38 39 import androidx.annotation.VisibleForTesting; 40 41 import com.android.car.settings.R; 42 import com.android.car.settings.common.FragmentController; 43 import com.android.car.settings.common.Logger; 44 import com.android.car.settings.common.PreferenceController; 45 import com.android.car.settings.common.SeekBarPreference; 46 import com.android.car.settings.enterprise.EnterpriseUtils; 47 48 /** Business logic for changing the brightness of the display. */ 49 public class BrightnessLevelPreferenceController extends PreferenceController<SeekBarPreference> { 50 51 private static final Logger LOG = new Logger(BrightnessLevelPreferenceController.class); 52 private static final Uri BRIGHTNESS_URI = Settings.System.getUriFor( 53 Settings.System.SCREEN_BRIGHTNESS); 54 private final Handler mHandler = new Handler(Looper.getMainLooper()); 55 56 private final ContentObserver mBrightnessObserver = new ContentObserver(mHandler) { 57 @Override 58 public void onChange(boolean selfChange) { 59 refreshUi(); 60 } 61 }; 62 63 @VisibleForTesting 64 final int mMaximumBacklight; 65 @VisibleForTesting 66 final int mMinimumBacklight; 67 BrightnessLevelPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)68 public BrightnessLevelPreferenceController(Context context, String preferenceKey, 69 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 70 super(context, preferenceKey, fragmentController, uxRestrictions); 71 72 PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 73 mMaximumBacklight = powerManager.getMaximumScreenBrightnessSetting(); 74 mMinimumBacklight = powerManager.getMinimumScreenBrightnessSetting(); 75 } 76 77 @Override getPreferenceType()78 protected Class<SeekBarPreference> getPreferenceType() { 79 return SeekBarPreference.class; 80 } 81 82 @Override onCreateInternal()83 protected void onCreateInternal() { 84 super.onCreateInternal(); 85 setClickableWhileDisabled(getPreference(), /* clickable= */ true, p -> { 86 if (hasUserRestrictionByDpm(getContext(), DISALLOW_CONFIG_BRIGHTNESS)) { 87 showActionDisabledByAdminDialog(); 88 } else { 89 Toast.makeText(getContext(), 90 getContext().getString(R.string.action_unavailable), 91 Toast.LENGTH_LONG).show(); 92 } 93 }); 94 } 95 96 @Override onStartInternal()97 protected void onStartInternal() { 98 super.onStartInternal(); 99 getContext().getContentResolver().registerContentObserver(BRIGHTNESS_URI, 100 /* notifyForDescendants= */ false, mBrightnessObserver); 101 } 102 103 @Override onStopInternal()104 protected void onStopInternal() { 105 super.onStopInternal(); 106 getContext().getContentResolver().unregisterContentObserver(mBrightnessObserver); 107 } 108 109 @Override updateState(SeekBarPreference preference)110 protected void updateState(SeekBarPreference preference) { 111 preference.setMax(GAMMA_SPACE_MAX); 112 preference.setValue(getSeekbarValue()); 113 preference.setContinuousUpdate(true); 114 } 115 116 @Override handlePreferenceChanged(SeekBarPreference preference, Object newValue)117 protected boolean handlePreferenceChanged(SeekBarPreference preference, Object newValue) { 118 int gamma = (Integer) newValue; 119 int linear = convertGammaToLinear(gamma, mMinimumBacklight, mMaximumBacklight); 120 saveScreenBrightnessLinearValue(linear); 121 return true; 122 } 123 getSeekbarValue()124 private int getSeekbarValue() { 125 int gamma = GAMMA_SPACE_MAX; 126 try { 127 int linear = getScreenBrightnessLinearValue(); 128 gamma = convertLinearToGamma(linear, mMinimumBacklight, mMaximumBacklight); 129 } catch (Settings.SettingNotFoundException e) { 130 LOG.w("Can't find setting for SCREEN_BRIGHTNESS."); 131 } 132 return gamma; 133 } 134 135 @Override getDefaultAvailabilityStatus()136 public int getDefaultAvailabilityStatus() { 137 if (hasUserRestrictionByUm(getContext(), DISALLOW_CONFIG_BRIGHTNESS) 138 || hasUserRestrictionByDpm(getContext(), DISALLOW_CONFIG_BRIGHTNESS)) { 139 return AVAILABLE_FOR_VIEWING; 140 } 141 return AVAILABLE; 142 } 143 showActionDisabledByAdminDialog()144 private void showActionDisabledByAdminDialog() { 145 getFragmentController().showDialog( 146 EnterpriseUtils.getActionDisabledByAdminDialog(getContext(), 147 DISALLOW_CONFIG_BRIGHTNESS), 148 DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG); 149 } 150 151 @VisibleForTesting getScreenBrightnessLinearValue()152 int getScreenBrightnessLinearValue() throws Settings.SettingNotFoundException { 153 return Settings.System.getIntForUser(getContext().getContentResolver(), 154 Settings.System.SCREEN_BRIGHTNESS, UserHandle.myUserId()); 155 } 156 157 @VisibleForTesting saveScreenBrightnessLinearValue(int linear)158 void saveScreenBrightnessLinearValue(int linear) { 159 Settings.System.putIntForUser(getContext().getContentResolver(), 160 Settings.System.SCREEN_BRIGHTNESS, linear, UserHandle.myUserId()); 161 } 162 } 163