Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fd3911ba authored by Aurélien Pomini's avatar Aurélien Pomini
Browse files

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
parent 30317dd2
Loading
Loading
Loading
Loading
+104 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:clickable="false"
    android:orientation="horizontal">

    <include layout="@layout/settingslib_icon_frame"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingTop="16dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@android:id/title"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceListItem"
                android:textColor="?android:attr/textColorPrimary"
                android:ellipsize="marquee"
                android:hyphenationFrequency="normalFast"
                android:lineBreakWordStyle="phrase"
                android:fadingEdge="horizontal"/>
            <LinearLayout
                android:id="@android:id/widget_frame"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:orientation="vertical"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layoutDirection="ltr"
            android:orientation="vertical">

            <com.android.settings.accessibility.ContrastLevelSeekBar
                android:id="@*android:id/seekbar"
                android:minHeight="?android:attr/listPreferredItemHeightSmall"
                android:paddingStart="0dp"
                android:paddingEnd="0dp"
                android:layout_gravity="center_vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/left_text"
                    android:text="@string/accessibility_contrast_level_left_label"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="?android:attr/textColorSecondary"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:maxLines="1"
                    android:textAlignment="viewStart" />
                <TextView
                    android:id="@+id/right_text"
                    android:text="@string/accessibility_contrast_level_right_label"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:textColor="?android:attr/textColorSecondary"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:maxLines="1"
                    android:textAlignment="viewEnd" />

            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
 No newline at end of file
+4 −0
Original line number Diff line number Diff line
@@ -220,6 +220,10 @@
    <dimen name="accessibility_qs_tooltip_margin">20dp</dimen>
    <dimen name="accessibility_qs_tooltip_margin_top">27dp</dimen>

    <!-- Contrast level slider -->
    <dimen name="contrast_level_seekbar_center_marker_height">14dp</dimen>
    <dimen name="contrast_level_seekbar_center_marker_width">1dp</dimen>

    <!-- Restricted icon in switch bar -->
    <dimen name="restricted_icon_margin_end">16dp</dimen>
    <!-- Restricted icon size in switch bar -->
+6 −0
Original line number Diff line number Diff line
@@ -4213,6 +4213,12 @@
    <string name="accessibility_disable_animations">Remove animations</string>
    <!-- Summary for the accessibility preference for disabling animations. [CHAR LIMIT=60] -->
    <string name="accessibility_disable_animations_summary">Reduce movement on the screen</string>
    <!-- Title for the accessibility preference for the contrast level slider. [CHAR LIMIT=35] -->
    <string name="accessibility_contrast_level_title">Contrast Level</string>
    <!-- 'Low' text at the left of the contrast level slider. [CHAR LIMIT=20] -->
    <string name="accessibility_contrast_level_left_label">Standard</string>
    <!-- 'High' text at the right of the contrast level slider. [CHAR LIMIT=20] -->
    <string name="accessibility_contrast_level_right_label">High</string>
    <!-- Title for the accessibility preference for primary mono. [CHAR LIMIT=35] -->
    <string name="accessibility_toggle_primary_mono_title">Mono audio</string>
    <!-- Summary for the accessibility preference for primary mono. [CHAR LIMIT=60] -->
+5 −0
Original line number Diff line number Diff line
@@ -64,6 +64,11 @@
        android:title="@string/accessibility_toggle_large_pointer_icon_title"
        settings:controller="com.android.settings.accessibility.LargePointerIconPreferenceController"/>

    <com.android.settings.accessibility.ContrastLevelSeekBarPreference
        android:key="seekbar_color_contrast"
        android:persistent="false"
        android:title="@string/accessibility_contrast_level_title"/>

    <PreferenceCategory
        android:key="experimental_category"
        android:persistent="false"
+118 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}
Loading