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;
18 
19 import static android.app.slice.Slice.EXTRA_TOGGLE_STATE;
20 
21 import android.annotation.ColorInt;
22 import android.app.NotificationManager;
23 import android.app.PendingIntent;
24 import android.app.settings.SettingsEnums;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.net.Uri;
29 import android.provider.Settings;
30 
31 import androidx.core.graphics.drawable.IconCompat;
32 import androidx.slice.Slice;
33 import androidx.slice.builders.ListBuilder;
34 import androidx.slice.builders.ListBuilder.RowBuilder;
35 import androidx.slice.builders.SliceAction;
36 
37 import com.android.settings.R;
38 import com.android.settings.SubSettings;
39 import com.android.settings.Utils;
40 import com.android.settings.slices.CustomSliceRegistry;
41 import com.android.settings.slices.SliceBroadcastReceiver;
42 import com.android.settings.slices.SliceBuilderUtils;
43 
44 public class ZenModeSliceBuilder {
45 
46     private static final String TAG = "ZenModeSliceBuilder";
47 
48     private static final String ZEN_MODE_SLICE_KEY = ZenModeButtonPreferenceController.KEY;
49 
50     /**
51      * Action notifying a change on the Zen Mode Slice.
52      */
53     public static final String ACTION_ZEN_MODE_SLICE_CHANGED =
54             "com.android.settings.notification.ZEN_MODE_CHANGED";
55 
56     public static final IntentFilter INTENT_FILTER = new IntentFilter();
57 
58     static {
59         INTENT_FILTER.addAction(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED);
60         INTENT_FILTER.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED);
61         INTENT_FILTER.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL);
62     }
63 
ZenModeSliceBuilder()64     private ZenModeSliceBuilder() {
65     }
66 
67     /**
68      * Return a ZenMode Slice bound to {@link CustomSliceRegistry#ZEN_MODE_URI}.
69      * <p>
70      * Note that you should register a listener for {@link #INTENT_FILTER} to get changes for
71      * ZenMode.
72      */
getSlice(Context context)73     public static Slice getSlice(Context context) {
74         final boolean isZenModeEnabled = isZenModeEnabled(context);
75         final CharSequence title = context.getText(R.string.zen_mode_settings_title);
76         final CharSequence subtitle = context.getText(R.string.zen_mode_slice_subtitle);
77         @ColorInt final int color = Utils.getColorAccentDefaultColor(context);
78         final PendingIntent toggleAction = getBroadcastIntent(context);
79         final PendingIntent primaryAction = getPrimaryAction(context);
80         final SliceAction primarySliceAction = SliceAction.createDeeplink(primaryAction,
81                 (IconCompat) null /* icon */, ListBuilder.ICON_IMAGE, title);
82         final SliceAction toggleSliceAction = SliceAction.createToggle(toggleAction,
83                 null /* actionTitle */,
84                 isZenModeEnabled);
85 
86         return new ListBuilder(context, CustomSliceRegistry.ZEN_MODE_SLICE_URI,
87                 ListBuilder.INFINITY)
88                 .setAccentColor(color)
89                 .addRow(new RowBuilder()
90                         .setTitle(title)
91                         .setSubtitle(subtitle)
92                         .addEndItem(toggleSliceAction)
93                         .setPrimaryAction(primarySliceAction))
94                 .build();
95     }
96 
97     /**
98      * Update the current ZenMode status to the boolean value keyed by
99      * {@link android.app.slice.Slice#EXTRA_TOGGLE_STATE} on {@param intent}.
100      */
handleUriChange(Context context, Intent intent)101     public static void handleUriChange(Context context, Intent intent) {
102         final boolean zenModeOn = intent.getBooleanExtra(EXTRA_TOGGLE_STATE, false);
103         final int zenMode;
104         if (zenModeOn) {
105             zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
106         } else {
107             zenMode = Settings.Global.ZEN_MODE_OFF;
108         }
109         NotificationManager.from(context).setZenMode(zenMode, null /* conditionId */, TAG);
110         // Do not notifyChange on Uri. The service takes longer to update the current value than it
111         // does for the Slice to check the current value again. Let {@link SliceBroadcastRelay}
112         // handle it.
113     }
114 
getIntent(Context context)115     public static Intent getIntent(Context context) {
116         final Uri contentUri = new Uri.Builder().appendPath(ZEN_MODE_SLICE_KEY).build();
117         final String screenTitle = context.getText(R.string.zen_mode_settings_title).toString();
118         return SliceBuilderUtils.buildSearchResultPageIntent(context,
119                 ZenModeSettings.class.getName(), ZEN_MODE_SLICE_KEY, screenTitle,
120                 SettingsEnums.NOTIFICATION_ZEN_MODE)
121                 .setClassName(context.getPackageName(), SubSettings.class.getName())
122                 .setData(contentUri);
123     }
124 
isZenModeEnabled(Context context)125     private static boolean isZenModeEnabled(Context context) {
126         final NotificationManager manager = context.getSystemService(NotificationManager.class);
127         final int zenMode = manager.getZenMode();
128 
129         switch (zenMode) {
130             case Settings.Global.ZEN_MODE_ALARMS:
131             case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS:
132             case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS:
133                 return true;
134             case Settings.Global.ZEN_MODE_OFF:
135             default:
136                 return false;
137         }
138     }
139 
getPrimaryAction(Context context)140     private static PendingIntent getPrimaryAction(Context context) {
141         final Intent intent = getIntent(context);
142         return PendingIntent.getActivity(context, 0 /* requestCode */, intent, 0 /* flags */);
143     }
144 
getBroadcastIntent(Context context)145     private static PendingIntent getBroadcastIntent(Context context) {
146         final Intent intent = new Intent(ACTION_ZEN_MODE_SLICE_CHANGED)
147                 .setClass(context, SliceBroadcastReceiver.class);
148         return PendingIntent.getBroadcast(context, 0 /* requestCode */, intent,
149                 PendingIntent.FLAG_CANCEL_CURRENT);
150     }
151 }
152