1 /*
2  * Copyright (C) 2023 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.batteryusage;
18 
19 import android.content.Context;
20 import android.text.TextUtils;
21 import android.util.AttributeSet;
22 import android.view.View;
23 import android.widget.ImageView;
24 import android.widget.LinearLayout;
25 import android.widget.TextView;
26 
27 import androidx.annotation.VisibleForTesting;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceViewHolder;
30 
31 import com.android.settings.R;
32 
33 import com.google.android.material.button.MaterialButton;
34 
35 /** A preference for displaying the battery tips card view. */
36 public class BatteryTipsCardPreference extends Preference implements View.OnClickListener {
37 
38     private static final String TAG = "BatteryTipsCardPreference";
39 
40     interface OnConfirmListener {
onConfirm()41         void onConfirm();
42     }
43 
44     interface OnRejectListener {
onReject()45         void onReject();
46     }
47 
48     private OnConfirmListener mOnConfirmListener;
49     private OnRejectListener mOnRejectListener;
50     private int mIconResourceId = 0;
51     private int mButtonColorResourceId = 0;
52 
53     @VisibleForTesting CharSequence mMainButtonLabel;
54     @VisibleForTesting CharSequence mDismissButtonLabel;
55 
BatteryTipsCardPreference(Context context, AttributeSet attrs)56     public BatteryTipsCardPreference(Context context, AttributeSet attrs) {
57         super(context, attrs);
58         setLayoutResource(R.layout.battery_tips_card);
59         setViewId(R.id.battery_tips_card);
60         setSelectable(false);
61     }
62 
setOnConfirmListener(OnConfirmListener listener)63     public void setOnConfirmListener(OnConfirmListener listener) {
64         mOnConfirmListener = listener;
65     }
66 
setOnRejectListener(OnRejectListener listener)67     public void setOnRejectListener(OnRejectListener listener) {
68         mOnRejectListener = listener;
69     }
70 
71     /**
72      * Sets the icon in tips card.
73      */
setIconResourceId(int resourceId)74     public void setIconResourceId(int resourceId) {
75         if (mIconResourceId != resourceId) {
76             mIconResourceId = resourceId;
77             notifyChanged();
78         }
79     }
80 
81     /**
82      * Sets the background color for main button and the text color for dismiss button.
83      */
setButtonColorResourceId(int resourceId)84     public void setButtonColorResourceId(int resourceId) {
85         if (mButtonColorResourceId != resourceId) {
86             mButtonColorResourceId = resourceId;
87             notifyChanged();
88         }
89     }
90 
91     /**
92      * Sets the label of main button in tips card.
93      */
setMainButtonLabel(CharSequence label)94     public void setMainButtonLabel(CharSequence label) {
95         if (!TextUtils.equals(mMainButtonLabel, label)) {
96             mMainButtonLabel = label;
97             notifyChanged();
98         }
99     }
100 
101     /**
102      * Sets the label of dismiss button in tips card.
103      */
setDismissButtonLabel(CharSequence label)104     public void setDismissButtonLabel(CharSequence label) {
105         if (!TextUtils.equals(mDismissButtonLabel, label)) {
106             mDismissButtonLabel = label;
107             notifyChanged();
108         }
109     }
110 
111     @Override
onClick(View view)112     public void onClick(View view) {
113         final int viewId = view.getId();
114         if (viewId == R.id.main_button || viewId == R.id.battery_tips_card) {
115             if (mOnConfirmListener != null) {
116                 mOnConfirmListener.onConfirm();
117             }
118         } else if (viewId == R.id.dismiss_button) {
119             if (mOnRejectListener != null) {
120                 mOnRejectListener.onReject();
121             }
122         }
123     }
124 
125     @Override
onBindViewHolder(PreferenceViewHolder view)126     public void onBindViewHolder(PreferenceViewHolder view) {
127         super.onBindViewHolder(view);
128 
129         ((TextView) view.findViewById(R.id.title)).setText(getTitle());
130 
131         final LinearLayout tipsCard = (LinearLayout) view.findViewById(R.id.battery_tips_card);
132         tipsCard.setOnClickListener(this);
133         final MaterialButton mainButton = (MaterialButton) view.findViewById(R.id.main_button);
134         mainButton.setOnClickListener(this);
135         mainButton.setText(mMainButtonLabel);
136         final MaterialButton dismissButton =
137                 (MaterialButton) view.findViewById(R.id.dismiss_button);
138         dismissButton.setOnClickListener(this);
139         dismissButton.setText(mDismissButtonLabel);
140         if (mButtonColorResourceId != 0) {
141             final int colorInt = getContext().getColor(mButtonColorResourceId);
142             mainButton.setBackgroundColor(colorInt);
143             dismissButton.setTextColor(colorInt);
144         }
145 
146         if (mIconResourceId != 0) {
147             ((ImageView) view.findViewById(R.id.icon)).setImageResource(mIconResourceId);
148         }
149     }
150 }
151