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.location; 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, int defStyle, @Nullable CharSequence contentDescription)38 public DimmableIconPreference(Context context, AttributeSet attrs, int defStyle, 39 @Nullable CharSequence contentDescription) { 40 super(context, attrs, defStyle); 41 mContentDescription = contentDescription; 42 } 43 DimmableIconPreference(Context context, AttributeSet attrs, @Nullable CharSequence contentDescription)44 public DimmableIconPreference(Context context, AttributeSet attrs, 45 @Nullable CharSequence contentDescription) { 46 super(context, attrs); 47 mContentDescription = contentDescription; 48 } 49 DimmableIconPreference(Context context, @Nullable CharSequence contentDescription)50 public DimmableIconPreference(Context context, @Nullable CharSequence contentDescription) { 51 super(context); 52 mContentDescription = contentDescription; 53 } 54 dimIcon(boolean dimmed)55 private void dimIcon(boolean dimmed) { 56 Drawable icon = getIcon(); 57 if (icon != null) { 58 icon.mutate().setAlpha(dimmed ? ICON_ALPHA_DISABLED : ICON_ALPHA_ENABLED); 59 setIcon(icon); 60 } 61 } 62 63 @Override onParentChanged(Preference parent, boolean disableChild)64 public void onParentChanged(Preference parent, boolean disableChild) { 65 dimIcon(disableChild); 66 super.onParentChanged(parent, disableChild); 67 } 68 69 @Override setEnabled(boolean enabled)70 public void setEnabled(boolean enabled) { 71 dimIcon(!enabled); 72 super.setEnabled(enabled); 73 } 74 75 @Override onBindView(View view)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); 81 } 82 } 83 } 84