Migrate Display Settings to Preferences.

Display settings can be represented with SwitchPreference and
SeekBarPreference. Note that SeekBarPreference looks slightly different
than what originally existed (seek bar is not aligned with text), but
this can be changed via changing the preference styles in the future.

Also, actual testing of the change of the display brightness was not
observed on the Mojave, because there isn't a correct LightsService
built to handle it. Still, all of the logic remains the same as before.

We include our own version of SeekBarPreference due to the inflexibility
of the standard SeekBarPreference. We add a functionality to send
continuous updates to the listener while changing the seek bar.

Bug: 117336555
Test: Build, Manual, Robolectric unit tests
Change-Id: I4c1f96fcfc414ca77c1addec64ab76a4c5e434d2
diff --git a/src/com/android/car/settings/common/SeekBarPreference.java b/src/com/android/car/settings/common/SeekBarPreference.java
new file mode 100644
index 0000000..d6479aa
--- /dev/null
+++ b/src/com/android/car/settings/common/SeekBarPreference.java
@@ -0,0 +1,394 @@
+/*
+ * Copyright (C) 2018 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.car.settings.common;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.view.KeyEvent;
+import android.view.View;
+import android.widget.SeekBar;
+import android.widget.TextView;
+
+import androidx.preference.Preference;
+import androidx.preference.PreferenceViewHolder;
+
+import com.android.car.settings.R;
+
+/**
+ * Car Setting's own version of SeekBarPreference.
+ *
+ * The code is directly taken from androidx.preference.SeekBarPreference. However it has 1 main
+ * functionality difference. There is a new field which can enable continuous updates while the
+ * seek bar value is changing. This can be set programmatically by using the {@link
+ * #setContinuousUpdate() setContinuousUpdate} method.
+ */
+public class SeekBarPreference extends Preference {
+
+    private int mSeekBarValue;
+    private int mMin;
+    private int mMax;
+    private int mSeekBarIncrement;
+    private boolean mTrackingTouch;
+    private SeekBar mSeekBar;
+    private TextView mSeekBarValueTextView;
+    private boolean mAdjustable; // whether the seekbar should respond to the left/right keys
+    private boolean mShowSeekBarValue; // whether to show the seekbar value TextView next to the bar
+    private boolean mContinuousUpdate; // whether scrolling provides continuous calls to listener
+
+    private static final String TAG = "SeekBarPreference";
+
+    /**
+     * Listener reacting to the SeekBar changing value by the user
+     */
+    private SeekBar.OnSeekBarChangeListener mSeekBarChangeListener =
+            new SeekBar.OnSeekBarChangeListener() {
+                @Override
+                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+                    if (fromUser && (mContinuousUpdate || !mTrackingTouch)) {
+                        syncValueInternal(seekBar);
+                    }
+                }
+
+                @Override
+                public void onStartTrackingTouch(SeekBar seekBar) {
+                    mTrackingTouch = true;
+                }
+
+                @Override
+                public void onStopTrackingTouch(SeekBar seekBar) {
+                    mTrackingTouch = false;
+                    if (seekBar.getProgress() + mMin != mSeekBarValue) {
+                        syncValueInternal(seekBar);
+                    }
+                }
+            };
+
+    /**
+     * Listener reacting to the user pressing DPAD left/right keys if {@code
+     * adjustable} attribute is set to true; it transfers the key presses to the SeekBar
+     * to be handled accordingly.
+     */
+    private View.OnKeyListener mSeekBarKeyListener = new View.OnKeyListener() {
+        @Override
+        public boolean onKey(View v, int keyCode, KeyEvent event) {
+            if (event.getAction() != KeyEvent.ACTION_DOWN) {
+                return false;
+            }
+
+            if (!mAdjustable && (keyCode == KeyEvent.KEYCODE_DPAD_LEFT
+                    || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)) {
+                // Right or left keys are pressed when in non-adjustable mode; Skip the keys.
+                return false;
+            }
+
+            // We don't want to propagate the click keys down to the seekbar view since it will
+            // create the ripple effect for the thumb.
+            if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
+                return false;
+            }
+
+            if (mSeekBar == null) {
+                Log.e(TAG, "SeekBar view is null and hence cannot be adjusted.");
+                return false;
+            }
+            return mSeekBar.onKeyDown(keyCode, event);
+        }
+    };
+
+    public SeekBarPreference(
+            Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
+        super(context, attrs, defStyleAttr, defStyleRes);
+
+        TypedArray a = context.obtainStyledAttributes(
+                attrs, R.styleable.SeekBarPreference, defStyleAttr, defStyleRes);
+
+        /**
+         * The ordering of these two statements are important. If we want to set max first, we need
+         * to perform the same steps by changing min/max to max/min as following:
+         * mMax = a.getInt(...) and setMin(...).
+         */
+        mMin = a.getInt(R.styleable.SeekBarPreference_min, 0);
+        setMax(a.getInt(R.styleable.SeekBarPreference_android_max, 100));
+        setSeekBarIncrement(a.getInt(R.styleable.SeekBarPreference_seekBarIncrement, 0));
+        mAdjustable = a.getBoolean(R.styleable.SeekBarPreference_adjustable, true);
+        mShowSeekBarValue = a.getBoolean(R.styleable.SeekBarPreference_showSeekBarValue, true);
+        a.recycle();
+    }
+
+    public SeekBarPreference(Context context, AttributeSet attrs, int defStyleAttr) {
+        this(context, attrs, defStyleAttr, 0);
+    }
+
+    public SeekBarPreference(Context context, AttributeSet attrs) {
+        this(context, attrs, R.attr.seekBarPreferenceStyle);
+    }
+
+    public SeekBarPreference(Context context) {
+        this(context, null);
+    }
+
+    @Override
+    public void onBindViewHolder(PreferenceViewHolder view) {
+        super.onBindViewHolder(view);
+        view.itemView.setOnKeyListener(mSeekBarKeyListener);
+        mSeekBar = (SeekBar) view.findViewById(R.id.seekbar);
+        mSeekBarValueTextView = (TextView) view.findViewById(R.id.seekbar_value);
+        if (mShowSeekBarValue) {
+            mSeekBarValueTextView.setVisibility(View.VISIBLE);
+        } else {
+            mSeekBarValueTextView.setVisibility(View.GONE);
+            mSeekBarValueTextView = null;
+        }
+
+        if (mSeekBar == null) {
+            Log.e(TAG, "SeekBar view is null in onBindViewHolder.");
+            return;
+        }
+        mSeekBar.setOnSeekBarChangeListener(mSeekBarChangeListener);
+        mSeekBar.setMax(mMax - mMin);
+        // If the increment is not zero, use that. Otherwise, use the default mKeyProgressIncrement
+        // in AbsSeekBar when it's zero. This default increment value is set by AbsSeekBar
+        // after calling setMax. That's why it's important to call setKeyProgressIncrement after
+        // calling setMax() since setMax() can change the increment value.
+        if (mSeekBarIncrement != 0) {
+            mSeekBar.setKeyProgressIncrement(mSeekBarIncrement);
+        } else {
+            mSeekBarIncrement = mSeekBar.getKeyProgressIncrement();
+        }
+
+        mSeekBar.setProgress(mSeekBarValue - mMin);
+        if (mSeekBarValueTextView != null) {
+            mSeekBarValueTextView.setText(String.valueOf(mSeekBarValue));
+        }
+        mSeekBar.setEnabled(isEnabled());
+    }
+
+    @Override
+    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
+        setValue(restoreValue ? getPersistedInt(mSeekBarValue)
+                : (Integer) defaultValue);
+    }
+
+    @Override
+    protected Object onGetDefaultValue(TypedArray a, int index) {
+        return a.getInt(index, 0);
+    }
+
+    /** Setter for the minimum value allowed on seek bar. */
+    public void setMin(int min) {
+        if (min > mMax) {
+            min = mMax;
+        }
+        if (min != mMin) {
+            mMin = min;
+            notifyChanged();
+        }
+    }
+
+    /** Getter for the minimum value allowed on seek bar. */
+    public int getMin() {
+        return mMin;
+    }
+
+    /** Setter for the maximum value allowed on seek bar. */
+    public final void setMax(int max) {
+        if (max < mMin) {
+            max = mMin;
+        }
+        if (max != mMax) {
+            mMax = max;
+            notifyChanged();
+        }
+    }
+
+    /**
+     * Returns the amount of increment change via each arrow key click. This value is derived
+     * from
+     * user's specified increment value if it's not zero. Otherwise, the default value is picked
+     * from the default mKeyProgressIncrement value in {@link android.widget.AbsSeekBar}.
+     *
+     * @return The amount of increment on the SeekBar performed after each user's arrow key press.
+     */
+    public final int getSeekBarIncrement() {
+        return mSeekBarIncrement;
+    }
+
+    /**
+     * Sets the increment amount on the SeekBar for each arrow key press.
+     *
+     * @param seekBarIncrement The amount to increment or decrement when the user presses an
+     *                         arrow key.
+     */
+    public final void setSeekBarIncrement(int seekBarIncrement) {
+        if (seekBarIncrement != mSeekBarIncrement) {
+            mSeekBarIncrement = Math.min(mMax - mMin, Math.abs(seekBarIncrement));
+            notifyChanged();
+        }
+    }
+
+    /** Getter for the maximum value allowed on seek bar. */
+    public int getMax() {
+        return mMax;
+    }
+
+    /** Setter for the functionality which allows for changing the values via keyboard arrows. */
+    public void setAdjustable(boolean adjustable) {
+        mAdjustable = adjustable;
+    }
+
+    /** Getter for the functionality which allows for changing the values via keyboard arrows. */
+    public boolean isAdjustable() {
+        return mAdjustable;
+    }
+
+    /** Setter for the functionality which allows for continuous triggering of listener code. */
+    public void setContinuousUpdate(boolean continuousUpdate) {
+        mContinuousUpdate = continuousUpdate;
+    }
+
+    /** Setter for the current value of the seek bar. */
+    public void setValue(int seekBarValue) {
+        setValueInternal(seekBarValue, true);
+    }
+
+    private void setValueInternal(int seekBarValue, boolean notifyChanged) {
+        if (seekBarValue < mMin) {
+            seekBarValue = mMin;
+        }
+        if (seekBarValue > mMax) {
+            seekBarValue = mMax;
+        }
+
+        if (seekBarValue != mSeekBarValue) {
+            mSeekBarValue = seekBarValue;
+            if (mSeekBarValueTextView != null) {
+                mSeekBarValueTextView.setText(String.valueOf(mSeekBarValue));
+            }
+            persistInt(seekBarValue);
+            if (notifyChanged) {
+                notifyChanged();
+            }
+        }
+    }
+
+    /** Getter for the current value of the seek bar. */
+    public int getValue() {
+        return mSeekBarValue;
+    }
+
+    /**
+     * Persist the seekBar's seekbar value if callChangeListener
+     * returns true, otherwise set the seekBar's value to the stored value
+     */
+    private void syncValueInternal(SeekBar seekBar) {
+        int seekBarValue = mMin + seekBar.getProgress();
+        if (seekBarValue != mSeekBarValue) {
+            if (callChangeListener(seekBarValue)) {
+                setValueInternal(seekBarValue, false);
+            } else {
+                seekBar.setProgress(mSeekBarValue - mMin);
+            }
+        }
+    }
+
+    @Override
+    protected Parcelable onSaveInstanceState() {
+        final Parcelable superState = super.onSaveInstanceState();
+        if (isPersistent()) {
+            // No need to save instance state since it's persistent
+            return superState;
+        }
+
+        // Save the instance state
+        final SeekBarPreference.SavedState myState = new SeekBarPreference.SavedState(superState);
+        myState.mSeekBarValue = mSeekBarValue;
+        myState.mMin = mMin;
+        myState.mMax = mMax;
+        return myState;
+    }
+
+    @Override
+    protected void onRestoreInstanceState(Parcelable state) {
+        if (!state.getClass().equals(SeekBarPreference.SavedState.class)) {
+            // Didn't save state for us in onSaveInstanceState
+            super.onRestoreInstanceState(state);
+            return;
+        }
+
+        // Restore the instance state
+        SeekBarPreference.SavedState myState = (SeekBarPreference.SavedState) state;
+        super.onRestoreInstanceState(myState.getSuperState());
+        mSeekBarValue = myState.mSeekBarValue;
+        mMin = myState.mMin;
+        mMax = myState.mMax;
+        notifyChanged();
+    }
+
+    /**
+     * SavedState, a subclass of {@link BaseSavedState}, will store the state
+     * of MyPreference, a subclass of Preference.
+     * <p>
+     * It is important to always call through to super methods.
+     */
+    private static class SavedState extends BaseSavedState {
+        int mSeekBarValue;
+        int mMin;
+        int mMax;
+
+        SavedState(Parcel source) {
+            super(source);
+
+            // Restore the click counter
+            mSeekBarValue = source.readInt();
+            mMin = source.readInt();
+            mMax = source.readInt();
+        }
+
+        @Override
+        public void writeToParcel(Parcel dest, int flags) {
+            super.writeToParcel(dest, flags);
+
+            // Save the click counter
+            dest.writeInt(mSeekBarValue);
+            dest.writeInt(mMin);
+            dest.writeInt(mMax);
+        }
+
+        SavedState(Parcelable superState) {
+            super(superState);
+        }
+
+        @SuppressWarnings("unused")
+        public static final Parcelable.Creator<SeekBarPreference.SavedState> CREATOR =
+                new Parcelable.Creator<SeekBarPreference.SavedState>() {
+                    @Override
+                    public SeekBarPreference.SavedState createFromParcel(Parcel in) {
+                        return new SeekBarPreference.SavedState(in);
+                    }
+
+                    @Override
+                    public SeekBarPreference.SavedState[] newArray(int size) {
+                        return new SeekBarPreference
+                                .SavedState[size];
+                    }
+                };
+    }
+}
diff --git a/src/com/android/car/settings/display/AdaptiveBrightnessTogglePreferenceController.java b/src/com/android/car/settings/display/AdaptiveBrightnessTogglePreferenceController.java
new file mode 100644
index 0000000..215f851
--- /dev/null
+++ b/src/com/android/car/settings/display/AdaptiveBrightnessTogglePreferenceController.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2018 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.car.settings.display;
+
+import android.content.Context;
+import android.provider.Settings;
+
+import androidx.preference.Preference;
+import androidx.preference.TwoStatePreference;
+
+import com.android.car.settings.common.FragmentController;
+import com.android.car.settings.common.NoSetupPreferenceController;
+
+/** Business logic for controlling the adaptive brightness setting. */
+public class AdaptiveBrightnessTogglePreferenceController extends
+        NoSetupPreferenceController implements
+        Preference.OnPreferenceChangeListener {
+
+    public AdaptiveBrightnessTogglePreferenceController(Context context, String preferenceKey,
+            FragmentController fragmentController) {
+        super(context, preferenceKey, fragmentController);
+    }
+
+    @Override
+    public void updateState(Preference preference) {
+        if (!(preference instanceof TwoStatePreference)) {
+            throw new IllegalArgumentException("Expecting an instance of TwoStatePreference");
+        }
+        ((TwoStatePreference) preference).setChecked(isAdaptiveBrightnessChecked());
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return supportsAdaptiveBrightness() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        if (!(preference instanceof TwoStatePreference)) {
+            throw new IllegalArgumentException("Expecting an instance of TwoStatePreference");
+        }
+        boolean enableAdaptiveBrightness = (boolean) newValue;
+        Settings.System.putInt(mContext.getContentResolver(),
+                Settings.System.SCREEN_BRIGHTNESS_MODE,
+                enableAdaptiveBrightness ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
+                        : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
+        return true;
+    }
+
+    private boolean isAdaptiveBrightnessChecked() {
+        int brightnessMode = Settings.System.getInt(mContext.getContentResolver(),
+                Settings.System.SCREEN_BRIGHTNESS_MODE,
+                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
+        return brightnessMode != Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
+    }
+
+    private boolean supportsAdaptiveBrightness() {
+        return mContext.getResources().getBoolean(
+                com.android.internal.R.bool.config_automatic_brightness_available);
+    }
+}
diff --git a/src/com/android/car/settings/display/BrightnessLevelPreferenceController.java b/src/com/android/car/settings/display/BrightnessLevelPreferenceController.java
new file mode 100644
index 0000000..4de0efc
--- /dev/null
+++ b/src/com/android/car/settings/display/BrightnessLevelPreferenceController.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright (C) 2018 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.car.settings.display;
+
+import static com.android.settingslib.display.BrightnessUtils.GAMMA_SPACE_MAX;
+import static com.android.settingslib.display.BrightnessUtils.convertGammaToLinear;
+import static com.android.settingslib.display.BrightnessUtils.convertLinearToGamma;
+
+import android.car.userlib.CarUserManagerHelper;
+import android.content.Context;
+import android.os.PowerManager;
+import android.provider.Settings;
+
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+
+import com.android.car.settings.common.FragmentController;
+import com.android.car.settings.common.Logger;
+import com.android.car.settings.common.NoSetupPreferenceController;
+import com.android.car.settings.common.SeekBarPreference;
+
+/** Business logic for changing the brightness of the display. */
+public class BrightnessLevelPreferenceController extends NoSetupPreferenceController implements
+        Preference.OnPreferenceChangeListener {
+
+    private static final Logger LOG = new Logger(BrightnessLevelPreferenceController.class);
+    private final CarUserManagerHelper mCarUserManagerHelper;
+    private final int mMaximumBacklight;
+    private final int mMinimumBacklight;
+    private SeekBarPreference mPreference;
+
+    public BrightnessLevelPreferenceController(Context context, String preferenceKey,
+            FragmentController fragmentController) {
+        super(context, preferenceKey, fragmentController);
+        mCarUserManagerHelper = new CarUserManagerHelper(mContext);
+        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
+        mMaximumBacklight = powerManager.getMaximumScreenBrightnessSetting();
+        mMinimumBacklight = powerManager.getMinimumScreenBrightnessSetting();
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        Preference preference = screen.findPreference(getPreferenceKey());
+        if (!(preference instanceof SeekBarPreference)) {
+            throw new IllegalStateException(
+                    "Preference for this controller should be SeekBarPreference");
+        }
+
+        mPreference = (SeekBarPreference) preference;
+        mPreference.setMax(GAMMA_SPACE_MAX);
+        mPreference.setValue(getSeekbarValue());
+        mPreference.setContinuousUpdate(true);
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        if (!(preference instanceof SeekBarPreference)) {
+            throw new IllegalArgumentException("Expecting SeekBarPreference");
+        }
+        int gamma = (Integer) newValue;
+        int linear = convertGammaToLinear(gamma, mMinimumBacklight, mMaximumBacklight);
+        Settings.System.putIntForUser(mContext.getContentResolver(),
+                Settings.System.SCREEN_BRIGHTNESS, linear,
+                mCarUserManagerHelper.getCurrentProcessUserId());
+        return true;
+    }
+
+    private int getSeekbarValue() {
+        int gamma = GAMMA_SPACE_MAX;
+        try {
+            int linear = Settings.System.getIntForUser(mContext.getContentResolver(),
+                    Settings.System.SCREEN_BRIGHTNESS,
+                    mCarUserManagerHelper.getCurrentProcessUserId());
+            gamma = convertLinearToGamma(linear, mMinimumBacklight, mMaximumBacklight);
+        } catch (Settings.SettingNotFoundException e) {
+            LOG.w("Can't find setting for SCREEN_BRIGHTNESS.");
+        }
+        return gamma;
+    }
+}
diff --git a/src/com/android/car/settings/display/BrightnessListItem.java b/src/com/android/car/settings/display/BrightnessListItem.java
deleted file mode 100644
index e704895..0000000
--- a/src/com/android/car/settings/display/BrightnessListItem.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * Copyright (C) 2018 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.car.settings.display;
-
-import static android.provider.Settings.System.SCREEN_BRIGHTNESS;
-
-import static com.android.settingslib.display.BrightnessUtils.GAMMA_SPACE_MAX;
-import static com.android.settingslib.display.BrightnessUtils.convertGammaToLinear;
-import static com.android.settingslib.display.BrightnessUtils.convertLinearToGamma;
-
-import android.car.userlib.CarUserManagerHelper;
-import android.content.Context;
-import android.os.PowerManager;
-import android.provider.Settings.SettingNotFoundException;
-import android.provider.Settings.System;
-import android.widget.SeekBar;
-import android.widget.SeekBar.OnSeekBarChangeListener;
-
-import androidx.car.widget.SeekbarListItem;
-
-import com.android.car.settings.R;
-import com.android.car.settings.common.Logger;
-
-/**
- * A ListItem that displays and sets display brightness.
- */
-public class BrightnessListItem extends SeekbarListItem {
-    private static final Logger LOG = new Logger(BrightnessListItem.class);
-    private CarUserManagerHelper mCarUserManagerHelper;
-    private final Context mContext;
-    private final int mMaximumBacklight;
-    private final int mMinimumBacklight;
-
-    /**
-     * Handles brightness change from user
-     */
-    private SeekBar.OnSeekBarChangeListener mOnSeekBarChangeListener =
-            new OnSeekBarChangeListener() {
-                @Override
-                public void onStartTrackingTouch(SeekBar seekBar) {
-                    // no-op
-                }
-
-                @Override
-                public void onStopTrackingTouch(SeekBar seekBar) {
-                    // no-op
-                }
-
-                @Override
-                public void onProgressChanged(SeekBar seekBar, int gamma, boolean fromUser) {
-                    int linear = convertGammaToLinear(gamma, mMinimumBacklight, mMaximumBacklight);
-                    System.putIntForUser(mContext.getContentResolver(), SCREEN_BRIGHTNESS, linear,
-                                         mCarUserManagerHelper.getCurrentForegroundUserId());
-                }
-            };
-
-    public BrightnessListItem(Context context) {
-        super(context);
-        mContext = context;
-        mCarUserManagerHelper = new CarUserManagerHelper(mContext);
-        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
-        mMaximumBacklight = powerManager.getMaximumScreenBrightnessSetting();
-        mMinimumBacklight = powerManager.getMinimumScreenBrightnessSetting();
-        setMax(GAMMA_SPACE_MAX);
-        setProgress(getSeekbarValue());
-        setOnSeekBarChangeListener(mOnSeekBarChangeListener);
-        setText(context.getString(R.string.brightness));
-    }
-
-    private int getSeekbarValue() {
-        int gamma = GAMMA_SPACE_MAX;
-        try {
-            int linear = System.getIntForUser(mContext.getContentResolver(), SCREEN_BRIGHTNESS,
-                                          mCarUserManagerHelper.getCurrentForegroundUserId());
-            gamma = convertLinearToGamma(linear, mMinimumBacklight, mMaximumBacklight);
-        } catch (SettingNotFoundException e) {
-            LOG.w("Can't find setting for SCREEN_BRIGHTNESS.");
-        }
-        return gamma;
-    }
-}
diff --git a/src/com/android/car/settings/display/DisplaySettingsFragment.java b/src/com/android/car/settings/display/DisplaySettingsFragment.java
index e7d6b83..03a45fb 100644
--- a/src/com/android/car/settings/display/DisplaySettingsFragment.java
+++ b/src/com/android/car/settings/display/DisplaySettingsFragment.java
@@ -15,71 +15,19 @@
  */
 package com.android.car.settings.display;
 
-import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
-import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
-import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
-
-import android.content.Context;
-import android.provider.Settings;
-
-import androidx.annotation.StringRes;
-import androidx.car.widget.ListItem;
-import androidx.car.widget.ListItemProvider;
-import androidx.car.widget.ListItemProvider.ListProvider;
-import androidx.car.widget.TextListItem;
+import androidx.annotation.XmlRes;
 
 import com.android.car.settings.R;
-import com.android.car.settings.common.ListItemSettingsFragment;
-
-import java.util.ArrayList;
-import java.util.List;
+import com.android.car.settings.common.BasePreferenceFragment;
 
 /**
- * Activity to host Display related preferences.
+ * Preference fragment to host Display related preferences.
  */
-public class DisplaySettingsFragment extends ListItemSettingsFragment {
+public class DisplaySettingsFragment extends BasePreferenceFragment {
 
     @Override
-    @StringRes
-    protected int getTitleId() {
-        return R.string.display_settings;
-    }
-
-    @Override
-    public ListItemProvider getItemProvider() {
-        return new ListProvider(getListItems());
-    }
-
-    private List<ListItem> getListItems() {
-        List<ListItem> listItems = new ArrayList<>();
-        Context context = getContext();
-        if (supportsAdaptiveBrightness()) {
-            TextListItem adaptiveBrightnessItem = new TextListItem(context);
-            adaptiveBrightnessItem.setTitle(context.getString(R.string.auto_brightness_title));
-            adaptiveBrightnessItem.setBody(
-                    context.getString(R.string.auto_brightness_summary));
-            adaptiveBrightnessItem.setSwitch(
-                    isAdaptiveBrightnessChecked(),
-                    /* showDivider= */false,
-                    (button, isChecked) ->
-                            Settings.System.putInt(context.getContentResolver(),
-                                    SCREEN_BRIGHTNESS_MODE,
-                                    isChecked ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC
-                                            : SCREEN_BRIGHTNESS_MODE_MANUAL));
-            listItems.add(adaptiveBrightnessItem);
-        }
-        listItems.add(new BrightnessListItem(context));
-        return listItems;
-    }
-
-    private boolean isAdaptiveBrightnessChecked() {
-        int brightnessMode = Settings.System.getInt(getContext().getContentResolver(),
-                SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
-        return brightnessMode != SCREEN_BRIGHTNESS_MODE_MANUAL;
-    }
-
-    private boolean supportsAdaptiveBrightness() {
-        return getContext().getResources().getBoolean(
-                com.android.internal.R.bool.config_automatic_brightness_available);
+    @XmlRes
+    protected int getPreferenceScreenResId() {
+        return R.xml.display_settings_fragment;
     }
 }