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 17package com.android.systemui.statusbar.policy; 18 19import android.content.ContentResolver; 20import android.content.Context; 21import android.os.AsyncTask; 22import android.os.IPowerManager; 23import android.os.PowerManager; 24import android.os.RemoteException; 25import android.os.ServiceManager; 26import android.provider.Settings; 27import android.provider.Settings.SettingNotFoundException; 28import android.util.Slog; 29import android.view.IWindowManager; 30import android.widget.CompoundButton; 31import android.widget.ImageView; 32 33import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback; 34 35import java.util.ArrayList; 36 37public class BrightnessController implements ToggleSlider.Listener { 38 private static final String TAG = "StatusBar.BrightnessController"; 39 40 private final int mMinimumBacklight; 41 private final int mMaximumBacklight; 42 43 private final Context mContext; 44 private final ImageView mIcon; 45 private final ToggleSlider mControl; 46 private final boolean mAutomaticAvailable; 47 private final IPowerManager mPower; 48 private final CurrentUserTracker mUserTracker; 49 50 private ArrayList<BrightnessStateChangeCallback> mChangeCallbacks = 51 new ArrayList<BrightnessStateChangeCallback>(); 52 53 public interface BrightnessStateChangeCallback { 54 public void onBrightnessLevelChanged(); 55 } 56 57 public BrightnessController(Context context, ImageView icon, ToggleSlider control) { 58 mContext = context; 59 mIcon = icon; 60 mControl = control; 61 mUserTracker = new CurrentUserTracker(mContext); 62 63 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); 64 mMinimumBacklight = pm.getMinimumScreenBrightnessSetting(); 65 mMaximumBacklight = pm.getMaximumScreenBrightnessSetting(); 66 67 mAutomaticAvailable = context.getResources().getBoolean( 68 com.android.internal.R.bool.config_automatic_brightness_available); 69 mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power")); 70 71 control.setOnChangedListener(this); 72 } 73 74 public void addStateChangedCallback(BrightnessStateChangeCallback cb) { 75 mChangeCallbacks.add(cb); 76 } 77 78 @Override 79 public void onInit(ToggleSlider control) { 80 if (mAutomaticAvailable) { 81 int automatic; 82 try { 83 automatic = Settings.System.getIntForUser(mContext.getContentResolver(), 84 Settings.System.SCREEN_BRIGHTNESS_MODE, 85 mUserTracker.getCurrentUserId()); 86 } catch (SettingNotFoundException snfe) { 87 automatic = 0; 88 } 89 control.setChecked(automatic != 0); 90 updateIcon(automatic != 0); 91 } else { 92 control.setChecked(false); 93 updateIcon(false /*automatic*/); 94 //control.hideToggle(); 95 } 96 97 int value; 98 try { 99 value = Settings.System.getIntForUser(mContext.getContentResolver(), 100 Settings.System.SCREEN_BRIGHTNESS, 101 mUserTracker.getCurrentUserId()); 102 } catch (SettingNotFoundException ex) { 103 value = mMaximumBacklight; 104 } 105 106 control.setMax(mMaximumBacklight - mMinimumBacklight); 107 control.setValue(value - mMinimumBacklight); 108 } 109 110 public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value) { 111 setMode(automatic ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC 112 : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 113 updateIcon(automatic); 114 if (!automatic) { 115 final int val = value + mMinimumBacklight; 116 setBrightness(val); 117 if (!tracking) { 118 AsyncTask.execute(new Runnable() { 119 public void run() { 120 Settings.System.putIntForUser(mContext.getContentResolver(), 121 Settings.System.SCREEN_BRIGHTNESS, val, 122 mUserTracker.getCurrentUserId()); 123 } 124 }); 125 } 126 } 127 128 for (BrightnessStateChangeCallback cb : mChangeCallbacks) { 129 cb.onBrightnessLevelChanged(); 130 } 131 } 132 133 private void setMode(int mode) { 134 Settings.System.putIntForUser(mContext.getContentResolver(), 135 Settings.System.SCREEN_BRIGHTNESS_MODE, mode, 136 mUserTracker.getCurrentUserId()); 137 } 138 139 private void setBrightness(int brightness) { 140 try { 141 mPower.setTemporaryScreenBrightnessSettingOverride(brightness); 142 } catch (RemoteException ex) { 143 } 144 } 145 146 private void updateIcon(boolean automatic) { 147 if (mIcon != null) { 148 mIcon.setImageResource(automatic ? 149 com.android.systemui.R.drawable.ic_qs_brightness_auto_on : 150 com.android.systemui.R.drawable.ic_qs_brightness_auto_off); 151 } 152 } 153} 154