1 /* 2 * Copyright (C) 2020 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 17 package com.android.settings.notification.app; 18 19 import android.app.NotificationChannel; 20 import android.content.Context; 21 22 import androidx.preference.Preference; 23 24 import com.android.settings.R; 25 import com.android.settings.notification.NotificationBackend; 26 import com.android.settingslib.RestrictedSwitchPreference; 27 import com.android.settingslib.widget.SettingsThemeHelper; 28 29 public class InvalidConversationPreferenceController extends NotificationPreferenceController 30 implements Preference.OnPreferenceChangeListener { 31 32 private static final String KEY = "invalid_conversation_switch"; 33 InvalidConversationPreferenceController(Context context, NotificationBackend backend)34 public InvalidConversationPreferenceController(Context context, NotificationBackend backend) { 35 super(context, backend); 36 } 37 38 @Override getPreferenceKey()39 public String getPreferenceKey() { 40 return KEY; 41 } 42 43 @Override isAvailable()44 public boolean isAvailable() { 45 if (mAppRow == null) { 46 return false; 47 } 48 if (mAppRow.banned) { 49 return false; 50 } 51 if (mPreferenceFilter != null && !isIncludedInFilter()) { 52 return false; 53 } 54 return mBackend.isInInvalidMsgState(mAppRow.pkg, mAppRow.uid); 55 } 56 57 @Override isIncludedInFilter()58 boolean isIncludedInFilter() { 59 return mPreferenceFilter.contains(NotificationChannel.EDIT_CONVERSATION); 60 } 61 62 @Override updateState(Preference preference)63 public void updateState(Preference preference) { 64 if (mAppRow == null) { 65 return; 66 } 67 RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference; 68 pref.setDisabledByAdmin(mAdmin); 69 pref.setEnabled(!pref.isDisabledByAdmin()); 70 pref.setChecked(!mBackend.hasUserDemotedInvalidMsgApp(mAppRow.pkg, mAppRow.uid)); 71 if (SettingsThemeHelper.isExpressiveTheme(mContext)) { 72 if (preference.getParent() != null) { 73 preference.getParent().setVisible(true); 74 } 75 preference.setSummary(mContext.getString( 76 R.string.conversation_section_switch_complete_summary)); 77 } else { 78 preference.setSummary(mContext.getString(R.string.conversation_section_switch_summary)); 79 } 80 } 81 82 @Override onPreferenceChange(Preference preference, Object newValue)83 public boolean onPreferenceChange(Preference preference, Object newValue) { 84 if (mAppRow == null) { 85 return false; 86 } 87 mBackend.setInvalidMsgAppDemoted(mAppRow.pkg, mAppRow.uid, !((Boolean) newValue)); 88 return true; 89 } 90 } 91