1 /*
2  * Copyright (C) 2021 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.qc;
18 
19 import static com.android.car.qc.QCItem.QC_ACTION_TOGGLE_STATE;
20 import static com.android.car.qc.QCItem.QC_TYPE_ACTION_SWITCH;
21 import static com.android.car.settings.qc.SettingsQCRegistry.ADAPTIVE_BRIGHTNESS_SWITCH_URI;
22 
23 import android.content.Context;
24 import android.content.Intent;
25 import android.net.Uri;
26 import android.provider.Settings;
27 
28 import androidx.annotation.VisibleForTesting;
29 
30 import com.android.car.qc.QCActionItem;
31 import com.android.car.qc.QCItem;
32 import com.android.car.qc.QCList;
33 import com.android.car.qc.QCRow;
34 import com.android.car.settings.R;
35 
36 /**
37  * QCItem for toggling adaptive brightness.
38  */
39 public class AdaptiveBrightnessSwitch extends SettingsQCItem {
AdaptiveBrightnessSwitch(Context context)40     public AdaptiveBrightnessSwitch(Context context) {
41         super(context);
42     }
43 
44     @Override
getQCItem()45     QCItem getQCItem() {
46         if (!supportsAdaptiveBrightness()) {
47             return null;
48         }
49         QCActionItem actionItem = new QCActionItem.Builder(QC_TYPE_ACTION_SWITCH)
50                 .setChecked(isAdaptiveBrightnessEnabled())
51                 .setAction(getBroadcastIntent())
52                 .build();
53 
54         QCList.Builder listBuilder = new QCList.Builder()
55                 .addRow(new QCRow.Builder()
56                         .setTitle(getContext().getString(R.string.auto_brightness_title))
57                         .addEndItem(actionItem)
58                         .build()
59                 );
60         return listBuilder.build();
61     }
62 
63     @Override
getUri()64     Uri getUri() {
65         return ADAPTIVE_BRIGHTNESS_SWITCH_URI;
66     }
67 
68     @Override
onNotifyChange(Intent intent)69     void onNotifyChange(Intent intent) {
70         boolean newState = intent.getBooleanExtra(QC_ACTION_TOGGLE_STATE,
71                 !isAdaptiveBrightnessEnabled());
72         Settings.System.putInt(getContext().getContentResolver(),
73                 Settings.System.SCREEN_BRIGHTNESS_MODE,
74                 newState ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
75                         : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
76     }
77 
isAdaptiveBrightnessEnabled()78     private boolean isAdaptiveBrightnessEnabled() {
79         int brightnessMode = Settings.System.getInt(getContext().getContentResolver(),
80                 Settings.System.SCREEN_BRIGHTNESS_MODE,
81                 Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
82         return brightnessMode != Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
83     }
84 
85     @VisibleForTesting
supportsAdaptiveBrightness()86     boolean supportsAdaptiveBrightness() {
87         return getContext().getResources().getBoolean(R.bool.config_automatic_brightness_available);
88     }
89 }
90