Home | History | Annotate | Download | only in display
      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 
     17 package com.android.car.settings.display;
     18 
     19 import static android.provider.Settings.System.SCREEN_BRIGHTNESS;
     20 
     21 import android.content.Context;
     22 import android.provider.Settings;
     23 import android.widget.SeekBar;
     24 import android.widget.SeekBar.OnSeekBarChangeListener;
     25 
     26 import androidx.car.widget.SeekbarListItem;
     27 
     28 import com.android.car.settings.R;
     29 import com.android.car.settings.common.Logger;
     30 
     31 /**
     32  * A LineItem that displays and sets display brightness.
     33  */
     34 public class BrightnessLineItem extends SeekbarListItem {
     35     private static final Logger LOG = new Logger(BrightnessLineItem.class);
     36     private static final int MAX_BRIGHTNESS = 255;
     37     private final Context mContext;
     38 
     39     /**
     40      * Handles brightness change from user
     41      */
     42     private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener =
     43             new OnSeekBarChangeListener() {
     44                 @Override
     45                 public void onStartTrackingTouch(SeekBar seekBar) {
     46                     // no-op
     47                 }
     48 
     49                 @Override
     50                 public void onStopTrackingTouch(SeekBar seekBar) {
     51                     // no-op
     52                 }
     53 
     54                 @Override
     55                 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
     56                     Settings.System.putInt(
     57                             mContext.getContentResolver(), SCREEN_BRIGHTNESS, progress);
     58                 }
     59             };
     60 
     61     public BrightnessLineItem(Context context) {
     62         super(context);
     63         mContext = context;
     64         setMax(MAX_BRIGHTNESS);
     65         setProgress(getSeekbarValue(context));
     66         setOnSeekBarChangeListener(mOnSeekBarChangeListener);
     67         setText(context.getString(R.string.brightness));
     68     }
     69 
     70     private static int getSeekbarValue(Context context) {
     71         int currentBrightness = 0;
     72         try {
     73             currentBrightness = Settings.System.getInt(context.getContentResolver(),
     74                     SCREEN_BRIGHTNESS);
     75         } catch (Settings.SettingNotFoundException e) {
     76             LOG.w("Can't find setting for SCREEN_BRIGHTNESS.");
     77         }
     78         return currentBrightness;
     79     }
     80 }
     81