blob: 8f09c53fe9c69dd2dbd4f27d1e1297bd0a34a821 [file] [log] [blame]
Lifu Tangdb0b7632014-08-18 11:08:43 -07001/*
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
Fyodor Kupolova7a7bfe2015-05-08 15:54:38 -070017package com.android.settings;
Lifu Tangdb0b7632014-08-18 11:08:43 -070018
Zoltan Szatmary-Ban86c877e2014-10-15 11:35:55 +010019import android.annotation.Nullable;
Lifu Tangdb0b7632014-08-18 11:08:43 -070020import android.content.Context;
21import android.graphics.drawable.Drawable;
Jason Monk39b46742015-09-10 15:52:51 -040022import android.support.v7.preference.Preference;
23import android.support.v7.preference.PreferenceViewHolder;
Zoltan Szatmary-Ban86c877e2014-10-15 11:35:55 +010024import android.text.TextUtils;
Lifu Tangdb0b7632014-08-18 11:08:43 -070025import android.util.AttributeSet;
Zoltan Szatmary-Ban86c877e2014-10-15 11:35:55 +010026import android.widget.TextView;
Lifu Tangdb0b7632014-08-18 11:08:43 -070027
Sudheer Shanka682a9162016-01-13 22:10:18 +000028import com.android.settingslib.RestrictedPreference;
29
Lifu Tangdb0b7632014-08-18 11:08:43 -070030/**
31 * A preference item that can dim the icon when it's disabled, either directly or because its parent
32 * is disabled.
33 */
Sudheer Shanka682a9162016-01-13 22:10:18 +000034public class DimmableIconPreference extends RestrictedPreference {
Lifu Tangdb0b7632014-08-18 11:08:43 -070035 private static final int ICON_ALPHA_ENABLED = 255;
36 private static final int ICON_ALPHA_DISABLED = 102;
37
Zoltan Szatmary-Ban86c877e2014-10-15 11:35:55 +010038 private final CharSequence mContentDescription;
39
Sudheer Shanka682a9162016-01-13 22:10:18 +000040 public DimmableIconPreference(Context context) {
41 this(context, (AttributeSet) null);
42 }
43
Fyodor Kupolova7a7bfe2015-05-08 15:54:38 -070044 public DimmableIconPreference(Context context, AttributeSet attrs) {
Lifu Tangdb0b7632014-08-18 11:08:43 -070045 super(context, attrs);
Fyodor Kupolova7a7bfe2015-05-08 15:54:38 -070046 mContentDescription = null;
Sudheer Shankaba1a68b2016-01-25 22:39:34 +000047 useAdminDisabledSummary(true);
Lifu Tangdb0b7632014-08-18 11:08:43 -070048 }
49
Zoltan Szatmary-Ban86c877e2014-10-15 11:35:55 +010050 public DimmableIconPreference(Context context, @Nullable CharSequence contentDescription) {
Lifu Tangdb0b7632014-08-18 11:08:43 -070051 super(context);
Zoltan Szatmary-Ban86c877e2014-10-15 11:35:55 +010052 mContentDescription = contentDescription;
Sudheer Shankaba1a68b2016-01-25 22:39:34 +000053 useAdminDisabledSummary(true);
Lifu Tangdb0b7632014-08-18 11:08:43 -070054 }
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
Jason Monk39b46742015-09-10 15:52:51 -040065 public void onBindViewHolder(PreferenceViewHolder view) {
66 super.onBindViewHolder(view);
Zoltan Szatmary-Ban86c877e2014-10-15 11:35:55 +010067 if (!TextUtils.isEmpty(mContentDescription)) {
68 final TextView titleView = (TextView) view.findViewById(android.R.id.title);
69 titleView.setContentDescription(mContentDescription);
70 }
Sudheer Shanka682a9162016-01-13 22:10:18 +000071 dimIcon(!isEnabled());
Zoltan Szatmary-Ban86c877e2014-10-15 11:35:55 +010072 }
Lifu Tangdb0b7632014-08-18 11:08:43 -070073}