1 /* 2 * Copyright (C) 2021 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.widget; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.widget.ImageView; 22 23 import androidx.preference.PreferenceViewHolder; 24 25 import com.android.settings.R; 26 import com.android.settingslib.utils.ColorUtil; 27 28 /** A preference with a Gear on the side and mutable Gear color. */ 29 public class MutableGearPreference extends GearPreference { 30 private static final int VALUE_ENABLED_ALPHA = 255; 31 32 private ImageView mGear; 33 private Context mContext; 34 private int mDisabledAlphaValue; 35 MutableGearPreference(Context context, AttributeSet attrs)36 public MutableGearPreference(Context context, AttributeSet attrs) { 37 super(context, attrs); 38 mContext = context; 39 mDisabledAlphaValue = (int) (ColorUtil.getDisabledAlpha(context) * VALUE_ENABLED_ALPHA); 40 } 41 42 @Override setGearEnabled(boolean enabled)43 public void setGearEnabled(boolean enabled) { 44 if (mGear != null) { 45 mGear.setEnabled(enabled); 46 mGear.setImageAlpha(enabled ? VALUE_ENABLED_ALPHA : mDisabledAlphaValue); 47 } 48 mGearState = enabled; 49 } 50 51 @Override onBindViewHolder(PreferenceViewHolder holder)52 public void onBindViewHolder(PreferenceViewHolder holder) { 53 super.onBindViewHolder(holder); 54 mGear = (ImageView) holder.findViewById(R.id.settings_button); 55 setGearEnabled(mGearState); 56 } 57 } 58