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.content.Context;
21 import android.os.Bundle;
22 import android.util.Log;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceScreen;
26 
27 import com.android.settings.R;
28 import com.android.settingslib.core.AbstractPreferenceController;
29 
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 abstract class ZenCustomRuleSettingsBase extends ZenModeSettingsBase {
34     static final String TAG = "ZenCustomRuleSettings";
35     static final String RULE_ID = "RULE_ID";
36 
37     String mId;
38     AutomaticZenRule mRule;
39     List<AbstractPreferenceController> mControllers = new ArrayList<>();
40 
41     /**
42      * @return null if no preference category exists
43       */
getPreferenceCategoryKey()44     abstract String getPreferenceCategoryKey();
45 
46     @Override
onAttach(Context context)47     public void onAttach(Context context) {
48         super.onAttach(context);
49         Bundle bundle = getArguments();
50         if (bundle != null && bundle.containsKey(RULE_ID)) {
51             mId = bundle.getString(RULE_ID);
52             mRule = mBackend.getAutomaticZenRule(mId);
53         } else {
54             Log.d(TAG, "Rule id required to set custom dnd rule config settings");
55             this.finish();
56         }
57     }
58 
59     @Override
onZenModeConfigChanged()60     public void onZenModeConfigChanged() {
61         super.onZenModeConfigChanged();
62         updatePreferences();
63     }
64 
updatePreferences()65     public void updatePreferences() {
66         mRule = mBackend.getAutomaticZenRule(mId);
67         final PreferenceScreen screen = getPreferenceScreen();
68         String categoryKey = getPreferenceCategoryKey();
69         if (categoryKey != null) {
70             Preference prefCategory = screen.findPreference(categoryKey);
71             if (prefCategory != null) {
72                 prefCategory.setTitle(mContext.getResources().getString(
73                         com.android.settings.R.string.zen_mode_custom_behavior_category_title,
74                         mRule.getName()));
75             }
76         }
77 
78         for (AbstractPreferenceController controller : mControllers) {
79             AbstractZenCustomRulePreferenceController zenRuleController =
80                     (AbstractZenCustomRulePreferenceController) controller;
81             zenRuleController.onResume(mRule, mId);
82             zenRuleController.displayPreference(screen);
83             updatePreference(zenRuleController);
84         }
85     }
86 
87     @Override
getHelpResource()88     public int getHelpResource() {
89         return R.string.help_uri_interruptions;
90     }
91 
92     @Override
onResume()93     public void onResume() {
94         super.onResume();
95         updatePreferences();
96     }
97 
createZenRuleBundle()98     Bundle createZenRuleBundle() {
99         Bundle bundle = new Bundle();
100         bundle.putString(RULE_ID, mId);
101         return bundle;
102     }
103 }
104