1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 package com.android.car.developeroptions.display; 15 16 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE; 17 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; 18 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL; 19 20 import android.content.Context; 21 import android.provider.Settings; 22 import android.text.TextUtils; 23 24 import com.android.car.developeroptions.R; 25 import com.android.car.developeroptions.core.TogglePreferenceController; 26 27 28 public class AutoBrightnessPreferenceController extends TogglePreferenceController { 29 30 private final String SYSTEM_KEY = SCREEN_BRIGHTNESS_MODE; 31 private final int DEFAULT_VALUE = SCREEN_BRIGHTNESS_MODE_MANUAL; 32 AutoBrightnessPreferenceController(Context context, String key)33 public AutoBrightnessPreferenceController(Context context, String key) { 34 super(context, key); 35 } 36 37 @Override isChecked()38 public boolean isChecked() { 39 return Settings.System.getInt(mContext.getContentResolver(), 40 SYSTEM_KEY, DEFAULT_VALUE) != DEFAULT_VALUE; 41 } 42 43 @Override setChecked(boolean isChecked)44 public boolean setChecked(boolean isChecked) { 45 Settings.System.putInt(mContext.getContentResolver(), SYSTEM_KEY, 46 isChecked ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : DEFAULT_VALUE); 47 return true; 48 } 49 50 @Override 51 @AvailabilityStatus getAvailabilityStatus()52 public int getAvailabilityStatus() { 53 return mContext.getResources().getBoolean( 54 com.android.internal.R.bool.config_automatic_brightness_available) 55 ? AVAILABLE 56 : UNSUPPORTED_ON_DEVICE; 57 } 58 59 @Override isSliceable()60 public boolean isSliceable() { 61 return TextUtils.equals(getPreferenceKey(), "auto_brightness"); 62 } 63 64 @Override getSummary()65 public CharSequence getSummary() { 66 return mContext.getText(isChecked() 67 ? R.string.auto_brightness_summary_on 68 : R.string.auto_brightness_summary_off); 69 } 70 } 71