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
This commit is contained in:
Raff Tsai
2019-12-20 13:29:10 +08:00
parent 4e561fb9cb
commit 00c79aecd1
6 changed files with 52 additions and 17 deletions

View File

@@ -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>

View File

@@ -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" />

View File

@@ -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

View File

@@ -25,6 +25,7 @@ import android.view.WindowManager;
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 @@ public class GestureNavigationSettingsFragment extends DashboardFragment {
}
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

View File

@@ -14,35 +14,59 @@
* limitations under the License.
*/
package com.android.settings.gestures;
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;
import com.android.settings.widget.SeekBarPreference;
/** A slider preference that is used to set the back gesture's sensitivity **/
public class GestureNavigationSeekBarPreference extends SeekBarPreference {
/** 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 GestureNavigationSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
public LabeledSeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setLayoutResource(R.layout.preference_gesture_navigation_slider);
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 GestureNavigationSeekBarPreference(Context context, AttributeSet attrs) {
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;
}

View File

@@ -27,6 +27,8 @@ import android.widget.SeekBar;
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.RobolectricTestRunner;
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 @@ public class GestureNavigationSeekBarPreferenceTest {
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(),