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