1 /* 2 * Copyright (C) 2017 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 package com.android.car.settings.display; 17 18 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE; 19 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; 20 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL; 21 22 import android.content.Context; 23 import android.os.Bundle; 24 import android.provider.Settings; 25 26 import androidx.car.widget.ListItem; 27 import androidx.car.widget.ListItemProvider; 28 import androidx.car.widget.ListItemProvider.ListProvider; 29 import androidx.car.widget.TextListItem; 30 31 import com.android.car.settings.R; 32 import com.android.car.settings.common.ListItemSettingsFragment; 33 34 import java.util.ArrayList; 35 import java.util.List; 36 37 /** 38 * Activity to host Display related preferences. 39 */ 40 public class DisplaySettingsFragment extends ListItemSettingsFragment { 41 42 /** 43 * Gets a new instance of this class. 44 */ 45 public static DisplaySettingsFragment newInstance() { 46 DisplaySettingsFragment displaySettingsFragment = new DisplaySettingsFragment(); 47 Bundle bundle = ListItemSettingsFragment.getBundle(); 48 bundle.putInt(EXTRA_TITLE_ID, R.string.display_settings); 49 bundle.putInt(EXTRA_ACTION_BAR_LAYOUT, R.layout.action_bar); 50 displaySettingsFragment.setArguments(bundle); 51 return displaySettingsFragment; 52 } 53 54 @Override 55 public ListItemProvider getItemProvider() { 56 return new ListProvider(getLineItems()); 57 } 58 59 private List<ListItem> getLineItems() { 60 List<ListItem> lineItems = new ArrayList<>(); 61 Context context = getContext(); 62 if (supportsAdaptiveBrightness()) { 63 TextListItem adaptiveBrightnessItem = new TextListItem(context); 64 adaptiveBrightnessItem.setTitle(context.getString(R.string.auto_brightness_title)); 65 adaptiveBrightnessItem.setBody( 66 context.getString(R.string.auto_brightness_summary)); 67 adaptiveBrightnessItem.setSwitch( 68 isAdaptiveBrightnessChecked(), 69 /* showDivider= */false, 70 (button, isChecked) -> 71 Settings.System.putInt(context.getContentResolver(), 72 SCREEN_BRIGHTNESS_MODE, 73 isChecked ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC 74 : SCREEN_BRIGHTNESS_MODE_MANUAL)); 75 lineItems.add(adaptiveBrightnessItem); 76 } 77 lineItems.add(new BrightnessLineItem(context)); 78 return lineItems; 79 } 80 81 private boolean isAdaptiveBrightnessChecked() { 82 int brightnessMode = Settings.System.getInt(getContext().getContentResolver(), 83 SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL); 84 return brightnessMode != SCREEN_BRIGHTNESS_MODE_MANUAL; 85 } 86 87 private boolean supportsAdaptiveBrightness() { 88 return getContext().getResources().getBoolean( 89 com.android.internal.R.bool.config_automatic_brightness_available); 90 } 91 } 92