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