1 /*
2  * Copyright (C) 2019 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;
18 
19 import android.content.Context;
20 import android.service.notification.Adjustment;
21 
22 import com.android.settings.core.TogglePreferenceController;
23 
24 import com.google.common.annotations.VisibleForTesting;
25 
26 import java.util.List;
27 
28 public class AssistantCapabilityPreferenceController extends TogglePreferenceController {
29 
30     static final String PRIORITIZER_KEY = "asst_capability_prioritizer";
31     static final String RANKING_KEY = "asst_capability_ranking";
32     static final String SMART_KEY = "asst_capabilities_actions_replies";
33     private NotificationBackend mBackend;
34 
AssistantCapabilityPreferenceController(Context context, String key)35     public AssistantCapabilityPreferenceController(Context context, String key) {
36         super(context, key);
37         mBackend = new NotificationBackend();
38     }
39 
40     @VisibleForTesting
setBackend(NotificationBackend backend)41     void setBackend(NotificationBackend backend) {
42         mBackend = backend;
43     }
44 
45     @Override
isChecked()46     public boolean isChecked() {
47         List<String> capabilities = mBackend.getAssistantAdjustments(mContext.getPackageName());
48         if (PRIORITIZER_KEY.equals(getPreferenceKey())) {
49             return capabilities.contains(Adjustment.KEY_IMPORTANCE);
50         } else if (RANKING_KEY.equals(getPreferenceKey())) {
51             return capabilities.contains(Adjustment.KEY_RANKING_SCORE);
52         } else if (SMART_KEY.equals(getPreferenceKey())) {
53             return capabilities.contains(Adjustment.KEY_CONTEXTUAL_ACTIONS)
54                     && capabilities.contains(Adjustment.KEY_TEXT_REPLIES);
55         }
56         return false;
57     }
58 
59     @Override
setChecked(boolean isChecked)60     public boolean setChecked(boolean isChecked) {
61         if (PRIORITIZER_KEY.equals(getPreferenceKey())) {
62             mBackend.allowAssistantAdjustment(Adjustment.KEY_IMPORTANCE, isChecked);
63         } else if (RANKING_KEY.equals(getPreferenceKey())) {
64             mBackend.allowAssistantAdjustment(Adjustment.KEY_RANKING_SCORE, isChecked);
65         } else if (SMART_KEY.equals(getPreferenceKey())) {
66             mBackend.allowAssistantAdjustment(Adjustment.KEY_CONTEXTUAL_ACTIONS, isChecked);
67             mBackend.allowAssistantAdjustment(Adjustment.KEY_TEXT_REPLIES, isChecked);
68         }
69         return true;
70     }
71 
72     @Override
getAvailabilityStatus()73     public int getAvailabilityStatus() {
74         return mBackend.getAllowedNotificationAssistant() != null
75                 ? AVAILABLE : DISABLED_DEPENDENT_SETTING;
76     }
77 }
78 
79 
80