Home | History | Annotate | Download | only in settingslib
      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.settingslib;
     18 
     19 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
     20 
     21 import android.content.Context;
     22 import android.os.UserHandle;
     23 import android.util.AttributeSet;
     24 import android.view.View;
     25 
     26 import androidx.core.content.res.TypedArrayUtils;
     27 import androidx.preference.PreferenceManager;
     28 import androidx.preference.PreferenceViewHolder;
     29 
     30 /**
     31  * Preference class that supports being disabled by a user restriction
     32  * set by a device admin.
     33  */
     34 public class RestrictedPreference extends TwoTargetPreference {
     35     RestrictedPreferenceHelper mHelper;
     36 
     37     public RestrictedPreference(Context context, AttributeSet attrs,
     38             int defStyleAttr, int defStyleRes) {
     39         super(context, attrs, defStyleAttr, defStyleRes);
     40         mHelper = new RestrictedPreferenceHelper(context, this, attrs);
     41     }
     42 
     43     public RestrictedPreference(Context context, AttributeSet attrs, int defStyleAttr) {
     44         this(context, attrs, defStyleAttr, 0);
     45     }
     46 
     47     public RestrictedPreference(Context context, AttributeSet attrs) {
     48         this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.preferenceStyle,
     49                 android.R.attr.preferenceStyle));
     50     }
     51 
     52     public RestrictedPreference(Context context) {
     53         this(context, null);
     54     }
     55 
     56     @Override
     57     protected int getSecondTargetResId() {
     58         return R.layout.restricted_icon;
     59     }
     60 
     61     @Override
     62     protected boolean shouldHideSecondTarget() {
     63         return !isDisabledByAdmin();
     64     }
     65 
     66     @Override
     67     public void onBindViewHolder(PreferenceViewHolder holder) {
     68         super.onBindViewHolder(holder);
     69         mHelper.onBindViewHolder(holder);
     70         final View restrictedIcon = holder.findViewById(R.id.restricted_icon);
     71         if (restrictedIcon != null) {
     72             restrictedIcon.setVisibility(isDisabledByAdmin() ? View.VISIBLE : View.GONE);
     73         }
     74     }
     75 
     76     @Override
     77     public void performClick() {
     78         if (!mHelper.performClick()) {
     79             super.performClick();
     80         }
     81     }
     82 
     83     public void useAdminDisabledSummary(boolean useSummary) {
     84         mHelper.useAdminDisabledSummary(useSummary);
     85     }
     86 
     87     @Override
     88     protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
     89         mHelper.onAttachedToHierarchy();
     90         super.onAttachedToHierarchy(preferenceManager);
     91     }
     92 
     93     public void checkRestrictionAndSetDisabled(String userRestriction) {
     94         mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId());
     95     }
     96 
     97     public void checkRestrictionAndSetDisabled(String userRestriction, int userId) {
     98         mHelper.checkRestrictionAndSetDisabled(userRestriction, userId);
     99     }
    100 
    101     @Override
    102     public void setEnabled(boolean enabled) {
    103         if (enabled && isDisabledByAdmin()) {
    104             mHelper.setDisabledByAdmin(null);
    105             return;
    106         }
    107         super.setEnabled(enabled);
    108     }
    109 
    110     public void setDisabledByAdmin(EnforcedAdmin admin) {
    111         if (mHelper.setDisabledByAdmin(admin)) {
    112             notifyChanged();
    113         }
    114     }
    115 
    116     public boolean isDisabledByAdmin() {
    117         return mHelper.isDisabledByAdmin();
    118     }
    119 }
    120