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

Commit 43d67b00 authored by Matías Hernández's avatar Matías Hernández
Browse files

Refactor preference controllers to receive and mutate ZenMode/ZenPolicy

Also fixed a handful of style issues / warnings along the way.

Fixes: 341950853
Fixes: 341910620
Test: atest com.android.settings.notification.modes
Flag: android.app.modes_ui
Change-Id: I65900941fcdf53824caf052fd0a24401c1bfb476
parent 51dc7210
Loading
Loading
Loading
Loading
+55 −12
Original line number Diff line number Diff line
@@ -16,9 +16,10 @@

package com.android.settings.notification.modes;

import android.app.AutomaticZenRule;
import android.app.Flags;
import android.content.Context;
import android.service.notification.ZenPolicy;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -26,11 +27,17 @@ import androidx.preference.Preference;

import com.android.settingslib.core.AbstractPreferenceController;

import com.google.common.base.Preconditions;

import java.util.function.Function;

/**
 * Base class for any preference controllers pertaining to any single Zen mode.
 */
abstract class AbstractZenModePreferenceController extends AbstractPreferenceController {

    private static final String TAG = "AbstractZenModePreferenceController";

    @Nullable
    protected ZenModesBackend mBackend;

@@ -38,7 +45,7 @@ abstract class AbstractZenModePreferenceController extends AbstractPreferenceCon
    private ZenMode mZenMode;

    @NonNull
    final String mKey;
    private final String mKey;

    // ZenModesBackend should only be passed in if the preference controller may set the user's
    // policy for this zen mode. Otherwise, if the preference controller is essentially read-only
@@ -67,20 +74,56 @@ abstract class AbstractZenModePreferenceController extends AbstractPreferenceCon
        updateState(preference);
    }

    @Nullable
    public ZenMode getMode() {
        return mZenMode;
    @Override
    public final void updateState(Preference preference) {
        super.updateState(preference);
        if (mZenMode != null) {
            updateState(preference, mZenMode);
        }
    }

    abstract void updateState(Preference preference, @NonNull ZenMode zenMode);

    @Override
    public final CharSequence getSummary() {
        if (mZenMode != null) {
            return getSummary(mZenMode);
        } else {
            return null;
        }
    }

    @Nullable
    public AutomaticZenRule getAZR() {
        if (mZenMode == null || mZenMode.getRule() == null) {
    protected CharSequence getSummary(@NonNull ZenMode zenMode) {
        return null;
    }
        return mZenMode.getRule();

    /**
     * Subclasses should call this method (or a more specific one, like {@link #savePolicy} from
     * their {@code onPreferenceChange()} or similar, in order to apply changes to the mode being
     * edited (e.g. {@code saveMode(mode -> { mode.setX(value); return mode; } }.
     *
     * @param updater Function to update the {@link ZenMode}. Modifying and returning the same
     *                instance is ok.
     */
    protected final boolean saveMode(Function<ZenMode, ZenMode> updater) {
        Preconditions.checkState(mBackend != null);
        ZenMode mode = mZenMode;
        if (mode == null) {
            Log.wtf(TAG, "Cannot save mode, it hasn't been loaded (" + getClass() + ")");
            return false;
        }
        mode = updater.apply(mode);
        mBackend.updateMode(mode);
        return true;
    }

    /** Implementations of this class should override
     *  {@link AbstractPreferenceController#updateState(Preference)} to specify what should
     *  happen when the preference is updated */
    protected final boolean savePolicy(Function<ZenPolicy.Builder, ZenPolicy.Builder> updater) {
        return saveMode(mode -> {
            ZenPolicy.Builder policyBuilder = new ZenPolicy.Builder(mode.getPolicy());
            policyBuilder = updater.apply(policyBuilder);
            mode.setPolicy(policyBuilder.build());
            return mode;
        });
    }
}
+8 −8
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.app.AutomaticZenRule;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.service.notification.ZenDeviceEffects;
import android.service.notification.ZenPolicy;
import android.util.Log;

@@ -150,14 +151,6 @@ class ZenMode {
        }
    }

    /**
     * Use sparingly. If you're updating a policy field, use
     * {@link #setPolicy(android.service.notification.ZenPolicy)} instead.
     */
    public void setAzr(@NonNull AutomaticZenRule newRule) {
        mRule = newRule;
    }

    /**
     * Updates the {@link ZenPolicy} of the associated {@link AutomaticZenRule} based on the
     * supplied policy. In some cases this involves conversions, so that the following call
@@ -204,6 +197,13 @@ class ZenMode {
        mRule.setZenPolicy(policy);
    }

    @NonNull
    public ZenDeviceEffects getDeviceEffects() {
        return mRule.getDeviceEffects() != null
                ? mRule.getDeviceEffects()
                : new ZenDeviceEffects.Builder().build();
    }

    public boolean canBeDeleted() {
        return !mIsManualDnd;
    }
+8 −6
Original line number Diff line number Diff line
@@ -20,12 +20,15 @@ import static com.android.settings.notification.modes.ZenModeFragmentBase.MODE_I

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.preference.Preference;

import com.android.settings.core.SubSettingLauncher;

public class ZenModeCallsLinkPreferenceController extends AbstractZenModePreferenceController  {
class ZenModeCallsLinkPreferenceController extends AbstractZenModePreferenceController  {

    private ZenModeSummaryHelper mSummaryHelper;
    private final ZenModeSummaryHelper mSummaryHelper;

    public ZenModeCallsLinkPreferenceController(Context context, String key,
            ZenModesBackend backend) {
@@ -34,16 +37,15 @@ public class ZenModeCallsLinkPreferenceController extends AbstractZenModePrefere
    }

    @Override
    public void updateState(Preference preference) {
        super.updateState(preference);
    public void updateState(Preference preference, @NonNull ZenMode zenMode) {
        Bundle bundle = new Bundle();
        bundle.putString(MODE_ID, getMode().getId());
        bundle.putString(MODE_ID, zenMode.getId());
        // TODO(b/332937635): Update metrics category
        preference.setIntent(new SubSettingLauncher(mContext)
                .setDestination(ZenModeCallsFragment.class.getName())
                .setSourceMetricsCategory(0)
                .setArguments(bundle)
                .toIntent());
        preference.setSummary(mSummaryHelper.getCallsSettingSummary(getMode()));
        preference.setSummary(mSummaryHelper.getCallsSettingSummary(zenMode));
    }
}
 No newline at end of file
+25 −37
Original line number Diff line number Diff line
@@ -16,16 +16,10 @@

package com.android.settings.notification.modes;

import static android.service.notification.ZenPolicy.PRIORITY_CATEGORY_ALARMS;
import static android.service.notification.ZenPolicy.PRIORITY_CATEGORY_EVENTS;
import static android.service.notification.ZenPolicy.PRIORITY_CATEGORY_MEDIA;
import static android.service.notification.ZenPolicy.PRIORITY_CATEGORY_REMINDERS;
import static android.service.notification.ZenPolicy.PRIORITY_CATEGORY_SYSTEM;

import android.app.AutomaticZenRule;
import android.content.Context;
import android.service.notification.ZenDeviceEffects;
import android.service.notification.ZenPolicy;

import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.TwoStatePreference;

@@ -38,9 +32,9 @@ public class ZenModeDisplayEffectPreferenceController extends AbstractZenModePre
    }

    @Override
    public void updateState(Preference preference) {
    public void updateState(Preference preference, @NonNull ZenMode zenMode) {
        TwoStatePreference pref = (TwoStatePreference) preference;
        ZenDeviceEffects effects =  getMode().getRule().getDeviceEffects();
        ZenDeviceEffects effects =  zenMode.getRule().getDeviceEffects();
        if (effects == null) {
            pref.setChecked(false);
        } else {
@@ -62,13 +56,11 @@ public class ZenModeDisplayEffectPreferenceController extends AbstractZenModePre
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
    public boolean onPreferenceChange(@NonNull Preference preference, Object newValue) {
        final boolean allow = (Boolean) newValue;

        ZenDeviceEffects currEffects =  getMode().getRule().getDeviceEffects();
        ZenDeviceEffects.Builder updatedEffects = currEffects == null
                ? new ZenDeviceEffects.Builder()
                : new ZenDeviceEffects.Builder(getMode().getRule().getDeviceEffects());
        return saveMode(zenMode -> {
            ZenDeviceEffects.Builder updatedEffects = new ZenDeviceEffects.Builder(
                    zenMode.getDeviceEffects());
            switch (getPreferenceKey()) {
                case "effect_greyscale":
                    updatedEffects.setShouldDisplayGrayscale(allow);
@@ -83,12 +75,8 @@ public class ZenModeDisplayEffectPreferenceController extends AbstractZenModePre
                    updatedEffects.setShouldUseNightMode(allow);
                    break;
            }
        AutomaticZenRule updatedAzr = new AutomaticZenRule.Builder(getMode().getRule())
                .setDeviceEffects(updatedEffects.build())
                .build();
        getMode().setAzr(updatedAzr);
        mBackend.updateMode(getMode());

        return true;
            zenMode.getRule().setDeviceEffects(updatedEffects.build());
            return zenMode;
        });
    }
}
+9 −7
Original line number Diff line number Diff line
@@ -20,12 +20,15 @@ import static com.android.settings.notification.modes.ZenModeFragmentBase.MODE_I

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.preference.Preference;

import com.android.settings.core.SubSettingLauncher;

public class ZenModeDisplayLinkPreferenceController extends AbstractZenModePreferenceController  {
class ZenModeDisplayLinkPreferenceController extends AbstractZenModePreferenceController  {

    ZenModeSummaryHelper mSummaryHelper;
    private final ZenModeSummaryHelper mSummaryHelper;

    public ZenModeDisplayLinkPreferenceController(Context context, String key,
            ZenModesBackend backend) {
@@ -34,10 +37,9 @@ public class ZenModeDisplayLinkPreferenceController extends AbstractZenModePrefe
    }

    @Override
    public void updateState(Preference preference) {
        super.updateState(preference);
    void updateState(Preference preference, @NonNull ZenMode zenMode) {
        Bundle bundle = new Bundle();
        bundle.putString(MODE_ID, getMode().getId());
        bundle.putString(MODE_ID, zenMode.getId());
        // TODO(b/332937635): Update metrics category
        preference.setIntent(new SubSettingLauncher(mContext)
                .setDestination(ZenModeDisplayFragment.class.getName())
@@ -47,7 +49,7 @@ public class ZenModeDisplayLinkPreferenceController extends AbstractZenModePrefe
    }

    @Override
    public CharSequence getSummary() {
        return mSummaryHelper.getDisplayEffectsSummary(getMode());
    public CharSequence getSummary(@NonNull ZenMode zenMode) {
        return mSummaryHelper.getDisplayEffectsSummary(zenMode);
    }
}
 No newline at end of file
Loading