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