1 /*
2  * Copyright (C) 2024 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.settings.display;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.os.UserHandle;
22 import android.provider.Settings;
23 import android.util.Log;
24 
25 import androidx.annotation.NonNull;
26 
27 import com.android.server.display.feature.flags.Flags;
28 import com.android.settings.R;
29 import com.android.settings.core.TogglePreferenceController;
30 
31 /**
32  * Controller for the settings toggle which allows screen brightness to go even dimmer than usual.
33  *
34  */
35 public class EvenDimmerPreferenceController extends TogglePreferenceController {
36 
37     private static final String TAG = "EvenDimmerPreferenceController";
38 
39     private final Resources mResources;
40 
EvenDimmerPreferenceController(@onNull Context context, @NonNull String key)41     public EvenDimmerPreferenceController(@NonNull Context context, @NonNull String key) {
42         super(context, key);
43         mResources = context.getResources();
44     }
45 
46     @Override
getAvailabilityStatus()47     public int getAvailabilityStatus() {
48         // enable based on flag and config.xml
49         final boolean enabledInConfig = mResources.getBoolean(
50                 com.android.internal.R.bool.config_evenDimmerEnabled);
51 
52         if (Flags.evenDimmer() && enabledInConfig) {
53             return AVAILABLE;
54         } else {
55             return UNSUPPORTED_ON_DEVICE;
56         }
57     }
58 
59     @Override
isChecked()60     public boolean isChecked() {
61         return getEvenDimmerActivated();
62     }
63 
64     @Override
setChecked(boolean isChecked)65     public boolean setChecked(boolean isChecked) {
66         final float enabled = getAvailabilityStatus() == AVAILABLE && isChecked ? 1 : 0;
67         Log.i(TAG, "setChecked to : " + enabled);
68 
69         return Settings.Secure.putFloat(
70                 mContext.getContentResolver(), Settings.Secure.EVEN_DIMMER_ACTIVATED, enabled);
71     }
72 
73     @Override
getSliceHighlightMenuRes()74     public int getSliceHighlightMenuRes() {
75         return R.string.menu_key_display;
76     }
77 
getEvenDimmerActivated()78     private boolean getEvenDimmerActivated() {
79         return Settings.Secure.getFloatForUser(mContext.getContentResolver(),
80                 Settings.Secure.EVEN_DIMMER_ACTIVATED,
81                 /* def= */ 1.0f, UserHandle.USER_CURRENT) == 1.0f;
82     }
83 }
84