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