• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.modes;
18 
19 import static android.provider.Settings.EXTRA_AUTOMATIC_ZEN_RULE_ID;
20 
21 import android.os.Bundle;
22 import android.util.Log;
23 import android.widget.Toast;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.lifecycle.Lifecycle;
28 
29 import com.android.settings.R;
30 import com.android.settingslib.notification.modes.ZenMode;
31 
32 import java.util.List;
33 
34 /**
35  * Base class for Settings pages used to configure individual modes.
36  */
37 abstract class ZenModeFragmentBase extends ZenModesFragmentBase {
38     static final String TAG = "ZenModeSettings";
39 
40     @Nullable private ZenMode mZenMode;
41     @Nullable private ZenMode mModeOnLastControllerUpdate;
42 
43     @Override
onCreate(Bundle icicle)44     public void onCreate(Bundle icicle) {
45         mZenMode = loadModeFromArguments();
46         if (mZenMode != null) {
47             // Propagate mode info through to controllers. Must be done before super.onCreate(),
48             // because that one calls AbstractPreferenceController.isAvailable().
49             for (var controller : getZenPreferenceControllers()) {
50                 controller.setZenMode(mZenMode);
51             }
52         } else {
53             toastAndFinish();
54         }
55 
56         super.onCreate(icicle);
57     }
58 
59     @Nullable
loadModeFromArguments()60     private ZenMode loadModeFromArguments() {
61         String id = null;
62         if (getActivity() != null && getActivity().getIntent() != null) {
63             id = getActivity().getIntent().getStringExtra(EXTRA_AUTOMATIC_ZEN_RULE_ID);
64         }
65         Bundle bundle = getArguments();
66         if (id == null && bundle != null && bundle.containsKey(EXTRA_AUTOMATIC_ZEN_RULE_ID)) {
67             id = bundle.getString(EXTRA_AUTOMATIC_ZEN_RULE_ID);
68         }
69         if (id == null) {
70             Log.d(TAG, "No id provided");
71             return null;
72         }
73 
74         ZenMode mode = mBackend.getMode(id);
75         if (mode == null) {
76             Log.d(TAG, "Mode with id " + id + " not found");
77             return null;
78         }
79         return mode;
80     }
81 
getZenPreferenceControllers()82     private Iterable<AbstractZenModePreferenceController> getZenPreferenceControllers() {
83         return getPreferenceControllers().stream()
84                 .flatMap(List::stream)
85                 .filter(AbstractZenModePreferenceController.class::isInstance)
86                 .map(AbstractZenModePreferenceController.class::cast)
87                 .toList();
88     }
89 
90     @Override
onUpdatedZenModeState()91     protected void onUpdatedZenModeState() {
92         if (mZenMode == null) {
93             Log.wtf(TAG, "mZenMode is null in onUpdatedZenModeState");
94             toastAndFinish();
95             return;
96         }
97 
98         String id = mZenMode.getId();
99         ZenMode mode = mBackend.getMode(id);
100         if (mode == null) {
101             Log.d(TAG, "Mode id=" + id + " not found");
102             toastAndFinish();
103             return;
104         }
105 
106         mZenMode = mode;
107         maybeUpdateControllersState(mode);
108     }
109 
110     /**
111      * Updates all {@link AbstractZenModePreferenceController} based on the loaded mode info.
112      * For each controller, {@link AbstractZenModePreferenceController#setZenMode} will be called.
113      * Then, {@link AbstractZenModePreferenceController#updateState} will be called as well, unless
114      * we determine it's not necessary (for example, if we know that {@code DashboardFragment} will
115      * do it soon).
116      */
maybeUpdateControllersState(@onNull ZenMode zenMode)117     private void maybeUpdateControllersState(@NonNull ZenMode zenMode) {
118         boolean needsFullUpdate =
119                 getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)
120                 && (mModeOnLastControllerUpdate == null
121                         || !mModeOnLastControllerUpdate.equals(zenMode));
122         mModeOnLastControllerUpdate = zenMode.copy();
123 
124         for (var controller : getZenPreferenceControllers()) {
125             controller.setZenMode(zenMode);
126         }
127 
128         if (needsFullUpdate) {
129             forceUpdatePreferences();
130         }
131     }
132 
toastAndFinish()133     private void toastAndFinish() {
134         Toast.makeText(mContext, R.string.zen_mode_not_found_text, Toast.LENGTH_SHORT)
135                 .show();
136         this.finish();
137     }
138 
139     /**
140      * Get current mode data.
141      */
142     @Nullable
getMode()143     public ZenMode getMode() {
144         return mZenMode;
145     }
146 }
147