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; 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.TextView; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceViewHolder; 28 29 import com.android.settings.R; 30 31 /** Custom preference for displaying the app power usage time. */ 32 public class PowerUsageTimePreference extends Preference { 33 private static final String TAG = "PowerUsageTimePreference"; 34 35 @VisibleForTesting CharSequence mTimeTitle; 36 @VisibleForTesting CharSequence mTimeSummary; 37 @VisibleForTesting CharSequence mAnomalyHintText; 38 PowerUsageTimePreference(Context context, AttributeSet attrs)39 public PowerUsageTimePreference(Context context, AttributeSet attrs) { 40 super(context, attrs); 41 setLayoutResource(R.layout.power_usage_time); 42 } 43 setTimeTitle(CharSequence timeTitle)44 void setTimeTitle(CharSequence timeTitle) { 45 if (!TextUtils.equals(mTimeTitle, timeTitle)) { 46 mTimeTitle = timeTitle; 47 notifyChanged(); 48 } 49 } 50 setTimeSummary(CharSequence timeSummary)51 void setTimeSummary(CharSequence timeSummary) { 52 if (!TextUtils.equals(mTimeSummary, timeSummary)) { 53 mTimeSummary = timeSummary; 54 notifyChanged(); 55 } 56 } 57 setAnomalyHint(CharSequence anomalyHintText)58 void setAnomalyHint(CharSequence anomalyHintText) { 59 if (!TextUtils.equals(mAnomalyHintText, anomalyHintText)) { 60 mAnomalyHintText = anomalyHintText; 61 notifyChanged(); 62 } 63 } 64 showAnomalyHint(PreferenceViewHolder view)65 private void showAnomalyHint(PreferenceViewHolder view) { 66 if (TextUtils.isEmpty(mAnomalyHintText)) { 67 return; 68 } 69 final View anomalyHintView = view.findViewById(R.id.anomaly_hints); 70 if (anomalyHintView == null) { 71 return; 72 } 73 final TextView warningInfo = anomalyHintView.findViewById(R.id.warning_info); 74 if (warningInfo == null) { 75 return; 76 } 77 warningInfo.setText(mAnomalyHintText); 78 anomalyHintView.setVisibility(View.VISIBLE); 79 } 80 81 @Override onBindViewHolder(PreferenceViewHolder view)82 public void onBindViewHolder(PreferenceViewHolder view) { 83 super.onBindViewHolder(view); 84 85 ((TextView) view.findViewById(R.id.time_title)).setText(mTimeTitle); 86 ((TextView) view.findViewById(R.id.time_summary)).setText(mTimeSummary); 87 88 showAnomalyHint(view); 89 } 90 } 91