blob: 00095037145b6fa23b6889205ad6d57052491d9c [file] [log] [blame]
Joe Onorato1e28f412010-12-01 09:33:36 -08001/*
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;
Joe Onoratoc07d7c12011-01-16 13:10:11 -080021import android.os.AsyncTask;
Joe Onorato1e28f412010-12-01 09:33:36 -080022import android.os.IPowerManager;
Jeff Brown96307042012-07-27 15:51:34 -070023import android.os.PowerManager;
Joe Onorato1e28f412010-12-01 09:33:36 -080024import 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;
31
Winson Chung5f623012012-09-14 14:58:43 -070032import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
33
34import java.util.ArrayList;
35
Joe Onorato1e28f412010-12-01 09:33:36 -080036public class BrightnessController implements ToggleSlider.Listener {
37 private static final String TAG = "StatusBar.BrightnessController";
38
Jeff Brown96307042012-07-27 15:51:34 -070039 private final int mMinimumBacklight;
40 private final int mMaximumBacklight;
Joe Onorato1e28f412010-12-01 09:33:36 -080041
John Spurlock48f37ec2012-10-05 16:32:51 -040042 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 Onorato1e28f412010-12-01 09:33:36 -080047
Winson Chung5f623012012-09-14 14:58:43 -070048 private ArrayList<BrightnessStateChangeCallback> mChangeCallbacks =
49 new ArrayList<BrightnessStateChangeCallback>();
50
51 public interface BrightnessStateChangeCallback {
52 public void onBrightnessLevelChanged();
53 }
54
Joe Onorato1e28f412010-12-01 09:33:36 -080055 public BrightnessController(Context context, ToggleSlider control) {
56 mContext = context;
57 mControl = control;
John Spurlock48f37ec2012-10-05 16:32:51 -040058 mUserTracker = new CurrentUserTracker(mContext);
Joe Onorato1e28f412010-12-01 09:33:36 -080059
Jeff Brown96307042012-07-27 15:51:34 -070060 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
61 mMinimumBacklight = pm.getMinimumScreenBrightnessSetting();
62 mMaximumBacklight = pm.getMaximumScreenBrightnessSetting();
63
John Spurlock48f37ec2012-10-05 16:32:51 -040064 mAutomaticAvailable = context.getResources().getBoolean(
Joe Onorato1e28f412010-12-01 09:33:36 -080065 com.android.internal.R.bool.config_automatic_brightness_available);
66 mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
67
John Spurlock48f37ec2012-10-05 16:32:51 -040068 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 Onorato1e28f412010-12-01 09:33:36 -080078 int automatic;
79 try {
John Spurlock48f37ec2012-10-05 16:32:51 -040080 automatic = Settings.System.getIntForUser(mContext.getContentResolver(),
81 Settings.System.SCREEN_BRIGHTNESS_MODE,
82 mUserTracker.getCurrentUserId());
Joe Onorato1e28f412010-12-01 09:33:36 -080083 } 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 Spurlock48f37ec2012-10-05 16:32:51 -040094 value = Settings.System.getIntForUser(mContext.getContentResolver(),
95 Settings.System.SCREEN_BRIGHTNESS,
96 mUserTracker.getCurrentUserId());
Joe Onorato1e28f412010-12-01 09:33:36 -080097 } catch (SettingNotFoundException ex) {
Jeff Brown96307042012-07-27 15:51:34 -070098 value = mMaximumBacklight;
Joe Onorato1e28f412010-12-01 09:33:36 -080099 }
100
Jeff Brown96307042012-07-27 15:51:34 -0700101 control.setMax(mMaximumBacklight - mMinimumBacklight);
102 control.setValue(value - mMinimumBacklight);
Winson Chung5f623012012-09-14 14:58:43 -0700103 }
104
Joe Onorato1e28f412010-12-01 09:33:36 -0800105 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 Brown96307042012-07-27 15:51:34 -0700109 final int val = value + mMinimumBacklight;
Joe Onoratoc07d7c12011-01-16 13:10:11 -0800110 setBrightness(val);
Joe Onorato772f5602011-01-16 11:10:16 -0800111 if (!tracking) {
Joe Onoratoc07d7c12011-01-16 13:10:11 -0800112 AsyncTask.execute(new Runnable() {
113 public void run() {
John Spurlock48f37ec2012-10-05 16:32:51 -0400114 Settings.System.putIntForUser(mContext.getContentResolver(),
115 Settings.System.SCREEN_BRIGHTNESS, val,
116 mUserTracker.getCurrentUserId());
Joe Onoratoc07d7c12011-01-16 13:10:11 -0800117 }
118 });
Joe Onorato772f5602011-01-16 11:10:16 -0800119 }
Joe Onorato1e28f412010-12-01 09:33:36 -0800120 }
Winson Chung5f623012012-09-14 14:58:43 -0700121
122 for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
123 cb.onBrightnessLevelChanged();
124 }
Joe Onorato1e28f412010-12-01 09:33:36 -0800125 }
126
127 private void setMode(int mode) {
John Spurlock48f37ec2012-10-05 16:32:51 -0400128 Settings.System.putIntForUser(mContext.getContentResolver(),
129 Settings.System.SCREEN_BRIGHTNESS_MODE, mode,
130 mUserTracker.getCurrentUserId());
Joe Onorato1e28f412010-12-01 09:33:36 -0800131 }
132
133 private void setBrightness(int brightness) {
134 try {
Jeff Brown96307042012-07-27 15:51:34 -0700135 mPower.setTemporaryScreenBrightnessSettingOverride(brightness);
Joe Onorato1e28f412010-12-01 09:33:36 -0800136 } catch (RemoteException ex) {
137 }
138 }
139}