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 
17 package com.android.settings.notification.modes;
18 
19 import android.app.NotificationManager;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 import android.service.notification.ConditionProviderService;
23 
24 import androidx.annotation.Nullable;
25 import androidx.fragment.app.Fragment;
26 
27 import com.android.settings.R;
28 import com.android.settings.search.BaseSearchIndexProvider;
29 import com.android.settings.utils.ManagedServiceSettings;
30 import com.android.settings.utils.ZenServiceListing;
31 import com.android.settingslib.core.AbstractPreferenceController;
32 import com.android.settingslib.search.SearchIndexable;
33 
34 import com.google.common.collect.ImmutableList;
35 
36 import java.util.List;
37 
38 @SearchIndexable
39 public class ZenModesListFragment extends ZenModesFragmentBase {
40 
41     private static final ManagedServiceSettings.Config CONFIG = getConditionProviderConfig();
42 
43     @Override
createPreferenceControllers(Context context)44     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
45         ZenServiceListing serviceListing = new ZenServiceListing(getContext(), CONFIG);
46         serviceListing.reloadApprovedServices();
47         return buildPreferenceControllers(context, this, serviceListing);
48     }
49 
buildPreferenceControllers(Context context, @Nullable Fragment parent, @Nullable ZenServiceListing serviceListing)50     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
51             @Nullable Fragment parent, @Nullable ZenServiceListing serviceListing) {
52         // We need to redefine ZenModesBackend here even though mBackend exists so that this method
53         // can be static; it must be static to be able to be used in SEARCH_INDEX_DATA_PROVIDER.
54         ZenModesBackend backend = ZenModesBackend.getInstance(context);
55 
56         return ImmutableList.of(
57                 new ZenModesListPreferenceController(context, parent, backend),
58                 new ZenModesListAddModePreferenceController(context, backend, serviceListing)
59         );
60     }
61 
62     @Override
updateZenModeState()63     protected void updateZenModeState() {
64         // TODO: b/322373473 -- update any overall description of modes state here if necessary.
65         // Note the preferences linking to individual rules do not need to be updated, as
66         // updateState() is called on all preference controllers whenever the page is resumed.
67     }
68 
69     @Override
getPreferenceScreenResId()70     protected int getPreferenceScreenResId() {
71         return R.xml.modes_list_settings;
72     }
73 
74     @Override
getMetricsCategory()75     public int getMetricsCategory() {
76         // TODO: b/332937635 - add new & set metrics categories correctly
77         return SettingsEnums.NOTIFICATION_ZEN_MODE_AUTOMATION;
78     }
79 
getConditionProviderConfig()80     private static ManagedServiceSettings.Config getConditionProviderConfig() {
81         return new ManagedServiceSettings.Config.Builder()
82                 .setTag(TAG)
83                 .setIntentAction(ConditionProviderService.SERVICE_INTERFACE)
84                 .setConfigurationIntentAction(NotificationManager.ACTION_AUTOMATIC_ZEN_RULE)
85                 .setPermission(android.Manifest.permission.BIND_CONDITION_PROVIDER_SERVICE)
86                 .setNoun("condition provider")
87                 .build();
88     }
89 
90     /**
91      * For Search.
92      */
93     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
94             new BaseSearchIndexProvider(R.xml.modes_list_settings) {
95 
96                 @Override
97                 public List<String> getNonIndexableKeys(Context context) {
98                     final List<String> keys = super.getNonIndexableKeys(context);
99                     // TODO: b/332937523 - determine if this should be removed once the preference
100                     //                     controller adds dynamic data to index
101                     keys.add(ZenModesListPreferenceController.KEY);
102                     return keys;
103                 }
104 
105                 @Override
106                 public List<AbstractPreferenceController> createPreferenceControllers(
107                         Context context) {
108                     return buildPreferenceControllers(context, null, null);
109                 }
110             };
111 }
112