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