Settings: Add high touch sensitivity toggle

* Like https://review.lineageos.org/c/LineageOS/android_packages_apps_Settings/+/321581
  but in an AOSP compatible way

Change-Id: Ib8712d2d0c0fe4396dbea7c7fc130e0c253d3207
diff --git a/Android.bp b/Android.bp
index b607166..a98db38 100644
--- a/Android.bp
+++ b/Android.bp
@@ -104,6 +104,7 @@
         "settings-telephony-protos-lite",
         "statslog-settings",
         "vendor.lineage.fastcharge-V1.0-java",
+        "vendor.lineage.touch-V1.0-java",
     ],
 
     plugins: ["androidx.room_room-compiler-plugin"],
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 2d3b3fd..eaaf327 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -230,6 +230,13 @@
             </intent-filter>
         </receiver>
 
+        <receiver android:name="org.leafos.settings.BootCompletedReceiver"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED"/>
+            </intent-filter>
+        </receiver>
+
         <activity android:name=".SubSettings"
                   android:exported="false"
                   android:theme="@style/Theme.SubSettings"
diff --git a/res/values/cm_strings.xml b/res/values/cm_strings.xml
index f97cadb..2f1dd56 100644
--- a/res/values/cm_strings.xml
+++ b/res/values/cm_strings.xml
@@ -18,4 +18,8 @@
     <!-- FastCharge feature -->
     <string name="fast_charging_title">Fast charging</string>
     <string name="fast_charging_summary">Disable to reduce the heat produced by the device while charging or to extend the lifespan of the battery</string>
+
+    <!-- High touch sensitivity -->
+    <string name="high_touch_sensitivity_title">High touch sensitivity</string>
+    <string name="high_touch_sensitivity_summary">Increase touchscreen sensitivity so it can be used while wearing gloves</string>
 </resources>
diff --git a/res/xml/display_settings.xml b/res/xml/display_settings.xml
index 2df360d..2fbbc01 100644
--- a/res/xml/display_settings.xml
+++ b/res/xml/display_settings.xml
@@ -170,6 +170,13 @@
             android:title="@string/tap_to_wake"
             android:summary="@string/tap_to_wake_summary"/>
 
+        <SwitchPreference
+            android:key="high_touch_sensitivity_enable"
+            android:title="@string/high_touch_sensitivity_title"
+            android:summary="@string/high_touch_sensitivity_summary"
+            android:defaultValue="false"
+            settings:controller="org.leafos.settings.display.HighTouchSensitivityPreferenceController"/>
+
         <ListPreference
             android:key="theme"
             android:title="@string/device_theme"
diff --git a/src/org/leafos/settings/BootCompletedReceiver.java b/src/org/leafos/settings/BootCompletedReceiver.java
new file mode 100644
index 0000000..638d676
--- /dev/null
+++ b/src/org/leafos/settings/BootCompletedReceiver.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2022 The LeafOS 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.
+ */
+
+package org.leafos.settings;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.provider.Settings;
+
+import org.leafos.settings.display.HighTouchSensitivityPreferenceController;
+
+public class BootCompletedReceiver extends BroadcastReceiver {
+
+    @Override
+    public void onReceive(final Context context, Intent intent) {
+        if (Settings.System.getInt(context.getContentResolver(), Settings.System.HIGH_TOUCH_SENSITIVITY_ENABLE, 0) != 0) {
+            HighTouchSensitivityPreferenceController.enableStatic(true);
+        }
+    }
+
+}
diff --git a/src/org/leafos/settings/controller/BaseSystemSwitchPreferenceController.java b/src/org/leafos/settings/controller/BaseSystemSwitchPreferenceController.java
new file mode 100644
index 0000000..a57b48f
--- /dev/null
+++ b/src/org/leafos/settings/controller/BaseSystemSwitchPreferenceController.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2022 The LeafOS 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.
+ */
+package org.leafos.settings.controller;
+
+import android.content.Context;
+import android.provider.Settings;
+import android.text.TextUtils;
+
+import androidx.preference.Preference;
+import androidx.preference.SwitchPreference;
+
+import com.android.settings.R;
+import com.android.settings.core.TogglePreferenceController;
+
+public class BaseSystemSwitchPreferenceController extends TogglePreferenceController implements
+        Preference.OnPreferenceChangeListener  {
+
+    private SwitchPreference mPreference;
+
+    public BaseSystemSwitchPreferenceController(Context context, String key) {
+        super(context, key);
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        mPreference = (SwitchPreference)preference;
+        super.updateState(preference);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE;
+    }
+
+    @Override
+    public boolean isSliceable() {
+        return mPreference != null;
+    }
+
+    @Override
+    public boolean isPublicSlice() {
+        return true;
+    }
+
+    @Override
+    public int getSliceHighlightMenuRes() {
+        return R.string.menu_key_display;
+    }
+
+    @Override
+    public boolean isChecked() {
+        return Settings.System.getInt(mContext.getContentResolver(), getPreferenceKey(),
+                mPreference.isChecked() ? 1 : 0) != 0;
+    }
+
+    @Override
+    public boolean setChecked(boolean isChecked) {
+        Settings.System.putInt(mContext.getContentResolver(), getPreferenceKey(), isChecked ? 1 : 0);
+        return true;
+    }
+}
diff --git a/src/org/leafos/settings/display/HighTouchSensitivityPreferenceController.java b/src/org/leafos/settings/display/HighTouchSensitivityPreferenceController.java
new file mode 100644
index 0000000..4cddd93
--- /dev/null
+++ b/src/org/leafos/settings/display/HighTouchSensitivityPreferenceController.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2022 The LeafOS 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.
+ */
+package org.leafos.settings.display;
+
+import android.content.Context;
+import android.os.RemoteException;
+
+import java.util.NoSuchElementException;
+
+import org.leafos.settings.controller.BaseSystemSwitchPreferenceController;
+
+import vendor.lineage.touch.V1_0.IGloveMode;
+
+public class HighTouchSensitivityPreferenceController extends BaseSystemSwitchPreferenceController  {
+
+    private IGloveMode mGloveMode;
+
+    public HighTouchSensitivityPreferenceController(Context context, String key) {
+        super(context, key);
+
+        try {
+            mGloveMode = IGloveMode.getService();
+        } catch (RemoteException ex) {
+            ex.printStackTrace();
+        } catch (NoSuchElementException ex) {
+            // service not available
+        }
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return mGloveMode != null ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
+    }
+
+    @Override
+    public boolean setChecked(boolean isChecked) {
+        try {
+            mGloveMode.setEnabled(isChecked);
+            return super.setChecked(isChecked);
+        } catch (RemoteException ex) {
+            ex.printStackTrace();
+        }
+        return false;
+    }
+
+    public static void enableStatic(boolean enable) {
+        try {
+            IGloveMode gloveMode = IGloveMode.getService();
+            gloveMode.setEnabled(enable);
+        } catch (RemoteException ex) {
+            ex.printStackTrace();
+        } catch (NoSuchElementException ex) {
+            // service not available
+        }
+    }
+}