Heemin Seog | 3565cf0 | 2018-10-25 11:35:48 -0700 | [diff] [blame^] | 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 android.content.Context; |
| 20 | import android.provider.Settings; |
| 21 | |
| 22 | import androidx.preference.Preference; |
| 23 | import androidx.preference.TwoStatePreference; |
| 24 | |
| 25 | import com.android.car.settings.common.FragmentController; |
| 26 | import com.android.car.settings.common.NoSetupPreferenceController; |
| 27 | |
| 28 | /** Business logic for controlling the adaptive brightness setting. */ |
| 29 | public class AdaptiveBrightnessTogglePreferenceController extends |
| 30 | NoSetupPreferenceController implements |
| 31 | Preference.OnPreferenceChangeListener { |
| 32 | |
| 33 | public AdaptiveBrightnessTogglePreferenceController(Context context, String preferenceKey, |
| 34 | FragmentController fragmentController) { |
| 35 | super(context, preferenceKey, fragmentController); |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public void updateState(Preference preference) { |
| 40 | if (!(preference instanceof TwoStatePreference)) { |
| 41 | throw new IllegalArgumentException("Expecting an instance of TwoStatePreference"); |
| 42 | } |
| 43 | ((TwoStatePreference) preference).setChecked(isAdaptiveBrightnessChecked()); |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public int getAvailabilityStatus() { |
| 48 | return supportsAdaptiveBrightness() ? AVAILABLE : UNSUPPORTED_ON_DEVICE; |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 53 | if (!(preference instanceof TwoStatePreference)) { |
| 54 | throw new IllegalArgumentException("Expecting an instance of TwoStatePreference"); |
| 55 | } |
| 56 | boolean enableAdaptiveBrightness = (boolean) newValue; |
| 57 | Settings.System.putInt(mContext.getContentResolver(), |
| 58 | Settings.System.SCREEN_BRIGHTNESS_MODE, |
| 59 | enableAdaptiveBrightness ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC |
| 60 | : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | private boolean isAdaptiveBrightnessChecked() { |
| 65 | int brightnessMode = Settings.System.getInt(mContext.getContentResolver(), |
| 66 | Settings.System.SCREEN_BRIGHTNESS_MODE, |
| 67 | Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); |
| 68 | return brightnessMode != Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL; |
| 69 | } |
| 70 | |
| 71 | private boolean supportsAdaptiveBrightness() { |
| 72 | return mContext.getResources().getBoolean( |
| 73 | com.android.internal.R.bool.config_automatic_brightness_available); |
| 74 | } |
| 75 | } |