1 /*
2  * Copyright (C) 2018 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.AutomaticZenRule;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.Bundle;
24 import android.text.TextUtils;
25 import android.view.View;
26 
27 import androidx.fragment.app.Fragment;
28 import androidx.preference.PreferenceFragmentCompat;
29 import androidx.preference.PreferenceScreen;
30 
31 import com.android.internal.logging.nano.MetricsProto;
32 import com.android.settings.R;
33 import com.android.settings.core.PreferenceControllerMixin;
34 import com.android.settings.core.SubSettingLauncher;
35 import com.android.settingslib.core.lifecycle.Lifecycle;
36 import com.android.settingslib.widget.ActionButtonsPreference;
37 
38 public class ZenRuleButtonsPreferenceController extends AbstractZenModePreferenceController
39     implements PreferenceControllerMixin {
40     public static final String KEY = "zen_action_buttons";
41 
42     private AutomaticZenRule mRule;
43     private String mId;
44     private PreferenceFragmentCompat mFragment;
45     private ActionButtonsPreference mButtonsPref;
46 
47 
ZenRuleButtonsPreferenceController(Context context, PreferenceFragmentCompat fragment, Lifecycle lc)48     public ZenRuleButtonsPreferenceController(Context context, PreferenceFragmentCompat fragment,
49             Lifecycle lc) {
50         super(context, KEY, lc);
51         mFragment = fragment;
52     }
53 
54 
55     @Override
isAvailable()56     public boolean isAvailable() {
57         return mRule != null;
58     }
59 
60     @Override
displayPreference(PreferenceScreen screen)61     public void displayPreference(PreferenceScreen screen) {
62         if (isAvailable()) {
63             mButtonsPref = ((ActionButtonsPreference) screen.findPreference(KEY))
64                     .setButton1Text(R.string.zen_mode_rule_name_edit)
65                     .setButton1Icon(com.android.internal.R.drawable.ic_mode_edit)
66                     .setButton1OnClickListener(new EditRuleNameClickListener())
67                     .setButton2Text(R.string.zen_mode_delete_rule_button)
68                     .setButton2Icon(R.drawable.ic_settings_delete)
69                     .setButton2OnClickListener(new DeleteRuleClickListener());
70         }
71     }
72 
73     public class EditRuleNameClickListener implements View.OnClickListener {
EditRuleNameClickListener()74         public EditRuleNameClickListener() {}
75 
76         @Override
onClick(View v)77         public void onClick(View v) {
78             ZenRuleNameDialog.show(mFragment, mRule.getName(), null,
79                     new ZenRuleNameDialog.PositiveClickListener() {
80                         @Override
81                         public void onOk(String ruleName, Fragment parent) {
82                             if (TextUtils.equals(ruleName, mRule.getName())) {
83                                 return;
84                             }
85                             mMetricsFeatureProvider.action(mContext,
86                                     SettingsEnums.ACTION_ZEN_MODE_RULE_NAME_CHANGE_OK);
87                             mRule.setName(ruleName);
88                             mRule.setModified(true);
89                             mBackend.updateZenRule(mId, mRule);
90                         }
91                     });
92         }
93     }
94 
95     public class DeleteRuleClickListener implements View.OnClickListener {
DeleteRuleClickListener()96         public DeleteRuleClickListener() {}
97 
98         @Override
onClick(View v)99         public void onClick(View v) {
100             ZenDeleteRuleDialog.show(mFragment, mRule.getName(), mId,
101                     new ZenDeleteRuleDialog.PositiveClickListener() {
102                         @Override
103                         public void onOk(String id) {
104                             Bundle bundle = new Bundle();
105                             bundle.putString(ZenModeAutomationSettings.DELETE, id);
106                             mMetricsFeatureProvider.action(mContext,
107                                     SettingsEnums.ACTION_ZEN_DELETE_RULE_OK);
108                             new SubSettingLauncher(mContext)
109                                     .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
110                                     .setDestination(ZenModeAutomationSettings.class.getName())
111                                     .setSourceMetricsCategory(MetricsProto.MetricsEvent
112                                             .NOTIFICATION_ZEN_MODE_AUTOMATION)
113                                     .setArguments(bundle)
114                                     .launch();
115                         }
116             });
117         }
118     }
119 
onResume(AutomaticZenRule rule, String id)120     protected void onResume(AutomaticZenRule rule, String id) {
121         mRule = rule;
122         mId = id;
123     }
124 }
125