Refactor GestureNavigationSeekBarPreference

- Make GestureNavigationSeekBarPreference more flexible to configure

Fixes: 146462894
Test: adb shell am start -a android.settings.ASSIST_GESTURE_SETTINGS
      adb shell am start -a com.android.settings.GESTURE_NAVIGATION_SETTINGS

Change-Id: Ibc7ed8456fe4a42f12f0d436f53756df268acfbe
diff --git a/res/layout/preference_gesture_navigation_slider.xml b/res/layout/preference_labeled_slider.xml
similarity index 96%
rename from res/layout/preference_gesture_navigation_slider.xml
rename to res/layout/preference_labeled_slider.xml
index ac7f334..42ad988 100644
--- a/res/layout/preference_gesture_navigation_slider.xml
+++ b/res/layout/preference_labeled_slider.xml
@@ -62,7 +62,6 @@
             android:layout_height="wrap_content"
             android:layout_gravity="start|top"
             android:gravity="start"
-            android:text="@string/low_label"
             android:layout_weight="1"/>
 
         <TextView
@@ -72,7 +71,6 @@
             android:layout_height="wrap_content"
             android:layout_gravity="end|top"
             android:gravity="end"
-            android:text="@string/high_label"
             android:layout_weight="1"/>
 
     </LinearLayout>
diff --git a/res/values/attrs.xml b/res/values/attrs.xml
index 6514304..ee47124 100644
--- a/res/values/attrs.xml
+++ b/res/values/attrs.xml
@@ -159,6 +159,12 @@
         <attr name="textOff" format="reference" />
     </declare-styleable>
 
+    <!-- For LabeledSeekBarPreference -->
+    <declare-styleable name="LabeledSeekBarPreference">
+        <attr name="textStart" format="reference" />
+        <attr name="textEnd" format="reference" />
+    </declare-styleable>
+
     <declare-styleable name="TintDrawable">
         <attr name="android:tint" />
         <attr name="android:drawable" />
diff --git a/res/xml/gesture_navigation_settings.xml b/res/xml/gesture_navigation_settings.xml
index 2751f88..6230054 100644
--- a/res/xml/gesture_navigation_settings.xml
+++ b/res/xml/gesture_navigation_settings.xml
@@ -28,17 +28,21 @@
         android:persistent="false"
         android:title="@string/back_sensitivity_dialog_title">
 
-        <com.android.settings.gestures.GestureNavigationSeekBarPreference
+        <com.android.settings.widget.LabeledSeekBarPreference
             android:key="gesture_left_back_sensitivity"
             android:title="@string/left_edge"
             android:max="3"
-            android:selectable="true"/>
+            android:selectable="true"
+            settings:textStart="@string/low_label"
+            settings:textEnd="@string/high_label"/>
 
-        <com.android.settings.gestures.GestureNavigationSeekBarPreference
+        <com.android.settings.widget.LabeledSeekBarPreference
             android:key="gesture_right_back_sensitivity"
             android:title="@string/right_edge"
             android:max="3"
-            android:selectable="true"/>
+            android:selectable="true"
+            settings:textStart="@string/low_label"
+            settings:textEnd="@string/high_label"/>
     </PreferenceCategory>
 
     <com.android.settingslib.widget.FooterPreference
diff --git a/src/com/android/settings/gestures/GestureNavigationSeekBarPreference.java b/src/com/android/settings/gestures/GestureNavigationSeekBarPreference.java
deleted file mode 100644
index 085411b..0000000
--- a/src/com/android/settings/gestures/GestureNavigationSeekBarPreference.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * Copyright (C) 2019 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.
- */
-
-package com.android.settings.gestures;
-
-import android.content.Context;
-import android.util.AttributeSet;
-import android.widget.SeekBar;
-
-import androidx.core.content.res.TypedArrayUtils;
-
-import com.android.settings.R;
-import com.android.settings.widget.SeekBarPreference;
-
-/** A slider preference that is used to set the back gesture's sensitivity **/
-public class GestureNavigationSeekBarPreference extends SeekBarPreference {
-
-    private OnPreferenceChangeListener mStopListener;
-
-    public GestureNavigationSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
-            int defStyleRes) {
-
-        super(context, attrs, defStyleAttr, defStyleRes);
-        setLayoutResource(R.layout.preference_gesture_navigation_slider);
-    }
-
-    public GestureNavigationSeekBarPreference(Context context, AttributeSet attrs) {
-        this(context, attrs, TypedArrayUtils.getAttr(context,
-                androidx.preference.R.attr.seekBarPreferenceStyle,
-                com.android.internal.R.attr.seekBarPreferenceStyle), 0);
-    }
-
-    public void setOnPreferenceChangeStopListener(OnPreferenceChangeListener listener) {
-        mStopListener = listener;
-    }
-
-    @Override
-    public void onStopTrackingTouch(SeekBar seekBar) {
-        super.onStopTrackingTouch(seekBar);
-
-        if (mStopListener != null) {
-            mStopListener.onPreferenceChange(this, seekBar.getProgress());
-        }
-    }
-}
-
diff --git a/src/com/android/settings/gestures/GestureNavigationSettingsFragment.java b/src/com/android/settings/gestures/GestureNavigationSettingsFragment.java
index c209c81..73d7b85 100644
--- a/src/com/android/settings/gestures/GestureNavigationSettingsFragment.java
+++ b/src/com/android/settings/gestures/GestureNavigationSettingsFragment.java
@@ -25,6 +25,7 @@
 import com.android.settings.R;
 import com.android.settings.dashboard.DashboardFragment;
 import com.android.settings.search.BaseSearchIndexProvider;
+import com.android.settings.widget.LabeledSeekBarPreference;
 import com.android.settingslib.search.SearchIndexable;
 
 /**
@@ -108,7 +109,7 @@
     }
 
     private void initSeekBarPreference(final String key) {
-        final GestureNavigationSeekBarPreference pref = getPreferenceScreen().findPreference(key);
+        final LabeledSeekBarPreference pref = getPreferenceScreen().findPreference(key);
         pref.setContinuousUpdates(true);
 
         final String settingsKey = key == LEFT_EDGE_SEEKBAR_KEY
diff --git a/src/com/android/settings/widget/LabeledSeekBarPreference.java b/src/com/android/settings/widget/LabeledSeekBarPreference.java
new file mode 100644
index 0000000..e2b2d68
--- /dev/null
+++ b/src/com/android/settings/widget/LabeledSeekBarPreference.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2019 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.
+ */
+
+package com.android.settings.widget;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.util.AttributeSet;
+import android.widget.SeekBar;
+import android.widget.TextView;
+
+import androidx.core.content.res.TypedArrayUtils;
+import androidx.preference.PreferenceViewHolder;
+
+import com.android.settings.R;
+
+/** A slider preference with left and right labels **/
+public class LabeledSeekBarPreference extends SeekBarPreference {
+
+    private final int mTextStartId;
+    private final int mTextEndId;
+    private OnPreferenceChangeListener mStopListener;
+
+    public LabeledSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
+            int defStyleRes) {
+
+        super(context, attrs, defStyleAttr, defStyleRes);
+        setLayoutResource(R.layout.preference_labeled_slider);
+
+        final TypedArray styledAttrs = context.obtainStyledAttributes(attrs,
+                R.styleable.LabeledSeekBarPreference);
+        mTextStartId = styledAttrs.getResourceId(
+                R.styleable.LabeledSeekBarPreference_textStart,
+                R.string.summary_placeholder);
+        mTextEndId = styledAttrs.getResourceId(
+                R.styleable.LabeledSeekBarPreference_textEnd,
+                R.string.summary_placeholder);
+        styledAttrs.recycle();
+    }
+
+    public LabeledSeekBarPreference(Context context, AttributeSet attrs) {
+        this(context, attrs, TypedArrayUtils.getAttr(context,
+                androidx.preference.R.attr.seekBarPreferenceStyle,
+                com.android.internal.R.attr.seekBarPreferenceStyle), 0);
+    }
+
+    @Override
+    public void onBindViewHolder(PreferenceViewHolder holder) {
+        super.onBindViewHolder(holder);
+
+        final TextView startText = (TextView) holder.findViewById(android.R.id.text1);
+        final TextView endText = (TextView) holder.findViewById(android.R.id.text2);
+        startText.setText(mTextStartId);
+        endText.setText(mTextEndId);
+    }
+
+    public void setOnPreferenceChangeStopListener(OnPreferenceChangeListener listener) {
+        mStopListener = listener;
+    }
+
+    @Override
+    public void onStopTrackingTouch(SeekBar seekBar) {
+        super.onStopTrackingTouch(seekBar);
+
+        if (mStopListener != null) {
+            mStopListener.onPreferenceChange(this, seekBar.getProgress());
+        }
+    }
+}
+
diff --git a/tests/robotests/src/com/android/settings/gestures/GestureNavigationSeekBarPreferenceTest.java b/tests/robotests/src/com/android/settings/gestures/LabeledSeekBarPreferenceTest.java
similarity index 89%
rename from tests/robotests/src/com/android/settings/gestures/GestureNavigationSeekBarPreferenceTest.java
rename to tests/robotests/src/com/android/settings/gestures/LabeledSeekBarPreferenceTest.java
index 82c3262..4fb94f8 100644
--- a/tests/robotests/src/com/android/settings/gestures/GestureNavigationSeekBarPreferenceTest.java
+++ b/tests/robotests/src/com/android/settings/gestures/LabeledSeekBarPreferenceTest.java
@@ -27,6 +27,8 @@
 
 import androidx.preference.Preference;
 
+import com.android.settings.widget.LabeledSeekBarPreference;
+
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -36,11 +38,11 @@
 import org.robolectric.RuntimeEnvironment;
 
 @RunWith(RobolectricTestRunner.class)
-public class GestureNavigationSeekBarPreferenceTest {
+public class LabeledSeekBarPreferenceTest {
 
     private Context mContext;
     private SeekBar mSeekBar;
-    private GestureNavigationSeekBarPreference mSeekBarPreference;
+    private LabeledSeekBarPreference mSeekBarPreference;
 
     @Mock
     private Preference.OnPreferenceChangeListener mListener;
@@ -50,7 +52,7 @@
         MockitoAnnotations.initMocks(this);
 
         mContext = RuntimeEnvironment.application;
-        mSeekBarPreference = new GestureNavigationSeekBarPreference(mContext, null);
+        mSeekBarPreference = new LabeledSeekBarPreference(mContext, null);
         LayoutInflater inflater = LayoutInflater.from(mContext);
         final View view =
                 inflater.inflate(mSeekBarPreference.getLayoutResource(),