Home | History | Annotate | Download | only in location
      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.content.Context;
     20 import android.graphics.drawable.Drawable;
     21 import android.preference.Preference;
     22 import android.util.AttributeSet;
     23 
     24 /**
     25  * A preference item that can dim the icon when it's disabled, either directly or because its parent
     26  * is disabled.
     27  */
     28 public class DimmableIconPreference extends Preference {
     29     private static final int ICON_ALPHA_ENABLED = 255;
     30     private static final int ICON_ALPHA_DISABLED = 102;
     31 
     32     public DimmableIconPreference(Context context, AttributeSet attrs, int defStyle) {
     33         super(context, attrs, defStyle);
     34     }
     35 
     36     public DimmableIconPreference(Context context, AttributeSet attrs) {
     37         super(context, attrs);
     38     }
     39 
     40     public DimmableIconPreference(Context context) {
     41         super(context);
     42     }
     43 
     44     private void dimIcon(boolean dimmed) {
     45         Drawable icon = getIcon();
     46         if (icon != null) {
     47             icon.mutate().setAlpha(dimmed ? ICON_ALPHA_DISABLED : ICON_ALPHA_ENABLED);
     48             setIcon(icon);
     49         }
     50     }
     51 
     52     @Override
     53     public void onParentChanged(Preference parent, boolean disableChild) {
     54         dimIcon(disableChild);
     55         super.onParentChanged(parent, disableChild);
     56     }
     57 
     58     @Override
     59     public void setEnabled(boolean enabled) {
     60         dimIcon(!enabled);
     61         super.setEnabled(enabled);
     62     }
     63 }
     64