blob: bb59420641db7bec70fb69f672ceb90e6ce160a6 [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
42 private Context mContext;
43 private ToggleSlider mControl;
44 private IPowerManager mPower;
45
Winson Chung5f623012012-09-14 14:58:43 -070046 private ArrayList<BrightnessStateChangeCallback> mChangeCallbacks =
47 new ArrayList<BrightnessStateChangeCallback>();
48
49 public interface BrightnessStateChangeCallback {
50 public void onBrightnessLevelChanged();
51 }
52
Joe Onorato1e28f412010-12-01 09:33:36 -080053 public BrightnessController(Context context, ToggleSlider control) {
54 mContext = context;
55 mControl = control;
56
Jeff Brown96307042012-07-27 15:51:34 -070057 PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
58 mMinimumBacklight = pm.getMinimumScreenBrightnessSetting();
59 mMaximumBacklight = pm.getMaximumScreenBrightnessSetting();
60
Joe Onorato1e28f412010-12-01 09:33:36 -080061 boolean automaticAvailable = context.getResources().getBoolean(
62 com.android.internal.R.bool.config_automatic_brightness_available);
63 mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
64
65 if (automaticAvailable) {
66 int automatic;
67 try {
68 automatic = Settings.System.getInt(mContext.getContentResolver(),
69 Settings.System.SCREEN_BRIGHTNESS_MODE);
70 } catch (SettingNotFoundException snfe) {
71 automatic = 0;
72 }
73 control.setChecked(automatic != 0);
74 } else {
75 control.setChecked(false);
76 //control.hideToggle();
77 }
78
79 int value;
80 try {
81 value = Settings.System.getInt(mContext.getContentResolver(),
82 Settings.System.SCREEN_BRIGHTNESS);
83 } catch (SettingNotFoundException ex) {
Jeff Brown96307042012-07-27 15:51:34 -070084 value = mMaximumBacklight;
Joe Onorato1e28f412010-12-01 09:33:36 -080085 }
86
Jeff Brown96307042012-07-27 15:51:34 -070087 control.setMax(mMaximumBacklight - mMinimumBacklight);
88 control.setValue(value - mMinimumBacklight);
Joe Onorato1e28f412010-12-01 09:33:36 -080089
90 control.setOnChangedListener(this);
91 }
92
Winson Chung5f623012012-09-14 14:58:43 -070093 public void addStateChangedCallback(BrightnessStateChangeCallback cb) {
94 mChangeCallbacks.add(cb);
95 }
96
Joe Onorato1e28f412010-12-01 09:33:36 -080097 public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value) {
98 setMode(automatic ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
99 : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
100 if (!automatic) {
Jeff Brown96307042012-07-27 15:51:34 -0700101 final int val = value + mMinimumBacklight;
Joe Onoratoc07d7c12011-01-16 13:10:11 -0800102 setBrightness(val);
Joe Onorato772f5602011-01-16 11:10:16 -0800103 if (!tracking) {
Joe Onoratoc07d7c12011-01-16 13:10:11 -0800104 AsyncTask.execute(new Runnable() {
105 public void run() {
106 Settings.System.putInt(mContext.getContentResolver(),
107 Settings.System.SCREEN_BRIGHTNESS, val);
108 }
109 });
Joe Onorato772f5602011-01-16 11:10:16 -0800110 }
Joe Onorato1e28f412010-12-01 09:33:36 -0800111 }
Winson Chung5f623012012-09-14 14:58:43 -0700112
113 for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
114 cb.onBrightnessLevelChanged();
115 }
Joe Onorato1e28f412010-12-01 09:33:36 -0800116 }
117
118 private void setMode(int mode) {
119 Settings.System.putInt(mContext.getContentResolver(),
120 Settings.System.SCREEN_BRIGHTNESS_MODE, mode);
121 }
122
123 private void setBrightness(int brightness) {
124 try {
Jeff Brown96307042012-07-27 15:51:34 -0700125 mPower.setTemporaryScreenBrightnessSettingOverride(brightness);
Joe Onorato1e28f412010-12-01 09:33:36 -0800126 } catch (RemoteException ex) {
127 }
128 }
129}