• 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.util.AttributeSet;
21 import android.view.View;
22 import android.widget.TextView;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.Preference;
26 import androidx.preference.PreferenceViewHolder;
27 
28 import com.android.internal.os.BatteryStatsHelper;
29 import com.android.settings.R;
30 import com.android.settings.widget.UsageView;
31 
32 /**
33  * Custom preference for displaying power consumption as a bar and an icon on the left for the
34  * subsystem/app type.
35  */
36 public class BatteryHistoryPreference extends Preference {
37     private static final String TAG = "BatteryHistoryPreference";
38 
39     private CharSequence mSummary;
40     private TextView mSummaryView;
41 
42     @VisibleForTesting
43     boolean hideSummary;
44     @VisibleForTesting
45     BatteryInfo mBatteryInfo;
46 
BatteryHistoryPreference(Context context, AttributeSet attrs)47     public BatteryHistoryPreference(Context context, AttributeSet attrs) {
48         super(context, attrs);
49         setLayoutResource(R.layout.battery_usage_graph);
50         setSelectable(false);
51     }
52 
setStats(BatteryStatsHelper batteryStats)53     public void setStats(BatteryStatsHelper batteryStats) {
54         BatteryInfo.getBatteryInfo(getContext(), info -> {
55             mBatteryInfo = info;
56             notifyChanged();
57         }, batteryStats, false);
58     }
59 
setBottomSummary(CharSequence text)60     public void setBottomSummary(CharSequence text) {
61         mSummary = text;
62         if (mSummaryView != null) {
63             mSummaryView.setVisibility(View.VISIBLE);
64             mSummaryView.setText(mSummary);
65         }
66         hideSummary = false;
67     }
68 
hideBottomSummary()69     public void hideBottomSummary() {
70         if (mSummaryView != null) {
71             mSummaryView.setVisibility(View.GONE);
72         }
73         hideSummary = true;
74     }
75 
76     @Override
onBindViewHolder(PreferenceViewHolder view)77     public void onBindViewHolder(PreferenceViewHolder view) {
78         super.onBindViewHolder(view);
79         final long startTime = System.currentTimeMillis();
80         if (mBatteryInfo == null) {
81             return;
82         }
83 
84         ((TextView) view.findViewById(R.id.charge)).setText(mBatteryInfo.batteryPercentString);
85         mSummaryView = (TextView) view.findViewById(R.id.bottom_summary);
86         if (mSummary != null) {
87             mSummaryView.setText(mSummary);
88         }
89         if (hideSummary) {
90             mSummaryView.setVisibility(View.GONE);
91         }
92         UsageView usageView = (UsageView) view.findViewById(R.id.battery_usage);
93         usageView.findViewById(R.id.label_group).setAlpha(.7f);
94         mBatteryInfo.bindHistory(usageView);
95         BatteryUtils.logRuntime(TAG, "onBindViewHolder", startTime);
96     }
97 }
98