1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.settings.notification.app; 17 18 import android.app.Flags; 19 import android.content.Context; 20 21 import androidx.annotation.NonNull; 22 import androidx.preference.Preference; 23 24 import com.android.settings.notification.NotificationBackend; 25 import com.android.settingslib.RestrictedSwitchPreference; 26 27 public class PromotedNotificationsPreferenceController extends 28 NotificationPreferenceController implements Preference.OnPreferenceChangeListener { 29 protected static final String KEY_PROMOTED_SWITCH = "promoted_switch"; 30 PromotedNotificationsPreferenceController(@onNull Context context, @NonNull NotificationBackend backend)31 public PromotedNotificationsPreferenceController(@NonNull Context context, 32 @NonNull NotificationBackend backend) { 33 super(context, backend); 34 } 35 36 @Override 37 @NonNull getPreferenceKey()38 public String getPreferenceKey() { 39 return KEY_PROMOTED_SWITCH; 40 } 41 42 @Override isAvailable()43 public boolean isAvailable() { 44 if (!Flags.uiRichOngoing()) { 45 return false; 46 } 47 return super.isAvailable(); 48 } 49 50 @Override isIncludedInFilter()51 boolean isIncludedInFilter() { 52 // not a channel-specific preference; only at the app level 53 return false; 54 } 55 56 /** 57 * Updates the state of the promoted notifications switch. 58 */ updateState(@onNull Preference preference)59 public void updateState(@NonNull Preference preference) { 60 RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference; 61 if (pref.getParent() != null) { 62 pref.getParent().setVisible(true); 63 } 64 65 if (pref != null && mAppRow != null) { 66 pref.setDisabledByAdmin(mAdmin); 67 pref.setEnabled(!pref.isDisabledByAdmin()); 68 pref.setChecked(mAppRow.canBePromoted); 69 pref.setOnPreferenceChangeListener(this); 70 } 71 } 72 73 @Override onPreferenceChange(@onNull Preference preference, @NonNull Object newValue)74 public boolean onPreferenceChange(@NonNull Preference preference, @NonNull Object newValue) { 75 final boolean canBePromoted = (Boolean) newValue; 76 if (mAppRow != null && mAppRow.canBePromoted != canBePromoted) { 77 mAppRow.canBePromoted = canBePromoted; 78 mBackend.setCanBePromoted(mAppRow.pkg, mAppRow.uid, canBePromoted); 79 } 80 return true; 81 } 82 } 83