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.app.ActivityManager;
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 final Context mContext;
39     private final int mMaximumBacklight;
40     private final int mMinimumBacklight;
41 
BrightnessTile(Context context)42     public BrightnessTile(Context context) {
43         mContext = context;
44         PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
45         mMaximumBacklight = powerManager.getMaximumScreenBrightnessSetting();
46         mMinimumBacklight = powerManager.getMinimumScreenBrightnessSetting();
47     }
48 
49     @Override
onStartTrackingTouch(SeekBar seekBar)50     public void onStartTrackingTouch(SeekBar seekBar) {
51         // don't care
52     }
53 
54     @Override
onStopTrackingTouch(SeekBar seekBar)55     public void onStopTrackingTouch(SeekBar seekBar) {
56         // don't care
57     }
58 
59     @Override
onProgressChanged(SeekBar seekBar, int gamma, boolean fromUser)60     public void onProgressChanged(SeekBar seekBar, int gamma, boolean fromUser) {
61         int linear = convertGammaToLinear(gamma, mMinimumBacklight, mMaximumBacklight);
62         System.putIntForUser(mContext.getContentResolver(), SCREEN_BRIGHTNESS, linear,
63                 ActivityManager.getCurrentUser());
64     }
65 
66     @Override
getMax()67     public int getMax() {
68         return GAMMA_SPACE_MAX;
69     }
70 
71     @Override
start()72     public void start() {
73     }
74 
75     @Override
stop()76     public void stop() {
77     }
78 
79     @Override
getCurrent()80     public int getCurrent() {
81         int gamma = GAMMA_SPACE_MAX;
82         try {
83             int linear = System.getIntForUser(mContext.getContentResolver(), SCREEN_BRIGHTNESS,
84                     ActivityManager.getCurrentUser());
85             gamma = convertLinearToGamma(linear, mMinimumBacklight, mMaximumBacklight);
86         } catch (SettingNotFoundException e) {
87             LOG.w("Can't find setting for SCREEN_BRIGHTNESS.");
88         }
89         return gamma;
90     }
91 }
92