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 package com.android.settings.notification.modes; 17 18 import static android.app.AutomaticZenRule.TYPE_SCHEDULE_CALENDAR; 19 import static android.app.AutomaticZenRule.TYPE_SCHEDULE_TIME; 20 21 import static com.android.settings.notification.modes.ZenModeFragmentBase.MODE_ID; 22 23 import android.content.Context; 24 import android.os.Bundle; 25 26 import androidx.annotation.NonNull; 27 import androidx.annotation.VisibleForTesting; 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceCategory; 30 31 import com.android.settings.R; 32 import com.android.settings.core.SubSettingLauncher; 33 import com.android.settingslib.PrimarySwitchPreference; 34 35 /** 36 * Preference controller for the link to an individual mode's configuration page. 37 */ 38 class ZenModeSetTriggerLinkPreferenceController extends AbstractZenModePreferenceController { 39 @VisibleForTesting 40 protected static final String AUTOMATIC_TRIGGER_PREF_KEY = "zen_automatic_trigger_settings"; 41 ZenModeSetTriggerLinkPreferenceController(Context context, String key, ZenModesBackend backend)42 ZenModeSetTriggerLinkPreferenceController(Context context, String key, 43 ZenModesBackend backend) { 44 super(context, key, backend); 45 } 46 47 @Override isAvailable(@onNull ZenMode zenMode)48 public boolean isAvailable(@NonNull ZenMode zenMode) { 49 return !zenMode.isManualDnd(); 50 } 51 52 @Override updateState(Preference preference, @NonNull ZenMode zenMode)53 public void updateState(Preference preference, @NonNull ZenMode zenMode) { 54 // This controller is expected to govern a preference category so that it controls the 55 // availability of the entire preference category if the mode doesn't have a way to 56 // automatically trigger (such as manual DND). 57 Preference switchPref = ((PreferenceCategory) preference).findPreference( 58 AUTOMATIC_TRIGGER_PREF_KEY); 59 if (switchPref == null) { 60 return; 61 } 62 ((PrimarySwitchPreference) switchPref).setChecked(zenMode.getRule().isEnabled()); 63 switchPref.setOnPreferenceChangeListener(mSwitchChangeListener); 64 65 Bundle bundle = new Bundle(); 66 bundle.putString(MODE_ID, zenMode.getId()); 67 68 // TODO: b/341961712 - direct preference to app-owned intent if available 69 switch (zenMode.getRule().getType()) { 70 case TYPE_SCHEDULE_TIME: 71 switchPref.setTitle(R.string.zen_mode_set_schedule_link); 72 switchPref.setSummary(zenMode.getRule().getTriggerDescription()); 73 switchPref.setIntent(new SubSettingLauncher(mContext) 74 .setDestination(ZenModeSetScheduleFragment.class.getName()) 75 // TODO: b/332937635 - set correct metrics category 76 .setSourceMetricsCategory(0) 77 .setArguments(bundle) 78 .toIntent()); 79 break; 80 case TYPE_SCHEDULE_CALENDAR: 81 switchPref.setTitle(R.string.zen_mode_set_calendar_link); 82 switchPref.setSummary(zenMode.getRule().getTriggerDescription()); 83 switchPref.setIntent(new SubSettingLauncher(mContext) 84 .setDestination(ZenModeSetCalendarFragment.class.getName()) 85 // TODO: b/332937635 - set correct metrics category 86 .setSourceMetricsCategory(0) 87 .setArguments(bundle) 88 .toIntent()); 89 break; 90 default: 91 // TODO: b/342156843 - change this to allow adding a trigger condition for system 92 // rules that don't yet have a type selected 93 switchPref.setTitle("not implemented"); 94 } 95 } 96 97 @VisibleForTesting 98 protected Preference.OnPreferenceChangeListener mSwitchChangeListener = (p, newValue) -> { 99 final boolean newEnabled = (Boolean) newValue; 100 return saveMode((zenMode) -> { 101 if (newEnabled != zenMode.getRule().isEnabled()) { 102 zenMode.getRule().setEnabled(newEnabled); 103 } 104 return zenMode; 105 }); 106 }; 107 } 108