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 package com.android.car.settings.quicksettings;
17 
18 import static android.provider.Settings.System.SCREEN_BRIGHTNESS;
19 
20 import static com.android.settingslib.display.BrightnessUtils.GAMMA_SPACE_MAX;
21 import static com.android.settingslib.display.BrightnessUtils.convertGammaToLinear;
22 import static com.android.settingslib.display.BrightnessUtils.convertLinearToGamma;
23 
24 import android.car.userlib.CarUserManagerHelper;
25 import android.content.Context;
26 import android.os.PowerManager;
27 import android.provider.Settings.SettingNotFoundException;
28 import android.provider.Settings.System;
29 import android.widget.SeekBar;
30 
31 import com.android.car.settings.common.Logger;
32 
33 /**
34  * A slider to adjust the brightness of the screen
35  */
36 public class BrightnessTile implements QuickSettingGridAdapter.SeekbarTile {
37     private static final Logger LOG = new Logger(BrightnessTile.class);
38     private CarUserManagerHelper mCarUserManagerHelper;
39     private final Context mContext;
40     private final int mMaximumBacklight;
41     private final int mMinimumBacklight;
42 
BrightnessTile(Context context)43     public BrightnessTile(Context context) {
44         mContext = context;
45         mCarUserManagerHelper = new CarUserManagerHelper(mContext);
46         PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
47         mMaximumBacklight = powerManager.getMaximumScreenBrightnessSetting();
48         mMinimumBacklight = powerManager.getMinimumScreenBrightnessSetting();
49     }
50 
51     @Override
onStartTrackingTouch(SeekBar seekBar)52     public void onStartTrackingTouch(SeekBar seekBar) {
53         // don't care
54     }
55 
56     @Override
onStopTrackingTouch(SeekBar seekBar)57     public void onStopTrackingTouch(SeekBar seekBar) {
58         // don't care
59     }
60 
61     @Override
onProgressChanged(SeekBar seekBar, int gamma, boolean fromUser)62     public void onProgressChanged(SeekBar seekBar, int gamma, boolean fromUser) {
63         int linear = convertGammaToLinear(gamma, mMinimumBacklight, mMaximumBacklight);
64         System.putIntForUser(mContext.getContentResolver(), SCREEN_BRIGHTNESS, linear,
65                              mCarUserManagerHelper.getCurrentForegroundUserId());
66     }
67 
68     @Override
getMax()69     public int getMax() {
70         return GAMMA_SPACE_MAX;
71     }
72 
73     @Override
start()74     public void start() {
75     }
76 
77     @Override
stop()78     public void stop() {
79     }
80 
81     @Override
getCurrent()82     public int getCurrent() {
83         int gamma = GAMMA_SPACE_MAX;
84         try {
85             int linear = System.getIntForUser(mContext.getContentResolver(), SCREEN_BRIGHTNESS,
86                                               mCarUserManagerHelper.getCurrentForegroundUserId());
87             gamma = convertLinearToGamma(linear, mMinimumBacklight, mMaximumBacklight);
88         } catch (SettingNotFoundException e) {
89             LOG.w("Can't find setting for SCREEN_BRIGHTNESS.");
90         }
91         return gamma;
92     }
93 }
94