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

Commit d810e83a authored by Jay Aliomer's avatar Jay Aliomer Committed by Android (Google) Code Review
Browse files

Merge "Custom dark theme scheduling"

parents 562a1872 c31c35fb
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -106,6 +106,8 @@
        <item>@string/dark_ui_auto_mode_never</item>
        <!-- 1: Auto -->
        <item>@string/dark_ui_auto_mode_auto</item>
        <!-- 2: Custom -->
        <item>@string/dark_ui_auto_mode_custom</item>
    </string-array>

    <!-- Security settings.  The delay after screen is turned off until device locks.
+13 −3
Original line number Diff line number Diff line
@@ -2806,6 +2806,8 @@
    <string name="dark_ui_auto_mode_never">None</string>
    <!-- Dark UI screen, setting option value for Dark UI to turn on/off automatically at sunset/sunrise. [CHAR LIMIT=32] -->
    <string name="dark_ui_auto_mode_auto">Turns on from sunset to sunrise</string>
    <!-- Dark UI screen, setting option value for Dark theme to turn on/off automatically according to a user defined schedule. [CHAR LIMIT=32] -->
    <string name="dark_ui_auto_mode_custom">Turns on at custom time</string>
    <!-- Dark UI screen, setting option name controlling the current activation status. [CHAR LIMIT=30] -->
    <string name="dark_ui_status_title">Status</string>
    <!-- Display settings screen, summary format of Dark UI when off. [CHAR LIMIT=NONE] -->
@@ -2814,12 +2816,20 @@
    <string name="dark_ui_summary_off_auto_mode_never">Will never turn on automatically</string>
    <!-- Display settings screen, summary of Dark UI when off and will turn on automatically at sunset. [CHAR LIMIT=NONE] -->
    <string name="dark_ui_summary_off_auto_mode_auto">Will turn on automatically at sunset</string>
        <!-- Display settings screen, summary format of night display when off and will turn on automatically at a user defined time. [CHAR LIMIT=NONE] -->
    <string name="dark_ui_summary_off_auto_mode_custom">Will turn on automatically at <xliff:g name="time" example="6 AM">%1$s</xliff:g></string>
    <!-- Display settings screen, summary format of Dark UI when on. [CHAR LIMIT=NONE] -->
    <string name="dark_ui_summary_on">On / <xliff:g name="auto_mode_summary" example="Never turn off automatically">%1$s</xliff:g></string>
    <!-- Display settings screen, summary of Dark UI when on and will *never* turn off automatically. [CHAR LIMIT=NONE] -->
    <string name="dark_ui_summary_on_auto_mode_never">Will never turn off automatically</string>
    <!-- Display settings screen, summary of Dark UI when on and will turn off automatically at sunrise. [CHAR LIMIT=NONE] -->
    <string name="dark_ui_summary_on_auto_mode_auto">Will turn off automatically at sunrise</string>
    <!-- Display settings screen, summary format of night display when on and will turn off automatically at a user defined time. [CHAR LIMIT=NONE] -->
    <string name="dark_ui_summary_on_auto_mode_custom">Will turn off automatically at <xliff:g name="time" example="10 PM">%1$s</xliff:g></string>
    <!-- Display settings screen, activation button action for custom schedule [CHAR LIMIT=40] -->
    <string name="dark_ui_activation_on_custom">Turn on until <xliff:g name="time" example="6 AM">%1$s</xliff:g></string>
    <!-- Display settings screen, deactivation button action for custom schedule [CHAR LIMIT=40] -->
    <string name="dark_ui_activation_off_custom">Turn off until <xliff:g name="time" example="10 PM">%1$s</xliff:g></string>
    <!-- Dark theme screen, description of Dark theme feature. [CHAR LIMIT=NONE] -->
    <string name="dark_ui_text">Dark theme uses a black background to help keep battery alive longer on some screens. Dark theme schedules wait to turn on until your screen is off.</string>
+10 −0
Original line number Diff line number Diff line
@@ -30,6 +30,16 @@
        settings:controller="com.android.settings.display.darkmode.DarkModeScheduleSelectorController"
        settings:keywords="@string/keywords_dark_ui_mode"/>

    <Preference
        android:key="dark_theme_start_time"
        android:title="@string/night_display_start_time_title"
        settings:searchable="false"/>

    <Preference
        android:key="dark_theme_end_time"
        android:title="@string/night_display_end_time_title"
        settings:searchable="false"/>

    <com.android.settingslib.widget.LayoutPreference
        android:key="dark_ui_activated"
        android:title="@string/dark_ui_title"
+34 −6
Original line number Diff line number Diff line
/*
 * 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
+105 −0
Original line number Diff line number Diff line
/*
 * 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));
    }
}
Loading