1 /*
2  * Copyright (C) 2023 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.os.UserHandle;
21 import android.util.AttributeSet;
22 import android.widget.Button;
23 
24 import com.android.settingslib.RestrictedLockUtils;
25 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
26 import com.android.settingslib.RestrictedLockUtilsInternal;
27 
28 /**
29  * A preference with a plus button on the side representing an "add" action. The plus button will
30  * only be visible when a non-null click listener is registered.
31  */
32 public class RestrictedButton extends Button {
33 
34     private UserHandle mUserHandle;
35     private String mUserRestriction;
36 
RestrictedButton(Context context)37     public RestrictedButton(Context context) {
38         super(context);
39     }
40 
RestrictedButton(Context context, AttributeSet attrs)41     public RestrictedButton(Context context, AttributeSet attrs) {
42         super(context, attrs);
43     }
44 
RestrictedButton(Context context, AttributeSet attrs, int defStyleAttr)45     public RestrictedButton(Context context, AttributeSet attrs, int defStyleAttr) {
46         super(context, attrs, defStyleAttr);
47     }
48 
RestrictedButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)49     public RestrictedButton(Context context, AttributeSet attrs, int defStyleAttr,
50             int defStyleRes) {
51         super(context, attrs, defStyleAttr, defStyleRes);
52     }
53 
54     @Override
performClick()55     public boolean performClick() {
56         EnforcedAdmin admin = getEnforcedAdmin();
57         if (admin != null) {
58             RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, admin);
59             return false;
60         }
61         return super.performClick();
62     }
63 
64     /** Initialize the button with {@link UserHandle} and a restriction */
init(UserHandle userHandle, String restriction)65     public void init(UserHandle userHandle, String restriction) {
66         setAllowClickWhenDisabled(true);
67         mUserHandle = userHandle;
68         mUserRestriction = restriction;
69     }
70 
71     /** Update the restriction state */
updateState()72     public void updateState() {
73         setEnabled(getEnforcedAdmin() == null);
74     }
75 
getEnforcedAdmin()76     private EnforcedAdmin getEnforcedAdmin() {
77         if (mUserHandle != null) {
78             EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
79                     mContext, mUserRestriction, mUserHandle.getIdentifier());
80             if (admin != null) {
81                 return admin;
82             }
83         }
84         return null;
85     }
86 }
87