2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.location;
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.graphics.drawable.Drawable;
22 import android.preference.Preference;
23 import android.text.TextUtils;
24 import android.util.AttributeSet;
25 import android.view.View;
26 import android.widget.TextView;
29 * A preference item that can dim the icon when it's disabled, either directly or because its parent
32 public class DimmableIconPreference extends Preference {
33 private static final int ICON_ALPHA_ENABLED = 255;
34 private static final int ICON_ALPHA_DISABLED = 102;
36 private final CharSequence mContentDescription;
38 public DimmableIconPreference(Context context, AttributeSet attrs, int defStyle,
39 @Nullable CharSequence contentDescription) {
40 super(context, attrs, defStyle);
41 mContentDescription = contentDescription;
44 public DimmableIconPreference(Context context, AttributeSet attrs,
45 @Nullable CharSequence contentDescription) {
46 super(context, attrs);
47 mContentDescription = contentDescription;
50 public DimmableIconPreference(Context context, @Nullable CharSequence contentDescription) {
52 mContentDescription = contentDescription;
55 private void dimIcon(boolean dimmed) {
56 Drawable icon = getIcon();
58 icon.mutate().setAlpha(dimmed ? ICON_ALPHA_DISABLED : ICON_ALPHA_ENABLED);
64 public void onParentChanged(Preference parent, boolean disableChild) {
65 dimIcon(disableChild);
66 super.onParentChanged(parent, disableChild);
70 public void setEnabled(boolean enabled) {
72 super.setEnabled(enabled);
76 protected void onBindView(View view) {
77 super.onBindView(view);
78 if (!TextUtils.isEmpty(mContentDescription)) {
79 final TextView titleView = (TextView) view.findViewById(android.R.id.title);
80 titleView.setContentDescription(mContentDescription);