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.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; 32 33 import java.util.Map; 34 35 public 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 setDialogIcon(R.drawable.ic_settings_display); 59 } 60 61 @Override 62 protected void onBindDialogView(View view) { 63 super.onBindDialogView(view); 64 65 mSeekBar = getSeekBar(view); 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 mSeekBar.setOnSeekBarChangeListener(this); 89 } 90 91 public void onProgressChanged(SeekBar seekBar, int progress, 92 boolean fromTouch) { 93 setBrightness(progress + MINIMUM_BACKLIGHT); 94 } 95 96 public void onStartTrackingTouch(SeekBar seekBar) { 97 // NA 98 } 99 100 public void onStopTrackingTouch(SeekBar seekBar) { 101 // NA 102 } 103 104 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 105 setMode(isChecked ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC 106 : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 107 if (!isChecked) { 108 setBrightness(mSeekBar.getProgress() + MINIMUM_BACKLIGHT); 109 } 110 } 111 112 @Override 113 protected void onDialogClosed(boolean positiveResult) { 114 super.onDialogClosed(positiveResult); 115 116 if (positiveResult) { 117 Settings.System.putInt(getContext().getContentResolver(), 118 Settings.System.SCREEN_BRIGHTNESS, 119 mSeekBar.getProgress() + MINIMUM_BACKLIGHT); 120 } else { 121 if (mAutomaticAvailable) { 122 setMode(mOldAutomatic); 123 } 124 if (!mAutomaticAvailable || mOldAutomatic == 0) { 125 setBrightness(mOldBrightness); 126 } 127 } 128 } 129 130 private void setBrightness(int brightness) { 131 try { 132 IPowerManager power = IPowerManager.Stub.asInterface( 133 ServiceManager.getService("power")); 134 if (power != null) { 135 power.setBacklightBrightness(brightness); 136 } 137 } catch (RemoteException doe) { 138 139 } 140 } 141 142 private void setMode(int mode) { 143 if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) { 144 mSeekBar.setVisibility(View.GONE); 145 } else { 146 mSeekBar.setVisibility(View.VISIBLE); 147 } 148 Settings.System.putInt(getContext().getContentResolver(), 149 Settings.System.SCREEN_BRIGHTNESS_MODE, mode); 150 } 151 } 152 153