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.ColorDrawable;
21import android.graphics.drawable.Drawable;
22import android.preference.Preference;
23import android.view.View;
24import android.widget.ProgressBar;
25import android.widget.TextView;
26
27import com.android.settings.R;
28import com.android.settings.Utils;
29
30/**
31 * Custom preference for displaying power consumption as a bar and an icon on
32 * the left for the subsystem/app type.
33 */
34public class PowerGaugePreference extends Preference {
35    private BatteryEntry mInfo;
36    private int mProgress;
37    private CharSequence mProgressText;
38    private final CharSequence mContentDescription;
39
40    public PowerGaugePreference(Context context, Drawable icon, CharSequence contentDescription,
41            BatteryEntry info) {
42        super(context);
43        setLayoutResource(R.layout.preference_app_percentage);
44        setIcon(icon != null ? icon : new ColorDrawable(0));
45        mInfo = info;
46        mContentDescription = contentDescription;
47    }
48
49    public void setPercent(double percentOfMax, double percentOfTotal) {
50        mProgress = (int) Math.ceil(percentOfMax);
51        mProgressText = Utils.formatPercentage((int) (percentOfTotal + 0.5));
52        notifyChanged();
53    }
54
55    BatteryEntry getInfo() {
56        return mInfo;
57    }
58
59    @Override
60    protected void onBindView(View view) {
61        super.onBindView(view);
62
63        final ProgressBar progress = (ProgressBar) view.findViewById(android.R.id.progress);
64        progress.setProgress(mProgress);
65
66        final TextView text1 = (TextView) view.findViewById(android.R.id.text1);
67        text1.setText(mProgressText);
68
69        if (mContentDescription != null) {
70            final TextView titleView = (TextView) view.findViewById(android.R.id.title);
71            titleView.setContentDescription(mContentDescription);
72        }
73    }
74}
75