Files
app_Settings/src/com/android/settings/accessibility/ContrastSelectorPreferenceController.java
marcusge cddd7b3660 [Accessibility] Add contrast control screen UI and setup entry point
Bug: 241805782
Test: atest, local raven device
Screenshot: attached on bug
Flag: aconfig
com.android.settings.accessibility.enable_color_contrast_control

Change-Id: I7fcc01b822b460c6585c41a5831575ba0ffbcc93
2024-02-27 23:36:52 +00:00

123 lines
4.4 KiB
Java

/*
* Copyright (C) 2024 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.app.UiModeManager.ContrastUtils.CONTRAST_LEVEL_HIGH;
import static android.app.UiModeManager.ContrastUtils.CONTRAST_LEVEL_MEDIUM;
import static android.app.UiModeManager.ContrastUtils.CONTRAST_LEVEL_STANDARD;
import static android.app.UiModeManager.ContrastUtils.fromContrastLevel;
import static android.app.UiModeManager.ContrastUtils.toContrastLevel;
import android.app.UiModeManager;
import android.content.Context;
import android.provider.Settings;
import android.view.View;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import com.android.settingslib.widget.LayoutPreference;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
/**
* Controller for contrast selector.
*/
public class ContrastSelectorPreferenceController extends BasePreferenceController
implements LifecycleObserver, OnStart, OnStop, UiModeManager.ContrastChangeListener {
private static final String KEY_COLOR_CONTRAST_SELECTOR = "color_contrast_selector";
private final Executor mMainExecutor;
private final UiModeManager mUiModeManager;
private Map<Integer, FrameLayout> mContrastButtons = new HashMap<>();
public ContrastSelectorPreferenceController(@NonNull Context context,
@NonNull String preferenceKey) {
super(context, preferenceKey);
mMainExecutor = mContext.getMainExecutor();
mUiModeManager = mContext.getSystemService(UiModeManager.class);
}
@Override
public void displayPreference(@NonNull PreferenceScreen screen) {
super.displayPreference(screen);
final LayoutPreference mLayoutPreference =
screen.findPreference(KEY_COLOR_CONTRAST_SELECTOR);
mContrastButtons = Map.ofEntries(
Map.entry(CONTRAST_LEVEL_STANDARD,
mLayoutPreference.findViewById(R.id.contrast_button_default)),
Map.entry(CONTRAST_LEVEL_MEDIUM,
mLayoutPreference.findViewById(R.id.contrast_button_medium)),
Map.entry(CONTRAST_LEVEL_HIGH,
mLayoutPreference.findViewById(R.id.contrast_button_high))
);
mContrastButtons.forEach((contrastLevel, contrastButton) -> {
contrastButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(@Nullable View v) {
Settings.Secure.putFloat(mContext.getContentResolver(),
Settings.Secure.CONTRAST_LEVEL,
fromContrastLevel(contrastLevel));
}
});
});
highlightContrast(toContrastLevel(mUiModeManager.getContrast()));
}
@Override
public int getAvailabilityStatus() {
// The main preferences screen is feature guarded, so this always returns AVAILABLE.
return AVAILABLE;
}
@Override
public void onStart() {
mUiModeManager.addContrastChangeListener(mMainExecutor, this);
}
@Override
public void onStop() {
mUiModeManager.removeContrastChangeListener(this);
}
@Override
public void onContrastChanged(float contrast) {
highlightContrast(toContrastLevel(contrast));
}
private void highlightContrast(int contrast) {
mContrastButtons.forEach((level, button) -> {
button.setSelected(level == contrast);
});
}
}