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 private static final int MINIMUM_BACKLIGHT = android.os.PowerManager.BRIGHTNESS_DIM; 35 private static final int MAXIMUM_BACKLIGHT = android.os.PowerManager.BRIGHTNESS_ON; 36 37 private Context mContext; 38 private ToggleSlider mControl; 39 private IPowerManager mPower; 40 41 public BrightnessController(Context context, ToggleSlider control) { 42 mContext = context; 43 mControl = control; 44 45 boolean automaticAvailable = context.getResources().getBoolean( 46 com.android.internal.R.bool.config_automatic_brightness_available); 47 mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power")); 48 49 if (automaticAvailable) { 50 int automatic; 51 try { 52 automatic = Settings.System.getInt(mContext.getContentResolver(), 53 Settings.System.SCREEN_BRIGHTNESS_MODE); 54 } catch (SettingNotFoundException snfe) { 55 automatic = 0; 56 } 57 control.setChecked(automatic != 0); 58 } else { 59 control.setChecked(false); 60 //control.hideToggle(); 61 } 62 63 int value; 64 try { 65 value = Settings.System.getInt(mContext.getContentResolver(), 66 Settings.System.SCREEN_BRIGHTNESS); 67 } catch (SettingNotFoundException ex) { 68 value = MAXIMUM_BACKLIGHT; 69 } 70 71 control.setMax(MAXIMUM_BACKLIGHT - MINIMUM_BACKLIGHT); 72 control.setValue(value - MINIMUM_BACKLIGHT); 73 74 control.setOnChangedListener(this); 75 } 76 77 public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value) { 78 setMode(automatic ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC 79 : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 80 if (!automatic) { 81 final int val = value + MINIMUM_BACKLIGHT; 82 setBrightness(val); 83 if (!tracking) { 84 AsyncTask.execute(new Runnable() { 85 public void run() { 86 Settings.System.putInt(mContext.getContentResolver(), 87 Settings.System.SCREEN_BRIGHTNESS, val); 88 } 89 }); 90 } 91 } 92 } 93 94 private void setMode(int mode) { 95 Settings.System.putInt(mContext.getContentResolver(), 96 Settings.System.SCREEN_BRIGHTNESS_MODE, mode); 97 } 98 99 private void setBrightness(int brightness) { 100 try { 101 mPower.setBacklightBrightness(brightness); 102 } catch (RemoteException ex) { 103 } 104 } 105 } 106