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.homepage.contextualcards.conditional; 18 19 import android.app.Flags; 20 import android.app.NotificationManager; 21 import android.app.settings.SettingsEnums; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.provider.Settings; 27 import android.service.notification.ZenModeConfig; 28 29 import androidx.annotation.VisibleForTesting; 30 31 import com.android.settings.R; 32 import com.android.settings.core.SubSettingLauncher; 33 import com.android.settings.homepage.contextualcards.ContextualCard; 34 import com.android.settings.notification.zen.ZenModeSettings; 35 36 import java.util.Objects; 37 38 39 public class DndConditionCardController implements ConditionalCardController { 40 static final int ID = Objects.hash("DndConditionCardController"); 41 42 @VisibleForTesting 43 static final IntentFilter DND_FILTER = 44 new IntentFilter(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL); 45 46 private static final String TAG = "DndCondition"; 47 private final Context mAppContext; 48 private final ConditionManager mConditionManager; 49 private final NotificationManager mNotificationManager; 50 private final Receiver mReceiver; 51 DndConditionCardController(Context appContext, ConditionManager conditionManager)52 public DndConditionCardController(Context appContext, ConditionManager conditionManager) { 53 mAppContext = appContext; 54 mConditionManager = conditionManager; 55 mNotificationManager = mAppContext.getSystemService(NotificationManager.class); 56 mReceiver = new Receiver(); 57 } 58 59 @Override getId()60 public long getId() { 61 return ID; 62 } 63 64 @Override isDisplayable()65 public boolean isDisplayable() { 66 return mNotificationManager.getZenMode() != Settings.Global.ZEN_MODE_OFF; 67 } 68 69 @Override startMonitoringStateChange()70 public void startMonitoringStateChange() { 71 mAppContext.registerReceiver(mReceiver, DND_FILTER); 72 } 73 74 @Override stopMonitoringStateChange()75 public void stopMonitoringStateChange() { 76 mAppContext.unregisterReceiver(mReceiver); 77 } 78 79 @Override onPrimaryClick(Context context)80 public void onPrimaryClick(Context context) { 81 new SubSettingLauncher(context) 82 .setDestination(ZenModeSettings.class.getName()) 83 .setSourceMetricsCategory(SettingsEnums.SETTINGS_HOMEPAGE) 84 .setTitleRes(R.string.zen_mode_settings_title) 85 .launch(); 86 } 87 88 @Override onActionClick()89 public void onActionClick() { 90 if (Flags.modesApi()) { 91 mNotificationManager.setZenMode(Settings.Global.ZEN_MODE_OFF, null, TAG, 92 /* fromUser= */ true); 93 } else { 94 mNotificationManager.setZenMode(Settings.Global.ZEN_MODE_OFF, null, TAG); 95 } 96 } 97 98 @Override buildContextualCard()99 public ContextualCard buildContextualCard() { 100 return new ConditionalContextualCard.Builder() 101 .setConditionId(ID) 102 .setMetricsConstant(SettingsEnums.SETTINGS_CONDITION_DND) 103 .setActionText(mAppContext.getText(R.string.condition_turn_off)) 104 .setName(mAppContext.getPackageName() + "/" 105 + mAppContext.getText(R.string.condition_zen_title)) 106 .setTitleText(mAppContext.getText(R.string.condition_zen_title).toString()) 107 .setSummaryText(getSummary()) 108 .setIconDrawable(mAppContext.getDrawable(R.drawable.ic_do_not_disturb_on_24dp)) 109 .setViewType(ConditionContextualCardRenderer.VIEW_TYPE_HALF_WIDTH) 110 .build(); 111 } 112 113 public class Receiver extends BroadcastReceiver { 114 @Override onReceive(Context context, Intent intent)115 public void onReceive(Context context, Intent intent) { 116 if (NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL 117 .equals(intent.getAction())) { 118 mConditionManager.onConditionChanged(); 119 } 120 } 121 } 122 getSummary()123 private String getSummary() { 124 if (ZenModeConfig.areAllZenBehaviorSoundsMuted(mNotificationManager.getZenModeConfig())) { 125 return mAppContext.getText(R.string.condition_zen_summary_phone_muted).toString(); 126 } 127 return mAppContext.getText(R.string.condition_zen_summary_with_exceptions).toString(); 128 } 129 } 130