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.CompoundButton; 22 import android.widget.CompoundButton.OnCheckedChangeListener; 23 24 import androidx.fragment.app.Fragment; 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceScreen; 27 28 import com.android.settings.R; 29 import com.android.settingslib.core.lifecycle.Lifecycle; 30 import com.android.settingslib.widget.MainSwitchPreference; 31 32 public class ZenAutomaticRuleSwitchPreferenceController extends 33 AbstractZenModeAutomaticRulePreferenceController implements 34 OnCheckedChangeListener { 35 36 private static final String KEY = "zen_automatic_rule_switch"; 37 private AutomaticZenRule mRule; 38 private String mId; 39 private MainSwitchPreference 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 setIdAndRule(String id, AutomaticZenRule rule)51 void setIdAndRule(String id, AutomaticZenRule rule) { 52 mId = id; 53 mRule = rule; 54 } 55 56 @Override isAvailable()57 public boolean isAvailable() { 58 return mRule != null && mId != null; 59 } 60 61 @Override displayPreference(PreferenceScreen screen)62 public void displayPreference(PreferenceScreen screen) { 63 super.displayPreference(screen); 64 final Preference pref = screen.findPreference(KEY); 65 mSwitchBar = (MainSwitchPreference) pref; 66 67 if (mSwitchBar != null) { 68 mSwitchBar.setTitle(mContext.getString(R.string.zen_mode_use_automatic_rule)); 69 try { 70 pref.setOnPreferenceClickListener(preference -> { 71 mRule.setEnabled(!mRule.isEnabled()); 72 mBackend.updateZenRule(mId, mRule); 73 return true; 74 }); 75 mSwitchBar.addOnSwitchChangeListener(this); 76 } catch (IllegalStateException e) { 77 // an exception is thrown if you try to add the listener twice 78 } 79 } 80 } 81 updateState(Preference preference)82 public void updateState(Preference preference) { 83 if (mRule != null) { 84 mSwitchBar.setChecked(mRule.isEnabled()); 85 } 86 } 87 88 @Override onCheckedChanged(CompoundButton buttonView, boolean isChecked)89 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 90 final boolean enabled = isChecked; 91 if (enabled == mRule.isEnabled()) return; 92 mRule.setEnabled(enabled); 93 mBackend.updateZenRule(mId, mRule); 94 } 95 } 96