1ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn/*
2ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn * Copyright (C) 2010 The Android Open Source Project
3ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn *
4ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn * Licensed under the Apache License, Version 2.0 (the "License");
5ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn * you may not use this file except in compliance with the License.
6ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn * You may obtain a copy of the License at
7ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn *
8ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn *      http://www.apache.org/licenses/LICENSE-2.0
9ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn *
10ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn * Unless required by applicable law or agreed to in writing, software
11ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn * distributed under the License is distributed on an "AS IS" BASIS,
12ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn * See the License for the specific language governing permissions and
14ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn * limitations under the License.
15ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn */
16ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn
17ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackbornpackage com.android.settings.fuelgauge;
18ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn
19ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackbornimport android.content.Context;
200be7598cb8c8cf6808b336204632615c5f26298fDianne Hackbornimport android.content.Intent;
21ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackbornimport android.graphics.drawable.Drawable;
22ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackbornimport android.os.BatteryStats;
23ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackbornimport android.preference.Preference;
24ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackbornimport android.view.View;
25c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackbornimport android.view.ViewGroup;
26ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackbornimport android.widget.ImageView;
27ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackbornimport android.widget.TextView;
28ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn
29ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackbornimport com.android.settings.R;
30ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn
31ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn/**
32ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn * Custom preference for displaying power consumption as a bar and an icon on the left for the
33ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn * subsystem/app type.
34ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn *
35ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn */
36ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackbornpublic class BatteryHistoryPreference extends Preference {
37ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn
380be7598cb8c8cf6808b336204632615c5f26298fDianne Hackborn    final private BatteryStats mStats;
390be7598cb8c8cf6808b336204632615c5f26298fDianne Hackborn    final private Intent mBatteryBroadcast;
40ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn
417158e6299210d08f82c29135c001652508b79dfbDianne Hackborn    private boolean mHideLabels;
427158e6299210d08f82c29135c001652508b79dfbDianne Hackborn    private View mLabelHeader;
43c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn    private BatteryHistoryChart mChart;
447158e6299210d08f82c29135c001652508b79dfbDianne Hackborn
450be7598cb8c8cf6808b336204632615c5f26298fDianne Hackborn    public BatteryHistoryPreference(Context context, BatteryStats stats, Intent batteryBroadcast) {
46ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn        super(context);
47ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn        setLayoutResource(R.layout.preference_batteryhistory);
48ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn        mStats = stats;
490be7598cb8c8cf6808b336204632615c5f26298fDianne Hackborn        mBatteryBroadcast = batteryBroadcast;
50ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn    }
51ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn
52ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn    BatteryStats getStats() {
53ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn        return mStats;
54ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn    }
55ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn
567158e6299210d08f82c29135c001652508b79dfbDianne Hackborn    public void setHideLabels(boolean hide) {
577158e6299210d08f82c29135c001652508b79dfbDianne Hackborn        if (mHideLabels != hide) {
587158e6299210d08f82c29135c001652508b79dfbDianne Hackborn            mHideLabels = hide;
597158e6299210d08f82c29135c001652508b79dfbDianne Hackborn            if (mLabelHeader != null) {
607158e6299210d08f82c29135c001652508b79dfbDianne Hackborn                mLabelHeader.setVisibility(hide ? View.GONE : View.VISIBLE);
617158e6299210d08f82c29135c001652508b79dfbDianne Hackborn            }
627158e6299210d08f82c29135c001652508b79dfbDianne Hackborn        }
637158e6299210d08f82c29135c001652508b79dfbDianne Hackborn    }
647158e6299210d08f82c29135c001652508b79dfbDianne Hackborn
65ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn    @Override
66ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn    protected void onBindView(View view) {
67ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn        super.onBindView(view);
68ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn
69ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn        BatteryHistoryChart chart = (BatteryHistoryChart)view.findViewById(
70ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn                R.id.battery_history_chart);
71c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn        if (mChart == null) {
72c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn            // First time: use and initialize this chart.
73c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn            chart.setStats(mStats, mBatteryBroadcast);
74c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn            mChart = chart;
75c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn        } else {
76c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn            // All future times: forget the newly inflated chart, re-use the
77c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn            // already initialized chart from last time.
78c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn            ViewGroup parent = (ViewGroup)chart.getParent();
79c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn            int index = parent.indexOfChild(chart);
80c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn            parent.removeViewAt(index);
81c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn            if (mChart.getParent() != null) {
82c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn                ((ViewGroup)mChart.getParent()).removeView(mChart);
83c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn            }
84c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn            parent.addView(mChart, index);
85c19eb361a407058b76cbbc866106db9fc81d9596Dianne Hackborn        }
867158e6299210d08f82c29135c001652508b79dfbDianne Hackborn        mLabelHeader = view.findViewById(R.id.labelsHeader);
877158e6299210d08f82c29135c001652508b79dfbDianne Hackborn        mLabelHeader.setVisibility(mHideLabels ? View.GONE : View.VISIBLE);
88ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn    }
89ea38e678537cf740b5f30c1d69c7a332e98cdd2cDianne Hackborn}
90