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 | |
John Spurlock | 48f37ec | 2012-10-05 16:32:51 -0400 | [diff] [blame] | 42 | private final Context mContext; |
| 43 | private final ToggleSlider mControl; |
| 44 | private final boolean mAutomaticAvailable; |
| 45 | private final IPowerManager mPower; |
| 46 | private final CurrentUserTracker mUserTracker; |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 47 | |
Winson Chung | 5f62301 | 2012-09-14 14:58:43 -0700 | [diff] [blame] | 48 | private ArrayList<BrightnessStateChangeCallback> mChangeCallbacks = |
| 49 | new ArrayList<BrightnessStateChangeCallback>(); |
| 50 | |
| 51 | public interface BrightnessStateChangeCallback { |
| 52 | public void onBrightnessLevelChanged(); |
| 53 | } |
| 54 | |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 55 | public BrightnessController(Context context, ToggleSlider control) { |
| 56 | mContext = context; |
| 57 | mControl = control; |
John Spurlock | 48f37ec | 2012-10-05 16:32:51 -0400 | [diff] [blame] | 58 | mUserTracker = new CurrentUserTracker(mContext); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 59 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 60 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 61 | mMinimumBacklight = pm.getMinimumScreenBrightnessSetting(); |
| 62 | mMaximumBacklight = pm.getMaximumScreenBrightnessSetting(); |
| 63 | |
John Spurlock | 48f37ec | 2012-10-05 16:32:51 -0400 | [diff] [blame] | 64 | mAutomaticAvailable = context.getResources().getBoolean( |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 65 | com.android.internal.R.bool.config_automatic_brightness_available); |
| 66 | mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power")); |
| 67 | |
John Spurlock | 48f37ec | 2012-10-05 16:32:51 -0400 | [diff] [blame] | 68 | control.setOnChangedListener(this); |
| 69 | } |
| 70 | |
| 71 | public void addStateChangedCallback(BrightnessStateChangeCallback cb) { |
| 72 | mChangeCallbacks.add(cb); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void onInit(ToggleSlider control) { |
| 77 | if (mAutomaticAvailable) { |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 78 | int automatic; |
| 79 | try { |
John Spurlock | 48f37ec | 2012-10-05 16:32:51 -0400 | [diff] [blame] | 80 | automatic = Settings.System.getIntForUser(mContext.getContentResolver(), |
| 81 | Settings.System.SCREEN_BRIGHTNESS_MODE, |
| 82 | mUserTracker.getCurrentUserId()); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 83 | } catch (SettingNotFoundException snfe) { |
| 84 | automatic = 0; |
| 85 | } |
| 86 | control.setChecked(automatic != 0); |
| 87 | } else { |
| 88 | control.setChecked(false); |
| 89 | //control.hideToggle(); |
| 90 | } |
| 91 | |
| 92 | int value; |
| 93 | try { |
John Spurlock | 48f37ec | 2012-10-05 16:32:51 -0400 | [diff] [blame] | 94 | value = Settings.System.getIntForUser(mContext.getContentResolver(), |
| 95 | Settings.System.SCREEN_BRIGHTNESS, |
| 96 | mUserTracker.getCurrentUserId()); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 97 | } catch (SettingNotFoundException ex) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 98 | value = mMaximumBacklight; |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 101 | control.setMax(mMaximumBacklight - mMinimumBacklight); |
| 102 | control.setValue(value - mMinimumBacklight); |
Winson Chung | 5f62301 | 2012-09-14 14:58:43 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 105 | public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value) { |
| 106 | setMode(automatic ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC |
| 107 | : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); |
| 108 | if (!automatic) { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 109 | final int val = value + mMinimumBacklight; |
Joe Onorato | c07d7c1 | 2011-01-16 13:10:11 -0800 | [diff] [blame] | 110 | setBrightness(val); |
Joe Onorato | 772f560 | 2011-01-16 11:10:16 -0800 | [diff] [blame] | 111 | if (!tracking) { |
Joe Onorato | c07d7c1 | 2011-01-16 13:10:11 -0800 | [diff] [blame] | 112 | AsyncTask.execute(new Runnable() { |
| 113 | public void run() { |
John Spurlock | 48f37ec | 2012-10-05 16:32:51 -0400 | [diff] [blame] | 114 | Settings.System.putIntForUser(mContext.getContentResolver(), |
| 115 | Settings.System.SCREEN_BRIGHTNESS, val, |
| 116 | mUserTracker.getCurrentUserId()); |
Joe Onorato | c07d7c1 | 2011-01-16 13:10:11 -0800 | [diff] [blame] | 117 | } |
| 118 | }); |
Joe Onorato | 772f560 | 2011-01-16 11:10:16 -0800 | [diff] [blame] | 119 | } |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 120 | } |
Winson Chung | 5f62301 | 2012-09-14 14:58:43 -0700 | [diff] [blame] | 121 | |
| 122 | for (BrightnessStateChangeCallback cb : mChangeCallbacks) { |
| 123 | cb.onBrightnessLevelChanged(); |
| 124 | } |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | private void setMode(int mode) { |
John Spurlock | 48f37ec | 2012-10-05 16:32:51 -0400 | [diff] [blame] | 128 | Settings.System.putIntForUser(mContext.getContentResolver(), |
| 129 | Settings.System.SCREEN_BRIGHTNESS_MODE, mode, |
| 130 | mUserTracker.getCurrentUserId()); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | private void setBrightness(int brightness) { |
| 134 | try { |
Jeff Brown | 9630704 | 2012-07-27 15:51:34 -0700 | [diff] [blame] | 135 | mPower.setTemporaryScreenBrightnessSettingOverride(brightness); |
Joe Onorato | 1e28f41 | 2010-12-01 09:33:36 -0800 | [diff] [blame] | 136 | } catch (RemoteException ex) { |
| 137 | } |
| 138 | } |
| 139 | } |