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