apps/settings: Add auto/manual brightness control to Brightness settings

Add changes to have the ability to turn on and off the
automatic light sensing for the device.  This is fully configurable
and is by default not  present.  Vendors should override the ALS setting
to enable the automatic lighting controls.

These changes will add a check box to the Brightness settings menu to give control
to the user to allow the device's display lighting to be controlled via the slide bar
or the auto lighting system.

If the user selects auto then the slide bar will become invisible.  Manual mode
will present the slide bar to the user.

Change-Id: I512c9d5dd72ddd831b33eb8fcd4680e2fc7f786e
Signed-off-by: Dan Murphy <D.Murphy@motorola.com>
Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/res/layout/preference_dialog_brightness.xml b/res/layout/preference_dialog_brightness.xml
new file mode 100644
index 0000000..071beed
--- /dev/null
+++ b/res/layout/preference_dialog_brightness.xml
@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2008 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+
+     Copyright (C) 2009 Motorola, Inc.
+     March 23, 2009 - Motorola - Allow automatic brightness changes.
+-->
+
+<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="fill_parent"
+        android:layout_height="fill_parent">
+    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+            android:layout_width="fill_parent"
+            android:layout_height="fill_parent"
+            android:orientation="vertical"
+            android:gravity="center_horizontal"
+            android:paddingBottom="20dip">
+
+        <ImageView android:id="@android:id/icon"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:paddingTop="20dip" />
+
+        <CheckBox android:id="@+id/automatic_mode"
+                android:layout_width="fill_parent"
+                android:layout_height="wrap_content"
+                android:text="@string/automatic_brightness"
+                android:textAppearance="?android:attr/textAppearanceSmall"
+                android:layout_marginTop="6dip"
+                android:layout_marginLeft="20dip"
+                android:layout_marginRight="20dip" />
+
+        <SeekBar android:id="@*android:id/seekbar"
+                android:layout_width="fill_parent"
+                android:layout_height="wrap_content"
+                android:padding="20dip" />
+
+    </LinearLayout>
+</ScrollView>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index c9ad806..d39c90b 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -916,6 +916,8 @@
     <string name="screen_timeout">Screen timeout</string>
     <!-- Sound & display settings screen, setting option summary to change screen timeout -->
     <string name="screen_timeout_summary">Adjust the delay before the screen automatically turns off</string>
+    <!-- Sound & display settings screen, setting option name to change whether the screen adjusts automatically based on lighting conditions -->
+    <string name="automatic_brightness">Automatic brightness</string>
 
     <!-- SIM lock settings title -->
     <string name="sim_lock_settings">SIM card lock settings</string>
diff --git a/src/com/android/settings/BrightnessPreference.java b/src/com/android/settings/BrightnessPreference.java
index 9f55463..61d3cf9 100644
--- a/src/com/android/settings/BrightnessPreference.java
+++ b/src/com/android/settings/BrightnessPreference.java
@@ -26,16 +26,22 @@
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.View;
+import android.widget.CheckBox;
+import android.widget.CompoundButton;
 import android.widget.SeekBar;
 
 import java.util.Map;
 
 public class BrightnessPreference extends SeekBarPreference implements
-        SeekBar.OnSeekBarChangeListener {
+        SeekBar.OnSeekBarChangeListener, CheckBox.OnCheckedChangeListener {
 
     private SeekBar mSeekBar;
+    private CheckBox mCheckBox;
     
     private int mOldBrightness;
+    private int mOldAutomatic;
+
+    private boolean mAutomaticAvailable;
     
     // Backlight range is from 0 - 255. Need to make sure that user
     // doesn't set the backlight to 0 and get stuck
@@ -44,6 +50,11 @@
 
     public BrightnessPreference(Context context, AttributeSet attrs) {
         super(context, attrs);
+
+        mAutomaticAvailable = context.getResources().getBoolean(
+                com.android.internal.R.bool.config_automatic_brightness_available);
+
+        setDialogLayoutResource(R.layout.preference_dialog_brightness);
     }
 
     @Override
@@ -60,6 +71,20 @@
             mOldBrightness = MAXIMUM_BACKLIGHT;
         }
         mSeekBar.setProgress(mOldBrightness - MINIMUM_BACKLIGHT);
+
+        mCheckBox = (CheckBox)view.findViewById(R.id.automatic_mode);
+        if (mAutomaticAvailable) {
+            mCheckBox.setOnCheckedChangeListener(this);
+            try {
+                mOldAutomatic = Settings.System.getInt(getContext().getContentResolver(),
+                        Settings.System.SCREEN_BRIGHTNESS_MODE);
+            } catch (SettingNotFoundException snfe) {
+                mOldAutomatic = 0;
+            }
+            mCheckBox.setChecked(mOldAutomatic != 0);
+        } else {
+            mCheckBox.setVisibility(View.GONE);
+        }
     }
 
     public void onProgressChanged(SeekBar seekBar, int progress,
@@ -75,6 +100,13 @@
         // NA
     }
 
+    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+        setMode(isChecked ? 1 : 0);
+        if (!isChecked) {
+            setBrightness(mSeekBar.getProgress() + MINIMUM_BACKLIGHT);
+        }
+    }
+
     @Override
     protected void onDialogClosed(boolean positiveResult) {
         super.onDialogClosed(positiveResult);
@@ -83,8 +115,16 @@
             Settings.System.putInt(getContext().getContentResolver(), 
                     Settings.System.SCREEN_BRIGHTNESS,
                     mSeekBar.getProgress() + MINIMUM_BACKLIGHT);
+            if (mAutomaticAvailable) {
+                Settings.System.putInt(getContext().getContentResolver(),
+                        Settings.System.SCREEN_BRIGHTNESS_MODE,
+                        mCheckBox.isChecked() ? 1 : 0);
+            }
         } else {
             setBrightness(mOldBrightness);
+            if (mAutomaticAvailable) {
+                setMode(mOldAutomatic);
+            }
         }
     }
     
@@ -99,5 +139,21 @@
             
         }        
     }
+
+    private void setMode(int automatic) {
+        if (automatic != 0) {
+            mSeekBar.setVisibility(View.GONE);
+        } else {
+            mSeekBar.setVisibility(View.VISIBLE);
+        }
+        try {
+            IHardwareService hardware = IHardwareService.Stub.asInterface(
+                    ServiceManager.getService("hardware"));
+            if (hardware != null) {
+                hardware.setAutoBrightness(automatic != 0);
+            }
+        } catch (RemoteException doe) {
+        }
+    }
 }