Merge "Custom dark theme scheduling"

This commit is contained in:
Jay Aliomer
2020-01-21 21:27:29 +00:00
committed by Android (Google) Code Review
12 changed files with 448 additions and 30 deletions

View File

@@ -1,8 +1,9 @@
/*
* Copyright (C) 2019 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
* 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
*
@@ -26,6 +27,8 @@ import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.widget.LayoutPreference;
import java.time.LocalTime;
/**
* Controller for activate/deactivate night mode button
*/
@@ -34,12 +37,20 @@ public class DarkModeActivationPreferenceController extends BasePreferenceContro
private PowerManager mPowerManager;
private Button mTurnOffButton;
private Button mTurnOnButton;
private TimeFormatter mFormat;
public DarkModeActivationPreferenceController(Context context,
String preferenceKey) {
super(context, preferenceKey);
mPowerManager = context.getSystemService(PowerManager.class);
mUiModeManager = context.getSystemService(UiModeManager.class);
mFormat = new TimeFormatter(context);
}
public DarkModeActivationPreferenceController(Context context,
String preferenceKey, TimeFormatter f) {
this(context, preferenceKey);
mFormat = f;
}
@Override
@@ -58,13 +69,21 @@ public class DarkModeActivationPreferenceController extends BasePreferenceContro
}
private void updateNightMode(boolean active) {
final int autoMode = mUiModeManager.getNightMode();
final int mode = mUiModeManager.getNightMode();
String buttonText;
if (autoMode == UiModeManager.MODE_NIGHT_AUTO) {
if (mode == UiModeManager.MODE_NIGHT_AUTO) {
buttonText = mContext.getString(active
? R.string.dark_ui_activation_off_auto
: R.string.dark_ui_activation_on_auto);
} else if (mode == UiModeManager.MODE_NIGHT_CUSTOM) {
final LocalTime time = active
? mUiModeManager.getCustomNightModeStart()
: mUiModeManager.getCustomNightModeEnd();
final String timeStr = mFormat.of(time);
buttonText = mContext.getString(active
? R.string.dark_ui_activation_off_custom
: R.string.dark_ui_activation_on_custom, timeStr);
} else {
buttonText = mContext.getString(active
? R.string.dark_ui_activation_off_manual
@@ -85,11 +104,20 @@ public class DarkModeActivationPreferenceController extends BasePreferenceContro
public CharSequence getSummary() {
final boolean isActivated = (mContext.getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_YES) != 0;
final int autoMode = mUiModeManager.getNightMode();
if (autoMode == UiModeManager.MODE_NIGHT_AUTO) {
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) {
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

View File

@@ -0,0 +1,105 @@
/*
* Copyright (C) 2020 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 android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.UiModeManager;
import android.content.Context;
import android.text.TextUtils;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import static android.app.UiModeManager.MODE_NIGHT_CUSTOM;
/**
* Controller for custom mode night mode time settings
*/
public class DarkModeCustomPreferenceController extends BasePreferenceController {
private static final String START_TIME_KEY = "dark_theme_start_time";
private static final String END_TIME_KEY = "dark_theme_end_time";
public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a");
private final UiModeManager mUiModeManager;
private TimeFormatter mFormat;
private DarkModeSettingsFragment mFragmet;
public DarkModeCustomPreferenceController(Context context, String key) {
super(context, key);
mFormat = new TimeFormatter(mContext);
mUiModeManager = context.getSystemService(UiModeManager.class);
}
public DarkModeCustomPreferenceController(
Context context, String key,
DarkModeSettingsFragment fragment) {
this(context, key);
mFragmet = fragment;
}
public DarkModeCustomPreferenceController(
Context context, String key,
DarkModeSettingsFragment fragment,
TimeFormatter format) {
this(context, key, fragment);
mFormat = format;
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
public TimePickerDialog getDialog() {
final LocalTime initialTime;
if (TextUtils.equals(getPreferenceKey(), START_TIME_KEY)) {
initialTime = mUiModeManager.getCustomNightModeStart();
} else {
initialTime = mUiModeManager.getCustomNightModeEnd();
}
return new TimePickerDialog(mContext, (view, hourOfDay, minute) -> {
final LocalTime time = LocalTime.of(hourOfDay, minute);
if (TextUtils.equals(getPreferenceKey(), START_TIME_KEY)) {
mUiModeManager.setCustomNightModeStart(time);
} else {
mUiModeManager.setCustomNightModeEnd(time);
}
if (mFragmet != null) {
mFragmet.refresh();
}
}, initialTime.getHour(), initialTime.getMinute(), mFormat.is24HourFormat());
}
@Override
protected void refreshSummary(Preference preference) {
if (mUiModeManager.getNightMode() != MODE_NIGHT_CUSTOM) {
preference.setVisible(false);
return;
}
preference.setVisible(true);
final LocalTime time;
if (TextUtils.equals(getPreferenceKey(), START_TIME_KEY)) {
time = mUiModeManager.getCustomNightModeStart();
} else {
time = mUiModeManager.getCustomNightModeEnd();
}
preference.setSummary(mFormat.of(time));
}
}

View File

@@ -22,6 +22,8 @@ import android.util.AttributeSet;
import com.android.settings.R;
import com.android.settings.widget.MasterSwitchPreference;
import java.time.LocalTime;
/**
* component for the display settings dark ui summary*/
public class DarkModePreference extends MasterSwitchPreference {
@@ -31,11 +33,14 @@ public class DarkModePreference extends MasterSwitchPreference {
private PowerManager mPowerManager;
private Runnable mCallback;
private TimeFormatter mFormat;
public DarkModePreference(Context context, AttributeSet attrs) {
super(context, attrs);
mDarkModeObserver = new DarkModeObserver(context);
mUiModeManager = context.getSystemService(UiModeManager.class);
mPowerManager = context.getSystemService(PowerManager.class);
mFormat = new TimeFormatter(context);
mCallback = () -> {
final boolean batterySaver = mPowerManager.isPowerSaveMode();
final boolean active = (getContext().getResources().getConfiguration().uiMode
@@ -60,21 +65,30 @@ public class DarkModePreference extends MasterSwitchPreference {
private void updateSummary(boolean batterySaver, boolean active) {
if (batterySaver) {
final int stringId = active ? R.string.dark_ui_mode_disabled_summary_dark_theme_on
final int stringId = active
? 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 boolean auto = mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_AUTO;
final int mode = mUiModeManager.getNightMode();
String detail;
if (active) {
detail = getContext().getString(auto
if (mode == UiModeManager.MODE_NIGHT_AUTO) {
detail = getContext().getString(active
? R.string.dark_ui_summary_on_auto_mode_auto
: R.string.dark_ui_summary_on_auto_mode_never);
: R.string.dark_ui_summary_off_auto_mode_auto);
} else if (mode == UiModeManager.MODE_NIGHT_CUSTOM) {
final LocalTime time = active
? mUiModeManager.getCustomNightModeEnd()
: mUiModeManager.getCustomNightModeStart();
final String timeStr = mFormat.of(time);
detail = getContext().getString(active
? R.string.dark_ui_summary_on_auto_mode_custom
: R.string.dark_ui_summary_off_auto_mode_custom, timeStr);
} else {
detail = getContext().getString(auto
? R.string.dark_ui_summary_off_auto_mode_auto
detail = getContext().getString(active
? R.string.dark_ui_summary_on_auto_mode_never
: R.string.dark_ui_summary_off_auto_mode_never);
}
String summary = getContext().getString(active

View File

@@ -63,8 +63,17 @@ public class DarkModeScheduleSelectorController extends BasePreferenceController
}
private int getCurrentMode() {
final int resId = mUiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_AUTO
? R.string.dark_ui_auto_mode_auto : R.string.dark_ui_auto_mode_never;
int resId;
switch (mUiModeManager.getNightMode()) {
case UiModeManager.MODE_NIGHT_AUTO:
resId = R.string.dark_ui_auto_mode_auto;
break;
case UiModeManager.MODE_NIGHT_CUSTOM:
resId = R.string.dark_ui_auto_mode_custom;
break;
default:
resId = R.string.dark_ui_auto_mode_never;
}
return mPreference.findIndexOfValue(mContext.getString(resId));
}
@@ -85,6 +94,9 @@ public class DarkModeScheduleSelectorController extends BasePreferenceController
} else if (mCurrentMode == mPreference.findIndexOfValue(
mContext.getString(R.string.dark_ui_auto_mode_auto))) {
mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_AUTO);
} else if (mCurrentMode == mPreference.findIndexOfValue(
mContext.getString(R.string.dark_ui_auto_mode_custom))) {
mUiModeManager.setNightMode(UiModeManager.MODE_NIGHT_CUSTOM);
}
return true;
}

View File

@@ -14,14 +14,23 @@
package com.android.settings.display.darkmode;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.UiModeManager;
import android.content.Context;
import android.os.Bundle;
import android.app.settings.SettingsEnums;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.search.SearchIndexable;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
/**
* Settings screen for Dark UI Mode
*/
@@ -29,15 +38,20 @@ import com.android.settingslib.search.SearchIndexable;
public class DarkModeSettingsFragment extends DashboardFragment {
private static final String TAG = "DarkModeSettingsFragment";
private static final String DARK_THEME_END_TIME = "dark_theme_end_time";
private static final String DARK_THEME_START_TIME = "dark_theme_start_time";
private DarkModeObserver mContentObserver;
private DarkModeCustomPreferenceController mCustomStartController;
private DarkModeCustomPreferenceController mCustomEndController;
private Runnable mCallback = () -> {
updatePreferenceStates();
};
private static final int DIALOG_START_TIME = 0;
private static final int DIALOG_END_TIME = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context context = getContext();
mContentObserver = new DarkModeObserver(context);
}
@@ -49,6 +63,18 @@ public class DarkModeSettingsFragment extends DashboardFragment {
mContentObserver.subscribe(mCallback);
}
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
List<AbstractPreferenceController> controllers = new ArrayList(2);
mCustomStartController = new DarkModeCustomPreferenceController(getContext(),
DARK_THEME_START_TIME, this);
mCustomEndController = new DarkModeCustomPreferenceController(getContext(),
DARK_THEME_END_TIME, this);
controllers.add(mCustomStartController);
controllers.add(mCustomEndController);
return controllers;
}
@Override
public void onStop() {
super.onStop();
@@ -56,6 +82,34 @@ public class DarkModeSettingsFragment extends DashboardFragment {
mContentObserver.unsubscribe();
}
@Override
public boolean onPreferenceTreeClick(Preference preference) {
if (DARK_THEME_END_TIME.equals(preference.getKey())) {
showDialog(DIALOG_END_TIME);
return true;
} else if (DARK_THEME_START_TIME.equals(preference.getKey())) {
showDialog(DIALOG_START_TIME);
return true;
}
return super.onPreferenceTreeClick(preference);
}
public void refresh() {
this.updatePreferenceStates();
}
@Override
public Dialog onCreateDialog(final int dialogId) {
if (dialogId == DIALOG_START_TIME || dialogId == DIALOG_END_TIME) {
if (dialogId == DIALOG_START_TIME) {
return mCustomStartController.getDialog();
} else {
return mCustomEndController.getDialog();
}
}
return super.onCreateDialog(dialogId);
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.dark_mode_settings;
@@ -76,6 +130,18 @@ public class DarkModeSettingsFragment extends DashboardFragment {
return SettingsEnums.DARK_UI_SETTINGS;
}
@Override
public int getDialogMetricsCategory(int dialogId) {
switch (dialogId) {
case DIALOG_START_TIME:
return SettingsEnums.DIALOG_DARK_THEME_SET_START_TIME;
case DIALOG_END_TIME:
return SettingsEnums.DIALOG_DARK_THEME_SET_END_TIME;
default:
return 0;
}
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.dark_mode_settings);
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright (C) 2020 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 android.content.Context;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* Formats LocalTime to the locale time string format
*/
public class TimeFormatter {
private final Context mContext;
public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm a");
public TimeFormatter(Context context) {
mContext = context;
}
public String of(LocalTime time) {
return is24HourFormat() ? time.toString() : formatter.format(time);
}
public boolean is24HourFormat() {
return android.text.format.DateFormat.is24HourFormat(mContext);
}
}