PowerGaugePreference.java revision ea38e678537cf740b5f30c1d69c7a332e98cdd2c
1/*
2 * Copyright (C) 2009 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
17package com.android.settings.fuelgauge;
18
19import android.content.Context;
20import android.graphics.drawable.Drawable;
21import android.preference.Preference;
22import android.view.View;
23import android.widget.ImageView;
24import android.widget.TextView;
25
26import com.android.settings.R;
27
28/**
29 * Custom preference for displaying power consumption as a bar and an icon on the left for the
30 * subsystem/app type.
31 *
32 */
33public class PowerGaugePreference extends Preference {
34
35    private Drawable mIcon;
36    private PercentageBar mGauge;
37    private double mValue;
38    private BatterySipper mInfo;
39    private double mPercent;
40
41    public PowerGaugePreference(Context context, Drawable icon, BatterySipper info) {
42        super(context);
43        setLayoutResource(R.layout.preference_powergauge);
44        mIcon = icon;
45        mGauge = new PercentageBar();
46        mGauge.bar = context.getResources().getDrawable(R.drawable.app_gauge);
47        mInfo = info;
48    }
49
50    /**
51     * Sets the width of the gauge in percentage (0 - 100)
52     * @param percent
53     */
54    void setGaugeValue(double percent) {
55        mValue = percent;
56        mGauge.percent = mValue;
57    }
58
59    void setPercent(double percent) {
60        mPercent = percent;
61    }
62
63    BatterySipper getInfo() {
64        return mInfo;
65    }
66
67    void setIcon(Drawable icon) {
68        mIcon = icon;
69        notifyChanged();
70    }
71
72    @Override
73    protected void onBindView(View view) {
74        super.onBindView(view);
75
76        ImageView appIcon = (ImageView) view.findViewById(R.id.appIcon);
77        if (mIcon == null) {
78            mIcon = getContext().getResources().getDrawable(android.R.drawable.sym_def_app_icon);
79        }
80        appIcon.setImageDrawable(mIcon);
81
82        ImageView appGauge = (ImageView) view.findViewById(R.id.appGauge);
83        appGauge.setImageDrawable(mGauge);
84
85        TextView percentView = (TextView) view.findViewById(R.id.percent);
86        percentView.setText((int) (Math.ceil(mPercent)) + "%");
87    }
88
89}
90