Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 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; |
Joe Onorato | c07d7c1 | 2011-01-16 13:10:11 -0800 | [diff] [blame] | 21 | import android.os.AsyncTask; |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 22 | import android.os.IPowerManager; |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 23 | import android.os.PowerManager; |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 24 | import android.os.RemoteException; |
| 25 | import android.os.ServiceManager; |
| 26 | import android.provider.Settings; |
| 27 | import android.provider.Settings.SettingNotFoundException; |
| 28 | import android.util.Slog; |
| 29 | import android.view.IWindowManager; |
| 30 | import android.widget.CompoundButton; |
| 31 | |
Winson Chung | 5f62301 | 2012-09-14 14:58:43 -0700 | [diff] [blame] | 32 | import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback; |
| 33 | |
| 34 | import java.util.ArrayList; |
| 35 | |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 36 | public class BrightnessController implements ToggleSlider.Listener { |
| 37 | private static final String TAG = "StatusBar.BrightnessController"; |
| 38 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 39 | private final int mMinimumBacklight; |
| 40 | private final int mMaximumBacklight; |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 41 | |
| 42 | private Context mContext; |
| 43 | private ToggleSlider mControl; |
| 44 | private IPowerManager mPower; |
| 45 | |
Winson Chung | 5f62301 | 2012-09-14 14:58:43 -0700 | [diff] [blame] | 46 | private ArrayList<BrightnessStateChangeCallback> mChangeCallbacks = |
| 47 | new ArrayList<BrightnessStateChangeCallback>(); |
| 48 | |
| 49 | public interface BrightnessStateChangeCallback { |
| 50 | public void onBrightnessLevelChanged(); |
| 51 | } |
| 52 | |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 53 | public BrightnessController(Context context, ToggleSlider control) { |
| 54 | mContext = context; |
| 55 | mControl = control; |
| 56 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 57 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 58 | mMinimumBacklight = pm.getMinimumScreenBrightnessSetting(); |
| 59 | mMaximumBacklight = pm.getMaximumScreenBrightnessSetting(); |
| 60 | |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 61 | boolean automaticAvailable = context.getResources().getBoolean( |
| 62 | com.android.internal.R.bool.config_automatic_brightness_available); |
| 63 | mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power")); |
| 64 | |
| 65 | if (automaticAvailable) { |
| 66 | int automatic; |
| 67 | try { |
| 68 | automatic = Settings.System.getInt(mContext.getContentResolver(), |
| 69 | Settings.System.SCREEN_BRIGHTNESS_MODE); |
| 70 | } catch (SettingNotFoundException snfe) { |
| 71 | automatic = 0; |
| 72 | } |
| 73 | control.setChecked(automatic != 0); |
| 74 | } else { |
| 75 | control.setChecked(false); |
| 76 | //control.hideToggle(); |
| 77 | } |
| 78 | |
| 79 | int value; |
| 80 | try { |
| 81 | value = Settings.System.getInt(mContext.getContentResolver(), |
| 82 | Settings.System.SCREEN_BRIGHTNESS); |
| 83 | } catch (SettingNotFoundException ex) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 84 | value = mMaximumBacklight; |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 87 | control.setMax(mMaximumBacklight - mMinimumBacklight); |
| 88 | control.setValue(value - mMinimumBacklight); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 89 | |
| 90 | control.setOnChangedListener(this); |
| 91 | } |
| 92 | |
Winson Chung | 5f62301 | 2012-09-14 14:58:43 -0700 | [diff] [blame] | 93 | public void addStateChangedCallback(BrightnessStateChangeCallback cb) { |
| 94 | mChangeCallbacks.add(cb); |
| 95 | } |
| 96 | |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 97 | public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value) { |
| 98 | setMode(automatic ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC |
| 99 | : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); |
| 100 | if (!automatic) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 101 | final int val = value + mMinimumBacklight; |
Joe Onorato | c07d7c1 | 2011-01-16 13:10:11 -0800 | [diff] [blame] | 102 | setBrightness(val); |
Joe Onorato | 772f560 | 2011-01-16 11:10:16 -0800 | [diff] [blame] | 103 | if (!tracking) { |
Joe Onorato | c07d7c1 | 2011-01-16 13:10:11 -0800 | [diff] [blame] | 104 | AsyncTask.execute(new Runnable() { |
| 105 | public void run() { |
| 106 | Settings.System.putInt(mContext.getContentResolver(), |
| 107 | Settings.System.SCREEN_BRIGHTNESS, val); |
| 108 | } |
| 109 | }); |
Joe Onorato | 772f560 | 2011-01-16 11:10:16 -0800 | [diff] [blame] | 110 | } |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 111 | } |
Winson Chung | 5f62301 | 2012-09-14 14:58:43 -0700 | [diff] [blame] | 112 | |
| 113 | for (BrightnessStateChangeCallback cb : mChangeCallbacks) { |
| 114 | cb.onBrightnessLevelChanged(); |
| 115 | } |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | private void setMode(int mode) { |
| 119 | Settings.System.putInt(mContext.getContentResolver(), |
| 120 | Settings.System.SCREEN_BRIGHTNESS_MODE, mode); |
| 121 | } |
| 122 | |
| 123 | private void setBrightness(int brightness) { |
| 124 | try { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 125 | mPower.setTemporaryScreenBrightnessSettingOverride(brightness); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 126 | } catch (RemoteException ex) { |
| 127 | } |
| 128 | } |
| 129 | } |