OSDN Git Service

Zen automatic rule page ui changes
[android-x86/packages-apps-Settings.git] / src / com / android / settings / notification / ZenAutomaticRuleSwitchPreferenceController.java
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;
18
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;
26
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;
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 Toast mEnabledToast;
40     private int mToastTextResource;
41     private SwitchBar mSwitchBar;
42
43     public ZenAutomaticRuleSwitchPreferenceController(Context context, Fragment parent,
44             int toastTextResource, Lifecycle lifecycle) {
45         super(context, KEY, parent, lifecycle);
46         mToastTextResource = toastTextResource;
47     }
48
49     @Override
50     public String getPreferenceKey() {
51         return KEY;
52     }
53
54     @Override
55     public boolean isAvailable() {
56         return mRule != null && mId != null;
57     }
58
59     @Override
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);
64
65         if (mSwitchBar != null) {
66             mSwitchBar.setSwitchBarText(R.string.zen_mode_use_automatic_rule,
67                     R.string.zen_mode_use_automatic_rule);
68             try {
69                 mSwitchBar.addOnSwitchChangeListener(this);
70             } catch (IllegalStateException e) {
71                 // an exception is thrown if you try to add the listener twice
72             }
73             mSwitchBar.show();
74         }
75     }
76
77
78     public void onResume(AutomaticZenRule rule, String id) {
79         mRule = rule;
80         mId = id;
81     }
82
83     public void updateState(Preference preference) {
84         if (mRule != null) {
85             mSwitchBar.setChecked(mRule.isEnabled());
86         }
87     }
88
89     @Override
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);
95         if (enabled) {
96             final int toastText = mToastTextResource;
97             if (toastText != 0) {
98                 mEnabledToast = Toast.makeText(mContext, toastText, Toast.LENGTH_SHORT);
99                 mEnabledToast.show();
100             }
101         } else {
102             if (mEnabledToast != null) {
103                 mEnabledToast.cancel();
104             }
105         }
106     }
107 }