• 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.util.AttributeSet;
21 import android.widget.TextView;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceViewHolder;
25 
26 import com.android.settings.R;
27 import com.android.settings.fuelgauge.BatteryUtils;
28 
29 /**
30  * Custom preference for displaying the battery level as chart graph.
31  */
32 public class BatteryHistoryPreference extends Preference {
33     private static final String TAG = "BatteryHistoryPreference";
34 
35     private BatteryChartView mDailyChartView;
36     private BatteryChartView mHourlyChartView;
37     private BatteryChartPreferenceController mChartPreferenceController;
38 
BatteryHistoryPreference(Context context, AttributeSet attrs)39     public BatteryHistoryPreference(Context context, AttributeSet attrs) {
40         super(context, attrs);
41         setLayoutResource(R.layout.battery_chart_graph);
42         setSelectable(false);
43     }
44 
setChartPreferenceController(BatteryChartPreferenceController controller)45     void setChartPreferenceController(BatteryChartPreferenceController controller) {
46         mChartPreferenceController = controller;
47         if (mDailyChartView != null && mHourlyChartView != null) {
48             mChartPreferenceController.setBatteryChartView(mDailyChartView, mHourlyChartView);
49         }
50     }
51 
52     @Override
onBindViewHolder(PreferenceViewHolder view)53     public void onBindViewHolder(PreferenceViewHolder view) {
54         super.onBindViewHolder(view);
55         final long startTime = System.currentTimeMillis();
56         final TextView companionTextView = (TextView) view.findViewById(R.id.companion_text);
57         mDailyChartView = (BatteryChartView) view.findViewById(R.id.daily_battery_chart);
58         mDailyChartView.setCompanionTextView(companionTextView);
59         mHourlyChartView = (BatteryChartView) view.findViewById(R.id.hourly_battery_chart);
60         mHourlyChartView.setCompanionTextView(companionTextView);
61         if (mChartPreferenceController != null) {
62             mChartPreferenceController.setBatteryChartView(mDailyChartView, mHourlyChartView);
63         }
64         BatteryUtils.logRuntime(TAG, "onBindViewHolder", startTime);
65     }
66 }
67