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