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