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.support.v14.preference.SwitchPreference;
     25 import android.support.v4.content.res.TypedArrayUtils;
     26 import android.support.v7.preference.PreferenceManager;
     27 import android.support.v7.preference.PreferenceViewHolder;
     28 import android.util.AttributeSet;
     29 import android.util.TypedValue;
     30 import android.view.View;
     31 import android.widget.TextView;
     32 
     33 /**
     34  * Version of SwitchPreference that can be disabled by a device admin
     35  * using a user restriction.
     36  */
     37 public class RestrictedSwitchPreference extends SwitchPreference {
     38     RestrictedPreferenceHelper mHelper;
     39     boolean mUseAdditionalSummary = false;
     40     CharSequence mRestrictedSwitchSummary;
     41 
     42     public RestrictedSwitchPreference(Context context, AttributeSet attrs,
     43             int defStyleAttr, int defStyleRes) {
     44         super(context, attrs, defStyleAttr, defStyleRes);
     45         setWidgetLayoutResource(R.layout.restricted_switch_widget);
     46         mHelper = new RestrictedPreferenceHelper(context, this, attrs);
     47         if (attrs != null) {
     48             final TypedArray attributes = context.obtainStyledAttributes(attrs,
     49                     R.styleable.RestrictedSwitchPreference);
     50             final TypedValue useAdditionalSummary = attributes.peekValue(
     51                     R.styleable.RestrictedSwitchPreference_useAdditionalSummary);
     52             if (useAdditionalSummary != null) {
     53                 mUseAdditionalSummary =
     54                         (useAdditionalSummary.type == TypedValue.TYPE_INT_BOOLEAN
     55                                 && useAdditionalSummary.data != 0);
     56             }
     57 
     58             final TypedValue restrictedSwitchSummary = attributes.peekValue(
     59                     R.styleable.RestrictedSwitchPreference_restrictedSwitchSummary);
     60             if (restrictedSwitchSummary != null
     61                     && restrictedSwitchSummary.type == TypedValue.TYPE_STRING) {
     62                 if (restrictedSwitchSummary.resourceId != 0) {
     63                     mRestrictedSwitchSummary =
     64                         context.getText(restrictedSwitchSummary.resourceId);
     65                 } else {
     66                     mRestrictedSwitchSummary = restrictedSwitchSummary.string;
     67                 }
     68             }
     69         }
     70         if (mUseAdditionalSummary) {
     71             setLayoutResource(R.layout.restricted_switch_preference);
     72             useAdminDisabledSummary(false);
     73         }
     74     }
     75 
     76     public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
     77         this(context, attrs, defStyleAttr, 0);
     78     }
     79 
     80     public RestrictedSwitchPreference(Context context, AttributeSet attrs) {
     81         this(context, attrs, TypedArrayUtils.getAttr(context, R.attr.switchPreferenceStyle,
     82                 android.R.attr.switchPreferenceStyle));
     83     }
     84 
     85     public RestrictedSwitchPreference(Context context) {
     86         this(context, null);
     87     }
     88 
     89     @Override
     90     public void onBindViewHolder(PreferenceViewHolder holder) {
     91         super.onBindViewHolder(holder);
     92         mHelper.onBindViewHolder(holder);
     93 
     94         CharSequence switchSummary;
     95         if (mRestrictedSwitchSummary == null) {
     96             switchSummary = getContext().getText(isChecked()
     97                 ? R.string.enabled_by_admin : R.string.disabled_by_admin);
     98         } else {
     99             switchSummary = mRestrictedSwitchSummary;
    100         }
    101 
    102         final View restrictedIcon = holder.findViewById(R.id.restricted_icon);
    103         final View switchWidget = holder.findViewById(android.R.id.switch_widget);
    104         if (restrictedIcon != null) {
    105             restrictedIcon.setVisibility(isDisabledByAdmin() ? View.VISIBLE : View.GONE);
    106         }
    107         if (switchWidget != null) {
    108             switchWidget.setVisibility(isDisabledByAdmin() ? View.GONE : View.VISIBLE);
    109         }
    110 
    111         if (mUseAdditionalSummary) {
    112             final TextView additionalSummaryView = (TextView) holder.findViewById(
    113                     R.id.additional_summary);
    114             if (additionalSummaryView != null) {
    115                 if (isDisabledByAdmin()) {
    116                     additionalSummaryView.setText(switchSummary);
    117                     additionalSummaryView.setVisibility(View.VISIBLE);
    118                 } else {
    119                     additionalSummaryView.setVisibility(View.GONE);
    120                 }
    121             }
    122         } else {
    123             final TextView summaryView = (TextView) holder.findViewById(android.R.id.summary);
    124             if (summaryView != null) {
    125                 if (isDisabledByAdmin()) {
    126                     summaryView.setText(switchSummary);
    127                     summaryView.setVisibility(View.VISIBLE);
    128                 }
    129                 // No need to change the visibility to GONE in the else case here since Preference
    130                 // class would have already changed it if there is no summary to display.
    131             }
    132         }
    133     }
    134 
    135     @Override
    136     public void performClick() {
    137         if (!mHelper.performClick()) {
    138             super.performClick();
    139         }
    140     }
    141 
    142     public void useAdminDisabledSummary(boolean useSummary) {
    143         mHelper.useAdminDisabledSummary(useSummary);
    144     }
    145 
    146     @Override
    147     protected void onAttachedToHierarchy(PreferenceManager preferenceManager) {
    148         mHelper.onAttachedToHierarchy();
    149         super.onAttachedToHierarchy(preferenceManager);
    150     }
    151 
    152     public void checkRestrictionAndSetDisabled(String userRestriction) {
    153         mHelper.checkRestrictionAndSetDisabled(userRestriction, UserHandle.myUserId());
    154     }
    155 
    156     public void checkRestrictionAndSetDisabled(String userRestriction, int userId) {
    157         mHelper.checkRestrictionAndSetDisabled(userRestriction, userId);
    158     }
    159 
    160     @Override
    161     public void setEnabled(boolean enabled) {
    162         if (enabled && isDisabledByAdmin()) {
    163             mHelper.setDisabledByAdmin(null);
    164             return;
    165         }
    166         super.setEnabled(enabled);
    167     }
    168 
    169     public void setDisabledByAdmin(EnforcedAdmin admin) {
    170         if (mHelper.setDisabledByAdmin(admin)) {
    171             notifyChanged();
    172         }
    173     }
    174 
    175     public boolean isDisabledByAdmin() {
    176         return mHelper.isDisabledByAdmin();
    177     }
    178 }
    179