Home | History | Annotate | Download | only in handheld
      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.packageinstaller.permission.ui.handheld;
     18 
     19 import android.content.Context;
     20 import android.preference.PreferenceScreen;
     21 import android.view.View;
     22 import android.widget.TextView;
     23 
     24 import com.android.packageinstaller.R;
     25 import com.android.settingslib.RestrictedLockUtils;
     26 
     27 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
     28 
     29 public class RestrictedSwitchPreference extends MultiTargetSwitchPreference {
     30     private final Context mContext;
     31     private boolean mDisabledByAdmin;
     32     private EnforcedAdmin mEnforcedAdmin;
     33     private final int mSwitchWidgetResId;
     34 
     35     public RestrictedSwitchPreference(Context context) {
     36         super(context);
     37         mSwitchWidgetResId = getWidgetLayoutResource();
     38         mContext = context;
     39     }
     40 
     41     @Override
     42     public void onBindView(View view) {
     43         super.onBindView(view);
     44         if (mDisabledByAdmin) {
     45             view.setEnabled(true);
     46         }
     47         if (mDisabledByAdmin) {
     48             final TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
     49             if (summaryView != null) {
     50                 summaryView.setText(
     51                         isChecked() ? R.string.enabled_by_admin : R.string.disabled_by_admin);
     52                 summaryView.setVisibility(View.VISIBLE);
     53             }
     54         }
     55     }
     56 
     57     @Override
     58     public void setEnabled(boolean enabled) {
     59         if (enabled && mDisabledByAdmin) {
     60             setDisabledByAdmin(null);
     61         } else {
     62             super.setEnabled(enabled);
     63         }
     64     }
     65 
     66     public void setDisabledByAdmin(EnforcedAdmin admin) {
     67         final boolean disabled = (admin != null ? true : false);
     68         mEnforcedAdmin = admin;
     69         if (mDisabledByAdmin != disabled) {
     70             mDisabledByAdmin = disabled;
     71             setWidgetLayoutResource(disabled ? R.layout.restricted_icon : mSwitchWidgetResId);
     72             setEnabled(!disabled);
     73         }
     74     }
     75 
     76     @Override
     77     public void performClick(PreferenceScreen preferenceScreen) {
     78         if (mDisabledByAdmin) {
     79             RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, mEnforcedAdmin);
     80         } else {
     81             super.performClick(preferenceScreen);
     82         }
     83     }
     84 }