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 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 final RowBuilder rowBuilder = new RowBuilder() 86 .setTitle(title) 87 .addEndItem(toggleSliceAction) 88 .setPrimaryAction(primarySliceAction); 89 if (!Utils.isSettingsIntelligence(context)) { 90 rowBuilder.setSubtitle(subtitle); 91 } 92 93 return new ListBuilder(context, CustomSliceRegistry.ZEN_MODE_SLICE_URI, 94 ListBuilder.INFINITY) 95 .setAccentColor(color) 96 .addRow(rowBuilder) 97 .build(); 98 } 99 100 /** 101 * Update the current ZenMode status to the boolean value keyed by 102 * {@link android.app.slice.Slice#EXTRA_TOGGLE_STATE} on {@param intent}. 103 */ handleUriChange(Context context, Intent intent)104 public static void handleUriChange(Context context, Intent intent) { 105 final boolean zenModeOn = intent.getBooleanExtra(EXTRA_TOGGLE_STATE, false); 106 final int zenMode; 107 if (zenModeOn) { 108 zenMode = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; 109 } else { 110 zenMode = Settings.Global.ZEN_MODE_OFF; 111 } 112 NotificationManager.from(context).setZenMode(zenMode, null /* conditionId */, TAG); 113 // Do not notifyChange on Uri. The service takes longer to update the current value than it 114 // does for the Slice to check the current value again. Let {@link SliceBroadcastRelay} 115 // handle it. 116 } 117 getIntent(Context context)118 public static Intent getIntent(Context context) { 119 final Uri contentUri = new Uri.Builder().appendPath(ZEN_MODE_SLICE_KEY).build(); 120 final String screenTitle = context.getText(R.string.zen_mode_settings_title).toString(); 121 return SliceBuilderUtils.buildSearchResultPageIntent(context, 122 ZenModeSettings.class.getName(), ZEN_MODE_SLICE_KEY, screenTitle, 123 SettingsEnums.NOTIFICATION_ZEN_MODE) 124 .setClassName(context.getPackageName(), SubSettings.class.getName()) 125 .setData(contentUri); 126 } 127 isZenModeEnabled(Context context)128 private static boolean isZenModeEnabled(Context context) { 129 final NotificationManager manager = context.getSystemService(NotificationManager.class); 130 final int zenMode = manager.getZenMode(); 131 132 switch (zenMode) { 133 case Settings.Global.ZEN_MODE_ALARMS: 134 case Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: 135 case Settings.Global.ZEN_MODE_NO_INTERRUPTIONS: 136 return true; 137 case Settings.Global.ZEN_MODE_OFF: 138 default: 139 return false; 140 } 141 } 142 getPrimaryAction(Context context)143 private static PendingIntent getPrimaryAction(Context context) { 144 final Intent intent = getIntent(context); 145 return PendingIntent.getActivity(context, 0 /* requestCode */, intent, 146 PendingIntent.FLAG_IMMUTABLE); 147 } 148 getBroadcastIntent(Context context)149 private static PendingIntent getBroadcastIntent(Context context) { 150 final Intent intent = new Intent(ACTION_ZEN_MODE_SLICE_CHANGED) 151 .setClass(context, SliceBroadcastReceiver.class); 152 return PendingIntent.getBroadcast(context, 0 /* requestCode */, intent, 153 PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_MUTABLE); 154 } 155 } 156