Instead of a the plain appearance of a EntityHeaderController, make the top icon bigger and use the same circled style as the choices in the list. Also highlight the current icon in the list as selected, even if it is the default for the mode type. Also cleaned up controllers that don't need a ZenModesBackend to not receive it. (Both of these changes also line up with the "new mode" fragment that is incoming). Bug: 333901673 Bug: 326442408 Test: atest com.android.settings.notification.modes Flag: android.app.modes_ui Change-Id: I0c9f3e34019a1a6c48658933dde545ad8d7399ae
171 lines
5.5 KiB
Java
171 lines
5.5 KiB
Java
/*
|
|
* Copyright (C) 2024 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.notification.modes;
|
|
|
|
import static com.google.common.base.Preconditions.checkState;
|
|
|
|
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;
|
|
import androidx.preference.Preference;
|
|
import androidx.preference.PreferenceScreen;
|
|
|
|
import com.android.settingslib.core.AbstractPreferenceController;
|
|
import com.android.settingslib.notification.modes.ZenMode;
|
|
import com.android.settingslib.notification.modes.ZenModesBackend;
|
|
|
|
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 final ZenModesBackend mBackend;
|
|
|
|
|
|
@Nullable // only until setZenMode() is called
|
|
private ZenMode mZenMode;
|
|
|
|
@NonNull
|
|
private final String mKey;
|
|
|
|
/**
|
|
* Constructor suitable for "read-only" controllers (e.g. link to a different sub-screen.
|
|
* Controllers that call this constructor to initialize themselves <em>cannot</em> call
|
|
* {@link #saveMode} or {@link #savePolicy} later.
|
|
*/
|
|
AbstractZenModePreferenceController(@NonNull Context context, @NonNull String key) {
|
|
super(context);
|
|
mKey = key;
|
|
mBackend = null;
|
|
}
|
|
|
|
/**
|
|
* Constructor suitable for controllers that will update the associated {@link ZenMode}.
|
|
* Controllers that call this constructor to initialize themselves may call {@link #saveMode} or
|
|
* {@link #savePolicy} later.
|
|
*/
|
|
AbstractZenModePreferenceController(@NonNull Context context, @NonNull String key,
|
|
@NonNull ZenModesBackend backend) {
|
|
super(context);
|
|
mKey = key;
|
|
mBackend = backend;
|
|
}
|
|
|
|
@Override
|
|
@NonNull
|
|
public String getPreferenceKey() {
|
|
return mKey;
|
|
}
|
|
|
|
@Override
|
|
public boolean isAvailable() {
|
|
if (mZenMode != null) {
|
|
return Flags.modesUi() && isAvailable(mZenMode);
|
|
} else {
|
|
return Flags.modesUi();
|
|
}
|
|
}
|
|
|
|
public boolean isAvailable(@NonNull ZenMode zenMode) {
|
|
return true;
|
|
}
|
|
|
|
// Called by parent Fragment onAttach, for any methods (such as isAvailable()) that need
|
|
// zen mode info before onStart. Most callers should use updateZenMode instead, which will
|
|
// do any further necessary propagation.
|
|
protected final void setZenMode(@NonNull ZenMode zenMode) {
|
|
mZenMode = zenMode;
|
|
}
|
|
|
|
// Called by the parent Fragment onStart, which means it will happen before resume.
|
|
public void updateZenMode(@NonNull Preference preference, @NonNull ZenMode zenMode) {
|
|
mZenMode = zenMode;
|
|
updateState(preference);
|
|
}
|
|
|
|
@Override
|
|
public void displayPreference(PreferenceScreen screen) {
|
|
super.displayPreference(screen);
|
|
if (mZenMode != null) {
|
|
displayPreference(screen, mZenMode);
|
|
}
|
|
}
|
|
|
|
public void displayPreference(PreferenceScreen screen, @NonNull ZenMode zenMode) {}
|
|
|
|
@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
|
|
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) {
|
|
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;
|
|
});
|
|
}
|
|
}
|