• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.fuelgauge;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.text.TextUtils;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.widget.TextView;
25 
26 import androidx.preference.Preference;
27 import androidx.preference.PreferenceViewHolder;
28 
29 import com.android.settings.R;
30 import com.android.settingslib.Utils;
31 import com.android.settingslib.widget.SettingsThemeHelper;
32 
33 /**
34  * Custom preference for displaying the {@link Preference} with an optional hint chip.
35  */
36 public class WarningFramePreference extends Preference {
37     private final int mTitleColorNormal;
38     private final int mSummaryColorNormal;
39     private final int mWarningChipBackgroundResId;
40     private final boolean mIsExpressiveTheme;
41 
42     @Nullable private CharSequence mHintText;
43 
WarningFramePreference(Context context, AttributeSet attrs)44     public WarningFramePreference(Context context, AttributeSet attrs) {
45         super(context, attrs);
46         mIsExpressiveTheme = SettingsThemeHelper.isExpressiveTheme(context);
47         int layoutResId =
48                 mIsExpressiveTheme
49                         ? R.layout.expressive_warning_frame_preference
50                         : R.layout.warning_frame_preference;
51         setLayoutResource(layoutResId);
52         mWarningChipBackgroundResId =
53                 mIsExpressiveTheme
54                         ? R.drawable.expressive_battery_hints_chip_bg_ripple
55                         : R.drawable.battery_hints_chip_bg_ripple;
56         mTitleColorNormal =
57                 Utils.getColorAttrDefaultColor(context, android.R.attr.textColorPrimary);
58         mSummaryColorNormal =
59                 Utils.getColorAttrDefaultColor(context, android.R.attr.textColorSecondary);
60     }
61 
62     /** Sets the text of hint to show. */
setHint(@ullable CharSequence hintText)63     public void setHint(@Nullable CharSequence hintText) {
64         if (!TextUtils.equals(mHintText, hintText)) {
65             mHintText = hintText;
66             notifyChanged();
67         }
68     }
69 
70     @Override
onBindViewHolder(PreferenceViewHolder view)71     public void onBindViewHolder(PreferenceViewHolder view) {
72         super.onBindViewHolder(view);
73 
74         if (mIsExpressiveTheme) {
75             final View preferenceFrame = view.findViewById(R.id.preference_frame);
76             preferenceFrame.setBackground(null);
77             preferenceFrame.setPadding(0, 0, 0, 0);
78         }
79 
80         final View warningChipFrame = view.findViewById(R.id.warning_chip_frame);
81         warningChipFrame
82                 .findViewById(R.id.warning_padding_placeholder)
83                 .setVisibility(getIcon() != null ? View.VISIBLE : View.GONE);
84         if (!TextUtils.isEmpty(mHintText)) {
85             ((TextView) warningChipFrame.findViewById(R.id.warning_info)).setText(mHintText);
86             warningChipFrame.setVisibility(View.VISIBLE);
87             warningChipFrame
88                     .findViewById(R.id.warning_chip)
89                     .setBackgroundResource(mWarningChipBackgroundResId);
90         } else {
91             warningChipFrame.setVisibility(View.GONE);
92         }
93         ((TextView) view.findViewById(android.R.id.title)).setTextColor(mTitleColorNormal);
94         ((TextView) view.findViewById(android.R.id.summary)).setTextColor(mSummaryColorNormal);
95     }
96 }
97 
98