[2/2] omnigears: button brightness timeout support

Allows setting a timeout value after that button lights
should be turned of automatically

Change-Id: Ia6e49c349860acb720348043ae2424208ca80c2d
diff --git a/res/layout/button_timeout.xml b/res/layout/button_timeout.xml
new file mode 100644
index 0000000..35d72c2
--- /dev/null
+++ b/res/layout/button_timeout.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--  Copyright (C) 2013 The OmniROM Project
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 2 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:orientation="vertical"
+        android:padding="8dip">
+
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:paddingTop="8dip"
+        android:text="@string/button_timeout_title" />
+
+    <RelativeLayout
+        android:id="@+id/timeout_container"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:paddingTop="2dip" >
+
+        <SeekBar
+            android:id="@+id/timeout_seekbar"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_gravity="center_vertical"
+            android:paddingLeft="40dip"
+            android:paddingRight="20dip" />
+
+        <TextView android:id="@+id/timeout_value"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_alignParentRight="true"
+            android:layout_below="@id/timeout_seekbar"
+            android:paddingRight="20dip" />
+
+    </RelativeLayout>
+
+</LinearLayout>
diff --git a/res/values/custom_strings.xml b/res/values/custom_strings.xml
index 51aec6c..9f6e10b 100644
--- a/res/values/custom_strings.xml
+++ b/res/values/custom_strings.xml
@@ -122,7 +122,7 @@
     <string name="button_link_brightness_summary">Buttons brightness will use the same value as the screen</string>
     <string name="button_manual_brightness">Manual level</string>
     <string name="button_manual_brightness_summary">Button brightness level for manual mode</string>
-    <string name="dialog_manual_brightness_title">Button brightness</string>
+    <string name="dialog_manual_brightness_title">Manual button brightness</string>
     <!-- Automatic brightness customization -->
     <string name="auto_brightness_setup_title">Automatic brightness</string>
     <string name="light_sensor_current_value">Current light sensor value: <xliff:g id="light_level">%d</xliff:g> lux</string>
@@ -144,6 +144,14 @@
     <string name="auto_brightness_menu_split">Split</string>
     <string name="auto_brightness_menu_remove">Remove</string>
     <string name="sensor_category">Sensor</string>
+    <string name="button_timeout_title">Timeout</string>
+    <plurals name="button_timeout_time">
+        <item quantity="one"><xliff:g id="seconds">%d</xliff:g> second</item>
+        <item quantity="other"><xliff:g id="seconds">%d</xliff:g> seconds</item>
+    </plurals>
+    <string name="button_timeout_enabled">Enabled for <xliff:g id="timeout">%s</xliff:g></string>
+    <string name="button_timeout_disabled">Disabled</string>
+    <string name="dialog_button_timeout_title">Button brightness timeout</string>
 
     <!-- button settings -->
     <string name="button_volume_keys_title">Volume</string>
diff --git a/res/xml/brightness_settings.xml b/res/xml/brightness_settings.xml
index 7f92c11..acb4625 100644
--- a/res/xml/brightness_settings.xml
+++ b/res/xml/brightness_settings.xml
@@ -38,6 +38,14 @@
                 android:key="button_brightness_category"
                 android:title="@string/button_brightness_category">
 
+                <Preference
+                        android:key="button_auto_brightness"
+                        android:title="@string/button_auto_brightness"
+                        android:summary="@string/button_auto_brighness_summary"/>
+                <Preference
+                        android:key="button_manual_brightness"
+                        android:title="@string/button_manual_brightness"
+                        android:summary="@string/button_manual_brightness_summary"/>
                 <CheckBoxPreference
                         android:key="button_no_brightness"
                         android:title="@string/button_no_brightness"
@@ -47,13 +55,9 @@
                         android:title="@string/button_link_brightness"
                         android:summary="@string/button_link_brightness_summary"/>
                 <Preference
-                        android:key="button_auto_brightness"
-                        android:title="@string/button_auto_brightness"
-                        android:summary="@string/button_auto_brighness_summary"/>
-                <Preference
-                        android:key="button_manual_brightness"
-                        android:title="@string/button_manual_brightness"
-                        android:summary="@string/button_manual_brightness_summary"/>
+                        android:key="button_timeout"
+                        android:title="@string/button_timeout_title" />
+
         </PreferenceCategory>
 
         <PreferenceCategory
diff --git a/src/org/omnirom/omnigears/brightness/BrightnessSettings.java b/src/org/omnirom/omnigears/brightness/BrightnessSettings.java
index 4405af4..ab16257 100644
--- a/src/org/omnirom/omnigears/brightness/BrightnessSettings.java
+++ b/src/org/omnirom/omnigears/brightness/BrightnessSettings.java
@@ -43,6 +43,7 @@
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.SeekBar;
+import android.widget.TextView;
 
 import org.omnirom.omnigears.R;
 
@@ -57,6 +58,7 @@
     private static final String KEY_SCREEN_AUTO_BRIGHTNESS = "screen_auto_brightness";
     private static final String KEY_BUTTON_AUTO_BRIGHTNESS = "button_auto_brightness";
     private static final String KEY_BUTTON_MANUAL_BRIGHTNESS = "button_manual_brightness";
+    private static final String KEY_BUTTON_TIMEOUT = "button_timeout";
 
     private ListPreference mAutomaticSensitivity;
     private CheckBoxPreference mNoButtonBrightness;
@@ -69,6 +71,8 @@
     private ManualButtonBrightnessDialog mManualBrightnessDialog;
     private boolean mButtonBrightnessSupport;
     private IPowerManager mPowerService;
+    private ButtonTimeoutDialog mButtonTimeoutDialog;
+    private Preference mButtonTimout;
 
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -103,8 +107,36 @@
 
             mAutomaticButtonBrightness = (Preference) findPreference(KEY_BUTTON_AUTO_BRIGHTNESS);
             mManualButtonBrightness = (Preference) findPreference(KEY_BUTTON_MANUAL_BRIGHTNESS);
+            mButtonTimout = (Preference) findPreference(KEY_BUTTON_TIMEOUT);
+
+            // to set initial summary
+            mButtonTimeoutDialog = new ButtonTimeoutDialog(getActivity());
+            mButtonTimeoutDialog.updateSummary();
 
             mPowerService = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
+
+            updateEnablement();
+        }
+    }
+
+    private void updateEnablement() {
+        if (mButtonBrightnessSupport){
+            if (mNoButtonBrightness.isChecked()){
+                mLinkButtonBrightness.setEnabled(false);
+                mButtonTimout.setEnabled(false);
+                mAutomaticButtonBrightness.setEnabled(false);
+                mManualButtonBrightness.setEnabled(false);
+            } else if (mLinkButtonBrightness.isChecked()){
+                mNoButtonBrightness.setEnabled(false);
+                mAutomaticButtonBrightness.setEnabled(false);
+                mManualButtonBrightness.setEnabled(false);
+            } else {
+                mNoButtonBrightness.setEnabled(true);
+                mLinkButtonBrightness.setEnabled(true);
+                mButtonTimout.setEnabled(true);
+                mAutomaticButtonBrightness.setEnabled(true);
+                mManualButtonBrightness.setEnabled(true);
+            }
         }
     }
 
@@ -126,6 +158,9 @@
         if (mManualBrightnessDialog != null) {
             mManualBrightnessDialog.dismiss();
         }
+        if (mButtonTimeoutDialog != null) {
+            mButtonTimeoutDialog.dismiss();
+        }
     }
 
     @Override
@@ -136,14 +171,18 @@
             showButtonAutoBrightnessDialog();
         } else if (preference == mManualButtonBrightness) {
             showButtonManualBrightnessDialog();
+        } else if (preference == mButtonTimout) {
+            showButtonTimoutDialog();
         } else if (preference == mNoButtonBrightness) {
             boolean checked = ((CheckBoxPreference)preference).isChecked();
             Settings.System.putInt(getActivity().getContentResolver(),
                     Settings.System.CUSTOM_BUTTON_DISABLE_BRIGHTNESS, checked ? 1:0);
+            updateEnablement();
         } else if (preference == mLinkButtonBrightness) {
             boolean checked = ((CheckBoxPreference)preference).isChecked();
             Settings.System.putInt(getActivity().getContentResolver(),
                     Settings.System.CUSTOM_BUTTON_USE_SCREEN_BRIGHTNESS, checked ? 1:0);
+            updateEnablement();
         } else {
             return false;
         }
@@ -199,6 +238,14 @@
         mButtonBrightnessDialog.show();
     }
 
+    private void showButtonTimoutDialog() {
+        if (mButtonTimeoutDialog.isShowing()) {
+            return;
+        }
+
+        mButtonTimeoutDialog.show();
+    }
+
     private void showButtonManualBrightnessDialog() {
         if (mManualBrightnessDialog != null && mManualBrightnessDialog.isShowing()) {
             return;
@@ -260,7 +307,7 @@
             if (which == DialogInterface.BUTTON_POSITIVE) {
                 try {
                     int newBacklight = Integer.valueOf(mBacklightInput.getText().toString());
-                    Settings.System.putInt(getActivity().getContentResolver(),
+                    Settings.System.putInt(getContext().getContentResolver(),
                             Settings.System.CUSTOM_BUTTON_BRIGHTNESS, newBacklight);
                 } catch (NumberFormatException e) {
                     Log.d(TAG, "NumberFormatException " + e);
@@ -269,7 +316,7 @@
         }
 
         private void init() {
-            int currentValue = Settings.System.getInt(getActivity().getContentResolver(),
+            int currentValue = Settings.System.getInt(getContext().getContentResolver(),
                             Settings.System.CUSTOM_BUTTON_BRIGHTNESS, 100);
 
             mBacklightBar.setProgress(brightnessToProgress(currentValue));
@@ -328,7 +375,7 @@
                             mBacklightBar.setProgress(brightnessToProgress(newBrightness));
                         }
                     } catch (NumberFormatException e) {
-                        //ignored, ok is false anyway
+                        //ignored, ok is false ayway
                     }
 
                     Button okButton = mManualBrightnessDialog.getButton(DialogInterface.BUTTON_POSITIVE);
@@ -339,5 +386,100 @@
             });
         }
     }
+    private class ButtonTimeoutDialog extends AlertDialog implements DialogInterface.OnClickListener {
+        private SeekBar mTimeoutBar;
+        private TextView mTimeoutValue;
+        private int mCurrentTimeout;
+        private boolean mIsDragging = false;
+
+        public ButtonTimeoutDialog(Context context) {
+            super(context);
+
+            // to allow initial summary setting
+            mCurrentTimeout = Settings.System.getInt(getContext().getContentResolver(),
+                            Settings.System.BUTTON_BACKLIGHT_TIMEOUT, 0);
+        }
+
+        @Override
+        protected void onCreate(Bundle savedInstanceState) {
+            final View v = getLayoutInflater().inflate(R.layout.button_timeout, null);
+            final Context context = getContext();
+
+            mTimeoutBar = (SeekBar) v.findViewById(R.id.timeout_seekbar);
+            mTimeoutValue = (TextView) v.findViewById(R.id.timeout_value);
+            mTimeoutBar.setMax(30);
+
+            setTitle(R.string.dialog_button_timeout_title);
+            setCancelable(true);
+            setView(v);
+
+            initListeners();
+            init();
+
+            setButton(DialogInterface.BUTTON_POSITIVE, context.getString(R.string.ok), this);
+            setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.cancel), this);
+
+            super.onCreate(savedInstanceState);
+        }
+
+        private void init() {
+            mCurrentTimeout = Settings.System.getInt(getContext().getContentResolver(),
+                            Settings.System.BUTTON_BACKLIGHT_TIMEOUT, 0);
+
+            mTimeoutBar.setProgress(mCurrentTimeout);
+            mTimeoutValue.setText(getTimeoutString());
+        }
+
+        @Override
+        public void onClick(DialogInterface dialog, int which) {
+            if (which == DialogInterface.BUTTON_POSITIVE) {
+                try {
+                    Settings.System.putInt(getContext().getContentResolver(),
+                            Settings.System.BUTTON_BACKLIGHT_TIMEOUT, mCurrentTimeout);
+                    updateSummary();
+                } catch (NumberFormatException e) {
+                    Log.d(TAG, "NumberFormatException " + e);
+                }
+            }
+        }
+
+        private void updateSummary() {
+            if (mCurrentTimeout == 0) {
+                mButtonTimout.setSummary(R.string.button_timeout_disabled);
+            } else {
+                mButtonTimout.setSummary(getContext().getString(R.string.button_timeout_enabled,
+                            getTimeoutString()));
+            }
+        }
+
+        private String getTimeoutString() {
+            if (mCurrentTimeout == 0) {
+                return getContext().getResources().getString(R.string.button_timeout_disabled);
+            } else {
+                return getContext().getResources().getQuantityString(
+                    R.plurals.button_timeout_time, mCurrentTimeout, mCurrentTimeout);
+            }
+        }
+
+        private void initListeners() {
+            mTimeoutBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
+                @Override
+                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+                    if (mIsDragging) {
+                        mCurrentTimeout = mTimeoutBar.getProgress();
+                        mTimeoutValue.setText(getTimeoutString());
+                    }
+                }
+                @Override
+                public void onStartTrackingTouch(SeekBar seekBar) {
+                    mIsDragging = true;
+                }
+                @Override
+                public void onStopTrackingTouch(SeekBar seekBar) {
+                    mIsDragging = false;
+                }
+            });
+        }
+    }
 }