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.content.res.TypedArray;
     23 import android.os.UserHandle;
     24 import android.text.TextUtils;
     25 import android.util.AttributeSet;
     26 import android.util.TypedValue;
     27 import android.widget.TextView;
     28 
     29 import androidx.preference.Preference;
     30 import androidx.preference.PreferenceViewHolder;
     31 
     32 /**
     33  * Helper class for managing settings preferences that can be disabled
     34  * by device admins via user restrictions.
     35  */
     36 public class RestrictedPreferenceHelper {
     37     private final Context mContext;
     38     private final Preference mPreference;
     39 
     40     private boolean mDisabledByAdmin;
     41     private EnforcedAdmin mEnforcedAdmin;
     42     private String mAttrUserRestriction = null;
     43     private boolean mUseAdminDisabledSummary = false;
     44 
     45     public RestrictedPreferenceHelper(Context context, Preference preference,
     46             AttributeSet attrs) {
     47         mContext = context;
     48         mPreference = preference;
     49 
     50         if (attrs != null) {
     51             final TypedArray attributes = context.obtainStyledAttributes(attrs,
     52                     R.styleable.RestrictedPreference);
     53             final TypedValue userRestriction =
     54                     attributes.peekValue(R.styleable.RestrictedPreference_userRestriction);
     55             CharSequence data = null;
     56             if (userRestriction != null && userRestriction.type == TypedValue.TYPE_STRING) {
     57                 if (userRestriction.resourceId != 0) {
     58                     data = context.getText(userRestriction.resourceId);
     59                 } else {
     60                     data = userRestriction.string;
     61                 }
     62             }
     63             mAttrUserRestriction = data == null ? null : data.toString();
     64             // If the system has set the user restriction, then we shouldn't add the padlock.
     65             if (RestrictedLockUtilsInternal.hasBaseUserRestriction(mContext, mAttrUserRestriction,
     66                     UserHandle.myUserId())) {
     67                 mAttrUserRestriction = null;
     68                 return;
     69             }
     70 
     71             final TypedValue useAdminDisabledSummary =
     72                     attributes.peekValue(R.styleable.RestrictedPreference_useAdminDisabledSummary);
     73             if (useAdminDisabledSummary != null) {
     74                 mUseAdminDisabledSummary =
     75                         (useAdminDisabledSummary.type == TypedValue.TYPE_INT_BOOLEAN
     76                                 && useAdminDisabledSummary.data != 0);
     77             }
     78         }
     79     }
     80 
     81     /**
     82      * Modify PreferenceViewHolder to add padlock if restriction is disabled.
     83      */
     84     public void onBindViewHolder(PreferenceViewHolder holder) {
     85         if (mDisabledByAdmin) {
     86             holder.itemView.setEnabled(true);
     87         }
     88         if (mUseAdminDisabledSummary) {
     89             final TextView summaryView = (TextView) holder.findViewById(android.R.id.summary);
     90             if (summaryView != null) {
     91                 final CharSequence disabledText = summaryView.getContext().getText(
     92                         R.string.disabled_by_admin_summary_text);
     93                 if (mDisabledByAdmin) {
     94                     summaryView.setText(disabledText);
     95                 } else if (TextUtils.equals(disabledText, summaryView.getText())) {
     96                     // It's previously set to disabled text, clear it.
     97                     summaryView.setText(null);
     98                 }
     99             }
    100         }
    101     }
    102 
    103     public void useAdminDisabledSummary(boolean useSummary) {
    104         mUseAdminDisabledSummary = useSummary;
    105     }
    106 
    107     /**
    108      * Check if the preference is disabled if so handle the click by informing the user.
    109      *
    110      * @return true if the method handled the click.
    111      */
    112     public boolean performClick() {
    113         if (mDisabledByAdmin) {
    114             RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, mEnforcedAdmin);
    115             return true;
    116         }
    117         return false;
    118     }
    119 
    120     /**
    121      * Disable / enable if we have been passed the restriction in the xml.
    122      */
    123     public void onAttachedToHierarchy() {
    124         if (mAttrUserRestriction != null) {
    125             checkRestrictionAndSetDisabled(mAttrUserRestriction, UserHandle.myUserId());
    126         }
    127     }
    128 
    129     /**
    130      * Set the user restriction that is used to disable this preference.
    131      *
    132      * @param userRestriction constant from {@link android.os.UserManager}
    133      * @param userId user to check the restriction for.
    134      */
    135     public void checkRestrictionAndSetDisabled(String userRestriction, int userId) {
    136         EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(mContext,
    137                 userRestriction, userId);
    138         setDisabledByAdmin(admin);
    139     }
    140 
    141     /**
    142      * Disable this preference based on the enforce admin.
    143      *
    144      * @param EnforcedAdmin Details of the admin who enforced the restriction. If it
    145      * is {@code null}, then this preference will be enabled. Otherwise, it will be disabled.
    146      * @return true if the disabled state was changed.
    147      */
    148     public boolean setDisabledByAdmin(EnforcedAdmin admin) {
    149         final boolean disabled = (admin != null ? true : false);
    150         mEnforcedAdmin = admin;
    151         boolean changed = false;
    152         if (mDisabledByAdmin != disabled) {
    153             mDisabledByAdmin = disabled;
    154             changed = true;
    155         }
    156         mPreference.setEnabled(!disabled);
    157         return changed;
    158     }
    159 
    160     public boolean isDisabledByAdmin() {
    161         return mDisabledByAdmin;
    162     }
    163 }
    164