• 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.Bundle;
21 import android.os.SystemClock;
22 import android.support.annotation.VisibleForTesting;
23 import android.support.v7.preference.Preference;
24 import android.support.v7.preference.PreferenceViewHolder;
25 import android.util.AttributeSet;
26 import android.widget.TextView;
27 import com.android.internal.os.BatteryStatsHelper;
28 import com.android.settings.R;
29 import com.android.settings.Utils;
30 import com.android.settings.overlay.FeatureFactory;
31 import com.android.settingslib.BatteryInfo;
32 import com.android.settingslib.graph.UsageView;
33 
34 /**
35  * Custom preference for displaying power consumption as a bar and an icon on the left for the
36  * subsystem/app type.
37  */
38 public class BatteryHistoryPreference extends Preference {
39 
40     @VisibleForTesting
41     BatteryInfo mBatteryInfo;
42 
BatteryHistoryPreference(Context context, AttributeSet attrs)43     public BatteryHistoryPreference(Context context, AttributeSet attrs) {
44         super(context, attrs);
45         setLayoutResource(R.layout.battery_usage_graph);
46         setSelectable(false);
47     }
48 
setStats(BatteryStatsHelper batteryStats)49     public void setStats(BatteryStatsHelper batteryStats) {
50         final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
51         mBatteryInfo = BatteryInfo.getBatteryInfo(getContext(), batteryStats.getBatteryBroadcast(),
52                 batteryStats.getStats(), elapsedRealtimeUs);
53         notifyChanged();
54     }
55 
56     @Override
onBindViewHolder(PreferenceViewHolder view)57     public void onBindViewHolder(PreferenceViewHolder view) {
58         super.onBindViewHolder(view);
59         if (mBatteryInfo == null) {
60             return;
61         }
62 
63         ((TextView) view.findViewById(R.id.charge)).setText(mBatteryInfo.batteryPercentString);
64         UsageView usageView = (UsageView) view.findViewById(R.id.battery_usage);
65         usageView.findViewById(R.id.label_group).setAlpha(.7f);
66         mBatteryInfo.bindHistory(usageView);
67     }
68 }
69