Lifu Tang | db0b763 | 2014-08-18 11:08:43 -0700 | [diff] [blame] | 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 | |
Fyodor Kupolov | a7a7bfe | 2015-05-08 15:54:38 -0700 | [diff] [blame] | 17 | package com.android.settings; |
Lifu Tang | db0b763 | 2014-08-18 11:08:43 -0700 | [diff] [blame] | 18 | |
Zoltan Szatmary-Ban | 86c877e | 2014-10-15 11:35:55 +0100 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Lifu Tang | db0b763 | 2014-08-18 11:08:43 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.graphics.drawable.Drawable; |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 22 | import android.support.v7.preference.Preference; |
| 23 | import android.support.v7.preference.PreferenceViewHolder; |
Zoltan Szatmary-Ban | 86c877e | 2014-10-15 11:35:55 +0100 | [diff] [blame] | 24 | import android.text.TextUtils; |
Lifu Tang | db0b763 | 2014-08-18 11:08:43 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
Zoltan Szatmary-Ban | 86c877e | 2014-10-15 11:35:55 +0100 | [diff] [blame] | 26 | import android.widget.TextView; |
Lifu Tang | db0b763 | 2014-08-18 11:08:43 -0700 | [diff] [blame] | 27 | |
Sudheer Shanka | 682a916 | 2016-01-13 22:10:18 +0000 | [diff] [blame] | 28 | import com.android.settingslib.RestrictedPreference; |
| 29 | |
Lifu Tang | db0b763 | 2014-08-18 11:08:43 -0700 | [diff] [blame] | 30 | /** |
| 31 | * A preference item that can dim the icon when it's disabled, either directly or because its parent |
| 32 | * is disabled. |
| 33 | */ |
Sudheer Shanka | 682a916 | 2016-01-13 22:10:18 +0000 | [diff] [blame] | 34 | public class DimmableIconPreference extends RestrictedPreference { |
Lifu Tang | db0b763 | 2014-08-18 11:08:43 -0700 | [diff] [blame] | 35 | private static final int ICON_ALPHA_ENABLED = 255; |
| 36 | private static final int ICON_ALPHA_DISABLED = 102; |
| 37 | |
Zoltan Szatmary-Ban | 86c877e | 2014-10-15 11:35:55 +0100 | [diff] [blame] | 38 | private final CharSequence mContentDescription; |
| 39 | |
Sudheer Shanka | 682a916 | 2016-01-13 22:10:18 +0000 | [diff] [blame] | 40 | public DimmableIconPreference(Context context) { |
| 41 | this(context, (AttributeSet) null); |
| 42 | } |
| 43 | |
Fyodor Kupolov | a7a7bfe | 2015-05-08 15:54:38 -0700 | [diff] [blame] | 44 | public DimmableIconPreference(Context context, AttributeSet attrs) { |
Lifu Tang | db0b763 | 2014-08-18 11:08:43 -0700 | [diff] [blame] | 45 | super(context, attrs); |
Fyodor Kupolov | a7a7bfe | 2015-05-08 15:54:38 -0700 | [diff] [blame] | 46 | mContentDescription = null; |
Sudheer Shanka | ba1a68b | 2016-01-25 22:39:34 +0000 | [diff] [blame] | 47 | useAdminDisabledSummary(true); |
Lifu Tang | db0b763 | 2014-08-18 11:08:43 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Zoltan Szatmary-Ban | 86c877e | 2014-10-15 11:35:55 +0100 | [diff] [blame] | 50 | public DimmableIconPreference(Context context, @Nullable CharSequence contentDescription) { |
Lifu Tang | db0b763 | 2014-08-18 11:08:43 -0700 | [diff] [blame] | 51 | super(context); |
Zoltan Szatmary-Ban | 86c877e | 2014-10-15 11:35:55 +0100 | [diff] [blame] | 52 | mContentDescription = contentDescription; |
Sudheer Shanka | ba1a68b | 2016-01-25 22:39:34 +0000 | [diff] [blame] | 53 | useAdminDisabledSummary(true); |
Lifu Tang | db0b763 | 2014-08-18 11:08:43 -0700 | [diff] [blame] | 54 | } |
| 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 Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 65 | public void onBindViewHolder(PreferenceViewHolder view) { |
| 66 | super.onBindViewHolder(view); |
Zoltan Szatmary-Ban | 86c877e | 2014-10-15 11:35:55 +0100 | [diff] [blame] | 67 | if (!TextUtils.isEmpty(mContentDescription)) { |
| 68 | final TextView titleView = (TextView) view.findViewById(android.R.id.title); |
| 69 | titleView.setContentDescription(mContentDescription); |
| 70 | } |
Sudheer Shanka | 682a916 | 2016-01-13 22:10:18 +0000 | [diff] [blame] | 71 | dimIcon(!isEnabled()); |
Zoltan Szatmary-Ban | 86c877e | 2014-10-15 11:35:55 +0100 | [diff] [blame] | 72 | } |
Lifu Tang | db0b763 | 2014-08-18 11:08:43 -0700 | [diff] [blame] | 73 | } |