2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import android.content.Context;
20 import android.os.RemoteException;
21 import android.os.IPowerManager;
22 import android.os.ServiceManager;
23 import android.preference.SeekBarPreference;
24 import android.provider.Settings;
25 import android.provider.Settings.SettingNotFoundException;
26 import android.util.AttributeSet;
27 import android.util.Log;
28 import android.view.View;
29 import android.widget.CheckBox;
30 import android.widget.CompoundButton;
31 import android.widget.SeekBar;
35 public class BrightnessPreference extends SeekBarPreference implements
36 SeekBar.OnSeekBarChangeListener, CheckBox.OnCheckedChangeListener {
38 private SeekBar mSeekBar;
39 private CheckBox mCheckBox;
41 private int mOldBrightness;
42 private int mOldAutomatic;
44 private boolean mAutomaticAvailable;
46 // Backlight range is from 0 - 255. Need to make sure that user
47 // doesn't set the backlight to 0 and get stuck
48 private static final int MINIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_DIM + 10;
49 private static final int MAXIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_ON;
51 public BrightnessPreference(Context context, AttributeSet attrs) {
52 super(context, attrs);
54 mAutomaticAvailable = context.getResources().getBoolean(
55 com.android.internal.R.bool.config_automatic_brightness_available);
57 setDialogLayoutResource(R.layout.preference_dialog_brightness);
58 setDialogIcon(R.drawable.ic_settings_display);
62 protected void onBindDialogView(View view) {
63 super.onBindDialogView(view);
65 mSeekBar = getSeekBar(view);
66 mSeekBar.setMax(MAXIMUM_BACKLIGHT - MINIMUM_BACKLIGHT);
68 mOldBrightness = Settings.System.getInt(getContext().getContentResolver(),
69 Settings.System.SCREEN_BRIGHTNESS);
70 } catch (SettingNotFoundException snfe) {
71 mOldBrightness = MAXIMUM_BACKLIGHT;
73 mSeekBar.setProgress(mOldBrightness - MINIMUM_BACKLIGHT);
75 mCheckBox = (CheckBox)view.findViewById(R.id.automatic_mode);
76 if (mAutomaticAvailable) {
77 mCheckBox.setOnCheckedChangeListener(this);
79 mOldAutomatic = Settings.System.getInt(getContext().getContentResolver(),
80 Settings.System.SCREEN_BRIGHTNESS_MODE);
81 } catch (SettingNotFoundException snfe) {
84 mCheckBox.setChecked(mOldAutomatic != 0);
86 mCheckBox.setVisibility(View.GONE);
88 mSeekBar.setOnSeekBarChangeListener(this);
91 public void onProgressChanged(SeekBar seekBar, int progress,
93 setBrightness(progress + MINIMUM_BACKLIGHT);
96 public void onStartTrackingTouch(SeekBar seekBar) {
100 public void onStopTrackingTouch(SeekBar seekBar) {
104 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
105 setMode(isChecked ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
106 : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
108 setBrightness(mSeekBar.getProgress() + MINIMUM_BACKLIGHT);
113 protected void onDialogClosed(boolean positiveResult) {
114 super.onDialogClosed(positiveResult);
116 if (positiveResult) {
117 Settings.System.putInt(getContext().getContentResolver(),
118 Settings.System.SCREEN_BRIGHTNESS,
119 mSeekBar.getProgress() + MINIMUM_BACKLIGHT);
121 if (mAutomaticAvailable) {
122 setMode(mOldAutomatic);
124 if (!mAutomaticAvailable || mOldAutomatic == 0) {
125 setBrightness(mOldBrightness);
130 private void setBrightness(int brightness) {
132 IPowerManager power = IPowerManager.Stub.asInterface(
133 ServiceManager.getService("power"));
135 power.setBacklightBrightness(brightness);
137 } catch (RemoteException doe) {
142 private void setMode(int mode) {
143 if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
144 mSeekBar.setVisibility(View.GONE);
146 mSeekBar.setVisibility(View.VISIBLE);
148 Settings.System.putInt(getContext().getContentResolver(),
149 Settings.System.SCREEN_BRIGHTNESS_MODE, mode);