1/*
2 * Copyright (C) 2014 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.settings;
18
19import android.annotation.Nullable;
20import android.content.Context;
21import android.graphics.drawable.Drawable;
22import android.support.v7.preference.Preference;
23import android.support.v7.preference.PreferenceViewHolder;
24import android.text.TextUtils;
25import android.util.AttributeSet;
26import android.widget.TextView;
27
28import com.android.settingslib.RestrictedPreference;
29
30/**
31 * A preference item that can dim the icon when it's disabled, either directly or because its parent
32 * is disabled.
33 */
34public class DimmableIconPreference extends RestrictedPreference {
35    private static final int ICON_ALPHA_ENABLED = 255;
36    private static final int ICON_ALPHA_DISABLED = 102;
37
38    private final CharSequence mContentDescription;
39
40    public DimmableIconPreference(Context context) {
41        this(context, (AttributeSet) null);
42    }
43
44    public DimmableIconPreference(Context context, AttributeSet attrs) {
45        super(context, attrs);
46        mContentDescription = null;
47        useAdminDisabledSummary(true);
48    }
49
50    public DimmableIconPreference(Context context, @Nullable CharSequence contentDescription) {
51        super(context);
52        mContentDescription = contentDescription;
53        useAdminDisabledSummary(true);
54    }
55
56    private void dimIcon(boolean dimmed) {
57        Drawable icon = getIcon();
58        if (icon != null) {
59            icon.mutate().setAlpha(dimmed ? ICON_ALPHA_DISABLED : ICON_ALPHA_ENABLED);
60            setIcon(icon);
61        }
62    }
63
64    @Override
65    public void onBindViewHolder(PreferenceViewHolder view) {
66        super.onBindViewHolder(view);
67        if (!TextUtils.isEmpty(mContentDescription)) {
68            final TextView titleView = (TextView) view.findViewById(android.R.id.title);
69            titleView.setContentDescription(mContentDescription);
70        }
71        dimIcon(!isEnabled());
72    }
73}
74