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