Move power key torch feature into its own page

Change-Id: I6a8b40b9de3c7f5a2d7a10fd564d82db597c5715
Signed-off-by: Joey <bevilacquajoey@gmail.com>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 01ecd0b..bf1d5ef 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -81,6 +81,7 @@
     <!-- Description of setting that controls gesture to open camera by double tapping the power button [CHAR LIMIT=NONE] -->
     <string name="camera_double_tap_power_gesture_desc">Quickly open camera without unlocking your screen</string>
 
+    <string name="gestures_power_key_torch">Quick torch</string>
     <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>
diff --git a/res/xml/button_settings.xml b/res/xml/button_settings.xml
index c104c90..67f8086 100644
--- a/res/xml/button_settings.xml
+++ b/res/xml/button_settings.xml
@@ -27,22 +27,6 @@
             android:key="power_menu"
             android:title="@string/power_menu_title"
             android:fragment="com.shiftos.shiftparts.input.PowerMenuActions" />
-
-        <shiftos.preference.ShiftSystemSettingSwitchPreference
-            android:key="torch_long_press_power_gesture"
-            android:title="@string/torch_long_press_power_gesture_title"
-            android:summary="@string/torch_long_press_power_gesture_desc"
-            android:defaultValue="false"
-            shift: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/res/xml/gestures_power_key_torch.xml b/res/xml/gestures_power_key_torch.xml
new file mode 100644
index 0000000..ead47c0
--- /dev/null
+++ b/res/xml/gestures_power_key_torch.xml
@@ -0,0 +1,37 @@
+<?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.
+-->
+
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:shift="http://schemas.android.com/apk/res/shiftos.platform"
+    android:key="gesture_power_key_torch"
+    android:title="@string/gestures_power_key_torch">
+
+    <shiftos.preference.ShiftSystemSettingSwitchPreference
+        android:key="torch_long_press_power_gesture"
+        android:title="@string/torch_long_press_power_gesture_title"
+        android:summary="@string/torch_long_press_power_gesture_desc"
+        android:defaultValue="false"
+        shift: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" />
+</PreferenceScreen>
diff --git a/res/xml/parts_catalog.xml b/res/xml/parts_catalog.xml
index c820ccf..581cb97 100644
--- a/res/xml/parts_catalog.xml
+++ b/res/xml/parts_catalog.xml
@@ -54,4 +54,9 @@
           android:title="@string/advanced_title"
           android:fragment="com.shiftos.shiftparts.advanced.AdvancedSettings"
           shift:xmlRes="@xml/advanced_settings" />
+
+    <part android:key="gesture_power_key_torch"
+          android:title="@string/gestures_power_key_torch"
+          android:fragment="com.shiftos.shiftparts.gestures.PowerKeyTorchGestureSettings"
+          shift:xmlRes="@xml/gestures_power_key_torch" />
 </parts-catalog>
diff --git a/src/com/shiftos/shiftparts/gestures/PowerKeyTorchGestureSettings.java b/src/com/shiftos/shiftparts/gestures/PowerKeyTorchGestureSettings.java
new file mode 100644
index 0000000..9ef6fee
--- /dev/null
+++ b/src/com/shiftos/shiftparts/gestures/PowerKeyTorchGestureSettings.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2020 Shift GmbH
+ *
+ * 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.
+ */
+package com.shiftos.shiftparts.gestures;
+
+import android.content.Context;
+import android.content.ContentResolver;
+import android.content.res.Resources;
+import android.os.Bundle;
+import android.view.KeyCharacterMap;
+import android.view.KeyEvent;
+import android.util.ArraySet;
+
+import androidx.preference.ListPreference;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+import androidx.preference.SwitchPreference;
+
+import com.shiftos.shiftparts.R;
+import com.shiftos.shiftparts.SettingsPreferenceFragment;
+import com.shiftos.shiftparts.search.BaseSearchIndexProvider;
+import com.shiftos.shiftparts.search.Searchable;
+import com.shiftos.shiftparts.utils.DeviceUtils;
+
+import static com.shiftos.internal.util.DeviceKeysConstants.*;
+
+import java.util.Set;
+
+import shiftos.providers.ShiftSettings;
+
+public class PowerKeyTorchGestureSettings extends SettingsPreferenceFragment implements
+        Preference.OnPreferenceChangeListener, Searchable {
+
+    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 SwitchPreference mTorchLongPressPowerGesture;
+    private ListPreference mTorchLongPressPowerTimeout;
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        addPreferencesFromResource(R.xml.gestures_power_key_torch);
+
+        final Resources res = getResources();
+        final ContentResolver resolver = getActivity().getContentResolver();
+        final PreferenceScreen prefScreen = getPreferenceScreen();
+
+        final boolean hasPowerKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_POWER);
+
+        mTorchLongPressPowerGesture =
+                (SwitchPreference) findPreference(KEY_TORCH_LONG_PRESS_POWER_GESTURE);
+        final int torchLongPressPowerTimeout = ShiftSettings.System.getInt(resolver,
+                ShiftSettings.System.TORCH_LONG_PRESS_POWER_TIMEOUT, 0);
+        mTorchLongPressPowerTimeout = initList(KEY_TORCH_LONG_PRESS_POWER_TIMEOUT,
+                torchLongPressPowerTimeout);
+
+        if (!hasPowerKey || !DeviceUtils.deviceSupportsFlashLight(getActivity())) {
+            prefScreen.removePreference(mTorchLongPressPowerGesture);
+            prefScreen.removePreference(mTorchLongPressPowerTimeout);
+        }
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        if (preference == mTorchLongPressPowerTimeout) {
+            handleListChange(mTorchLongPressPowerTimeout, newValue,
+                    ShiftSettings.System.TORCH_LONG_PRESS_POWER_TIMEOUT);
+            return true;
+        }
+        return false;
+    }
+
+    private ListPreference initList(String key, int value) {
+        ListPreference list = (ListPreference) getPreferenceScreen().findPreference(key);
+        if (list == null) return null;
+        list.setValue(Integer.toString(value));
+        list.setSummary(list.getEntry());
+        list.setOnPreferenceChangeListener(this);
+        return list;
+    }
+
+    private void handleListChange(ListPreference pref, Object newValue, String setting) {
+        String value = (String) newValue;
+        int index = pref.findIndexOfValue(value);
+        pref.setSummary(pref.getEntries()[index]);
+        ShiftSettings.System.putInt(getContentResolver(), setting, Integer.valueOf(value));
+    }
+
+    public static final Searchable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+            new BaseSearchIndexProvider() {
+
+        @Override
+        public Set<String> getNonIndexableKeys(Context context) {
+            final Set<String> result = new ArraySet<String>();
+            final boolean hasPowerKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_POWER);
+
+            if (!hasPowerKey) {
+                result.add(KEY_TORCH_LONG_PRESS_POWER_GESTURE);
+                result.add(KEY_TORCH_LONG_PRESS_POWER_TIMEOUT);
+            }
+            return result;
+        }
+    };
+}
diff --git a/src/com/shiftos/shiftparts/input/ButtonSettings.java b/src/com/shiftos/shiftparts/input/ButtonSettings.java
index eadf161..7328bff 100644
--- a/src/com/shiftos/shiftparts/input/ButtonSettings.java
+++ b/src/com/shiftos/shiftparts/input/ButtonSettings.java
@@ -64,10 +64,6 @@
     private static final String KEY_ASSIST_LONG_PRESS = "hardware_keys_assist_long_press";
     private static final String KEY_POWER_END_CALL = "power_end_call";
     private static final String KEY_VOLUME_MUSIC_CONTROLS = "volbtn_music_controls";
-    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_ASSIST = "assist_key";
@@ -82,8 +78,6 @@
     private SwitchPreference mVolumeWakeScreen;
     private SwitchPreference mVolumeMusicControls;
     private SwitchPreference mPowerEndCall;
-    private SwitchPreference mTorchLongPressPowerGesture;
-    private ListPreference mTorchLongPressPowerTimeout;
 
     private Handler mHandler;
 
@@ -124,14 +118,6 @@
         // Power button ends calls.
         mPowerEndCall = (SwitchPreference) findPreference(KEY_POWER_END_CALL);
 
-        // Long press power while display is off to activate torchlight
-        mTorchLongPressPowerGesture =
-                (SwitchPreference) findPreference(KEY_TORCH_LONG_PRESS_POWER_GESTURE);
-        final int torchLongPressPowerTimeout = ShiftSettings.System.getInt(resolver,
-                ShiftSettings.System.TORCH_LONG_PRESS_POWER_TIMEOUT, 0);
-        mTorchLongPressPowerTimeout = initList(KEY_TORCH_LONG_PRESS_POWER_TIMEOUT,
-                torchLongPressPowerTimeout);
-
         mHandler = new Handler();
 
         if (hasPowerKey) {
@@ -139,10 +125,6 @@
                 powerCategory.removePreference(mPowerEndCall);
                 mPowerEndCall = null;
             }
-            if (!DeviceUtils.deviceSupportsFlashLight(getActivity())) {
-                powerCategory.removePreference(mTorchLongPressPowerGesture);
-                powerCategory.removePreference(mTorchLongPressPowerTimeout);
-            }
         } else {
             prefScreen.removePreference(powerCategory);
         }
@@ -257,13 +239,6 @@
         ShiftSettings.System.putInt(getContentResolver(), setting, Integer.valueOf(value));
     }
 
-    private void handleSystemListChange(ListPreference pref, Object newValue, String setting) {
-        String value = (String) newValue;
-        int index = pref.findIndexOfValue(value);
-        pref.setSummary(pref.getEntries()[index]);
-        Settings.System.putInt(getContentResolver(), setting, Integer.valueOf(value));
-    }
-
     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) {
         if (preference == mAssistPressAction) {
@@ -274,10 +249,6 @@
             handleListChange(mAssistLongPressAction, newValue,
                     ShiftSettings.System.KEY_ASSIST_LONG_PRESS_ACTION);
             return true;
-        } else if (preference == mTorchLongPressPowerTimeout) {
-            handleListChange(mTorchLongPressPowerTimeout, newValue,
-                    ShiftSettings.System.TORCH_LONG_PRESS_POWER_TIMEOUT);
-            return true;
         }
         return false;
     }