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 17 package com.android.settings; 18 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; 27 28 /** 29 * A preference item that can dim the icon when it's disabled, either directly or because its parent 30 * is disabled. 31 */ 32 public class DimmableIconPreference extends Preference { 33 private static final int ICON_ALPHA_ENABLED = 255; 34 private static final int ICON_ALPHA_DISABLED = 102; 35 36 private final CharSequence mContentDescription; 37 DimmableIconPreference(Context context, AttributeSet attrs)38 public DimmableIconPreference(Context context, AttributeSet attrs) { 39 super(context, attrs); 40 mContentDescription = null; 41 } 42 DimmableIconPreference(Context context, @Nullable CharSequence contentDescription)43 public DimmableIconPreference(Context context, @Nullable CharSequence contentDescription) { 44 super(context); 45 mContentDescription = contentDescription; 46 } 47 dimIcon(boolean dimmed)48 private void dimIcon(boolean dimmed) { 49 Drawable icon = getIcon(); 50 if (icon != null) { 51 icon.mutate().setAlpha(dimmed ? ICON_ALPHA_DISABLED : ICON_ALPHA_ENABLED); 52 setIcon(icon); 53 } 54 } 55 56 @Override onParentChanged(Preference parent, boolean disableChild)57 public void onParentChanged(Preference parent, boolean disableChild) { 58 dimIcon(disableChild); 59 super.onParentChanged(parent, disableChild); 60 } 61 62 @Override setEnabled(boolean enabled)63 public void setEnabled(boolean enabled) { 64 dimIcon(!enabled); 65 super.setEnabled(enabled); 66 } 67 68 @Override onBindView(View view)69 protected void onBindView(View view) { 70 super.onBindView(view); 71 if (!TextUtils.isEmpty(mContentDescription)) { 72 final TextView titleView = (TextView) view.findViewById(android.R.id.title); 73 titleView.setContentDescription(mContentDescription); 74 } 75 } 76 } 77