Files
app_Settings/src/com/android/settings/notification/modes/ZenSettingsObserver.java
Matías Hernández b8b897e552 Improve lifecycle of ZenModeFragment & friends
* Don't keep Settings observers longer than start-stop.
* Only call updateState() once on controllers during create->start->resume.
* Remove some duplicate controller update methods from ZenModesFragmentBase (we can directly call DashboardFragment's).
* Don't update controllers if unrelated modes were changed.
* Extract ZenSettingsObserver for use in the link tile later.
* Add tests.

Fixes: 353946788
Test: atest com.android.settings.notification.modes
Flag: android.app.modes_ui
Change-Id: I64b51714d699b5c3a592a76fcb615d2999998829
2024-07-29 20:10:03 +02:00

69 lines
2.2 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 android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.provider.Settings;
import androidx.annotation.Nullable;
class ZenSettingsObserver extends ContentObserver {
private static final Uri ZEN_MODE_URI = Settings.Global.getUriFor(Settings.Global.ZEN_MODE);
private static final Uri ZEN_MODE_CONFIG_ETAG_URI = Settings.Global.getUriFor(
Settings.Global.ZEN_MODE_CONFIG_ETAG);
private final Context mContext;
@Nullable private Runnable mCallback;
ZenSettingsObserver(Context context) {
this(context, null);
}
ZenSettingsObserver(Context context, @Nullable Runnable callback) {
super(context.getMainExecutor(), 0);
mContext = context;
setOnChangeListener(callback);
}
void register() {
mContext.getContentResolver().registerContentObserver(ZEN_MODE_URI, false, this);
mContext.getContentResolver().registerContentObserver(ZEN_MODE_CONFIG_ETAG_URI, false,
this);
}
void unregister() {
mContext.getContentResolver().unregisterContentObserver(this);
}
void setOnChangeListener(@Nullable Runnable callback) {
mCallback = callback;
}
@Override
public void onChange(boolean selfChange, @Nullable Uri uri) {
super.onChange(selfChange, uri);
// Shouldn't have any other URIs trigger this method, but check just in case.
if (ZEN_MODE_URI.equals(uri) || ZEN_MODE_CONFIG_ETAG_URI.equals(uri)) {
if (mCallback != null) {
mCallback.run();
}
}
}
}