11e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato/*
21e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato * Copyright (C) 2010 The Android Open Source Project
31e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato *
41e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato * Licensed under the Apache License, Version 2.0 (the "License");
51e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato * you may not use this file except in compliance with the License.
61e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato * You may obtain a copy of the License at
71e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato *
81e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato *      http://www.apache.org/licenses/LICENSE-2.0
91e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato *
101e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato * Unless required by applicable law or agreed to in writing, software
111e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato * distributed under the License is distributed on an "AS IS" BASIS,
121e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato * See the License for the specific language governing permissions and
141e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato * limitations under the License.
151e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato */
161e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato
171e28f4110f4005a38a1c55836b87df706e8e3229Joe Onoratopackage com.android.systemui.statusbar.policy;
181e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato
191e28f4110f4005a38a1c55836b87df706e8e3229Joe Onoratoimport android.content.ContentResolver;
201e28f4110f4005a38a1c55836b87df706e8e3229Joe Onoratoimport android.content.Context;
21c07d7c139bafab41024d6abc2e39ded880c23e6dJoe Onoratoimport android.os.AsyncTask;
221e28f4110f4005a38a1c55836b87df706e8e3229Joe Onoratoimport android.os.IPowerManager;
231e28f4110f4005a38a1c55836b87df706e8e3229Joe Onoratoimport android.os.RemoteException;
241e28f4110f4005a38a1c55836b87df706e8e3229Joe Onoratoimport android.os.ServiceManager;
251e28f4110f4005a38a1c55836b87df706e8e3229Joe Onoratoimport android.provider.Settings;
261e28f4110f4005a38a1c55836b87df706e8e3229Joe Onoratoimport android.provider.Settings.SettingNotFoundException;
271e28f4110f4005a38a1c55836b87df706e8e3229Joe Onoratoimport android.util.Slog;
281e28f4110f4005a38a1c55836b87df706e8e3229Joe Onoratoimport android.view.IWindowManager;
291e28f4110f4005a38a1c55836b87df706e8e3229Joe Onoratoimport android.widget.CompoundButton;
301e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato
311e28f4110f4005a38a1c55836b87df706e8e3229Joe Onoratopublic class BrightnessController implements ToggleSlider.Listener {
321e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato    private static final String TAG = "StatusBar.BrightnessController";
331e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato
347304c343821309dd15f769b18f1de2fa43751573Jeff Brown    private static final int MINIMUM_BACKLIGHT = android.os.PowerManager.BRIGHTNESS_DIM;
357304c343821309dd15f769b18f1de2fa43751573Jeff Brown    private static final int MAXIMUM_BACKLIGHT = android.os.PowerManager.BRIGHTNESS_ON;
361e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato
371e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato    private Context mContext;
381e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato    private ToggleSlider mControl;
391e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato    private IPowerManager mPower;
401e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato
411e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato    public BrightnessController(Context context, ToggleSlider control) {
421e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        mContext = context;
431e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        mControl = control;
441e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato
451e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        boolean automaticAvailable = context.getResources().getBoolean(
461e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato                com.android.internal.R.bool.config_automatic_brightness_available);
471e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
481e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato
491e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        if (automaticAvailable) {
501e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato            int automatic;
511e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato            try {
521e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato                automatic = Settings.System.getInt(mContext.getContentResolver(),
531e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato                        Settings.System.SCREEN_BRIGHTNESS_MODE);
541e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato            } catch (SettingNotFoundException snfe) {
551e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato                automatic = 0;
561e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato            }
571e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato            control.setChecked(automatic != 0);
581e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        } else {
591e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato            control.setChecked(false);
601e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato            //control.hideToggle();
611e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        }
621e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato
631e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        int value;
641e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        try {
651e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato            value = Settings.System.getInt(mContext.getContentResolver(),
661e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato                    Settings.System.SCREEN_BRIGHTNESS);
671e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        } catch (SettingNotFoundException ex) {
681e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato            value = MAXIMUM_BACKLIGHT;
691e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        }
701e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato
711e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        control.setMax(MAXIMUM_BACKLIGHT - MINIMUM_BACKLIGHT);
721e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        control.setValue(value - MINIMUM_BACKLIGHT);
731e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato
741e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        control.setOnChangedListener(this);
751e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato    }
761e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato
771e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato    public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value) {
781e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        setMode(automatic ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
791e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato                : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
801e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        if (!automatic) {
81fac86056b285bf59c1c3e774f1cb9f6de0be59baJoe Onorato            final int val = value + MINIMUM_BACKLIGHT;
82c07d7c139bafab41024d6abc2e39ded880c23e6dJoe Onorato            setBrightness(val);
83772f5600ef720945b98054246982fd3b711830a9Joe Onorato            if (!tracking) {
84c07d7c139bafab41024d6abc2e39ded880c23e6dJoe Onorato                AsyncTask.execute(new Runnable() {
85c07d7c139bafab41024d6abc2e39ded880c23e6dJoe Onorato                        public void run() {
86c07d7c139bafab41024d6abc2e39ded880c23e6dJoe Onorato                            Settings.System.putInt(mContext.getContentResolver(),
87c07d7c139bafab41024d6abc2e39ded880c23e6dJoe Onorato                                    Settings.System.SCREEN_BRIGHTNESS, val);
88c07d7c139bafab41024d6abc2e39ded880c23e6dJoe Onorato                        }
89c07d7c139bafab41024d6abc2e39ded880c23e6dJoe Onorato                    });
90772f5600ef720945b98054246982fd3b711830a9Joe Onorato            }
911e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        }
921e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato    }
931e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato
941e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato    private void setMode(int mode) {
951e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        Settings.System.putInt(mContext.getContentResolver(),
961e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato                Settings.System.SCREEN_BRIGHTNESS_MODE, mode);
971e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato    }
981e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato
991e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato    private void setBrightness(int brightness) {
1001e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        try {
1011e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato            mPower.setBacklightBrightness(brightness);
1021e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        } catch (RemoteException ex) {
1031e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato        }
1041e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato    }
1051e28f4110f4005a38a1c55836b87df706e8e3229Joe Onorato}
106