The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.RemoteException; |
| 21 | import android.os.IHardwareService; |
| 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.SeekBar; |
| 30 | |
| 31 | import java.util.Map; |
| 32 | |
| 33 | public 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 | |