1 /* 2 * Copyright (C) 2017 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 android.app.AutomaticZenRule; 20 import android.content.Context; 21 import android.widget.Switch; 22 23 import androidx.fragment.app.Fragment; 24 import androidx.preference.Preference; 25 import androidx.preference.PreferenceScreen; 26 27 import com.android.settings.R; 28 import com.android.settings.widget.SwitchBar; 29 import com.android.settingslib.core.lifecycle.Lifecycle; 30 import com.android.settingslib.widget.LayoutPreference; 31 32 public class ZenAutomaticRuleSwitchPreferenceController extends 33 AbstractZenModeAutomaticRulePreferenceController implements 34 SwitchBar.OnSwitchChangeListener { 35 36 private static final String KEY = "zen_automatic_rule_switch"; 37 private AutomaticZenRule mRule; 38 private String mId; 39 private SwitchBar mSwitchBar; 40 ZenAutomaticRuleSwitchPreferenceController(Context context, Fragment parent, Lifecycle lifecycle)41 public ZenAutomaticRuleSwitchPreferenceController(Context context, Fragment parent, 42 Lifecycle lifecycle) { 43 super(context, KEY, parent, lifecycle); 44 } 45 46 @Override getPreferenceKey()47 public String getPreferenceKey() { 48 return KEY; 49 } 50 51 @Override isAvailable()52 public boolean isAvailable() { 53 return mRule != null && mId != null; 54 } 55 56 @Override displayPreference(PreferenceScreen screen)57 public void displayPreference(PreferenceScreen screen) { 58 super.displayPreference(screen); 59 LayoutPreference pref = screen.findPreference(KEY); 60 mSwitchBar = pref.findViewById(R.id.switch_bar); 61 62 if (mSwitchBar != null) { 63 mSwitchBar.setSwitchBarText(R.string.zen_mode_use_automatic_rule, 64 R.string.zen_mode_use_automatic_rule); 65 try { 66 pref.setOnPreferenceClickListener(preference -> { 67 mRule.setEnabled(!mRule.isEnabled()); 68 mBackend.updateZenRule(mId, mRule); 69 return true; 70 }); 71 mSwitchBar.addOnSwitchChangeListener(this); 72 } catch (IllegalStateException e) { 73 // an exception is thrown if you try to add the listener twice 74 } 75 mSwitchBar.show(); 76 } 77 } 78 onResume(AutomaticZenRule rule, String id)79 public void onResume(AutomaticZenRule rule, String id) { 80 mRule = rule; 81 mId = id; 82 } 83 updateState(Preference preference)84 public void updateState(Preference preference) { 85 if (mRule != null) { 86 mSwitchBar.setChecked(mRule.isEnabled()); 87 } 88 } 89 90 @Override onSwitchChanged(Switch switchView, boolean isChecked)91 public void onSwitchChanged(Switch switchView, boolean isChecked) { 92 final boolean enabled = isChecked; 93 if (enabled == mRule.isEnabled()) return; 94 mRule.setEnabled(enabled); 95 mBackend.updateZenRule(mId, mRule); 96 } 97 } 98