Michael Wright | 0087a14 | 2013-02-05 16:29:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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.settings; |
| 18 | |
| 19 | import android.content.ContentResolver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.database.ContentObserver; |
| 23 | import android.net.Uri; |
| 24 | import android.os.AsyncTask; |
| 25 | import android.os.Handler; |
| 26 | import android.os.IPowerManager; |
| 27 | import android.os.PowerManager; |
| 28 | import android.os.RemoteException; |
| 29 | import android.os.ServiceManager; |
| 30 | import android.os.UserHandle; |
| 31 | import android.provider.Settings; |
| 32 | import android.provider.Settings.SettingNotFoundException; |
| 33 | import android.util.Slog; |
| 34 | import android.view.IWindowManager; |
| 35 | import android.widget.CompoundButton; |
| 36 | import android.widget.ImageView; |
| 37 | |
| 38 | import java.util.ArrayList; |
| 39 | |
| 40 | public class BrightnessController implements ToggleSlider.Listener { |
| 41 | private static final String TAG = "StatusBar.BrightnessController"; |
| 42 | |
| 43 | private final int mMinimumBacklight; |
| 44 | private final int mMaximumBacklight; |
| 45 | |
| 46 | private final Context mContext; |
| 47 | private final ImageView mIcon; |
| 48 | private final ToggleSlider mControl; |
| 49 | private final boolean mAutomaticAvailable; |
| 50 | private final IPowerManager mPower; |
| 51 | private final CurrentUserTracker mUserTracker; |
| 52 | private final Handler mHandler; |
| 53 | private final BrightnessObserver mBrightnessObserver; |
| 54 | |
| 55 | private ArrayList<BrightnessStateChangeCallback> mChangeCallbacks = |
| 56 | new ArrayList<BrightnessStateChangeCallback>(); |
| 57 | |
| 58 | public interface BrightnessStateChangeCallback { |
| 59 | public void onBrightnessLevelChanged(); |
| 60 | } |
| 61 | |
| 62 | /** ContentObserver to watch brightness **/ |
| 63 | private class BrightnessObserver extends ContentObserver { |
| 64 | |
| 65 | private final Uri BRIGHTNESS_MODE_URI = |
| 66 | Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE); |
| 67 | private final Uri BRIGHTNESS_URI = |
| 68 | Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS); |
| 69 | |
| 70 | public BrightnessObserver(Handler handler) { |
| 71 | super(handler); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void onChange(boolean selfChange) { |
| 76 | onChange(selfChange, null); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void onChange(boolean selfChange, Uri uri) { |
| 81 | if (selfChange) return; |
| 82 | if (BRIGHTNESS_MODE_URI.equals(uri)) { |
| 83 | updateMode(); |
| 84 | } else if (BRIGHTNESS_URI.equals(uri)) { |
| 85 | updateSlider(); |
| 86 | } else { |
| 87 | updateMode(); |
| 88 | updateSlider(); |
| 89 | } |
| 90 | for (BrightnessStateChangeCallback cb : mChangeCallbacks) { |
| 91 | cb.onBrightnessLevelChanged(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | public void startObserving() { |
| 96 | final ContentResolver cr = mContext.getContentResolver(); |
| 97 | cr.unregisterContentObserver(this); |
| 98 | cr.registerContentObserver( |
| 99 | BRIGHTNESS_MODE_URI, |
| 100 | false, this, UserHandle.USER_ALL); |
| 101 | cr.registerContentObserver( |
| 102 | BRIGHTNESS_URI, |
| 103 | false, this, UserHandle.USER_ALL); |
| 104 | } |
| 105 | |
| 106 | public void stopObserving() { |
| 107 | final ContentResolver cr = mContext.getContentResolver(); |
| 108 | cr.unregisterContentObserver(this); |
| 109 | } |
| 110 | |
| 111 | } |
| 112 | |
| 113 | public BrightnessController(Context context, ImageView icon, ToggleSlider control) { |
| 114 | mContext = context; |
| 115 | mIcon = icon; |
| 116 | mControl = control; |
| 117 | mHandler = new Handler(); |
| 118 | mUserTracker = new CurrentUserTracker(mContext) { |
| 119 | @Override |
| 120 | public void onUserSwitched(int newUserId) { |
| 121 | updateMode(); |
| 122 | updateSlider(); |
| 123 | } |
| 124 | }; |
| 125 | mBrightnessObserver = new BrightnessObserver(mHandler); |
| 126 | mBrightnessObserver.startObserving(); |
| 127 | |
| 128 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
| 129 | mMinimumBacklight = pm.getMinimumScreenBrightnessSetting(); |
| 130 | mMaximumBacklight = pm.getMaximumScreenBrightnessSetting(); |
| 131 | |
| 132 | mAutomaticAvailable = context.getResources().getBoolean( |
| 133 | com.android.internal.R.bool.config_automatic_brightness_available); |
| 134 | mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power")); |
| 135 | |
| 136 | // Update the slider and mode before attaching the listener so we don't receive the |
| 137 | // onChanged notifications for the initial values. |
| 138 | updateMode(); |
| 139 | updateSlider(); |
| 140 | |
| 141 | control.setOnChangedListener(this); |
| 142 | } |
| 143 | |
| 144 | public void addStateChangedCallback(BrightnessStateChangeCallback cb) { |
| 145 | mChangeCallbacks.add(cb); |
| 146 | } |
| 147 | |
| 148 | public boolean removeStateChangedCallback(BrightnessStateChangeCallback cb) { |
| 149 | return mChangeCallbacks.remove(cb); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void onInit(ToggleSlider control) { |
| 154 | // Do nothing |
| 155 | } |
| 156 | |
| 157 | /** Unregister all call backs, both to and from the controller */ |
| 158 | public void unregisterCallbacks() { |
| 159 | mBrightnessObserver.stopObserving(); |
| 160 | mChangeCallbacks.clear(); |
| 161 | mUserTracker.stopTracking(); |
| 162 | } |
| 163 | |
| 164 | public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value) { |
| 165 | setMode(automatic ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC |
| 166 | : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); |
| 167 | updateIcon(automatic); |
| 168 | if (!automatic) { |
| 169 | final int val = value + mMinimumBacklight; |
| 170 | setBrightness(val); |
| 171 | if (!tracking) { |
| 172 | AsyncTask.execute(new Runnable() { |
| 173 | public void run() { |
| 174 | Settings.System.putIntForUser(mContext.getContentResolver(), |
| 175 | Settings.System.SCREEN_BRIGHTNESS, val, |
| 176 | UserHandle.USER_CURRENT); |
| 177 | } |
| 178 | }); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | for (BrightnessStateChangeCallback cb : mChangeCallbacks) { |
| 183 | cb.onBrightnessLevelChanged(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | private void setMode(int mode) { |
| 188 | Settings.System.putIntForUser(mContext.getContentResolver(), |
| 189 | Settings.System.SCREEN_BRIGHTNESS_MODE, mode, |
| 190 | mUserTracker.getCurrentUserId()); |
| 191 | } |
| 192 | |
| 193 | private void setBrightness(int brightness) { |
| 194 | try { |
| 195 | mPower.setTemporaryScreenBrightnessSettingOverride(brightness); |
| 196 | } catch (RemoteException ex) { |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | private void updateIcon(boolean automatic) { |
| 201 | if (mIcon != null) { |
| 202 | mIcon.setImageResource(automatic ? |
| 203 | com.android.systemui.R.drawable.ic_qs_brightness_auto_on : |
| 204 | com.android.systemui.R.drawable.ic_qs_brightness_auto_off); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** Fetch the brightness mode from the system settings and update the icon */ |
| 209 | private void updateMode() { |
| 210 | if (mAutomaticAvailable) { |
| 211 | int automatic; |
| 212 | try { |
| 213 | automatic = Settings.System.getIntForUser(mContext.getContentResolver(), |
| 214 | Settings.System.SCREEN_BRIGHTNESS_MODE, |
| 215 | UserHandle.USER_CURRENT); |
| 216 | } catch (SettingNotFoundException snfe) { |
| 217 | automatic = 0; |
| 218 | } |
| 219 | mControl.setChecked(automatic != 0); |
| 220 | updateIcon(automatic != 0); |
| 221 | } else { |
| 222 | mControl.setChecked(false); |
| 223 | updateIcon(false /*automatic*/); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** Fetch the brightness from the system settings and update the slider */ |
| 228 | private void updateSlider() { |
| 229 | int value; |
| 230 | try { |
| 231 | value = Settings.System.getIntForUser(mContext.getContentResolver(), |
| 232 | Settings.System.SCREEN_BRIGHTNESS, |
| 233 | UserHandle.USER_CURRENT); |
| 234 | } catch (SettingNotFoundException ex) { |
| 235 | value = mMaximumBacklight; |
| 236 | } |
| 237 | mControl.setMax(mMaximumBacklight - mMinimumBacklight); |
| 238 | mControl.setValue(value - mMinimumBacklight); |
| 239 | } |
| 240 | |
| 241 | } |