• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.os.BatteryUsageStats;
21 import android.util.AttributeSet;
22 import android.util.Log;
23 import android.view.View;
24 import android.widget.TextView;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.VisibleForTesting;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceViewHolder;
30 
31 import com.android.settings.R;
32 import com.android.settings.fuelgauge.BatteryInfo;
33 import com.android.settings.fuelgauge.BatteryUtils;
34 import com.android.settings.overlay.FeatureFactory;
35 import com.android.settings.widget.UsageView;
36 
37 /**
38  * Custom preference for displaying the battery level as chart graph.
39  */
40 public class BatteryHistoryPreference extends Preference {
41     private static final String TAG = "BatteryHistoryPreference";
42 
43     @VisibleForTesting
44     boolean mHideSummary;
45     @VisibleForTesting
46     BatteryInfo mBatteryInfo;
47 
48     private boolean mIsChartGraphEnabled;
49 
50     private TextView mSummaryView;
51     private CharSequence mSummaryContent;
52     private BatteryChartView mDailyChartView;
53     private BatteryChartView mHourlyChartView;
54     private BatteryChartPreferenceController mChartPreferenceController;
55 
BatteryHistoryPreference(Context context, AttributeSet attrs)56     public BatteryHistoryPreference(Context context, AttributeSet attrs) {
57         super(context, attrs);
58         mIsChartGraphEnabled =
59                 FeatureFactory.getFactory(context).getPowerUsageFeatureProvider(context)
60                         .isChartGraphEnabled(context);
61         Log.i(TAG, "isChartGraphEnabled: " + mIsChartGraphEnabled);
62         setLayoutResource(
63                 mIsChartGraphEnabled
64                         ? R.layout.battery_chart_graph
65                         : R.layout.battery_usage_graph);
66         setSelectable(false);
67     }
68 
69     /** Sets the text of bottom summary. */
setBottomSummary(CharSequence text)70     public void setBottomSummary(CharSequence text) {
71         mSummaryContent = text;
72         if (mSummaryView != null) {
73             mSummaryView.setVisibility(View.VISIBLE);
74             mSummaryView.setText(mSummaryContent);
75         }
76         mHideSummary = false;
77     }
78 
79     /** Hides the bottom summary. */
hideBottomSummary()80     public void hideBottomSummary() {
81         if (mSummaryView != null) {
82             mSummaryView.setVisibility(View.GONE);
83         }
84         mHideSummary = true;
85     }
86 
setBatteryUsageStats(@onNull BatteryUsageStats batteryUsageStats)87     void setBatteryUsageStats(@NonNull BatteryUsageStats batteryUsageStats) {
88         BatteryInfo.getBatteryInfo(getContext(), info -> {
89             mBatteryInfo = info;
90             notifyChanged();
91         }, batteryUsageStats, false);
92     }
93 
setChartPreferenceController(BatteryChartPreferenceController controller)94     void setChartPreferenceController(BatteryChartPreferenceController controller) {
95         mChartPreferenceController = controller;
96         if (mDailyChartView != null && mHourlyChartView != null) {
97             mChartPreferenceController.setBatteryChartView(mDailyChartView, mHourlyChartView);
98         }
99     }
100 
101     @Override
onBindViewHolder(PreferenceViewHolder view)102     public void onBindViewHolder(PreferenceViewHolder view) {
103         super.onBindViewHolder(view);
104         final long startTime = System.currentTimeMillis();
105         if (mBatteryInfo == null) {
106             return;
107         }
108         if (mIsChartGraphEnabled) {
109             final TextView companionTextView = (TextView) view.findViewById(R.id.companion_text);
110             mDailyChartView = (BatteryChartView) view.findViewById(R.id.daily_battery_chart);
111             mDailyChartView.setCompanionTextView(companionTextView);
112             mHourlyChartView = (BatteryChartView) view.findViewById(R.id.hourly_battery_chart);
113             mHourlyChartView.setCompanionTextView(companionTextView);
114             if (mChartPreferenceController != null) {
115                 mChartPreferenceController.setBatteryChartView(mDailyChartView, mHourlyChartView);
116             }
117         } else {
118             final TextView chargeView = (TextView) view.findViewById(R.id.charge);
119             chargeView.setText(mBatteryInfo.batteryPercentString);
120             mSummaryView = (TextView) view.findViewById(R.id.bottom_summary);
121             if (mSummaryContent != null) {
122                 mSummaryView.setText(mSummaryContent);
123             }
124             if (mHideSummary) {
125                 mSummaryView.setVisibility(View.GONE);
126             }
127             final UsageView usageView = (UsageView) view.findViewById(R.id.battery_usage);
128             usageView.findViewById(R.id.label_group).setAlpha(.7f);
129             mBatteryInfo.bindHistory(usageView);
130         }
131         BatteryUtils.logRuntime(TAG, "onBindViewHolder", startTime);
132     }
133 }
134