[2/2] Settings: navigation mode settings

* Updated for 11

Add:
show changes in height in the indicator too

Change-Id: I4e8070f5bf58b7148f42717e82cb401b9c5c7be6
diff --git a/res/values/custom_strings.xml b/res/values/custom_strings.xml
index b6c7bf2..15c31cd 100644
--- a/res/values/custom_strings.xml
+++ b/res/values/custom_strings.xml
@@ -30,4 +30,9 @@
     <string name="battery_image">Show battery image</string>
     <string name="battery_image_description">Show battery image in status bar</string>
 
+    <!-- Back gesture height -->
+    <string name="back_height_low_label">Full</string>
+    <string name="back_height_high_label">Bottom</string>
+    <string name="back_options_dialog_title">Options</string>
+    <string name="back_height_message">Amount of screen height used as touchable region for back gesture.</string>
 </resources>
diff --git a/res/xml/gesture_navigation_settings.xml b/res/xml/gesture_navigation_settings.xml
index a4b5af9..9a826eb 100644
--- a/res/xml/gesture_navigation_settings.xml
+++ b/res/xml/gesture_navigation_settings.xml
@@ -55,6 +55,14 @@
             android:max="2"
             settings:textStart="@string/low_label"
             settings:textEnd="@string/high_label"/>
+
+        <com.android.settings.widget.LabeledSeekBarPreference
+            android:key="gesture_back_height"
+            android:title="@string/back_height_message"
+            android:max="3"
+            android:selectable="true"
+            settings:textStart="@string/back_height_low_label"
+            settings:textEnd="@string/back_height_high_label"/>
     </PreferenceCategory>
 
     <com.android.settingslib.widget.FooterPreference
diff --git a/src/com/android/settings/gestures/BackGestureIndicatorView.java b/src/com/android/settings/gestures/BackGestureIndicatorView.java
index c60afd0..3bf40ec 100644
--- a/src/com/android/settings/gestures/BackGestureIndicatorView.java
+++ b/src/com/android/settings/gestures/BackGestureIndicatorView.java
@@ -19,6 +19,7 @@
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.graphics.PixelFormat;
+import android.graphics.Point;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -37,6 +38,7 @@
     private ImageView mRightIndicator;
     private BackGestureIndicatorDrawable mLeftDrawable;
     private BackGestureIndicatorDrawable mRightDrawable;
+    private int mHeightScale;
 
     public BackGestureIndicatorView(Context context) {
         super(context);
@@ -87,6 +89,10 @@
         indicator.setWidth(width);
     }
 
+    public void setIndicatorHeightScale(int heightScale) {
+        mHeightScale = heightScale;
+    }
+
     public WindowManager.LayoutParams getLayoutParams(
             WindowManager.LayoutParams parentWindowAttributes) {
         int copiedFlags = (parentWindowAttributes.flags
@@ -99,8 +105,33 @@
                         | copiedFlags,
                 PixelFormat.TRANSLUCENT);
 
+        setCurrentGestureHeight(lp);
         lp.setTitle("BackGestureIndicatorView");
         lp.token = getContext().getActivityToken();
         return lp;
     }
+
+    private void setCurrentGestureHeight(WindowManager.LayoutParams lp) {
+        Point displaySize = new Point();
+        getContext().getDisplay().getRealSize(displaySize);
+
+        // mHeightScale cant be range 0 - 3
+        // 0 means full height
+        // 1 measns half of the screen
+        // 2 means lower third of the screen
+        // 3 means lower sicth of the screen
+        if (mHeightScale == 0) {
+            lp.height = displaySize.y;
+            lp.y = 0;
+        } else if (mHeightScale == 1) {
+            lp.height = displaySize.y / 2;
+            lp.y = displaySize.y - lp.height;
+        } else if (mHeightScale == 2) {
+            lp.height = displaySize.y / 3;
+            lp.y = displaySize.y - lp.height;
+        } else {
+            lp.height = displaySize.y / 6;
+            lp.y = displaySize.y - lp.height;
+        }
+    }
 }
diff --git a/src/com/android/settings/gestures/GestureNavigationSettingsFragment.java b/src/com/android/settings/gestures/GestureNavigationSettingsFragment.java
index 546581b..80031a9 100644
--- a/src/com/android/settings/gestures/GestureNavigationSettingsFragment.java
+++ b/src/com/android/settings/gestures/GestureNavigationSettingsFragment.java
@@ -31,6 +31,8 @@
 import com.android.settings.widget.SeekBarPreference;
 import com.android.settingslib.search.SearchIndexable;
 
+import org.omnirom.omnilib.utils.OmniSettings;
+
 /**
  * A fragment to include all the settings related to Gesture Navigation mode.
  */
@@ -44,12 +46,16 @@
 
     private static final String LEFT_EDGE_SEEKBAR_KEY = "gesture_left_back_sensitivity";
     private static final String RIGHT_EDGE_SEEKBAR_KEY = "gesture_right_back_sensitivity";
+    private static final String KEY_BACK_HEIGHT = "gesture_back_height";
 
     private WindowManager mWindowManager;
     private BackGestureIndicatorView mIndicatorView;
 
     private float[] mBackGestureInsetScales;
     private float mDefaultBackGestureInset;
+    private float[] mBackGestureHeightScales = { 0f, 1f, 2f, 3f };
+    private int mCurrentRightWidth;
+    private int mCurrentLefttWidth;
 
     public GestureNavigationSettingsFragment() {
         super();
@@ -75,6 +81,7 @@
 
         initSeekBarPreference(LEFT_EDGE_SEEKBAR_KEY);
         initSeekBarPreference(RIGHT_EDGE_SEEKBAR_KEY);
+        initSeekBarPreference(KEY_BACK_HEIGHT);
     }
 
     @Override
@@ -118,12 +125,30 @@
         pref.setContinuousUpdates(true);
         pref.setHapticFeedbackMode(SeekBarPreference.HAPTIC_FEEDBACK_MODE_ON_TICKS);
 
-        final String settingsKey = key == LEFT_EDGE_SEEKBAR_KEY
-                ? Settings.Secure.BACK_GESTURE_INSET_SCALE_LEFT
-                : Settings.Secure.BACK_GESTURE_INSET_SCALE_RIGHT;
-        final float initScale = Settings.Secure.getFloat(
+        final String settingsKey = key == LEFT_EDGE_SEEKBAR_KEY ?
+                Settings.Secure.BACK_GESTURE_INSET_SCALE_LEFT
+                : key == RIGHT_EDGE_SEEKBAR_KEY ?
+                Settings.Secure.BACK_GESTURE_INSET_SCALE_RIGHT
+                : OmniSettings.OMNI_BACK_GESTURE_HEIGHT;
+
+
+        float initScale = Settings.Secure.getFloat(
                 getContext().getContentResolver(), settingsKey, 1.0f);
 
+        // needed if we just change the height
+        float currentWidthScale = Settings.Secure.getFloat(
+                getContext().getContentResolver(), Settings.Secure.BACK_GESTURE_INSET_SCALE_RIGHT, 1.0f);
+        mCurrentRightWidth = (int) (mDefaultBackGestureInset * currentWidthScale);
+        currentWidthScale = Settings.Secure.getFloat(
+                getContext().getContentResolver(), Settings.Secure.BACK_GESTURE_INSET_SCALE_LEFT, 1.0f);
+        mCurrentLefttWidth = (int) (mDefaultBackGestureInset * currentWidthScale);
+
+        if (key == KEY_BACK_HEIGHT) {
+            mBackGestureInsetScales = mBackGestureHeightScales;
+            initScale = Settings.System.getInt(
+                    getContext().getContentResolver(), settingsKey, 0);
+        }
+
         // Find the closest value to initScale
         float minDistance = Float.MAX_VALUE;
         int minDistanceIndex = -1;
@@ -137,15 +162,38 @@
         pref.setProgress(minDistanceIndex);
 
         pref.setOnPreferenceChangeListener((p, v) -> {
-            final int width = (int) (mDefaultBackGestureInset * mBackGestureInsetScales[(int) v]);
-            mIndicatorView.setIndicatorWidth(width, key == LEFT_EDGE_SEEKBAR_KEY);
+            if (key != KEY_BACK_HEIGHT) {
+                final int width = (int) (mDefaultBackGestureInset * mBackGestureInsetScales[(int) v]);
+                mIndicatorView.setIndicatorWidth(width, key == LEFT_EDGE_SEEKBAR_KEY);
+                if (key == LEFT_EDGE_SEEKBAR_KEY) {
+                    mCurrentLefttWidth = width;
+                } else {
+                    mCurrentRightWidth = width;
+                }
+            } else {
+                final int heightScale = (int) (mBackGestureInsetScales[(int) v]);
+                mIndicatorView.setIndicatorHeightScale(heightScale);
+                // dont use updateViewLayout else it will animate
+                mWindowManager.removeView(mIndicatorView);
+                mWindowManager.addView(mIndicatorView, mIndicatorView.getLayoutParams(
+                        getActivity().getWindow().getAttributes()));
+                // peek the indicators
+                mIndicatorView.setIndicatorWidth(mCurrentRightWidth, false);
+                mIndicatorView.setIndicatorWidth(mCurrentLefttWidth, true);
+            }
             return true;
         });
 
         pref.setOnPreferenceChangeStopListener((p, v) -> {
-            mIndicatorView.setIndicatorWidth(0, key == LEFT_EDGE_SEEKBAR_KEY);
             final float scale = mBackGestureInsetScales[(int) v];
-            Settings.Secure.putFloat(getContext().getContentResolver(), settingsKey, scale);
+            if (key == KEY_BACK_HEIGHT) {
+                mIndicatorView.setIndicatorWidth(0, false);
+                mIndicatorView.setIndicatorWidth(0, true);
+                Settings.System.putInt(getContext().getContentResolver(), settingsKey, (int) scale);
+            } else {
+                mIndicatorView.setIndicatorWidth(0, key == LEFT_EDGE_SEEKBAR_KEY);
+                Settings.Secure.putFloat(getContext().getContentResolver(), settingsKey, scale);
+            }
             return true;
         });
     }