2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
19 import android.app.AutomaticZenRule;
20 import android.app.Fragment;
21 import android.content.Context;
22 import android.support.v7.preference.Preference;
23 import android.support.v7.preference.PreferenceScreen;
24 import android.widget.Switch;
25 import android.widget.Toast;
27 import com.android.settings.R;
28 import com.android.settings.applications.LayoutPreference;
29 import com.android.settings.widget.SwitchBar;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
32 public class ZenAutomaticRuleSwitchPreferenceController extends
33 AbstractZenModeAutomaticRulePreferenceController implements
34 SwitchBar.OnSwitchChangeListener {
36 private static final String KEY = "zen_automatic_rule_switch";
37 private AutomaticZenRule mRule;
39 private Toast mEnabledToast;
40 private int mToastTextResource;
41 private SwitchBar mSwitchBar;
43 public ZenAutomaticRuleSwitchPreferenceController(Context context, Fragment parent,
44 int toastTextResource, Lifecycle lifecycle) {
45 super(context, KEY, parent, lifecycle);
46 mToastTextResource = toastTextResource;
50 public String getPreferenceKey() {
55 public boolean isAvailable() {
56 return mRule != null && mId != null;
60 public void displayPreference(PreferenceScreen screen) {
61 super.displayPreference(screen);
62 LayoutPreference pref = (LayoutPreference) screen.findPreference(KEY);
63 mSwitchBar = pref.findViewById(R.id.switch_bar);
65 if (mSwitchBar != null) {
66 mSwitchBar.setSwitchBarText(R.string.zen_mode_use_automatic_rule,
67 R.string.zen_mode_use_automatic_rule);
69 mSwitchBar.addOnSwitchChangeListener(this);
70 } catch (IllegalStateException e) {
71 // an exception is thrown if you try to add the listener twice
78 public void onResume(AutomaticZenRule rule, String id) {
83 public void updateState(Preference preference) {
85 mSwitchBar.setChecked(mRule.isEnabled());
90 public void onSwitchChanged(Switch switchView, boolean isChecked) {
91 final boolean enabled = isChecked;
92 if (enabled == mRule.isEnabled()) return;
93 mRule.setEnabled(enabled);
94 mBackend.setZenRule(mId, mRule);
96 final int toastText = mToastTextResource;
98 mEnabledToast = Toast.makeText(mContext, toastText, Toast.LENGTH_SHORT);
102 if (mEnabledToast != null) {
103 mEnabledToast.cancel();