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

Commit 07cfa206 authored by Lucas Silva's avatar Lucas Silva
Browse files

Cleanup dream complication setting to reflect single toggle users see in

Settings.

The original implementation was designed to allow users to individually
enable/disable certain complications. However, we have since decided to
simplify the complication controls around a single master toggle. This
fixes an issue where when new complications are added, we will no longer
switch the toggle to off in Settings.

Bug: 241469874
Test: atest ComplicationTypesUpdaterTest
Test: atest DreamBackendTest
Test: manually on device
Change-Id: I1bbfbbe2abd6398b74bcd0b9ba601583a1403bef
parent 28930b91
Loading
Loading
Loading
Loading
+3 −5
Original line number Diff line number Diff line
@@ -9142,14 +9142,12 @@ public final class Settings {
        public static final String SCREENSAVER_DEFAULT_COMPONENT = "screensaver_default_component";
        /**
         * The complications that are enabled to be shown over the screensaver by the user. Holds
         * a comma separated list of
         * {@link com.android.settingslib.dream.DreamBackend.ComplicationType}.
         * Whether complications are enabled to be shown over the screensaver by the user.
         *
         * @hide
         */
        public static final String SCREENSAVER_ENABLED_COMPLICATIONS =
                "screensaver_enabled_complications";
        public static final String SCREENSAVER_COMPLICATIONS_ENABLED =
                "screensaver_complications_enabled";
        /**
+0 −3
Original line number Diff line number Diff line
@@ -2427,9 +2427,6 @@
    <!-- The list of supported dream complications -->
    <integer-array name="config_supportedDreamComplications">
    </integer-array>
    <!-- The list of dream complications which should be enabled by default -->
    <integer-array name="config_dreamComplicationsEnabledByDefault">
    </integer-array>

    <!-- Are we allowed to dream while not plugged in? -->
    <bool name="config_dreamsEnabledOnBattery">false</bool>
+0 −1
Original line number Diff line number Diff line
@@ -2230,7 +2230,6 @@
  <java-symbol type="string" name="config_dreamsDefaultComponent" />
  <java-symbol type="bool" name="config_dreamsOnlyEnabledForSystemUser" />
  <java-symbol type="array" name="config_supportedDreamComplications" />
  <java-symbol type="array" name="config_dreamComplicationsEnabledByDefault" />
  <java-symbol type="array" name="config_disabledDreamComponents" />
  <java-symbol type="bool" name="config_dismissDreamOnActivityStart" />
  <java-symbol type="string" name="config_loggable_dream_prefix" />
+0 −15
Original line number Diff line number Diff line
@@ -1601,21 +1601,6 @@
    <!-- Content description of the no calling for accessibility (not shown on the screen). [CHAR LIMIT=NONE] -->
    <string name="accessibility_no_calling">No calling.</string>

    <!-- Screensaver overlay which displays the time. [CHAR LIMIT=20] -->
    <string name="dream_complication_title_time">Time</string>
    <!-- Screensaver overlay which displays the date. [CHAR LIMIT=20] -->
    <string name="dream_complication_title_date">Date</string>
    <!-- Screensaver overlay which displays the weather. [CHAR LIMIT=20] -->
    <string name="dream_complication_title_weather">Weather</string>
    <!-- Screensaver overlay which displays air quality. [CHAR LIMIT=20] -->
    <string name="dream_complication_title_aqi">Air Quality</string>
    <!-- Screensaver overlay which displays cast info. [CHAR LIMIT=20] -->
    <string name="dream_complication_title_cast_info">Cast Info</string>
    <!-- Screensaver overlay which displays home controls. [CHAR LIMIT=20] -->
    <string name="dream_complication_title_home_controls">Home Controls</string>
    <!-- Screensaver overlay which displays smartspace. [CHAR LIMIT=20] -->
    <string name="dream_complication_title_smartspace">Smartspace</string>


    <!-- Title for a screen allowing the user to choose a profile picture. [CHAR LIMIT=NONE] -->
    <string name="avatar_picker_title">Choose a profile picture</string>
+31 −106
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.settingslib.dream;

import android.annotation.IntDef;
import android.annotation.Nullable;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@@ -32,17 +31,14 @@ import android.os.ServiceManager;
import android.provider.Settings;
import android.service.dreams.DreamService;
import android.service.dreams.IDreamManager;
import android.text.TextUtils;
import android.util.Log;

import com.android.settingslib.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@@ -64,18 +60,21 @@ public class DreamBackend {
        public String toString() {
            StringBuilder sb = new StringBuilder(DreamInfo.class.getSimpleName());
            sb.append('[').append(caption);
            if (isActive)
            if (isActive) {
                sb.append(",active");
            }
            sb.append(',').append(componentName);
            if (settingsComponentName != null)
            if (settingsComponentName != null) {
                sb.append("settings=").append(settingsComponentName);
            }
            return sb.append(']').toString();
        }
    }

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({WHILE_CHARGING, WHILE_DOCKED, EITHER, NEVER})
    public @interface WhenToDream {}
    public @interface WhenToDream {
    }

    public static final int WHILE_CHARGING = 0;
    public static final int WHILE_DOCKED = 1;
@@ -96,7 +95,8 @@ public class DreamBackend {
            COMPLICATION_TYPE_SMARTSPACE
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface ComplicationType {}
    public @interface ComplicationType {
    }

    public static final int COMPLICATION_TYPE_TIME = 1;
    public static final int COMPLICATION_TYPE_DATE = 2;
@@ -114,8 +114,6 @@ public class DreamBackend {
    private final boolean mDreamsActivatedOnDockByDefault;
    private final Set<ComponentName> mDisabledDreams;
    private final Set<Integer> mSupportedComplications;
    private final Set<Integer> mDefaultEnabledComplications;

    private static DreamBackend sInstance;

    public static DreamBackend getInstance(Context context) {
@@ -147,13 +145,6 @@ public class DreamBackend {
                        com.android.internal.R.array.config_supportedDreamComplications))
                .boxed()
                .collect(Collectors.toSet());

        mDefaultEnabledComplications = Arrays.stream(resources.getIntArray(
                        com.android.internal.R.array.config_dreamComplicationsEnabledByDefault))
                .boxed()
                // A complication can only be enabled by default if it is also supported.
                .filter(mSupportedComplications::contains)
                .collect(Collectors.toSet());
    }

    public List<DreamInfo> getDreamInfos() {
@@ -251,7 +242,8 @@ public class DreamBackend {
        return null;
    }

    public @WhenToDream int getWhenToDreamSetting() {
    @WhenToDream
    public int getWhenToDreamSetting() {
        return isActivatedOnDock() && isActivatedOnSleep() ? EITHER
                : isActivatedOnDock() ? WHILE_DOCKED
                        : isActivatedOnSleep() ? WHILE_CHARGING
@@ -283,98 +275,29 @@ public class DreamBackend {
        }
    }

    /** Returns whether a particular complication is enabled */
    public boolean isComplicationEnabled(@ComplicationType int complication) {
        return getEnabledComplications().contains(complication);
    }

    /** Gets all complications which have been enabled by the user. */
    public Set<Integer> getEnabledComplications() {
        final String enabledComplications = Settings.Secure.getString(
                mContext.getContentResolver(),
                Settings.Secure.SCREENSAVER_ENABLED_COMPLICATIONS);

        if (enabledComplications == null) {
            return mDefaultEnabledComplications;
        }

        return parseFromString(enabledComplications);
    }

    /** Gets all dream complications which are supported on this device. **/
    public Set<Integer> getSupportedComplications() {
        return mSupportedComplications;
    }

    /**
     * Enables or disables a particular dream complication.
     *
     * @param complicationType The dream complication to be enabled/disabled.
     * @param value            If true, the complication is enabled. Otherwise it is disabled.
     */
    public void setComplicationEnabled(@ComplicationType int complicationType, boolean value) {
        if (!mSupportedComplications.contains(complicationType)) return;

        Set<Integer> enabledComplications = getEnabledComplications();
        if (value) {
            enabledComplications.add(complicationType);
        } else {
            enabledComplications.remove(complicationType);
        return getComplicationsEnabled() ? mSupportedComplications : Collections.emptySet();
    }

        Settings.Secure.putString(mContext.getContentResolver(),
                Settings.Secure.SCREENSAVER_ENABLED_COMPLICATIONS,
                convertToString(enabledComplications));
    /** Sets complication enabled state. */
    public void setComplicationsEnabled(boolean enabled) {
        Settings.Secure.putInt(mContext.getContentResolver(),
                Settings.Secure.SCREENSAVER_COMPLICATIONS_ENABLED, enabled ? 1 : 0);
    }

    /**
     * Gets the title of a particular complication type to be displayed to the user. If there
     * is no title, null is returned.
     * Gets whether complications are enabled on this device
     */
    @Nullable
    public CharSequence getComplicationTitle(@ComplicationType int complicationType) {
        int res = 0;
        switch (complicationType) {
            case COMPLICATION_TYPE_TIME:
                res = R.string.dream_complication_title_time;
                break;
            case COMPLICATION_TYPE_DATE:
                res = R.string.dream_complication_title_date;
                break;
            case COMPLICATION_TYPE_WEATHER:
                res = R.string.dream_complication_title_weather;
                break;
            case COMPLICATION_TYPE_AIR_QUALITY:
                res = R.string.dream_complication_title_aqi;
                break;
            case COMPLICATION_TYPE_CAST_INFO:
                res = R.string.dream_complication_title_cast_info;
                break;
            case COMPLICATION_TYPE_HOME_CONTROLS:
                res = R.string.dream_complication_title_home_controls;
                break;
            case COMPLICATION_TYPE_SMARTSPACE:
                res = R.string.dream_complication_title_smartspace;
                break;
            default:
                return null;
        }
        return mContext.getString(res);
    }

    private static String convertToString(Set<Integer> set) {
        return set.stream()
                .map(String::valueOf)
                .collect(Collectors.joining(","));
    public boolean getComplicationsEnabled() {
        return Settings.Secure.getInt(
                mContext.getContentResolver(),
                Settings.Secure.SCREENSAVER_COMPLICATIONS_ENABLED, 1) == 1;
    }

    private static Set<Integer> parseFromString(String string) {
        if (TextUtils.isEmpty(string)) {
            return new HashSet<>();
        }
        return Arrays.stream(string.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toSet());
    /** Gets all dream complications which are supported on this device. **/
    public Set<Integer> getSupportedComplications() {
        return mSupportedComplications;
    }

    public boolean isEnabled() {
@@ -416,8 +339,9 @@ public class DreamBackend {

    public void setActiveDream(ComponentName dream) {
        logd("setActiveDream(%s)", dream);
        if (mDreamManager == null)
        if (mDreamManager == null) {
            return;
        }
        try {
            ComponentName[] dreams = {dream};
            mDreamManager.setDreamComponents(dream == null ? null : dreams);
@@ -427,8 +351,9 @@ public class DreamBackend {
    }

    public ComponentName getActiveDream() {
        if (mDreamManager == null)
        if (mDreamManager == null) {
            return null;
        }
        try {
            ComponentName[] dreams = mDreamManager.getDreamComponents();
            return dreams != null && dreams.length > 0 ? dreams[0] : null;
Loading