1 /*
2  * Copyright (C) 2017 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.zen;
18 
19 import android.app.AlertDialog;
20 import android.app.AutomaticZenRule;
21 import android.app.NotificationManager;
22 import android.app.settings.SettingsEnums;
23 import android.content.Context;
24 import android.content.DialogInterface;
25 import android.service.notification.ConditionProviderService;
26 import android.view.Menu;
27 import android.view.MenuInflater;
28 import android.view.MenuItem;
29 
30 import androidx.fragment.app.Fragment;
31 
32 import com.android.settings.R;
33 import com.android.settings.search.BaseSearchIndexProvider;
34 import com.android.settings.utils.ManagedServiceSettings;
35 import com.android.settings.utils.ZenServiceListing;
36 import com.android.settingslib.core.AbstractPreferenceController;
37 import com.android.settingslib.core.lifecycle.Lifecycle;
38 import com.android.settingslib.search.SearchIndexable;
39 
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.Map;
43 
44 @SearchIndexable
45 public class ZenModeAutomationSettings extends ZenModeSettingsBase {
46     public static final String DELETE = "DELETE_RULE";
47     protected final ManagedServiceSettings.Config CONFIG = getConditionProviderConfig();
48     private CharSequence[] mDeleteDialogRuleNames;
49     private String[] mDeleteDialogRuleIds;
50     private boolean[] mDeleteDialogChecked;
51 
52     @Override
createPreferenceControllers(Context context)53     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
54         ZenServiceListing serviceListing = new ZenServiceListing(getContext(), CONFIG);
55         serviceListing.reloadApprovedServices();
56         return buildPreferenceControllers(context, this, serviceListing, getSettingsLifecycle());
57     }
58 
buildPreferenceControllers(Context context, Fragment parent, ZenServiceListing serviceListing, Lifecycle lifecycle)59     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
60             Fragment parent, ZenServiceListing serviceListing, Lifecycle lifecycle) {
61         ZenModeBackend backend = new ZenModeBackend(context);
62         List<AbstractPreferenceController> controllers = new ArrayList<>();
63         controllers.add(new ZenModeAddAutomaticRulePreferenceController(context, parent,
64                 serviceListing, lifecycle));
65         controllers.add(new ZenModeAutomaticRulesPreferenceController(
66                 context, parent, lifecycle, backend));
67 
68         return controllers;
69     }
70 
71     @Override
getPreferenceScreenResId()72     protected int getPreferenceScreenResId() {
73         return R.xml.zen_mode_automation_settings;
74     }
75 
76     @Override
getMetricsCategory()77     public int getMetricsCategory() {
78         return SettingsEnums.NOTIFICATION_ZEN_MODE_AUTOMATION;
79     }
80 
getConditionProviderConfig()81     protected static ManagedServiceSettings.Config getConditionProviderConfig() {
82         return new ManagedServiceSettings.Config.Builder()
83                 .setTag(TAG)
84                 .setIntentAction(ConditionProviderService.SERVICE_INTERFACE)
85                 .setConfigurationIntentAction(NotificationManager.ACTION_AUTOMATIC_ZEN_RULE)
86                 .setPermission(android.Manifest.permission.BIND_CONDITION_PROVIDER_SERVICE)
87                 .setNoun("condition provider")
88                 .build();
89     }
90     private final int DELETE_RULES = 1;
91 
92     @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)93     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
94         menu.add(Menu.NONE, DELETE_RULES, Menu.NONE, R.string.zen_mode_delete_automatic_rules);
95         super.onCreateOptionsMenu(menu, inflater);
96     }
97 
98     @Override
onOptionsItemSelected(MenuItem item)99     public boolean onOptionsItemSelected(MenuItem item) {
100         switch (item.getItemId()) {
101             case DELETE_RULES:
102                 Map.Entry<String, AutomaticZenRule>[] rules = mBackend.getAutomaticZenRules();
103                 mDeleteDialogRuleNames = new CharSequence[rules.length];
104                 mDeleteDialogRuleIds = new String[rules.length];
105                 mDeleteDialogChecked = new boolean[rules.length];
106                 for (int i = 0; i < rules.length; i++) {
107                     mDeleteDialogRuleNames[i] = rules[i].getValue().getName();
108                     mDeleteDialogRuleIds[i] = rules[i].getKey();
109                 }
110                 new AlertDialog.Builder(mContext)
111                         .setTitle(R.string.zen_mode_delete_automatic_rules)
112                         .setMultiChoiceItems(mDeleteDialogRuleNames, null,
113                                 new DialogInterface.OnMultiChoiceClickListener() {
114                                     @Override
115                                     public void onClick(DialogInterface dialog, int which,
116                                             boolean isChecked) {
117                                         mDeleteDialogChecked[which] = isChecked;
118                                     }
119                                 })
120                         .setPositiveButton(R.string.zen_mode_schedule_delete,
121                                 new DialogInterface.OnClickListener() {
122                             @Override
123                             public void onClick(DialogInterface dialog, int which) {
124                                 for (int i = 0; i < rules.length; i++) {
125                                     if (mDeleteDialogChecked[i]) {
126                                         mBackend.removeZenRule(mDeleteDialogRuleIds[i]);
127                                     }
128                                 }
129                             }
130                         }).show();
131                 return true;
132             default:
133                 return super.onOptionsItemSelected(item);
134         }
135     }
136 
137     /**
138      * For Search.
139      */
140     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
141             new BaseSearchIndexProvider(R.xml.zen_mode_automation_settings) {
142 
143                 @Override
144                 public List<String> getNonIndexableKeys(Context context) {
145                     final List<String> keys = super.getNonIndexableKeys(context);
146                     keys.add(ZenModeAddAutomaticRulePreferenceController.KEY);
147                     keys.add(ZenModeAutomaticRulesPreferenceController.KEY);
148                     return keys;
149                 }
150 
151                 @Override
152                 public List<AbstractPreferenceController> createPreferenceControllers(
153                         Context context) {
154                     return buildPreferenceControllers(context, null, null, null);
155                 }
156             };
157 }
158