[3/3] Torch long press power: add auto-off function

Change-Id: I701cd239a355c6e4ddbed858262d89e21c5b8fae
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index 23d3525..21af519 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -322,4 +322,20 @@
         <item>8</item>
         <item>9</item>
     </string-array>
+
+    <string-array name="torch_long_press_power_timeout_entries">
+        <item>@string/auto_power_save_never</item>
+        <item>1 minute</item>
+        <item>2 minutes</item>
+        <item>5 minutes</item>
+        <item>10 minutes</item>
+    </string-array>
+
+    <string-array name="torch_long_press_power_timeout_values" translatable="false">
+        <item>0</item>
+        <item>60</item>
+        <item>120</item>
+        <item>300</item>
+        <item>600</item>
+    </string-array>
 </resources>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 33cb8e5..ba2ba14 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -259,6 +259,7 @@
 
     <string name="torch_long_press_power_gesture_title">Long press for torch</string>
     <string name="torch_long_press_power_gesture_desc">Activate the torch by long pressing the power button while the display is off</string>
+    <string name="torch_long_press_power_timeout_title">Automatically turn torch off</string>
 
     <!-- Profiles -->
     <string name="profile_menu_delete_title">Delete</string>
diff --git a/res/xml/button_settings.xml b/res/xml/button_settings.xml
index ad28e0c..9b71607 100644
--- a/res/xml/button_settings.xml
+++ b/res/xml/button_settings.xml
@@ -99,6 +99,15 @@
             android:defaultValue="false"
             cm:requiresConfig="@*android:bool/config_supportLongPressPowerWhenNonInteractive" />
 
+        <ListPreference
+            android:key="torch_long_press_power_timeout"
+            android:dialogTitle="@string/torch_long_press_power_timeout_title"
+            android:title="@string/torch_long_press_power_timeout_title"
+            android:entries="@array/torch_long_press_power_timeout_entries"
+            android:entryValues="@array/torch_long_press_power_timeout_values"
+            android:dependency="torch_long_press_power_gesture"
+            android:persistent="false" />
+
     </PreferenceCategory>
 
     <PreferenceCategory
diff --git a/src/org/cyanogenmod/cmparts/input/ButtonSettings.java b/src/org/cyanogenmod/cmparts/input/ButtonSettings.java
index f87d4fc..ac6e15c 100644
--- a/src/org/cyanogenmod/cmparts/input/ButtonSettings.java
+++ b/src/org/cyanogenmod/cmparts/input/ButtonSettings.java
@@ -78,6 +78,8 @@
     private static final String KEY_VOLUME_CONTROL_RING_STREAM = "volume_keys_control_ring_stream";
     private static final String KEY_TORCH_LONG_PRESS_POWER_GESTURE =
             "torch_long_press_power_gesture";
+    private static final String KEY_TORCH_LONG_PRESS_POWER_TIMEOUT =
+            "torch_long_press_power_timeout";
 
     private static final String CATEGORY_POWER = "power_key";
     private static final String CATEGORY_HOME = "home_key";
@@ -152,6 +154,7 @@
     private SwitchPreference mPowerEndCall;
     private SwitchPreference mHomeAnswerCall;
     private SwitchPreference mTorchLongPressPowerGesture;
+    private ListPreference mTorchLongPressPowerTimeout;
 
     private PreferenceCategory mNavigationPreferencesCat;
 
@@ -213,6 +216,10 @@
         // Long press power while display is off to activate torchlight
         mTorchLongPressPowerGesture =
                 (SwitchPreference) findPreference(KEY_TORCH_LONG_PRESS_POWER_GESTURE);
+        final int torchLongPressPowerTimeout = CMSettings.System.getInt(resolver,
+                CMSettings.System.TORCH_LONG_PRESS_POWER_TIMEOUT, 0);
+        mTorchLongPressPowerTimeout = initList(KEY_TORCH_LONG_PRESS_POWER_TIMEOUT,
+                torchLongPressPowerTimeout);
 
         // Home button answers calls.
         mHomeAnswerCall = (SwitchPreference) findPreference(KEY_HOME_ANSWER_CALL);
@@ -288,6 +295,7 @@
             }
             if (!QSUtils.deviceSupportsFlashLight(getActivity())) {
                 powerCategory.removePreference(mTorchLongPressPowerGesture);
+                powerCategory.removePreference(mTorchLongPressPowerTimeout);
             }
         } else {
             prefScreen.removePreference(powerCategory);
@@ -634,6 +642,10 @@
             CMSettings.Secure.putString(getContentResolver(),
                     CMSettings.Secure.RECENTS_LONG_PRESS_ACTIVITY, putString);
             return true;
+        } else if (preference == mTorchLongPressPowerTimeout) {
+            handleListChange(mTorchLongPressPowerTimeout, newValue,
+                    CMSettings.System.TORCH_LONG_PRESS_POWER_TIMEOUT);
+            return true;
         }
         return false;
     }