1 /* 2 * Copyright (C) 2010 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.systemui.statusbar.policy; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.os.AsyncTask; 22 import android.os.IPowerManager; 23 import android.os.RemoteException; 24 import android.os.ServiceManager; 25 import android.provider.Settings; 26 import android.provider.Settings.SettingNotFoundException; 27 import android.util.Slog; 28 import android.view.IWindowManager; 29 import android.widget.CompoundButton; 30 31 public class BrightnessController implements ToggleSlider.Listener { 32 private static final String TAG = "StatusBar.BrightnessController"; 33 34 // Backlight range is from 0 - 255. Need to make sure that user 35 // doesn't set the backlight to 0 and get stuck 36 private static final int MINIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_DIM + 10; 37 private static final int MAXIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_ON; 38 39 private Context mContext; 40 private ToggleSlider mControl; 41 private IPowerManager mPower; 42 43 public BrightnessController(Context context, ToggleSlider control) { 44 mContext = context; 45 mControl = control; 46 47 boolean automaticAvailable = context.getResources().getBoolean( 48 com.android.internal.R.bool.config_automatic_brightness_available); 49 mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power")); 50 51 if (automaticAvailable) { 52 int automatic; 53 try { 54 automatic = Settings.System.getInt(mContext.getContentResolver(), 55 Settings.System.SCREEN_BRIGHTNESS_MODE); 56 } catch (SettingNotFoundException snfe) { 57 automatic = 0; 58 } 59 control.setChecked(automatic != 0); 60 } else { 61 control.setChecked(false); 62 //control.hideToggle(); 63 } 64 65 int value; 66 try { 67 value = Settings.System.getInt(mContext.getContentResolver(), 68 Settings.System.SCREEN_BRIGHTNESS); 69 } catch (SettingNotFoundException ex) { 70 value = MAXIMUM_BACKLIGHT; 71 } 72 73 control.setMax(MAXIMUM_BACKLIGHT - MINIMUM_BACKLIGHT); 74 control.setValue(value - MINIMUM_BACKLIGHT); 75 76 control.setOnChangedListener(this); 77 } 78 79 public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value) { 80 setMode(automatic ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC 81 : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 82 if (!automatic) { 83 final int val = value + MINIMUM_BACKLIGHT; 84 setBrightness(val); 85 if (!tracking) { 86 AsyncTask.execute(new Runnable() { 87 public void run() { 88 Settings.System.putInt(mContext.getContentResolver(), 89 Settings.System.SCREEN_BRIGHTNESS, val); 90 } 91 }); 92 } 93 } 94 } 95 96 private void setMode(int mode) { 97 Settings.System.putInt(mContext.getContentResolver(), 98 Settings.System.SCREEN_BRIGHTNESS_MODE, mode); 99 } 100 101 private void setBrightness(int brightness) { 102 try { 103 mPower.setBacklightBrightness(brightness); 104 } catch (RemoteException ex) { 105 } 106 } 107 } 108