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 createPreferenceControllers(Context context)54 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { 55 ZenServiceListing serviceListing = new ZenServiceListing(getContext(), CONFIG); 56 serviceListing.reloadApprovedServices(); 57 return buildPreferenceControllers(context, this, serviceListing, getSettingsLifecycle()); 58 } 59 buildPreferenceControllers(Context context, Fragment parent, ZenServiceListing serviceListing, Lifecycle lifecycle)60 private static List<AbstractPreferenceController> buildPreferenceControllers(Context context, 61 Fragment parent, ZenServiceListing serviceListing, Lifecycle lifecycle) { 62 List<AbstractPreferenceController> controllers = new ArrayList<>(); 63 controllers.add(new ZenModeAddAutomaticRulePreferenceController(context, parent, 64 serviceListing, lifecycle)); 65 controllers.add(new ZenModeAutomaticRulesPreferenceController(context, parent, lifecycle)); 66 67 return controllers; 68 } 69 70 @Override getPreferenceScreenResId()71 protected int getPreferenceScreenResId() { 72 return R.xml.zen_mode_automation_settings; 73 } 74 75 @Override getMetricsCategory()76 public int getMetricsCategory() { 77 return SettingsEnums.NOTIFICATION_ZEN_MODE_AUTOMATION; 78 } 79 getConditionProviderConfig()80 protected 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 private final int DELETE_RULES = 1; 90 91 @Override onCreateOptionsMenu(Menu menu, MenuInflater inflater)92 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 93 menu.add(Menu.NONE, DELETE_RULES, Menu.NONE, R.string.zen_mode_delete_automatic_rules); 94 super.onCreateOptionsMenu(menu, inflater); 95 } 96 97 @Override onOptionsItemSelected(MenuItem item)98 public boolean onOptionsItemSelected(MenuItem item) { 99 switch (item.getItemId()) { 100 case DELETE_RULES: 101 Map.Entry<String, AutomaticZenRule>[] rules = mBackend.getAutomaticZenRules(); 102 mDeleteDialogRuleNames = new CharSequence[rules.length]; 103 mDeleteDialogRuleIds = new String[rules.length]; 104 mDeleteDialogChecked = new boolean[rules.length]; 105 for (int i = 0; i < rules.length; i++) { 106 mDeleteDialogRuleNames[i] = rules[i].getValue().getName(); 107 mDeleteDialogRuleIds[i] = rules[i].getKey(); 108 } 109 new AlertDialog.Builder(mContext) 110 .setTitle(R.string.zen_mode_delete_automatic_rules) 111 .setMultiChoiceItems(mDeleteDialogRuleNames, null, 112 new DialogInterface.OnMultiChoiceClickListener() { 113 @Override 114 public void onClick(DialogInterface dialog, int which, 115 boolean isChecked) { 116 mDeleteDialogChecked[which] = isChecked; 117 } 118 }) 119 .setPositiveButton(R.string.zen_mode_schedule_delete, 120 new DialogInterface.OnClickListener() { 121 @Override 122 public void onClick(DialogInterface dialog, int which) { 123 for (int i = 0; i < rules.length; i++) { 124 if (mDeleteDialogChecked[i]) { 125 mBackend.removeZenRule(mDeleteDialogRuleIds[i]); 126 } 127 } 128 } 129 }).show(); 130 return true; 131 default: 132 return super.onOptionsItemSelected(item); 133 } 134 } 135 136 /** 137 * For Search. 138 */ 139 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 140 new BaseSearchIndexProvider(R.xml.zen_mode_automation_settings) { 141 142 @Override 143 public List<String> getNonIndexableKeys(Context context) { 144 final List<String> keys = super.getNonIndexableKeys(context); 145 keys.add(ZenModeAddAutomaticRulePreferenceController.KEY); 146 keys.add(ZenModeAutomaticRulesPreferenceController.KEY); 147 return keys; 148 } 149 150 @Override 151 public List<AbstractPreferenceController> createPreferenceControllers( 152 Context context) { 153 return buildPreferenceControllers(context, null, null, null); 154 } 155 }; 156 } 157