1 /* 2 * Copyright (C) 2024 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.accessibility; 18 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.graphics.drawable.Drawable; 22 import android.util.AttributeSet; 23 import android.widget.LinearLayout; 24 25 import androidx.annotation.DrawableRes; 26 import androidx.annotation.NonNull; 27 import androidx.annotation.Nullable; 28 import androidx.core.content.res.TypedArrayUtils; 29 import androidx.preference.Preference; 30 import androidx.preference.PreferenceViewHolder; 31 32 import com.android.settings.R; 33 34 /** 35 * A preference with custom background. 36 */ 37 public class BackgroundPreference extends Preference { 38 39 private int mBackgroundId; 40 BackgroundPreference(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)41 public BackgroundPreference(@NonNull Context context, 42 @Nullable AttributeSet attrs, int defStyleAttr, 43 int defStyleRes) { 44 super(context, attrs, defStyleAttr, defStyleRes); 45 setLayoutResource(R.layout.preference_background); 46 setIconSpaceReserved(false); 47 48 final TypedArray styledAttrs = context.obtainStyledAttributes(attrs, 49 R.styleable.BackgroundPreference); 50 mBackgroundId = styledAttrs.getResourceId(R.styleable.BackgroundPreference_background, 0); 51 styledAttrs.recycle(); 52 } 53 BackgroundPreference(@onNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)54 public BackgroundPreference(@NonNull Context context, @Nullable AttributeSet attrs, 55 int defStyleAttr) { 56 this(context, attrs, defStyleAttr, 0); 57 } 58 BackgroundPreference(@onNull Context context, @Nullable AttributeSet attrs)59 public BackgroundPreference(@NonNull Context context, @Nullable AttributeSet attrs) { 60 this(context, attrs, TypedArrayUtils.getAttr(context, 61 androidx.preference.R.attr.preferenceStyle, 62 com.android.internal.R.attr.preferenceStyle), 0); 63 } 64 BackgroundPreference(@onNull Context context)65 public BackgroundPreference(@NonNull Context context) { 66 this(context, null, TypedArrayUtils.getAttr(context, 67 androidx.preference.R.attr.preferenceStyle, 68 com.android.internal.R.attr.preferenceStyle), 0); 69 } 70 71 @Override onBindViewHolder(@onNull PreferenceViewHolder holder)72 public void onBindViewHolder(@NonNull PreferenceViewHolder holder) { 73 super.onBindViewHolder(holder); 74 75 final LinearLayout layout = (LinearLayout) holder.findViewById(R.id.background); 76 if (mBackgroundId != 0) { 77 final Drawable backgroundDrawable = getContext().getDrawable(mBackgroundId); 78 layout.setBackground(backgroundDrawable); 79 } 80 } 81 82 /** 83 * Sets the background to a given resource. The resource should refer to a Drawable object. 84 * 85 * @param resId The identifier of the resource. 86 */ setBackground(@rawableRes int resId)87 public void setBackground(@DrawableRes int resId) { 88 if (mBackgroundId != resId) { 89 mBackgroundId = resId; 90 notifyChanged(); 91 } 92 } 93 } 94