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
This commit is contained in:
Matías Hernández
2024-05-23 15:51:03 +00:00
parent 51dc721031
commit 43d67b0080
17 changed files with 213 additions and 199 deletions

View File

@@ -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);
}
}
@Nullable
public AutomaticZenRule getAZR() {
if (mZenMode == null || mZenMode.getRule() == null) {
abstract void updateState(Preference preference, @NonNull ZenMode zenMode);
@Override
public final CharSequence getSummary() {
if (mZenMode != null) {
return getSummary(mZenMode);
} else {
return null;
}
return mZenMode.getRule();
}
/** Implementations of this class should override
* {@link AbstractPreferenceController#updateState(Preference)} to specify what should
* happen when the preference is updated */
@Nullable
protected CharSequence getSummary(@NonNull ZenMode zenMode) {
return null;
}
/**
* 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;
}
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;
});
}
}