Merge "Update "Dark Theme" Settings screen to account for Modes that can toggle it" into main

This commit is contained in:
Matías Hernández
2024-09-11 15:57:46 +00:00
committed by Android (Google) Code Review
10 changed files with 562 additions and 156 deletions

View File

@@ -0,0 +1,105 @@
/*
* 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.display.darkmode;
import static android.app.UiModeManager.MODE_ATTENTION_THEME_OVERLAY_NIGHT;
import static android.app.UiModeManager.MODE_NIGHT_AUTO;
import static android.app.UiModeManager.MODE_NIGHT_CUSTOM_TYPE_BEDTIME;
import static android.app.UiModeManager.MODE_NIGHT_CUSTOM_TYPE_SCHEDULE;
import static com.google.common.base.Preconditions.checkNotNull;
import android.app.Flags;
import android.app.UiModeManager;
import android.content.Context;
import com.android.settings.R;
import com.android.settingslib.notification.modes.ZenMode;
import com.android.settingslib.notification.modes.ZenModesBackend;
import java.time.LocalTime;
import java.util.List;
class AutoDarkTheme {
static String getStatus(Context context, boolean active) {
UiModeManager uiModeManager = checkNotNull(context.getSystemService(UiModeManager.class));
final int mode = uiModeManager.getNightMode();
if (mode == MODE_NIGHT_AUTO) {
return context.getString(active
? R.string.dark_ui_summary_on_auto_mode_auto
: R.string.dark_ui_summary_off_auto_mode_auto);
}
if (Flags.modesUi()) {
if (active && uiModeManager.getAttentionModeThemeOverlay()
== MODE_ATTENTION_THEME_OVERLAY_NIGHT) {
List<String> modes = getActiveModesThatChangeDarkTheme(context);
if (!modes.isEmpty()) {
return context.getString(R.string.dark_ui_summary_on_auto_mode_modes,
modes.get(0));
}
} else if (!active) {
List<String> modes = getModesThatChangeDarkTheme(context);
if (!modes.isEmpty()) {
return context.getString(R.string.dark_ui_summary_off_auto_mode_modes,
modes.get(0));
}
}
}
if (mode == UiModeManager.MODE_NIGHT_CUSTOM) {
int modeCustomType = uiModeManager.getNightModeCustomType();
if (!Flags.modesUi() && modeCustomType == MODE_NIGHT_CUSTOM_TYPE_BEDTIME) {
return context.getString(active
? R.string.dark_ui_summary_on_auto_mode_custom_bedtime
: R.string.dark_ui_summary_off_auto_mode_custom_bedtime);
}
if (modeCustomType == MODE_NIGHT_CUSTOM_TYPE_SCHEDULE) {
final LocalTime time = active
? uiModeManager.getCustomNightModeEnd()
: uiModeManager.getCustomNightModeStart();
final String timeStr = new TimeFormatter(context).of(time);
return context.getString(active
? R.string.dark_ui_summary_on_auto_mode_custom
: R.string.dark_ui_summary_off_auto_mode_custom, timeStr);
}
}
return context.getString(active
? R.string.dark_ui_summary_on_auto_mode_never
: R.string.dark_ui_summary_off_auto_mode_never);
}
static List<String> getModesThatChangeDarkTheme(Context context) {
return ZenModesBackend.getInstance(context)
.getModes().stream()
.filter(m -> m.getDeviceEffects().shouldUseNightMode())
.map(ZenMode::getName)
.toList();
}
static List<String> getActiveModesThatChangeDarkTheme(Context context) {
return ZenModesBackend.getInstance(context)
.getModes().stream()
.filter(ZenMode::isActive)
.filter(m -> m.getDeviceEffects().shouldUseNightMode())
.map(ZenMode::getName)
.toList();
}
}

View File

@@ -24,14 +24,11 @@ import android.widget.CompoundButton.OnCheckedChangeListener;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.widget.MainSwitchPreference;
import java.time.LocalTime;
/**
* Controller for activate/deactivate night mode button
*/
@@ -40,22 +37,14 @@ public class DarkModeActivationPreferenceController extends BasePreferenceContro
private final UiModeManager mUiModeManager;
private final MetricsFeatureProvider mMetricsFeatureProvider;
private TimeFormatter mFormat;
private MainSwitchPreference mPreference;
public DarkModeActivationPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mUiModeManager = context.getSystemService(UiModeManager.class);
mFormat = new TimeFormatter(context);
mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
}
public DarkModeActivationPreferenceController(Context context, String preferenceKey,
TimeFormatter f) {
this(context, preferenceKey);
mFormat = f;
}
@Override
public final void updateState(Preference preference) {
final boolean active = (mContext.getResources().getConfiguration().uiMode
@@ -67,32 +56,7 @@ public class DarkModeActivationPreferenceController extends BasePreferenceContro
public CharSequence getSummary() {
final boolean isActivated = (mContext.getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_YES) != 0;
final int mode = mUiModeManager.getNightMode();
if (mode == UiModeManager.MODE_NIGHT_AUTO) {
return mContext.getString(isActivated
? R.string.dark_ui_summary_on_auto_mode_auto
: R.string.dark_ui_summary_off_auto_mode_auto);
} else if (mode == UiModeManager.MODE_NIGHT_CUSTOM) {
if (mUiModeManager.getNightModeCustomType()
== UiModeManager.MODE_NIGHT_CUSTOM_TYPE_BEDTIME) {
return mContext.getString(isActivated
? R.string.dark_ui_summary_on_auto_mode_custom_bedtime
: R.string.dark_ui_summary_off_auto_mode_custom_bedtime);
}
final LocalTime time = isActivated
? mUiModeManager.getCustomNightModeEnd()
: mUiModeManager.getCustomNightModeStart();
final String timeStr = mFormat.of(time);
return mContext.getString(isActivated
? R.string.dark_ui_summary_on_auto_mode_custom
: R.string.dark_ui_summary_off_auto_mode_custom, timeStr);
} else {
return mContext.getString(isActivated
? R.string.dark_ui_summary_on_auto_mode_never
: R.string.dark_ui_summary_off_auto_mode_never);
}
return AutoDarkTheme.getStatus(mContext, isActivated);
}
@Override

View File

@@ -1,74 +0,0 @@
/*
* 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.display.darkmode;
import static android.app.UiModeManager.MODE_NIGHT_CUSTOM_TYPE_BEDTIME;
import android.app.UiModeManager;
import android.content.Context;
import android.content.Intent;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.FooterPreference;
/** Controller for the night mode bedtime custom mode footer. */
public class DarkModeCustomBedtimePreferenceController extends BasePreferenceController {
private final UiModeManager mUiModeManager;
private FooterPreference mFooterPreference;
private BedtimeSettings mBedtimeSettings;
public DarkModeCustomBedtimePreferenceController(Context context, String key) {
super(context, key);
mUiModeManager = context.getSystemService(UiModeManager.class);
mBedtimeSettings = new BedtimeSettings(context);
}
@Override
public int getAvailabilityStatus() {
return mBedtimeSettings.getBedtimeSettingsIntent() == null
? UNSUPPORTED_ON_DEVICE
: AVAILABLE_UNSEARCHABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mFooterPreference = screen.findPreference(getPreferenceKey());
mFooterPreference.setLearnMoreAction(
v -> {
Intent bedtimeSettingsIntent = mBedtimeSettings.getBedtimeSettingsIntent();
if (bedtimeSettingsIntent != null) {
v.getContext().startActivity(bedtimeSettingsIntent);
}
});
mFooterPreference.setLearnMoreText(
mContext.getString(R.string.dark_ui_bedtime_footer_action));
}
@Override
public void updateState(Preference preference) {
if (mUiModeManager.getNightModeCustomType() != MODE_NIGHT_CUSTOM_TYPE_BEDTIME) {
preference.setVisible(false);
return;
}
preference.setVisible(true);
}
}

View File

@@ -0,0 +1,112 @@
/*
* 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.display.darkmode;
import static android.app.UiModeManager.MODE_NIGHT_CUSTOM_TYPE_BEDTIME;
import static android.app.settings.SettingsEnums.DARK_UI_SETTINGS;
import static com.google.common.base.Preconditions.checkNotNull;
import android.app.Flags;
import android.app.UiModeManager;
import android.content.Context;
import android.content.Intent;
import android.icu.text.MessageFormat;
import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.notification.modes.ZenModesListFragment;
import com.android.settingslib.widget.FooterPreference;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/** Controller for the dark theme Modes / Bedtime custom footer. */
public class DarkModeCustomModesPreferenceController extends BasePreferenceController {
private final UiModeManager mUiModeManager;
private final BedtimeSettings mBedtimeSettings;
public DarkModeCustomModesPreferenceController(@NonNull Context context, @NonNull String key) {
super(context, key);
mUiModeManager = context.getSystemService(UiModeManager.class);
mBedtimeSettings = new BedtimeSettings(context);
}
@Override
public int getAvailabilityStatus() {
return (Flags.modesUi() || mBedtimeSettings.getBedtimeSettingsIntent() != null)
? AVAILABLE_UNSEARCHABLE
: UNSUPPORTED_ON_DEVICE;
}
@Override
public void displayPreference(@NonNull PreferenceScreen screen) {
super.displayPreference(screen);
FooterPreference footerPreference = checkNotNull(screen.findPreference(getPreferenceKey()));
if (Flags.modesUi()) {
List<String> modesUsingDarkTheme = AutoDarkTheme.getModesThatChangeDarkTheme(
screen.getContext());
MessageFormat titleFormat = new MessageFormat(
mContext.getString(R.string.dark_ui_modes_footer_summary),
Locale.getDefault());
Map<String, Object> args = new HashMap<>();
args.put("count", modesUsingDarkTheme.size());
for (int i = 0; i < modesUsingDarkTheme.size() && i < 3; i++) {
args.put("mode_" + (i + 1), modesUsingDarkTheme.get(i));
}
footerPreference.setTitle(titleFormat.format(args));
footerPreference.setLearnMoreAction(
v -> new SubSettingLauncher(v.getContext())
.setDestination(ZenModesListFragment.class.getName())
.setSourceMetricsCategory(DARK_UI_SETTINGS)
.launch());
footerPreference.setLearnMoreText(
mContext.getString(R.string.dark_ui_modes_footer_action));
} else {
footerPreference.setTitle(R.string.dark_ui_bedtime_footer_summary);
footerPreference.setLearnMoreAction(
v -> {
Intent bedtimeSettingsIntent = mBedtimeSettings.getBedtimeSettingsIntent();
if (bedtimeSettingsIntent != null) {
v.getContext().startActivity(bedtimeSettingsIntent);
}
});
footerPreference.setLearnMoreText(
mContext.getString(R.string.dark_ui_bedtime_footer_action));
}
}
@Override
public void updateState(@NonNull Preference preference) {
if (!Flags.modesUi()) {
if (mUiModeManager.getNightModeCustomType() != MODE_NIGHT_CUSTOM_TYPE_BEDTIME) {
preference.setVisible(false);
return;
}
preference.setVisible(true);
}
}
}

View File

@@ -23,8 +23,6 @@ import android.util.AttributeSet;
import com.android.settings.R;
import com.android.settingslib.PrimarySwitchPreference;
import java.time.LocalTime;
/**
* component for the display settings dark ui summary
*/
@@ -71,36 +69,8 @@ public class DarkModePreference extends PrimarySwitchPreference {
? R.string.dark_ui_mode_disabled_summary_dark_theme_on
: R.string.dark_ui_mode_disabled_summary_dark_theme_off;
setSummary(getContext().getString(stringId));
return;
}
final int mode = mUiModeManager.getNightMode();
String summary;
if (mode == UiModeManager.MODE_NIGHT_AUTO) {
summary = getContext().getString(active
? R.string.dark_ui_summary_on_auto_mode_auto
: R.string.dark_ui_summary_off_auto_mode_auto);
} else if (mode == UiModeManager.MODE_NIGHT_CUSTOM) {
if (mUiModeManager.getNightModeCustomType()
== UiModeManager.MODE_NIGHT_CUSTOM_TYPE_BEDTIME) {
summary = getContext().getString(active
? R.string.dark_ui_summary_on_auto_mode_custom_bedtime
: R.string.dark_ui_summary_off_auto_mode_custom_bedtime);
} else {
final LocalTime time = active
? mUiModeManager.getCustomNightModeEnd()
: mUiModeManager.getCustomNightModeStart();
final String timeStr = mFormat.of(time);
summary = getContext().getString(active
? R.string.dark_ui_summary_on_auto_mode_custom
: R.string.dark_ui_summary_off_auto_mode_custom, timeStr);
}
} else {
summary = getContext().getString(active
? R.string.dark_ui_summary_on_auto_mode_never
: R.string.dark_ui_summary_off_auto_mode_never);
setSummary(AutoDarkTheme.getStatus(getContext(), active));
}
setSummary(summary);
}
}