Add discrete contrast UI slider in the setting

This slider is temporary and will be replaced by the final UI in the next couple weeks.

In this first version, the slider is added in Settings -> Accessibility -> Color and motion. It is discrete with three values.

The slider manipulates the setting currently named CONTRAST_LEVEL in Settings.Secure. This setting is ranging from [-1, 1] with a default value of 0. However, the three values of the slider correspond to the contrast values [0, 0.5, 1] respectively

Test: Manual: put a log before Settings.System.putFloatForUser, move the slider manually and verify the logs
Test: make RunSettingsRoboTests ROBOTEST_FILTER=ContrastLevelSeekBar
Bug: 259091608
Change-Id: I2eaf751d6008ad20b3564dde2c0f4648cc6c8178
This commit is contained in:
Aurélien Pomini
2022-11-14 15:28:00 +00:00
parent 30317dd2b9
commit fd3911ba29
8 changed files with 485 additions and 0 deletions

View File

@@ -0,0 +1,118 @@
/*
* Copyright (C) 2022 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.accessibility;
import static android.view.HapticFeedbackConstants.CLOCK_TICK;
import static com.android.settings.Utils.isNightMode;
import static com.android.settings.accessibility.ContrastLevelSeekBarPreference.CONTRAST_SLIDER_TICKS;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.AttributeSet;
import android.widget.SeekBar;
import com.android.settings.R;
/**
* A custom seekbar for the contrast level setting.
*
* Adds a center line indicator between left and right, which snaps to if close.
* Updates the Settings.Secure.CONTRAST_LEVEL setting on progress changed.
*
* TODO(b/266071578): remove this class and replace this with the final UI
*/
public class ContrastLevelSeekBar extends SeekBar {
private final Context mContext;
private int mLastProgress = -1;
private final Paint mMarkerPaint;
private final Rect mMarkerRect;
private final OnSeekBarChangeListener mProxySeekBarListener = new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) { }
@Override
public void onStartTrackingTouch(SeekBar seekBar) { }
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (!fromUser || progress == mLastProgress) return;
seekBar.performHapticFeedback(CLOCK_TICK);
mLastProgress = progress;
// rescale progress from [0, 1, 2] to [0, 0.5, 1]
final float contrastLevel = (float) progress / CONTRAST_SLIDER_TICKS;
Settings.Secure.putFloatForUser(mContext.getContentResolver(),
Settings.Secure.CONTRAST_LEVEL, contrastLevel, UserHandle.USER_CURRENT);
}
};
public ContrastLevelSeekBar(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.seekBarStyle);
}
public ContrastLevelSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0 /* defStyleRes */);
}
public ContrastLevelSeekBar(
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mContext = context;
Resources res = getResources();
mMarkerRect = new Rect(0 /* left */, 0 /* top */,
res.getDimensionPixelSize(R.dimen.contrast_level_seekbar_center_marker_width),
res.getDimensionPixelSize(R.dimen.contrast_level_seekbar_center_marker_height));
mMarkerPaint = new Paint();
// the might be a better colour for the markers, but this slider is temporary anyway
mMarkerPaint.setColor(isNightMode(context) ? Color.WHITE : Color.BLACK);
mMarkerPaint.setStyle(Paint.Style.FILL);
// Remove the progress colour
setProgressTintList(ColorStateList.valueOf(Color.TRANSPARENT));
super.setOnSeekBarChangeListener(mProxySeekBarListener);
}
@Override
public void setOnSeekBarChangeListener(OnSeekBarChangeListener listener) { }
// Note: the superclass AbsSeekBar.onDraw is synchronized.
@Override
protected synchronized void onDraw(Canvas canvas) {
// Draw a marker at the center of the seekbar
int seekBarCenter = (getHeight() - getPaddingBottom()) / 2;
float sliderWidth = getWidth() - mMarkerRect.right - getPaddingEnd();
canvas.save();
canvas.translate(sliderWidth / 2f,
seekBarCenter - (mMarkerRect.bottom / 2f));
canvas.drawRect(mMarkerRect, mMarkerPaint);
canvas.restore();
super.onDraw(canvas);
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2022 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.accessibility;
import android.content.Context;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.AttributeSet;
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 directly controls the contrast level **/
public class ContrastLevelSeekBarPreference extends SeekBarPreference {
/**
* The number of ticks of the slider (the more ticks, the more continuous the slider feels).
*/
public static final int CONTRAST_SLIDER_TICKS = 2;
private final Context mContext;
private ContrastLevelSeekBar mSeekBar;
public ContrastLevelSeekBarPreference(Context context, AttributeSet attrs) {
super(context, attrs, TypedArrayUtils.getAttr(context,
R.attr.preferenceStyle,
android.R.attr.preferenceStyle));
mContext = context;
setLayoutResource(R.layout.preference_contrast_level_slider);
}
@Override
public void onBindViewHolder(PreferenceViewHolder view) {
super.onBindViewHolder(view);
mSeekBar = (ContrastLevelSeekBar) view.findViewById(
com.android.internal.R.id.seekbar);
init();
}
private void init() {
if (mSeekBar == null) {
return;
}
final float contrastLevel = Settings.Secure.getFloatForUser(
mContext.getContentResolver(), Settings.Secure.CONTRAST_LEVEL,
0.f /* default */, UserHandle.USER_CURRENT);
mSeekBar.setMax(CONTRAST_SLIDER_TICKS);
// Rescale contrast from [0, 0.5, 1] to [0, 1, 2]
int progress = Math.max(0, Math.round(contrastLevel * CONTRAST_SLIDER_TICKS));
mSeekBar.setProgress(progress);
mSeekBar.setEnabled(isEnabled());
}
}