blob: 6af75b7183ddae9733c851e0f6aa25ff780c0ec9 [file] [log] [blame]
Mahaver Chopra8ce12192015-12-23 16:13:32 +00001/*
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.phone;
18
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.drawable.Drawable;
22import android.os.UserManager;
23import android.preference.PreferenceScreen;
24import android.preference.SwitchPreference;
25import android.provider.Settings;
26import android.util.AttributeSet;
27import android.view.View;
28import android.widget.TextView;
29
30public class RestrictedSwitchPreference extends SwitchPreference {
31 private final Context mContext;
32 private final Drawable mRestrictedPadlock;
33 private final int mRestrictedPadlockPadding;
34 private boolean mDisabledByAdmin;
35
36 public RestrictedSwitchPreference(Context context, AttributeSet attrs,
37 int defStyleAttr, int defStyleRes) {
38 super(context, attrs, defStyleAttr, defStyleRes);
39 mContext = context;
40 mRestrictedPadlock = mContext
41 .getDrawable(R.drawable.ic_settings_lock_outline);
42 final int iconSize = mContext.getResources().getDimensionPixelSize(
43 R.dimen.restricted_lock_icon_size);
44 mRestrictedPadlock.setBounds(0, 0, iconSize, iconSize);
45 mRestrictedPadlockPadding = mContext.getResources()
46 .getDimensionPixelSize(R.dimen.restricted_lock_icon_padding);
47 }
48
49 public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
50 this(context, attrs, defStyleAttr, 0);
51 }
52
53 public RestrictedSwitchPreference(Context context, AttributeSet attrs) {
54 this(context, attrs, com.android.internal.R.attr.switchPreferenceStyle);
55 }
56
57 public RestrictedSwitchPreference(Context context) {
58 this(context, null);
59 }
60
61 @Override
62 public void onBindView(View view) {
63 super.onBindView(view);
64 final TextView titleView = (TextView) view.findViewById(com.android.internal.R.id.title);
65 if (titleView != null) {
66 if (mDisabledByAdmin) {
67 view.setEnabled(true);
68 titleView.setCompoundDrawablesRelative(null, null, mRestrictedPadlock, null);
69 titleView.setCompoundDrawablePadding(mRestrictedPadlockPadding);
70 } else {
71 titleView.setCompoundDrawablesRelative(null, null, null, null);
72 }
73 }
74 }
75
76 public void checkRestrictionAndSetDisabled(String userRestriction) {
77 final UserManager mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
78 if (mUm.hasUserRestriction(userRestriction)) {
79 mDisabledByAdmin = true;
80 setEnabled(false);
81 } else {
82 mDisabledByAdmin = false;
83 }
84 }
85
86 @Override
87 public void performClick(PreferenceScreen preferenceScreen) {
88 if (mDisabledByAdmin) {
89 Intent intent = new Intent(Settings.ACTION_SHOW_ADMIN_SUPPORT_DETAILS);
90 mContext.startActivity(intent);
91 } else {
92 super.performClick(preferenceScreen);
93 }
94 }
95}