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     private boolean mIsFirstLaunch = true;
41 
42     /**
43      * @return null if no preference category exists
44      */
getPreferenceCategoryKey()45     abstract String getPreferenceCategoryKey();
46 
47     @Override
onAttach(Context context)48     public void onAttach(Context context) {
49         super.onAttach(context);
50         Bundle bundle = getArguments();
51         if (bundle != null && bundle.containsKey(RULE_ID)) {
52             mId = bundle.getString(RULE_ID);
53             updateRule();
54         } else {
55             Log.d(TAG, "Rule id required to set custom dnd rule config settings");
56             this.finish();
57         }
58     }
59 
60     @Override
onResume()61     public void onResume() {
62         if (!mIsFirstLaunch) {
63             // Rule will be used in updatePreferenceStates() in super.onResume().
64             updateRule();
65         }
66         super.onResume();
67         updatePreferences();
68     }
69 
70     @Override
onZenModeConfigChanged()71     public void onZenModeConfigChanged() {
72         super.onZenModeConfigChanged();
73         updateRule();
74         updatePreferences();
75         updatePreferenceStates();
76     }
77 
updateRule()78     private void updateRule() {
79         mRule = mBackend.getAutomaticZenRule(mId);
80         for (AbstractPreferenceController controller : mControllers) {
81             AbstractZenCustomRulePreferenceController zenRuleController =
82                     (AbstractZenCustomRulePreferenceController) controller;
83             zenRuleController.setIdAndRule(mId, mRule);
84         }
85     }
86 
updatePreferences()87     public void updatePreferences() {
88         final PreferenceScreen screen = getPreferenceScreen();
89         String categoryKey = getPreferenceCategoryKey();
90         if (categoryKey != null) {
91             Preference prefCategory = screen.findPreference(categoryKey);
92             if (prefCategory != null) {
93                 prefCategory.setTitle(mContext.getResources().getString(
94                         com.android.settings.R.string.zen_mode_custom_behavior_category_title,
95                         mRule.getName()));
96             }
97         }
98 
99         for (AbstractPreferenceController controller : mControllers) {
100             AbstractZenCustomRulePreferenceController zenRuleController =
101                     (AbstractZenCustomRulePreferenceController) controller;
102             zenRuleController.onResume();
103             if (!mIsFirstLaunch) {
104                 // In first launch, displayPreference() is already called in DashboardFragment's
105                 // onCreate().
106                 zenRuleController.displayPreference(screen);
107             }
108         }
109 
110         mIsFirstLaunch = false;
111     }
112 
113     @Override
getHelpResource()114     public int getHelpResource() {
115         return R.string.help_uri_interruptions;
116     }
117 
createZenRuleBundle()118     Bundle createZenRuleBundle() {
119         Bundle bundle = new Bundle();
120         bundle.putString(RULE_ID, mId);
121         return bundle;
122     }
123 }
124