LineageParts: Add "Enable taskbar" toggle

Change-Id: Ie0ea98898c887a70f44511b35ac95f5d2750545d
diff --git a/Android.bp b/Android.bp
index 198dfd7..dd35ae9 100644
--- a/Android.bp
+++ b/Android.bp
@@ -41,6 +41,7 @@
         "androidx.recyclerview_recyclerview",
         "jsr305",
         "org.lineageos.platform.internal",
+        "SystemUISharedLib",
     ],
 
     required: [
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 9fe4181..0915dfd 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -242,6 +242,7 @@
 
     <!-- Navigation Bar -->
     <string name="navigation_bar_category">Navigation bar</string>
+    <string name="navigation_bar_enable_taskbar_title">Enable taskbar</string>
     <string name="navigation_bar_invert_layout_title">Invert layout</string>
     <string name="navigation_bar_invert_layout_summary">Inverts the layout of the navigation bar and other items, such as the IME switcher</string>
     <string name="navigation_bar_left_summary">Place the navigation bar on the left side of the screen in landscape mode</string>
diff --git a/res/xml/button_settings.xml b/res/xml/button_settings.xml
index 09f8ea7..2c1f967 100644
--- a/res/xml/button_settings.xml
+++ b/res/xml/button_settings.xml
@@ -43,6 +43,10 @@
         android:key="navigation_bar_category"
         android:title="@string/navigation_bar_category" >
 
+        <SwitchPreference
+                android:key="enable_taskbar"
+                android:title="@string/navigation_bar_enable_taskbar_title" />
+
         <lineageos.preference.LineageSystemSettingSwitchPreference
                 android:key="navigation_bar_menu_arrow_keys"
                 android:title="@string/navigation_bar_arrow_keys_title"
diff --git a/src/org/lineageos/lineageparts/input/ButtonSettings.java b/src/org/lineageos/lineageparts/input/ButtonSettings.java
index 0bbded0..2ee2d7f 100644
--- a/src/org/lineageos/lineageparts/input/ButtonSettings.java
+++ b/src/org/lineageos/lineageparts/input/ButtonSettings.java
@@ -17,7 +17,9 @@
 
 package org.lineageos.lineageparts.input;
 
+import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON;
 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON_OVERLAY;
+import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL_OVERLAY;
 
 import android.app.ActivityManager;
 import android.content.ComponentName;
@@ -49,6 +51,8 @@
 import androidx.preference.PreferenceScreen;
 import androidx.preference.SwitchPreference;
 
+import static com.android.systemui.shared.recents.utilities.Utilities.isTablet;
+
 import org.lineageos.lineageparts.R;
 import org.lineageos.lineageparts.SettingsPreferenceFragment;
 import org.lineageos.lineageparts.search.BaseSearchIndexProvider;
@@ -110,6 +114,8 @@
     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 KEY_NAV_BAR_INVERSE = "sysui_nav_bar_inverse";
+    private static final String KEY_ENABLE_TASKBAR = "enable_taskbar";
 
     private static final String CATEGORY_POWER = "power_key";
     private static final String CATEGORY_HOME = "home_key";
@@ -152,6 +158,8 @@
     private SwitchPreference mTorchLongPressPowerGesture;
     private ListPreference mTorchLongPressPowerTimeout;
     private SwitchPreference mSwapCapacitiveKeys;
+    private SwitchPreference mNavBarInverse;
+    private SwitchPreference mEnableTaskbar;
 
     private PreferenceCategory mNavigationPreferencesCat;
 
@@ -474,6 +482,21 @@
             mSwapCapacitiveKeys.setDependency(KEY_DISABLE_NAV_KEYS);
         }
 
+        mNavBarInverse = findPreference(KEY_NAV_BAR_INVERSE);
+
+        mEnableTaskbar = findPreference(KEY_ENABLE_TASKBAR);
+        if (mEnableTaskbar != null) {
+            if (!isTablet(getContext()) || !hasNavigationBar()) {
+                mNavigationPreferencesCat.removePreference(mEnableTaskbar);
+            } else {
+                mEnableTaskbar.setOnPreferenceChangeListener(this);
+                mEnableTaskbar.setChecked(LineageSettings.System.getInt(getContentResolver(),
+                        LineageSettings.System.ENABLE_TASKBAR,
+                        isTablet(getContext()) ? 1 : 0) == 1);
+                toggleTaskBarDependencies(mEnableTaskbar.isChecked());
+            }
+        }
+
         // 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);
@@ -642,10 +665,60 @@
         } else if (preference == mSwapCapacitiveKeys) {
             mHardware.set(LineageHardwareManager.FEATURE_KEY_SWAP, (Boolean) newValue);
             return true;
+        } else if (preference == mEnableTaskbar) {
+            toggleTaskBarDependencies((Boolean) newValue);
+            if ((Boolean) newValue && is2ButtonNavigationEnabled(getContext())) {
+                // Let's switch to gestural mode if user previously had 2 buttons enabled.
+                setButtonNavigationMode(NAV_BAR_MODE_GESTURAL_OVERLAY);
+            }
+            LineageSettings.System.putInt(getContentResolver(),
+                    LineageSettings.System.ENABLE_TASKBAR, ((Boolean) newValue) ? 1 : 0);
+            return true;
         }
         return false;
     }
 
+    private static boolean is2ButtonNavigationEnabled(Context context) {
+        return NAV_BAR_MODE_2BUTTON == context.getResources().getInteger(
+                com.android.internal.R.integer.config_navBarInteractionMode);
+    }
+
+    private static void setButtonNavigationMode(String overlayPackage) {
+        IOverlayManager overlayManager = IOverlayManager.Stub.asInterface(
+                ServiceManager.getService(Context.OVERLAY_SERVICE));
+        try {
+            overlayManager.setEnabledExclusiveInCategory(overlayPackage, UserHandle.USER_CURRENT);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    private void toggleTaskBarDependencies(boolean enabled) {
+        if (mNavigationArrowKeys != null) {
+            mNavigationArrowKeys.setEnabled(!enabled);
+        }
+
+        if (mNavBarInverse != null) {
+            mNavBarInverse.setEnabled(!enabled);
+        }
+
+        if (mNavigationBackLongPressAction != null) {
+            mNavigationBackLongPressAction.setEnabled(!enabled);
+        }
+
+        if (mNavigationHomeLongPressAction != null) {
+            mNavigationHomeLongPressAction.setEnabled(!enabled);
+        }
+
+        if (mNavigationHomeDoubleTapAction != null) {
+            mNavigationHomeDoubleTapAction.setEnabled(!enabled);
+        }
+
+        if (mNavigationAppSwitchLongPressAction != null) {
+            mNavigationAppSwitchLongPressAction.setEnabled(!enabled);
+        }
+    }
+
     private static void writeDisableNavkeysOption(Context context, boolean enabled) {
         LineageSettings.System.putIntForUser(context.getContentResolver(),
                 LineageSettings.System.FORCE_SHOW_NAVBAR, enabled ? 1 : 0, UserHandle.USER_CURRENT);
@@ -825,14 +898,7 @@
             mDisableNavigationKeys.setEnabled(false);
             mNavigationPreferencesCat.setEnabled(false);
             if (!mDisableNavigationKeys.isChecked()) {
-                IOverlayManager overlayManager = IOverlayManager.Stub.asInterface(
-                        ServiceManager.getService(Context.OVERLAY_SERVICE));
-                try {
-                    overlayManager.setEnabledExclusiveInCategory(NAV_BAR_MODE_3BUTTON_OVERLAY,
-                            UserHandle.USER_CURRENT);
-                } catch (RemoteException e) {
-                    throw e.rethrowFromSystemServer();
-                }
+                setButtonNavigationMode(NAV_BAR_MODE_3BUTTON_OVERLAY);
             }
             writeDisableNavkeysOption(getActivity(), mDisableNavigationKeys.isChecked());
             updateDisableNavkeysOption();