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 boolean state = false; 45 if (mGear != null) { 46 state = enabled && !(isDisabledByAdmin() || isDisabledByEcm()); 47 mGear.setEnabled(state); 48 mGear.setImageAlpha(state ? VALUE_ENABLED_ALPHA : mDisabledAlphaValue); 49 } 50 mGearState = state; 51 } 52 53 @Override onBindViewHolder(PreferenceViewHolder holder)54 public void onBindViewHolder(PreferenceViewHolder holder) { 55 super.onBindViewHolder(holder); 56 mGear = (ImageView) holder.findViewById(R.id.settings_button); 57 setGearEnabled(mGearState); 58 } 59 } 60