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.settingslib.core.lifecycle.Lifecycle; 29 import com.android.settingslib.widget.MainSwitchPreference; 30 import com.android.settingslib.widget.OnMainSwitchChangeListener; 31 32 public class ZenAutomaticRuleSwitchPreferenceController extends 33 AbstractZenModeAutomaticRulePreferenceController implements 34 OnMainSwitchChangeListener { 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 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 final Preference pref = screen.findPreference(KEY); 60 mSwitchBar = (MainSwitchPreference) pref; 61 62 if (mSwitchBar != null) { 63 mSwitchBar.setTitle(mContext.getString(R.string.zen_mode_use_automatic_rule)); 64 try { 65 pref.setOnPreferenceClickListener(preference -> { 66 mRule.setEnabled(!mRule.isEnabled()); 67 mBackend.updateZenRule(mId, mRule); 68 return true; 69 }); 70 mSwitchBar.addOnSwitchChangeListener(this); 71 } catch (IllegalStateException e) { 72 // an exception is thrown if you try to add the listener twice 73 } 74 } 75 } 76 onResume(AutomaticZenRule rule, String id)77 public void onResume(AutomaticZenRule rule, String id) { 78 mRule = rule; 79 mId = id; 80 } 81 updateState(Preference preference)82 public void updateState(Preference preference) { 83 if (mRule != null) { 84 mSwitchBar.updateStatus(mRule.isEnabled()); 85 } 86 } 87 88 @Override onSwitchChanged(Switch switchView, boolean isChecked)89 public void onSwitchChanged(Switch switchView, 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