blob: a9851cc93a2c6786af961d098767425e8c8c1a12 [file] [log] [blame]
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -07001/*
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.IHardwareService;
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.SeekBar;
30
31import java.util.Map;
32
33public class BrightnessPreference extends SeekBarPreference implements
34 SeekBar.OnSeekBarChangeListener {
35
36 private SeekBar mSeekBar;
37
38 private int mOldBrightness;
39
40 // Backlight range is from 0 - 255. Need to make sure that user
41 // doesn't set the backlight to 0 and get stuck
42 private static final int MINIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_DIM + 10;
43 private static final int MAXIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_ON;
44
45 public BrightnessPreference(Context context, AttributeSet attrs) {
46 super(context, attrs);
47 }
48
49 @Override
50 protected void onBindDialogView(View view) {
51 super.onBindDialogView(view);
52
53 mSeekBar = getSeekBar(view);
54 mSeekBar.setOnSeekBarChangeListener(this);
55 mSeekBar.setMax(MAXIMUM_BACKLIGHT - MINIMUM_BACKLIGHT);
56 try {
57 mOldBrightness = Settings.System.getInt(getContext().getContentResolver(),
58 Settings.System.SCREEN_BRIGHTNESS);
59 } catch (SettingNotFoundException snfe) {
60 mOldBrightness = MAXIMUM_BACKLIGHT;
61 }
62 mSeekBar.setProgress(mOldBrightness - MINIMUM_BACKLIGHT);
63 }
64
65 public void onProgressChanged(SeekBar seekBar, int progress,
66 boolean fromTouch) {
67 setBrightness(progress + MINIMUM_BACKLIGHT);
68 }
69
70 public void onStartTrackingTouch(SeekBar seekBar) {
71 // NA
72 }
73
74 public void onStopTrackingTouch(SeekBar seekBar) {
75 // NA
76 }
77
78 @Override
79 protected void onDialogClosed(boolean positiveResult) {
80 super.onDialogClosed(positiveResult);
81
82 if (positiveResult) {
83 Settings.System.putInt(getContext().getContentResolver(),
84 Settings.System.SCREEN_BRIGHTNESS,
85 mSeekBar.getProgress() + MINIMUM_BACKLIGHT);
86 } else {
87 setBrightness(mOldBrightness);
88 }
89 }
90
91 private void setBrightness(int brightness) {
92 try {
93 IHardwareService hardware = IHardwareService.Stub.asInterface(
94 ServiceManager.getService("hardware"));
95 if (hardware != null) {
96 hardware.setScreenBacklight(brightness);
97 }
98 } catch (RemoteException doe) {
99
100 }
101 }
102}
103