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 import android.text.TextUtils;
22 
23 import androidx.preference.Preference;
24 
25 import com.android.settings.SettingsPreferenceFragment;
26 import com.android.settings.core.PreferenceControllerMixin;
27 import com.android.settings.notification.NotificationBackend;
28 import com.android.settingslib.RestrictedSwitchPreference;
29 
30 public class ConversationPromotePreferenceController extends NotificationPreferenceController
31         implements PreferenceControllerMixin {
32 
33     private static final String KEY = "convo_promote";
34 
35     SettingsPreferenceFragment mHostFragment;
36 
ConversationPromotePreferenceController(Context context, SettingsPreferenceFragment hostFragment, NotificationBackend backend)37     public ConversationPromotePreferenceController(Context context,
38             SettingsPreferenceFragment hostFragment,
39             NotificationBackend backend) {
40         super(context, backend);
41         mHostFragment = hostFragment;
42     }
43 
44     @Override
getPreferenceKey()45     public String getPreferenceKey() {
46         return KEY;
47     }
48 
49     @Override
isAvailable()50     public boolean isAvailable() {
51         if (!super.isAvailable()) {
52             return false;
53         }
54         if (mAppRow == null || mChannel == null) {
55             return false;
56         }
57         return !TextUtils.isEmpty(mChannel.getConversationId()) && mChannel.isDemoted();
58     }
59 
60     @Override
isIncludedInFilter()61     boolean isIncludedInFilter() {
62         return mPreferenceFilter.contains(NotificationChannel.EDIT_CONVERSATION);
63     }
64 
updateState(Preference preference)65     public void updateState(Preference preference) {
66         preference.setEnabled(mAdmin == null);
67     }
68 
69     @Override
handlePreferenceTreeClick(Preference preference)70     public boolean handlePreferenceTreeClick(Preference preference) {
71         if (mChannel == null || !KEY.equals(preference.getKey())) {
72             return false;
73         }
74         mChannel.setDemoted(false);
75         mChannel.setBypassDnd(false);
76         saveChannel();
77 
78         if (mHostFragment != null) {
79             mHostFragment.getActivity().finish();
80         }
81 
82         return true;
83     }
84 }
85