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