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