1 /*
2  * Copyright (C) 2016 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;
18 
19 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
20 
21 import android.content.Context;
22 import android.util.AttributeSet;
23 import android.widget.RadioButton;
24 import android.widget.TextView;
25 
26 import com.android.settingslib.RestrictedLockUtils;
27 import com.android.settingslib.RestrictedLockUtilsInternal;
28 import com.android.settingslib.utils.ColorUtil;
29 
30 public class RestrictedRadioButton extends RadioButton {
31     private Context mContext;
32     private boolean mDisabledByAdmin;
33     private EnforcedAdmin mEnforcedAdmin;
34 
RestrictedRadioButton(Context context)35     public RestrictedRadioButton(Context context) {
36         this(context, null);
37     }
38 
RestrictedRadioButton(Context context, AttributeSet attrs)39     public RestrictedRadioButton(Context context, AttributeSet attrs) {
40         this(context, attrs, com.android.internal.R.attr.radioButtonStyle);
41     }
42 
RestrictedRadioButton(Context context, AttributeSet attrs, int defStyleAttr)43     public RestrictedRadioButton(Context context, AttributeSet attrs, int defStyleAttr) {
44         this(context, attrs, defStyleAttr, 0);
45     }
46 
RestrictedRadioButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)47     public RestrictedRadioButton(Context context, AttributeSet attrs, int defStyleAttr,
48             int defStyleRes) {
49         super(context, attrs, defStyleAttr, defStyleRes);
50         mContext = context;
51     }
52 
53     @Override
performClick()54     public boolean performClick() {
55         if (mDisabledByAdmin) {
56             RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, mEnforcedAdmin);
57             return true;
58         }
59         return super.performClick();
60     }
61 
setDisabledByAdmin(EnforcedAdmin admin)62     public void setDisabledByAdmin(EnforcedAdmin admin) {
63         final boolean disabled = (admin != null);
64         mEnforcedAdmin = admin;
65         if (mDisabledByAdmin != disabled) {
66             mDisabledByAdmin = disabled;
67             RestrictedLockUtilsInternal.setTextViewAsDisabledByAdmin(mContext,
68                     (TextView) this, mDisabledByAdmin);
69             if (mDisabledByAdmin) {
70                 getButtonDrawable().setAlpha(
71                         (int) (255 * ColorUtil.getDisabledAlpha(mContext)));
72             } else {
73                 getButtonDrawable().setAlpha(0);
74             }
75         }
76     }
77 
isDisabledByAdmin()78     public boolean isDisabledByAdmin() {
79         return mDisabledByAdmin;
80     }
81 }