ButtonSettings: Add a setting to swap capacitive keys

Change-Id: Ibfcd698989c6496e45ca756a7f279216fe671329
diff --git a/res/values/strings.xml b/res/values/strings.xml
index ad53e96..eb0d487 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
 	Copyright (C) 2015 The CyanogenMod Project
-	              2017-2020 The LineageOS Project
+	              2017-2021 The LineageOS Project
 
 	Licensed under the Apache License, Version 2.0 (the "License");
 	you may not use this file except in compliance with the License.
@@ -233,6 +233,10 @@
     <string name="disable_navkeys_title">Enable on-screen nav bar</string>
     <string name="disable_navkeys_summary">Enable on-screen navigation bar and disable hardware buttons</string>
 
+    <!-- Buttons - Swap capacitive keys -->
+    <string name="swap_capacitive_keys_title">Swap capacitive buttons</string>
+    <string name="swap_capacitive_keys_summary">Swap the order of recents and back buttons</string>
+
     <!-- Navigation Bar -->
     <string name="navigation_bar_category">Navigation bar</string>
     <string name="navigation_bar_invert_layout_title">Invert layout</string>
diff --git a/res/xml/button_settings.xml b/res/xml/button_settings.xml
index 828c310..f4880f1 100644
--- a/res/xml/button_settings.xml
+++ b/res/xml/button_settings.xml
@@ -25,6 +25,12 @@
         android:summary="@string/disable_navkeys_summary"
         android:defaultValue="false" />
 
+    <SwitchPreference
+        android:key="swap_capacitive_keys"
+        android:title="@string/swap_capacitive_keys_title"
+        android:summary="@string/swap_capacitive_keys_summary"
+        android:defaultValue="false" />
+
     <org.lineageos.lineageparts.input.ButtonBacklightBrightness
         android:key="button_backlight"
         android:title="@string/button_backlight_title"
diff --git a/src/org/lineageos/lineageparts/BootReceiver.java b/src/org/lineageos/lineageparts/BootReceiver.java
index 30d75f5..6b0ab87 100644
--- a/src/org/lineageos/lineageparts/BootReceiver.java
+++ b/src/org/lineageos/lineageparts/BootReceiver.java
@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2012 The CyanogenMod Project
+ *               2017-2019,2021 The LineageOS project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -40,6 +41,7 @@
             setRestoredTunable(ctx);
         }
 
+        ButtonSettings.restoreKeySwapper(ctx);
         TouchscreenGestureSettings.restoreTouchscreenGestureStates(ctx);
 
         // Extract the contributors database
diff --git a/src/org/lineageos/lineageparts/input/ButtonSettings.java b/src/org/lineageos/lineageparts/input/ButtonSettings.java
index b46a6f3..9af549e 100644
--- a/src/org/lineageos/lineageparts/input/ButtonSettings.java
+++ b/src/org/lineageos/lineageparts/input/ButtonSettings.java
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2016 The CyanogenMod project
- *               2017-2020 The LineageOS project
+ *               2017-2021 The LineageOS project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.content.res.Resources;
+import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.RemoteException;
@@ -40,6 +41,7 @@
 import androidx.preference.ListPreference;
 import androidx.preference.Preference;
 import androidx.preference.PreferenceCategory;
+import androidx.preference.PreferenceManager;
 import androidx.preference.PreferenceScreen;
 import androidx.preference.SwitchPreference;
 
@@ -101,6 +103,7 @@
             "torch_long_press_power_timeout";
     private static final String KEY_CLICK_PARTIAL_SCREENSHOT =
             "click_partial_screenshot";
+    private static final String KEY_SWAP_CAPACITIVE_KEYS = "swap_capacitive_keys";
 
     private static final String CATEGORY_POWER = "power_key";
     private static final String CATEGORY_HOME = "home_key";
@@ -140,15 +143,20 @@
     private SwitchPreference mHomeAnswerCall;
     private SwitchPreference mTorchLongPressPowerGesture;
     private ListPreference mTorchLongPressPowerTimeout;
+    private SwitchPreference mSwapCapacitiveKeys;
 
     private PreferenceCategory mNavigationPreferencesCat;
 
     private Handler mHandler;
 
+    private LineageHardwareManager mHardware;
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
+        mHardware = LineageHardwareManager.getInstance(getActivity());
+
         addPreferencesFromResource(R.xml.button_settings);
 
         final Resources res = getResources();
@@ -245,6 +253,7 @@
             // Remove keys that can be provided by the navbar
             updateDisableNavkeysOption();
             mNavigationPreferencesCat.setEnabled(mDisableNavigationKeys.isChecked());
+            mDisableNavigationKeys.setDisableDependentsState(true);
         } else {
             prefScreen.removePreference(mDisableNavigationKeys);
         }
@@ -434,6 +443,14 @@
             }
         }
 
+        mSwapCapacitiveKeys = findPreference(KEY_SWAP_CAPACITIVE_KEYS);
+        if (mSwapCapacitiveKeys != null && !isKeySwapperSupported(getActivity())) {
+            prefScreen.removePreference(mSwapCapacitiveKeys);
+        } else {
+            mSwapCapacitiveKeys.setOnPreferenceChangeListener(this);
+            mSwapCapacitiveKeys.setDependency(KEY_DISABLE_NAV_KEYS);
+        }
+
         // Override key actions on Go devices in order to hide any unsupported features
         if (ActivityManager.isLowRamDeviceStatic()) {
             String[] actionEntriesGo = res.getStringArray(R.array.hardware_keys_action_entries_go);
@@ -586,6 +603,9 @@
             handleListChange(mEdgeLongSwipeAction, newValue,
                     LineageSettings.System.KEY_EDGE_LONG_SWIPE_ACTION);
             return true;
+        } else if (preference == mSwapCapacitiveKeys) {
+            mHardware.set(LineageHardwareManager.FEATURE_KEY_SWAP, (Boolean) newValue);
+            return true;
         }
         return false;
     }
@@ -692,6 +712,11 @@
         return hardware.isSupported(LineageHardwareManager.FEATURE_KEY_DISABLE);
     }
 
+    private static boolean isKeySwapperSupported(Context context) {
+        final LineageHardwareManager hardware = LineageHardwareManager.getInstance(context);
+        return hardware.isSupported(LineageHardwareManager.FEATURE_KEY_SWAP);
+    }
+
     public static void restoreKeyDisabler(Context context) {
         if (!isKeyDisablerSupported(context)) {
             return;
@@ -703,6 +728,17 @@
         writeDisableNavkeysOption(context, enabled);
     }
 
+    public static void restoreKeySwapper(Context context) {
+        if (!isKeySwapperSupported(context)) {
+            return;
+        }
+
+        final SharedPreferences preferences =
+                PreferenceManager.getDefaultSharedPreferences(context);
+        final LineageHardwareManager hardware = LineageHardwareManager.getInstance(context);
+        hardware.set(LineageHardwareManager.FEATURE_KEY_SWAP,
+                preferences.getBoolean(KEY_SWAP_CAPACITIVE_KEYS, false));
+    }
 
     @Override
     public boolean onPreferenceTreeClick(Preference preference) {
@@ -856,6 +892,10 @@
                 result.add(KEY_DISABLE_NAV_KEYS);
             }
 
+            if (!isKeySwapperSupported(context)) {
+                result.add(KEY_SWAP_CAPACITIVE_KEYS);
+            }
+
             if (!DeviceUtils.hasButtonBacklightSupport(context)
                     && !DeviceUtils.hasKeyboardBacklightSupport(context)) {
                 result.add(KEY_BUTTON_BACKLIGHT);