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 final ZenModeRuleSettingsBase mFragment;
43     private String mId;
44     private AutomaticZenRule mRule;
45 
ZenRuleButtonsPreferenceController(Context context, ZenModeRuleSettingsBase fragment, Lifecycle lc)46     public ZenRuleButtonsPreferenceController(Context context, ZenModeRuleSettingsBase fragment,
47             Lifecycle lc) {
48         super(context, KEY, lc);
49         mFragment = fragment;
50     }
51 
setIdAndRule(String id, AutomaticZenRule rule)52     void setIdAndRule(String id, AutomaticZenRule rule) {
53         mId = id;
54         mRule = rule;
55     }
56 
57     @Override
isAvailable()58     public boolean isAvailable() {
59         return mRule != null;
60     }
61 
62     @Override
displayPreference(PreferenceScreen screen)63     public void displayPreference(PreferenceScreen screen) {
64         if (isAvailable()) {
65             ((ActionButtonsPreference) screen.findPreference(KEY))
66                     .setButton1Text(R.string.zen_mode_rule_name_edit)
67                     .setButton1Icon(com.android.internal.R.drawable.ic_mode_edit)
68                     .setButton1OnClickListener(new EditRuleNameClickListener())
69                     .setButton2Text(R.string.zen_mode_delete_rule_button)
70                     .setButton2Icon(R.drawable.ic_settings_delete)
71                     .setButton2OnClickListener(new DeleteRuleClickListener());
72         }
73     }
74 
75     public class EditRuleNameClickListener implements View.OnClickListener {
EditRuleNameClickListener()76         public EditRuleNameClickListener() {}
77 
78         @Override
onClick(View v)79         public void onClick(View v) {
80             ZenRuleNameDialog.show(mFragment, mRule.getName(), null,
81                     new ZenRuleNameDialog.PositiveClickListener() {
82                         @Override
83                         public void onOk(String ruleName, Fragment parent) {
84                             if (TextUtils.equals(ruleName, mRule.getName())) {
85                                 return;
86                             }
87                             mMetricsFeatureProvider.action(mContext,
88                                     SettingsEnums.ACTION_ZEN_MODE_RULE_NAME_CHANGE_OK);
89                             mRule.setName(ruleName);
90                             mRule.setModified(true);
91                             mBackend.updateZenRule(mId, mRule);
92                         }
93                     });
94         }
95     }
96 
97     public class DeleteRuleClickListener implements View.OnClickListener {
DeleteRuleClickListener()98         public DeleteRuleClickListener() {}
99 
100         @Override
onClick(View v)101         public void onClick(View v) {
102             ZenDeleteRuleDialog.show(mFragment, mRule.getName(), mId,
103                     new ZenDeleteRuleDialog.PositiveClickListener() {
104                         @Override
105                         public void onOk(String id) {
106                             mBackend.removeZenRule(id);
107                             mMetricsFeatureProvider.action(mContext,
108                                     SettingsEnums.ACTION_ZEN_DELETE_RULE_OK);
109                             mFragment.onRuleRemoved();
110                         }
111             });
112         }
113     }
114 }
115