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
17package com.android.settings.notification;
18
19import android.content.Context;
20import android.support.v7.preference.PreferenceViewHolder;
21import android.util.AttributeSet;
22import android.view.View;
23
24import com.android.settings.applications.LayoutPreference;
25import com.android.settings.widget.ToggleSwitch;
26import com.android.settingslib.RestrictedLockUtils;
27
28public class NotificationSwitchBarPreference extends LayoutPreference {
29    private ToggleSwitch mSwitch;
30    private boolean mChecked;
31    private boolean mEnableSwitch = true;
32
33    public NotificationSwitchBarPreference(Context context, AttributeSet attrs) {
34        super(context, attrs);
35    }
36
37    @Override
38    public void onBindViewHolder(PreferenceViewHolder holder) {
39        super.onBindViewHolder(holder);
40        mSwitch = (ToggleSwitch) holder.findViewById(android.R.id.switch_widget);
41        if (mSwitch != null) {
42            mSwitch.setOnClickListener(new View.OnClickListener() {
43                @Override
44                public void onClick(View v) {
45                    if (!mSwitch.isEnabled()) {
46                        return;
47                    }
48                    mChecked = !mChecked;
49                    setChecked(mChecked);
50                    if (!callChangeListener(mChecked)) {
51                        setChecked(!mChecked);
52                    }
53                }
54            });
55            mSwitch.setChecked(mChecked);
56            mSwitch.setEnabled(mEnableSwitch);
57        }
58    }
59
60    public boolean isChecked() {
61        return mSwitch != null && mSwitch.isEnabled() && mChecked;
62    }
63
64    public void setChecked(boolean checked) {
65        mChecked = checked;
66        if (mSwitch != null) {
67            mSwitch.setChecked(checked);
68        }
69    }
70
71    public void setSwitchEnabled(boolean enabled) {
72        mEnableSwitch = enabled;
73        if (mSwitch != null) {
74            mSwitch.setEnabled(enabled);
75        }
76    }
77
78    public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
79        setSwitchEnabled(admin == null);
80    }
81}
82