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 com.android.car.settings.R; 40 import com.android.car.settings.common.FragmentController; 41 import com.android.car.settings.common.Logger; 42 import com.android.car.settings.common.PreferenceController; 43 import com.android.car.settings.common.SeekBarPreference; 44 import com.android.car.settings.enterprise.EnterpriseUtils; 45 46 /** Business logic for changing the brightness of the display. */ 47 public class BrightnessLevelPreferenceController extends PreferenceController<SeekBarPreference> { 48 49 private static final Logger LOG = new Logger(BrightnessLevelPreferenceController.class); 50 private static final Uri BRIGHTNESS_URI = Settings.System.getUriFor( 51 Settings.System.SCREEN_BRIGHTNESS); 52 private final int mMaximumBacklight; 53 private final int mMinimumBacklight; 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 BrightnessLevelPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)63 public BrightnessLevelPreferenceController(Context context, String preferenceKey, 64 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 65 super(context, preferenceKey, fragmentController, uxRestrictions); 66 67 PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 68 mMaximumBacklight = powerManager.getMaximumScreenBrightnessSetting(); 69 mMinimumBacklight = powerManager.getMinimumScreenBrightnessSetting(); 70 } 71 72 @Override getPreferenceType()73 protected Class<SeekBarPreference> getPreferenceType() { 74 return SeekBarPreference.class; 75 } 76 77 @Override onCreateInternal()78 protected void onCreateInternal() { 79 super.onCreateInternal(); 80 setClickableWhileDisabled(getPreference(), /* clickable= */ true, p -> { 81 if (hasUserRestrictionByDpm(getContext(), DISALLOW_CONFIG_BRIGHTNESS)) { 82 showActionDisabledByAdminDialog(); 83 } else { 84 Toast.makeText(getContext(), 85 getContext().getString(R.string.action_unavailable), 86 Toast.LENGTH_LONG).show(); 87 } 88 }); 89 } 90 91 @Override onStartInternal()92 protected void onStartInternal() { 93 super.onStartInternal(); 94 getContext().getContentResolver().registerContentObserver(BRIGHTNESS_URI, 95 /* notifyForDescendants= */ false, mBrightnessObserver); 96 } 97 98 @Override onStopInternal()99 protected void onStopInternal() { 100 super.onStopInternal(); 101 getContext().getContentResolver().unregisterContentObserver(mBrightnessObserver); 102 } 103 104 @Override updateState(SeekBarPreference preference)105 protected void updateState(SeekBarPreference preference) { 106 preference.setMax(GAMMA_SPACE_MAX); 107 preference.setValue(getSeekbarValue()); 108 preference.setContinuousUpdate(true); 109 } 110 111 @Override handlePreferenceChanged(SeekBarPreference preference, Object newValue)112 protected boolean handlePreferenceChanged(SeekBarPreference preference, Object newValue) { 113 int gamma = (Integer) newValue; 114 int linear = convertGammaToLinear(gamma, mMinimumBacklight, mMaximumBacklight); 115 Settings.System.putIntForUser(getContext().getContentResolver(), 116 Settings.System.SCREEN_BRIGHTNESS, linear, UserHandle.myUserId()); 117 return true; 118 } 119 getSeekbarValue()120 private int getSeekbarValue() { 121 int gamma = GAMMA_SPACE_MAX; 122 try { 123 int linear = Settings.System.getIntForUser(getContext().getContentResolver(), 124 Settings.System.SCREEN_BRIGHTNESS, UserHandle.myUserId()); 125 gamma = convertLinearToGamma(linear, mMinimumBacklight, mMaximumBacklight); 126 } catch (Settings.SettingNotFoundException e) { 127 LOG.w("Can't find setting for SCREEN_BRIGHTNESS."); 128 } 129 return gamma; 130 } 131 132 @Override getAvailabilityStatus()133 public int getAvailabilityStatus() { 134 if (hasUserRestrictionByUm(getContext(), DISALLOW_CONFIG_BRIGHTNESS) 135 || hasUserRestrictionByDpm(getContext(), DISALLOW_CONFIG_BRIGHTNESS)) { 136 return AVAILABLE_FOR_VIEWING; 137 } 138 return AVAILABLE; 139 } 140 showActionDisabledByAdminDialog()141 private void showActionDisabledByAdminDialog() { 142 getFragmentController().showDialog( 143 EnterpriseUtils.getActionDisabledByAdminDialog(getContext(), 144 DISALLOW_CONFIG_BRIGHTNESS), 145 DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG); 146 } 147 } 148