BrightnessPreference.java revision 53dcdeeb85e523e0b752d802d3413f5d4001d575
1/*
2 * Copyright (C) 2008 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
17package com.android.settings;
18
19import android.content.Context;
20import android.os.RemoteException;
21import android.os.IPowerManager;
22import android.os.ServiceManager;
23import android.preference.SeekBarPreference;
24import android.provider.Settings;
25import android.provider.Settings.SettingNotFoundException;
26import android.util.AttributeSet;
27import android.util.Log;
28import android.view.View;
29import android.widget.CheckBox;
30import android.widget.CompoundButton;
31import android.widget.SeekBar;
32
33import java.util.Map;
34
35public class BrightnessPreference extends SeekBarPreference implements
36        SeekBar.OnSeekBarChangeListener, CheckBox.OnCheckedChangeListener {
37
38    private SeekBar mSeekBar;
39    private CheckBox mCheckBox;
40
41    private int mOldBrightness;
42    private int mOldAutomatic;
43
44    private boolean mAutomaticAvailable;
45
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;
50
51    public BrightnessPreference(Context context, AttributeSet attrs) {
52        super(context, attrs);
53
54        mAutomaticAvailable = context.getResources().getBoolean(
55                com.android.internal.R.bool.config_automatic_brightness_available);
56
57        setDialogLayoutResource(R.layout.preference_dialog_brightness);
58    }
59
60    @Override
61    protected void onBindDialogView(View view) {
62        super.onBindDialogView(view);
63
64        mSeekBar = getSeekBar(view);
65        mSeekBar.setOnSeekBarChangeListener(this);
66        mSeekBar.setMax(MAXIMUM_BACKLIGHT - MINIMUM_BACKLIGHT);
67        try {
68            mOldBrightness = Settings.System.getInt(getContext().getContentResolver(),
69                Settings.System.SCREEN_BRIGHTNESS);
70        } catch (SettingNotFoundException snfe) {
71            mOldBrightness = MAXIMUM_BACKLIGHT;
72        }
73        mSeekBar.setProgress(mOldBrightness - MINIMUM_BACKLIGHT);
74
75        mCheckBox = (CheckBox)view.findViewById(R.id.automatic_mode);
76        if (mAutomaticAvailable) {
77            mCheckBox.setOnCheckedChangeListener(this);
78            try {
79                mOldAutomatic = Settings.System.getInt(getContext().getContentResolver(),
80                        Settings.System.SCREEN_BRIGHTNESS_MODE);
81            } catch (SettingNotFoundException snfe) {
82                mOldAutomatic = 0;
83            }
84            mCheckBox.setChecked(mOldAutomatic != 0);
85        } else {
86            mCheckBox.setVisibility(View.GONE);
87        }
88    }
89
90    public void onProgressChanged(SeekBar seekBar, int progress,
91            boolean fromTouch) {
92        setBrightness(progress + MINIMUM_BACKLIGHT);
93    }
94
95    public void onStartTrackingTouch(SeekBar seekBar) {
96        // NA
97    }
98
99    public void onStopTrackingTouch(SeekBar seekBar) {
100        // NA
101    }
102
103    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
104        setMode(isChecked ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
105                : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
106        if (!isChecked) {
107            setBrightness(mSeekBar.getProgress() + MINIMUM_BACKLIGHT);
108        }
109    }
110
111    @Override
112    protected void onDialogClosed(boolean positiveResult) {
113        super.onDialogClosed(positiveResult);
114
115        if (positiveResult) {
116            Settings.System.putInt(getContext().getContentResolver(),
117                    Settings.System.SCREEN_BRIGHTNESS,
118                    mSeekBar.getProgress() + MINIMUM_BACKLIGHT);
119        } else {
120            if (mAutomaticAvailable) {
121                setMode(mOldAutomatic);
122            }
123            if (!mAutomaticAvailable || mOldAutomatic == 0) {
124                setBrightness(mOldBrightness);
125            }
126        }
127    }
128
129    private void setBrightness(int brightness) {
130        try {
131            IPowerManager power = IPowerManager.Stub.asInterface(
132                    ServiceManager.getService("power"));
133            if (power != null) {
134                power.setBacklightBrightness(brightness);
135            }
136        } catch (RemoteException doe) {
137
138        }
139    }
140
141    private void setMode(int mode) {
142        if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
143            mSeekBar.setVisibility(View.GONE);
144        } else {
145            mSeekBar.setVisibility(View.VISIBLE);
146        }
147        Settings.System.putInt(getContext().getContentResolver(),
148                Settings.System.SCREEN_BRIGHTNESS_MODE, mode);
149    }
150}
151
152