• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.widget;
18 
19 import android.content.Context;
20 import android.support.v7.preference.PreferenceViewHolder;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.view.View.OnClickListener;
24 import android.widget.Switch;
25 
26 import com.android.settings.R;
27 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
28 import com.android.settingslib.TwoTargetPreference;
29 
30 /**
31  * A custom preference that provides inline switch toggle. It has a mandatory field for title, and
32  * optional fields for icon and sub-text.
33  */
34 public class MasterSwitchPreference extends TwoTargetPreference {
35 
36     private Switch mSwitch;
37     private boolean mChecked;
38     private boolean mEnableSwitch = true;
39 
MasterSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)40     public MasterSwitchPreference(Context context, AttributeSet attrs,
41             int defStyleAttr, int defStyleRes) {
42         super(context, attrs, defStyleAttr, defStyleRes);
43     }
44 
MasterSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr)45     public MasterSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
46         super(context, attrs, defStyleAttr);
47     }
48 
MasterSwitchPreference(Context context, AttributeSet attrs)49     public MasterSwitchPreference(Context context, AttributeSet attrs) {
50         super(context, attrs);
51     }
52 
MasterSwitchPreference(Context context)53     public MasterSwitchPreference(Context context) {
54         super(context);
55     }
56 
57     @Override
getSecondTargetResId()58     protected int getSecondTargetResId() {
59         return R.layout.preference_widget_master_switch;
60     }
61 
62     @Override
onBindViewHolder(PreferenceViewHolder holder)63     public void onBindViewHolder(PreferenceViewHolder holder) {
64         super.onBindViewHolder(holder);
65         final View widgetView = holder.findViewById(android.R.id.widget_frame);
66         if (widgetView != null) {
67             widgetView.setOnClickListener(new OnClickListener() {
68                 @Override
69                 public void onClick(View v) {
70                     if (mSwitch != null && !mSwitch.isEnabled()) {
71                         return;
72                     }
73                     setChecked(!mChecked);
74                     if (!callChangeListener(mChecked)) {
75                         setChecked(!mChecked);
76                     } else {
77                         persistBoolean(mChecked);
78                     }
79                 }
80             });
81         }
82 
83         mSwitch = (Switch) holder.findViewById(R.id.switchWidget);
84         if (mSwitch != null) {
85             mSwitch.setContentDescription(getTitle());
86             mSwitch.setChecked(mChecked);
87             mSwitch.setEnabled(mEnableSwitch);
88         }
89     }
90 
isChecked()91     public boolean isChecked() {
92         return mSwitch != null && mChecked;
93     }
94 
setChecked(boolean checked)95     public void setChecked(boolean checked) {
96         mChecked = checked;
97         if (mSwitch != null) {
98             mSwitch.setChecked(checked);
99         }
100     }
101 
setSwitchEnabled(boolean enabled)102     public void setSwitchEnabled(boolean enabled) {
103         mEnableSwitch = enabled;
104         if (mSwitch != null) {
105             mSwitch.setEnabled(enabled);
106         }
107     }
108 
109     /**
110      * If admin is not null, disables the switch.
111      * Otherwise, keep it enabled.
112      */
setDisabledByAdmin(EnforcedAdmin admin)113     public void setDisabledByAdmin(EnforcedAdmin admin) {
114         setSwitchEnabled(admin == null);
115     }
116 
getSwitch()117     public Switch getSwitch() {
118         return mSwitch;
119     }
120 }
121